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/>.
19 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 /** SCORM_TYPE_LOCAL = local */
24 define('SCORM_TYPE_LOCAL', 'local');
25 /** SCORM_TYPE_LOCALSYNC = localsync */
26 define('SCORM_TYPE_LOCALSYNC', 'localsync');
27 /** SCORM_TYPE_EXTERNAL = external */
28 define('SCORM_TYPE_EXTERNAL', 'external');
29 /** SCORM_TYPE_IMSREPOSITORY = imsrepository */
30 define('SCORM_TYPE_IMSREPOSITORY', 'imsrepository');
31 /** SCORM_TYPE_AICCURL = external AICC url */
32 define('SCORM_TYPE_AICCURL', 'aiccurl');
34 define('SCORM_TOC_SIDE', 0);
35 define('SCORM_TOC_HIDDEN', 1);
36 define('SCORM_TOC_POPUP', 2);
37 define('SCORM_TOC_DISABLED', 3);
39 //used to check what SCORM version is being used.
40 define('SCORM_12', 1);
41 define('SCORM_13', 2);
42 define('SCORM_AICC', 3);
45 * Given an object containing all the necessary data,
46 * (defined by the form in mod_form.php) this function
47 * will create a new instance and return the id number
48 * of the new instance.
52 * @uses CONTEXT_MODULE
53 * @uses SCORM_TYPE_LOCAL
54 * @uses SCORM_TYPE_LOCALSYNC
55 * @uses SCORM_TYPE_EXTERNAL
56 * @uses SCORM_TYPE_IMSREPOSITORY
57 * @param object $scorm Form data
58 * @param object $mform
59 * @return int new instance id
61 function scorm_add_instance($scorm, $mform=null) {
64 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
66 if (empty($scorm->timeopen)) {
69 if (empty($scorm->timeclose)) {
70 $scorm->timeclose = 0;
72 $cmid = $scorm->coursemodule;
73 $cmidnumber = $scorm->cmidnumber;
74 $courseid = $scorm->course;
76 $context = get_context_instance(CONTEXT_MODULE, $cmid);
78 $scorm = scorm_option2text($scorm);
79 $scorm->width = (int)str_replace('%', '', $scorm->width);
80 $scorm->height = (int)str_replace('%', '', $scorm->height);
82 if (!isset($scorm->whatgrade)) {
83 $scorm->whatgrade = 0;
86 $id = $DB->insert_record('scorm', $scorm);
88 /// update course module record - from now on this instance properly exists and all function may be used
89 $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
91 /// reload scorm instance
92 $record = $DB->get_record('scorm', array('id'=>$id));
94 /// store the package and verify
95 if ($record->scormtype === SCORM_TYPE_LOCAL) {
97 $filename = $mform->get_new_filename('packagefile');
98 if ($filename !== false) {
99 $fs = get_file_storage();
100 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
101 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
102 $record->reference = $filename;
106 } else if ($record->scormtype === SCORM_TYPE_LOCALSYNC) {
107 $record->reference = $scorm->packageurl;
108 } else if ($record->scormtype === SCORM_TYPE_EXTERNAL) {
109 $record->reference = $scorm->packageurl;
110 } else if ($record->scormtype === SCORM_TYPE_IMSREPOSITORY) {
111 $record->reference = $scorm->packageurl;
112 } else if ($record->scormtype === SCORM_TYPE_AICCURL) {
113 $record->reference = $scorm->packageurl;
119 $DB->update_record('scorm', $record);
121 /// extra fields required in grade related functions
122 $record->course = $courseid;
123 $record->cmidnumber = $cmidnumber;
124 $record->cmid = $cmid;
126 scorm_parse($record, true);
128 scorm_grade_item_update($record);
134 * Given an object containing all the necessary data,
135 * (defined by the form in mod_form.php) this function
136 * will update an existing instance with new data.
140 * @uses CONTEXT_MODULE
141 * @uses SCORM_TYPE_LOCAL
142 * @uses SCORM_TYPE_LOCALSYNC
143 * @uses SCORM_TYPE_EXTERNAL
144 * @uses SCORM_TYPE_IMSREPOSITORY
145 * @param object $scorm Form data
146 * @param object $mform
149 function scorm_update_instance($scorm, $mform=null) {
152 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
154 if (empty($scorm->timeopen)) {
155 $scorm->timeopen = 0;
157 if (empty($scorm->timeclose)) {
158 $scorm->timeclose = 0;
161 $cmid = $scorm->coursemodule;
162 $cmidnumber = $scorm->cmidnumber;
163 $courseid = $scorm->course;
165 $scorm->id = $scorm->instance;
167 $context = get_context_instance(CONTEXT_MODULE, $cmid);
169 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
171 $filename = $mform->get_new_filename('packagefile');
172 if ($filename !== false) {
173 $scorm->reference = $filename;
174 $fs = get_file_storage();
175 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
176 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
180 } else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
181 $scorm->reference = $scorm->packageurl;
183 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
184 $scorm->reference = $scorm->packageurl;
186 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
187 $scorm->reference = $scorm->packageurl;
188 } else if ($scorm->scormtype === SCORM_TYPE_AICCURL) {
189 $scorm->reference = $scorm->packageurl;
194 $scorm = scorm_option2text($scorm);
195 $scorm->width = (int)str_replace('%', '', $scorm->width);
196 $scorm->height = (int)str_replace('%', '', $scorm->height);
197 $scorm->timemodified = time();
199 if (!isset($scorm->whatgrade)) {
200 $scorm->whatgrade = 0;
203 $DB->update_record('scorm', $scorm);
205 $scorm = $DB->get_record('scorm', array('id'=>$scorm->id));
207 /// extra fields required in grade related functions
208 $scorm->course = $courseid;
209 $scorm->idnumber = $cmidnumber;
210 $scorm->cmid = $cmid;
212 scorm_parse($scorm, (bool)$scorm->updatefreq);
214 scorm_grade_item_update($scorm);
215 scorm_update_grades($scorm);
221 * Given an ID of an instance of this module,
222 * this function will permanently delete the instance
223 * and any data that depends on it.
227 * @param int $id Scorm instance id
230 function scorm_delete_instance($id) {
233 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
239 // Delete any dependent records
240 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
243 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
244 foreach ($scoes as $sco) {
245 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
249 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
253 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
257 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
260 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
263 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
266 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
269 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
272 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
275 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
279 scorm_grade_item_delete($scorm);
285 * Return a small object with summary information about what a
286 * user has done with a given particular instance of this module
287 * Used for user activity reports.
290 * @param int $course Course id
291 * @param int $user User id
293 * @param int $scorm The scorm id
296 function scorm_user_outline($course, $user, $mod, $scorm) {
298 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
300 require_once("$CFG->libdir/gradelib.php");
301 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
302 if (!empty($grades->items[0]->grades)) {
303 $grade = reset($grades->items[0]->grades);
304 $result = new stdClass();
305 $result->info = get_string('grade') . ': '. $grade->str_long_grade;
307 //datesubmitted == time created. dategraded == time modified or time overridden
308 //if grade was last modified by the user themselves use date graded. Otherwise use date submitted
309 //TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
310 if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
311 $result->time = $grade->dategraded;
313 $result->time = $grade->datesubmitted;
322 * Print a detailed representation of what a user has done with
323 * a given particular instance of this module, for user activity reports.
327 * @param object $course
328 * @param object $user
330 * @param object $scorm
333 function scorm_user_complete($course, $user, $mod, $scorm) {
334 global $CFG, $DB, $OUTPUT;
335 require_once("$CFG->libdir/gradelib.php");
337 $liststyle = 'structlist';
341 $sometoreport = false;
344 // First Access and Last Access dates for SCOs
345 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
346 $timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
347 $firstmodify = $timetracks->start;
348 $lastmodify = $timetracks->finish;
350 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
351 if (!empty($grades->items[0]->grades)) {
352 $grade = reset($grades->items[0]->grades);
353 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
354 if ($grade->str_feedback) {
355 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
359 if ($orgs = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
360 $DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
361 $DB->sql_isempty('scorm_scoes', 'organization', false, false),
362 array($scorm->id), 'id', 'id,identifier,title')) {
363 if (count($orgs) <= 1) {
365 $orgs[]->identifier = '';
367 $report .= '<div class="mod-scorm">'."\n";
368 foreach ($orgs as $org) {
369 $conditions = array();
371 if (!empty($org->identifier)) {
372 $report .= '<div class="orgtitle">'.$org->title.'</div>';
373 $currentorg = $org->identifier;
374 $conditions['organization'] = $currentorg;
376 $report .= "<ul id='0' class='$liststyle'>";
377 $conditions['scorm'] = $scorm->id;
378 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")) {
379 // drop keys so that we can access array sequentially
380 $scoes = array_values($scoes);
383 $parents[$level]='/';
384 foreach ($scoes as $pos => $sco) {
385 if ($parents[$level]!=$sco->parent) {
386 if ($level>0 && $parents[$level-1]==$sco->parent) {
387 $report .= "\t\t</ul></li>\n";
392 while (($i > 0) && ($parents[$level] != $sco->parent)) {
393 $closelist .= "\t\t</ul></li>\n";
396 if (($i == 0) && ($sco->parent != $currentorg)) {
397 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
400 $report .= $closelist;
403 $parents[$level]=$sco->parent;
406 $report .= "\t\t<li>";
407 if (isset($scoes[$pos+1])) {
408 $nextsco = $scoes[$pos+1];
412 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
415 $report .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
421 if ($usertrack=scorm_get_tracks($sco->id, $user->id)) {
422 if ($usertrack->status == '') {
423 $usertrack->status = 'notattempted';
425 $strstatus = get_string($usertrack->status, 'scorm');
426 $report .= "<img src='".$OUTPUT->pix_url($usertrack->status, 'scorm')."' alt='$strstatus' title='$strstatus' />";
428 if ($sco->scormtype == 'sco') {
429 $report .= '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted', 'scorm').'" title="'.get_string('notattempted', 'scorm').'" />';
431 $report .= '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset', 'scorm').'" title="'.get_string('asset', 'scorm').'" />';
434 $report .= " $sco->title $score$totaltime</li>\n";
435 if ($usertrack !== false) {
436 $sometoreport = true;
437 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
438 foreach ($usertrack as $element => $value) {
439 if (substr($element, 0, 3) == 'cmi') {
440 $report .= '<li>'.$element.' => '.s($value).'</li>';
443 $report .= "\t\t\t</ul></li>\n";
446 $report .= " $sco->title</li>\n";
449 for ($i=0; $i<$level; $i++) {
450 $report .= "\t\t</ul></li>\n";
453 $report .= "\t</ul><br />\n";
455 $report .= "</div>\n";
458 if ($firstmodify < $now) {
459 $timeago = format_time($now - $firstmodify);
460 echo get_string('firstaccess', 'scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
462 if ($lastmodify > 0) {
463 $timeago = format_time($now - $lastmodify);
464 echo get_string('lastaccess', 'scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
466 echo get_string('report', 'scorm').":<br />\n";
469 print_string('noactivity', 'scorm');
476 * Function to be run periodically according to the moodle cron
477 * This function searches for things that need to be done, such
478 * as sending out mail, toggling flags etc ...
484 function scorm_cron () {
487 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
489 $sitetimezone = $CFG->timezone;
490 /// Now see if there are any scorm updates to be done
492 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
493 set_config('scorm_updatetimelast', 0);
497 $updatetime = usergetmidnight($timenow, $sitetimezone);
499 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
501 set_config('scorm_updatetimelast', $timenow);
503 mtrace('Updating scorm packages which require daily update');//We are updating
505 $scormsupdate = $DB->get_records_select('scorm', 'updatefreq = ? AND scormtype <> ?', array(SCORM_UPDATE_EVERYDAY, SCORM_TYPE_LOCAL));
506 foreach ($scormsupdate as $scormupdate) {
507 scorm_parse($scormupdate, true);
510 //now clear out AICC session table with old session data
511 $cfg_scorm = get_config('scorm');
512 if (!empty($cfg_scorm->allowaicchacp)) {
513 $expiretime = time() - ($cfg_scorm->aicchacpkeepsessiondata*24*60*60);
514 $DB->delete_records_select('scorm_aicc_session', 'timemodified < ?', array($expiretime));
522 * Return grade for given user or all users.
526 * @param int $scormid id of scorm
527 * @param int $userid optional user id, 0 means all users
528 * @return array array of grades, false if none
530 function scorm_get_user_grades($scorm, $userid=0) {
532 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
535 if (empty($userid)) {
536 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
537 foreach ($scousers as $scouser) {
538 $grades[$scouser->userid] = new stdClass();
539 $grades[$scouser->userid]->id = $scouser->userid;
540 $grades[$scouser->userid]->userid = $scouser->userid;
541 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
548 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
549 return false; //no attempt yet
551 $grades[$userid] = new stdClass();
552 $grades[$userid]->id = $userid;
553 $grades[$userid]->userid = $userid;
554 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
561 * Update grades in central gradebook
564 * @param object $scorm
565 * @param int $userid specific user only, 0 mean all
566 * @param bool $nullifnone
568 function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
570 require_once($CFG->libdir.'/gradelib.php');
572 if ($grades = scorm_get_user_grades($scorm, $userid)) {
573 scorm_grade_item_update($scorm, $grades);
575 } else if ($userid and $nullifnone) {
576 $grade = new stdClass();
577 $grade->userid = $userid;
578 $grade->rawgrade = null;
579 scorm_grade_item_update($scorm, $grade);
582 scorm_grade_item_update($scorm);
587 * Update all grades in gradebook.
591 function scorm_upgrade_grades() {
594 $sql = "SELECT COUNT('x')
595 FROM {scorm} s, {course_modules} cm, {modules} m
596 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
597 $count = $DB->count_records_sql($sql);
599 $sql = "SELECT s.*, cm.idnumber AS cmidnumber, s.course AS courseid
600 FROM {scorm} s, {course_modules} cm, {modules} m
601 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
602 $rs = $DB->get_recordset_sql($sql);
604 $pbar = new progress_bar('scormupgradegrades', 500, true);
606 foreach ($rs as $scorm) {
608 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
609 scorm_update_grades($scorm, 0, false);
610 $pbar->update($i, $count, "Updating Scorm grades ($i/$count).");
617 * Update/create grade item for given scorm
620 * @uses GRADE_TYPE_VALUE
621 * @uses GRADE_TYPE_NONE
622 * @param object $scorm object with extra cmidnumber
623 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
624 * @return object grade_item
626 function scorm_grade_item_update($scorm, $grades=null) {
628 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
629 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
630 require_once($CFG->libdir.'/gradelib.php');
633 $params = array('itemname'=>$scorm->name);
634 if (isset($scorm->cmidnumber)) {
635 $params['idnumber'] = $scorm->cmidnumber;
638 if ($scorm->grademethod == GRADESCOES) {
639 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id))) {
640 $params['gradetype'] = GRADE_TYPE_VALUE;
641 $params['grademax'] = $maxgrade;
642 $params['grademin'] = 0;
644 $params['gradetype'] = GRADE_TYPE_NONE;
647 $params['gradetype'] = GRADE_TYPE_VALUE;
648 $params['grademax'] = $scorm->maxgrade;
649 $params['grademin'] = 0;
652 if ($grades === 'reset') {
653 $params['reset'] = true;
657 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
661 * Delete grade item for given scorm
664 * @param object $scorm object
665 * @return object grade_item
667 function scorm_grade_item_delete($scorm) {
669 require_once($CFG->libdir.'/gradelib.php');
671 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, null, array('deleted'=>1));
677 function scorm_get_view_actions() {
678 return array('pre-view', 'view', 'view all', 'report');
684 function scorm_get_post_actions() {
689 * @param object $scorm
690 * @return object $scorm
692 function scorm_option2text($scorm) {
693 $scorm_popoup_options = scorm_get_popup_options_array();
695 if (isset($scorm->popup)) {
696 if ($scorm->popup == 1) {
697 $optionlist = array();
698 foreach ($scorm_popoup_options as $name => $option) {
699 if (isset($scorm->$name)) {
700 $optionlist[] = $name.'='.$scorm->$name;
702 $optionlist[] = $name.'=0';
705 $scorm->options = implode(',', $optionlist);
707 $scorm->options = '';
711 $scorm->options = '';
717 * Implementation of the function for printing the form elements that control
718 * whether the course reset functionality affects the scorm.
720 * @param object $mform form passed by reference
722 function scorm_reset_course_form_definition(&$mform) {
723 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
724 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts', 'scorm'));
728 * Course reset form defaults.
732 function scorm_reset_course_form_defaults($course) {
733 return array('reset_scorm'=>1);
737 * Removes all grades from gradebook
741 * @param int $courseid
742 * @param string optional type
744 function scorm_reset_gradebook($courseid, $type='') {
747 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
748 FROM {scorm} s, {course_modules} cm, {modules} m
749 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
751 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
752 foreach ($scorms as $scorm) {
753 scorm_grade_item_update($scorm, 'reset');
759 * Actual implementation of the reset course functionality, delete all the
760 * scorm attempts for course $data->courseid.
764 * @param object $data the data submitted from the reset course.
765 * @return array status array
767 function scorm_reset_userdata($data) {
770 $componentstr = get_string('modulenameplural', 'scorm');
773 if (!empty($data->reset_scorm)) {
774 $scormssql = "SELECT s.id
778 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
780 // remove all grades from gradebook
781 if (empty($data->reset_gradebook_grades)) {
782 scorm_reset_gradebook($data->courseid);
785 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
788 // no dates to shift here
794 * Returns all other caps used in module
798 function scorm_get_extra_capabilities() {
799 return array('moodle/site:accessallgroups');
803 * Lists all file areas current user may browse
805 * @param object $course
807 * @param object $context
810 function scorm_get_file_areas($course, $cm, $context) {
812 $areas['content'] = get_string('areacontent', 'scorm');
813 $areas['package'] = get_string('areapackage', 'scorm');
818 * File browsing support for SCORM file areas
822 * @param file_browser $browser file browser instance
823 * @param array $areas file areas
824 * @param stdClass $course course object
825 * @param stdClass $cm course module object
826 * @param stdClass $context context object
827 * @param string $filearea file area
828 * @param int $itemid item ID
829 * @param string $filepath file path
830 * @param string $filename file name
831 * @return file_info instance or null if not found
833 function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
836 if (!has_capability('moodle/course:managefiles', $context)) {
840 // no writing for now!
842 $fs = get_file_storage();
844 if ($filearea === 'content') {
846 $filepath = is_null($filepath) ? '/' : $filepath;
847 $filename = is_null($filename) ? '.' : $filename;
849 $urlbase = $CFG->wwwroot.'/pluginfile.php';
850 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
851 if ($filepath === '/' and $filename === '.') {
852 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
858 require_once("$CFG->dirroot/mod/scorm/locallib.php");
859 return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
861 } else if ($filearea === 'package') {
862 $filepath = is_null($filepath) ? '/' : $filepath;
863 $filename = is_null($filename) ? '.' : $filename;
865 $urlbase = $CFG->wwwroot.'/pluginfile.php';
866 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, $filepath, $filename)) {
867 if ($filepath === '/' and $filename === '.') {
868 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
874 return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
877 // scorm_intro handled in file_browser
883 * Serves scorm content, introduction images and packages. Implements needed access control ;-)
887 * @param stdClass $course course object
888 * @param stdClass $cm course module object
889 * @param stdClass $context context object
890 * @param string $filearea file area
891 * @param array $args extra arguments
892 * @param bool $forcedownload whether or not force download
893 * @return bool false if file not found, does not return if found - just send the file
895 function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
898 if ($context->contextlevel != CONTEXT_MODULE) {
902 require_login($course, true, $cm);
904 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
906 if ($filearea === 'content') {
907 $revision = (int)array_shift($args); // prevents caching problems - ignored here
908 $relativepath = implode('/', $args);
909 $fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
910 // TODO: add any other access restrictions here if needed!
912 } else if ($filearea === 'package') {
913 if (!has_capability('moodle/course:manageactivities', $context)) {
916 $relativepath = implode('/', $args);
917 $fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
918 $lifetime = 0; // no caching here
924 $fs = get_file_storage();
925 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
926 if ($filearea === 'content') { //return file not found straight away to improve performance.
933 // finally send the file
934 send_stored_file($file, $lifetime, 0, false);
938 * @uses FEATURE_GROUPS
939 * @uses FEATURE_GROUPINGS
940 * @uses FEATURE_GROUPMEMBERSONLY
941 * @uses FEATURE_MOD_INTRO
942 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
943 * @uses FEATURE_GRADE_HAS_GRADE
944 * @uses FEATURE_GRADE_OUTCOMES
945 * @param string $feature FEATURE_xx constant for requested feature
946 * @return mixed True if module supports feature, false if not, null if doesn't know
948 function scorm_supports($feature) {
950 case FEATURE_GROUPS: return false;
951 case FEATURE_GROUPINGS: return false;
952 case FEATURE_GROUPMEMBERSONLY: return true;
953 case FEATURE_MOD_INTRO: return true;
954 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
955 case FEATURE_GRADE_HAS_GRADE: return true;
956 case FEATURE_GRADE_OUTCOMES: return true;
957 case FEATURE_BACKUP_MOODLE2: return true;
958 case FEATURE_SHOW_DESCRIPTION: return true;
960 default: return null;
965 * This function extends the global navigation for the site.
966 * It is important to note that you should not rely on PAGE objects within this
967 * body of code as there is no guarantee that during an AJAX request they are
970 * @param navigation_node $navigation The scorm node within the global navigation
971 * @param stdClass $course The course object returned from the DB
972 * @param stdClass $module The module object returned from the DB
973 * @param stdClass $cm The course module instance returned from the DB
975 function scorm_extend_navigation($navigation, $course, $module, $cm) {
977 * This is currently just a stub so that it can be easily expanded upon.
978 * When expanding just remove this comment and the line below and then add
981 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
985 * Get the filename for a temp log file
987 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
988 * @param integer $scoid - scoid of object this log entry is for
989 * @return string The filename as an absolute path
991 function scorm_debug_log_filename($type, $scoid) {
994 $logpath = $CFG->tempdir.'/scormlogs';
995 $logfile = $logpath.'/'.$type.'debug_'.$USER->id.'_'.$scoid.'.log';
1000 * writes log output to a temp log file
1002 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1003 * @param string $text - text to be written to file.
1004 * @param integer $scoid - scoid of object this log entry is for.
1006 function scorm_debug_log_write($type, $text, $scoid) {
1008 $debugenablelog = get_config('scorm', 'allowapidebug');
1009 if (!$debugenablelog || empty($text)) {
1012 if (make_temp_directory('scormlogs/')) {
1013 $logfile = scorm_debug_log_filename($type, $scoid);
1014 @file_put_contents($logfile, date('Y/m/d H:i:s O')." DEBUG $text\r\n", FILE_APPEND);
1019 * Remove debug log file
1021 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1022 * @param integer $scoid - scoid of object this log entry is for
1023 * @return boolean True if the file is successfully deleted, false otherwise
1025 function scorm_debug_log_remove($type, $scoid) {
1027 $debugenablelog = get_config('scorm', 'allowapidebug');
1028 $logfile = scorm_debug_log_filename($type, $scoid);
1029 if (!$debugenablelog || !file_exists($logfile)) {
1033 return @unlink($logfile);
1037 * writes overview info for course_overview block - displays upcoming scorm objects that have a due date
1039 * @param object $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1040 * @param array $htmlarray
1043 function scorm_print_overview($courses, &$htmlarray) {
1044 global $USER, $CFG, $DB;
1046 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1050 if (!$scorms = get_all_instances_in_courses('scorm', $courses)) {
1054 $scormids = array();
1056 // Do scorm::isopen() here without loading the whole thing for speed
1057 foreach ($scorms as $key => $scorm) {
1059 if ($scorm->timeopen) {
1060 $isopen = ($scorm->timeopen <= $time && $time <= $scorm->timeclose);
1062 if (empty($scorm->displayattemptstatus) && (empty($isopen) || empty($scorm->timeclose))) {
1063 unset($scorms[$key]);
1065 $scormids[] = $scorm->id;
1069 if (empty($scormids)) {
1070 // no scorms to look at - we're done
1073 $strscorm = get_string('modulename', 'scorm');
1074 $strduedate = get_string('duedate', 'scorm');
1076 foreach ($scorms as $scorm) {
1077 $str = '<div class="scorm overview"><div class="name">'.$strscorm. ': '.
1078 '<a '.($scorm->visible ? '':' class="dimmed"').
1079 'title="'.$strscorm.'" href="'.$CFG->wwwroot.
1080 '/mod/scorm/view.php?id='.$scorm->coursemodule.'">'.
1081 $scorm->name.'</a></div>';
1082 if ($scorm->timeclose) {
1083 $str .= '<div class="info">'.$strduedate.': '.userdate($scorm->timeclose).'</div>';
1085 if ($scorm->displayattemptstatus == 1) {
1086 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
1087 $str .= '<div class="details">'.scorm_get_attempt_status($USER, $scorm).'</div>';
1090 if (empty($htmlarray[$scorm->course]['scorm'])) {
1091 $htmlarray[$scorm->course]['scorm'] = $str;
1093 $htmlarray[$scorm->course]['scorm'] .= $str;
1099 * Return a list of page types
1100 * @param string $pagetype current page type
1101 * @param stdClass $parentcontext Block's parent context
1102 * @param stdClass $currentcontext Current context of block
1104 function scorm_page_type_list($pagetype, $parentcontext, $currentcontext) {
1105 $module_pagetype = array('mod-scorm-*'=>get_string('page-mod-scorm-x', 'scorm'));
1106 return $module_pagetype;
1110 * Returns the SCORM version used.
1111 * @param string $scormversion comes from $scorm->version
1112 * @param string $version one of the defined vars SCORM_12, SCORM_13, SCORM_AICC (or empty)
1113 * @return Scorm version.
1115 function scorm_version_check($scormversion, $version='') {
1116 $scormversion = trim(strtolower($scormversion));
1117 if (empty($version) || $version==SCORM_12) {
1118 if ($scormversion == 'scorm_12' || $scormversion == 'scorm_1.2') {
1121 if (!empty($version)) {
1125 if (empty($version) || $version == SCORM_13) {
1126 if ($scormversion == 'scorm_13' || $scormversion == 'scorm_1.3') {
1129 if (!empty($version)) {
1133 if (empty($version) || $version == SCORM_AICC) {
1134 if (strpos($scormversion, 'aicc')) {
1137 if (!empty($version)) {