forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
2001 lines (1799 loc) · 76.8 KB
/
locallib.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 internal classes and functions for module SCORM
*
* @package mod_scorm
* @copyright 1999 onwards Roberto Pinna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("$CFG->dirroot/mod/scorm/lib.php");
require_once("$CFG->libdir/filelib.php");
// Constants and settings for module scorm.
define('SCORM_UPDATE_NEVER', '0');
define('SCORM_UPDATE_EVERYDAY', '2');
define('SCORM_UPDATE_EVERYTIME', '3');
define('SCORM_SKIPVIEW_NEVER', '0');
define('SCORM_SKIPVIEW_FIRST', '1');
define('SCORM_SKIPVIEW_ALWAYS', '2');
define('SCO_ALL', 0);
define('SCO_DATA', 1);
define('SCO_ONLY', 2);
define('GRADESCOES', '0');
define('GRADEHIGHEST', '1');
define('GRADEAVERAGE', '2');
define('GRADESUM', '3');
define('HIGHESTATTEMPT', '0');
define('AVERAGEATTEMPT', '1');
define('FIRSTATTEMPT', '2');
define('LASTATTEMPT', '3');
define('TOCJSLINK', 1);
define('TOCFULLURL', 2);
// Local Library of functions for module scorm.
/**
* @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
*/
class scorm_package_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}
/**
* Returns an array of the popup options for SCORM and each options default value
*
* @return array an array of popup options as the key and their defaults as the value
*/
function scorm_get_popup_options_array() {
$cfgscorm = get_config('scorm');
return array('scrollbars' => isset($cfgscorm->scrollbars) ? $cfgscorm->scrollbars : 0,
'directories' => isset($cfgscorm->directories) ? $cfgscorm->directories : 0,
'location' => isset($cfgscorm->location) ? $cfgscorm->location : 0,
'menubar' => isset($cfgscorm->menubar) ? $cfgscorm->menubar : 0,
'toolbar' => isset($cfgscorm->toolbar) ? $cfgscorm->toolbar : 0,
'status' => isset($cfgscorm->status) ? $cfgscorm->status : 0);
}
/**
* Returns an array of the array of what grade options
*
* @return array an array of what grade options
*/
function scorm_get_grade_method_array() {
return array (GRADESCOES => get_string('gradescoes', 'scorm'),
GRADEHIGHEST => get_string('gradehighest', 'scorm'),
GRADEAVERAGE => get_string('gradeaverage', 'scorm'),
GRADESUM => get_string('gradesum', 'scorm'));
}
/**
* Returns an array of the array of what grade options
*
* @return array an array of what grade options
*/
function scorm_get_what_grade_array() {
return array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'),
AVERAGEATTEMPT => get_string('averageattempt', 'scorm'),
FIRSTATTEMPT => get_string('firstattempt', 'scorm'),
LASTATTEMPT => get_string('lastattempt', 'scorm'));
}
/**
* Returns an array of the array of skip view options
*
* @return array an array of skip view options
*/
function scorm_get_skip_view_array() {
return array(SCORM_SKIPVIEW_NEVER => get_string('never'),
SCORM_SKIPVIEW_FIRST => get_string('firstaccess', 'scorm'),
SCORM_SKIPVIEW_ALWAYS => get_string('always'));
}
/**
* Returns an array of the array of hide table of contents options
*
* @return array an array of hide table of contents options
*/
function scorm_get_hidetoc_array() {
return array(SCORM_TOC_SIDE => get_string('sided', 'scorm'),
SCORM_TOC_HIDDEN => get_string('hidden', 'scorm'),
SCORM_TOC_POPUP => get_string('popupmenu', 'scorm'),
SCORM_TOC_DISABLED => get_string('disabled', 'scorm'));
}
/**
* Returns an array of the array of update frequency options
*
* @return array an array of update frequency options
*/
function scorm_get_updatefreq_array() {
return array(SCORM_UPDATE_NEVER => get_string('never'),
SCORM_UPDATE_EVERYDAY => get_string('everyday', 'scorm'),
SCORM_UPDATE_EVERYTIME => get_string('everytime', 'scorm'));
}
/**
* Returns an array of the array of popup display options
*
* @return array an array of popup display options
*/
function scorm_get_popup_display_array() {
return array(0 => get_string('currentwindow', 'scorm'),
1 => get_string('popup', 'scorm'));
}
/**
* Returns an array of the array of navigation buttons display options
*
* @return array an array of navigation buttons display options
*/
function scorm_get_navigation_display_array() {
return array(SCORM_NAV_DISABLED => get_string('no'),
SCORM_NAV_UNDER_CONTENT => get_string('undercontent', 'scorm'),
SCORM_NAV_FLOATING => get_string('floating', 'scorm'));
}
/**
* Returns an array of the array of attempt options
*
* @return array an array of attempt options
*/
function scorm_get_attempts_array() {
$attempts = array(0 => get_string('nolimit', 'scorm'),
1 => get_string('attempt1', 'scorm'));
for ($i = 2; $i <= 6; $i++) {
$attempts[$i] = get_string('attemptsx', 'scorm', $i);
}
return $attempts;
}
/**
* Returns an array of the attempt status options
*
* @return array an array of attempt status options
*/
function scorm_get_attemptstatus_array() {
return array(SCORM_DISPLAY_ATTEMPTSTATUS_NO => get_string('no'),
SCORM_DISPLAY_ATTEMPTSTATUS_ALL => get_string('attemptstatusall', 'scorm'),
SCORM_DISPLAY_ATTEMPTSTATUS_MY => get_string('attemptstatusmy', 'scorm'),
SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY => get_string('attemptstatusentry', 'scorm'));
}
/**
* Extracts scrom package, sets up all variables.
* Called whenever scorm changes
* @param object $scorm instance - fields are updated and changes saved into database
* @param bool $full force full update if true
* @return void
*/
function scorm_parse($scorm, $full) {
global $CFG, $DB;
$cfgscorm = get_config('scorm');
if (!isset($scorm->cmid)) {
$cm = get_coursemodule_from_instance('scorm', $scorm->id);
$scorm->cmid = $cm->id;
}
$context = context_module::instance($scorm->cmid);
$newhash = $scorm->sha1hash;
if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
$fs = get_file_storage();
$packagefile = false;
$packagefileimsmanifest = false;
if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) {
if ($packagefile->is_external_file()) { // Get zip file so we can check it is correct.
$packagefile->import_external_file_contents();
}
$newhash = $packagefile->get_contenthash();
if (strtolower($packagefile->get_filename()) == 'imsmanifest.xml') {
$packagefileimsmanifest = true;
}
} else {
$newhash = null;
}
} else {
if (!$cfgscorm->allowtypelocalsync) {
// Sorry - localsync disabled.
return;
}
if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
$fs->delete_area_files($context->id, 'mod_scorm', 'package');
$filerecord = array('contextid' => $context->id, 'component' => 'mod_scorm', 'filearea' => 'package',
'itemid' => 0, 'filepath' => '/');
if ($packagefile = $fs->create_file_from_url($filerecord, $scorm->reference, array('calctimeout' => true))) {
$newhash = sha1($scorm->reference);
} else {
$newhash = null;
}
}
}
if ($packagefile) {
if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
if (strpos($scorm->version, 'SCORM') !== false) {
if ($packagefileimsmanifest || $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
// No need to update.
return;
}
} else if (strpos($scorm->version, 'AICC') !== false) {
// TODO: add more sanity checks - something really exists in scorm_content area.
return;
}
}
if (!$packagefileimsmanifest) {
// Now extract files.
$fs->delete_area_files($context->id, 'mod_scorm', 'content');
$packer = get_file_packer('application/zip');
$packagefile->extract_to_storage($packer, $context->id, 'mod_scorm', 'content', 0, '/');
}
} else if (!$full) {
return;
}
if ($packagefileimsmanifest) {
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
// Direct link to imsmanifest.xml file.
if (!scorm_parse_scorm($scorm, $packagefile)) {
$scorm->version = 'ERROR';
}
} else if ($manifest = $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
// SCORM.
if (!scorm_parse_scorm($scorm, $manifest)) {
$scorm->version = 'ERROR';
}
} else {
require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
// AICC.
$result = scorm_parse_aicc($scorm);
if (!$result) {
$scorm->version = 'ERROR';
} else {
$scorm->version = 'AICC';
}
}
} else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $cfgscorm->allowtypeexternal) {
require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
// SCORM only, AICC can not be external.
if (!scorm_parse_scorm($scorm, $scorm->reference)) {
$scorm->version = 'ERROR';
}
$newhash = sha1($scorm->reference);
} else if ($scorm->scormtype === SCORM_TYPE_AICCURL and $cfgscorm->allowtypeexternalaicc) {
require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
// AICC.
$result = scorm_parse_aicc($scorm);
if (!$result) {
$scorm->version = 'ERROR';
} else {
$scorm->version = 'AICC';
}
} else {
// Sorry, disabled type.
return;
}
$scorm->revision++;
$scorm->sha1hash = $newhash;
$DB->update_record('scorm', $scorm);
}
function scorm_array_search($item, $needle, $haystacks, $strict=false) {
if (!empty($haystacks)) {
foreach ($haystacks as $key => $element) {
if ($strict) {
if ($element->{$item} === $needle) {
return $key;
}
} else {
if ($element->{$item} == $needle) {
return $key;
}
}
}
}
return false;
}
function scorm_repeater($what, $times) {
if ($times <= 0) {
return null;
}
$return = '';
for ($i = 0; $i < $times; $i++) {
$return .= $what;
}
return $return;
}
function scorm_external_link($link) {
// Check if a link is external.
$result = false;
$link = strtolower($link);
if (substr($link, 0, 7) == 'http://') {
$result = true;
} else if (substr($link, 0, 8) == 'https://') {
$result = true;
} else if (substr($link, 0, 4) == 'www.') {
$result = true;
}
return $result;
}
/**
* Returns an object containing all datas relative to the given sco ID
*
* @param integer $id The sco ID
* @return mixed (false if sco id does not exists)
*/
function scorm_get_sco($id, $what=SCO_ALL) {
global $DB;
if ($sco = $DB->get_record('scorm_scoes', array('id' => $id))) {
$sco = ($what == SCO_DATA) ? new stdClass() : $sco;
if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $id)))) {
foreach ($scodatas as $scodata) {
$sco->{$scodata->name} = $scodata->value;
}
} else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $id))))) {
$sco->parameters = '';
}
return $sco;
} else {
return false;
}
}
/**
* Returns an object (array) containing all the scoes data related to the given sco ID
*
* @param integer $id The sco ID
* @param integer $organisation an organisation ID - defaults to false if not required
* @return mixed (false if there are no scoes or an array)
*/
function scorm_get_scoes($id, $organisation=false) {
global $DB;
$queryarray = array('scorm' => $id);
if (!empty($organisation)) {
$queryarray['organization'] = $organisation;
}
if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'sortorder, id')) {
// Drop keys so that it is a simple array as expected.
$scoes = array_values($scoes);
foreach ($scoes as $sco) {
if ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $sco->id))) {
foreach ($scodatas as $scodata) {
$sco->{$scodata->name} = $scodata->value;
}
}
}
return $scoes;
} else {
return false;
}
}
function scorm_insert_track($userid, $scormid, $scoid, $attempt, $element, $value, $forcecompleted=false, $trackdata = null) {
global $DB, $CFG;
$id = null;
if ($forcecompleted) {
// TODO - this could be broadened to encompass SCORM 2004 in future.
if (($element == 'cmi.core.lesson_status') && ($value == 'incomplete')) {
if ($track = $DB->get_record_select('scorm_scoes_track',
'userid=? AND scormid=? AND scoid=? AND attempt=? '.
'AND element=\'cmi.core.score.raw\'',
array($userid, $scormid, $scoid, $attempt))) {
$value = 'completed';
}
}
if ($element == 'cmi.core.score.raw') {
if ($tracktest = $DB->get_record_select('scorm_scoes_track',
'userid=? AND scormid=? AND scoid=? AND attempt=? '.
'AND element=\'cmi.core.lesson_status\'',
array($userid, $scormid, $scoid, $attempt))) {
if ($tracktest->value == "incomplete") {
$tracktest->value = "completed";
$DB->update_record('scorm_scoes_track', $tracktest);
}
}
}
if (($element == 'cmi.success_status') && ($value == 'passed' || $value == 'failed')) {
if ($DB->get_record('scorm_scoes_data', array('scoid' => $scoid, 'name' => 'objectivesetbycontent'))) {
$objectiveprogressstatus = true;
$objectivesatisfiedstatus = false;
if ($value == 'passed') {
$objectivesatisfiedstatus = true;
}
if ($track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
'scormid' => $scormid,
'scoid' => $scoid,
'attempt' => $attempt,
'element' => 'objectiveprogressstatus'))) {
$track->value = $objectiveprogressstatus;
$track->timemodified = time();
$DB->update_record('scorm_scoes_track', $track);
$id = $track->id;
} else {
$track = new stdClass();
$track->userid = $userid;
$track->scormid = $scormid;
$track->scoid = $scoid;
$track->attempt = $attempt;
$track->element = 'objectiveprogressstatus';
$track->value = $objectiveprogressstatus;
$track->timemodified = time();
$id = $DB->insert_record('scorm_scoes_track', $track);
}
if ($objectivesatisfiedstatus) {
if ($track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
'scormid' => $scormid,
'scoid' => $scoid,
'attempt' => $attempt,
'element' => 'objectivesatisfiedstatus'))) {
$track->value = $objectivesatisfiedstatus;
$track->timemodified = time();
$DB->update_record('scorm_scoes_track', $track);
$id = $track->id;
} else {
$track = new stdClass();
$track->userid = $userid;
$track->scormid = $scormid;
$track->scoid = $scoid;
$track->attempt = $attempt;
$track->element = 'objectivesatisfiedstatus';
$track->value = $objectivesatisfiedstatus;
$track->timemodified = time();
$id = $DB->insert_record('scorm_scoes_track', $track);
ob_start();
$filepath = $CFG->dataroot."\\temp\\tempfile.txt";
$fh = fopen($filepath, "a+");
var_dump($track);
$string = ob_get_clean();
fwrite($fh, $string);
fclose($fh);
}
}
}
}
}
$track = null;
if ($trackdata !== null) {
if (isset($trackdata[$element])) {
$track = $trackdata[$element];
}
} else {
$track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
'scormid' => $scormid,
'scoid' => $scoid,
'attempt' => $attempt,
'element' => $element));
}
if ($track) {
if ($element != 'x.start.time' ) { // Don't update x.start.time - keep the original value.
if ($track->value != $value) {
$track->value = $value;
$track->timemodified = time();
$DB->update_record('scorm_scoes_track', $track);
}
$id = $track->id;
}
} else {
$track = new stdClass();
$track->userid = $userid;
$track->scormid = $scormid;
$track->scoid = $scoid;
$track->attempt = $attempt;
$track->element = $element;
$track->value = $value;
$track->timemodified = time();
$id = $DB->insert_record('scorm_scoes_track', $track);
}
if (strstr($element, '.score.raw') ||
(in_array($element, array('cmi.completion_status', 'cmi.core.lesson_status', 'cmi.success_status'))
&& in_array($track->value, array('completed', 'passed')))) {
$scorm = $DB->get_record('scorm', array('id' => $scormid));
include_once($CFG->dirroot.'/mod/scorm/lib.php');
scorm_update_grades($scorm, $userid);
}
return $id;
}
/**
* simple quick function to return true/false if this user has tracks in this scorm
*
* @param integer $scormid The scorm ID
* @param integer $userid the users id
* @return boolean (false if there are no tracks)
*/
function scorm_has_tracks($scormid, $userid) {
global $DB;
return $DB->record_exists('scorm_scoes_track', array('userid' => $userid, 'scormid' => $scormid));
}
function scorm_get_tracks($scoid, $userid, $attempt='') {
// Gets all tracks of specified sco and user.
global $DB;
if (empty($attempt)) {
if ($scormid = $DB->get_field('scorm_scoes', 'scorm', array('id' => $scoid))) {
$attempt = scorm_get_last_attempt($scormid, $userid);
} else {
$attempt = 1;
}
}
if ($tracks = $DB->get_records('scorm_scoes_track', array('userid' => $userid, 'scoid' => $scoid,
'attempt' => $attempt), 'element ASC')) {
$usertrack = scorm_format_interactions($tracks);
$usertrack->userid = $userid;
$usertrack->scoid = $scoid;
return $usertrack;
} else {
return false;
}
}
/**
* helper function to return a formatted list of interactions for reports.
*
* @param array $trackdata the records from scorm_scoes_track table
* @return object formatted list of interactions
*/
function scorm_format_interactions($trackdata) {
$usertrack = new stdClass();
// Defined in order to unify scorm1.2 and scorm2004.
$usertrack->score_raw = '';
$usertrack->status = '';
$usertrack->total_time = '00:00:00';
$usertrack->session_time = '00:00:00';
$usertrack->timemodified = 0;
foreach ($trackdata as $track) {
$element = $track->element;
$usertrack->{$element} = $track->value;
switch ($element) {
case 'cmi.core.lesson_status':
case 'cmi.completion_status':
if ($track->value == 'not attempted') {
$track->value = 'notattempted';
}
$usertrack->status = $track->value;
break;
case 'cmi.core.score.raw':
case 'cmi.score.raw':
$usertrack->score_raw = (float) sprintf('%2.2f', $track->value);
break;
case 'cmi.core.session_time':
case 'cmi.session_time':
$usertrack->session_time = $track->value;
break;
case 'cmi.core.total_time':
case 'cmi.total_time':
$usertrack->total_time = $track->value;
break;
}
if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
$usertrack->timemodified = $track->timemodified;
}
}
return $usertrack;
}
/* Find the start and finsh time for a a given SCO attempt
*
* @param int $scormid SCORM Id
* @param int $scoid SCO Id
* @param int $userid User Id
* @param int $attemt Attempt Id
*
* @return object start and finsh time EPOC secods
*
*/
function scorm_get_sco_runtime($scormid, $scoid, $userid, $attempt=1) {
global $DB;
$timedata = new stdClass();
$params = array('userid' => $userid, 'scormid' => $scormid, 'attempt' => $attempt);
if (!empty($scoid)) {
$params['scoid'] = $scoid;
}
$tracks = $DB->get_records('scorm_scoes_track', $params, "timemodified ASC");
if ($tracks) {
$tracks = array_values($tracks);
}
if ($tracks) {
$timedata->start = $tracks[0]->timemodified;
} else {
$timedata->start = false;
}
if ($tracks && $track = array_pop($tracks)) {
$timedata->finish = $track->timemodified;
} else {
$timedata->finish = $timedata->start;
}
return $timedata;
}
function scorm_grade_user_attempt($scorm, $userid, $attempt=1) {
global $DB;
$attemptscore = new stdClass();
$attemptscore->scoes = 0;
$attemptscore->values = 0;
$attemptscore->max = 0;
$attemptscore->sum = 0;
$attemptscore->lastmodify = 0;
if (!$scoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id), 'sortorder, id')) {
return null;
}
foreach ($scoes as $sco) {
if ($userdata = scorm_get_tracks($sco->id, $userid, $attempt)) {
if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
$attemptscore->scoes++;
}
if (!empty($userdata->score_raw) || (isset($scorm->type) && $scorm->type == 'sco' && isset($userdata->score_raw))) {
$attemptscore->values++;
$attemptscore->sum += $userdata->score_raw;
$attemptscore->max = ($userdata->score_raw > $attemptscore->max) ? $userdata->score_raw : $attemptscore->max;
if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
$attemptscore->lastmodify = $userdata->timemodified;
} else {
$attemptscore->lastmodify = 0;
}
}
}
}
switch ($scorm->grademethod) {
case GRADEHIGHEST:
$score = (float) $attemptscore->max;
break;
case GRADEAVERAGE:
if ($attemptscore->values > 0) {
$score = $attemptscore->sum / $attemptscore->values;
} else {
$score = 0;
}
break;
case GRADESUM:
$score = $attemptscore->sum;
break;
case GRADESCOES:
$score = $attemptscore->scoes;
break;
default:
$score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default.
}
return $score;
}
function scorm_grade_user($scorm, $userid) {
// Ensure we dont grade user beyond $scorm->maxattempt settings.
$lastattempt = scorm_get_last_attempt($scorm->id, $userid);
if ($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt) {
$lastattempt = $scorm->maxattempt;
}
switch ($scorm->whatgrade) {
case FIRSTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, 1);
break;
case LASTATTEMPT:
return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_completed_attempt($scorm->id, $userid));
break;
case HIGHESTATTEMPT:
$maxscore = 0;
for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
$attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
$maxscore = $attemptscore > $maxscore ? $attemptscore : $maxscore;
}
return $maxscore;
break;
case AVERAGEATTEMPT:
$attemptcount = scorm_get_attempt_count($userid, $scorm, true, true);
if (empty($attemptcount)) {
return 0;
} else {
$attemptcount = count($attemptcount);
}
$lastattempt = scorm_get_last_attempt($scorm->id, $userid);
$sumscore = 0;
for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
$attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
$sumscore += $attemptscore;
}
return round($sumscore / $attemptcount);
break;
}
}
function scorm_count_launchable($scormid, $organization='') {
global $DB;
$sqlorganization = '';
$params = array($scormid);
if (!empty($organization)) {
$sqlorganization = " AND organization=?";
$params[] = $organization;
}
return $DB->count_records_select('scorm_scoes', "scorm = ? $sqlorganization AND ".
$DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
$params);
}
/**
* Returns the last attempt used - if no attempts yet, returns 1 for first attempt
*
* @param int $scormid the id of the scorm.
* @param int $userid the id of the user.
*
* @return int The attempt number to use.
*/
function scorm_get_last_attempt($scormid, $userid) {
global $DB;
// Find the last attempt number for the given user id and scorm id.
$sql = "SELECT MAX(attempt)
FROM {scorm_scoes_track}
WHERE userid = ? AND scormid = ?";
$lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
if (empty($lastattempt)) {
return '1';
} else {
return $lastattempt;
}
}
/**
* Returns the last completed attempt used - if no completed attempts yet, returns 1 for first attempt
*
* @param int $scormid the id of the scorm.
* @param int $userid the id of the user.
*
* @return int The attempt number to use.
*/
function scorm_get_last_completed_attempt($scormid, $userid) {
global $DB;
// Find the last completed attempt number for the given user id and scorm id.
$sql = "SELECT MAX(attempt)
FROM {scorm_scoes_track}
WHERE userid = ? AND scormid = ?
AND (value='completed' OR value='passed')";
$lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
if (empty($lastattempt)) {
return '1';
} else {
return $lastattempt;
}
}
/**
* Returns the full list of attempts a user has made.
*
* @param int $scormid the id of the scorm.
* @param int $userid the id of the user.
*
* @return array array of attemptids
*/
function scorm_get_all_attempts($scormid, $userid) {
global $DB;
$attemptids = array();
$sql = "SELECT DISTINCT attempt FROM {scorm_scoes_track} WHERE userid = ? AND scormid = ? ORDER BY attempt";
$attempts = $DB->get_records_sql($sql, array($userid, $scormid));
foreach ($attempts as $attempt) {
$attemptids[] = $attempt->attempt;
}
return $attemptids;
}
function scorm_view_display ($user, $scorm, $action, $cm) {
global $CFG, $DB, $PAGE, $OUTPUT, $COURSE;
if ($scorm->scormtype != SCORM_TYPE_LOCAL && $scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
scorm_parse($scorm, false);
}
$organization = optional_param('organization', '', PARAM_INT);
if ($scorm->displaycoursestructure == 1) {
echo $OUTPUT->box_start('generalbox boxaligncenter toc', 'toc');
echo html_writer::div(get_string('contents', 'scorm'), 'structurehead');
}
if (empty($organization)) {
$organization = $scorm->launch;
}
if ($orgs = $DB->get_records_select_menu('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,title')) {
if (count($orgs) > 1) {
$select = new single_select(new moodle_url($action), 'organization', $orgs, $organization, null);
$select->label = get_string('organizations', 'scorm');
$select->class = 'scorm-center';
echo $OUTPUT->render($select);
}
}
$orgidentifier = '';
if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
if (($sco->organization == '') && ($sco->launch == '')) {
$orgidentifier = $sco->identifier;
} else {
$orgidentifier = $sco->organization;
}
}
$scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
$scorm->version = 'scorm_12';
}
require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
$result = scorm_get_toc($user, $scorm, $cm->id, TOCFULLURL, $orgidentifier);
$incomplete = $result->incomplete;
// Do we want the TOC to be displayed?
if ($scorm->displaycoursestructure == 1) {
echo $result->toc;
echo $OUTPUT->box_end();
}
// Is this the first attempt ?
$attemptcount = scorm_get_attempt_count($user->id, $scorm);
// Do not give the player launch FORM if the SCORM object is locked after the final attempt.
if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) {
echo html_writer::start_div('scorm-center');
echo html_writer::start_tag('form', array('id' => 'scormviewform',
'method' => 'post',
'action' => $CFG->wwwroot.'/mod/scorm/player.php'));
if ($scorm->hidebrowse == 0) {
print_string('mode', 'scorm');
echo ': '.html_writer::empty_tag('input', array('type' => 'radio', 'id' => 'b', 'name' => 'mode', 'value' => 'browse')).
html_writer::label(get_string('browse', 'scorm'), 'b');
echo html_writer::empty_tag('input', array('type' => 'radio',
'id' => 'n', 'name' => 'mode',
'value' => 'normal', 'checked' => 'checked')).
html_writer::label(get_string('normal', 'scorm'), 'n');
} else {
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'mode', 'value' => 'normal'));
}
if ($scorm->forcenewattempt == 1) {
if ($incomplete === false) {
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'newattempt', 'value' => 'on'));
}
} else if (!empty($attemptcount) && ($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
echo html_writer::empty_tag('br');
echo html_writer::checkbox('newattempt', 'on', false, '', array('id' => 'a'));
echo html_writer::label(get_string('newattempt', 'scorm'), 'a');
}
if (!empty($scorm->popup)) {
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'display', 'value' => 'popup'));
}
echo html_writer::empty_tag('br');
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scoid', 'value' => $scorm->launch));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'cm', 'value' => $cm->id));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'currentorg', 'value' => $orgidentifier));
echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('enter', 'scorm')));
echo html_writer::end_tag('form');
echo html_writer::end_div();
}
}
function scorm_simple_play($scorm, $user, $context, $cmid) {
global $DB;
$result = false;
if (has_capability('mod/scorm:viewreport', $context)) {
// If this user can view reports, don't skipview so they can see links to reports.
return $result;
}
if ($scorm->scormtype != SCORM_TYPE_LOCAL && $scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
scorm_parse($scorm, false);
}
$scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id), 'sortorder, id', 'id');
if ($scoes) {
$orgidentifier = '';
if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
if (($sco->organization == '') && ($sco->launch == '')) {
$orgidentifier = $sco->identifier;
} else {
$orgidentifier = $sco->organization;
}
}
if ($scorm->skipview >= SCORM_SKIPVIEW_FIRST) {
$sco = current($scoes);
$url = new moodle_url('/mod/scorm/player.php', array('a' => $scorm->id,
'currentorg' => $orgidentifier,
'scoid' => $sco->id));
if ($scorm->skipview == SCORM_SKIPVIEW_ALWAYS || !scorm_has_tracks($scorm->id, $user->id)) {
if (!empty($scorm->forcenewattempt)) {
$result = scorm_get_toc($user, $scorm, $cmid, TOCFULLURL, $orgidentifier);
if ($result->incomplete === false) {
$url->param('newattempt', 'on');
}
}
redirect($url);
}
}
}
return $result;
}
function scorm_get_count_users($scormid, $groupingid=null) {
global $CFG, $DB;
if (!empty($groupingid)) {
$sql = "SELECT COUNT(DISTINCT st.userid)
FROM {scorm_scoes_track} st
INNER JOIN {groups_members} gm ON st.userid = gm.userid
INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
WHERE st.scormid = ? AND gg.groupingid = ?
";
$params = array($scormid, $groupingid);
} else {