-
Notifications
You must be signed in to change notification settings - Fork 16
/
lib.php
637 lines (567 loc) · 20.9 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
<?php
// This file is part of the EQUELLA module - http://git.io/vUuof
//
// 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/>.
defined('MOODLE_INTERNAL') || die();
require_once ($CFG->dirroot . '/mod/equella/common/lib.php');
require_once ($CFG->dirroot . '/lib/filelib.php');
require_once ($CFG->dirroot . '/course/lib.php');
require_once ($CFG->libdir.'/gradelib.php');
require_once (dirname(__FILE__) . '/equella_rest_api.php');
define('EQUELLA_ITEM_TYPE', 'mod');
define('EQUELLA_ITEM_MODULE', 'equella');
define('EQUELLA_SOURCE', 'mod/equella');
// This must be FALSE in released code
define('EQUELLA_DEV_DEBUG_MODE', false);
define('EQUELLA_CONFIG_SELECT_RESTRICT_NONE', 'none');
define('EQUELLA_CONFIG_SELECT_RESTRICT_ITEMS_ONLY', 'itemonly');
define('EQUELLA_CONFIG_SELECT_RESTRICT_ATTACHMENTS_ONLY', 'attachmentonly');
define('EQUELLA_CONFIG_SELECT_RESTRICT_PACKAGES_ONLY', 'packageonly');
define('EQUELLA_CONFIG_INTERCEPT_NONE', 0);
define('EQUELLA_CONFIG_INTERCEPT_ASK', 1);
define('EQUELLA_CONFIG_INTERCEPT_FULL', 2);
define('EQUELLA_CONFIG_INTERCEPT_META', 3);
define('EQUELLA_ACTION_SELECTORADD', 'selectOrAdd');
define('EQUELLA_ACTION_STRUCTURED', 'structured');
// The default width is the size of equella resource page
define('EQUELLA_DEFAULT_WINDOW_WIDTH', 860);
define('EQUELLA_DEFAULT_WINDOW_HEIGHT', 450);
/**
* Returns the information if the module supports a feature
*
* @see plugin_supports() in lib/moodlelib.php
* @param string $feature FEATURE_xx constant for requested feature
* @return mixed true if the feature is supported, null if unknown
*/
function equella_supports($feature) {
switch($feature) {
case FEATURE_MOD_ARCHETYPE:
return MOD_ARCHETYPE_RESOURCE;
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
default:
return null;
}
}
function equella_get_window_options() {
global $CFG;
$width = EQUELLA_DEFAULT_WINDOW_WIDTH;
if (!empty($CFG->equella_default_window_width)) {
$width = $CFG->equella_default_window_width;
}
$height = EQUELLA_DEFAULT_WINDOW_HEIGHT;
if (!empty($CFG->equella_default_window_height)) {
$height = $CFG->equella_default_window_height;
}
return array('width' => $width,'height' => $height,'resizable' => 1,'scrollbars' => 1,'directories' => 0,'location' => 0,'menubar' => 0,'toolbar' => 0,'status' => 0);
}
/**
* Return course code (aka course id number)
*
* @param int $courseid
* @return string
*/
function equella_get_coursecode($courseid) {
global $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
return equella_get_course($courseid)->idnumber;
}
function equella_add_instance($equella, $mform = null) {
global $DB, $CFG;
$equella->timecreated = time();
$equella->timemodified = time();
if (!empty($CFG->equella_open_in_new_window)) {
$equella->windowpopup = 1;
}
$equella = equella_postprocess($equella);
return $DB->insert_record("equella", $equella);
}
/**
* Validate and process EQUELLA options
*
* @param stdClass $resource
* @return stdClass
*/
function equella_postprocess($resource) {
if (!empty($resource->windowpopup)) {
$optionlist = array();
foreach(equella_get_window_options() as $option => $value) {
if (($option == 'width' or $option == 'height') and empty($resource->$option)) {
$resource->$option = $value;
}
if (isset($resource->$option)) {
$optionlist[] = ($option . "=" . $resource->$option);
unset($resource->$option);
} else {
$optionlist[] = ($option . "=" . $value);
}
}
$resource->popup = implode(',', $optionlist);
unset($resource->windowpopup);
} else {
$resource->popup = '';
}
$pattern = "/(?P<uuid>[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})\/(?P<version>[0-9]*)\/(?P<path>.*)/";
$url = $resource->url;
preg_match($pattern, $url, $matches);
if (!empty($matches['uuid'])) {
$resource->uuid = $matches['uuid'];
}
// version could be 0, so don't test it with !empty()
if (isset($matches['version'])) {
$resource->version = $matches['version'];
}
if (!empty($matches['path'])) {
$resource->path = $matches['path'];
}
return $resource;
}
function equella_update_instance($equella) {
global $DB;
// Given an object containing all the necessary data,
// will update an existing instance with new data.
$equella->timemodified = time();
$equella->id = $equella->instance;
$equella = equella_postprocess($equella);
return $DB->update_record("equella", $equella);
}
function equella_delete_instance($id) {
global $DB, $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
if (!$equella = equella_get_activity($id, false)) {
return false;
}
if ($equella->activation) {
$url = str_replace("signon.do", "access/activationwebservice.do", $CFG->equella_url);
$url = equella_appendtoken($url) . "&activationUuid=" . rawurlencode($equella->activation);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($curl);
curl_close($curl);
}
$DB->delete_records("equella", array("id" => $equella->id));
return true;
}
/**
* Given a coursemodule object, this function returns the extra
* information needed to print this activity in various places.
*
* @param cm_info $coursemodule
* @return cached_cm_info info
*/
function equella_get_coursemodule_info($coursemodule) {
global $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
$info = new cached_cm_info();
if ($resource = equella_get_activity($coursemodule->instance, false)) {
$info->icon = equella_guess_icon($resource, 24);
if ($coursemodule->showdescription) {
$info->content = format_module_intro('equella', $resource, $coursemodule->id, false);
}
if (!empty($resource->popup)) {
$url = new moodle_url('/mod/equella/popup.php', array('cmid' => $coursemodule->id));
$url = $url->out(false);
$info->onclick = "window.open('{$url}', '','{$resource->popup}'); return false;";
}
}
return $info;
}
/**
* Optimised mimetype detection from general URL.
* Copied from /mod/url/locallib.php
*
* @param $equella stdclass
* @param $size int
* @return string
*/
function equella_guess_icon($equella, $size = 24) {
global $CFG;
$icon = null;
if (!empty($equella->filename)) {
if ('document/unknown' != mimeinfo('type', $equella->filename)) {
$icon = 'f/' . mimeinfo('icon' . $size, $equella->filename);
}
}
if (empty($icon)) {
$mimetype = $equella->mimetype;
$icon = file_mimetype_icon($mimetype, $size);
}
return $icon;
}
/**
* Capture moodle files in modules
*
* @param stdClass $event
* @param array
*/
function equella_capture_files($event) {
global $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
$fs = get_file_storage();
if (!$cm = get_coursemodule_from_id($event->other['modulename'], $event->objectid)) {
return array();
}
if (!$course = equella_get_course($cm->course, false)) {
}
$context = context_module::instance($cm->id);
if ($event->other['modulename'] != 'folder' && $event->other['modulename'] != 'resource') {
return array();
}
require_once ($CFG->dirroot . '/mod/' . $event->other['modulename'] . '/lib.php');
// find out all supported areas
$functionname = 'mod_' . $event->other['modulename'] . '_get_file_areas';
$functionname_old = $event->other['modulename'] . '_get_file_areas';
if (function_exists($functionname)) {
$areas = $functionname($course, $cm, $context);
} else if (function_exists($functionname_old)) {
$areas = $functionname_old($course, $cm, $context);
} else {
$areas = array();
}
$files = array();
foreach($areas as $area => $name) {
$area_files = $fs->get_area_files($context->id, 'mod_' . $event->other['modulename'], $area, false, 'sortorder, itemid', false);
$files = array_merge($files, $area_files);
}
return $files;
}
function equella_find_repository() {
global $CFG;
require_once ($CFG->dirroot . '/repository/lib.php');
$instances = repository::get_instances(array('type' => 'equella'));
foreach($instances as $e) {
if ($e->get_option('equella_url') == $CFG->equella_url) {
return $e;
}
}
return null;
}
function equella_replace_contents_with_references($file, $info) {
if (empty($info->attachments)) {
return;
}
$items = $info->attachments;
$fs = get_file_storage();
// replace moodle files with equella references
if ($equellarepository = equella_find_repository()) {
$item = array_pop($items);
$repositoryid = $equellarepository->id;
$record = new stdClass();
$record->filepath = $file->get_filepath();
$record->filename = $item->filename;
$record->component = $file->get_component();
$record->filearea = $file->get_filearea();
$record->itemid = $file->get_itemid();
$record->license = $file->get_license();
$record->source = $source = base64_encode(serialize((object)array('url' => $item->links->view,'filename' => $item->filename)));
$record->contextid = $file->get_contextid();
$record->userid = $file->get_userid();
$now = time();
$record->timecreated = $now;
$record->timemodified = $now;
$reference = $equellarepository->get_file_reference($source);
$record->referencelifetime = $equellarepository->get_reference_file_lifetime($reference);
// delete before create reference to avoid pathnamehash conflict
$file->delete();
$fs->create_file_from_reference($record, $repositoryid, $reference);
}
}
function equella_module_event_handler($event) {
global $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
if (empty($CFG->equella_intercept_files)) {
return;
}
if ((int)$CFG->equella_intercept_files != EQUELLA_CONFIG_INTERCEPT_FULL) {
return;
}
$course = equella_get_course($event->courseid);
$params = array();
equella_add_lmsinfo_parameters($params, $course, 'quick');
$params['moodle/module/type'] = $event->other['modulename'];
$params['moodle/module/name'] = $event->other['name'];
$params['moodle/module/id'] = $event->objectid;
// Legacy. Deprecated
$params['moodlemoduletype'] = $event->other['modulename'];
$params['moodlemodulename'] = $event->other['name'];
$params['moodlemoduleid'] = $event->objectid;
$params['moodlecoursefullname'] = $course->fullname;
$params['moodlecourseshortname'] = $course->shortname;
$params['moodlecourseid'] = $course->id;
$params['moodlecourseidnumber'] = $course->idnumber;
$files = equella_capture_files($event);
foreach($files as $file) {
$handle = $file->get_content_file_handle();
equella_add_fileinfo_parameters($params, $file);
// pushing files to equella
$info = equella_rest_api::contribute_file_with_shared_secret($file->get_filename(), $handle, $params);
// replace contents
equella_replace_contents_with_references($file, $info);
}
return true;
}
/**
* Handle module created event
*/
function equella_handle_mod_created($event) {
return equella_module_event_handler($event);
}
/**
* Handle module updated event
*/
function equella_handle_mod_updated($event) {
return equella_module_event_handler($event);
}
/**
* Register the ability to handle drag and drop file uploads
*
* @return array containing details of the files / types the mod can handle
*/
if (isset($CFG->equella_intercept_files) && (int)$CFG->equella_intercept_files == EQUELLA_CONFIG_INTERCEPT_ASK) {
function equella_dndupload_register() {
return array('files' => array(array('extension' => '*','message' => get_string('dnduploadresource', 'mod_equella'))));
}
}
/**
* Register the ability to handle drag and drop file uploads with meta data
*
* @return array containing details of the files / types the mod can handle
*/
if (isset($CFG->equella_intercept_files) && (int)$CFG->equella_intercept_files == EQUELLA_CONFIG_INTERCEPT_META) {
function equella_dndupload_register() {
global $PAGE, $CFG, $COURSE;
$config = [
[
'courseid' => $COURSE->id,
'maxbytes' => get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes)
]
];
$PAGE->requires->yui_module('moodle-mod_equella-dndupload', 'M.mod_equella.dndupload.init', $config);
return array('files' => array(
array('extension' => '*', 'message' => get_string('dnduploadresourcemetadata', 'mod_equella'))
));
}
}
//https://github.com/equella/moodle-mod_equella/issues/60
if (isset($CFG->equella_intercept_files) && (int)$CFG->equella_intercept_files == EQUELLA_CONFIG_INTERCEPT_NONE) {
function equella_dndupload_register() {
return null;
}
}
/**
* Handle a file that has been uploaded
*
* @param object $uploadinfo details of the file / content that has been uploaded
* @return int instance id of the newly created mod
*/
function equella_dndupload_handle($uploadinfo) {
global $USER, $CFG;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
$fs = get_file_storage();
// Gather the required info.
$courseid = $uploadinfo->course->id;
$coursemodule = $uploadinfo->coursemodule;
$usercontext = context_user::instance($USER->id);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $uploadinfo->draftitemid, 'id', false);
$moduleid = null;
foreach($draftfiles as $file) {
$handle = $file->get_content_file_handle();
// pushing files to equella
$params = array();
equella_add_lmsinfo_parameters($params, $uploadinfo->course, 'quick');
/*
$params['moodle/module/type'] = $event->other['modulename'];
$params['moodle/module/name'] = $event->other['name'];
$params['moodle/module/id'] = $event->objectid;
// Legacy. Deprecated
$params['moodlemoduletype'] = $event->other['modulename'];
$params['moodlemodulename'] = $event->other['name'];
$params['moodlemoduleid'] = $event->objectid;
$params['moodlecoursefullname'] = $course->fullname;
$params['moodlecourseshortname'] = $course->shortname;
$params['moodlecourseid'] = $course->id;
$params['moodlecourseidnumber'] = $course->idnumber;
*/
equella_add_fileinfo_parameters($params, $file);
$mimetype = $file->get_mimetype();
if (isset($uploadinfo->displayname)) {
$params['item/title'] = $uploadinfo->displayname;
// Legacy. Deprecated
$params['displayname'] = $uploadinfo->displayname;
}
if (isset($uploadinfo->itemdescription)) {
$params['item/description'] = $uploadinfo->itemdescription;
// Legacy. Deprecated
$params['itemdescription'] = $uploadinfo->itemdescription;
}
if (isset($uploadinfo->copyright)){
$params['item/iscopyright'] = $uploadinfo->copyright;
// Legacy. Deprecated
$params['copyrightflag'] = $uploadinfo->copyright;
}
if (isset($uploadinfo->itemkeyword)) {
$params['item/keyword'] = $uploadinfo->itemkeyword;
// Legacy. Deprecated
$params['itemkeyword'] = $uploadinfo->itemkeyword;
}
$info = equella_rest_api::contribute_file_with_shared_secret($file->get_filename(), $handle, $params);
if (isset($info->error)) {
throw new equella_exception($info->error_description);
}
$modulename = '';
if (!empty($info->name)) {
$modulename = $info->name;
} else {
if (!empty($info->description)) {
$modulename = $info->description;
} else {
$modulename = $info->uuid;
}
}
$eqresource = new stdClass;
$eqresource->course = $courseid;
$eqresource->name = $modulename;
$eqresource->intro = $info->description;
$eqresource->introformat = FORMAT_HTML;
$eqresource->mimetype = $mimetype;
$eqresource->filename = $file->get_filename();
$item = array_pop($info->attachments);
$eqresource->attachmentuuid = $item->uuid;
$eqresource->url = $item->links->view;
try {
$eqresourceid = equella_add_instance($eqresource, null);
} catch(Exception $ex) {
throw new equella_exception('Failed to create EQUELLA resource.');
}
}
return $eqresourceid;
}
class equella_exception extends Exception {
function __construct($message, $debuginfo = null) {
global $CFG;
parent::__construct($message, 0);
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
equella_debug_log($debuginfo);
}
}
/**
*
* @return array
*/
function equella_get_view_actions() {
return array('view all','view','view equella resource');
}
/**
*
* @return array
*/
function equella_get_post_actions() {
return array('add','update','delete','update equella resource','delete equella resource','add equella resource');
}
/**
* Get equella module recent activities
*
* @global object
* @global object
* @global object
* @global object
* @uses CONTEXT_MODULE
* @param array $activities Passed by reference
* @param int $index Passed by reference
* @param int $timemodified Timestamp
* @param int $courseid
* @param int $cmid
* @param int $userid
* @param int $groupid
* @return void
*/
function equella_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
global $CFG, $COURSE;
require_once ($CFG->dirroot . '/mod/equella/locallib.php');
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = equella_get_course($courseid);
}
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->cms[$cmid];
$equella = equella_get_activity($cm->instance, true);
if ($equella->timemodified < $timestart) {
// Remove the activity
unset($activities[$index--]);
}
return;
}
/**
* Create grade item for given equella.
*
* @param stdClass $equella record
* @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function equella_grade_item_update($eq, $grades=null) {
global $CFG;
if (!isset($eq->courseid)) {
$eq->courseid = $eq->course;
}
$params = array('itemname'=>$eq->name, 'idnumber'=>$eq->cmidnumber);
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update(EQUELLA_SOURCE,
$eq->courseid,
EQUELLA_ITEM_TYPE,
EQUELLA_ITEM_MODULE,
$eq->id,
0,
$grades,
$params);
}
function equella_get_user_grades($equella, $userid = 0) {
$grades = grade_get_grades($equella->course, EQUELLA_ITEM_TYPE, EQUELLA_ITEM_MODULE,
$equella->id, $userid);
if (isset($grades) && isset($grades->items[0]) && is_array($grades->items[0]->grades)) {
foreach($grades->items[0]->grades as $agrade) {
$grade = $agrade->grade;
return $grade;
break;
}
}
return null;
}
/**
* delete all grades
*/
function equella_grade_item_delete($eq) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update(EQUELLA_SOURCE, $eq->courseid, EQUELLA_ITEM_TYPE, EQUELLA_ITEM_MODULE, $eq->id, 0, NULL, array('deleted'=>1));
}