2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Library of internal classes and functions for module SCORM
21 * @copyright 1999 onwards Roberto Pinna
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("$CFG->dirroot/mod/scorm/lib.php");
26 require_once("$CFG->libdir/filelib.php");
28 // Constants and settings for module scorm.
29 define('SCORM_UPDATE_NEVER', '0');
30 define('SCORM_UPDATE_EVERYDAY', '2');
31 define('SCORM_UPDATE_EVERYTIME', '3');
33 define('SCORM_SKIPVIEW_NEVER', '0');
34 define('SCORM_SKIPVIEW_FIRST', '1');
35 define('SCORM_SKIPVIEW_ALWAYS', '2');
38 define('SCO_DATA', 1);
39 define('SCO_ONLY', 2);
41 define('GRADESCOES', '0');
42 define('GRADEHIGHEST', '1');
43 define('GRADEAVERAGE', '2');
44 define('GRADESUM', '3');
46 define('HIGHESTATTEMPT', '0');
47 define('AVERAGEATTEMPT', '1');
48 define('FIRSTATTEMPT', '2');
49 define('LASTATTEMPT', '3');
51 define('TOCJSLINK', 1);
52 define('TOCFULLURL', 2);
54 // Local Library of functions for module scorm.
58 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
59 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61 class scorm_package_file_info extends file_info_stored {
62 public function get_parent() {
63 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
64 return $this->browser->get_file_info($this->context);
66 return parent::get_parent();
68 public function get_visible_name() {
69 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
70 return $this->topvisiblename;
72 return parent::get_visible_name();
77 * Returns an array of the popup options for SCORM and each options default value
79 * @return array an array of popup options as the key and their defaults as the value
81 function scorm_get_popup_options_array() {
82 $cfgscorm = get_config('scorm');
84 return array('scrollbars' => isset($cfgscorm->scrollbars) ? $cfgscorm->scrollbars : 0,
85 'directories' => isset($cfgscorm->directories) ? $cfgscorm->directories : 0,
86 'location' => isset($cfgscorm->location) ? $cfgscorm->location : 0,
87 'menubar' => isset($cfgscorm->menubar) ? $cfgscorm->menubar : 0,
88 'toolbar' => isset($cfgscorm->toolbar) ? $cfgscorm->toolbar : 0,
89 'status' => isset($cfgscorm->status) ? $cfgscorm->status : 0);
93 * Returns an array of the array of what grade options
95 * @return array an array of what grade options
97 function scorm_get_grade_method_array() {
98 return array (GRADESCOES => get_string('gradescoes', 'scorm'),
99 GRADEHIGHEST => get_string('gradehighest', 'scorm'),
100 GRADEAVERAGE => get_string('gradeaverage', 'scorm'),
101 GRADESUM => get_string('gradesum', 'scorm'));
105 * Returns an array of the array of what grade options
107 * @return array an array of what grade options
109 function scorm_get_what_grade_array() {
110 return array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'),
111 AVERAGEATTEMPT => get_string('averageattempt', 'scorm'),
112 FIRSTATTEMPT => get_string('firstattempt', 'scorm'),
113 LASTATTEMPT => get_string('lastattempt', 'scorm'));
117 * Returns an array of the array of skip view options
119 * @return array an array of skip view options
121 function scorm_get_skip_view_array() {
122 return array(SCORM_SKIPVIEW_NEVER => get_string('never'),
123 SCORM_SKIPVIEW_FIRST => get_string('firstaccess', 'scorm'),
124 SCORM_SKIPVIEW_ALWAYS => get_string('always'));
128 * Returns an array of the array of hide table of contents options
130 * @return array an array of hide table of contents options
132 function scorm_get_hidetoc_array() {
133 return array(SCORM_TOC_SIDE => get_string('sided', 'scorm'),
134 SCORM_TOC_HIDDEN => get_string('hidden', 'scorm'),
135 SCORM_TOC_POPUP => get_string('popupmenu', 'scorm'),
136 SCORM_TOC_DISABLED => get_string('disabled', 'scorm'));
140 * Returns an array of the array of update frequency options
142 * @return array an array of update frequency options
144 function scorm_get_updatefreq_array() {
145 return array(SCORM_UPDATE_NEVER => get_string('never'),
146 SCORM_UPDATE_EVERYDAY => get_string('everyday', 'scorm'),
147 SCORM_UPDATE_EVERYTIME => get_string('everytime', 'scorm'));
151 * Returns an array of the array of popup display options
153 * @return array an array of popup display options
155 function scorm_get_popup_display_array() {
156 return array(0 => get_string('currentwindow', 'scorm'),
157 1 => get_string('popup', 'scorm'));
161 * Returns an array of the array of navigation buttons display options
163 * @return array an array of navigation buttons display options
165 function scorm_get_navigation_display_array() {
166 return array(SCORM_NAV_DISABLED => get_string('no'),
167 SCORM_NAV_UNDER_CONTENT => get_string('undercontent', 'scorm'),
168 SCORM_NAV_FLOATING => get_string('floating', 'scorm'));
172 * Returns an array of the array of attempt options
174 * @return array an array of attempt options
176 function scorm_get_attempts_array() {
177 $attempts = array(0 => get_string('nolimit', 'scorm'),
178 1 => get_string('attempt1', 'scorm'));
180 for ($i = 2; $i <= 6; $i++) {
181 $attempts[$i] = get_string('attemptsx', 'scorm', $i);
188 * Returns an array of the attempt status options
190 * @return array an array of attempt status options
192 function scorm_get_attemptstatus_array() {
193 return array(SCORM_DISPLAY_ATTEMPTSTATUS_NO => get_string('no'),
194 SCORM_DISPLAY_ATTEMPTSTATUS_ALL => get_string('attemptstatusall', 'scorm'),
195 SCORM_DISPLAY_ATTEMPTSTATUS_MY => get_string('attemptstatusmy', 'scorm'),
196 SCORM_DISPLAY_ATTEMPTSTATUS_ENTRY => get_string('attemptstatusentry', 'scorm'));
200 * Extracts scrom package, sets up all variables.
201 * Called whenever scorm changes
202 * @param object $scorm instance - fields are updated and changes saved into database
203 * @param bool $full force full update if true
206 function scorm_parse($scorm, $full) {
208 $cfgscorm = get_config('scorm');
210 if (!isset($scorm->cmid)) {
211 $cm = get_coursemodule_from_instance('scorm', $scorm->id);
212 $scorm->cmid = $cm->id;
214 $context = context_module::instance($scorm->cmid);
215 $newhash = $scorm->sha1hash;
217 if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
219 $fs = get_file_storage();
220 $packagefile = false;
221 $packagefileimsmanifest = false;
223 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
224 if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) {
225 if ($packagefile->is_external_file()) { // Get zip file so we can check it is correct.
226 $packagefile->import_external_file_contents();
228 $newhash = $packagefile->get_contenthash();
229 if (strtolower($packagefile->get_filename()) == 'imsmanifest.xml') {
230 $packagefileimsmanifest = true;
236 if (!$cfgscorm->allowtypelocalsync) {
237 // Sorry - localsync disabled.
240 if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
241 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
242 $filerecord = array('contextid' => $context->id, 'component' => 'mod_scorm', 'filearea' => 'package',
243 'itemid' => 0, 'filepath' => '/');
244 if ($packagefile = $fs->create_file_from_url($filerecord, $scorm->reference, array('calctimeout' => true))) {
245 $newhash = sha1($scorm->reference);
253 if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
254 if (strpos($scorm->version, 'SCORM') !== false) {
255 if ($packagefileimsmanifest || $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
256 // No need to update.
259 } else if (strpos($scorm->version, 'AICC') !== false) {
260 // TODO: add more sanity checks - something really exists in scorm_content area.
264 if (!$packagefileimsmanifest) {
265 // Now extract files.
266 $fs->delete_area_files($context->id, 'mod_scorm', 'content');
268 $packer = get_file_packer('application/zip');
269 $packagefile->extract_to_storage($packer, $context->id, 'mod_scorm', 'content', 0, '/');
275 if ($packagefileimsmanifest) {
276 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
277 // Direct link to imsmanifest.xml file.
278 if (!scorm_parse_scorm($scorm, $packagefile)) {
279 $scorm->version = 'ERROR';
282 } else if ($manifest = $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
283 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
285 if (!scorm_parse_scorm($scorm, $manifest)) {
286 $scorm->version = 'ERROR';
289 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
291 $result = scorm_parse_aicc($scorm);
293 $scorm->version = 'ERROR';
295 $scorm->version = 'AICC';
299 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $cfgscorm->allowtypeexternal) {
300 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
301 // SCORM only, AICC can not be external.
302 if (!scorm_parse_scorm($scorm, $scorm->reference)) {
303 $scorm->version = 'ERROR';
305 $newhash = sha1($scorm->reference);
307 } else if ($scorm->scormtype === SCORM_TYPE_AICCURL and $cfgscorm->allowtypeexternalaicc) {
308 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
310 $result = scorm_parse_aicc($scorm);
312 $scorm->version = 'ERROR';
314 $scorm->version = 'AICC';
318 // Sorry, disabled type.
323 $scorm->sha1hash = $newhash;
324 $DB->update_record('scorm', $scorm);
328 function scorm_array_search($item, $needle, $haystacks, $strict=false) {
329 if (!empty($haystacks)) {
330 foreach ($haystacks as $key => $element) {
332 if ($element->{$item} === $needle) {
336 if ($element->{$item} == $needle) {
345 function scorm_repeater($what, $times) {
350 for ($i = 0; $i < $times; $i++) {
356 function scorm_external_link($link) {
357 // Check if a link is external.
359 $link = strtolower($link);
360 if (substr($link, 0, 7) == 'http://') {
362 } else if (substr($link, 0, 8) == 'https://') {
364 } else if (substr($link, 0, 4) == 'www.') {
371 * Returns an object containing all datas relative to the given sco ID
373 * @param integer $id The sco ID
374 * @return mixed (false if sco id does not exists)
376 function scorm_get_sco($id, $what=SCO_ALL) {
379 if ($sco = $DB->get_record('scorm_scoes', array('id' => $id))) {
380 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
381 if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $id)))) {
382 foreach ($scodatas as $scodata) {
383 $sco->{$scodata->name} = $scodata->value;
385 } else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $id))))) {
386 $sco->parameters = '';
395 * Returns an object (array) containing all the scoes data related to the given sco ID
397 * @param integer $id The sco ID
398 * @param integer $organisation an organisation ID - defaults to false if not required
399 * @return mixed (false if there are no scoes or an array)
401 function scorm_get_scoes($id, $organisation=false) {
404 $queryarray = array('scorm' => $id);
405 if (!empty($organisation)) {
406 $queryarray['organization'] = $organisation;
408 if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'sortorder, id')) {
409 // Drop keys so that it is a simple array as expected.
410 $scoes = array_values($scoes);
411 foreach ($scoes as $sco) {
412 if ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid' => $sco->id))) {
413 foreach ($scodatas as $scodata) {
414 $sco->{$scodata->name} = $scodata->value;
424 function scorm_insert_track($userid, $scormid, $scoid, $attempt, $element, $value, $forcecompleted=false, $trackdata = null) {
429 if ($forcecompleted) {
430 // TODO - this could be broadened to encompass SCORM 2004 in future.
431 if (($element == 'cmi.core.lesson_status') && ($value == 'incomplete')) {
432 if ($track = $DB->get_record_select('scorm_scoes_track',
433 'userid=? AND scormid=? AND scoid=? AND attempt=? '.
434 'AND element=\'cmi.core.score.raw\'',
435 array($userid, $scormid, $scoid, $attempt))) {
436 $value = 'completed';
439 if ($element == 'cmi.core.score.raw') {
440 if ($tracktest = $DB->get_record_select('scorm_scoes_track',
441 'userid=? AND scormid=? AND scoid=? AND attempt=? '.
442 'AND element=\'cmi.core.lesson_status\'',
443 array($userid, $scormid, $scoid, $attempt))) {
444 if ($tracktest->value == "incomplete") {
445 $tracktest->value = "completed";
446 $DB->update_record('scorm_scoes_track', $tracktest);
450 if (($element == 'cmi.success_status') && ($value == 'passed' || $value == 'failed')) {
451 if ($DB->get_record('scorm_scoes_data', array('scoid' => $scoid, 'name' => 'objectivesetbycontent'))) {
452 $objectiveprogressstatus = true;
453 $objectivesatisfiedstatus = false;
454 if ($value == 'passed') {
455 $objectivesatisfiedstatus = true;
458 if ($track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
459 'scormid' => $scormid,
461 'attempt' => $attempt,
462 'element' => 'objectiveprogressstatus'))) {
463 $track->value = $objectiveprogressstatus;
464 $track->timemodified = time();
465 $DB->update_record('scorm_scoes_track', $track);
468 $track = new stdClass();
469 $track->userid = $userid;
470 $track->scormid = $scormid;
471 $track->scoid = $scoid;
472 $track->attempt = $attempt;
473 $track->element = 'objectiveprogressstatus';
474 $track->value = $objectiveprogressstatus;
475 $track->timemodified = time();
476 $id = $DB->insert_record('scorm_scoes_track', $track);
478 if ($objectivesatisfiedstatus) {
479 if ($track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
480 'scormid' => $scormid,
482 'attempt' => $attempt,
483 'element' => 'objectivesatisfiedstatus'))) {
484 $track->value = $objectivesatisfiedstatus;
485 $track->timemodified = time();
486 $DB->update_record('scorm_scoes_track', $track);
489 $track = new stdClass();
490 $track->userid = $userid;
491 $track->scormid = $scormid;
492 $track->scoid = $scoid;
493 $track->attempt = $attempt;
494 $track->element = 'objectivesatisfiedstatus';
495 $track->value = $objectivesatisfiedstatus;
496 $track->timemodified = time();
497 $id = $DB->insert_record('scorm_scoes_track', $track);
499 $filepath = $CFG->dataroot."\\temp\\tempfile.txt";
500 $fh = fopen($filepath, "a+");
502 $string = ob_get_clean();
503 fwrite($fh, $string);
513 if ($trackdata !== null) {
514 if (isset($trackdata[$element])) {
515 $track = $trackdata[$element];
518 $track = $DB->get_record('scorm_scoes_track', array('userid' => $userid,
519 'scormid' => $scormid,
521 'attempt' => $attempt,
522 'element' => $element));
525 if ($element != 'x.start.time' ) { // Don't update x.start.time - keep the original value.
526 if ($track->value != $value) {
527 $track->value = $value;
528 $track->timemodified = time();
529 $DB->update_record('scorm_scoes_track', $track);
534 $track = new stdClass();
535 $track->userid = $userid;
536 $track->scormid = $scormid;
537 $track->scoid = $scoid;
538 $track->attempt = $attempt;
539 $track->element = $element;
540 $track->value = $value;
541 $track->timemodified = time();
542 $id = $DB->insert_record('scorm_scoes_track', $track);
545 if (strstr($element, '.score.raw') ||
546 (in_array($element, array('cmi.completion_status', 'cmi.core.lesson_status', 'cmi.success_status'))
547 && in_array($track->value, array('completed', 'passed')))) {
548 $scorm = $DB->get_record('scorm', array('id' => $scormid));
549 include_once($CFG->dirroot.'/mod/scorm/lib.php');
550 scorm_update_grades($scorm, $userid);
557 * simple quick function to return true/false if this user has tracks in this scorm
559 * @param integer $scormid The scorm ID
560 * @param integer $userid the users id
561 * @return boolean (false if there are no tracks)
563 function scorm_has_tracks($scormid, $userid) {
565 return $DB->record_exists('scorm_scoes_track', array('userid' => $userid, 'scormid' => $scormid));
568 function scorm_get_tracks($scoid, $userid, $attempt='') {
569 // Gets all tracks of specified sco and user.
572 if (empty($attempt)) {
573 if ($scormid = $DB->get_field('scorm_scoes', 'scorm', array('id' => $scoid))) {
574 $attempt = scorm_get_last_attempt($scormid, $userid);
579 if ($tracks = $DB->get_records('scorm_scoes_track', array('userid' => $userid, 'scoid' => $scoid,
580 'attempt' => $attempt), 'element ASC')) {
581 $usertrack = scorm_format_interactions($tracks);
582 $usertrack->userid = $userid;
583 $usertrack->scoid = $scoid;
591 * helper function to return a formatted list of interactions for reports.
593 * @param array $trackdata the records from scorm_scoes_track table
594 * @return object formatted list of interactions
596 function scorm_format_interactions($trackdata) {
597 $usertrack = new stdClass();
599 // Defined in order to unify scorm1.2 and scorm2004.
600 $usertrack->score_raw = '';
601 $usertrack->status = '';
602 $usertrack->total_time = '00:00:00';
603 $usertrack->session_time = '00:00:00';
604 $usertrack->timemodified = 0;
606 foreach ($trackdata as $track) {
607 $element = $track->element;
608 $usertrack->{$element} = $track->value;
610 case 'cmi.core.lesson_status':
611 case 'cmi.completion_status':
612 if ($track->value == 'not attempted') {
613 $track->value = 'notattempted';
615 $usertrack->status = $track->value;
617 case 'cmi.core.score.raw':
618 case 'cmi.score.raw':
619 $usertrack->score_raw = (float) sprintf('%2.2f', $track->value);
621 case 'cmi.core.session_time':
622 case 'cmi.session_time':
623 $usertrack->session_time = $track->value;
625 case 'cmi.core.total_time':
626 case 'cmi.total_time':
627 $usertrack->total_time = $track->value;
630 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
631 $usertrack->timemodified = $track->timemodified;
637 /* Find the start and finsh time for a a given SCO attempt
639 * @param int $scormid SCORM Id
640 * @param int $scoid SCO Id
641 * @param int $userid User Id
642 * @param int $attemt Attempt Id
644 * @return object start and finsh time EPOC secods
647 function scorm_get_sco_runtime($scormid, $scoid, $userid, $attempt=1) {
650 $timedata = new stdClass();
651 $params = array('userid' => $userid, 'scormid' => $scormid, 'attempt' => $attempt);
652 if (!empty($scoid)) {
653 $params['scoid'] = $scoid;
655 $tracks = $DB->get_records('scorm_scoes_track', $params, "timemodified ASC");
657 $tracks = array_values($tracks);
661 $timedata->start = $tracks[0]->timemodified;
663 $timedata->start = false;
665 if ($tracks && $track = array_pop($tracks)) {
666 $timedata->finish = $track->timemodified;
668 $timedata->finish = $timedata->start;
673 function scorm_grade_user_attempt($scorm, $userid, $attempt=1) {
675 $attemptscore = new stdClass();
676 $attemptscore->scoes = 0;
677 $attemptscore->values = 0;
678 $attemptscore->max = 0;
679 $attemptscore->sum = 0;
680 $attemptscore->lastmodify = 0;
682 if (!$scoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id), 'sortorder, id')) {
686 foreach ($scoes as $sco) {
687 if ($userdata = scorm_get_tracks($sco->id, $userid, $attempt)) {
688 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
689 $attemptscore->scoes++;
691 if (!empty($userdata->score_raw) || (isset($scorm->type) && $scorm->type == 'sco' && isset($userdata->score_raw))) {
692 $attemptscore->values++;
693 $attemptscore->sum += $userdata->score_raw;
694 $attemptscore->max = ($userdata->score_raw > $attemptscore->max) ? $userdata->score_raw : $attemptscore->max;
695 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
696 $attemptscore->lastmodify = $userdata->timemodified;
698 $attemptscore->lastmodify = 0;
703 switch ($scorm->grademethod) {
705 $score = (float) $attemptscore->max;
708 if ($attemptscore->values > 0) {
709 $score = $attemptscore->sum / $attemptscore->values;
715 $score = $attemptscore->sum;
718 $score = $attemptscore->scoes;
721 $score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default.
727 function scorm_grade_user($scorm, $userid) {
729 // Ensure we dont grade user beyond $scorm->maxattempt settings.
730 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
731 if ($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt) {
732 $lastattempt = $scorm->maxattempt;
735 switch ($scorm->whatgrade) {
737 return scorm_grade_user_attempt($scorm, $userid, 1);
740 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_completed_attempt($scorm->id, $userid));
744 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
745 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
746 $maxscore = $attemptscore > $maxscore ? $attemptscore : $maxscore;
752 $attemptcount = scorm_get_attempt_count($userid, $scorm, true, true);
753 if (empty($attemptcount)) {
756 $attemptcount = count($attemptcount);
758 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
760 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
761 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
762 $sumscore += $attemptscore;
765 return round($sumscore / $attemptcount);
770 function scorm_count_launchable($scormid, $organization='') {
773 $sqlorganization = '';
774 $params = array($scormid);
775 if (!empty($organization)) {
776 $sqlorganization = " AND organization=?";
777 $params[] = $organization;
779 return $DB->count_records_select('scorm_scoes', "scorm = ? $sqlorganization AND ".
780 $DB->sql_isnotempty('scorm_scoes', 'launch', false, true),
785 * Returns the last attempt used - if no attempts yet, returns 1 for first attempt
787 * @param int $scormid the id of the scorm.
788 * @param int $userid the id of the user.
790 * @return int The attempt number to use.
792 function scorm_get_last_attempt($scormid, $userid) {
795 // Find the last attempt number for the given user id and scorm id.
796 $sql = "SELECT MAX(attempt)
797 FROM {scorm_scoes_track}
798 WHERE userid = ? AND scormid = ?";
799 $lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
800 if (empty($lastattempt)) {
808 * Returns the last completed attempt used - if no completed attempts yet, returns 1 for first attempt
810 * @param int $scormid the id of the scorm.
811 * @param int $userid the id of the user.
813 * @return int The attempt number to use.
815 function scorm_get_last_completed_attempt($scormid, $userid) {
818 // Find the last completed attempt number for the given user id and scorm id.
819 $sql = "SELECT MAX(attempt)
820 FROM {scorm_scoes_track}
821 WHERE userid = ? AND scormid = ?
822 AND (value='completed' OR value='passed')";
823 $lastattempt = $DB->get_field_sql($sql, array($userid, $scormid));
824 if (empty($lastattempt)) {
832 * Returns the full list of attempts a user has made.
834 * @param int $scormid the id of the scorm.
835 * @param int $userid the id of the user.
837 * @return array array of attemptids
839 function scorm_get_all_attempts($scormid, $userid) {
841 $attemptids = array();
842 $sql = "SELECT DISTINCT attempt FROM {scorm_scoes_track} WHERE userid = ? AND scormid = ? ORDER BY attempt";
843 $attempts = $DB->get_records_sql($sql, array($userid, $scormid));
844 foreach ($attempts as $attempt) {
845 $attemptids[] = $attempt->attempt;
850 function scorm_view_display ($user, $scorm, $action, $cm) {
851 global $CFG, $DB, $OUTPUT;
853 if ($scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
854 scorm_parse($scorm, false);
857 $organization = optional_param('organization', '', PARAM_INT);
859 if ($scorm->displaycoursestructure == 1) {
860 echo $OUTPUT->box_start('generalbox boxaligncenter toc', 'toc');
861 echo html_writer::div(get_string('contents', 'scorm'), 'structurehead');
863 if (empty($organization)) {
864 $organization = $scorm->launch;
866 if ($orgs = $DB->get_records_select_menu('scorm_scoes', 'scorm = ? AND '.
867 $DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
868 $DB->sql_isempty('scorm_scoes', 'organization', false, false),
869 array($scorm->id), 'sortorder, id', 'id,title')) {
870 if (count($orgs) > 1) {
871 $select = new single_select(new moodle_url($action), 'organization', $orgs, $organization, null);
872 $select->label = get_string('organizations', 'scorm');
873 $select->class = 'scorm-center';
874 echo $OUTPUT->render($select);
878 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
879 if (($sco->organization == '') && ($sco->launch == '')) {
880 $orgidentifier = $sco->identifier;
882 $orgidentifier = $sco->organization;
886 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
887 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
888 $scorm->version = 'scorm_12';
890 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
892 $result = scorm_get_toc($user, $scorm, $cm->id, TOCFULLURL, $orgidentifier);
893 $incomplete = $result->incomplete;
895 // Do we want the TOC to be displayed?
896 if ($scorm->displaycoursestructure == 1) {
898 echo $OUTPUT->box_end();
901 // Is this the first attempt ?
902 $attemptcount = scorm_get_attempt_count($user->id, $scorm);
904 // Do not give the player launch FORM if the SCORM object is locked after the final attempt.
905 if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) {
906 echo html_writer::start_div('scorm-center');
907 echo html_writer::start_tag('form', array('id' => 'scormviewform',
909 'action' => $CFG->wwwroot.'/mod/scorm/player.php'));
910 if ($scorm->hidebrowse == 0) {
911 print_string('mode', 'scorm');
912 echo ': '.html_writer::empty_tag('input', array('type' => 'radio', 'id' => 'b', 'name' => 'mode', 'value' => 'browse')).
913 html_writer::label(get_string('browse', 'scorm'), 'b');
914 echo html_writer::empty_tag('input', array('type' => 'radio',
915 'id' => 'n', 'name' => 'mode',
916 'value' => 'normal', 'checked' => 'checked')).
917 html_writer::label(get_string('normal', 'scorm'), 'n');
920 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'mode', 'value' => 'normal'));
922 if ($scorm->forcenewattempt == 1) {
923 if ($incomplete === false) {
924 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'newattempt', 'value' => 'on'));
926 } else if (!empty($attemptcount) && ($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
927 echo html_writer::empty_tag('br');
928 echo html_writer::checkbox('newattempt', 'on', false, '', array('id' => 'a'));
929 echo html_writer::label(get_string('newattempt', 'scorm'), 'a');
931 if (!empty($scorm->popup)) {
932 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'display', 'value' => 'popup'));
935 echo html_writer::empty_tag('br');
936 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scoid', 'value' => $scorm->launch));
937 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'cm', 'value' => $cm->id));
938 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'currentorg', 'value' => $orgidentifier));
939 echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('enter', 'scorm')));
940 echo html_writer::end_tag('form');
941 echo html_writer::end_div();
945 function scorm_simple_play($scorm, $user, $context, $cmid) {
950 if (has_capability('mod/scorm:viewreport', $context)) {
951 // If this user can view reports, don't skipview so they can see links to reports.
955 if ($scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
956 scorm_parse($scorm, false);
958 $scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
959 $DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id), 'sortorder, id', 'id');
963 if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
964 if (($sco->organization == '') && ($sco->launch == '')) {
965 $orgidentifier = $sco->identifier;
967 $orgidentifier = $sco->organization;
970 if ($scorm->skipview >= SCORM_SKIPVIEW_FIRST) {
971 $sco = current($scoes);
972 $url = new moodle_url('/mod/scorm/player.php', array('a' => $scorm->id,
973 'currentorg' => $orgidentifier,
974 'scoid' => $sco->id));
975 if ($scorm->skipview == SCORM_SKIPVIEW_ALWAYS || !scorm_has_tracks($scorm->id, $user->id)) {
976 if (!empty($scorm->forcenewattempt)) {
977 $result = scorm_get_toc($user, $scorm, $cmid, TOCFULLURL, $orgidentifier);
978 if ($result->incomplete === false) {
979 $url->param('newattempt', 'on');
989 function scorm_get_count_users($scormid, $groupingid=null) {
992 if (!empty($groupingid)) {
993 $sql = "SELECT COUNT(DISTINCT st.userid)
994 FROM {scorm_scoes_track} st
995 INNER JOIN {groups_members} gm ON st.userid = gm.userid
996 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
997 WHERE st.scormid = ? AND gg.groupingid = ?
999 $params = array($scormid, $groupingid);
1001 $sql = "SELECT COUNT(DISTINCT st.userid)
1002 FROM {scorm_scoes_track} st
1003 WHERE st.scormid = ?
1005 $params = array($scormid);
1008 return ($DB->count_records_sql($sql, $params));
1012 * Build up the JavaScript representation of an array element
1014 * @param string $sversion SCORM API version
1015 * @param array $userdata User track data
1016 * @param string $elementname Name of array element to get values for
1017 * @param array $children list of sub elements of this array element that also need instantiating
1018 * @return Javascript array elements
1020 function scorm_reconstitute_array_element($sversion, $userdata, $elementname, $children) {
1021 // Reconstitute comments_from_learner and comments_from_lms.
1023 $currentsubelement = '';
1027 $scormseperator = '_';
1029 if (scorm_version_check($sversion, SCORM_13)) { // Scorm 1.3 elements use a . instead of an _ .
1030 $scormseperator = '.';
1032 // Filter out the ones we want.
1033 $elementlist = array();
1034 foreach ($userdata as $element => $value) {
1035 if (substr($element, 0, strlen($elementname)) == $elementname) {
1036 $elementlist[$element] = $value;
1040 // Sort elements in .n array order.
1041 uksort($elementlist, "scorm_element_cmp");
1043 // Generate JavaScript.
1044 foreach ($elementlist as $element => $value) {
1045 if (scorm_version_check($sversion, SCORM_13)) {
1046 $element = preg_replace('/\.(\d+)\./', ".N\$1.", $element);
1047 preg_match('/\.(N\d+)\./', $element, $matches);
1049 $element = preg_replace('/\.(\d+)\./', "_\$1.", $element);
1050 preg_match('/\_(\d+)\./', $element, $matches);
1052 if (count($matches) > 0 && $current != $matches[1]) {
1053 if ($countsub > 0) {
1054 $return .= ' '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
1056 $current = $matches[1];
1058 $currentsubelement = '';
1061 $end = strpos($element, $matches[1]) + strlen($matches[1]);
1062 $subelement = substr($element, 0, $end);
1063 $return .= ' '.$subelement." = new Object();\n";
1064 // Now add the children.
1065 foreach ($children as $child) {
1066 $return .= ' '.$subelement.".".$child." = new Object();\n";
1067 $return .= ' '.$subelement.".".$child."._children = ".$child."_children;\n";
1071 // Now - flesh out the second level elements if there are any.
1072 if (scorm_version_check($sversion, SCORM_13)) {
1073 $element = preg_replace('/(.*?\.N\d+\..*?)\.(\d+)\./', "\$1.N\$2.", $element);
1074 preg_match('/.*?\.N\d+\.(.*?)\.(N\d+)\./', $element, $matches);
1076 $element = preg_replace('/(.*?\_\d+\..*?)\.(\d+)\./', "\$1_\$2.", $element);
1077 preg_match('/.*?\_\d+\.(.*?)\_(\d+)\./', $element, $matches);
1080 // Check the sub element type.
1081 if (count($matches) > 0 && $currentsubelement != $matches[1]) {
1082 if ($countsub > 0) {
1083 $return .= ' '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
1085 $currentsubelement = $matches[1];
1088 $end = strpos($element, $matches[1]) + strlen($matches[1]);
1089 $subelement = substr($element, 0, $end);
1090 $return .= ' '.$subelement." = new Object();\n";
1093 // Now check the subelement subscript.
1094 if (count($matches) > 0 && $currentsub != $matches[2]) {
1095 $currentsub = $matches[2];
1097 $end = strrpos($element, $matches[2]) + strlen($matches[2]);
1098 $subelement = substr($element, 0, $end);
1099 $return .= ' '.$subelement." = new Object();\n";
1102 $return .= ' '.$element.' = \''.$value."';\n";
1104 if ($countsub > 0) {
1105 $return .= ' '.$elementname.$scormseperator.$current.'.'.$currentsubelement.'._count = '.$countsub.";\n";
1108 $return .= ' '.$elementname.'._count = '.$count.";\n";
1114 * Build up the JavaScript representation of an array element
1116 * @param string $a left array element
1117 * @param string $b right array element
1118 * @return comparator - 0,1,-1
1120 function scorm_element_cmp($a, $b) {
1121 preg_match('/.*?(\d+)\./', $a, $matches);
1122 $left = intval($matches[1]);
1123 preg_match('/.?(\d+)\./', $b, $matches);
1124 $right = intval($matches[1]);
1125 if ($left < $right) {
1126 return -1; // Smaller.
1127 } else if ($left > $right) {
1128 return 1; // Bigger.
1130 // Look for a second level qualifier eg cmi.interactions_0.correct_responses_0.pattern.
1131 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $a, $matches)) {
1132 $leftterm = intval($matches[2]);
1133 $left = intval($matches[3]);
1134 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $b, $matches)) {
1135 $rightterm = intval($matches[2]);
1136 $right = intval($matches[3]);
1137 if ($leftterm < $rightterm) {
1138 return -1; // Smaller.
1139 } else if ($leftterm > $rightterm) {
1140 return 1; // Bigger.
1142 if ($left < $right) {
1143 return -1; // Smaller.
1144 } else if ($left > $right) {
1145 return 1; // Bigger.
1150 // Fall back for no second level matches or second level matches are equal.
1151 return 0; // Equal to.
1156 * Generate the user attempt status string
1158 * @param object $user Current context user
1159 * @param object $scorm a moodle scrom object - mdl_scorm
1160 * @return string - Attempt status string
1162 function scorm_get_attempt_status($user, $scorm, $cm='') {
1163 global $DB, $PAGE, $OUTPUT;
1165 $attempts = scorm_get_attempt_count($user->id, $scorm, true);
1166 if (empty($attempts)) {
1169 $attemptcount = count($attempts);
1172 $result = html_writer::start_tag('p').get_string('noattemptsallowed', 'scorm').': ';
1173 if ($scorm->maxattempt > 0) {
1174 $result .= $scorm->maxattempt . html_writer::empty_tag('br');
1176 $result .= get_string('unlimited').html_writer::empty_tag('br');
1178 $result .= get_string('noattemptsmade', 'scorm').': ' . $attemptcount . html_writer::empty_tag('br');
1180 if ($scorm->maxattempt == 1) {
1181 switch ($scorm->grademethod) {
1183 $grademethod = get_string('gradehighest', 'scorm');
1186 $grademethod = get_string('gradeaverage', 'scorm');
1189 $grademethod = get_string('gradesum', 'scorm');
1192 $grademethod = get_string('gradescoes', 'scorm');
1196 switch ($scorm->whatgrade) {
1197 case HIGHESTATTEMPT:
1198 $grademethod = get_string('highestattempt', 'scorm');
1200 case AVERAGEATTEMPT:
1201 $grademethod = get_string('averageattempt', 'scorm');
1204 $grademethod = get_string('firstattempt', 'scorm');
1207 $grademethod = get_string('lastattempt', 'scorm');
1212 if (!empty($attempts)) {
1214 foreach ($attempts as $attempt) {
1215 $gradereported = scorm_grade_user_attempt($scorm, $user->id, $attempt->attemptnumber);
1216 if ($scorm->grademethod !== GRADESCOES && !empty($scorm->maxgrade)) {
1217 $gradereported = $gradereported / $scorm->maxgrade;
1218 $gradereported = number_format($gradereported * 100, 0) .'%';
1220 $result .= get_string('gradeforattempt', 'scorm').' ' . $i . ': ' . $gradereported .html_writer::empty_tag('br');
1224 $calculatedgrade = scorm_grade_user($scorm, $user->id);
1225 if ($scorm->grademethod !== GRADESCOES && !empty($scorm->maxgrade)) {
1226 $calculatedgrade = $calculatedgrade / $scorm->maxgrade;
1227 $calculatedgrade = number_format($calculatedgrade * 100, 0) .'%';
1229 $result .= get_string('grademethod', 'scorm'). ': ' . $grademethod;
1230 if (empty($attempts)) {
1231 $result .= html_writer::empty_tag('br').get_string('gradereported', 'scorm').
1232 ': '.get_string('none').html_writer::empty_tag('br');
1234 $result .= html_writer::empty_tag('br').get_string('gradereported', 'scorm').
1235 ': '.$calculatedgrade.html_writer::empty_tag('br');
1237 $result .= html_writer::end_tag('p');
1238 if ($attemptcount >= $scorm->maxattempt and $scorm->maxattempt > 0) {
1239 $result .= html_writer::tag('p', get_string('exceededmaxattempts', 'scorm'), array('class' => 'exceededmaxattempts'));
1242 $context = context_module::instance($cm->id);
1243 if (has_capability('mod/scorm:deleteownresponses', $context) &&
1244 $DB->record_exists('scorm_scoes_track', array('userid' => $user->id, 'scormid' => $scorm->id))) {
1245 // Check to see if any data is stored for this user.
1246 $deleteurl = new moodle_url($PAGE->url, array('action' => 'delete', 'sesskey' => sesskey()));
1247 $result .= $OUTPUT->single_button($deleteurl, get_string('deleteallattempts', 'scorm'));
1255 * Get SCORM attempt count
1257 * @param object $user Current context user
1258 * @param object $scorm a moodle scrom object - mdl_scorm
1259 * @param bool $returnobjects if true returns a object with attempts, if false returns count of attempts.
1260 * @param bool $ignoremissingcompletion - ignores attempts that haven't reported a grade/completion.
1261 * @return int - no. of attempts so far
1263 function scorm_get_attempt_count($userid, $scorm, $returnobjects = false, $ignoremissingcompletion = false) {
1266 // Historically attempts that don't report these elements haven't been included in the average attempts grading method
1267 // we may want to change this in future, but to avoid unexpected grade decreases we're leaving this in. MDL-43222 .
1268 if (scorm_version_check($scorm->version, SCORM_13)) {
1269 $element = 'cmi.score.raw';
1270 } else if ($scorm->grademethod == GRADESCOES) {
1271 $element = 'cmi.core.lesson_status';
1273 $element = 'cmi.core.score.raw';
1276 if ($returnobjects) {
1277 $params = array('userid' => $userid, 'scormid' => $scorm->id);
1278 if ($ignoremissingcompletion) { // Exclude attempts that don't have the completion element requested.
1279 $params['element'] = $element;
1281 $attempts = $DB->get_records('scorm_scoes_track', $params, 'attempt', 'DISTINCT attempt AS attemptnumber');
1284 $params = array($userid, $scorm->id);
1285 $sql = "SELECT COUNT(DISTINCT attempt)
1286 FROM {scorm_scoes_track}
1287 WHERE userid = ? AND scormid = ?";
1288 if ($ignoremissingcompletion) { // Exclude attempts that don't have the completion element requested.
1289 $sql .= ' AND element = ?';
1290 $params[] = $element;
1293 $attemptscount = $DB->count_records_sql($sql, $params);
1294 return $attemptscount;
1299 * Figure out with this is a debug situation
1301 * @param object $scorm a moodle scrom object - mdl_scorm
1302 * @return boolean - debugging true/false
1304 function scorm_debugging($scorm) {
1306 $cfgscorm = get_config('scorm');
1308 if (!$cfgscorm->allowapidebug) {
1311 $identifier = $USER->username.':'.$scorm->name;
1312 $test = $cfgscorm->apidebugmask;
1313 // Check the regex is only a short list of safe characters.
1314 if (!preg_match('/^[\w\s\*\.\?\+\:\_\\\]+$/', $test)) {
1318 eval('$res = preg_match(\'/^'.$test.'/\', $identifier) ? true : false;');
1323 * Delete Scorm tracks for selected users
1325 * @param array $attemptids list of attempts that need to be deleted
1326 * @param stdClass $scorm instance
1328 * @return bool true deleted all responses, false failed deleting an attempt - stopped here
1330 function scorm_delete_responses($attemptids, $scorm) {
1331 if (!is_array($attemptids) || empty($attemptids)) {
1335 foreach ($attemptids as $num => $attemptid) {
1336 if (empty($attemptid)) {
1337 unset($attemptids[$num]);
1341 foreach ($attemptids as $attempt) {
1342 $keys = explode(':', $attempt);
1343 if (count($keys) == 2) {
1344 $userid = clean_param($keys[0], PARAM_INT);
1345 $attemptid = clean_param($keys[1], PARAM_INT);
1346 if (!$userid || !$attemptid || !scorm_delete_attempt($userid, $scorm, $attemptid)) {
1357 * Delete Scorm tracks for selected users
1359 * @param int $userid ID of User
1360 * @param stdClass $scorm Scorm object
1361 * @param int $attemptid user attempt that need to be deleted
1363 * @return bool true suceeded
1365 function scorm_delete_attempt($userid, $scorm, $attemptid) {
1368 $DB->delete_records('scorm_scoes_track', array('userid' => $userid, 'scormid' => $scorm->id, 'attempt' => $attemptid));
1369 $cm = get_coursemodule_from_instance('scorm', $scorm->id);
1371 // Trigger instances list viewed event.
1372 $event = \mod_scorm\event\attempt_deleted::create(array(
1373 'other' => array('attemptid' => $attemptid),
1374 'context' => context_module::instance($cm->id),
1375 'relateduserid' => $userid
1377 $event->add_record_snapshot('course_modules', $cm);
1378 $event->add_record_snapshot('scorm', $scorm);
1381 include_once('lib.php');
1382 scorm_update_grades($scorm, $userid, true);
1387 * Converts SCORM duration notation to human-readable format
1388 * The function works with both SCORM 1.2 and SCORM 2004 time formats
1389 * @param $duration string SCORM duration
1390 * @return string human-readable date/time
1392 function scorm_format_duration($duration) {
1393 // Fetch date/time strings.
1394 $stryears = get_string('years');
1395 $strmonths = get_string('nummonths');
1396 $strdays = get_string('days');
1397 $strhours = get_string('hours');
1398 $strminutes = get_string('minutes');
1399 $strseconds = get_string('seconds');
1401 if ($duration[0] == 'P') {
1402 // If timestamp starts with 'P' - it's a SCORM 2004 format
1403 // this regexp discards empty sections, takes Month/Minute ambiguity into consideration,
1404 // and outputs filled sections, discarding leading zeroes and any format literals
1405 // also saves the only zero before seconds decimals (if there are any) and discards decimals if they are zero.
1406 $pattern = array( '#([A-Z])0+Y#', '#([A-Z])0+M#', '#([A-Z])0+D#', '#P(|\d+Y)0*(\d+)M#',
1407 '#0*(\d+)Y#', '#0*(\d+)D#', '#P#', '#([A-Z])0+H#', '#([A-Z])[0.]+S#',
1408 '#\.0+S#', '#T(|\d+H)0*(\d+)M#', '#0*(\d+)H#', '#0+\.(\d+)S#',
1409 '#0*([\d.]+)S#', '#T#' );
1410 $replace = array( '$1', '$1', '$1', '$1$2 '.$strmonths.' ', '$1 '.$stryears.' ', '$1 '.$strdays.' ',
1411 '', '$1', '$1', 'S', '$1$2 '.$strminutes.' ', '$1 '.$strhours.' ',
1412 '0.$1 '.$strseconds, '$1 '.$strseconds, '');
1414 // Else we have SCORM 1.2 format there
1415 // first convert the timestamp to some SCORM 2004-like format for conveniency.
1416 $duration = preg_replace('#^(\d+):(\d+):([\d.]+)$#', 'T$1H$2M$3S', $duration);
1417 // Then convert in the same way as SCORM 2004.
1418 $pattern = array( '#T0+H#', '#([A-Z])0+M#', '#([A-Z])[0.]+S#', '#\.0+S#', '#0*(\d+)H#',
1419 '#0*(\d+)M#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' );
1420 $replace = array( 'T', '$1', '$1', 'S', '$1 '.$strhours.' ', '$1 '.$strminutes.' ',
1421 '0.$1 '.$strseconds, '$1 '.$strseconds, '' );
1424 $result = preg_replace($pattern, $replace, $duration);
1429 function scorm_get_toc_object($user, $scorm, $currentorg='', $scoid='', $mode='normal', $attempt='',
1430 $play=false, $organizationsco=null) {
1431 global $CFG, $DB, $PAGE, $OUTPUT;
1433 // Always pass the mode even if empty as that is what is done elsewhere and the urls have to match.
1434 $modestr = '&mode=';
1435 if ($mode != 'normal') {
1436 $modestr = '&mode='.$mode;
1440 $incomplete = false;
1442 if (!empty($organizationsco)) {
1443 $result[0] = $organizationsco;
1444 $result[0]->isvisible = true;
1445 $result[0]->statusicon = '';
1446 $result[0]->url = '';
1449 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)) {
1450 // Retrieve user tracking data for each learning object.
1451 $usertracks = array();
1452 foreach ($scoes as $sco) {
1453 if (!empty($sco->launch)) {
1454 if ($usertrack = scorm_get_tracks($sco->id, $user->id, $attempt)) {
1455 if ($usertrack->status == '') {
1456 $usertrack->status = 'notattempted';
1458 $usertracks[$sco->identifier] = $usertrack;
1462 foreach ($scoes as $sco) {
1463 if (!isset($sco->isvisible)) {
1464 $sco->isvisible = true;
1467 if (empty($sco->title)) {
1468 $sco->title = $sco->identifier;
1471 if (scorm_version_check($scorm->version, SCORM_13)) {
1472 $sco->prereq = true;
1474 $sco->prereq = empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites, $usertracks);
1477 if ($sco->isvisible) {
1478 if (!empty($sco->launch)) {
1479 if (empty($scoid) && ($mode != 'normal')) {
1483 if (isset($usertracks[$sco->identifier])) {
1484 $usertrack = $usertracks[$sco->identifier];
1485 $strstatus = get_string($usertrack->status, 'scorm');
1487 if ($sco->scormtype == 'sco') {
1488 $statusicon = html_writer::img($OUTPUT->pix_url($usertrack->status, 'scorm'), $strstatus,
1489 array('title' => $strstatus));
1491 $statusicon = html_writer::img($OUTPUT->pix_url('asset', 'scorm'), get_string('assetlaunched', 'scorm'),
1492 array('title' => get_string('assetlaunched', 'scorm')));
1495 if (($usertrack->status == 'notattempted') ||
1496 ($usertrack->status == 'incomplete') ||
1497 ($usertrack->status == 'browsed')) {
1499 if ($play && empty($scoid)) {
1504 $strsuspended = get_string('suspended', 'scorm');
1506 $exitvar = 'cmi.core.exit';
1508 if (scorm_version_check($scorm->version, SCORM_13)) {
1509 $exitvar = 'cmi.exit';
1512 if ($incomplete && isset($usertrack->{$exitvar}) && ($usertrack->{$exitvar} == 'suspend')) {
1513 $statusicon = html_writer::img($OUTPUT->pix_url('suspend', 'scorm'), $strstatus.' - '.$strsuspended,
1514 array('title' => $strstatus.' - '.$strsuspended));
1518 if ($play && empty($scoid)) {
1524 if ($sco->scormtype == 'sco') {
1525 $statusicon = html_writer::img($OUTPUT->pix_url('notattempted', 'scorm'),
1526 get_string('notattempted', 'scorm'),
1527 array('title' => get_string('notattempted', 'scorm')));
1529 $statusicon = html_writer::img($OUTPUT->pix_url('asset', 'scorm'), get_string('asset', 'scorm'),
1530 array('title' => get_string('asset', 'scorm')));
1536 if (empty($statusicon)) {
1537 $sco->statusicon = html_writer::img($OUTPUT->pix_url('notattempted', 'scorm'), get_string('notattempted', 'scorm'),
1538 array('title' => get_string('notattempted', 'scorm')));
1540 $sco->statusicon = $statusicon;
1543 $sco->url = 'a='.$scorm->id.'&scoid='.$sco->id.'¤torg='.$currentorg.$modestr.'&attempt='.$attempt;
1544 $sco->incomplete = $incomplete;
1546 if (!in_array($sco->id, array_keys($result))) {
1547 $result[$sco->id] = $sco;
1552 // Get the parent scoes!
1553 $result = scorm_get_toc_get_parent_child($result, $currentorg);
1555 // Be safe, prevent warnings from showing up while returning array.
1556 if (!isset($scoid)) {
1560 return array('scoes' => $result, 'usertracks' => $usertracks, 'scoid' => $scoid);
1563 function scorm_get_toc_get_parent_child(&$result, $currentorg) {
1566 // Organization is always the root, prevparent.
1567 if (!empty($currentorg)) {
1568 $prevparent = $currentorg;
1573 foreach ($result as $sco) {
1574 if ($sco->parent == '/') {
1575 $final[$level][$sco->identifier] = $sco;
1576 $prevparent = $sco->identifier;
1577 unset($result[$sco->id]);
1579 if ($sco->parent == $prevparent) {
1580 $final[$level][$sco->identifier] = $sco;
1581 $prevparent = $sco->identifier;
1582 unset($result[$sco->id]);
1584 if (!empty($final[$level])) {
1586 foreach ($final[$level] as $fin) {
1587 if ($sco->parent == $fin->identifier) {
1593 $final[$level][$sco->identifier] = $sco;
1594 unset($result[$sco->id]);
1598 $final[$level][$sco->identifier] = $sco;
1599 unset($result[$sco->id]);
1606 for ($i = 0; $i <= $level; $i++) {
1608 foreach ($final[$i] as $ident => $sco) {
1609 if (empty($prevparent)) {
1610 $prevparent = $ident;
1612 if (!isset($final[$i][$prevparent]->children)) {
1613 $final[$i][$prevparent]->children = array();
1615 if ($sco->parent == $prevparent) {
1616 $final[$i][$prevparent]->children[] = $sco;
1617 $prevparent = $ident;
1620 foreach ($final[$i] as $identifier => $scoobj) {
1621 if ($identifier == $sco->parent) {
1622 $parent = $identifier;
1626 if ($parent !== false) {
1627 $final[$i][$parent]->children[] = $sco;
1634 for ($i = 0; $i <= $level; $i++) {
1635 $keys = array_keys($final[$i]);
1636 $results[] = $final[$i][$keys[0]];
1642 function scorm_format_toc_for_treeview($user, $scorm, $scoes, $usertracks, $cmid, $toclink=TOCJSLINK, $currentorg='',
1643 $attempt='', $play=false, $organizationsco=null, $children=false) {
1646 $result = new stdClass();
1647 $result->prerequisites = true;
1648 $result->incomplete = true;
1652 $attemptsmade = scorm_get_attempt_count($user->id, $scorm);
1653 $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attemptsmade;
1657 $result->toc = html_writer::start_tag('ul');
1659 if (!$play && !empty($organizationsco)) {
1660 $result->toc .= html_writer::start_tag('li').$organizationsco->title.html_writer::end_tag('li');
1665 if (!empty($scoes)) {
1666 foreach ($scoes as $sco) {
1667 $result->toc .= html_writer::start_tag('li');
1670 $sco->isvisible = true;
1672 if ($sco->isvisible) {
1675 if (isset($usertracks[$sco->identifier])) {
1676 $viewscore = has_capability('mod/scorm:viewscores', context_module::instance($cmid));
1677 if (isset($usertracks[$sco->identifier]->score_raw) && $viewscore) {
1678 if ($usertracks[$sco->identifier]->score_raw != '') {
1679 $score = '('.get_string('score', 'scorm').': '.$usertracks[$sco->identifier]->score_raw.')';
1684 if (!empty($sco->prereq)) {
1685 if ($sco->id == $scoid) {
1686 $result->prerequisites = true;
1689 if (!empty($prevsco) && scorm_version_check($scorm->version, SCORM_13) && !empty($prevsco->hidecontinue)) {
1690 if ($sco->scormtype == 'sco') {
1691 $result->toc .= html_writer::span($sco->statusicon.' '.format_string($sco->title));
1693 $result->toc .= html_writer::span(' '.format_string($sco->title));
1695 } else if ($toclink == TOCFULLURL) {
1696 $url = $CFG->wwwroot.'/mod/scorm/player.php?'.$sco->url;
1697 if (!empty($sco->launch)) {
1698 if ($sco->scormtype == 'sco') {
1699 $result->toc .= $sco->statusicon.' ';
1700 $result->toc .= html_writer::link($url, format_string($sco->title)).$score;
1702 $result->toc .= ' '.html_writer::link($url, format_string($sco->title),
1703 array('data-scoid' => $sco->id)).$score;
1706 if ($sco->scormtype == 'sco') {
1707 $result->toc .= $sco->statusicon.' '.format_string($sco->title).$score;
1709 $result->toc .= ' '.format_string($sco->title).$score;
1713 if (!empty($sco->launch)) {
1714 if ($sco->scormtype == 'sco') {
1715 $result->toc .= html_writer::tag('a', $sco->statusicon.' '.
1716 format_string($sco->title).' '.$score,
1717 array('data-scoid' => $sco->id, 'title' => $sco->url));
1719 $result->toc .= html_writer::tag('a', ' '.format_string($sco->title).' '.$score,
1720 array('data-scoid' => $sco->id, 'title' => $sco->url));
1723 if ($sco->scormtype == 'sco') {
1724 $result->toc .= html_writer::span($sco->statusicon.' '.format_string($sco->title));
1726 $result->toc .= html_writer::span(' '.format_string($sco->title));
1733 if ($sco->scormtype == 'sco') {
1734 $result->toc .= html_writer::span($sco->statusicon.' '.format_string($sco->title));
1736 $result->toc .= ' '.format_string($sco->title).html_writer::end_span();
1739 if ($sco->scormtype == 'sco') {
1740 $result->toc .= $sco->statusicon.' '.format_string($sco->title);
1742 $result->toc .= ' '.format_string($sco->title);
1748 $result->toc .= " ".format_string($sco->title);
1751 if (!empty($sco->children)) {
1752 $result->toc .= html_writer::start_tag('ul');
1753 $childresult = scorm_format_toc_for_treeview($user, $scorm, $sco->children, $usertracks, $cmid,
1754 $toclink, $currentorg, $attempt, $play, $organizationsco, true);
1755 $result->toc .= $childresult->toc;
1756 $result->toc .= html_writer::end_tag('ul');
1757 $result->toc .= html_writer::end_tag('li');
1759 $result->toc .= html_writer::end_tag('li');
1763 $result->incomplete = $sco->incomplete;
1767 $result->toc .= html_writer::end_tag('ul');
1773 function scorm_format_toc_for_droplist($scorm, $scoes, $usertracks, $currentorg='', $organizationsco=null,
1774 $children=false, $level=0, $tocmenus=array()) {
1775 if (!empty($scoes)) {
1776 if (!empty($organizationsco) && !$children) {
1777 $tocmenus[$organizationsco->id] = $organizationsco->title;
1780 $parents[$level] = '/';
1781 foreach ($scoes as $sco) {
1782 if ($parents[$level] != $sco->parent) {
1783 if ($newlevel = array_search($sco->parent, $parents)) {
1787 while (($i > 0) && ($parents[$level] != $sco->parent)) {
1791 if (($i == 0) && ($sco->parent != $currentorg)) {
1797 $parents[$level] = $sco->parent;
1802 if ($sco->scormtype == 'sco') {
1803 $tocmenus[$sco->id] = scorm_repeater('−', $level) . '>' . format_string($sco->title);
1806 if ($sco->scormtype == 'sco') {
1807 $tocmenus[$sco->id] = scorm_repeater('−', $level) . '>' . format_string($sco->title);
1811 if (!empty($sco->children)) {
1812 $tocmenus = scorm_format_toc_for_droplist($scorm, $sco->children, $usertracks, $currentorg,
1813 $organizationsco, true, $level, $tocmenus);
1821 function scorm_get_toc($user, $scorm, $cmid, $toclink=TOCJSLINK, $currentorg='', $scoid='', $mode='normal',
1822 $attempt='', $play=false, $tocheader=false) {
1823 global $CFG, $DB, $OUTPUT;
1825 if (empty($attempt)) {
1826 $attempt = scorm_get_last_attempt($scorm->id, $user->id);
1829 $result = new stdClass();
1830 $organizationsco = null;
1833 $result->toc = html_writer::start_div('yui3-g-r', array('id' => 'scorm_layout'));
1834 $result->toc .= html_writer::start_div('yui3-u-1-5', array('id' => 'scorm_toc'));
1835 $result->toc .= html_writer::div('', '', array('id' => 'scorm_toc_title'));
1836 $result->toc .= html_writer::start_div('', array('id' => 'scorm_tree'));
1839 if (!empty($currentorg)) {
1840 $organizationsco = $DB->get_record('scorm_scoes', array('scorm' => $scorm->id, 'identifier' => $currentorg));
1841 if (!empty($organizationsco->title)) {
1843 $result->toctitle = $organizationsco->title;
1848 $scoes = scorm_get_toc_object($user, $scorm, $currentorg, $scoid, $mode, $attempt, $play, $organizationsco);
1850 $treeview = scorm_format_toc_for_treeview($user, $scorm, $scoes['scoes'][0]->children, $scoes['usertracks'], $cmid,
1851 $toclink, $currentorg, $attempt, $play, $organizationsco, false);
1854 $result->toc .= $treeview->toc;
1856 $result->toc = $treeview->toc;
1859 if (!empty($scoes['scoid'])) {
1860 $scoid = $scoes['scoid'];
1863 if (empty($scoid)) {
1864 // If this is a normal package with an org sco and child scos get the first child.
1865 if (!empty($scoes['scoes'][0]->children)) {
1866 $result->sco = $scoes['scoes'][0]->children[0];
1867 } else { // This package only has one sco - it may be a simple external AICC package.
1868 $result->sco = $scoes['scoes'][0];
1872 $result->sco = scorm_get_sco($scoid);
1875 if ($scorm->hidetoc == SCORM_TOC_POPUP) {
1876 $tocmenu = scorm_format_toc_for_droplist($scorm, $scoes['scoes'][0]->children, $scoes['usertracks'],
1877 $currentorg, $organizationsco);
1880 if ($mode != 'normal') {
1881 $modestr = '&mode='.$mode;
1884 $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'¤torg='.$currentorg.$modestr);
1885 $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenu, $result->sco->id, null, "tocmenu");
1888 $result->prerequisites = $treeview->prerequisites;
1889 $result->incomplete = $treeview->incomplete;
1890 $result->attemptleft = $treeview->attemptleft;
1893 $result->toc .= html_writer::end_div().html_writer::end_div();
1894 $result->toc .= html_writer::start_div('', array('id' => 'scorm_toc_toggle'));
1895 $result->toc .= html_writer::tag('button', '', array('id' => 'scorm_toc_toggle_btn')).html_writer::end_div();
1896 $result->toc .= html_writer::start_div('', array('id' => 'scorm_content'));
1897 $result->toc .= html_writer::div('', '', array('id' => 'scorm_navpanel'));
1898 $result->toc .= html_writer::end_div().html_writer::end_div();
1904 function scorm_get_adlnav_json ($scoes, &$adlnav = array(), $parentscoid = null) {
1905 if (is_object($scoes)) {
1907 if (isset($sco->url)) {
1908 $adlnav[$sco->id]['identifier'] = $sco->identifier;
1909 $adlnav[$sco->id]['launch'] = $sco->launch;
1910 $adlnav[$sco->id]['title'] = $sco->title;
1911 $adlnav[$sco->id]['url'] = $sco->url;
1912 $adlnav[$sco->id]['parent'] = $sco->parent;
1913 if (isset($sco->choice)) {
1914 $adlnav[$sco->id]['choice'] = $sco->choice;
1916 if (isset($sco->flow)) {
1917 $adlnav[$sco->id]['flow'] = $sco->flow;
1918 } else if (isset($parentscoid) && isset($adlnav[$parentscoid]['flow'])) {
1919 $adlnav[$sco->id]['flow'] = $adlnav[$parentscoid]['flow'];
1921 if (isset($sco->isvisible)) {
1922 $adlnav[$sco->id]['isvisible'] = $sco->isvisible;
1924 if (isset($sco->parameters)) {
1925 $adlnav[$sco->id]['parameters'] = $sco->parameters;
1927 if (isset($sco->hidecontinue)) {
1928 $adlnav[$sco->id]['hidecontinue'] = $sco->hidecontinue;
1930 if (isset($sco->hideprevious)) {
1931 $adlnav[$sco->id]['hideprevious'] = $sco->hideprevious;
1933 if (isset($sco->hidesuspendall)) {
1934 $adlnav[$sco->id]['hidesuspendall'] = $sco->hidesuspendall;
1936 if (!empty($parentscoid)) {
1937 $adlnav[$sco->id]['parentscoid'] = $parentscoid;
1939 if (isset($adlnav['prevscoid'])) {
1940 $adlnav[$sco->id]['prevscoid'] = $adlnav['prevscoid'];
1941 $adlnav[$adlnav['prevscoid']]['nextscoid'] = $sco->id;
1942 if (isset($adlnav['prevparent']) && $adlnav['prevparent'] == $sco->parent) {
1943 $adlnav[$sco->id]['prevsibling'] = $adlnav['prevscoid'];
1944 $adlnav[$adlnav['prevscoid']]['nextsibling'] = $sco->id;
1947 $adlnav['prevscoid'] = $sco->id;
1948 $adlnav['prevparent'] = $sco->parent;
1950 if (isset($sco->children)) {
1951 foreach ($sco->children as $children) {
1952 scorm_get_adlnav_json($children, $adlnav, $sco->id);
1956 foreach ($scoes as $sco) {
1957 scorm_get_adlnav_json ($sco, $adlnav);
1959 unset($adlnav['prevscoid']);
1960 unset($adlnav['prevparent']);
1962 return json_encode($adlnav);
1966 * Check for the availability of a resource by URL.
1968 * Check is performed using an HTTP HEAD call.
1970 * @param $url string A valid URL
1971 * @return bool|string True if no issue is found. The error string message, otherwise
1973 function scorm_check_url($url) {
1975 // Same options as in {@link download_file_content()}, used in {@link scorm_parse_scorm()}.
1976 $curl->setopt(array('CURLOPT_FOLLOWLOCATION' => true, 'CURLOPT_MAXREDIRS' => 5));
1977 $cmsg = $curl->head($url);
1978 $info = $curl->get_info();
1979 if (empty($info['http_code']) || $info['http_code'] != 200) {
1980 return get_string('invalidurlhttpcheck', 'scorm', array('cmsg' => $cmsg));
1987 * Check for a parameter in userdata and return it if it's set
1988 * or return the value from $ifempty if its empty
1990 * @param stdClass $userdata Contains user's data
1991 * @param string $param parameter that should be checked
1992 * @param string $ifempty value to be replaced with if $param is not set
1993 * @return string value from $userdata->$param if its not empty, or $ifempty
1995 function scorm_isset($userdata, $param, $ifempty = '') {
1996 if (isset($userdata->$param)) {
1997 return $userdata->$param;