-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
executable file
·381 lines (332 loc) · 12.3 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?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 of interface functions and constants for module consentform
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
*
* All the consentform specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_consentform
* @copyright 2020 Thomas Niedermaier, Medical University of Vienna <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('EXPECTEDCOMPLETIONVALUE', 1);
define('GRADEVALUETOWRITE', 1);
define('CONSENTFORM_STATUS_AGREED', 1);
define('CONSENTFORM_STATUS_REVOKED', 0);
define('CONSENTFORM_STATUS_REFUSED', -1);
define('CONSENTFORM_STATUS_NOACTION', 2);
define('CONSENTFORM_ALL', 99);
define('CONSENTFORM_NOTIMESTAMP', '-');
/* Moodle core API */
/**
* Returns the information on whether the module supports a feature
*
* See plugin_supports() for more info.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function consentform_supports($feature) {
switch($feature) {
case FEATURE_MOD_INTRO:
return false;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GROUPS:
return false;
case FEATURE_GROUPINGS:
return false;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_ADMINISTRATION;
default:
return null;
}
}
/**
* Saves a new instance of the consentform into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param stdClass $consentform Submitted data from the form in mod_form.php
* @param mod_consentform_mod_form $mform The form instance itself (if needed)
* @return int The id of the newly inserted consentform record
*/
function consentform_add_instance(stdClass $consentform, mod_consentform_mod_form $mform = null) {
global $DB, $CFG;
$consentform->timecreated = time();
$consentform->id = $DB->insert_record('consentform', $consentform);
if ($consentform->confirmincourseoverview) {
$iframeparms = array();
$url = $CFG->wwwroot."/mod/consentform/confirmation.php?id=".$consentform->id;
$iframeparms["src"] = $url;
$iframeparms["scrolling"] = "no";
$js = "this.style.height=this.contentWindow.document.documentElement.scrollHeight + 'px';";
$js .= "this.contentWindow.document.getElementById('page').style.marginTop='0px';";
$iframeparms["onload"] = $js;
$iframeparms["frameborder"] = "0";
$iframeparms["class"] = "w-75";
$iframeparms["name"] = "consentformiframe$consentform->id";
$html = html_writer::tag("iframe", null, $iframeparms);
$consentformintro = $html;
$DB->set_field("consentform", "intro", $consentformintro, array("id" => $consentform->id));
}
if ($consentform->usegrade) {
consentform_grade_item_update($consentform);
}
return $consentform->id;
}
/**
* Updates an instance of the consentform in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param stdClass $consentform An object from the form in mod_form.php
* @param mod_consentform_mod_form $mform The form instance itself (if needed)
* @return boolean Success/Fail
*/
function consentform_update_instance(stdClass $consentform, mod_consentform_mod_form $mform = null) {
global $DB;
$consentform->timemodified = time();
$consentform->completionunlocked = false;
$consentform->id = $consentform->instance;
$consentformdb = $DB->get_record('consentform', ["id" => $consentform->id]);
if ($consentformdb->usegrade != $consentform->usegrade) {
if ($consentform->usegrade) {
consentform_grade_item_update($consentform);
consentform_usegradechange_writegrades($consentform);
} else {
consentform_grade_item_delete($consentform);
}
}
$result = $DB->update_record('consentform', $consentform);
return $result;
}
/**
* Removes an instance of the consentform from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
* @throws dml_exception
* @throws coding_exception
*/
function consentform_delete_instance($id) {
global $DB;
// Get cm and consentform.
$cm = get_coursemodule_from_instance('consentform', $id);
if (!$consentform = $DB->get_record('consentform', array('id' => $id))) {
return false;
}
// Delete dataset in consentform and dependent entries in consentform_state.
$DB->delete_records('consentform', array('id' => $consentform->id));
$DB->delete_records('consentform_state', array('consentformcmid' => $cm->id));
consentform_grade_item_update($consentform);
rebuild_course_cache($consentform->course, false);
return true;
}
/* Gradebook API */
/**
* Creates or updates grade item for the given consentform instance
*
* Needed by grade_update_mod_grades().
*
* @param stdClass $consentform instance object with extra cmidnumber and modname property
* @param grade_item $grades reset grades in the gradebook
* @return void
*/
function consentform_grade_item_update(stdClass $consentform, $grades=null) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
$item = array();
$item['itemname'] = clean_param($consentform->name, PARAM_NOTAGS);
$item['gradetype'] = GRADE_TYPE_VALUE;
$item['grademax'] = 1;
$item['grademin'] = 0;
if ($grades === 'reset') {
$item['reset'] = true;
$grades = null;
}
grade_update('mod/consentform', $consentform->course, 'mod', 'consentform',
$consentform->id, 0, $grades, $item);
}
/**
* Delete grade item for given consentform instance
*
* @param stdClass $consentform instance object
* @return grade_item
*/
function consentform_grade_item_delete($consentform) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/consentform', $consentform->course, 'mod', 'consentform',
$consentform->id, 0, null, array('deleted' => 1));
}
/**
* Update consentform grades in the gradebook
*
* Needed by grade_update_mod_grades().
*
* @param stdClass $consentform instance object with extra cmidnumber and modname property
* @param int $userid update grade of specific user only, 0 means all participants
* @throws coding_exception
*/
function consentform_update_grades(stdClass $consentform, $userid = 0) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
// Populate array of grade objects indexed by userid.
$grades = array();
grade_update('mod/consentform', $consentform->course, 'mod', 'consentform', $consentform->id, 0, $grades);
}
/**
* Set user's grade according to reaction
*
* @param object $consentform
* @param int $userid
* @param bool $agreed
* @return false|void
*/
function consentform_set_user_grade($consentform, $userid, $agreed=true) {
if ($userid) {
$grade = new stdClass();
$grade->userid = $userid;
if ($agreed) {
$grade->rawgrade = 1;
} else {
$grade->rawgrade = 0;
}
$time = time();
$grade->dategraded = $time;
$grade->datesubmitted = $time;
return consentform_grade_item_update($consentform, $grade);
} else {
return false;
}
}
/**
* Called by course/reset.php
*
* @param \moodle_form $mform
* @throws coding_exception
*/
function consentform_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'consentformheader', get_string('modulenameplural', 'consentform'));
$mform->addElement('checkbox', 'reset_consentform', get_string('resetconsentform', 'consentform'));
}
/**
* Reset consentform state userdata
*
* @param object $data
* @return array
* @throws coding_exception
* @throws dml_exception
*/
function consentform_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'consentform');
$status = array();
if (!empty($data->reset_consentform)) {
$consentformmoduleid = $DB->get_field('modules', 'id', array('name' => 'consentform'));
$cms = $DB->get_records('course_modules', array('course' => $data->courseid, 'module' => $consentformmoduleid));
foreach ($cms as $cm) {
$DB->delete_records('consentform_state', array('consentformcmid' => $cm->id));
$consentform = $DB->get_record('consentform', array('id' => $cm->instance), '*');
consentform_grade_item_delete($consentform);
}
$status[] = array('component' => $componentstr, 'item' => get_string('resetok', 'consentform'), 'error' => false);
}
return $status;
}
/**
* Reset consentform instance
*
* @param object $course
* @return int[]
*/
function consentform_reset_course_form_defaults($course) {
return array('reset_consentform' => 1);
}
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settingsnav The settings navigation object
* @param navigation_node $consentformnode The node to add module settings to
* @throws coding_exception
* @throws moodle_exception
*/
function consentform_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $consentformnode) {
global $DB;
if (empty($settingsnav->get_page()->cm->context)) {
$settingsnav->get_page()->cm->context = context_module::instance($settingsnav->get_page()->cm->instance);
}
if (!(has_capability('mod/consentform:submit', $settingsnav->get_page()->context) || is_siteadmin())) {
return;
}
// Find appropriate key where our link should come. Probably won't work, but at least try.
$keys = [
'contextlocking' => navigation_node::TYPE_SETTING
];
$beforekey = null;
foreach ($keys as $key => $type) {
if ($foundnode = $consentformnode->find($key, $type)) {
$beforekey = $key;
break;
}
}
$instanceid = $settingsnav->get_page()->cm->instance;
if (!$DB->get_field('consentform', 'nocoursemoduleslist', array('id' => $instanceid))) {
$url = new moodle_url('/mod/consentform/modulelist.php', array('id' => $settingsnav->get_page()->cm->id));
$title = get_string('dependencies', 'mod_consentform');
$childnode = navigation_node::create(
$title,
$url,
navigation_node::TYPE_SETTING,
'dependencies',
'modulelist'
);
$consentformnode->add_node($childnode, $beforekey);
}
$url = new moodle_url('/mod/consentform/listusers.php', array('id' => $settingsnav->get_page()->cm->id));
$title = get_string('listusers', 'consentform');
$childnode = navigation_node::create(
$title,
$url,
navigation_node::TYPE_SETTING,
'agreements',
'listusers'
);
$node = $consentformnode->add_node($childnode, $beforekey);
}