forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
1528 lines (1351 loc) · 52.5 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/>.
/**
* @package mod_scorm
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** SCORM_TYPE_LOCAL = local */
define('SCORM_TYPE_LOCAL', 'local');
/** SCORM_TYPE_LOCALSYNC = localsync */
define('SCORM_TYPE_LOCALSYNC', 'localsync');
/** SCORM_TYPE_EXTERNAL = external */
define('SCORM_TYPE_EXTERNAL', 'external');
/** SCORM_TYPE_AICCURL = external AICC url */
define('SCORM_TYPE_AICCURL', 'aiccurl');
define('SCORM_TOC_SIDE', 0);
define('SCORM_TOC_HIDDEN', 1);
define('SCORM_TOC_POPUP', 2);
define('SCORM_TOC_DISABLED', 3);
// Used to show/hide navigation buttons and set their position.
define('SCORM_NAV_DISABLED', 0);
define('SCORM_NAV_UNDER_CONTENT', 1);
define('SCORM_NAV_FLOATING', 2);
// Used to check what SCORM version is being used.
define('SCORM_12', 1);
define('SCORM_13', 2);
define('SCORM_AICC', 3);
// List of possible attemptstatusdisplay options.
define('SCORM_DISPLAY_ATTEMPTSTATUS_NO', 0);
define('SCORM_DISPLAY_ATTEMPTSTATUS_ALL', 1);
define('SCORM_DISPLAY_ATTEMPTSTATUS_MY', 2);
define('SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY', 3);
/**
* Return an array of status options
*
* Optionally with translated strings
*
* @param bool $with_strings (optional)
* @return array
*/
function scorm_status_options($withstrings = false) {
// Id's are important as they are bits.
$options = array(
2 => 'passed',
4 => 'completed'
);
if ($withstrings) {
foreach ($options as $key => $value) {
$options[$key] = get_string('completionstatus_'.$value, 'scorm');
}
}
return $options;
}
/**
* 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.
*
* @global stdClass
* @global object
* @uses CONTEXT_MODULE
* @uses SCORM_TYPE_LOCAL
* @uses SCORM_TYPE_LOCALSYNC
* @uses SCORM_TYPE_EXTERNAL
* @param object $scorm Form data
* @param object $mform
* @return int new instance id
*/
function scorm_add_instance($scorm, $mform=null) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
if (empty($scorm->timeopen)) {
$scorm->timeopen = 0;
}
if (empty($scorm->timeclose)) {
$scorm->timeclose = 0;
}
if (empty($scorm->completionstatusallscos)) {
$scorm->completionstatusallscos = 0;
}
$cmid = $scorm->coursemodule;
$cmidnumber = $scorm->cmidnumber;
$courseid = $scorm->course;
$context = context_module::instance($cmid);
$scorm = scorm_option2text($scorm);
$scorm->width = (int)str_replace('%', '', $scorm->width);
$scorm->height = (int)str_replace('%', '', $scorm->height);
if (!isset($scorm->whatgrade)) {
$scorm->whatgrade = 0;
}
$id = $DB->insert_record('scorm', $scorm);
// Update course module record - from now on this instance properly exists and all function may be used.
$DB->set_field('course_modules', 'instance', $id, array('id' => $cmid));
// Reload scorm instance.
$record = $DB->get_record('scorm', array('id' => $id));
// Store the package and verify.
if ($record->scormtype === SCORM_TYPE_LOCAL) {
if (!empty($scorm->packagefile)) {
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_scorm', 'package');
file_save_draft_area_files($scorm->packagefile, $context->id, 'mod_scorm', 'package',
0, array('subdirs' => 0, 'maxfiles' => 1));
// Get filename of zip that was uploaded.
$files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, '', false);
$file = reset($files);
$filename = $file->get_filename();
if ($filename !== false) {
$record->reference = $filename;
}
}
} else if ($record->scormtype === SCORM_TYPE_LOCALSYNC) {
$record->reference = $scorm->packageurl;
} else if ($record->scormtype === SCORM_TYPE_EXTERNAL) {
$record->reference = $scorm->packageurl;
} else if ($record->scormtype === SCORM_TYPE_AICCURL) {
$record->reference = $scorm->packageurl;
$record->hidetoc = SCORM_TOC_DISABLED; // TOC is useless for direct AICCURL so disable it.
} else {
return false;
}
// Save reference.
$DB->update_record('scorm', $record);
// Extra fields required in grade related functions.
$record->course = $courseid;
$record->cmidnumber = $cmidnumber;
$record->cmid = $cmid;
scorm_parse($record, true);
scorm_grade_item_update($record);
return $record->id;
}
/**
* 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.
*
* @global stdClass
* @global object
* @uses CONTEXT_MODULE
* @uses SCORM_TYPE_LOCAL
* @uses SCORM_TYPE_LOCALSYNC
* @uses SCORM_TYPE_EXTERNAL
* @param object $scorm Form data
* @param object $mform
* @return bool
*/
function scorm_update_instance($scorm, $mform=null) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
if (empty($scorm->timeopen)) {
$scorm->timeopen = 0;
}
if (empty($scorm->timeclose)) {
$scorm->timeclose = 0;
}
if (empty($scorm->completionstatusallscos)) {
$scorm->completionstatusallscos = 0;
}
$cmid = $scorm->coursemodule;
$cmidnumber = $scorm->cmidnumber;
$courseid = $scorm->course;
$scorm->id = $scorm->instance;
$context = context_module::instance($cmid);
if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
if (!empty($scorm->packagefile)) {
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_scorm', 'package');
file_save_draft_area_files($scorm->packagefile, $context->id, 'mod_scorm', 'package',
0, array('subdirs' => 0, 'maxfiles' => 1));
// Get filename of zip that was uploaded.
$files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, '', false);
$file = reset($files);
$filename = $file->get_filename();
if ($filename !== false) {
$scorm->reference = $filename;
}
}
} else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
$scorm->reference = $scorm->packageurl;
} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
$scorm->reference = $scorm->packageurl;
} else if ($scorm->scormtype === SCORM_TYPE_AICCURL) {
$scorm->reference = $scorm->packageurl;
$scorm->hidetoc = SCORM_TOC_DISABLED; // TOC is useless for direct AICCURL so disable it.
} else {
return false;
}
$scorm = scorm_option2text($scorm);
$scorm->width = (int)str_replace('%', '', $scorm->width);
$scorm->height = (int)str_replace('%', '', $scorm->height);
$scorm->timemodified = time();
if (!isset($scorm->whatgrade)) {
$scorm->whatgrade = 0;
}
$DB->update_record('scorm', $scorm);
$scorm = $DB->get_record('scorm', array('id' => $scorm->id));
// Extra fields required in grade related functions.
$scorm->course = $courseid;
$scorm->idnumber = $cmidnumber;
$scorm->cmid = $cmid;
scorm_parse($scorm, (bool)$scorm->updatefreq);
scorm_grade_item_update($scorm);
scorm_update_grades($scorm);
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @global stdClass
* @global object
* @param int $id Scorm instance id
* @return boolean
*/
function scorm_delete_instance($id) {
global $CFG, $DB;
if (! $scorm = $DB->get_record('scorm', array('id' => $id))) {
return false;
}
$result = true;
// Delete any dependent records.
if (! $DB->delete_records('scorm_scoes_track', array('scormid' => $scorm->id))) {
$result = false;
}
if ($scoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id))) {
foreach ($scoes as $sco) {
if (! $DB->delete_records('scorm_scoes_data', array('scoid' => $sco->id))) {
$result = false;
}
}
$DB->delete_records('scorm_scoes', array('scorm' => $scorm->id));
}
if (! $DB->delete_records('scorm', array('id' => $scorm->id))) {
$result = false;
}
/*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
$result = false;
}
if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
$result = false;
}*/
scorm_grade_item_delete($scorm);
return $result;
}
/**
* Return 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.
*
* @global stdClass
* @param int $course Course id
* @param int $user User id
* @param int $mod
* @param int $scorm The scorm id
* @return mixed
*/
function scorm_user_outline($course, $user, $mod, $scorm) {
global $CFG;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
$result = new stdClass();
$result->info = get_string('grade') . ': '. $grade->str_long_grade;
// Datesubmitted == time created. dategraded == time modified or time overridden
// if grade was last modified by the user themselves use date graded. Otherwise use date submitted.
// TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704.
if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
$result->time = $grade->dategraded;
} else {
$result->time = $grade->datesubmitted;
}
return $result;
}
return null;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @global stdClass
* @global object
* @param object $course
* @param object $user
* @param object $mod
* @param object $scorm
* @return boolean
*/
function scorm_user_complete($course, $user, $mod, $scorm) {
global $CFG, $DB, $OUTPUT;
require_once("$CFG->libdir/gradelib.php");
$liststyle = 'structlist';
$now = time();
$firstmodify = $now;
$lastmodify = 0;
$sometoreport = false;
$report = '';
// First Access and Last Access dates for SCOs.
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
$timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
$firstmodify = $timetracks->start;
$lastmodify = $timetracks->finish;
$grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
if ($grade->str_feedback) {
echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
}
}
if ($orgs = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
$DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
$DB->sql_isempty('scorm_scoes', 'organization', false, false),
array($scorm->id), 'sortorder, id', 'id, identifier, title')) {
if (count($orgs) <= 1) {
unset($orgs);
$orgs = array();
$org = new stdClass();
$org->identifier = '';
$orgs[] = $org;
}
$report .= html_writer::start_div('mod-scorm');
foreach ($orgs as $org) {
$conditions = array();
$currentorg = '';
if (!empty($org->identifier)) {
$report .= html_writer::div($org->title, 'orgtitle');
$currentorg = $org->identifier;
$conditions['organization'] = $currentorg;
}
$report .= html_writer::start_tag('ul', array('id' => '0', 'class' => $liststyle));
$conditions['scorm'] = $scorm->id;
if ($scoes = $DB->get_records('scorm_scoes', $conditions, "sortorder, id")) {
// Drop keys so that we can access array sequentially.
$scoes = array_values($scoes);
$level = 0;
$sublist = 1;
$parents[$level] = '/';
foreach ($scoes as $pos => $sco) {
if ($parents[$level] != $sco->parent) {
if ($level > 0 && $parents[$level - 1] == $sco->parent) {
$report .= html_writer::end_tag('ul').html_writer::end_tag('li');
$level--;
} else {
$i = $level;
$closelist = '';
while (($i > 0) && ($parents[$level] != $sco->parent)) {
$closelist .= html_writer::end_tag('ul').html_writer::end_tag('li');
$i--;
}
if (($i == 0) && ($sco->parent != $currentorg)) {
$report .= html_writer::start_tag('li');
$report .= html_writer::start_tag('ul', array('id' => $sublist, 'class' => $liststyle));
$level++;
} else {
$report .= $closelist;
$level = $i;
}
$parents[$level] = $sco->parent;
}
}
$report .= html_writer::start_tag('li');
if (isset($scoes[$pos + 1])) {
$nextsco = $scoes[$pos + 1];
} else {
$nextsco = false;
}
if (($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
(($level == 0) || (($level > 0) && ($nextsco->parent == $sco->identifier)))) {
$sublist++;
} else {
$report .= $OUTPUT->spacer(array("height" => "12", "width" => "13"));
}
if ($sco->launch) {
$score = '';
$totaltime = '';
if ($usertrack = scorm_get_tracks($sco->id, $user->id)) {
if ($usertrack->status == '') {
$usertrack->status = 'notattempted';
}
$strstatus = get_string($usertrack->status, 'scorm');
$report .= html_writer::img($OUTPUT->pix_url($usertrack->status, 'scorm'),
$strstatus, array('title' => $strstatus));
} else {
if ($sco->scormtype == 'sco') {
$report .= html_writer::img($OUTPUT->pix_url('notattempted', 'scorm'),
get_string('notattempted', 'scorm'),
array('title' => get_string('notattempted', 'scorm')));
} else {
$report .= html_writer::img($OUTPUT->pix_url('asset', 'scorm'), get_string('asset', 'scorm'),
array('title' => get_string('asset', 'scorm')));
}
}
$report .= " $sco->title $score$totaltime".html_writer::end_tag('li');
if ($usertrack !== false) {
$sometoreport = true;
$report .= html_writer::start_tag('li').html_writer::start_tag('ul', array('class' => $liststyle));
foreach ($usertrack as $element => $value) {
if (substr($element, 0, 3) == 'cmi') {
$report .= html_writer::tag('li', $element.' => '.s($value));
}
}
$report .= html_writer::end_tag('ul').html_writer::end_tag('li');
}
} else {
$report .= " $sco->title".html_writer::end_tag('li');
}
}
for ($i = 0; $i < $level; $i++) {
$report .= html_writer::end_tag('ul').html_writer::end_tag('li');
}
}
$report .= html_writer::end_tag('ul').html_writer::empty_tag('br');
}
$report .= html_writer::end_div();
}
if ($sometoreport) {
if ($firstmodify < $now) {
$timeago = format_time($now - $firstmodify);
echo get_string('firstaccess', 'scorm').': '.userdate($firstmodify).' ('.$timeago.")".html_writer::empty_tag('br');
}
if ($lastmodify > 0) {
$timeago = format_time($now - $lastmodify);
echo get_string('lastaccess', 'scorm').': '.userdate($lastmodify).' ('.$timeago.")".html_writer::empty_tag('br');
}
echo get_string('report', 'scorm').":".html_writer::empty_tag('br');
echo $report;
} else {
print_string('noactivity', 'scorm');
}
return true;
}
/**
* 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 ...
*
* @global stdClass
* @global object
* @return boolean
*/
function scorm_cron () {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
$sitetimezone = core_date::get_server_timezone();
// Now see if there are any scorm updates to be done.
if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time.
set_config('scorm_updatetimelast', 0);
}
$timenow = time();
$updatetime = usergetmidnight($timenow, $sitetimezone);
if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
set_config('scorm_updatetimelast', $timenow);
mtrace('Updating scorm packages which require daily update');// We are updating.
$scormsupdate = $DB->get_records('scorm', array('updatefreq' => SCORM_UPDATE_EVERYDAY));
foreach ($scormsupdate as $scormupdate) {
scorm_parse($scormupdate, true);
}
// Now clear out AICC session table with old session data.
$cfgscorm = get_config('scorm');
if (!empty($cfgscorm->allowaicchacp)) {
$expiretime = time() - ($cfgscorm->aicchacpkeepsessiondata * 24 * 60 * 60);
$DB->delete_records_select('scorm_aicc_session', 'timemodified < ?', array($expiretime));
}
}
return true;
}
/**
* Return grade for given user or all users.
*
* @global stdClass
* @global object
* @param int $scormid id of scorm
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function scorm_get_user_grades($scorm, $userid=0) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
$grades = array();
if (empty($userid)) {
$scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid",
array($scorm->id), "", "userid,null");
if ($scousers) {
foreach ($scousers as $scouser) {
$grades[$scouser->userid] = new stdClass();
$grades[$scouser->userid]->id = $scouser->userid;
$grades[$scouser->userid]->userid = $scouser->userid;
$grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
}
} else {
return false;
}
} else {
$preattempt = $DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid",
array($scorm->id, $userid), "", "userid,null");
if (!$preattempt) {
return false; // No attempt yet.
}
$grades[$userid] = new stdClass();
$grades[$userid]->id = $userid;
$grades[$userid]->userid = $userid;
$grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
}
return $grades;
}
/**
* Update grades in central gradebook
*
* @category grade
* @param object $scorm
* @param int $userid specific user only, 0 mean all
* @param bool $nullifnone
*/
function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/completionlib.php');
if ($grades = scorm_get_user_grades($scorm, $userid)) {
scorm_grade_item_update($scorm, $grades);
// Set complete.
scorm_set_completion($scorm, $userid, COMPLETION_COMPLETE, $grades);
} else if ($userid and $nullifnone) {
$grade = new stdClass();
$grade->userid = $userid;
$grade->rawgrade = null;
scorm_grade_item_update($scorm, $grade);
// Set incomplete.
scorm_set_completion($scorm, $userid, COMPLETION_INCOMPLETE);
} else {
scorm_grade_item_update($scorm);
}
}
/**
* Update/create grade item for given scorm
*
* @category grade
* @uses GRADE_TYPE_VALUE
* @uses GRADE_TYPE_NONE
* @param object $scorm object with extra cmidnumber
* @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return object grade_item
*/
function scorm_grade_item_update($scorm, $grades=null) {
global $CFG, $DB;
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
require_once($CFG->libdir.'/gradelib.php');
}
$params = array('itemname' => $scorm->name);
if (isset($scorm->cmidnumber)) {
$params['idnumber'] = $scorm->cmidnumber;
}
if ($scorm->grademethod == GRADESCOES) {
$maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND '.
$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id));
if ($maxgrade) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $maxgrade;
$params['grademin'] = 0;
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}
} else {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $scorm->maxgrade;
$params['grademin'] = 0;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
}
/**
* Delete grade item for given scorm
*
* @category grade
* @param object $scorm object
* @return object grade_item
*/
function scorm_grade_item_delete($scorm) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, null, array('deleted' => 1));
}
/**
* List the actions that correspond to a view of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = 'r' and edulevel = LEVEL_PARTICIPATING will
* be considered as view action.
*
* @return array
*/
function scorm_get_view_actions() {
return array('pre-view', 'view', 'view all', 'report');
}
/**
* List the actions that correspond to a post of this module.
* This is used by the participation report.
*
* Note: This is not used by new logging system. Event with
* crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
* will be considered as post action.
*
* @return array
*/
function scorm_get_post_actions() {
return array();
}
/**
* @param object $scorm
* @return object $scorm
*/
function scorm_option2text($scorm) {
$scormpopoupoptions = scorm_get_popup_options_array();
if (isset($scorm->popup)) {
if ($scorm->popup == 1) {
$optionlist = array();
foreach ($scormpopoupoptions as $name => $option) {
if (isset($scorm->$name)) {
$optionlist[] = $name.'='.$scorm->$name;
} else {
$optionlist[] = $name.'=0';
}
}
$scorm->options = implode(',', $optionlist);
} else {
$scorm->options = '';
}
} else {
$scorm->popup = 0;
$scorm->options = '';
}
return $scorm;
}
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the scorm.
*
* @param object $mform form passed by reference
*/
function scorm_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
$mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts', 'scorm'));
}
/**
* Course reset form defaults.
*
* @return array
*/
function scorm_reset_course_form_defaults($course) {
return array('reset_scorm' => 1);
}
/**
* Removes all grades from gradebook
*
* @global stdClass
* @global object
* @param int $courseid
* @param string optional type
*/
function scorm_reset_gradebook($courseid, $type='') {
global $CFG, $DB;
$sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
FROM {scorm} s, {course_modules} cm, {modules} m
WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
foreach ($scorms as $scorm) {
scorm_grade_item_update($scorm, 'reset');
}
}
}
/**
* Actual implementation of the reset course functionality, delete all the
* scorm attempts for course $data->courseid.
*
* @global stdClass
* @global object
* @param object $data the data submitted from the reset course.
* @return array status array
*/
function scorm_reset_userdata($data) {
global $CFG, $DB;
$componentstr = get_string('modulenameplural', 'scorm');
$status = array();
if (!empty($data->reset_scorm)) {
$scormssql = "SELECT s.id
FROM {scorm} s
WHERE s.course=?";
$DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
// Remove all grades from gradebook.
if (empty($data->reset_gradebook_grades)) {
scorm_reset_gradebook($data->courseid);
}
$status[] = array('component' => $componentstr, 'item' => get_string('deleteallattempts', 'scorm'), 'error' => false);
}
// No dates to shift here.
return $status;
}
/**
* Returns all other caps used in module
*
* @return array
*/
function scorm_get_extra_capabilities() {
return array('moodle/site:accessallgroups');
}
/**
* Lists all file areas current user may browse
*
* @param object $course
* @param object $cm
* @param object $context
* @return array
*/
function scorm_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 SCORM file areas
*
* @package mod_scorm
* @category files
* @param file_browser $browser file browser instance
* @param array $areas file areas
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param int $itemid item ID
* @param string $filepath file path
* @param string $filename file name
* @return file_info instance or null if not found
*/
function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
global $CFG;
if (!has_capability('moodle/course:managefiles', $context)) {
return null;
}
// No writing for now!
$fs = get_file_storage();
if ($filearea === 'content') {
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
$urlbase = $CFG->wwwroot.'/pluginfile.php';
if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
if ($filepath === '/' and $filename === '.') {
$storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
} else {
// Not found.
return null;
}
}
require_once("$CFG->dirroot/mod/scorm/locallib.php");
return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
} else 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_scorm', 'package', 0, $filepath, $filename)) {
if ($filepath === '/' and $filename === '.') {
$storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
} else {
// Not found.
return null;
}
}
return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
}
// Scorm_intro handled in file_browser.
return false;
}
/**
* Serves scorm content, introduction images and packages. Implements needed access control ;-)
*
* @package mod_scorm
* @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 scorm_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);
$lifetime = null;
// Check SCORM availability.
if (!$canmanageactivity) {
require_once($CFG->dirroot.'/mod/scorm/locallib.php');
$scorm = $DB->get_record('scorm', array('id' => $cm->instance), 'id, timeopen, timeclose', MUST_EXIST);
list($available, $warnings) = scorm_get_availability_status($scorm);
if (!$available) {
return false;
}
}
if ($filearea === 'content') {
$revision = (int)array_shift($args); // Prevents caching problems - ignored here.
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
// TODO: add any other access restrictions here if needed!
} else if ($filearea === 'package') {
// Check if the global setting for disabling package downloads is enabled.
$protectpackagedownloads = get_config('scorm', 'protectpackagedownloads');
if ($protectpackagedownloads and !$canmanageactivity) {
return false;
}
$revision = (int)array_shift($args); // Prevents caching problems - ignored here.
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
$lifetime = 0; // No caching here.
} else if ($filearea === 'imsmanifest') { // This isn't a real filearea, it's a url parameter for this type of package.
$revision = (int)array_shift($args); // Prevents caching problems - ignored here.
$relativepath = implode('/', $args);
// Get imsmanifest file.
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'mod_scorm', 'package', 0, '', false);
$file = reset($files);
// Check that the package file is an imsmanifest.xml file - if not then this method is not allowed.
$packagefilename = $file->get_filename();
if (strtolower($packagefilename) !== 'imsmanifest.xml') {
return false;
}
$file->send_relative_file($relativepath);
} else {
return false;
}
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
if ($filearea === 'content') { // Return file not found straight away to improve performance.