-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.php
More file actions
443 lines (370 loc) · 13.7 KB
/
lib.php
File metadata and controls
443 lines (370 loc) · 13.7 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
// This file is part of mod_openbook for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* Contains interface and callback methods for mod_openbook
*
* @package mod_openbook
* @author University of Geneva, E-Learning Team
* @author Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @copyright 2025 University of Geneva {@link http://www.unige.ch}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/locallib.php');
/**
* Adds a new openbook instance
*
* @param stdClass $openbook data (from mod_openbook_mod_form)
* @return int openbook ID
*/
function openbook_add_instance($openbook) {
global $DB, $OUTPUT;
$cmid = $openbook->coursemodule;
$courseid = $openbook->course;
$id = 0;
try {
$id = $DB->insert_record('openbook', $openbook);
} catch (Exception $e) {
echo $OUTPUT->notification($e->getMessage(), 'error');
}
$DB->set_field('course_modules', 'instance', $id, ['id' => $cmid]);
$record = $DB->get_record('openbook', ['id' => $id]);
$record->course = $courseid;
$record->cmid = $cmid;
$course = $DB->get_record('course', ['id' => $record->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_id('openbook', $cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$instance = new openbook($cm, $course, $context);
$instance->update_calendar_event();
return $record->id;
}
// For versions of Moodle prior to 5.1, we need to define that constant here.
if (!defined('FEATURE_MOD_OTHERPURPOSE')) {
define('FEATURE_MOD_OTHERPURPOSE', 'mod_otherpurpose');
}
/**
* Return the list if Moodle features this module supports
*
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed True if module supports feature, null if doesn't know
*/
function openbook_supports($feature) {
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_GRADE_HAS_GRADE:
return false;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_IDNUMBER:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_CONTENT;
case FEATURE_MOD_OTHERPURPOSE:
return MOD_PURPOSE_COLLABORATION;
default:
return null;
}
}
/**
* updates an existing openbook instance
*
* @param stdClass $openbook from mod_openbook_mod_form
* @return bool true
*/
function openbook_update_instance($openbook) {
global $DB;
if ($openbook->filesarepersonal == 1) {
$openbook->obtainstudentapproval = "0";
}
$openbook->id = $openbook->instance;
$openbook->timemodified = time();
$DB->update_record('openbook', $openbook);
$course = $DB->get_record('course', ['id' => $openbook->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('openbook', $openbook->id, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$instance = new openbook($cm, $course, $context);
$approvalreseted = false;
// If the files are personal, reset the student approval.
if ($openbook->filesarepersonal == 1) {
$approvalreseted = $instance->reset_all_studentapproval();
}
$instance->update_calendar_event();
return true;
}
/**
* Completely deletes an openbook instance
*
* @param int $id
* @return bool
*/
function openbook_delete_instance($id) {
global $DB;
if (!$openbook = $DB->get_record('openbook', ['id' => $id])) {
return false;
}
$fs = get_file_storage();
$fs->delete_area_files($openbook->id, 'mod_openbook', 'attachment');
$DB->delete_records('openbook_file', ['openbook' => $openbook->id]);
$DB->delete_records('event', ['modulename' => 'openbook', 'instance' => $openbook->id]);
$tableuniqueid = \mod_openbook\local\allfilestable\base::get_table_uniqueid($id);
$DB->delete_records('user_preferences', ['name' => $tableuniqueid]);
$filteruserpreference = 'mod-openbook-perpage-' . $id;
$DB->delete_records('user_preferences', ['name' => $filteruserpreference]);
$result = true;
if (!$DB->delete_records('openbook', ['id' => $openbook->id])) {
$result = false;
}
return $result;
}
/**
* Returns info object about the course module
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return bool|cached_cm_info An object on information that the courses will know about (most noticeably, an icon) or false.
*/
function openbook_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, alwaysshowdescription, allowsubmissionsfromdate, intro, introformat, completionupload';
if (!$openbook = $DB->get_record('openbook', $dbparams, $fields)) {
return false;
}
$result = new cached_cm_info();
$result->name = $openbook->name;
if ($coursemodule->showdescription) {
if ($openbook->alwaysshowdescription || time() > $openbook->allowsubmissionsfromdate) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$result->content = format_module_intro('openbook', $openbook, $coursemodule->id, false);
}
}
// Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionupload'] = $openbook->completionupload;
}
return $result;
}
/**
* Defines which elements mod_openbook needs to add to reset form
*
* @param MoodleQuickForm $mform The reset course form to extend
*/
function openbook_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'openbookheader', get_string('modulenameplural', 'openbook'));
$mform->addElement('checkbox', 'reset_openbook_userdata', get_string('reset_userdata', 'openbook'));
}
/**
* Reset the userdata in openbook module
*
* @param object $data settings object which userdata to reset
* @return array[] array of associative arrays giving feedback what has been successfully reset
*/
function openbook_reset_userdata($data) {
global $DB;
if (!$DB->count_records('openbook', ['course' => $data->courseid])) {
return [];
}
$componentstr = get_string('modulenameplural', 'openbook');
$status = [];
if (isset($data->reset_openbook_userdata)) {
$openbooks = $DB->get_records('openbook', ['course' => $data->courseid]);
foreach ($openbooks as $openbook) {
$filerecords = $DB->get_records('openbook_file', ['openbook' => $openbook->id]);
$fs = get_file_storage();
foreach ($filerecords as $filerecord) {
if ($file = $fs->get_file_by_id($filerecord->fileid)) {
$file->delete();
}
}
$DB->delete_records('openbook_file', ['openbook' => $openbook->id]);
$status[] = [
'component' => $componentstr,
'item' => $openbook->name,
'error' => false,
];
}
}
return $status;
}
/**
* extend an assigment navigation settings
*
* @param settings_navigation $settings
* @param navigation_node $navref
* @return void
*/
function openbook_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/openbook/locallib.php');
// We want to add these new nodes after the Edit settings node, and before the
// Locally assigned roles node. Of course, both of those are controlled by capabilities.
$keys = $navref->get_children_key_list();
$beforekey = null;
$i = array_search('modedit', $keys);
if ($i === false && array_key_exists(0, $keys)) {
$beforekey = $keys[0];
} else if (array_key_exists($i + 1, $keys)) {
$beforekey = $keys[$i + 1];
}
$cm = $settings->get_page()->cm;
if (!$cm) {
return;
}
$context = $cm->context;
$course = $settings->get_page()->course;
if (!$course) {
return;
}
if (has_capability('mod/openbook:addinstance', $settings->get_page()->cm->context)) {
$url = new moodle_url('/mod/openbook/view.php', ['id' => $settings->get_page()->cm->id, 'allfilespage' => '1']);
$node = navigation_node::create(
get_string('allfiles', 'openbook'),
$url,
navigation_node::TYPE_SETTING,
null,
'mod_openbook_allfiles'
);
$navref->add_node($node, $beforekey);
}
if (has_capability('mod/openbook:manageoverrides', $settings->get_page()->cm->context)) {
$url = new moodle_url('/mod/openbook/overrides.php', ['id' => $settings->get_page()->cm->id]);
$node = navigation_node::create(
get_string('overrides', 'assign'),
$url,
navigation_node::TYPE_SETTING,
null,
'mod_openbook_useroverrides'
);
$navref->add_node($node, $beforekey);
}
}
/**
* Serves resource files.
*
* @param mixed $course course or id of the course
* @param mixed $cm course module or id of the course module
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - just send the file
*/
function mod_openbook_pluginfile(
$course,
$cm,
context $context,
$filearea,
$args,
$forcedownload,
array $options = []
) {
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login($course, false, $cm);
if (!has_capability('mod/openbook:view', $context)) {
return false;
}
if ($filearea !== 'attachment' && $filearea !== 'commonteacherfiles') {
return false;
}
$itemid = (int)array_shift($args);
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/mod_openbook/$filearea/$itemid/$relativepath";
$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, true, $options);
// Wont be reached!
return false;
}
/**
* Callback for block_myoverview which will decide whether it will be shown in the overview
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
*/
function mod_openbook_core_calendar_provide_event_action(
calendar_event $event,
\core_calendar\action_factory $factory
) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . '/mod/openbook/locallib.php');
// Get the instance of the openbook with the way recommended by the docs.
$courseinstance = get_fast_modinfo($event->courseid)->instances['openbook'][$event->instance];
$instance = new openbook($courseinstance);
// Only show this instance if it's open.
if ($instance->is_open()) {
// Also don't show this instance when the user already uploaded one or more files.
$files = $DB->count_records('openbook_file', ['openbook' => $event->instance,
'userid' => $USER->id]);
if ($files >= 1) {
return null;
}
return $factory->create_instance(
get_string('add_uploads', 'openbook'), // Name of the action button.
new \moodle_url(
'/mod/openbook/view.php',
['id' => $courseinstance->id],
), // URL of the instance.
1, // Count of necessary actions.
true, // Whether the user can take action on this folder.
);
}
}
/**
* Callback which returns human-readable strings describing the active completion custom rules for the module instance.
*
* @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
* @return array $descriptions the array of descriptions for the custom rules.
*/
function mod_openbook_get_completion_active_rule_descriptions($cm) {
// Values will be present in cm_info, and we assume these are up to date.
if (
empty($cm->customdata['customcompletionrules'])
|| $cm->completion != COMPLETION_TRACKING_AUTOMATIC
) {
return [];
}
$descriptions = [];
foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
switch ($key) {
case 'completionupload':
if (!empty($val)) {
$descriptions[] = get_string('completionupload', 'openbook');
}
break;
default:
break;
}
}
return $descriptions;
}