MDL-29956: Implement export of Quiz activity (r16977, r17032)
[moodle.git] / mod / scorm / lib.php
blobb4c84a94fa4b38054e7163561b3edf00570ab5c6
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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 /**
18 * @package mod-scorm
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);
44 /**
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.
50 * @global stdClass
51 * @global object
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) {
62 global $CFG, $DB;
64 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
66 if (empty($scorm->timeopen)) {
67 $scorm->timeopen = 0;
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) {
96 if ($mform) {
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;
114 } else {
115 return false;
118 // save reference
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);
130 return $record->id;
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.
138 * @global stdClass
139 * @global object
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
147 * @return bool
149 function scorm_update_instance($scorm, $mform=null) {
150 global $CFG, $DB;
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) {
170 if ($mform) {
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;
189 } else {
190 return false;
193 $scorm = scorm_option2text($scorm);
194 $scorm->width = (int)str_replace('%', '', $scorm->width);
195 $scorm->height = (int)str_replace('%', '', $scorm->height);
196 $scorm->timemodified = time();
198 if (!isset($scorm->whatgrade)) {
199 $scorm->whatgrade = 0;
202 $DB->update_record('scorm', $scorm);
204 $scorm = $DB->get_record('scorm', array('id'=>$scorm->id));
206 /// extra fields required in grade related functions
207 $scorm->course = $courseid;
208 $scorm->idnumber = $cmidnumber;
209 $scorm->cmid = $cmid;
211 scorm_parse($scorm, (bool)$scorm->updatefreq);
213 scorm_grade_item_update($scorm);
214 scorm_update_grades($scorm);
216 return true;
220 * Given an ID of an instance of this module,
221 * this function will permanently delete the instance
222 * and any data that depends on it.
224 * @global stdClass
225 * @global object
226 * @param int $id Scorm instance id
227 * @return boolean
229 function scorm_delete_instance($id) {
230 global $CFG, $DB;
232 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
233 return false;
236 $result = true;
238 // Delete any dependent records
239 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
240 $result = false;
242 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
243 foreach ($scoes as $sco) {
244 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
245 $result = false;
248 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
249 } else {
250 $result = false;
252 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
253 $result = false;
256 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
257 $result = false;
259 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
260 $result = false;
262 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
263 $result = false;
265 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
266 $result = false;
268 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
269 $result = false;
271 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
272 $result = false;
274 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
275 $result = false;
278 scorm_grade_item_delete($scorm);
280 return $result;
284 * Return a small object with summary information about what a
285 * user has done with a given particular instance of this module
286 * Used for user activity reports.
288 * @global stdClass
289 * @param int $course Course id
290 * @param int $user User id
291 * @param int $mod
292 * @param int $scorm The scorm id
293 * @return mixed
295 function scorm_user_outline($course, $user, $mod, $scorm) {
296 global $CFG;
297 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
299 require_once("$CFG->libdir/gradelib.php");
300 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
301 if (!empty($grades->items[0]->grades)) {
302 $grade = reset($grades->items[0]->grades);
303 $result = new stdClass();
304 $result->info = get_string('grade') . ': '. $grade->str_long_grade;
306 //datesubmitted == time created. dategraded == time modified or time overridden
307 //if grade was last modified by the user themselves use date graded. Otherwise use date submitted
308 //TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
309 if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
310 $result->time = $grade->dategraded;
311 } else {
312 $result->time = $grade->datesubmitted;
315 return $result;
317 return null;
321 * Print a detailed representation of what a user has done with
322 * a given particular instance of this module, for user activity reports.
324 * @global stdClass
325 * @global object
326 * @param object $course
327 * @param object $user
328 * @param object $mod
329 * @param object $scorm
330 * @return boolean
332 function scorm_user_complete($course, $user, $mod, $scorm) {
333 global $CFG, $DB, $OUTPUT;
334 require_once("$CFG->libdir/gradelib.php");
336 $liststyle = 'structlist';
337 $now = time();
338 $firstmodify = $now;
339 $lastmodify = 0;
340 $sometoreport = false;
341 $report = '';
343 // First Access and Last Access dates for SCOs
344 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
345 $timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
346 $firstmodify = $timetracks->start;
347 $lastmodify = $timetracks->finish;
349 $grades = grade_get_grades($course->id, 'mod', 'scorm', $scorm->id, $user->id);
350 if (!empty($grades->items[0]->grades)) {
351 $grade = reset($grades->items[0]->grades);
352 echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
353 if ($grade->str_feedback) {
354 echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
358 if ($orgs = $DB->get_records_select('scorm_scoes', 'scorm = ? AND '.
359 $DB->sql_isempty('scorm_scoes', 'launch', false, true).' AND '.
360 $DB->sql_isempty('scorm_scoes', 'organization', false, false),
361 array($scorm->id), 'id', 'id,identifier,title')) {
362 if (count($orgs) <= 1) {
363 unset($orgs);
364 $orgs[]->identifier = '';
366 $report .= '<div class="mod-scorm">'."\n";
367 foreach ($orgs as $org) {
368 $conditions = array();
369 $currentorg = '';
370 if (!empty($org->identifier)) {
371 $report .= '<div class="orgtitle">'.$org->title.'</div>';
372 $currentorg = $org->identifier;
373 $conditions['organization'] = $currentorg;
375 $report .= "<ul id='0' class='$liststyle'>";
376 $conditions['scorm'] = $scorm->id;
377 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")) {
378 // drop keys so that we can access array sequentially
379 $scoes = array_values($scoes);
380 $level=0;
381 $sublist=1;
382 $parents[$level]='/';
383 foreach ($scoes as $pos => $sco) {
384 if ($parents[$level]!=$sco->parent) {
385 if ($level>0 && $parents[$level-1]==$sco->parent) {
386 $report .= "\t\t</ul></li>\n";
387 $level--;
388 } else {
389 $i = $level;
390 $closelist = '';
391 while (($i > 0) && ($parents[$level] != $sco->parent)) {
392 $closelist .= "\t\t</ul></li>\n";
393 $i--;
395 if (($i == 0) && ($sco->parent != $currentorg)) {
396 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
397 $level++;
398 } else {
399 $report .= $closelist;
400 $level = $i;
402 $parents[$level]=$sco->parent;
405 $report .= "\t\t<li>";
406 if (isset($scoes[$pos+1])) {
407 $nextsco = $scoes[$pos+1];
408 } else {
409 $nextsco = false;
411 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
412 $sublist++;
413 } else {
414 $report .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
417 if ($sco->launch) {
418 $score = '';
419 $totaltime = '';
420 if ($usertrack=scorm_get_tracks($sco->id, $user->id)) {
421 if ($usertrack->status == '') {
422 $usertrack->status = 'notattempted';
424 $strstatus = get_string($usertrack->status, 'scorm');
425 $report .= "<img src='".$OUTPUT->pix_url($usertrack->status, 'scorm')."' alt='$strstatus' title='$strstatus' />";
426 } else {
427 if ($sco->scormtype == 'sco') {
428 $report .= '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted', 'scorm').'" title="'.get_string('notattempted', 'scorm').'" />';
429 } else {
430 $report .= '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset', 'scorm').'" title="'.get_string('asset', 'scorm').'" />';
433 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
434 if ($usertrack !== false) {
435 $sometoreport = true;
436 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
437 foreach ($usertrack as $element => $value) {
438 if (substr($element, 0, 3) == 'cmi') {
439 $report .= '<li>'.$element.' => '.s($value).'</li>';
442 $report .= "\t\t\t</ul></li>\n";
444 } else {
445 $report .= "&nbsp;$sco->title</li>\n";
448 for ($i=0; $i<$level; $i++) {
449 $report .= "\t\t</ul></li>\n";
452 $report .= "\t</ul><br />\n";
454 $report .= "</div>\n";
456 if ($sometoreport) {
457 if ($firstmodify < $now) {
458 $timeago = format_time($now - $firstmodify);
459 echo get_string('firstaccess', 'scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
461 if ($lastmodify > 0) {
462 $timeago = format_time($now - $lastmodify);
463 echo get_string('lastaccess', 'scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
465 echo get_string('report', 'scorm').":<br />\n";
466 echo $report;
467 } else {
468 print_string('noactivity', 'scorm');
471 return true;
475 * Function to be run periodically according to the moodle cron
476 * This function searches for things that need to be done, such
477 * as sending out mail, toggling flags etc ...
479 * @global stdClass
480 * @global object
481 * @return boolean
483 function scorm_cron () {
484 global $CFG, $DB;
486 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
488 $sitetimezone = $CFG->timezone;
489 /// Now see if there are any scorm updates to be done
491 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
492 set_config('scorm_updatetimelast', 0);
495 $timenow = time();
496 $updatetime = usergetmidnight($timenow, $sitetimezone);
498 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
500 set_config('scorm_updatetimelast', $timenow);
502 mtrace('Updating scorm packages which require daily update');//We are updating
504 $scormsupdate = $DB->get_records('scorm', array('updatefreq'=>UPDATE_EVERYDAY));
505 foreach ($scormsupdate as $scormupdate) {
506 scorm_parse($scormupdate, true);
509 //now clear out AICC session table with old session data
510 $cfg_scorm = get_config('scorm');
511 if (!empty($cfg_scorm->allowaicchacp)) {
512 $expiretime = time() - ($cfg_scorm->aicchacpkeepsessiondata*24*60*60);
513 $DB->delete_records_select('scorm_aicc_session', 'WHERE timemodified < ?', array($expiretime));
517 return true;
521 * Return grade for given user or all users.
523 * @global stdClass
524 * @global object
525 * @param int $scormid id of scorm
526 * @param int $userid optional user id, 0 means all users
527 * @return array array of grades, false if none
529 function scorm_get_user_grades($scorm, $userid=0) {
530 global $CFG, $DB;
531 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
533 $grades = array();
534 if (empty($userid)) {
535 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
536 foreach ($scousers as $scouser) {
537 $grades[$scouser->userid] = new stdClass();
538 $grades[$scouser->userid]->id = $scouser->userid;
539 $grades[$scouser->userid]->userid = $scouser->userid;
540 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
542 } else {
543 return false;
546 } else {
547 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
548 return false; //no attempt yet
550 $grades[$userid] = new stdClass();
551 $grades[$userid]->id = $userid;
552 $grades[$userid]->userid = $userid;
553 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
556 return $grades;
560 * Update grades in central gradebook
562 * @global stdClass
563 * @global object
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) {
569 global $CFG, $DB;
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);
581 } else {
582 scorm_grade_item_update($scorm);
587 * Update all grades in gradebook.
589 * @global object
591 function scorm_upgrade_grades() {
592 global $DB;
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);
603 if ($rs->valid()) {
604 $pbar = new progress_bar('scormupgradegrades', 500, true);
605 $i=0;
606 foreach ($rs as $scorm) {
607 $i++;
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).");
613 $rs->close();
617 * Update/create grade item for given scorm
619 * @global stdClass
620 * @global object
621 * @uses GRADE_TYPE_VALUE
622 * @uses GRADE_TYPE_NONE
623 * @param object $scorm object with extra cmidnumber
624 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
625 * @return object grade_item
627 function scorm_grade_item_update($scorm, $grades=null) {
628 global $CFG, $DB;
629 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
630 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
631 require_once($CFG->libdir.'/gradelib.php');
634 $params = array('itemname'=>$scorm->name);
635 if (isset($scorm->cmidnumber)) {
636 $params['idnumber'] = $scorm->cmidnumber;
639 if ($scorm->grademethod == GRADESCOES) {
640 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id))) {
641 $params['gradetype'] = GRADE_TYPE_VALUE;
642 $params['grademax'] = $maxgrade;
643 $params['grademin'] = 0;
644 } else {
645 $params['gradetype'] = GRADE_TYPE_NONE;
647 } else {
648 $params['gradetype'] = GRADE_TYPE_VALUE;
649 $params['grademax'] = $scorm->maxgrade;
650 $params['grademin'] = 0;
653 if ($grades === 'reset') {
654 $params['reset'] = true;
655 $grades = null;
658 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
662 * Delete grade item for given scorm
664 * @global stdClass
665 * @param object $scorm object
666 * @return object grade_item
668 function scorm_grade_item_delete($scorm) {
669 global $CFG;
670 require_once($CFG->libdir.'/gradelib.php');
672 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, null, array('deleted'=>1));
676 * @return array
678 function scorm_get_view_actions() {
679 return array('pre-view', 'view', 'view all', 'report');
683 * @return array
685 function scorm_get_post_actions() {
686 return array();
690 * @param object $scorm
691 * @return object $scorm
693 function scorm_option2text($scorm) {
694 $scorm_popoup_options = scorm_get_popup_options_array();
696 if (isset($scorm->popup)) {
697 if ($scorm->popup == 1) {
698 $optionlist = array();
699 foreach ($scorm_popoup_options as $name => $option) {
700 if (isset($scorm->$name)) {
701 $optionlist[] = $name.'='.$scorm->$name;
702 } else {
703 $optionlist[] = $name.'=0';
706 $scorm->options = implode(',', $optionlist);
707 } else {
708 $scorm->options = '';
710 } else {
711 $scorm->popup = 0;
712 $scorm->options = '';
714 return $scorm;
718 * Implementation of the function for printing the form elements that control
719 * whether the course reset functionality affects the scorm.
721 * @param object $mform form passed by reference
723 function scorm_reset_course_form_definition(&$mform) {
724 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
725 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts', 'scorm'));
729 * Course reset form defaults.
731 * @return array
733 function scorm_reset_course_form_defaults($course) {
734 return array('reset_scorm'=>1);
738 * Removes all grades from gradebook
740 * @global stdClass
741 * @global object
742 * @param int $courseid
743 * @param string optional type
745 function scorm_reset_gradebook($courseid, $type='') {
746 global $CFG, $DB;
748 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
749 FROM {scorm} s, {course_modules} cm, {modules} m
750 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
752 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
753 foreach ($scorms as $scorm) {
754 scorm_grade_item_update($scorm, 'reset');
760 * Actual implementation of the reset course functionality, delete all the
761 * scorm attempts for course $data->courseid.
763 * @global stdClass
764 * @global object
765 * @param object $data the data submitted from the reset course.
766 * @return array status array
768 function scorm_reset_userdata($data) {
769 global $CFG, $DB;
771 $componentstr = get_string('modulenameplural', 'scorm');
772 $status = array();
774 if (!empty($data->reset_scorm)) {
775 $scormssql = "SELECT s.id
776 FROM {scorm} s
777 WHERE s.course=?";
779 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
781 // remove all grades from gradebook
782 if (empty($data->reset_gradebook_grades)) {
783 scorm_reset_gradebook($data->courseid);
786 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
789 // no dates to shift here
791 return $status;
795 * Returns all other caps used in module
797 * @return array
799 function scorm_get_extra_capabilities() {
800 return array('moodle/site:accessallgroups');
804 * Lists all file areas current user may browse
806 * @param object $course
807 * @param object $cm
808 * @param object $context
809 * @return array
811 function scorm_get_file_areas($course, $cm, $context) {
812 $areas = array();
813 $areas['content'] = get_string('areacontent', 'scorm');
814 $areas['package'] = get_string('areapackage', 'scorm');
815 return $areas;
819 * File browsing support for SCORM file areas
821 * @param stdclass $browser
822 * @param stdclass $areas
823 * @param stdclass $course
824 * @param stdclass $cm
825 * @param stdclass $context
826 * @param string $filearea
827 * @param int $itemid
828 * @param string $filepath
829 * @param string $filename
830 * @return stdclass file_info instance or null if not found
832 function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
833 global $CFG;
835 if (!has_capability('moodle/course:managefiles', $context)) {
836 return null;
839 // no writing for now!
841 $fs = get_file_storage();
843 if ($filearea === 'content') {
845 $filepath = is_null($filepath) ? '/' : $filepath;
846 $filename = is_null($filename) ? '.' : $filename;
848 $urlbase = $CFG->wwwroot.'/pluginfile.php';
849 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
850 if ($filepath === '/' and $filename === '.') {
851 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
852 } else {
853 // not found
854 return null;
857 require_once("$CFG->dirroot/mod/scorm/locallib.php");
858 return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
860 } else if ($filearea === 'package') {
861 $filepath = is_null($filepath) ? '/' : $filepath;
862 $filename = is_null($filename) ? '.' : $filename;
864 $urlbase = $CFG->wwwroot.'/pluginfile.php';
865 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, $filepath, $filename)) {
866 if ($filepath === '/' and $filename === '.') {
867 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
868 } else {
869 // not found
870 return null;
873 return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
876 // scorm_intro handled in file_browser
878 return false;
882 * Serves scorm content, introduction images and packages. Implements needed access control ;-)
884 * @param object $course
885 * @param object $cm
886 * @param object $context
887 * @param string $filearea
888 * @param array $args
889 * @param bool $forcedownload
890 * @return bool false if file not found, does not return if found - just send the file
892 function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
893 global $CFG;
895 if ($context->contextlevel != CONTEXT_MODULE) {
896 return false;
899 require_login($course, true, $cm);
901 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
903 if ($filearea === 'content') {
904 $revision = (int)array_shift($args); // prevents caching problems - ignored here
905 $relativepath = implode('/', $args);
906 $fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
907 // TODO: add any other access restrictions here if needed!
909 } else if ($filearea === 'package') {
910 if (!has_capability('moodle/course:manageactivities', $context)) {
911 return false;
913 $relativepath = implode('/', $args);
914 $fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
915 $lifetime = 0; // no caching here
917 } else {
918 return false;
921 $fs = get_file_storage();
922 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
923 return false;
926 // finally send the file
927 send_stored_file($file, $lifetime, 0, false);
931 * @uses FEATURE_GROUPS
932 * @uses FEATURE_GROUPINGS
933 * @uses FEATURE_GROUPMEMBERSONLY
934 * @uses FEATURE_MOD_INTRO
935 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
936 * @uses FEATURE_GRADE_HAS_GRADE
937 * @uses FEATURE_GRADE_OUTCOMES
938 * @param string $feature FEATURE_xx constant for requested feature
939 * @return mixed True if module supports feature, false if not, null if doesn't know
941 function scorm_supports($feature) {
942 switch($feature) {
943 case FEATURE_GROUPS: return false;
944 case FEATURE_GROUPINGS: return false;
945 case FEATURE_GROUPMEMBERSONLY: return true;
946 case FEATURE_MOD_INTRO: return true;
947 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
948 case FEATURE_GRADE_HAS_GRADE: return true;
949 case FEATURE_GRADE_OUTCOMES: return true;
950 case FEATURE_BACKUP_MOODLE2: return true;
951 case FEATURE_SHOW_DESCRIPTION: return true;
953 default: return null;
958 * This function extends the global navigation for the site.
959 * It is important to note that you should not rely on PAGE objects within this
960 * body of code as there is no guarantee that during an AJAX request they are
961 * available
963 * @param navigation_node $navigation The scorm node within the global navigation
964 * @param stdClass $course The course object returned from the DB
965 * @param stdClass $module The module object returned from the DB
966 * @param stdClass $cm The course module instance returned from the DB
968 function scorm_extend_navigation($navigation, $course, $module, $cm) {
970 * This is currently just a stub so that it can be easily expanded upon.
971 * When expanding just remove this comment and the line below and then add
972 * you content.
974 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
978 * Get the filename for a temp log file
980 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
981 * @param integer $scoid - scoid of object this log entry is for
982 * @return string The filename as an absolute path
984 function scorm_debug_log_filename($type, $scoid) {
985 global $CFG, $USER;
987 $logpath = $CFG->tempdir.'/scormlogs';
988 $logfile = $logpath.'/'.$type.'debug_'.$USER->id.'_'.$scoid.'.log';
989 return $logfile;
993 * writes log output to a temp log file
995 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
996 * @param string $text - text to be written to file.
997 * @param integer $scoid - scoid of object this log entry is for.
999 function scorm_debug_log_write($type, $text, $scoid) {
1001 $debugenablelog = get_config('scorm', 'allowapidebug');
1002 if (!$debugenablelog || empty($text)) {
1003 return;
1005 if (make_temp_directory('scormlogs/')) {
1006 $logfile = scorm_debug_log_filename($type, $scoid);
1007 @file_put_contents($logfile, date('Y/m/d H:i:s O')." DEBUG $text\r\n", FILE_APPEND);
1012 * Remove debug log file
1014 * @param string $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1015 * @param integer $scoid - scoid of object this log entry is for
1016 * @return boolean True if the file is successfully deleted, false otherwise
1018 function scorm_debug_log_remove($type, $scoid) {
1020 $debugenablelog = get_config('scorm', 'allowapidebug');
1021 $logfile = scorm_debug_log_filename($type, $scoid);
1022 if (!$debugenablelog || !file_exists($logfile)) {
1023 return false;
1026 return @unlink($logfile);
1030 * writes overview info for course_overview block - displays upcoming scorm objects that have a due date
1032 * @param object $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1033 * @param array $htmlarray
1034 * @return mixed
1036 function scorm_print_overview($courses, &$htmlarray) {
1037 global $USER, $CFG, $DB;
1039 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1040 return array();
1043 if (!$scorms = get_all_instances_in_courses('scorm', $courses)) {
1044 return;
1047 $scormids = array();
1049 // Do scorm::isopen() here without loading the whole thing for speed
1050 foreach ($scorms as $key => $scorm) {
1051 $time = time();
1052 if ($scorm->timeopen) {
1053 $isopen = ($scorm->timeopen <= $time && $time <= $scorm->timeclose);
1055 if (empty($scorm->displayattemptstatus) && (empty($isopen) || empty($scorm->timeclose))) {
1056 unset($scorms[$key]);
1057 } else {
1058 $scormids[] = $scorm->id;
1062 if (empty($scormids)) {
1063 // no scorms to look at - we're done
1064 return true;
1066 $strscorm = get_string('modulename', 'scorm');
1067 $strduedate = get_string('duedate', 'scorm');
1069 foreach ($scorms as $scorm) {
1070 $str = '<div class="scorm overview"><div class="name">'.$strscorm. ': '.
1071 '<a '.($scorm->visible ? '':' class="dimmed"').
1072 'title="'.$strscorm.'" href="'.$CFG->wwwroot.
1073 '/mod/scorm/view.php?id='.$scorm->coursemodule.'">'.
1074 $scorm->name.'</a></div>';
1075 if ($scorm->timeclose) {
1076 $str .= '<div class="info">'.$strduedate.': '.userdate($scorm->timeclose).'</div>';
1078 if ($scorm->displayattemptstatus == 1) {
1079 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
1080 $str .= '<div class="details">'.scorm_get_attempt_status($USER, $scorm).'</div>';
1082 $str .= '</div>';
1083 if (empty($htmlarray[$scorm->course]['scorm'])) {
1084 $htmlarray[$scorm->course]['scorm'] = $str;
1085 } else {
1086 $htmlarray[$scorm->course]['scorm'] .= $str;
1092 * Return a list of page types
1093 * @param string $pagetype current page type
1094 * @param stdClass $parentcontext Block's parent context
1095 * @param stdClass $currentcontext Current context of block
1097 function scorm_page_type_list($pagetype, $parentcontext, $currentcontext) {
1098 $module_pagetype = array('mod-scorm-*'=>get_string('page-mod-scorm-x', 'scorm'));
1099 return $module_pagetype;
1103 * Returns the SCORM version used.
1104 * @param string $scormversion comes from $scorm->version
1105 * @param string $version one of the defined vars SCORM_12, SCORM_13, SCORM_AICC (or empty)
1106 * @return Scorm version.
1108 function scorm_version_check($scormversion, $version='') {
1109 $scormversion = trim(strtolower($scormversion));
1110 if (empty($version) || $version==SCORM_12) {
1111 if ($scormversion == 'scorm_12' || $scormversion == 'scorm_1.2') {
1112 return SCORM_12;
1114 if (!empty($version)) {
1115 return false;
1118 if (empty($version) || $version == SCORM_13) {
1119 if ($scormversion == 'scorm_13' || $scormversion == 'scorm_1.3') {
1120 return SCORM_13;
1122 if (!empty($version)) {
1123 return false;
1126 if (empty($version) || $version == SCORM_AICC) {
1127 if (strpos($scormversion, 'aicc')) {
1128 return SCORM_AICC;
1130 if (!empty($version)) {
1131 return false;
1134 return false;