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');
32 define('SCORM_TOC_SIDE', 0);
33 define('SCORM_TOC_HIDDEN', 1);
34 define('SCORM_TOC_POPUP', 2);
35 define('SCORM_TOC_DISABLED', 3);
37 //used to check what SCORM version is being used.
38 define('SCORM_12', 1);
39 define('SCORM_13', 2);
40 define('SCORM_AICC', 3);
43 * Given an object containing all the necessary data,
44 * (defined by the form in mod_form.php) this function
45 * will create a new instance and return the id number
46 * of the new instance.
50 * @uses CONTEXT_MODULE
51 * @uses SCORM_TYPE_LOCAL
52 * @uses SCORM_TYPE_LOCALSYNC
53 * @uses SCORM_TYPE_EXTERNAL
54 * @uses SCORM_TYPE_IMSREPOSITORY
55 * @param object $scorm Form data
56 * @param object $mform
57 * @return int new instance id
59 function scorm_add_instance($scorm, $mform=null) {
62 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
64 if (empty($scorm->timeopen)) {
67 if (empty($scorm->timeclose)) {
68 $scorm->timeclose = 0;
70 $cmid = $scorm->coursemodule;
71 $cmidnumber = $scorm->cmidnumber;
72 $courseid = $scorm->course;
74 $context = get_context_instance(CONTEXT_MODULE, $cmid);
76 $scorm = scorm_option2text($scorm);
77 $scorm->width = (int)str_replace('%', '', $scorm->width);
78 $scorm->height = (int)str_replace('%', '', $scorm->height);
80 if (!isset($scorm->whatgrade)) {
81 $scorm->whatgrade = 0;
84 $id = $DB->insert_record('scorm', $scorm);
86 /// update course module record - from now on this instance properly exists and all function may be used
87 $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
89 /// reload scorm instance
90 $record = $DB->get_record('scorm', array('id'=>$id));
92 /// store the package and verify
93 if ($record->scormtype === SCORM_TYPE_LOCAL) {
95 $filename = $mform->get_new_filename('packagefile');
96 if ($filename !== false) {
97 $fs = get_file_storage();
98 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
99 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
100 $record->reference = $filename;
104 } else if ($record->scormtype === SCORM_TYPE_LOCALSYNC) {
105 $record->reference = $scorm->packageurl;
107 } else if ($record->scormtype === SCORM_TYPE_EXTERNAL) {
108 $record->reference = $scorm->packageurl;
110 } else if ($record->scormtype === SCORM_TYPE_IMSREPOSITORY) {
111 $record->reference = $scorm->packageurl;
118 $DB->update_record('scorm', $record);
120 /// extra fields required in grade related functions
121 $record->course = $courseid;
122 $record->cmidnumber = $cmidnumber;
123 $record->cmid = $cmid;
125 scorm_parse($record, true);
127 scorm_grade_item_update($record);
133 * Given an object containing all the necessary data,
134 * (defined by the form in mod_form.php) this function
135 * will update an existing instance with new data.
139 * @uses CONTEXT_MODULE
140 * @uses SCORM_TYPE_LOCAL
141 * @uses SCORM_TYPE_LOCALSYNC
142 * @uses SCORM_TYPE_EXTERNAL
143 * @uses SCORM_TYPE_IMSREPOSITORY
144 * @param object $scorm Form data
145 * @param object $mform
148 function scorm_update_instance($scorm, $mform=null) {
151 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
153 if (empty($scorm->timeopen)) {
154 $scorm->timeopen = 0;
156 if (empty($scorm->timeclose)) {
157 $scorm->timeclose = 0;
160 $cmid = $scorm->coursemodule;
161 $cmidnumber = $scorm->cmidnumber;
162 $courseid = $scorm->course;
164 $scorm->id = $scorm->instance;
166 $context = get_context_instance(CONTEXT_MODULE, $cmid);
168 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
170 $filename = $mform->get_new_filename('packagefile');
171 if ($filename !== false) {
172 $scorm->reference = $filename;
173 $fs = get_file_storage();
174 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
175 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
179 } else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
180 $scorm->reference = $scorm->packageurl;
182 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
183 $scorm->reference = $scorm->packageurl;
185 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
186 $scorm->reference = $scorm->packageurl;
192 $scorm = scorm_option2text($scorm);
193 $scorm->width = (int)str_replace('%', '', $scorm->width);
194 $scorm->height = (int)str_replace('%', '', $scorm->height);
195 $scorm->timemodified = time();
197 if (!isset($scorm->whatgrade)) {
198 $scorm->whatgrade = 0;
201 $DB->update_record('scorm', $scorm);
203 $scorm = $DB->get_record('scorm', array('id'=>$scorm->id));
205 /// extra fields required in grade related functions
206 $scorm->course = $courseid;
207 $scorm->idnumber = $cmidnumber;
208 $scorm->cmid = $cmid;
210 scorm_parse($scorm, (bool)$scorm->updatefreq);
212 scorm_grade_item_update($scorm);
213 scorm_update_grades($scorm);
219 * Given an ID of an instance of this module,
220 * this function will permanently delete the instance
221 * and any data that depends on it.
225 * @param int $id Scorm instance id
228 function scorm_delete_instance($id) {
231 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
237 // Delete any dependent records
238 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
241 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
242 foreach ($scoes as $sco) {
243 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
247 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
251 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
255 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
258 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
261 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
264 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
267 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
270 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
273 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
277 scorm_grade_item_delete($scorm);
283 * Return a small object with summary information about what a
284 * user has done with a given particular instance of this module
285 * Used for user activity reports.
288 * @param int $course Course id
289 * @param int $user User id
291 * @param int $scorm The scorm id
294 function scorm_user_outline($course, $user, $mod, $scorm) {
296 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
298 require_once("$CFG->libdir/gradelib.php");
299 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
300 if (!empty($grades->items[0]->grades)) {
301 $grade = reset($grades->items[0]->grades);
302 $result = new stdClass();
303 $result->info = get_string('grade') . ': '. $grade->str_long_grade;
305 //datesubmitted == time created. dategraded == time modified or time overridden
306 //if grade was last modified by the user themselves use date graded. Otherwise use date submitted
307 //TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
308 if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
309 $result->time = $grade->dategraded;
311 $result->time = $grade->datesubmitted;
320 * Print a detailed representation of what a user has done with
321 * a given particular instance of this module, for user activity reports.
325 * @param object $course
326 * @param object $user
328 * @param object $scorm
331 function scorm_user_complete($course, $user, $mod, $scorm) {
332 global $CFG, $DB, $OUTPUT;
333 require_once("$CFG->libdir/gradelib.php");
335 $liststyle = 'structlist';
339 $sometoreport = false;
342 // First Access and Last Access dates for SCOs
343 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
344 $timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
345 $firstmodify = $timetracks->start;
346 $lastmodify = $timetracks->finish;
348 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
349 if (!empty($grades->items[0]->grades)) {
350 $grade = reset($grades->items[0]->grades);
351 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
352 if ($grade->str_feedback) {
353 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
357 if ($orgs = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
358 $DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
359 $DB->sql_isempty('scorm_scoes', 'organization', false, false),
360 array($scorm->id), 'id', 'id,identifier,title')) {
361 if (count($orgs) <= 1) {
363 $orgs[]->identifier = '';
365 $report .= '<div class="mod-scorm">'."\n";
366 foreach ($orgs as $org) {
367 $conditions = array();
369 if (!empty($org->identifier)) {
370 $report .= '<div class="orgtitle">'.$org->title.'</div>';
371 $currentorg = $org->identifier;
372 $conditions['organization'] = $currentorg;
374 $report .= "<ul id='0' class='$liststyle'>";
375 $conditions['scorm'] = $scorm->id;
376 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")) {
377 // drop keys so that we can access array sequentially
378 $scoes = array_values($scoes);
381 $parents[$level]='/';
382 foreach ($scoes as $pos => $sco) {
383 if ($parents[$level]!=$sco->parent) {
384 if ($level>0 && $parents[$level-1]==$sco->parent) {
385 $report .= "\t\t</ul></li>\n";
390 while (($i > 0) && ($parents[$level] != $sco->parent)) {
391 $closelist .= "\t\t</ul></li>\n";
394 if (($i == 0) && ($sco->parent != $currentorg)) {
395 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
398 $report .= $closelist;
401 $parents[$level]=$sco->parent;
404 $report .= "\t\t<li>";
405 if (isset($scoes[$pos+1])) {
406 $nextsco = $scoes[$pos+1];
410 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
413 $report .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
419 if ($usertrack=scorm_get_tracks($sco->id, $user->id)) {
420 if ($usertrack->status == '') {
421 $usertrack->status = 'notattempted';
423 $strstatus = get_string($usertrack->status, 'scorm');
424 $report .= "<img src='".$OUTPUT->pix_url($usertrack->status, 'scorm')."' alt='$strstatus' title='$strstatus' />";
426 if ($sco->scormtype == 'sco') {
427 $report .= '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted', 'scorm').'" title="'.get_string('notattempted', 'scorm').'" />';
429 $report .= '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset', 'scorm').'" title="'.get_string('asset', 'scorm').'" />';
432 $report .= " $sco->title $score$totaltime</li>\n";
433 if ($usertrack !== false) {
434 $sometoreport = true;
435 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
436 foreach ($usertrack as $element => $value) {
437 if (substr($element, 0, 3) == 'cmi') {
438 $report .= '<li>'.$element.' => '.s($value).'</li>';
441 $report .= "\t\t\t</ul></li>\n";
444 $report .= " $sco->title</li>\n";
447 for ($i=0; $i<$level; $i++) {
448 $report .= "\t\t</ul></li>\n";
451 $report .= "\t</ul><br />\n";
453 $report .= "</div>\n";
456 if ($firstmodify < $now) {
457 $timeago = format_time($now - $firstmodify);
458 echo get_string('firstaccess', 'scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
460 if ($lastmodify > 0) {
461 $timeago = format_time($now - $lastmodify);
462 echo get_string('lastaccess', 'scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
464 echo get_string('report', 'scorm').":<br />\n";
467 print_string('noactivity', 'scorm');
474 * Function to be run periodically according to the moodle cron
475 * This function searches for things that need to be done, such
476 * as sending out mail, toggling flags etc ...
482 function scorm_cron () {
485 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
487 $sitetimezone = $CFG->timezone;
488 /// Now see if there are any scorm updates to be done
490 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
491 set_config('scorm_updatetimelast', 0);
495 $updatetime = usergetmidnight($timenow, $sitetimezone);
497 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
499 set_config('scorm_updatetimelast', $timenow);
501 mtrace('Updating scorm packages which require daily update');//We are updating
503 $scormsupdate = $DB->get_records('scorm', array('updatefreq'=>UPDATE_EVERYDAY));
504 foreach ($scormsupdate as $scormupdate) {
505 scorm_parse($scormupdate, true);
513 * Return grade for given user or all users.
517 * @param int $scormid id of scorm
518 * @param int $userid optional user id, 0 means all users
519 * @return array array of grades, false if none
521 function scorm_get_user_grades($scorm, $userid=0) {
523 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
526 if (empty($userid)) {
527 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
528 foreach ($scousers as $scouser) {
529 $grades[$scouser->userid] = new stdClass();
530 $grades[$scouser->userid]->id = $scouser->userid;
531 $grades[$scouser->userid]->userid = $scouser->userid;
532 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
539 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
540 return false; //no attempt yet
542 $grades[$userid] = new stdClass();
543 $grades[$userid]->id = $userid;
544 $grades[$userid]->userid = $userid;
545 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
552 * Update grades in central gradebook
556 * @param object $scorm
557 * @param int $userid specific user only, 0 mean all
558 * @param bool $nullifnone
560 function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
562 require_once($CFG->libdir.'/gradelib.php');
564 if ($grades = scorm_get_user_grades($scorm, $userid)) {
565 scorm_grade_item_update($scorm, $grades);
567 } else if ($userid and $nullifnone) {
568 $grade = new stdClass();
569 $grade->userid = $userid;
570 $grade->rawgrade = null;
571 scorm_grade_item_update($scorm, $grade);
574 scorm_grade_item_update($scorm);
579 * Update all grades in gradebook.
583 function scorm_upgrade_grades() {
586 $sql = "SELECT COUNT('x')
587 FROM {scorm} s, {course_modules} cm, {modules} m
588 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
589 $count = $DB->count_records_sql($sql);
591 $sql = "SELECT s.*, cm.idnumber AS cmidnumber, s.course AS courseid
592 FROM {scorm} s, {course_modules} cm, {modules} m
593 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
594 $rs = $DB->get_recordset_sql($sql);
596 $pbar = new progress_bar('scormupgradegrades', 500, true);
598 foreach ($rs as $scorm) {
600 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
601 scorm_update_grades($scorm, 0, false);
602 $pbar->update($i, $count, "Updating Scorm grades ($i/$count).");
609 * Update/create grade item for given scorm
613 * @uses GRADE_TYPE_VALUE
614 * @uses GRADE_TYPE_NONE
615 * @param object $scorm object with extra cmidnumber
616 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
617 * @return object grade_item
619 function scorm_grade_item_update($scorm, $grades=null) {
621 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
622 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
623 require_once($CFG->libdir.'/gradelib.php');
626 $params = array('itemname'=>$scorm->name);
627 if (isset($scorm->cmidnumber)) {
628 $params['idnumber'] = $scorm->cmidnumber;
631 if ($scorm->grademethod == GRADESCOES) {
632 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id))) {
633 $params['gradetype'] = GRADE_TYPE_VALUE;
634 $params['grademax'] = $maxgrade;
635 $params['grademin'] = 0;
637 $params['gradetype'] = GRADE_TYPE_NONE;
640 $params['gradetype'] = GRADE_TYPE_VALUE;
641 $params['grademax'] = $scorm->maxgrade;
642 $params['grademin'] = 0;
645 if ($grades === 'reset') {
646 $params['reset'] = true;
650 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
654 * Delete grade item for given scorm
657 * @param object $scorm object
658 * @return object grade_item
660 function scorm_grade_item_delete($scorm) {
662 require_once($CFG->libdir.'/gradelib.php');
664 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, null, array('deleted'=>1));
670 function scorm_get_view_actions() {
671 return array('pre-view', 'view', 'view all', 'report');
677 function scorm_get_post_actions() {
682 * @param object $scorm
683 * @return object $scorm
685 function scorm_option2text($scorm) {
686 $scorm_popoup_options = scorm_get_popup_options_array();
688 if (isset($scorm->popup)) {
689 if ($scorm->popup == 1) {
690 $optionlist = array();
691 foreach ($scorm_popoup_options as $name => $option) {
692 if (isset($scorm->$name)) {
693 $optionlist[] = $name.'='.$scorm->$name;
695 $optionlist[] = $name.'=0';
698 $scorm->options = implode(',', $optionlist);
700 $scorm->options = '';
704 $scorm->options = '';
710 * Implementation of the function for printing the form elements that control
711 * whether the course reset functionality affects the scorm.
713 * @param object $mform form passed by reference
715 function scorm_reset_course_form_definition(&$mform) {
716 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
717 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts', 'scorm'));
721 * Course reset form defaults.
725 function scorm_reset_course_form_defaults($course) {
726 return array('reset_scorm'=>1);
730 * Removes all grades from gradebook
734 * @param int $courseid
735 * @param string optional type
737 function scorm_reset_gradebook($courseid, $type='') {
740 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
741 FROM {scorm} s, {course_modules} cm, {modules} m
742 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
744 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
745 foreach ($scorms as $scorm) {
746 scorm_grade_item_update($scorm, 'reset');
752 * Actual implementation of the reset course functionality, delete all the
753 * scorm attempts for course $data->courseid.
757 * @param object $data the data submitted from the reset course.
758 * @return array status array
760 function scorm_reset_userdata($data) {
763 $componentstr = get_string('modulenameplural', 'scorm');
766 if (!empty($data->reset_scorm)) {
767 $scormssql = "SELECT s.id
771 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
773 // remove all grades from gradebook
774 if (empty($data->reset_gradebook_grades)) {
775 scorm_reset_gradebook($data->courseid);
778 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
781 // no dates to shift here
787 * Returns all other caps used in module
791 function scorm_get_extra_capabilities() {
792 return array('moodle/site:accessallgroups');
796 * Lists all file areas current user may browse
798 * @param object $course
800 * @param object $context
803 function scorm_get_file_areas($course, $cm, $context) {
805 $areas['content'] = get_string('areacontent', 'scorm');
806 $areas['package'] = get_string('areapackage', 'scorm');
811 * File browsing support for SCORM file areas
813 * @param stdclass $browser
814 * @param stdclass $areas
815 * @param stdclass $course
816 * @param stdclass $cm
817 * @param stdclass $context
818 * @param string $filearea
820 * @param string $filepath
821 * @param string $filename
822 * @return stdclass file_info instance or null if not found
824 function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
827 if (!has_capability('moodle/course:managefiles', $context)) {
831 // no writing for now!
833 $fs = get_file_storage();
835 if ($filearea === 'content') {
837 $filepath = is_null($filepath) ? '/' : $filepath;
838 $filename = is_null($filename) ? '.' : $filename;
840 $urlbase = $CFG->wwwroot.'/pluginfile.php';
841 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
842 if ($filepath === '/' and $filename === '.') {
843 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
849 require_once("$CFG->dirroot/mod/scorm/locallib.php");
850 return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
852 } else if ($filearea === 'package') {
853 $filepath = is_null($filepath) ? '/' : $filepath;
854 $filename = is_null($filename) ? '.' : $filename;
856 $urlbase = $CFG->wwwroot.'/pluginfile.php';
857 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, $filepath, $filename)) {
858 if ($filepath === '/' and $filename === '.') {
859 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
865 return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
868 // scorm_intro handled in file_browser
874 * Serves scorm content, introduction images and packages. Implements needed access control ;-)
876 * @param object $course
878 * @param object $context
879 * @param string $filearea
881 * @param bool $forcedownload
882 * @return bool false if file not found, does not return if found - just send the file
884 function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
887 if ($context->contextlevel != CONTEXT_MODULE) {
891 require_login($course, true, $cm);
893 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
895 if ($filearea === 'content') {
896 $revision = (int)array_shift($args); // prevents caching problems - ignored here
897 $relativepath = implode('/', $args);
898 $fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
899 // TODO: add any other access restrictions here if needed!
901 } else if ($filearea === 'package') {
902 if (!has_capability('moodle/course:manageactivities', $context)) {
905 $relativepath = implode('/', $args);
906 $fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
907 $lifetime = 0; // no caching here
913 $fs = get_file_storage();
914 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
918 // finally send the file
919 send_stored_file($file, $lifetime, 0, false);
923 * @uses FEATURE_GROUPS
924 * @uses FEATURE_GROUPINGS
925 * @uses FEATURE_GROUPMEMBERSONLY
926 * @uses FEATURE_MOD_INTRO
927 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
928 * @uses FEATURE_GRADE_HAS_GRADE
929 * @uses FEATURE_GRADE_OUTCOMES
930 * @param string $feature FEATURE_xx constant for requested feature
931 * @return mixed True if module supports feature, false if not, null if doesn't know
933 function scorm_supports($feature) {
935 case FEATURE_GROUPS: return false;
936 case FEATURE_GROUPINGS: return false;
937 case FEATURE_GROUPMEMBERSONLY: return true;
938 case FEATURE_MOD_INTRO: return true;
939 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
940 case FEATURE_GRADE_HAS_GRADE: return true;
941 case FEATURE_GRADE_OUTCOMES: return true;
942 case FEATURE_BACKUP_MOODLE2: return true;
944 default: return null;
949 * This function extends the global navigation for the site.
950 * It is important to note that you should not rely on PAGE objects within this
951 * body of code as there is no guarantee that during an AJAX request they are
954 * @param navigation_node $navigation The scorm node within the global navigation
955 * @param stdClass $course The course object returned from the DB
956 * @param stdClass $module The module object returned from the DB
957 * @param stdClass $cm The course module instance returned from the DB
959 function scorm_extend_navigation($navigation, $course, $module, $cm) {
961 * This is currently just a stub so that it can be easily expanded upon.
962 * When expanding just remove this comment and the line below and then add
965 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
969 * Get the filename for a temp log file
971 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
972 * @param integer $scoid - scoid of object this log entry is for
973 * @return string The filename as an absolute path
975 function scorm_debug_log_filename($type, $scoid) {
978 $logpath = $CFG->dataroot.'/temp/scormlogs';
979 $logfile = $logpath.'/'.$type.'debug_'.$USER->id.'_'.$scoid.'.log';
984 * writes log output to a temp log file
986 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
987 * @param string $text - text to be written to file.
988 * @param integer $scoid - scoid of object this log entry is for.
990 function scorm_debug_log_write($type, $text, $scoid) {
992 $debugenablelog = get_config('scorm', 'allowapidebug');
993 if (!$debugenablelog || empty($text)) {
996 if (make_upload_directory('temp/scormlogs/')) {
997 $logfile = scorm_debug_log_filename($type, $scoid);
998 @file_put_contents($logfile, date('Y/m/d H:i:s O')." DEBUG $text\r\n", FILE_APPEND);
1003 * Remove debug log file
1005 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1006 * @param integer $scoid - scoid of object this log entry is for
1007 * @return boolean True if the file is successfully deleted, false otherwise
1009 function scorm_debug_log_remove($type, $scoid) {
1011 $debugenablelog = get_config('scorm', 'allowapidebug');
1012 $logfile = scorm_debug_log_filename($type, $scoid);
1013 if (!$debugenablelog || !file_exists($logfile)) {
1017 return @unlink($logfile);
1021 * writes overview info for course_overview block - displays upcoming scorm objects that have a due date
1023 * @param object $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1024 * @param array $htmlarray
1027 function scorm_print_overview($courses, &$htmlarray) {
1028 global $USER, $CFG, $DB;
1030 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1034 if (!$scorms = get_all_instances_in_courses('scorm', $courses)) {
1038 $scormids = array();
1040 // Do scorm::isopen() here without loading the whole thing for speed
1041 foreach ($scorms as $key => $scorm) {
1043 if ($scorm->timeopen) {
1044 $isopen = ($scorm->timeopen <= $time && $time <= $scorm->timeclose);
1046 if (empty($scorm->displayattemptstatus) && (empty($isopen) || empty($scorm->timeclose))) {
1047 unset($scorms[$key]);
1049 $scormids[] = $scorm->id;
1053 if (empty($scormids)) {
1054 // no scorms to look at - we're done
1057 $strscorm = get_string('modulename', 'scorm');
1058 $strduedate = get_string('duedate', 'scorm');
1060 foreach ($scorms as $scorm) {
1061 $str = '<div class="scorm overview"><div class="name">'.$strscorm. ': '.
1062 '<a '.($scorm->visible ? '':' class="dimmed"').
1063 'title="'.$strscorm.'" href="'.$CFG->wwwroot.
1064 '/mod/scorm/view.php?id='.$scorm->coursemodule.'">'.
1065 $scorm->name.'</a></div>';
1066 if ($scorm->timeclose) {
1067 $str .= '<div class="info">'.$strduedate.': '.userdate($scorm->timeclose).'</div>';
1069 if ($scorm->displayattemptstatus == 1) {
1070 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
1071 $str .= '<div class="details">'.scorm_get_attempt_status($USER, $scorm).'</div>';
1074 if (empty($htmlarray[$scorm->course]['scorm'])) {
1075 $htmlarray[$scorm->course]['scorm'] = $str;
1077 $htmlarray[$scorm->course]['scorm'] .= $str;
1083 * Return a list of page types
1084 * @param string $pagetype current page type
1085 * @param stdClass $parentcontext Block's parent context
1086 * @param stdClass $currentcontext Current context of block
1088 function scorm_page_type_list($pagetype, $parentcontext, $currentcontext) {
1089 $module_pagetype = array('mod-scorm-*'=>get_string('page-mod-scorm-x', 'scorm'));
1090 return $module_pagetype;
1094 * Returns the SCORM version used.
1095 * @param string $scormversion comes from $scorm->version
1096 * @param string $version one of the defined vars SCORM_12, SCORM_13, SCORM_AICC (or empty)
1097 * @return Scorm version.
1099 function scorm_version_check($scormversion, $version='') {
1100 $scormversion = trim(strtolower($scormversion));
1101 if (empty($version) || $version==SCORM_12) {
1102 if ($scormversion == 'scorm_12' || $scormversion == 'scorm_1.2') {
1105 if (!empty($version)) {
1109 if (empty($version) || $version == SCORM_13) {
1110 if ($scormversion == 'scorm_13' || $scormversion == 'scorm_1.3') {
1113 if (!empty($version)) {
1117 if (empty($version) || $version == SCORM_AICC) {
1118 if (strpos($scormversion, 'aicc')) {
1121 if (!empty($version)) {