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/>.
17 require_once("$CFG->dirroot/mod/scorm/lib.php");
18 require_once("$CFG->libdir/filelib.php");
20 /// Constants and settings for module scorm
21 define('SCORM_UPDATE_NEVER', '0');
22 define('SCORM_UPDATE_EVERYDAY', '2');
23 define('SCORM_UPDATE_EVERYTIME', '3');
26 define('SCO_DATA', 1);
27 define('SCO_ONLY', 2);
29 define('GRADESCOES', '0');
30 define('GRADEHIGHEST', '1');
31 define('GRADEAVERAGE', '2');
32 define('GRADESUM', '3');
34 define('HIGHESTATTEMPT', '0');
35 define('AVERAGEATTEMPT', '1');
36 define('FIRSTATTEMPT', '2');
37 define('LASTATTEMPT', '3');
39 define('TOCJSLINK', 1);
40 define('TOCFULLURL', 2);
42 /// Local Library of functions for module scorm
46 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 class scorm_package_file_info extends file_info_stored {
50 public function get_parent() {
51 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
52 return $this->browser->get_file_info($this->context);
54 return parent::get_parent();
56 public function get_visible_name() {
57 if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
58 return $this->topvisiblename;
60 return parent::get_visible_name();
65 * Returns an array of the popup options for SCORM and each options default value
67 * @return array an array of popup options as the key and their defaults as the value
69 function scorm_get_popup_options_array() {
71 $cfg_scorm = get_config('scorm');
73 return array('resizable'=> isset($cfg_scorm->resizable) ? $cfg_scorm->resizable : 0,
74 'scrollbars'=> isset($cfg_scorm->scrollbars) ? $cfg_scorm->scrollbars : 0,
75 'directories'=> isset($cfg_scorm->directories) ? $cfg_scorm->directories : 0,
76 'location'=> isset($cfg_scorm->location) ? $cfg_scorm->location : 0,
77 'menubar'=> isset($cfg_scorm->menubar) ? $cfg_scorm->menubar : 0,
78 'toolbar'=> isset($cfg_scorm->toolbar) ? $cfg_scorm->toolbar : 0,
79 'status'=> isset($cfg_scorm->status) ? $cfg_scorm->status : 0);
83 * Returns an array of the array of what grade options
85 * @return array an array of what grade options
87 function scorm_get_grade_method_array() {
88 return array (GRADESCOES => get_string('gradescoes', 'scorm'),
89 GRADEHIGHEST => get_string('gradehighest', 'scorm'),
90 GRADEAVERAGE => get_string('gradeaverage', 'scorm'),
91 GRADESUM => get_string('gradesum', 'scorm'));
95 * Returns an array of the array of what grade options
97 * @return array an array of what grade options
99 function scorm_get_what_grade_array() {
100 return array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'),
101 AVERAGEATTEMPT => get_string('averageattempt', 'scorm'),
102 FIRSTATTEMPT => get_string('firstattempt', 'scorm'),
103 LASTATTEMPT => get_string('lastattempt', 'scorm'));
107 * Returns an array of the array of skip view options
109 * @return array an array of skip view options
111 function scorm_get_skip_view_array() {
112 return array(0 => get_string('never'),
113 1 => get_string('firstaccess', 'scorm'),
114 2 => get_string('always'));
118 * Returns an array of the array of hide table of contents options
120 * @return array an array of hide table of contents options
122 function scorm_get_hidetoc_array() {
123 return array(SCORM_TOC_SIDE => get_string('sided', 'scorm'),
124 SCORM_TOC_HIDDEN => get_string('hidden', 'scorm'),
125 SCORM_TOC_POPUP => get_string('popupmenu', 'scorm'),
126 SCORM_TOC_DISABLED => get_string('disabled', 'scorm'));
130 * Returns an array of the array of update frequency options
132 * @return array an array of update frequency options
134 function scorm_get_updatefreq_array() {
135 return array(SCORM_UPDATE_NEVER => get_string('never'),
136 SCORM_UPDATE_EVERYDAY => get_string('everyday', 'scorm'),
137 SCORM_UPDATE_EVERYTIME => get_string('everytime', 'scorm'));
141 * Returns an array of the array of popup display options
143 * @return array an array of popup display options
145 function scorm_get_popup_display_array() {
146 return array(0 => get_string('currentwindow', 'scorm'),
147 1 => get_string('popup', 'scorm'));
151 * Returns an array of the array of attempt options
153 * @return array an array of attempt options
155 function scorm_get_attempts_array() {
156 $attempts = array(0 => get_string('nolimit', 'scorm'),
157 1 => get_string('attempt1', 'scorm'));
159 for ($i=2; $i<=6; $i++) {
160 $attempts[$i] = get_string('attemptsx', 'scorm', $i);
166 * Extracts scrom package, sets up all variables.
167 * Called whenever scorm changes
168 * @param object $scorm instance - fields are updated and changes saved into database
169 * @param bool $full force full update if true
172 function scorm_parse($scorm, $full) {
174 $cfg_scorm = get_config('scorm');
176 if (!isset($scorm->cmid)) {
177 $cm = get_coursemodule_from_instance('scorm', $scorm->id);
178 $scorm->cmid = $cm->id;
180 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
181 $newhash = $scorm->sha1hash;
183 if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
185 $fs = get_file_storage();
186 $packagefile = false;
188 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
189 if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) {
190 $newhash = $packagefile->get_contenthash();
195 if (!$cfg_scorm->allowtypelocalsync) {
196 // sorry - localsync disabled
199 if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
200 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
201 $file_record = array('contextid'=>$context->id, 'component'=>'mod_scorm', 'filearea'=>'package', 'itemid'=>0, 'filepath'=>'/');
202 if ($packagefile = $fs->create_file_from_url($file_record, $scorm->reference, array('calctimeout' => true))) {
203 $newhash = sha1($scorm->reference);
211 if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
212 if (strpos($scorm->version, 'SCORM') !== false) {
213 if ($fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
217 } else if (strpos($scorm->version, 'AICC') !== false) {
218 // TODO: add more sanity checks - something really exists in scorm_content area
224 $fs->delete_area_files($context->id, 'mod_scorm', 'content');
226 $packer = get_file_packer('application/zip');
227 $packagefile->extract_to_storage($packer, $context->id, 'mod_scorm', 'content', 0, '/');
233 if ($manifest = $fs->get_file($context->id, 'mod_scorm', 'content', 0, '/', 'imsmanifest.xml')) {
234 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
236 if (!scorm_parse_scorm($scorm, $manifest)) {
237 $scorm->version = 'ERROR';
240 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
242 if (!scorm_parse_aicc($scorm)) {
243 $scorm->version = 'ERROR';
245 $scorm->version = 'AICC';
248 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $cfg_scorm->allowtypeexternal) {
249 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
252 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
253 // SCORM only, AICC can not be external
254 if (!scorm_parse_scorm($scorm, $scorm->reference)) {
255 $scorm->version = 'ERROR';
257 $newhash = sha1($scorm->reference);
259 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY and !empty($CFG->repositoryactivate) and $cfg_scorm->allowtypeimsrepository) {
260 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
263 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
264 if (!scorm_parse_scorm($scorm, $CFG->repository.substr($scorm->reference, 1).'/imsmanifest.xml')) {
265 $scorm->version = 'ERROR';
267 $newhash = sha1($scorm->reference);
268 } else if ($scorm->scormtype === SCORM_TYPE_AICCURL and $cfg_scorm->allowtypeexternalaicc) {
269 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
271 if (!scorm_parse_aicc($scorm)) {
272 $scorm->version = 'ERROR';
274 $scorm->version = 'AICC';
276 // sorry, disabled type
281 $scorm->sha1hash = $newhash;
282 $DB->update_record('scorm', $scorm);
286 function scorm_array_search($item, $needle, $haystacks, $strict=false) {
287 if (!empty($haystacks)) {
288 foreach ($haystacks as $key => $element) {
290 if ($element->{$item} === $needle) {
294 if ($element->{$item} == $needle) {
303 function scorm_repeater($what, $times) {
308 for ($i=0; $i<$times; $i++) {
314 function scorm_external_link($link) {
315 // check if a link is external
317 $link = strtolower($link);
318 if (substr($link, 0, 7) == 'http://') {
320 } else if (substr($link, 0, 8) == 'https://') {
322 } else if (substr($link, 0, 4) == 'www.') {
329 * Returns an object containing all datas relative to the given sco ID
331 * @param integer $id The sco ID
332 * @return mixed (false if sco id does not exists)
334 function scorm_get_sco($id, $what=SCO_ALL) {
337 if ($sco = $DB->get_record('scorm_scoes', array('id'=>$id))) {
338 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
339 if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id)))) {
340 foreach ($scodatas as $scodata) {
341 $sco->{$scodata->name} = $scodata->value;
343 } else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id))))) {
344 $sco->parameters = '';
353 * Returns an object (array) containing all the scoes data related to the given sco ID
355 * @param integer $id The sco ID
356 * @param integer $organisation an organisation ID - defaults to false if not required
357 * @return mixed (false if there are no scoes or an array)
359 function scorm_get_scoes($id, $organisation=false) {
362 $organizationsql = '';
363 $queryarray = array('scorm'=>$id);
364 if (!empty($organisation)) {
365 $queryarray['organization'] = $organisation;
367 if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'id ASC')) {
368 // drop keys so that it is a simple array as expected
369 $scoes = array_values($scoes);
370 foreach ($scoes as $sco) {
371 if ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
372 foreach ($scodatas as $scodata) {
373 $sco->{$scodata->name} = $scodata->value;
383 function scorm_insert_track($userid, $scormid, $scoid, $attempt, $element, $value, $forcecompleted=false) {
388 if ($forcecompleted) {
389 //TODO - this could be broadened to encompass SCORM 2004 in future
390 if (($element == 'cmi.core.lesson_status') && ($value == 'incomplete')) {
391 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))) {
392 $value = 'completed';
395 if ($element == 'cmi.core.score.raw') {
396 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))) {
397 if ($tracktest->value == "incomplete") {
398 $tracktest->value = "completed";
399 $DB->update_record('scorm_scoes_track', $tracktest);
405 if ($track = $DB->get_record('scorm_scoes_track', array('userid'=>$userid, 'scormid'=>$scormid, 'scoid'=>$scoid, 'attempt'=>$attempt, 'element'=>$element))) {
406 if ($element != 'x.start.time' ) { //don't update x.start.time - keep the original value.
407 $track->value = $value;
408 $track->timemodified = time();
409 $DB->update_record('scorm_scoes_track', $track);
413 $track->userid = $userid;
414 $track->scormid = $scormid;
415 $track->scoid = $scoid;
416 $track->attempt = $attempt;
417 $track->element = $element;
418 $track->value = $value;
419 $track->timemodified = time();
420 $id = $DB->insert_record('scorm_scoes_track', $track);
423 if (strstr($element, '.score.raw') ||
424 (($element == 'cmi.core.lesson_status' || $element == 'cmi.completion_status') && ($track->value == 'completed' || $track->value == 'passed'))) {
425 $scorm = $DB->get_record('scorm', array('id' => $scormid));
426 include_once($CFG->dirroot.'/mod/scorm/lib.php');
427 scorm_update_grades($scorm, $userid);
433 function scorm_get_tracks($scoid, $userid, $attempt='') {
434 /// Gets all tracks of specified sco and user
437 if (empty($attempt)) {
438 if ($scormid = $DB->get_field('scorm_scoes', 'scorm', array('id'=>$scoid))) {
439 $attempt = scorm_get_last_attempt($scormid, $userid);
444 if ($tracks = $DB->get_records('scorm_scoes_track', array('userid'=>$userid, 'scoid'=>$scoid, 'attempt'=>$attempt), 'element ASC')) {
445 $usertrack = new stdClass();
446 $usertrack->userid = $userid;
447 $usertrack->scoid = $scoid;
448 // Defined in order to unify scorm1.2 and scorm2004
449 $usertrack->score_raw = '';
450 $usertrack->status = '';
451 $usertrack->total_time = '00:00:00';
452 $usertrack->session_time = '00:00:00';
453 $usertrack->timemodified = 0;
454 foreach ($tracks as $track) {
455 $element = $track->element;
456 $usertrack->{$element} = $track->value;
458 case 'cmi.core.lesson_status':
459 case 'cmi.completion_status':
460 if ($track->value == 'not attempted') {
461 $track->value = 'notattempted';
463 $usertrack->status = $track->value;
465 case 'cmi.core.score.raw':
466 case 'cmi.score.raw':
467 $usertrack->score_raw = (float) sprintf('%2.2f', $track->value);
469 case 'cmi.core.session_time':
470 case 'cmi.session_time':
471 $usertrack->session_time = $track->value;
473 case 'cmi.core.total_time':
474 case 'cmi.total_time':
475 $usertrack->total_time = $track->value;
478 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
479 $usertrack->timemodified = $track->timemodified;
482 if (is_array($usertrack)) {
492 /* Find the start and finsh time for a a given SCO attempt
494 * @param int $scormid SCORM Id
495 * @param int $scoid SCO Id
496 * @param int $userid User Id
497 * @param int $attemt Attempt Id
499 * @return object start and finsh time EPOC secods
502 function scorm_get_sco_runtime($scormid, $scoid, $userid, $attempt=1) {
505 $timedata = new stdClass();
506 $sql = !empty($scoid) ? "userid=$userid AND scormid=$scormid AND scoid=$scoid AND attempt=$attempt" : "userid=$userid AND scormid=$scormid AND attempt=$attempt";
507 $tracks = $DB->get_records_select('scorm_scoes_track', "$sql ORDER BY timemodified ASC");
509 $tracks = array_values($tracks);
513 $timedata->start = $tracks[0]->timemodified;
515 $timedata->start = false;
517 if ($tracks && $track = array_pop($tracks)) {
518 $timedata->finish = $track->timemodified;
520 $timedata->finish = $timedata->start;
526 function scorm_get_user_data($userid) {
528 /// Gets user info required to display the table of scorm results
531 return $DB->get_record('user', array('id'=>$userid), user_picture::fields());
534 function scorm_grade_user_attempt($scorm, $userid, $attempt=1) {
536 $attemptscore = null;
537 $attemptscore->scoes = 0;
538 $attemptscore->values = 0;
539 $attemptscore->max = 0;
540 $attemptscore->sum = 0;
541 $attemptscore->lastmodify = 0;
543 if (!$scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
547 foreach ($scoes as $sco) {
548 if ($userdata=scorm_get_tracks($sco->id, $userid, $attempt)) {
549 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
550 $attemptscore->scoes++;
552 if (!empty($userdata->score_raw) || (isset($scorm->type) && $scorm->type=='sco' && isset($userdata->score_raw))) {
553 $attemptscore->values++;
554 $attemptscore->sum += $userdata->score_raw;
555 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
556 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
557 $attemptscore->lastmodify = $userdata->timemodified;
559 $attemptscore->lastmodify = 0;
564 switch ($scorm->grademethod) {
566 $score = (float) $attemptscore->max;
569 if ($attemptscore->values > 0) {
570 $score = $attemptscore->sum/$attemptscore->values;
576 $score = $attemptscore->sum;
579 $score = $attemptscore->scoes;
582 $score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default
588 function scorm_grade_user($scorm, $userid) {
590 // ensure we dont grade user beyond $scorm->maxattempt settings
591 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
592 if ($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt) {
593 $lastattempt = $scorm->maxattempt;
596 switch ($scorm->whatgrade) {
598 return scorm_grade_user_attempt($scorm, $userid, 1);
601 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_completed_attempt($scorm->id, $userid));
605 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
606 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
607 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
613 $attemptcount = scorm_get_attempt_count($userid, $scorm, true);
614 if (empty($attemptcount)) {
617 $attemptcount = count($attemptcount);
619 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
621 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
622 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt);
623 $sumscore += $attemptscore;
626 return round($sumscore / $attemptcount);
631 function scorm_count_launchable($scormid, $organization='') {
634 $sqlorganization = '';
635 $params = array($scormid);
636 if (!empty($organization)) {
637 $sqlorganization = " AND organization=?";
638 $params[] = $organization;
640 return $DB->count_records_select('scorm_scoes', "scorm = ? $sqlorganization AND ".$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), $params);
643 function scorm_get_last_attempt($scormid, $userid) {
646 /// Find the last attempt number for the given user id and scorm id
647 if ($lastattempt = $DB->get_record('scorm_scoes_track', array('userid'=>$userid, 'scormid'=>$scormid), 'max(attempt) as a')) {
648 if (empty($lastattempt->a)) {
651 return $lastattempt->a;
658 function scorm_get_last_completed_attempt($scormid, $userid) {
661 /// Find the last attempt number for the given user id and scorm id
662 if ($lastattempt = $DB->get_record_select('scorm_scoes_track', "userid = ? AND scormid = ? AND (value='completed' OR value='passed')", array($userid, $scormid), 'max(attempt) as a')) {
663 if (empty($lastattempt->a)) {
666 return $lastattempt->a;
673 function scorm_course_format_display($user, $course) {
674 global $CFG, $DB, $PAGE, $OUTPUT;
676 $strupdate = get_string('update');
677 $context = get_context_instance(CONTEXT_COURSE, $course->id);
679 echo '<div class="mod-scorm">';
680 if ($scorms = get_all_instances_in_course('scorm', $course)) {
681 // The module SCORM activity with the least id is the course
682 $scorm = current($scorms);
683 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
684 print_error('invalidcoursemodule');
686 $contextmodule = get_context_instance(CONTEXT_MODULE, $cm->id);
687 if ((has_capability('mod/scorm:skipview', $contextmodule))) {
688 scorm_simple_play($scorm, $user, $contextmodule, $cm->id);
691 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
692 if (has_capability('moodle/course:manageactivities', $context)) {
693 if ($PAGE->user_is_editing()) {
694 // Display update icon
695 $path = $CFG->wwwroot.'/course';
696 $headertext .= '<span class="commands">'.
697 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&sesskey='.sesskey().'">'.
698 '<img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
700 $headertext .= '</td>';
701 // Display report link
702 $trackedusers = $DB->get_record('scorm_scoes_track', array('scormid'=>$scorm->id), 'count(distinct(userid)) as c');
703 if ($trackedusers->c > 0) {
704 $headertext .= '<td class="reportlink">'.
705 '<a href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
706 get_string('viewallreports', 'scorm', $trackedusers->c).'</a>';
708 $headertext .= '<td class="reportlink">'.get_string('noreports', 'scorm');
710 $colspan = ' colspan="2"';
712 $headertext .= '</td></tr><tr><td'.$colspan.'>'.get_string('summary').':<br />'.format_module_intro('scorm', $scorm, $scorm->coursemodule).'</td></tr></table>';
713 echo $OUTPUT->box($headertext, 'generalbox boxwidthwide');
714 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm);
716 if (has_capability('moodle/course:update', $context)) {
717 // Create a new activity
718 $url = new moodle_url('/course/mod.php', array('id'=>$course->id, 'section'=>'0', 'sesskey'=>sesskey(),'add'=>'scorm'));
721 echo $OUTPUT->notification('Could not find a scorm course here');
727 function scorm_view_display ($user, $scorm, $action, $cm) {
728 global $CFG, $DB, $PAGE, $OUTPUT;
730 if ($scorm->scormtype != SCORM_TYPE_LOCAL && $scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
731 scorm_parse($scorm, false);
734 $organization = optional_param('organization', '', PARAM_INT);
736 if ($scorm->displaycoursestructure == 1) {
737 echo $OUTPUT->box_start('generalbox boxaligncenter toc');
739 <div class="structurehead"><?php print_string('contents', 'scorm') ?></div>
742 if (empty($organization)) {
743 $organization = $scorm->launch;
745 if ($orgs = $DB->get_records_select_menu('scorm_scoes', 'scorm = ? AND '.
746 $DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
747 $DB->sql_isempty('scorm_scoes', 'organization', false, false),
748 array($scorm->id), 'id', 'id,title')) {
749 if (count($orgs) > 1) {
750 $select = new single_select(new moodle_url($action), 'organization', $orgs, $organization, null);
751 $select->label = get_string('organizations', 'scorm');
752 $select->class = 'scorm-center';
753 echo $OUTPUT->render($select);
757 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
758 if (($sco->organization == '') && ($sco->launch == '')) {
759 $orgidentifier = $sco->identifier;
761 $orgidentifier = $sco->organization;
765 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
766 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
767 $scorm->version = 'scorm_12';
769 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
771 $result = scorm_get_toc($user, $scorm, $cm->id, TOCFULLURL, $orgidentifier);
772 $incomplete = $result->incomplete;
774 // do we want the TOC to be displayed?
775 if ($scorm->displaycoursestructure == 1) {
777 echo $OUTPUT->box_end();
780 // is this the first attempt ?
781 $attemptcount = scorm_get_attempt_count($user->id, $scorm);
783 // do not give the player launch FORM if the SCORM object is locked after the final attempt
784 if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) {
786 <div class="scorm-center">
787 <form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php">
789 if ($scorm->hidebrowse == 0) {
790 print_string('mode', 'scorm');
791 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse', 'scorm').'</label>'."\n";
792 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal', 'scorm')."</label>\n";
794 echo '<input type="hidden" name="mode" value="normal" />'."\n";
796 if ($scorm->forcenewattempt == 1) {
797 if ($incomplete === false) {
798 echo '<input type="hidden" name="newattempt" value="on" />'."\n";
800 } else if (!empty($attemptcount) && ($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
803 <input type="checkbox" id="a" name="newattempt" />
804 <label for="a"><?php print_string('newattempt', 'scorm') ?></label>
809 <input type="hidden" name="scoid"/>
810 <input type="hidden" name="cm" value="<?php echo $cm->id ?>"/>
811 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
812 <input type="submit" value="<?php print_string('enter', 'scorm') ?>" />
819 function scorm_simple_play($scorm, $user, $context, $cmid) {
824 if ($scorm->scormtype != SCORM_TYPE_LOCAL && $scorm->updatefreq == SCORM_UPDATE_EVERYTIME) {
825 scorm_parse($scorm, false);
827 if (has_capability('mod/scorm:viewreport', $context)) { //if this user can view reports, don't skipview so they can see links to reports.
831 $scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id), 'id', 'id');
835 if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
836 if (($sco->organization == '') && ($sco->launch == '')) {
837 $orgidentifier = $sco->identifier;
839 $orgidentifier = $sco->organization;
842 if ($scorm->skipview >= 1) {
843 $sco = current($scoes);
844 $url = new moodle_url('/mod/scorm/player.php', array('a' => $scorm->id,
845 'currentorg'=>$orgidentifier,
847 if ($scorm->skipview == 2 || scorm_get_tracks($sco->id, $user->id) === false) {
848 if (!empty($scorm->forcenewattempt)) {
849 $result = scorm_get_toc($user, $scorm, $cmid, TOCFULLURL, $orgidentifier);
850 if ($result->incomplete === false) {
851 $url->param('newattempt','on');
861 function scorm_get_count_users($scormid, $groupingid=null) {
864 if (!empty($groupingid)) {
865 $sql = "SELECT COUNT(DISTINCT st.userid)
866 FROM {scorm_scoes_track} st
867 INNER JOIN {groups_members} gm ON st.userid = gm.userid
868 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
869 WHERE st.scormid = ? AND gg.groupingid = ?
871 $params = array($scormid, $groupingid);
873 $sql = "SELECT COUNT(DISTINCT st.userid)
874 FROM {scorm_scoes_track} st
877 $params = array($scormid);
880 return ($DB->count_records_sql($sql, $params));
884 * Build up the JavaScript representation of an array element
886 * @param string $sversion SCORM API version
887 * @param array $userdata User track data
888 * @param string $element_name Name of array element to get values for
889 * @param array $children list of sub elements of this array element that also need instantiating
892 function scorm_reconstitute_array_element($sversion, $userdata, $element_name, $children) {
893 // reconstitute comments_from_learner and comments_from_lms
895 $current_subelement = '';
899 $scormseperator = '_';
900 if (scorm_version_check($sversion, SCORM_13)) { //scorm 1.3 elements use a . instead of an _
901 $scormseperator = '.';
903 // filter out the ones we want
904 $element_list = array();
905 foreach ($userdata as $element => $value) {
906 if (substr($element, 0, strlen($element_name)) == $element_name) {
907 $element_list[$element] = $value;
911 // sort elements in .n array order
912 uksort($element_list, "scorm_element_cmp");
914 // generate JavaScript
915 foreach ($element_list as $element => $value) {
916 if (scorm_version_check($sversion, SCORM_13)) {
917 $element = preg_replace('/\.(\d+)\./', ".N\$1.", $element);
918 preg_match('/\.(N\d+)\./', $element, $matches);
920 $element = preg_replace('/\.(\d+)\./', "_\$1.", $element);
921 preg_match('/\_(\d+)\./', $element, $matches);
923 if (count($matches) > 0 && $current != $matches[1]) {
924 if ($count_sub > 0) {
925 echo ' '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
927 $current = $matches[1];
929 $current_subelement = '';
932 $end = strpos($element, $matches[1])+strlen($matches[1]);
933 $subelement = substr($element, 0, $end);
934 echo ' '.$subelement." = new Object();\n";
935 // now add the children
936 foreach ($children as $child) {
937 echo ' '.$subelement.".".$child." = new Object();\n";
938 echo ' '.$subelement.".".$child."._children = ".$child."_children;\n";
942 // now - flesh out the second level elements if there are any
943 if (scorm_version_check($sversion, SCORM_13)) {
944 $element = preg_replace('/(.*?\.N\d+\..*?)\.(\d+)\./', "\$1.N\$2.", $element);
945 preg_match('/.*?\.N\d+\.(.*?)\.(N\d+)\./', $element, $matches);
947 $element = preg_replace('/(.*?\_\d+\..*?)\.(\d+)\./', "\$1_\$2.", $element);
948 preg_match('/.*?\_\d+\.(.*?)\_(\d+)\./', $element, $matches);
951 // check the sub element type
952 if (count($matches) > 0 && $current_subelement != $matches[1]) {
953 if ($count_sub > 0) {
954 echo ' '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
956 $current_subelement = $matches[1];
959 $end = strpos($element, $matches[1])+strlen($matches[1]);
960 $subelement = substr($element, 0, $end);
961 echo ' '.$subelement." = new Object();\n";
964 // now check the subelement subscript
965 if (count($matches) > 0 && $current_sub != $matches[2]) {
966 $current_sub = $matches[2];
968 $end = strrpos($element, $matches[2])+strlen($matches[2]);
969 $subelement = substr($element, 0, $end);
970 echo ' '.$subelement." = new Object();\n";
973 echo ' '.$element.' = \''.$value."';\n";
975 if ($count_sub > 0) {
976 echo ' '.$element_name.$scormseperator.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
979 echo ' '.$element_name.'._count = '.$count.";\n";
984 * Build up the JavaScript representation of an array element
986 * @param string $a left array element
987 * @param string $b right array element
988 * @return comparator - 0,1,-1
990 function scorm_element_cmp($a, $b) {
991 preg_match('/.*?(\d+)\./', $a, $matches);
992 $left = intval($matches[1]);
993 preg_match('/.?(\d+)\./', $b, $matches);
994 $right = intval($matches[1]);
995 if ($left < $right) {
996 return -1; // smaller
997 } else if ($left > $right) {
1000 // look for a second level qualifier eg cmi.interactions_0.correct_responses_0.pattern
1001 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $a, $matches)) {
1002 $leftterm = intval($matches[2]);
1003 $left = intval($matches[3]);
1004 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $b, $matches)) {
1005 $rightterm = intval($matches[2]);
1006 $right = intval($matches[3]);
1007 if ($leftterm < $rightterm) {
1008 return -1; // smaller
1009 } else if ($leftterm > $rightterm) {
1012 if ($left < $right) {
1013 return -1; // smaller
1014 } else if ($left > $right) {
1020 // fall back for no second level matches or second level matches are equal
1021 return 0; // equal to
1026 * Generate the user attempt status string
1028 * @param object $user Current context user
1029 * @param object $scorm a moodle scrom object - mdl_scorm
1030 * @return string - Attempt status string
1032 function scorm_get_attempt_status($user, $scorm, $cm='') {
1033 global $DB, $PAGE, $OUTPUT;
1035 $attempts = scorm_get_attempt_count($user->id, $scorm, true);
1036 if (empty($attempts)) {
1039 $attemptcount = count($attempts);
1042 $result = '<p>'.get_string('noattemptsallowed', 'scorm').': ';
1043 if ($scorm->maxattempt > 0) {
1044 $result .= $scorm->maxattempt . '<br />';
1046 $result .= get_string('unlimited').'<br />';
1048 $result .= get_string('noattemptsmade', 'scorm').': ' . $attemptcount . '<br />';
1050 if ($scorm->maxattempt == 1) {
1051 switch ($scorm->grademethod) {
1053 $grademethod = get_string('gradehighest', 'scorm');
1056 $grademethod = get_string('gradeaverage', 'scorm');
1059 $grademethod = get_string('gradesum', 'scorm');
1062 $grademethod = get_string('gradescoes', 'scorm');
1066 switch ($scorm->whatgrade) {
1067 case HIGHESTATTEMPT:
1068 $grademethod = get_string('highestattempt', 'scorm');
1070 case AVERAGEATTEMPT:
1071 $grademethod = get_string('averageattempt', 'scorm');
1074 $grademethod = get_string('firstattempt', 'scorm');
1077 $grademethod = get_string('lastattempt', 'scorm');
1082 if (!empty($attempts)) {
1084 foreach ($attempts as $attempt) {
1085 $gradereported = scorm_grade_user_attempt($scorm, $user->id, $attempt->attemptnumber);
1086 if ($scorm->grademethod !== GRADESCOES && !empty($scorm->maxgrade)) {
1087 $gradereported = $gradereported/$scorm->maxgrade;
1088 $gradereported = number_format($gradereported*100, 0) .'%';
1090 $result .= get_string('gradeforattempt', 'scorm').' ' . $i . ': ' . $gradereported .'<br />';
1094 $calculatedgrade = scorm_grade_user($scorm, $user->id);
1095 if ($scorm->grademethod !== GRADESCOES && !empty($scorm->maxgrade)) {
1096 $calculatedgrade = $calculatedgrade/$scorm->maxgrade;
1097 $calculatedgrade = number_format($calculatedgrade*100, 0) .'%';
1099 $result .= get_string('grademethod', 'scorm'). ': ' . $grademethod;
1100 if (empty($attempts)) {
1101 $result .= '<br />' . get_string('gradereported', 'scorm') . ': ' . get_string('none') . '<br />';
1103 $result .= '<br />' . get_string('gradereported', 'scorm') . ': ' . $calculatedgrade . '<br />';
1106 if ($attemptcount >= $scorm->maxattempt and $scorm->maxattempt > 0) {
1107 $result .= '<p><font color="#cc0000">'.get_string('exceededmaxattempts', 'scorm').'</font></p>';
1110 $context = context_module::instance($cm->id);
1111 if (has_capability('mod/scorm:deleteownresponses', $context) &&
1112 $DB->record_exists('scorm_scoes_track', array('userid' => $user->id, 'scormid' => $scorm->id))) {
1113 //check to see if any data is stored for this user:
1114 $deleteurl = new moodle_url($PAGE->url, array('action'=>'delete', 'sesskey' => sesskey()));
1115 $result .= $OUTPUT->single_button($deleteurl, get_string('deleteallattempts', 'scorm'));
1124 * Get SCORM attempt count
1126 * @param object $user Current context user
1127 * @param object $scorm a moodle scrom object - mdl_scorm
1128 * @param bool $attempts return the list of attempts
1129 * @return int - no. of attempts so far
1131 function scorm_get_attempt_count($userid, $scorm, $attempts_only=false) {
1134 $element = 'cmi.core.score.raw';
1135 if ($scorm->grademethod == GRADESCOES) {
1136 $element = 'cmi.core.lesson_status';
1138 if (scorm_version_check($scorm->version, SCORM_13)) {
1139 $element = 'cmi.score.raw';
1141 $attempts = $DB->get_records_select('scorm_scoes_track', "element=? AND userid=? AND scormid=?", array($element, $userid, $scorm->id), 'attempt', 'DISTINCT attempt AS attemptnumber');
1142 if ($attempts_only) {
1145 if (!empty($attempts)) {
1146 $attemptcount = count($attempts);
1148 return $attemptcount;
1152 * Figure out with this is a debug situation
1154 * @param object $scorm a moodle scrom object - mdl_scorm
1155 * @return boolean - debugging true/false
1157 function scorm_debugging($scorm) {
1159 $cfg_scorm = get_config('scorm');
1161 if (!$cfg_scorm->allowapidebug) {
1164 $identifier = $USER->username.':'.$scorm->name;
1165 $test = $cfg_scorm->apidebugmask;
1166 // check the regex is only a short list of safe characters
1167 if (!preg_match('/^[\w\s\*\.\?\+\:\_\\\]+$/', $test)) {
1171 eval('$res = preg_match(\'/^'.$test.'/\', $identifier) ? true : false;');
1176 * Delete Scorm tracks for selected users
1178 * @param array $attemptids list of attempts that need to be deleted
1179 * @param int $scorm instance
1181 * return bool true deleted all responses, false failed deleting an attempt - stopped here
1183 function scorm_delete_responses($attemptids, $scorm) {
1184 if (!is_array($attemptids) || empty($attemptids)) {
1188 foreach ($attemptids as $num => $attemptid) {
1189 if (empty($attemptid)) {
1190 unset($attemptids[$num]);
1194 foreach ($attemptids as $attempt) {
1195 $keys = explode(':', $attempt);
1196 if (count($keys) == 2) {
1197 $userid = clean_param($keys[0], PARAM_INT);
1198 $attemptid = clean_param($keys[1], PARAM_INT);
1199 if (!$userid || !$attemptid || !scorm_delete_attempt($userid, $scorm, $attemptid)) {
1210 * Delete Scorm tracks for selected users
1212 * @param int $userid ID of User
1213 * @param int $scormid ID of Scorm
1214 * @param int $attemptid user attempt that need to be deleted
1216 * return bool true suceeded
1218 function scorm_delete_attempt($userid, $scorm, $attemptid) {
1221 $DB->delete_records('scorm_scoes_track', array('userid' => $userid, 'scormid' => $scorm->id, 'attempt' => $attemptid));
1222 include_once('lib.php');
1223 scorm_update_grades($scorm, $userid, true);
1228 * Converts SCORM duration notation to human-readable format
1229 * The function works with both SCORM 1.2 and SCORM 2004 time formats
1230 * @param $duration string SCORM duration
1231 * @return string human-readable date/time
1233 function scorm_format_duration($duration) {
1234 // fetch date/time strings
1235 $stryears = get_string('years');
1236 $strmonths = get_string('nummonths');
1237 $strdays = get_string('days');
1238 $strhours = get_string('hours');
1239 $strminutes = get_string('minutes');
1240 $strseconds = get_string('seconds');
1242 if ($duration[0] == 'P') {
1243 // if timestamp starts with 'P' - it's a SCORM 2004 format
1244 // this regexp discards empty sections, takes Month/Minute ambiguity into consideration,
1245 // and outputs filled sections, discarding leading zeroes and any format literals
1246 // also saves the only zero before seconds decimals (if there are any) and discards decimals if they are zero
1247 $pattern = array( '#([A-Z])0+Y#', '#([A-Z])0+M#', '#([A-Z])0+D#', '#P(|\d+Y)0*(\d+)M#', '#0*(\d+)Y#', '#0*(\d+)D#', '#P#',
1248 '#([A-Z])0+H#', '#([A-Z])[0.]+S#', '#\.0+S#', '#T(|\d+H)0*(\d+)M#', '#0*(\d+)H#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' );
1249 $replace = array( '$1', '$1', '$1', '$1$2 '.$strmonths.' ', '$1 '.$stryears.' ', '$1 '.$strdays.' ', '',
1250 '$1', '$1', 'S', '$1$2 '.$strminutes.' ', '$1 '.$strhours.' ', '0.$1 '.$strseconds, '$1 '.$strseconds, '');
1252 // else we have SCORM 1.2 format there
1253 // first convert the timestamp to some SCORM 2004-like format for conveniency
1254 $duration = preg_replace('#^(\d+):(\d+):([\d.]+)$#', 'T$1H$2M$3S', $duration);
1255 // then convert in the same way as SCORM 2004
1256 $pattern = array( '#T0+H#', '#([A-Z])0+M#', '#([A-Z])[0.]+S#', '#\.0+S#', '#0*(\d+)H#', '#0*(\d+)M#', '#0+\.(\d+)S#', '#0*([\d.]+)S#', '#T#' );
1257 $replace = array( 'T', '$1', '$1', 'S', '$1 '.$strhours.' ', '$1 '.$strminutes.' ', '0.$1 '.$strseconds, '$1 '.$strseconds, '' );
1260 $result = preg_replace($pattern, $replace, $duration);
1265 function scorm_get_toc($user,$scorm,$cmid,$toclink=TOCJSLINK,$currentorg='',$scoid='',$mode='normal',$attempt='',$play=false, $tocheader=false) {
1266 global $CFG, $DB, $PAGE, $OUTPUT;
1269 if ($mode == 'browse') {
1270 $modestr = '&mode='.$mode;
1273 $result = new stdClass();
1275 $result->toc = '<div id="scorm_layout">';
1276 $result->toc .= '<div id="scorm_toc">';
1277 $result->toc .= '<div id="scorm_tree">';
1279 $result->toc .= '<ul>';
1280 $tocmenus = array();
1281 $result->prerequisites = true;
1282 $incomplete = false;
1285 // Get the current organization infos
1287 if (!empty($currentorg)) {
1288 if (($organizationtitle = $DB->get_field('scorm_scoes','title', array('scorm'=>$scorm->id,'identifier'=>$currentorg))) != '') {
1290 $result->toctitle = "$organizationtitle";
1293 $result->toc .= "\t<li>$organizationtitle</li>\n";
1295 $tocmenus[] = $organizationtitle;
1300 // If not specified retrieve the last attempt number
1302 if (empty($attempt)) {
1303 $attempt = scorm_get_attempt_count($user->id, $scorm);
1305 $result->attemptleft = $scorm->maxattempt == 0 ? 1 : $scorm->maxattempt - $attempt;
1306 if ($scoes = scorm_get_scoes($scorm->id, $currentorg)){
1308 // Retrieve user tracking data for each learning object
1310 $usertracks = array();
1311 foreach ($scoes as $sco) {
1312 if (!empty($sco->launch)) {
1313 if ($usertrack = scorm_get_tracks($sco->id,$user->id,$attempt)) {
1314 if ($usertrack->status == '') {
1315 $usertrack->status = 'notattempted';
1317 $usertracks[$sco->identifier] = $usertrack;
1327 $parents[$level]='/';
1329 foreach ($scoes as $pos => $sco) {
1331 $sco->title = $sco->title;
1332 if (!isset($sco->isvisible) || (isset($sco->isvisible) && ($sco->isvisible == 'true'))) {
1335 if ($parents[$level] != $sco->parent) {
1336 if ($newlevel = array_search($sco->parent,$parents)) {
1337 for ($i=0; $i<($level-$newlevel); $i++) {
1338 $result->toc .= "\t\t</li></ul></li>\n";
1344 while (($i > 0) && ($parents[$level] != $sco->parent)) {
1345 if ($i === 1 && $level > 1) {
1346 $closelist .= "\t\t</ul></li>\n";
1348 $closelist .= "\t</li></ul></li>\n";
1352 if (($i == 0) && ($sco->parent != $currentorg)) {
1353 $result->toc .= "\n\t<ul>\n";
1356 $result->toc .= $closelist;
1359 $parents[$level] = $sco->parent;
1363 $result->toc .= "<li>";
1365 if (isset($scoes[$pos+1])) {
1366 $nextsco = $scoes[$pos+1];
1370 $nextisvisible = false;
1371 if (($nextsco !== false) && (!isset($nextsco->isvisible) || (isset($nextsco->isvisible) && ($nextsco->isvisible == 'true')))) {
1372 $nextisvisible = true;
1374 if ($nextisvisible && ($nextsco !== false) && ($sco->parent != $nextsco->parent) &&
1375 (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
1378 if (empty($sco->title)) {
1379 $sco->title = $sco->identifier;
1382 if (!empty($sco->launch)) {
1384 if (empty($scoid) && ($mode != 'normal')) {
1387 if (isset($usertracks[$sco->identifier])) {
1388 $usertrack = $usertracks[$sco->identifier];
1389 $strstatus = get_string($usertrack->status,'scorm');
1390 if ($sco->scormtype == 'sco') {
1391 $statusicon = '<img src="'.$OUTPUT->pix_url($usertrack->status, 'scorm').'" alt="'.$strstatus.'" title="'.$strstatus.'" />';
1393 $statusicon = '<img src="'.$OUTPUT->pix_url('assetc', 'scorm').'" alt="'.get_string('assetlaunched','scorm').'" title="'.get_string('assetlaunched','scorm').'" />';
1396 if (($usertrack->status == 'notattempted') || ($usertrack->status == 'incomplete') || ($usertrack->status == 'browsed')) {
1398 if ($play && empty($scoid)) {
1402 if ($usertrack->score_raw != '' && has_capability('mod/scorm:viewscores', get_context_instance(CONTEXT_MODULE,$cmid))) {
1403 $score = '('.get_string('score','scorm').': '.$usertrack->score_raw.')';
1405 $strsuspended = get_string('suspended','scorm');
1406 $exitvar = 'cmi.core.exit';
1407 if (scorm_version_check($scorm->version, SCORM_13)) {
1408 $exitvar = 'cmi.exit';
1410 if ($incomplete && isset($usertrack->{$exitvar}) && ($usertrack->{$exitvar} == 'suspend')) {
1411 $statusicon = '<img src="'.$OUTPUT->pix_url('suspend', 'scorm').'" alt="'.$strstatus.' - '.$strsuspended.'" title="'.$strstatus.' - '.$strsuspended.'" />';
1414 if ($play && empty($scoid)) {
1418 if ($sco->scormtype == 'sco') {
1419 $statusicon = '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
1421 $statusicon = '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
1424 if ($sco->id == $scoid) {
1428 if (($nextid == 0) && (scorm_count_launchable($scorm->id,$currentorg) > 1) && ($nextsco!==false) && (!$findnext)) {
1429 if (!empty($sco->launch)) {
1433 if (scorm_version_check($scorm->version, SCORM_13)) {
1434 require_once($CFG->dirroot.'/mod/scorm/datamodels/sequencinglib.php');
1435 $prereq = scorm_seq_evaluate($sco->id,$usertracks);
1437 //TODO: split check for sco->prerequisites only for AICC as I think that's the only case it's set.
1438 $prereq = empty($sco->prerequisites) || scorm_eval_prerequisites($sco->prerequisites,$usertracks);
1441 if ($sco->id == $scoid) {
1442 $result->prerequisites = true;
1444 if (!empty($prevsco) && scorm_version_check($scorm->version, SCORM_13) && !empty($prevsco->hidecontinue)) {
1445 $result->toc .= '<span>'.$statusicon.' '.format_string($sco->title).'</span>';
1446 } else if ($toclink == TOCFULLURL) { //display toc with urls for structure page
1447 $url = $CFG->wwwroot.'/mod/scorm/player.php?a='.$scorm->id.'&currentorg='.$currentorg.$modestr.'&scoid='.$sco->id;
1448 $result->toc .= $statusicon.' <a href="'.$url.'">'.format_string($sco->title).'</a>'.$score."\n";
1449 } else { //display toc for inside scorm player
1451 $link = 'a='.$scorm->id.'&scoid='.$sco->id.'¤torg='.$currentorg.$modestr.'&attempt='.$attempt;
1452 $result->toc .= '<a title="'.$link.'">'.$statusicon.' '.format_string($sco->title).' '.$score.'</a>';
1454 $result->toc .= '<span>'.$statusicon.' '.format_string($sco->title).'</span>';
1457 $tocmenus[$sco->id] = scorm_repeater('−',$level) . '>' . format_string($sco->title);
1459 if ($sco->id == $scoid) {
1460 $result->prerequisites = false;
1463 // should be disabled
1464 $result->toc .= '<span>'.$statusicon.' '.format_string($sco->title).'</span>';
1466 $result->toc .= $statusicon.' '.format_string($sco->title)."\n";
1470 $result->toc .= ' '.format_string($sco->title);
1472 if (($nextsco === false) || $nextsco->parent == $sco->parent) {
1473 $result->toc .= "</li>\n";
1476 if (($nextsco !== false) && ($nextid == 0) && ($findnext)) {
1477 if (!empty($nextsco->launch)) {
1478 $nextid = $nextsco->id;
1483 for ($i=0;$i<$level;$i++) {
1484 $result->toc .= "\t\t</ul></li>\n";
1488 // it is possible that $scoid is still not set, in this case we don't want an empty object
1490 $sco = scorm_get_sco($scoid);
1492 $sco->previd = $previd;
1493 $sco->nextid = $nextid;
1494 $result->sco = $sco;
1495 $result->incomplete = $incomplete;
1497 $result->incomplete = $incomplete;
1500 $result->toc .= '</ul>';
1504 $result->toc .= '</div></div></div>';
1505 $result->toc .= '<div id="scorm_navpanel"></div>';
1508 $url = new moodle_url('/mod/scorm/player.php?a='.$scorm->id.'¤torg='.$currentorg.$modestr);
1509 $result->tocmenu = $OUTPUT->single_select($url, 'scoid', $tocmenus, $sco->id, null, "tocmenu");