forked from dthies/moodle-block_deft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
234 lines (202 loc) · 6.2 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library functions for Deft.
*
* @package block_deft
* @copyright 2022 Daniel Thies <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/comment/lib.php');
require_once($CFG->dirroot . '/mod/lti/locallib.php');
use block_deft\output\view;
use block_deft\socket;
use block_deft\task;
/**
* Validate comment parameter before perform other comments actions
*
* @package block_deft
* @category comment
*
* @param stdClass $commentparam {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return boolean
*/
function block_deft_comment_validate($commentparam) {
if ($commentparam->commentarea != 'task') {
throw new comment_exception('invalidcommentarea');
}
$cache = cache::make('block_deft', 'tasks');
if (
(!$tasks = $cache->get($commentparam->context->instanceid))
|| (!$task = $tasks[$commentparam->itemid])
|| $task->type != 'comments'
) {
throw new comment_exception('invalidcommentitemid');
}
return true;
}
/**
* Running addtional permission check on plugins
*
* @package block_deft
* @category comment
*
* @param stdClass $args
* @return array
*/
function block_deft_comment_permissions($args) {
return [
'post' => true,
'view' => true,
];
}
/**
* Validate comment data before displaying comments
*
* @package block_deft
* @category comment
*
* @param stdClass $comments
* @param stdClass $args
* @return boolean
*/
function block_deft_comment_display($comments, $args) {
if ($args->commentarea != 'task') {
throw new comment_exception('invalidcommentarea');
}
$cache = cache::make('block_deft', 'tasks');
if (
(!$tasks = $cache->get($args->context->instanceid))
|| (!$task = $tasks[$args->itemid])
|| $task->type != 'comments'
) {
throw new comment_exception('invalidcommentitemid');
}
return $comments;
}
/**
* Serve the comments as a fragment.
*
* @param array $args List of named arguments for the fragment loader.
* @return string
*/
function block_deft_output_fragment_choose($args) {
global $DB, $USER;
$context = $args['context'];
$id = $args['id'];
$option = (string) $args['option'];
if ($context->contextlevel != CONTEXT_BLOCK) {
return null;
}
require_capability('block/deft:choose', $context);
$cache = cache::make('block_deft', 'tasks');
$tasks = $cache->get($context->instanceid);
$task = new task();
$task->from_record($tasks[$id]);
$config = $task->get_config();
if (!empty($task->get_state()->preventresponse)) {
return null;
}
$timenow = time();
$cache = cache::make('block_deft', 'results');
if ($cache->get($id . 'x' . $USER->id) === $config->option[$option]) {
return '';
} else if ($option == '') {
$DB->delete_records('block_deft_response', [
'task' => $id,
'userid' => $USER->id,
]);
} else if ($record = $DB->get_record('block_deft_response', ['task' => $id, 'userid' => $USER->id])) {
$record->response = $config->option[$option];
$record->timemodified = $timenow;
$DB->update_record('block_deft_response', $record);
} else {
$DB->insert_record('block_deft_response', [
'task' => $id,
'userid' => $USER->id,
'response' => $config->option[$option],
'timecreated' => $timenow,
'timemodified' => $timenow,
]);
}
// Clear the results cache.
$cache->delete($id);
$cache->delete($id . 'x' . $USER->id);
$cache->get($id);
$cache->get($id . 'x' . $USER->id);
if (!empty($task->get_state()->showsummary)) {
$socket = new socket($context);
$socket->dispatch();
}
return 'change';
}
/**
* Serve the comments as a fragment.
*
* @param array $args List of named arguments for the fragment loader.
* @return string
*/
function block_deft_output_fragment_content($args) {
global $OUTPUT;
$context = $args['context'];
if ($context->contextlevel != CONTEXT_BLOCK) {
return null;
}
$jsondata = json_decode($args['jsondata']);
$view = new view($context, $jsondata);
$data = $view->export_for_template($OUTPUT);
if (!empty($jsondata->lastmodified) && ($jsondata->lastmodified >= $data['lastmodified'])) {
return '';
}
return $OUTPUT->render_from_template('block_deft/view', $data);
}
/**
* Serve the comments as a fragment.
*
* @param array $args List of named arguments for the fragment loader.
* @return string
*/
function block_deft_output_fragment_test($args) {
$context = $args['context'];
if (!is_siteadmin()) {
return null;
}
$socket = new socket($context);
$socket->dispatch();
return get_string('messagesent', 'block_deft');
}
/**
* Callback to remove linked logins for deleted users.
*
* @param stdClass $user
*/
function block_deft_pre_user_delete($user) {
global $DB;
if (!$tasks = $DB->get_fieldset_select('block_deft_response', 'task', 'userid = ?', [$user->id])) {
return;
}
// Clear the results cache.
$cache = cache::make('block_deft', 'results');
$cache->delete_many($tasks);
$DB->delete_records('block_deft_response', ['userid' => $user->id]);
}