forked from Annoto/moodle-local_annoto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
executable file
·764 lines (674 loc) · 26.8 KB
/
lib.php
File metadata and controls
executable file
·764 lines (674 loc) · 26.8 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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<?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/>.
/**
* Local plugin "Annoto" - Library
* @package local_annoto
* @copyright Annoto Ltd.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if (!defined('USREGION')) {
define('USREGION', 'us.annoto.net');
}
if (!defined('EUREGION')) {
define('EUREGION', 'eu.annoto.net');
}
if (!defined('CUSTOM')) {
define('CUSTOM', 'custom');
}
if (!defined('DEFAULTWIDTH')) {
define('DEFAULTWIDTH', 854);
}
if (!defined('DEFAULTHEIGHT')) {
define('DEFAULTHEIGHT', 480);
}
if (!defined('LOCAL_ANNOTO_TOP_OF_BODY_THEMES')) {
define('LOCAL_ANNOTO_TOP_OF_BODY_THEMES', 'lambda,adaptable,academi');
}
require_once($CFG->libdir . '/completionlib.php');
require_once(__DIR__ . '/classes/completion.php');
require_once(__DIR__ . '/classes/completiondata.php');
require_once(__DIR__ . '/classes/log.php');
use local_annoto\log;
use local_annoto\annoto_completion;
use local_annoto\annoto_completiondata;
/**
* Function allows plugins to injecting JS across the site, like analytics.
*
*/
function local_annoto_before_footer() {
local_annoto_init();
return '';
}
/**
* Function Insert a chunk of html at the start of the html document.
* @return string HTML fragment.
*/
function local_annoto_before_standard_top_of_body_html() {
global $PAGE;
// Prevent callback loading for all themes except those:.
$themes = explode(',', LOCAL_ANNOTO_TOP_OF_BODY_THEMES);
if (in_array($PAGE->theme->name, $themes)) {
local_annoto_init();
}
return '';
}
/**
* Function init plugin according to the proper environment conditions.
* @return boolean
*/
function local_annoto_init() {
global $PAGE, $COURSE;
$istargetpage = false;
$possiblepages = [
'mod-',
'course-view-',
'blocks-',
];
foreach ($possiblepages as $possiblepage) {
if ((strpos($PAGE->pagetype, $possiblepage) !== false)) {
$istargetpage = true;
break;
}
}
// Start local_annoto on a specific pages only.
local_annoto_set_jslog('Page ' . $istargetpage);
if ($istargetpage) {
$courseid = $COURSE->id;
$modid = 0;
if (isset($PAGE->cm->id)) {
$modid = (int)$PAGE->cm->id;
}
$PAGE->requires->js('/local/annoto/initkaltura.js');
$PAGE->requires->js_call_amd('local_annoto/annoto', 'init', [$courseid, $modid]);
}
}
/**
* Function gets user token for Annoto script.
* @param stdClass $settings the plugin global settings.
* @param int $courseid the id of the course.
* @return string
*/
function local_annoto_get_user_token($settings, $courseid) {
global $USER, $PAGE;
// Is user logged in or is guest.
$userloggined = isloggedin();
if (!$userloggined) {
return '';
}
// Provide page and js with data.
// Get user's avatar.
$userpicture = new user_picture($USER);
$userpicture->size = 150;
$userpictureurl = $userpicture->get_url($PAGE);
$issuedat = time(); // Get current time.
$expire = $issuedat + 60 * 20; // Adding 20 minutes.
$payload = [
"jti" => $USER->id, // User's id in Moodle.
"name" => fullname($USER), // User's fullname in Moodle.
"email" => $USER->email, // User's email.
"photoUrl" => is_object($userpictureurl) ? $userpictureurl->out() : '', // User's avatar in Moodle.
"iss" => $settings->clientid, // ClientID from global settings.
"exp" => $expire, // JWT token expiration time.
"scope" => local_annoto_get_user_scope($settings, $courseid),
];
$enctoken = \Firebase\JWT\JWT::encode($payload, $settings->ssosecret, 'HS256');
return $enctoken;
}
/**
* Function gets user scope for Annoto permissions.
* @param stdClass $settings the plugin global settings.
* @param int $courseid the id of the course.
* @return 'super-mod'|'user'
*/
function local_annoto_get_user_scope($settings, $courseid) {
$userloggedin = isloggedin();
if (!$userloggedin) {
return 'user';
}
$capability = 'local/annoto:moderatediscussion';
$ismoderator = local_annoto_has_capability($settings->moderatorroles, $courseid, $capability);
return $ismoderator ? 'super-mod' : 'user';
}
/**
* Function gets current language for Annoto script.
* @param stdClass $course Course object
* @return string
*/
function local_annoto_get_lang($course) {
global $SESSION, $USER;
if (isset($course->lang) && !empty($course->lang)) {
return $course->lang;
}
if (isset($SESSION->lang) && !empty($SESSION->lang)) {
return $SESSION->lang;
}
if (isset($USER->lang) && !empty($USER->lang)) {
return $USER->lang;
}
return current_language();
}
/**
* Function defines either is current user a 'moderator' or not (in the context of Annoto script).
* @param string $allowedroles comma separated string of roles.
* @param int $courseid the id of the course.
* @param string $capability the name of the capability to check
* @return bool
*/
function local_annoto_has_capability($allowedroles, $courseid, $capability) {
global $USER;
$hascapability = false;
$coursecontext = context_course::instance($courseid);
// Check if user has a role as defined in settings.
$userroles = get_user_roles($coursecontext, $USER->id, true);
$allowedroles = explode(',', $allowedroles);
foreach ($userroles as $role) {
if (in_array($role->roleid, $allowedroles) && has_capability($capability, $coursecontext)) {
$hascapability = true;
}
}
return $hascapability;
}
/**
* Get parameters for Anooto's JS script
* @param int $courseid the id of the course.
* @param string $pageurl url of the current page.
* @param int $modid mod id.
* @return array
*/
function local_annoto_get_jsparam($courseid, $modid) {
global $CFG;
global $USER;
$course = get_course($courseid);
$userloggedin = isloggedin();
// Get plugin global settings.
$settings = get_config('local_annoto');
// Get login, logout urls.
$loginurl = $CFG->wwwroot . '/login/index.php';
$logouturl = $CFG->wwwroot . '/login/logout.php?sesskey=' . sesskey();
$activitycompletionenabled = false;
$activitycompletionreq = null;
$userscope = local_annoto_get_user_scope($settings, $courseid);
$context = \context_course::instance($courseid);
$userisenrolled = is_enrolled($context, $USER, '', true);
// Get activity data for mediaDetails.
if ($modid) {
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($modid);
$cmtitle = $cm->name;
$cmintro = $cm->content;
if ($settings->activitycompletion && $userloggedin) {
$completionrecord = annoto_completion::get_record(['cmid' => $modid]);
if ($completionrecord) {
$activitycompletionreq = $completionrecord->to_record();
// Moodle v3 do not have clean_param and returns type string.
if ((int)$completionrecord->get('enabled') == annoto_completion::COMPLETION_TRACKING_AUTOMATIC) {
$activitycompletionenabled = true;
if ($userscope === 'user') {
if (
$completiondata = annoto_completiondata::get_record(
['completionid' => $completionrecord->get('id'), 'userid' => $USER->id]
)
) {
$activitycompletionreq->user_data = $completiondata->to_record();
}
}
}
}
}
}
$jsparams = [
'deploymentDomain' => local_annoto_get_deployment_domain(),
'bootstrapUrl' => $settings->scripturl,
'clientId' => $settings->clientid,
'userToken' => local_annoto_get_user_token($settings, $courseid),
'loginUrl' => $loginurl,
'logoutUrl' => $logouturl,
'mediaTitle' => $cmtitle ?? '',
'mediaDescription' => $cmintro ?? '',
'mediaGroupId' => $courseid,
'mediaGroupTitle' => $course->fullname,
'mediaGroupDescription' => $course->summary,
'locale' => $settings->locale ? local_annoto_get_lang($course) : false,
'cmid' => $modid ?? null,
'moodleVersion' => $CFG->version,
'moodleRelease' => $CFG->release,
'activityCompletionEnabled' => $activitycompletionenabled,
'activityCompletionReq' => $activitycompletionreq,
'userScope' => $userscope,
'userIsEnrolled' => $userisenrolled,
];
return $jsparams;
}
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settingsnav The settings navigation object
* @param context $context The node to add module settings to
*/
function local_annoto_extend_settings_navigation(settings_navigation $settingsnav, context $context) {
global $CFG, $PAGE, $COURSE;
if (
(strpos($PAGE->pagetype, 'mod-') === false) &&
(strpos($PAGE->pagetype, 'course-view-') === false)
) {
return;
}
// Get plugin global settings.
$settings = get_config('local_annoto');
// Only let users with the appropriate capability see this settings item.
$capability = 'local/annoto:managementdashboard';
if (!local_annoto_has_capability($settings->managementdashboard, $COURSE->id, $capability)) {
return;
}
if (!$settingnode = $settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) {
return;
}
// Check and create LTI external tool.
require_once($CFG->dirroot . '/mod/lti/locallib.php');
$deploymentdomain = local_annoto_get_deployment_domain();
if (empty($deploymentdomain)) {
return;
}
// Will return all available tools by domain from toolurl.
$possibletools = array_merge(
lti_get_tools_by_domain($deploymentdomain, LTI_TOOL_STATE_CONFIGURED, null),
lti_get_tools_by_domain('auth.' . $deploymentdomain, LTI_TOOL_STATE_CONFIGURED, null)
);
// Will find all lti1.3 dashboard tools and return first one.
foreach ($possibletools as $tool) {
if ($tool->ltiversion === '1.3.0' && strpos($tool->baseurl, 'dashboard') !== false) {
$ltitool = $tool;
break;
}
}
// Will find all lti1.1 dashboard tools and return first one.
if (empty($ltitool)) {
foreach ($possibletools as $tool) {
if ($tool->ltiversion === 'LTI-1p0' && strpos($tool->baseurl, 'course-insights') !== false) {
$ltitool = $tool;
break;
}
}
}
if (empty($ltitool)) {
return;
}
// Create a dashboard instance if not available.
// If the dashboard is available, add a navigation button to the settings block, even if addingdashboard is false.
if (!$cm = local_annoto_get_lti_course_module($ltitool)) {
if (!$settings->addingdashboard) {
return;
}
if (!$cm = local_annoto_create_lti_course_module($ltitool)) {
return;
}
}
$text = get_string('lti_activity_name', 'local_annoto');
$type = navigation_node::TYPE_SETTING;
$icon = new pix_icon('icon', '', 'local_annoto');
$url = new moodle_url($cm->url->out());
// Add nav button to Annoto dashboard.
$annotodashboard = navigation_node::create($text, $url, $type, null, 'annotodashboard', $icon);
if ($settingnode->find('coursereports', navigation_node::TYPE_CONTAINER)) {
$settingnode->add_node($annotodashboard, 'coursereports');
} else {
// The nodes next to the course report node have no keys,
// So the target node is calculated as a delta from the closest know key.
$keys = array_flip($settingnode->get_children_key_list());
$key = isset($keys['users']) ? $keys['users'] : 0;
// Insert the new node two places after the users node.
$settingnode->add_node($annotodashboard, $settingnode->children->get_key_list()[$key + 2]);
}
}
/**
* Returns annoto dashboard's lti course module of current course.
* @param stdClass $ltitool required Tool configuration
* @return cm_info|null $cm
*/
function local_annoto_get_lti_course_module($ltitool) {
global $PAGE, $DB;
$modinfo = get_fast_modinfo($PAGE->course);
foreach ($modinfo->get_instances_of('lti') as $cm) {
if (empty($cm->instance)) {
continue;
}
$lti = $DB->get_record('lti', ['id' => $cm->instance], 'typeid');
if (empty($lti)) {
continue;
}
if ($lti->typeid == $ltitool->id) {
return $cm;
}
}
return null;
}
/**
* creates annoto dashboard's lti course module for current course
* @param stdClass $ltitool LTI extrnall tool configuration
* @return cm_info|null $cm
*/
function local_annoto_create_lti_course_module($ltitool) {
global $CFG, $PAGE;
$context = context_course::instance($PAGE->course->id);
if (!has_capability('local/annoto:managementdashboard', $context)) {
return null;
}
if (!course_allowed_module($PAGE->course, 'lti')) {
return null;
}
require_once($CFG->dirroot . '/mod/lti/locallib.php');
$toolconfig = lti_get_type_config($ltitool->id);
$newdashboard = new stdClass();
$newdashboard->modulename = 'lti';
$newdashboard->name = get_string('lti_activity_name', 'local_annoto');
$newdashboard->course = $PAGE->course->id;
$newdashboard->introeditor = [
'itemid' => 0,
'format' => FORMAT_PLAIN,
'text' => get_string('pluginname', 'local_annoto'),
];
$newdashboard->section = 0;
$newdashboard->visible = 0;
$newdashboard->typeid = $ltitool->id;
$newdashboard->servicesalt = $toolconfig['servicesalt'];
$newdashboard->instructorchoicesendname = 1;
$newdashboard->instructorchoicesendemailaddr = 1;
$newdashboard->showtitlelaunch = 1;
$newdashboard->timecreated = time();
$newdashboard->timemodified = time();
if (!create_module($newdashboard)) {
return null;
}
return local_annoto_get_lti_course_module($ltitool);
}
/**
* get all Annoto dashboard's roles with default archetypes
*
* @return array
*/
function local_annoto_get_all_dashboard_roles() {
$capabilitiy = 'local/annoto:managementdashboard';
$choices = $defaultchoices = [];
if ($roles = get_roles_with_capability($capabilitiy, CAP_ALLOW)) {
$choices = role_fix_names($roles, null, ROLENAME_ORIGINAL, true);
foreach ($choices as $key => $val) {
$defaultchoices[$key] = 1;
}
}
return [$choices, $defaultchoices];
}
/**
* Provide log into console browser
* @param string $log the id of the course.
* @return void
*/
function local_annoto_set_jslog($log = '') {
global $PAGE;
global $CFG;
$themename = $PAGE->theme->get_theme_name();
$pluginmanager = core_plugin_manager::instance();
$plugininfo = $pluginmanager->get_plugin_info('local_annoto');
$version = $plugininfo->versiondb;
$release = $plugininfo->release;
$jscode = "(function () {
console.dir('AnnotoBackend: Moodle version " . $CFG->release . "');
console.dir('AnnotoBackend: Plugin version " . $release . ' - ' . $version . "');
console.dir('AnnotoBackend: theme " . $themename . "');
console.dir('AnnotoBackend: " . $log . "');
}());";
$PAGE->requires->js_amd_inline($jscode);
}
/**
* Inject the annoto completion elements into moodle module settings forms.
*
* @param moodleform_mod $formwrapper The moodle quickforms wrapper object.
* @param MoodleQuickForm $mform The actual form object (required to modify the form).
*
* Note: original completion form is located in moodle/completion/classes/form/form_trait.php
* The extension is triggered by standard_coursemodule_elements() function in moodle/course/moodleform_mod.php
*/
function local_annoto_coursemodule_standard_elements($formwrapper, $mform) {
log::debug('local_annoto_coursemodule_standard_elements');
$settings = get_config('local_annoto');
$supportedmodtypes = [
'page',
'label',
'h5p',
'hvp',
'h5pactivity',
'kalvidres',
'lti',
];
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* $completionel = 'completion';
$conditionsgroupel = 'conditionsgroup';
$nnautomatic = $mform->createElement(
'radio',
$completionel,
'',
'Annoto completion',
9,
['class' => 'left-indented']
);
$mform->insertElementBefore($nnautomatic, $conditionsgroupel);
log::debug('coursemodule_standard_elements: ' . print_r($mform->getElement($conditionsgroupel), true)); */
$modulename = $formwrapper->get_current()->modulename;
if (!$settings->activitycompletion || !in_array($modulename, $supportedmodtypes)) {
return;
}
/*
* tooldomain - parsed from lti_toolurl of the plugin that launched page
* deploymentdomain, customdomain - domain recieved from the local
* annoto plagin integration
* domain
*/
if ($modulename === 'lti') {
$typeid = $mform->getElementValue('typeid');
$lticonfig = lti_get_type_config($typeid);
$toolurl = $lticonfig['toolurl'];
$tooldomain = (new moodle_url($toolurl))->get_host();
$deploymentdomain = local_annoto_get_deployment_domain();
if (empty($deploymentdomain)) {
return;
}
// If the tool is not Annoto LTI or is dashboard, do not add completion settings.
if (
strpos($tooldomain, $deploymentdomain) === false ||
strpos($toolurl, 'dashboard') !== false ||
strpos($toolurl, 'course-insights') !== false
) {
return;
}
}
$mform->addElement('header', 'annotocompletion', get_string('annotocompletion', 'local_annoto'));
$cmid = null;
$cm = $formwrapper->get_coursemodule();
$completionoptions = (object) [
'enabled' => annoto_completion::COMPLETION_TRACKING_NONE,
'totalview' => 0,
'comments' => 0,
'replies' => 0,
];
if (!empty($cm) && isset($cm)) {
$cmid = $cm->id;
if ($completionsettings = annoto_completion::get_record(['cmid' => $cmid])) {
$completionoptions = $completionsettings->to_record();
}
}
$completionenabledel = 'annotocompletionenabled';
$completionview = 'annotocompletionview';
$completioncomments = 'annotocompletioncomments';
$completionreplies = 'annotocompletionreplies';
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// FIXME: $completionexpected = 'annotocompletionexpected';.
$completionmenu = annoto_completion::get_enabled_menu();
$mform->addElement('select', $completionenabledel, get_string('completionenabled', 'local_annoto'), $completionmenu);
$mform->setDefault($completionenabledel, $completionoptions->enabled);
$mform->disabledIf($completionenabledel, 'completion', 'noteq', 0);
$mform->disabledIf('completion', $completionenabledel, 'noteq', annoto_completion::COMPLETION_TRACKING_NONE);
$addgroup = function ($groupname, $value, $valuerules, $info) use ($mform, $completionenabledel) {
$enabledel = 'enabled';
$valueel = 'value';
$getvalueingroup = function ($groupname) use ($valueel) {
return $groupname . '[' . $valueel . ']';
};
$getenabledingroup = function ($groupname) use ($enabledel) {
return $groupname . '[' . $enabledel . ']';
};
$group = [$mform->createElement('advcheckbox', $enabledel)];
if (isset($info) && $info->prefix) {
$group[] = &$mform->createElement('static', 'prefix', null, get_string($groupname . 'prefix', 'local_annoto'));
}
$group[] = &$mform->createElement('text', $valueel);
if (isset($info) && $info->suffix) {
$group[] = &$mform->createElement('static', 'suffix', null, get_string($groupname . 'suffix', 'local_annoto'));
}
$mform->addGroup($group, $groupname, get_string($groupname, 'local_annoto'));
$mform->disabledIf($getvalueingroup($groupname), $getenabledingroup($groupname), 'notchecked');
$mform->setType($getvalueingroup($groupname), PARAM_INT);
$mform->setDefault($getvalueingroup($groupname), $value);
$mform->setDefault($getenabledingroup($groupname), $value > 0);
if (isset($valurules)) {
$grouprule = [];
$grouprule[$valueel] = $valuerules;
$mform->addGroupRule($groupname, $grouprule);
}
$mform->hideIf($groupname, $completionenabledel, 'ne', annoto_completion::COMPLETION_TRACKING_AUTOMATIC);
};
$addgroup(
$completionview,
$completionoptions->totalview,
[
[get_string('numericrule', 'local_annoto'), 'numeric'],
],
(object) [
'prefix' => true,
'suffix' => true,
]
);
$addgroup(
$completioncomments,
$completionoptions->comments,
[
[get_string('numericrule', 'local_annoto'), 'numeric'],
],
(object) [
'prefix' => true,
'suffix' => false,
]
);
$addgroup(
$completionreplies,
$completionoptions->replies,
[
[get_string('numericrule', 'local_annoto'), 'numeric'],
],
(object) [
'prefix' => true,
'suffix' => false,
]
);
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// FIXME: $mform->addElement('date_time_selector', $completionexpected,
// get_string($completionexpected, 'local_annoto'), ['optional' => true]);.
}
/**
* Hook to process data from submitted form of coursemodule
*
* @param stdClass $data moduleinfo data from the form submission.
* @param stdClass $course The course.
* @return stdClass updated moduleinfo
*
* See plugin_extend_coursemodule_edit_post_actions in
* https://github.com/moodle/moodle/blob/master/course/modlib.php
*
*/
function local_annoto_coursemodule_edit_post_actions($data, $course) {
global $DB;
$settings = get_config('local_annoto');
$cmid = $data->coursemodule;
log::debug('local_annoto_coursemodule_edit_post_actions');
if (isset($data->annotocompletionenabled) && $settings->activitycompletion) {
$completionenabled = $data->annotocompletionenabled;
$completionview = $data->annotocompletionview;
$completioncomments = $data->annotocompletioncomments;
$completionreplies = $data->annotocompletionreplies;
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// FIXME: $completionexpected = $data->annotocompletionexpected;.
$completionrecord = new stdClass();
$completionrecord->courseid = $course->id;
$completionrecord->cmid = $cmid;
$completionrecord->enabled = $completionenabled;
$completionrecord->totalview = $completionview['enabled'] ? $completionview['value'] : 0;
$completionrecord->comments = $completioncomments['enabled'] ? $completioncomments['value'] : 0;
$completionrecord->replies = $completionreplies['enabled'] ? $completionreplies['value'] : 0;
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
// FIXME: $completiondata->completionexpected = $completionexpected;.
log::debug('coursemodule_edit_post_actions - completiondata: ' . json_encode($completionrecord));
if (!$record = annoto_completion::get_record(['cmid' => $cmid])) {
$record = new annoto_completion(0, $completionrecord);
$record->create();
} else {
$record->from_record($completionrecord);
$record->update();
}
$cm = $DB->get_record('course_modules', ['id' => $cmid]);
if (!$cm) {
return $data;
}
$completiontracking = $data->completion;
if ($completionenabled == annoto_completion::COMPLETION_TRACKING_AUTOMATIC) {
$completiontracking = annoto_completion::COMPLETION_TRACKING_AUTOMATIC;
// Lock completion form if annotocompletion is used.
// The update_moduleinfo() at moodle/course/modlib.php calls reset_all_state() if completion is unlocked.
// Reset can cause all users completion state to be set to invalid completed state for following sequence of events:
// 1. New mod created with completion not setup.
// 2. Edit mod settings and setup Annoto completion.
// 3. Save mod settings. (if completion is unlocked, reset_all_state() will be called and set all users to completed).
unset($data->completionunlocked);
// Delete all completion state for this cm to clear native moodle completion or other configuration changes.
// The completion state will be re-calculated by Annoto completion scheduled task.
$completion = new completion_info($course);
$completion->delete_all_state($cm);
}
// FIXME: need to cleanup this override if $settings->activitycompletion changes to false for all the courses.
$cm->completion = $completiontracking;
$DB->update_record('course_modules', $cm);
// See edit_module_post_actions() in moodle/course/modlib.php
// coursemodule_edit_post_actions is called after edit_module_post_actions() clears the cache
// because we change the cm we need to clear it again.
if (method_exists(\course_modinfo::class, 'purge_course_module_cache')) {
log::debug('coursemodule_edit_post_actions - purge_course_module_cache and partial rebuild_course_cache v4');
// Moodle v4 and up.
\course_modinfo::purge_course_module_cache($cm->course, $cmid);
rebuild_course_cache($cm->course, true, true);
} else {
// Moodle v3.
log::debug('coursemodule_edit_post_actions - rebuild_course_cache v3');
rebuild_course_cache($cm->course, true);
}
}
return $data;
}
/**
* Function to get the configured deployment domain
* @return string|null
*/
function local_annoto_get_deployment_domain() {
$settings = get_config('local_annoto');
return $settings->deploymentdomain != 'custom' ? $settings->deploymentdomain : $settings->customdomain;
}