-
Notifications
You must be signed in to change notification settings - Fork 6
/
lib.php
executable file
·1154 lines (958 loc) · 39.1 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/>.
/**
* Library of interface functions and constants for module cmi5launch
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
* All the cmi5launch specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_cmi5launch
* @copyright 2013 Andrew Downes
* @copyright 2024 Megan Bohland - added functions for cmi5launch
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Cmi5PHP - required for interacting with the LRS in cmi5launch_get_statements.
require_once("$CFG->dirroot/mod/cmi5launch/cmi5PHP/autoload.php");
// SCORM library from the SCORM module. Required for its xml2Array class by cmi5launch_process_new_package.
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
use mod_cmi5launch\local\cmi5_connectors;
use mod_cmi5launch\local\au_helpers;
use mod_cmi5launch\local\grade_helpers;
global $cmi5launchsettings;
$cmi5launchsettings = null;
// Moodle Core API.
/**
* Returns the information on whether 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 cmi5launch_supports($feature) {
switch($feature) {
// True if module supports intro editor.
case FEATURE_MOD_INTRO:
return true;
// MB - we do not currently support the next two, but leaving in, in case we start to.
// True if module has code to track whether somebody viewed it
// case FEATURE_COMPLETION_TRACKS_VIEWS:
// return true;
// True if module has custom completion rules
// case FEATURE_COMPLETION_HAS_RULES:
// return true;
// True if module supports backup/restore of moodle2 format.
case FEATURE_BACKUP_MOODLE2:
return true;
// True if module supports groups.
case FEATURE_GROUPS:
return true;
// True if module supports groupings.
case FEATURE_GROUPINGS:
return true;
// True if module can provide a grade.
case FEATURE_GRADE_HAS_GRADE:
return true;
// True if module supports outcomes.
case FEATURE_GRADE_OUTCOMES:
return true;
// True if module can show description on course main page.
case FEATURE_SHOW_DESCRIPTION:
return true;
// Type of module.
// This effects how icons are displayed?
// case FEATURE_MOD_PURPOSE:
// return MOD_PURPOSE_CONTENT;
default:
return null;
}
}
/**
* Saves a new instance of the cmi5launch into the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $cmi5launch An object from the form in mod_form.php
* @param mod_cmi5launch_mod_form $mform
* @return int The id of the newly inserted cmi5launch record
*/
function cmi5launch_add_instance(stdClass $cmi5launch, mod_cmi5launch_mod_form $mform = null) {
global $DB, $CFG;
$cmi5launch->timecreated = time();
// Need the id of the newly created instance to return (and use if override defaults checkbox is checked).
$cmi5launch->id = $DB->insert_record('cmi5launch', $cmi5launch);
// Process uploaded file.
if (!empty($cmi5launch->packagefile)) {
cmi5launch_process_new_package($cmi5launch);
}
return $cmi5launch->id;
}
/**
* Updates an instance of the cmi5launch in the database
*
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $cmi5launch An object from the form in mod_form.php
* @param mod_cmi5launch_mod_form $mform
* @return boolean Success/Fail
*/
function cmi5launch_update_instance(stdClass $cmi5launch, mod_cmi5launch_mod_form $mform = null) {
global $DB, $CFG;
$cmi5launch->timemodified = time();
$cmi5launch->id = $cmi5launch->instance;
// We removed this part of lrs box -MB
// $cmi5launchlrs = cmi5launch_build_lrs_settings($cmi5launch);
/*
// Determine if override defaults checkbox is checked.
if ($cmi5launch->overridedefaults == '1') {
// Check to see if there is a record of this instance in the table.
$cmi5launchlrsid = $DB->get_field(
'cmi5launch_lrs',
'id',
array('cmi5launchid' => $cmi5launch->instance),
IGNORE_MISSING
);
// If not, will need to insert_record.
if (!$cmi5launchlrsid) {
if (!$DB->insert_record('cmi5launch_lrs', $cmi5launchlrs)) {
return false;
}
} else { // If it does exist, update it.
$cmi5launchlrs->id = $cmi5launchlrsid;
if (!$DB->update_record('cmi5launch_lrs', $cmi5launchlrs)) {
return false;
}
}
}
*/
if (!$DB->update_record('cmi5launch', $cmi5launch)) {
return false;
}
// Process uploaded file.
if (!empty($cmi5launch->packagefile)) {
cmi5launch_process_new_package($cmi5launch);
}
return true;
}
function cmi5launch_build_lrs_settings(stdClass $cmi5launch) {
global $DB, $CFG;
// Data for cmi5launch_lrs table.
$cmi5launchlrs = new stdClass();
$cmi5launchlrs->customacchp = $cmi5launch->cmi5launchcustomacchp;
$cmi5launchlrs->useactoremail = $cmi5launch->cmi5launchuseactoremail;
$cmi5launchlrs->cmi5launchid = $cmi5launch->instance;
return $cmi5launchlrs;
}
/**
* Removes an instance of the cmi5launch from the database
*
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function cmi5launch_delete_instance($id) {
global $DB;
// Currently it deletes only the cmi5launch, Do we want it to delete ALL tables? What is it suppossed to do>
// Like if it deletes all tables is it expected to delete user data?
if (! $cmi5launch = $DB->get_record('cmi5launch', array('id' => $id))) {
return false;
}
// FORGET THE HARDCODED, THIS WORKS WITHOUT IT, IT APPEARS TO DELETE EVERYTHIN IN A"COURSE" BUT IF AN ACTIVITY IS DELETED IT DISAPEARS FROM MOODLE
//BUT NOT FROM DB. is THIS SUPPOSSED TO DO DB?
///huh? why is this hardcoded?
// Determine if there is a record of this (ever) in the cmi5launch_lrs table.
/* $cmi5launchlrsid = $DB->get_field('cmi5launch_lrs', 'id', array('cmi5launchid' => $id), $strictness = IGNORE_MISSING);
if ($cmi5launchlrsid) {
// If there is, delete it.
$DB->delete_records('cmi5launch_lrs', array('id' => $cmi5launchlrsid));
}
*/
$DB->delete_records('cmi5launch', array('id' => $cmi5launch->id));
return true;
}
/**
* Returns a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @return stdClass|null
*/
function cmi5launch_user_outline($course, $user, $mod, $cmi5launch) {
$return = new stdClass();
$return->time = 0;
$return->info = '';
return $return;
}
/**
* Prints a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param stdClass $course the current course record
* @param stdClass $user the record of the user we are generating report for
* @param cm_info $mod course module info
* @param stdClass $cmi5launch the module instance record
* @return void, is supposed to echp directly
*/
function cmi5launch_user_complete($course, $user, $mod, $cmi5launch) {
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in cmi5launch activities and print it out.
* Return true if there was output, or false is there was none.
*
* @return boolean
*/
function cmi5launch_print_recent_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Prepares the recent activity data
*
* This callback function is supposed to populate the passed array with
* custom activity records. These records are then rendered into HTML via
* {@link cmi5launch_print_recent_mod_activity()}.
*
* @param array $activities sequentially indexed array of objects with the 'cmid' property
* @param int $index the index in the $activities to use for the next record
* @param int $timestart append activity since this time
* @param int $courseid the id of the course we produce the report for
* @param int $cmid course module id
* @param int $userid check for a particular user's activity only, defaults to 0 (all users)
* @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
* @return void adds items into $activities and increases $index
*/
function cmi5launch_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid = 0, $groupid = 0) {
}
/**
* Prints single activity item prepared by {@see cmi5launch_get_recent_mod_activity()}
* @return void
*/
function cmi5launch_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* @return boolean
* @todo Finish documenting this function
**/
function cmi5launch_cron() {
return true;
}
/**
* Returns all other caps used in the module
*
* @example return array('moodle/site:accessallgroups');
* @return array
*/
function cmi5launch_get_extra_capabilities() {
return array();
}
// File API.
/**
* 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 {@link file_browser::get_file_info_context_module()}
*
* @param stdClass $course
* @param stdClass $cm
* @param stdClass $context
* @return array of [(string)filearea] => (string)description
*/
function cmi5launch_get_file_areas($course, $cm, $context) {
$areas = array();
$areas['content'] = get_string('areacontent', 'scorm');
$areas['package'] = get_string('areapackage', 'scorm');
return $areas;
}
/**
* File browsing support for cmi5launch file areas
*
* @package mod_cmi5launch
* @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 cmi5launch_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
global $CFG;
if (!has_capability('moodle/course:managefiles', $context)) {
return null;
}
$fs = get_file_storage();
if ($filearea === 'package') {
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
$urlbase = $CFG->wwwroot.'/pluginfile.php';
if (!$storedfile = $fs->get_file($context->id, 'mod_cmi5launch', 'package', 0, $filepath, $filename)) {
if ($filepath === '/' && $filename === '.') {
$storedfile = new virtual_root_file($context->id, 'mod_cmi5launch', 'package', 0);
} else {
// Not found.
return null;
}
}
return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
}
return false;
}
/**
* Serves cmi5 content, introduction images and packages. Implements needed access control ;-)
*
* @package mod_cmi5launch
* @category files
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @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 cmi5launch_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
global $CFG, $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login($course, true, $cm);
$canmanageactivity = has_capability('moodle/course:manageactivities', $context);
$filename = array_pop($args);
$filepath = implode('/', $args);
if ($filearea === 'content') {
$lifetime = null;
} else if ($filearea === 'package') {
$lifetime = 0; // No caching here.
} else {
return false;
}
$fs = get_file_storage();
if (
(!$file = $fs->get_file($context->id, 'mod_cmi5launch', $filearea, 0, '/'.$filepath.'/', $filename))
|| ($file->is_directory())
) {
if ($filearea === 'content') { // Return file not found straight away to improve performance.
send_header_404();
die;
}
return false;
}
// Finally send the file.
send_stored_file($file, $lifetime, 0, false, $options);
}
/**
* Export file resource contents for web service access.
*
* @param cm_info $cm Course module object.
* @param string $baseurl Base URL for Moodle.
* @return array array of file content
*/
function cmi5launch_export_contents($cm, $baseurl) {
global $CFG;
$contents = array();
$context = context_module::instance($cm->id);
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_cmi5launch', 'package', 0, 'sortorder DESC, id ASC', false);
foreach ($files as $fileinfo) {
$file = array();
$file['type'] = 'file';
$file['filename'] = $fileinfo->get_filename();
$file['filepath'] = $fileinfo->get_filepath();
$file['filesize'] = $fileinfo->get_filesize();
$file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_cmi5launch/package'.
$fileinfo->get_filepath().$fileinfo->get_filename(), true);
$file['timecreated'] = $fileinfo->get_timecreated();
$file['timemodified'] = $fileinfo->get_timemodified();
$file['sortorder'] = $fileinfo->get_sortorder();
$file['userid'] = $fileinfo->get_userid();
$file['author'] = $fileinfo->get_author();
$file['license'] = $fileinfo->get_license();
$contents[] = $file;
}
return $contents;
}
// Navigation API.
/**
* Extends the global navigation tree by adding cmi5launch nodes if there is a relevant content
*
* This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
*
* @param navigation_node $navref An object representing the navigation tree node of the cmi5launch module instance
* @param stdClass $course
* @param stdClass $module
* @param cm_info $cm
*/
function cmi5launch_extend_navigation(navigation_node $navref, stdclass $course, stdclass $module, cm_info $cm) {
}
/**
* Extends the settings navigation with the cmi5launch settings
*
* This function is called when the context for the page is a cmi5launch module. This is not called by AJAX
* so it is safe to rely on the $PAGE.
*
* @param settings_navigation $settingsnav {@link settings_navigation}
* @param navigation_node $cmi5launchnode {@link navigation_node}
*/
function cmi5launch_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $cmi5launchnode = null) {
}
// Called by Moodle core.
function cmi5launch_get_completion_state($course, $cm, $userid, $type) {
global $CFG, $DB;
$result = $type; // Default return value.
// Get cmi5launch.
if (!$cmi5launch = $DB->get_record('cmi5launch', array('id' => $cm->instance))) {
throw new Exception("Can't find activity {$cm->instance}"); // TODO: localise this.
}
$cmi5launchsettings = cmi5launch_settings($cm->instance);
$expirydate = null;
$expirydays = $cmi5launch->cmi5expiry;
if ($expirydays > 0) {
$expirydatetime = new DateTime();
$expirydatetime->sub(new DateInterval('P'.$expirydays.'D'));
$expirydate = $expirydatetime->format('c');
}
if (!empty($cmi5launch->cmi5verbid)) {
// Try to get a statement matching actor, verb and object specified in module settings.
$statementquery = cmi5launch_get_statements(
$cmi5launchsettings['cmi5launchlrsendpoint'],
$cmi5launchsettings['cmi5launchlrslogin'],
$cmi5launchsettings['cmi5launchlrspass'],
$cmi5launchsettings['cmi5launchlrsversion'],
$cmi5launch->cmi5activityid,
cmi5launch_getactor($cm->instance),
$cmi5launch->cmi5verbid,
$expirydate
);
// If the statement exists, return true else return false.
if (!empty($statementquery->content) && $statementquery->success) {
$result = true;
} else {
$result = false;
}
}
return $result;
}
// Cmi5Launch specific functions.
/*
The functions below should really be in locallib, however they are required for one
or more of the functions above so need to be here.
It looks like the standard Quiz module does that same thing, so I don't feel so bad.
*/
/**
* Handles uploaded zip packages when a module is added or updated. Unpacks the zip contents
* and extracts the launch url and activity id from the cmi5.xml file.
* Note: This takes the *first* activity from the cmi5.xml file to be the activity intended
* to be launched. It will not go hunting for launch URLs any activities listed below.
* Based closely on code from the SCORM and (to a lesser extent) Resource modules.
* @package mod_cmi5launch
* @category cmi5
* @param object $cmi5launch An object from the form in mod_form.php
* @return array empty if no issue is found. Array of error message otherwise
*/
function cmi5launch_process_new_package($cmi5launch) {
global $DB, $CFG;
$cmid = $cmi5launch->coursemodule;
$context = context_module::instance($cmid);
// Bring in functions from classes cmi5Connector and AU helpers.
$connectors = new cmi5_connectors;
$auhelper = new au_helpers;
// Bring in functions from class cmi5_table_connectors and AU helpers.
$createcourse = $connectors->cmi5launch_get_create_course();
$retrieveaus = $auhelper->get_cmi5launch_retrieve_aus();
// Reload cmi5 instance.
$record = $DB->get_record('cmi5launch', array('id' => $cmi5launch->id));
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_cmi5launch', 'package');
file_save_draft_area_files(
$cmi5launch->packagefile,
$context->id,
'mod_cmi5launch',
'package',
0,
array('subdirs' => 0, 'maxfiles' => 1)
);
// Get filename of zip that was uploaded.
$files = $fs->get_area_files($context->id, 'mod_cmi5launch', 'package', 0, '', false);
if (count($files) < 1) {
return false;
}
$zipfile = reset($files);
$zipfilename = $zipfile->get_filename();
$packagefile = false;
$packagefile = $fs->get_file($context->id, 'mod_cmi5launch', 'package', 0, '/', $zipfilename);
// Retrieve user settings to apply to newly created record.
$settings = cmi5launch_settings($cmi5launch->id);
$token = $settings['cmi5launchtenanttoken'];
// Create the course and retrieve info for saving to DB.
$courseresults = $createcourse($context->id, $token, $packagefile);
// Take the results of created course and save new course id to table.
$record->courseinfo = $courseresults;
$returnedinfo = json_decode($courseresults, true);
// Retrieve the lmsId of course.
$lmsid = $returnedinfo["lmsId"];
$record->cmi5activityid = $lmsid;
$record->courseid = $returnedinfo["id"];
// TEST
$test = "";
// Create url for sending to when requesting launch url for course.
$playerurl = $settings['cmi5launchplayerurl'];
// Build and save launchurl.
$url = $playerurl . "/api/v1/" . $record->courseid . "/launch-url/";
$record->launchurl = $url;
$aus = ($retrieveaus($returnedinfo));
$record->aus = (json_encode($aus));
$fs->delete_area_files($context->id, 'mod_cmi5launch', 'content');
$packer = get_file_packer('application/zip');
$packagefile->extract_to_storage($packer, $context->id, 'mod_cmi5launch', 'content', 0, '/');
// If the cmi5.xml file isn't there, don't do try to use it.
// This is unlikely as it should have been checked when the file was validated.
if ($manifestfile = $fs->get_file($context->id, 'mod_cmi5launch', 'content', 0, '/', 'cmi5.xml')) {
$xmltext = $manifestfile->get_content();
$defaultorgid = 0;
$firstinorg = 0;
$pattern = '/&(?!\w{2,6};)/';
$replacement = '&';
$xmltext = preg_replace($pattern, $replacement, $xmltext);
$objxml = new xml2Array();
$manifest = $objxml->parse($xmltext);
// Update activity id from the first activity in cmi5.xml, if it is found.
// Skip without error if not. (The Moodle admin will need to enter the id manually).
if (isset ($manifest[0]["children"][0]["children"][0]["attrs"]["ID"])) {
$record->cmi5activityid = $manifest[0]["children"][0]["children"][0]["attrs"]["ID"];
}
// Update launch from the first activity in cmi5.xml, if it is found.
// Skip if not. (The Moodle admin will need to enter the url manually).
foreach ($manifest[0]["children"][0]["children"][0]["children"] as $property) {
if ($property["name"] === "LAUNCH") {
$record->cmi5launchurl = $CFG->wwwroot . "/pluginfile.php/" . $context->id . "/mod_cmi5launch/"
. $manifestfile->get_filearea() . "/" . $property["tagData"];
}
}
// Save reference.
// Turn off to trigger echo.
return $DB->update_record('cmi5launch', $record);
}
}
/**
* Check that a Zip file contains a cmi5.xml file in the right place. Used in mod_form.php.
* Heavily based on scorm_validate_package in /mod/scorm/lib.php
* @package mod_cmi5launch
* @category cmi5
* @param stored_file $file a Zip file.
* @return array empty if no issue is found. Array of error message otherwise
*/
function cmi5launch_validate_package($file) {
$packer = get_file_packer('application/zip');
$errors = array();
$filelist = $file->list_files($packer);
if (!is_array($filelist)) {
$errors['packagefile'] = get_string('badarchive', 'cmi5launch');
} else {
$badmanifestpresent = false;
foreach ($filelist as $info) {
if ($info->pathname == 'cmi5.xml') {
return array();
} else if (strpos($info->pathname, 'cmi5.xml') !== false) {
// This package has cmi5 xml file inside a folder of the package.
$badmanifestpresent = true;
}
if (preg_match('/\.cst$/', $info->pathname)) {
return array();
}
}
if ($badmanifestpresent) {
$errors['packagefile'] = get_string('badimsmanifestlocation', 'cmi5launch');
} else {
$errors['packagefile'] = get_string('nomanifest', 'cmi5launch');
}
}
return $errors;
}
/**
* Check for AUs and their satisifed status in a block. Recursive to handle nested blocks.
*
* @package mod_cmi5launch
* @category cmi5
* @param mixed bool|array - $auinfoin - the info containing a block, au, or true/false au satisfied value
* @param string $aulmsid - the lms id of the au we are looking for.
* @return mixed - returns an array of aus or au satisfied value.
*/
function cmi5launch_find_au_satisfied($auinfoin, $aulmsid) {
$ausatisfied = "";
// Check if auinfoin is a boolean or an array.
// It will be an array if an AU was found on recursive call.
if (is_bool($auinfoin)) {
// Return value to func that called it.
return $auinfoin;
// If it's an array it is either a block we still need to break down, or an AU we need to find satisfied value for.
} else if (is_array($auinfoin)) {
// Check AU's satisifeid value and display accordingly.
foreach ($auinfoin as $key => $auinfo) {
$ausatisfied = "false";
// If it's a block, we need to keep breaking it down.
if ($auinfo["type"] == "block" ) {
// Grab its children, this is what other blocks or AU's will be nested in.
$auchildren = $auinfo["children"];
// Now recursively call function again.
$ausatisfied = cmi5launch_find_au_satisfied($auchildren, $aulmsid);
// If it's an AU, we need to check if it's the one we are looking for.
} else if ($auinfo["type"] == "au") {
// Search for the correct lms id and take only the AU that matches.
if ( $auinfo["lmsId"] == $aulmsid) {
// If it is, retrieve the satisfied value.
$ausatisfied = $auinfo["satisfied"];
} else {
// If no ids match we have a problem, and need to return.
$ausatisfied = "No ids match";
}
} else {
// This shouldn't be reachable, but in case add error message.
echo "Type from statement does not equal either block or AU.";
}
}
} else {
echo"Incorrect value passed to function cmi5launch_find_au_satisfied. Correct values are a boolean or array";
}
return $ausatisfied;
}
/**
* Fetches Statements from the LRS. This is used for completion tracking -
* we check for a statement matching certain criteria for each learner.
*
* @package mod_cmi5launch
* @category cmi5
* @param string $url LRS endpoint URL
* @param string $basiclogin login/key for the LRS
* @param string $basicpass pass/secret for the LRS
* @param string $version version of xAPI to use
* @param string $activityid Activity Id to filter by
* @param cmi5 Agent $agent Agent to filter by
* @param string $verb Verb Id to filter by
* @param string $since Since date to filter by
* @return cmi5 LRS Response
*/
function cmi5launch_get_statements($url, $basiclogin, $basicpass, $version, $activityid, $agent, $verb, $since = null) {
$lrs = new \cmi5\RemoteLRS($url, $version, $basiclogin, $basicpass);
$statementsquery = array(
"agent" => $agent,
"verb" => new \cmi5\Verb(array("id" => trim($verb))),
"activity" => new \cmi5\Activity(array("id" => trim($activityid))),
"related_activities" => "false",
"format" => "ids",
);
if (!is_null($since)) {
$statementsquery["since"] = $since;
}
// Get all the statements from the LRS.
$statementsresponse = $lrs->queryStatements($statementsquery);
if ($statementsresponse->success == false) {
return $statementsresponse;
}
$allthestatements = $statementsresponse->content->getStatements();
$morestatementsurl = $statementsresponse->content->getMore();
while (!empty($morestatementsurl)) {
$morestmtsresponse = $lrs->moreStatements($morestatementsurl);
if ($morestmtsresponse->success == false) {
return $morestmtsresponse;
}
$morestatements = $morestmtsresponse->content->getStatements();
$morestatementsurl = $morestmtsresponse->content->getMore();
// Note: due to the structure of the arrays, array_merge does not work as expected.
foreach ($morestatements as $morestatement) {
array_push($allthestatements, $morestatement);
}
}
return new \cmi5\LRSResponse(
$statementsresponse->success,
$allthestatements,
$statementsresponse->httpResponse
);
}
/**
* Build a cmi5 Agent based on the current user
*
* @package mod_cmi5launch
* @category cmi5
* @return cmi5 Agent $agent Agent
*/
function cmi5launch_getactor($instance) {
global $USER, $CFG;
$settings = cmi5launch_settings($instance);
if ($USER->idnumber && $settings['cmi5launchcustomacchp']) {
$agent = array(
"name" => fullname($USER),
"account" => array(
"homePage" => $settings['cmi5launchcustomacchp'],
"name" => $USER->idnumber,
),
"objectType" => "Agent",
);
} else if ($USER->email && $settings['cmi5launchuseactoremail']) {
$agent = array(
"name" => fullname($USER),
"mbox" => "mailto:".$USER->email,
"objectType" => "Agent",
);
} else {
$agent = array(
"name" => fullname($USER),
"account" => array(
"homePage" => $CFG->wwwroot,
"name" => $USER->username,
),
"objectType" => "Agent",
);
}
return new \cmi5\Agent($agent);
}
/**
* Returns the LRS settings relating to a cmi5 Launch module instance
*
* @package mod_cmi5launch
* @category cmi5
* @param string $instance The Moodle id for the cmi5 module instance.
* @return array LRS settings to use
*/
function cmi5launch_settings($instance) {
global $DB, $CFG, $cmi5launchsettings;
if (!is_null($cmi5launchsettings)) {
return $cmi5launchsettings;
}
$expresult = array();
$result = $DB->get_records('config_plugins', array('plugin' => 'cmi5launch'));
foreach ($result as $value) {
$expresult[$value->name] = $value->value;
}
$expresult['cmi5launchlrsversion'] = '1.0.0';
$cmi5launchsettings = $expresult;
return $expresult;
}
/**
* Should the global LRS settings be used instead of the instance specific ones?
*
* @package mod_cmi5launch
* @category cmi5
* @param string $instance The Moodle id for the cmi5 module instance.
* @return bool
*/
function cmi5launch_use_global_cmi5_lrs_settings($instance) {
global $DB;
// Determine if there is a row in cmi5launch_lrs matching the current activity id.
$activitysettings = $DB->get_record('cmi5launch', array('id' => $instance));
/* Removed override defaults from db
if ($activitysettings->overridedefaults == 1) {
return false;
}
*/
return true;
}
// Grade functions.
/**
* Return grade for given user or all users.
*
* @global stdClass
* @global object
* @param int $cmi5id id of scorm
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function cmi5launch_get_user_grades($cmi5launch, $userid=0) {
// External class and functions.
$gradehelpers = new grade_helpers;
$updategrades = $gradehelpers->get_cmi5launch_check_user_grades_for_updates();
global $CFG, $DB;
$id = required_param('id', PARAM_INT);
$contextmodule = context_module::instance($id);
$grades = array();
// If userid is empty it means we want all users.
if (empty($userid)) {
// If the userid is empty, use get_enrolled_users for this course then update all their grades.
$users = get_enrolled_users($contextmodule);
// If there is a list of users then iterate through it and make grade objects with them and their updated grades.
if ($users) {
foreach ($users as $user) {
$grades[$user->id] = new stdClass();
$grades[$user->id]->id = $user->id;
$grades[$user->id]->userid = $user->id;
$grades[$user->id]->rawgrade = $updategrades($user);
}
} else {
// Return false to avoid null values if no users.
return false;
}
} else {
// This is if we have a specific user, so we need to retrieve their information.
$user = $DB->get_record('user', ['id' => $userid]);
// Make grade objects with the individual user and their updated grades.
$grades[$userid] = new stdClass();
$grades[$userid]->id = $userid;
$grades[$userid]->userid = $userid;
$grades[$userid]->rawgrade = $updategrades($user);
}
return $grades;
}
/**
* Update grades in central gradebook.
* @category grade
* @param object $cmi5launch - mod object
* @param int $userid - A user ID or 0 for all users.
* @param bool $nullifnone - If true and a single user is specified with no grade, a grade item with a null rawgrade is inserted.
*/
// This function is called automatically by moodle if it needs the users grades updated.
// It can also be called manually when you want to push a new grade to gradebook.
// This function should do whatever is needed to generate the relevant grades to push into gradebook
// then call 'myplugin_grade_item_update with the grades to write.
function cmi5launch_update_grades($cmi5launch, $userid = 0, $nullifnone = true) {