-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.php
1696 lines (1531 loc) · 67.7 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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Callback implementations for Interactivevideo
*
* Documentation: {@link https://moodledev.io/docs/apis/plugintypes/mod}
*
* @package mod_interactivevideo
* @copyright 2024 Sokunthearith Makara <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\url;
define('INTERACTIVEVIDEO_DISPLAY_INLINE', 1);
/**
* Return if the plugin supports $feature.
*
* @param string $feature Constant representing the feature.
* @return true | null True if the feature is supported, null otherwise.
*/
function interactivevideo_supports($feature) {
switch ($feature) {
case FEATURE_MOD_INTRO:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_CONTENT;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
default:
return null;
}
}
/**
* Returns subplugins with class name.
* @param string $classname The class name.
* @return array The subplugins.
*/
function interactivevideo_get_subplugins($classname) {
$allsubplugins = explode(',', get_config('mod_interactivevideo', 'enablecontenttypes'));
$subpluginclass = [];
foreach ($allsubplugins as $subplugin) {
$class = $subplugin . '\\' . $classname;
if (class_exists($class)) {
$subpluginclass[] = $class;
}
}
return $subpluginclass;
}
/**
* Mod edit form display options
*
* @param mixed $moduleinstance
* @return mixed
*/
function interactivevideo_display_options($moduleinstance) {
$options = [];
$options['disablechapternavigation'] = $moduleinstance->disablechapternavigation ?? 0;
$options['preventskipping'] = $moduleinstance->preventskipping ?? 0;
$options['useoriginalvideocontrols'] = $moduleinstance->useoriginalvideocontrols ?? 0;
$options['hidemainvideocontrols'] = $moduleinstance->hidemainvideocontrols ?? 0;
$options['preventseeking'] = $moduleinstance->preventseeking ?? 0;
$options['disableinteractionclick'] = $moduleinstance->disableinteractionclick ?? 0;
$options['disableinteractionclickuntilcompleted'] = $moduleinstance->disableinteractionclickuntilcompleted ?? 0;
$options['hideinteractions'] = $moduleinstance->hideinteractions ?? 0;
$options['theme'] = $moduleinstance->theme ?? '';
$options['distractionfreemode'] = $moduleinstance->distractionfreemode ?? 0;
$options['darkmode'] = $moduleinstance->distractionfreemode == 1 ? $moduleinstance->darkmode : 0;
$options['usefixedratio'] = $moduleinstance->distractionfreemode == 1 ? $moduleinstance->usefixedratio : 1;
$options['pauseonblur'] = $moduleinstance->pauseonblur ?? 0;
$options['usecustomposterimage'] = $moduleinstance->usecustomposterimage ?? 0;
$options['displayinline'] = $moduleinstance->displayinline ?? 0;
$options['cardsize'] = $moduleinstance->cardsize ?? 'large';
$options['cardonly'] = $moduleinstance->cardonly ?? 0;
$options['showposterimageright'] = $moduleinstance->showposterimageright ?? 0;
$options['usecustomdescription'] = $moduleinstance->usecustomdescription ?? 0;
$options['customdescription'] = $moduleinstance->customdescription ?? '';
$options['launchinpopup'] = $moduleinstance->launchinpopup ?? 0;
$options['showprogressbar'] = $moduleinstance->showprogressbar ?? 0;
$options['showcompletionrequirements'] = $moduleinstance->showcompletionrequirements ?? 0;
$options['showposterimage'] = $moduleinstance->showposterimage ?? 0;
$options['squareposterimage'] = $moduleinstance->squareposterimage ?? 0;
$options['showname'] = $moduleinstance->showname ?? 0;
$options['autoplay'] = $moduleinstance->autoplay ?? 0;
$options['columnlayout'] = $moduleinstance->columnlayout ?? 0;
$options['showdescriptiononheader'] = $moduleinstance->showdescriptiononheader ?? 0;
$options['passwordprotected'] = $moduleinstance->passwordprotected ?? 0;
return $options;
}
/**
* Saves a new instance of the mod_interactivevideo 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 instance.
*
* @param object $moduleinstance An object from the form.
* @param mod_interactivevideo_mod_form $mform The form.
* @param bool $batch True if the function is called from bulk insert.
* @return int The id of the newly inserted record.
*/
function interactivevideo_add_instance($moduleinstance, $mform = null, $batch = false) {
global $DB, $USER;
$cmid = $moduleinstance->coursemodule;
$moduleinstance->timecreated = time();
$moduleinstance->timemodified = time();
if (empty($moduleinstance->displayasstartscreen)) {
$moduleinstance->displayasstartscreen = 0;
}
$moduleinstance->text = $moduleinstance->endscreentext;
if (!$batch) {
$moduleinstance->endscreentext = json_encode($moduleinstance->endscreentext);
}
$moduleinstance->displayoptions = json_encode(interactivevideo_display_options($moduleinstance));
$moduleinstance->id = $DB->insert_record('interactivevideo', $moduleinstance);
$context = context_module::instance($cmid);
if (!$batch) {
// Update the completion expected date.
if (!empty($moduleinstance->completionexpected)) {
\core_completion\api::update_completion_date_event(
$moduleinstance->coursemodule,
'interactivevideo',
$moduleinstance->id,
$moduleinstance->completionexpected
);
}
$cmexist = $DB->record_exists('course_modules', [
'module' => $moduleinstance->module,
'instance' => $moduleinstance->id,
]);
if (!$cmexist) {
$DB->set_field('course_modules', 'instance', $moduleinstance->id, ['id' => $cmid]);
}
if ($mform && !empty($moduleinstance->text['itemid'])) {
$draftitemid = $moduleinstance->text['itemid'];
$moduleinstance->endscreentext = file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'endscreentext',
0,
['subdirs' => 0],
$moduleinstance->text['text']
);
$DB->update_record('interactivevideo', $moduleinstance);
}
// Handle the file upload for video.
if ($moduleinstance->source == 'url') {
// Make sure the video field is empty.
$DB->set_field('interactivevideo', 'video', '', ['id' => $moduleinstance->id]);
// Delete the draft area files. This is normally done by the cron job, but we might as well do it now.
if (!empty($moduleinstance->video)) {
$fs = get_file_storage();
$usercontext = context_user::instance($USER->id);
$fs->delete_area_files($usercontext->id, 'user', 'draft', $moduleinstance->video);
}
} else {
$draftitemid = $moduleinstance->video;
// Move the file from draft area to the correct area.
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'video',
0,
);
// Clear the videourl field.
$DB->set_field('interactivevideo', 'videourl', '', ['id' => $moduleinstance->id]);
// Delete the draft area files. This is normally done by the cron job, but we might as well do it now.
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
$fs->delete_area_files($usercontext->id, 'user', 'draft', $draftitemid);
}
// Save poster image file from draft area.
$draftitemid = isset($moduleinstance->posterimagefile) ? $moduleinstance->posterimagefile : null;
if ($draftitemid) {
$moduleinstance->posterimage = file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'posterimage',
0
);
}
}
interactivevideo_grade_item_update($moduleinstance);
// Handle external plugins.
$subplugins = interactivevideo_get_subplugins('ivmform');
foreach ($subplugins as $subplugin) {
if (method_exists($subplugin, 'add_instance')) {
$subplugin::add_instance($moduleinstance, $mform, $context);
}
}
return $moduleinstance->id;
}
/**
* Updates an instance of the mod_interactivevideo in the database.
*
* Given an object containing all the necessary data (defined in mod_form.php),
* this function will update an existing instance with new data.
*
* @param object $moduleinstance An object from the form in mod_form.php.
* @param mod_interactivevideo_mod_form $mform The form.
* @return bool True if successful, false otherwise.
*/
function interactivevideo_update_instance($moduleinstance, $mform = null) {
global $DB, $USER;
$moduleinstance->id = $moduleinstance->instance;
// Before we do anything, we need to check if the module instance has any video file, so we can delete it later.
$oldvideo = $DB->get_field('interactivevideo', 'video', ['id' => $moduleinstance->id]);
$moduleinstance->timemodified = time();
$cmid = $moduleinstance->coursemodule;
$draftitemid = $moduleinstance->endscreentext['itemid'];
$text = $moduleinstance->endscreentext['text'];
$moduleinstance->timemodified = time();
// Put the endscreentext stdClass into a single field.
$moduleinstance->endscreentext = json_encode($moduleinstance->endscreentext);
// Put all the display options into a single field.
$moduleinstance->displayoptions = json_encode(interactivevideo_display_options($moduleinstance));
$completiontimeexpected = !empty($moduleinstance->completionexpected) ? $moduleinstance->completionexpected : null;
\core_completion\api::update_completion_date_event(
$moduleinstance->coursemodule,
'interactivevideo',
$moduleinstance->id,
$completiontimeexpected
);
$context = context_module::instance($cmid);
if ($draftitemid) {
$moduleinstance->endscreentext = file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'endscreentext',
0,
['subdirs' => 0],
$text
);
}
if ($moduleinstance->source == 'url') {
// Delete video file if any.
if ($oldvideo) {
// Delete the draft area files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_interactivevideo', 'video', 0);
if ($moduleinstance->video) {
$usercontext = context_user::instance($USER->id);
$fs->delete_area_files($usercontext->id, 'user', 'draft', $moduleinstance->video);
}
$moduleinstance->video = '';
}
} else {
if ($oldvideo != $moduleinstance->video) {
// Move the file from draft area to the correct area. This process will delete the old file, if any.
$draftitemid = $moduleinstance->video;
file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'video',
0,
);
// Delete the draft area files. This is normally done by the cron job, but we might as well do it now.
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
$fs->delete_area_files(
$usercontext->id,
'user',
'draft',
$draftitemid
);
}
// Make sure the videourl field is empty.
$moduleinstance->videourl = '';
}
// Finally update the record.
$DB->update_record('interactivevideo', $moduleinstance);
// Save poster image file from draft area.
$draftitemid = $moduleinstance->posterimagefile;
if ($draftitemid) {
$moduleinstance->posterimage = file_save_draft_area_files(
$draftitemid,
$context->id,
'mod_interactivevideo',
'posterimage',
0
);
}
// Let's update the grade item.
interactivevideo_grade_item_update($moduleinstance);
interactivevideo_update_grades($moduleinstance);
// Handle external plugins.
$subplugins = interactivevideo_get_subplugins('ivmform');
foreach ($subplugins as $subplugin) {
if (method_exists($subplugin, 'update_instance')) {
$subplugin::update_instance($moduleinstance, $mform, $context);
}
}
return true;
}
/**
* Removes an instance of the mod_interactivevideo from the database.
*
* @param int $id Id of the module instance.
* @return bool True if successful, false on failure.
*/
function interactivevideo_delete_instance($id) {
global $DB;
$exists = $DB->get_record('interactivevideo', ['id' => $id]);
if (!$exists) {
return false;
}
$cm = get_coursemodule_from_instance('interactivevideo', $id);
// Handle external plugins.
$subplugins = interactivevideo_get_subplugins('ivmform');
foreach ($subplugins as $subplugin) {
if (method_exists($subplugin, 'delete_instance')) {
$subplugin::delete_instance($exists, $cm);
}
}
\core_completion\api::update_completion_date_event($cm->id, 'interactivevideo', $exists->id, null);
interactivevideo_grade_item_delete($exists);
$DB->delete_records('interactivevideo', ['id' => $id]);
// Delete all the annotations and their items.
$DB->delete_records('interactivevideo_items', ['annotationid' => $id]);
// Delete all the completion records.
$DB->delete_records('interactivevideo_completion', ['cmid' => $id]);
// Delete all the logs.
$DB->delete_records('interactivevideo_log', ['cmid' => $id]);
return true;
}
/**
* Returns the lists of all browsable file areas within the given module context.
*
* The file area 'intro' for the activity introduction field is added automatically
* by {@see file_browser::get_file_info_context_module()}.
*
* @package mod_interactivevideo
* @category files
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return string[].
*/
function interactivevideo_get_file_areas($course, $cm, $context) {
return [
'public',
'content',
'endscreentext',
'attachments',
'text1',
'text2',
'text3',
];
}
/**
* File browsing support for mod_interactivevideo file areas.
*
* @package mod_interactivevideo
* @category files
*
* @param file_browser $browser
* @param array $areas
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info Instance or null if not found.
*/
function interactivevideo_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
return null;
}
/**
* Serves the files from the mod_interactivevideo file areas.
*
* @package mod_interactivevideo
* @category files
*
* @param stdClass $course The course object.
* @param stdClass $cm The course module object.
* @param stdClass $context The mod_interactivevideo's context.
* @param string $filearea The name of the file area.
* @param array $args Extra arguments (itemid, path).
* @param bool $forcedownload Whether or not force download.
* @param array $options Additional options affecting the file serving.
*/
function interactivevideo_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = []) {
if ($filearea != 'public') {
require_login($course, true, $cm);
}
$itemid = array_shift($args);
$filename = array_pop($args);
if (!$args) {
$filepath = '/';
} else {
$filepath = '/' . implode('/', $args) . '/';
}
// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'mod_interactivevideo', $filearea, $itemid, $filepath, $filename);
if (!$file) {
send_file_not_found();
}
// Finally send the file.
send_stored_file($file, 0, 0, $forcedownload, $options);
}
/**
* Extends the settings navigation with the mod_interactivevideo settings.
*
* This function is called when the context for the page is a mod_interactivevideo module.
* This is not called by AJAX so it is safe to rely on the $PAGE.
*
* @param settings_navigation $settingsnav {@see settings_navigation}
* @param navigation_node $interactivevideonode {@see navigation_node}
*/
function interactivevideo_extend_settings_navigation($settingsnav, $interactivevideonode = null) {
$page = $settingsnav->get_page();
// Interaction tab.
if (has_capability('mod/interactivevideo:edit', $page->context)) {
$interactivevideonode->add(
get_string('interactions', 'mod_interactivevideo'),
new moodle_url('/mod/interactivevideo/interactions.php', ['id' => $page->cm->id]),
$interactivevideonode::TYPE_SETTING,
null,
null,
new pix_icon('i/edit', '')
);
}
// Report tab.
if (has_capability('mod/interactivevideo:viewreport', $page->context)) {
$interactivevideonode->add(
get_string('report', 'mod_interactivevideo'),
new moodle_url('/mod/interactivevideo/report.php', ['id' => $page->cm->id, 'group' => 0]),
$interactivevideonode::TYPE_SETTING,
null,
null,
new pix_icon('i/report', '')
);
}
}
/**
* Add a get_coursemodule_info function.
*
* Given a course_module object, this function returns any "extra" information that may be needed
* when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return cached_cm_info An object on information that the courses
* will know about (most noticeably, an icon).
*/
function interactivevideo_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$interactive = $DB->get_record('interactivevideo', $dbparams, '*');
if (!$interactive) {
return false;
}
$result = new cached_cm_info();
$result->name = $interactive->name;
$result->customdata['displayoptions'] = $interactive->displayoptions;
if ($coursemodule->showdescription) {
$result->content = format_module_intro('interactivevideo', $interactive, $coursemodule->id, false);
}
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionpercentage'] = $interactive->completionpercentage;
// Add extended completion.
$result->customdata['extendedcompletion'] = $interactive->extendedcompletion ?? '[]';
foreach (json_decode($result->customdata['extendedcompletion']) as $rule => $value) {
$result->customdata['customcompletionrules'][$rule] = $value;
}
// Pass startendtime to be used in the completion tracking.
$result->customdata['startendtime'] = $interactive->starttime . "-" . $interactive->endtime;
}
return $result;
}
/**
* Dynamically updates the course module information.
*
* @param cm_info $cm The course module information object.
*/
function interactivevideo_cm_info_dynamic(cm_info $cm) {
global $PAGE;
if (strpos($PAGE->bodyclasses, 'path-course-view') === false) { // MUST be in course view only.
return;
}
// Set after link.
$afterlink = '';
$context = context_module::instance($cm->id);
if (has_capability('mod/interactivevideo:edit', $context)) {
$afterlink .= html_writer::link(
new moodle_url('/course/modedit.php?', ['update' => $cm->id]),
'<i class="fa fa-edit" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('edit', 'mod_interactivevideo'),
'aria-label' => get_string('edit', 'mod_interactivevideo'),
]
);
$afterlink .= html_writer::link(
new moodle_url('/mod/interactivevideo/interactions.php', ['id' => $cm->id]),
'<i class="fa fa-bullseye" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('interactions', 'mod_interactivevideo'),
'aria-label' => get_string('interactions', 'mod_interactivevideo'),
]
);
}
if (has_capability('mod/interactivevideo:viewreport', $context)) {
$afterlink .= html_writer::link(
new moodle_url('/mod/interactivevideo/report.php', ['id' => $cm->id, 'group' => 0]),
'<i class="fa fa-table" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('report', 'mod_interactivevideo'),
'aria-label' => get_string('report', 'mod_interactivevideo'),
]
);
}
$customdata = $cm->customdata;
$displayoptions = json_decode($customdata['displayoptions']);
$isivformat = strpos($PAGE->bodyclasses, 'format-test') !== false && !$PAGE->user_is_editing();
if ((isset($displayoptions->displayinline) && $displayoptions->displayinline == INTERACTIVEVIDEO_DISPLAY_INLINE)
|| $isivformat
) {
$cm->set_no_view_link();
} else {
$cm->set_after_link($afterlink);
}
}
/**
* Callback function to display custom information for the interactive video module.
*
* This function is called when viewing the course module information.
*
* @param cm_info $cm The course module information object.
*/
function interactivevideo_cm_info_view(cm_info $cm) {
global $PAGE;
$customdata = $cm->customdata;
$isivformat = strpos($PAGE->bodyclasses, 'format-test') !== false && !$PAGE->user_is_editing();
$displayoptions = json_decode($customdata['displayoptions']);
$displayinline = isset($displayoptions->displayinline) && $displayoptions->displayinline == INTERACTIVEVIDEO_DISPLAY_INLINE;
if ($displayinline || $isivformat) {
$cm->set_content(interactivevideo_displayinline($cm));
}
}
/**
* Displays the interactive video inline.
*
* @param cm_info $cm Course module information.
*/
function interactivevideo_displayinline(cm_info $cm) {
global $DB, $USER, $CFG, $OUTPUT, $PAGE;
// Get data from interactivevideo and interactivevideo_items and interactivevideo_completion for current user.
$interactivevideo = $DB->get_record(
'interactivevideo',
['id' => $cm->instance],
'id, name, starttime, endtime, type, posterimage, intro, introformat, completionpercentage'
);
if (!$interactivevideo) {
return '';
}
// Set after link.
$afterlink = '';
$context = context_module::instance($cm->id);
if (has_capability('mod/interactivevideo:edit', $context)) {
$afterlink .= html_writer::link(
new moodle_url('/course/modedit.php?', ['update' => $cm->id]),
'<i class="fa fa-edit" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('edit', 'mod_interactivevideo'),
'aria-label' => get_string('edit', 'mod_interactivevideo'),
]
);
$afterlink .= html_writer::link(
new moodle_url('/mod/interactivevideo/interactions.php', ['id' => $cm->id]),
'<i class="fa fa-bullseye" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('interactions', 'mod_interactivevideo'),
'aria-label' => get_string('interactions', 'mod_interactivevideo'),
]
);
}
if (has_capability('mod/interactivevideo:viewreport', $context)) {
$afterlink .= html_writer::link(
new moodle_url('/mod/interactivevideo/report.php', ['id' => $cm->id, 'group' => 0]),
'<i class="fa fa-table" aria-hidden="true"></i>',
[
'class' => 'p-1 mr-1',
'title' => get_string('report', 'mod_interactivevideo'),
'aria-label' => get_string('report', 'mod_interactivevideo'),
]
);
}
// Support for format_iv.
// Get course format.
$isivformat = strpos($PAGE->bodyclasses, 'format-test') !== false && !$PAGE->user_is_editing();
$displayoptions = json_decode($cm->customdata['displayoptions']);
if (isset($displayoptions->usecustomposterimage) && $displayoptions->usecustomposterimage) {
$fs = get_file_storage();
$file = $fs->get_area_files(
$cm->context->id,
'mod_interactivevideo',
'posterimage',
0,
'filesize DESC',
);
$file = reset($file);
if ($file) {
$posterimage = moodle_url::make_pluginfile_url(
$file->get_contextid(),
$file->get_component(),
$file->get_filearea(),
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);
$interactivevideo->posterimage = $posterimage->out();
}
}
$interactivevideo->posterimage = $interactivevideo->posterimage == '' ?
$OUTPUT->get_generated_image_for_id($cm->id) : $interactivevideo->posterimage; // Fallback to default image.
$duration = $interactivevideo->endtime - $interactivevideo->starttime;
// Convert to hh:mm:ss format.
$duration = gmdate($duration > 3600 ? 'H:i:s' : 'i:s', (int) $duration);
// Format the intro: keep text only and truncate it.
$datafortemplate = [
'interactivevideo' => $interactivevideo,
'cm' => $cm,
'hascompletion' => isset($displayoptions->hascompletion),
'passwordprotected' => isset($displayoptions->passwordprotected) && $displayoptions->passwordprotected,
'overallcomplete' => isset($displayoptions->hascompletion)
&& isset($displayoptions->overallcompletion) && $displayoptions->overallcompletion == 1,
'launchinpopup' => isset($displayoptions->launchinpopup) && $displayoptions->launchinpopup,
'baseurl' => $CFG->wwwroot,
'duration' => $duration,
'formattedname' => format_string($cm->name),
'showdescription' => $cm->showdescription,
'formattedintro' => format_module_intro('interactivevideo', $interactivevideo, $cm->id, false),
'originalintro' => htmlentities($interactivevideo->intro ?? ''),
'size' => isset($displayoptions->cardsize) ? $displayoptions->cardsize : 'large',
'issmall' => isset($displayoptions->cardsize)
&& ($displayoptions->cardsize == 'small' || $displayoptions->cardsize == 'medium'
|| $displayoptions->cardsize == 'mediumlarge'),
'columnlayout' => isset($displayoptions->columnlayout) ? $displayoptions->columnlayout : '1',
'afterlink' => $afterlink,
'posterimagesquare' => isset($displayoptions->squareposterimage) && $displayoptions->squareposterimage,
];
if (isset($displayoptions->cardsize)) {
$datafortemplate['usecustomdesc'] = isset($displayoptions->usecustomdescription) && $displayoptions->usecustomdescription;
$datafortemplate['customdesc'] = isset($displayoptions->customdescription) ? $displayoptions->customdescription : '';
$datafortemplate['showposterimageright'] = isset($displayoptions->showposterimageright)
&& $displayoptions->showposterimageright;
$datafortemplate['showposterimage'] = isset($displayoptions->showposterimage) && $displayoptions->showposterimage;
$datafortemplate['showprogressbar'] = isset($displayoptions->showprogressbar) && $displayoptions->showprogressbar;
$datafortemplate['showcompletion'] = isset($displayoptions->showcompletionrequirements)
&& $displayoptions->showcompletionrequirements;
$datafortemplate['cardonly'] = isset($displayoptions->cardonly) && $displayoptions->cardonly;
$datafortemplate['showname'] = isset($displayoptions->showname) && $displayoptions->showname;
if ($datafortemplate['cardonly']) {
$datafortemplate['showposterimageright'] = false;
$datafortemplate['usecustomdesc'] = false;
$datafortemplate['showdescription'] = false;
$datafortemplate['showposterimage'] = true;
$datafortemplate['columnlayout'] = false;
}
if ($datafortemplate['usecustomdesc']) {
$datafortemplate['showdescription'] = true;
$datafortemplate['formattedintro'] = $datafortemplate['customdesc'];
$datafortemplate['originalintro'] = $datafortemplate['customdesc'];
}
}
if ($isivformat) {
$datafortemplate['showdescription'] = false;
$datafortemplate['showposterimage'] = true;
$datafortemplate['showprogressbar'] = true;
$datafortemplate['showcompletion'] = true;
$datafortemplate['showname'] = true;
$datafortemplate['size'] = 'large';
$datafortemplate['columnlayout'] = true;
$datafortemplate['launchinpopup'] = false;
$datafortemplate['formativ'] = true;
$datafortemplate['cardonly'] = false;
}
if (!$cm->uservisible) {
return $OUTPUT->render_from_template('mod_interactivevideo/activitycard', $datafortemplate);
}
// Completion details.
$completion = null;
$getcompletion = false;
if ($datafortemplate['launchinpopup']) {
$getcompletion = true;
}
if ($datafortemplate['showcompletion']) {
$getcompletion = true;
}
if ($USER->id <= 1) { // Guest user.
$getcompletion = false;
}
if ($getcompletion) {
$completiondetails = \core_completion\cm_completion_details::get_instance($cm, $USER->id);
if ($CFG->branch < 404) {
$completion = $OUTPUT->activity_information($cm, $completiondetails, []);
} else {
$activitycompletion = new \core_course\output\activity_completion($cm, $completiondetails);
$output = $PAGE->get_renderer('core');
$activitycompletiondata = (array) $activitycompletion->export_for_template($output);
if ($activitycompletiondata["hascompletion"]) {
$completion = $OUTPUT->render_from_template('core_course/activity_info', $activitycompletiondata);
}
}
}
$datafortemplate['completion'] = $completion;
// Get interactive_items.
$select = "annotationid = ? AND ((timestamp >= ? AND timestamp <= ?) OR timestamp < 0) "
. "AND (hascompletion = 1 OR type = 'skipsegment'"
. ($datafortemplate['showprogressbar'] ? " OR type = 'analytics')" : ')');
$relevantitems = $DB->get_records_select(
'interactivevideo_items',
$select,
[$cm->instance, $interactivevideo->starttime, $interactivevideo->endtime],
'',
'id, type, xp, timestamp, title, char1, hascompletion'
);
$skipsegment = array_filter($relevantitems, function ($item) {
return $item->type === 'skipsegment';
});
$analytics = array_filter($relevantitems, function ($item) {
return $item->type === 'analytics';
});
$analytics = reset($analytics);
$relevantitems = array_filter($relevantitems, function ($item) use ($skipsegment) {
foreach ($skipsegment as $ss) {
if ($item->timestamp > $ss->timestamp && $item->timestamp < $ss->title && $item->timestamp >= 0) {
return false;
}
}
if ($item->type === 'skipsegment') {
return false;
}
return true;
});
if ($USER->id > 1) {
$usercompletion = $DB->get_records(
'interactivevideo_completion',
[
'userid' => $USER->id,
'cmid' => $cm->instance,
],
'xp, completionpercentage, completeditems' . ($analytics ? ', completiondetails' : '')
);
$usercompletion = reset($usercompletion);
} else {
require_once($CFG->dirroot . '/mod/interactivevideo/locallib.php');
$usercompletion = interactivevideo_util::get_progress($cm->instance, 1, false);
}
if (!$relevantitems) {
$datafortemplate['noitems'] = true;
if (!$usercompletion) {
$datafortemplate['new'] = true;
}
return $OUTPUT->render_from_template('mod_interactivevideo/activitycard', $datafortemplate);
}
if (!$usercompletion) {
$datafortemplate['new'] = true;
$usercompletion = [
'xp' => 0,
'completionpercentage' => 0,
'completeditems' => '[]',
'completiondetails' => '[]',
];
} else {
$usercompletion = (array) $usercompletion;
}
if ($usercompletion['completeditems'] == '') {
$usercompletion['completeditems'] = '[]';
}
$completeditems = json_decode($usercompletion['completeditems'], true);
$datafortemplate['completeditems'] = count($completeditems);
// Remove analytics that does not have completion.
$relevantitems = array_filter($relevantitems, function ($item) {
return $item->hascompletion == 1;
});
$datafortemplate['noitems'] = count($relevantitems) == 0;
if (!$datafortemplate['noitems']) {
$datafortemplate['totalitems'] = count($relevantitems);
$datafortemplate['totalxp'] = array_sum(array_column($relevantitems, 'xp'));
if ((float) $datafortemplate['totalxp'] == 0) {
$datafortemplate['noxp'] = true;
}
// Get completion information.
$usercompletion['completionpercentage'] = round(($datafortemplate['completeditems'] / $datafortemplate['totalitems'])
* 100);
$datafortemplate['usercompletion'] = $usercompletion;
$datafortemplate['completed'] = $usercompletion['completionpercentage'] == 100
|| ($interactivevideo->completionpercentage > 0
&& $usercompletion['completionpercentage'] >= $interactivevideo->completionpercentage);
}
if ($analytics && $datafortemplate['showprogressbar']) {
$datafortemplate['analyticsexpected'] = (int) $analytics->char1;
$datafortemplate['hasanalytics'] = true;
$datafortemplate['analytics'] = 0;
$datafortemplate['analyticscompleted'] = false;
if ($usercompletion) {
$analyticsid = $analytics->id;
$completiondetails = (array)json_decode($usercompletion['completiondetails'], true);
$completiondetails = array_map(function ($item) {
$item = json_decode($item, true);
return (object)$item;
}, $completiondetails);
$analyticsitem = array_filter($completiondetails, function ($item) use ($analyticsid) {
return $item->id == $analyticsid;
});
$analyticsitem = reset($analyticsitem);
if ($analyticsitem) {
$datafortemplate['analytics'] = $analyticsitem->percentage;
}
if ($datafortemplate['analytics'] == 100 || ($datafortemplate['analyticsexpected'] > 0
&& $datafortemplate['analytics'] >= $datafortemplate['analyticsexpected'])) {
$datafortemplate['analyticscompleted'] = true;
}
}
}
return $OUTPUT->render_from_template('mod_interactivevideo/activitycard', $datafortemplate);
}
if ($CFG->branch <= 403) {
/**
* Adds JavaScript before the footer is rendered.
*
* This function is called to add JavaScript before the footer is rendered
* when the page is a course view.
*/
function interactivevideo_before_footer() {
global $PAGE;
if (strpos($PAGE->bodyclasses, 'path-course-view') === false) {
return;
}
$PAGE->requires->js_call_amd('mod_interactivevideo/launch', 'init');
}
}
/**
* Creates or updates grade item for the given mod_interactivevideo instance.
*
* Needed by {@see grade_update_mod_grades()}.
*
* @param stdClass $moduleinstance Instance object with extra cmidnumber and modname property.
* @param mixed $grades Null to update all grades, false to delete all grades, or array of user grades.
* @return void.
*/
function interactivevideo_grade_item_update($moduleinstance, $grades = null) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
if (!isset($moduleinstance->courseid)) {
$moduleinstance->courseid = $moduleinstance->course;
}
$item = [];
$item['iteminfo'] = null;
$item['itemname'] = clean_param($moduleinstance->name, PARAM_NOTAGS);
if ($moduleinstance->grade > 0) {
$item['gradetype'] = GRADE_TYPE_VALUE;
$item['grademax'] = $moduleinstance->grade;
$item['grademin'] = 0;
} else {
$item['gradetype'] = GRADE_TYPE_NONE;
}
if ($grades === 'reset') {
$item['reset'] = true;