MDL-28933 SCORM use a global function to check SCORM version to make sure all methods...
[moodle.git] / mod / scorm / lib.php
CommitLineData
28f672b2 1<?php
28f672b2 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.
13//
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/>.
e4aa175a 16
28f672b2 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
21 */
22
23/** SCORM_TYPE_LOCAL = local */
9528568b 24define('SCORM_TYPE_LOCAL', 'local');
28f672b2 25/** SCORM_TYPE_LOCALSYNC = localsync */
9528568b 26define('SCORM_TYPE_LOCALSYNC', 'localsync');
28f672b2 27/** SCORM_TYPE_EXTERNAL = external */
9528568b 28define('SCORM_TYPE_EXTERNAL', 'external');
28f672b2 29/** SCORM_TYPE_IMSREPOSITORY = imsrepository */
9528568b 30define('SCORM_TYPE_IMSREPOSITORY', 'imsrepository');
31
99da7a95
DM
32define('SCORM_TOC_SIDE', 0);
33define('SCORM_TOC_HIDDEN', 1);
34define('SCORM_TOC_POPUP', 2);
35define('SCORM_TOC_DISABLED', 3);
9528568b 36
e6402b54
DM
37//used to check what SCORM version is being used.
38define('SCORM_12', 1);
39define('SCORM_13', 2);
40define('SCORM_AICC', 3);
41
e4aa175a 42/**
28f672b2 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.
47 *
48 * @global stdClass
49 * @global object
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
58 */
9528568b 59function scorm_add_instance($scorm, $mform=null) {
c18269c7 60 global $CFG, $DB;
a679d64d 61
86996ffe 62 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a679d64d 63
413900b4 64 if (empty($scorm->timeopen)) {
d54e2145 65 $scorm->timeopen = 0;
413900b4
DM
66 }
67 if (empty($scorm->timeclose)) {
d54e2145 68 $scorm->timeclose = 0;
69 }
9528568b 70 $cmid = $scorm->coursemodule;
71 $cmidnumber = $scorm->cmidnumber;
72 $courseid = $scorm->course;
76ea4fb4 73
9528568b 74 $context = get_context_instance(CONTEXT_MODULE, $cmid);
a679d64d 75
9528568b 76 $scorm = scorm_option2text($scorm);
77 $scorm->width = (int)str_replace('%', '', $scorm->width);
78 $scorm->height = (int)str_replace('%', '', $scorm->height);
e4aa175a 79
9528568b 80 if (!isset($scorm->whatgrade)) {
81 $scorm->whatgrade = 0;
82 }
b3659259 83
a8f3a651 84 $id = $DB->insert_record('scorm', $scorm);
e4aa175a 85
f7b5c6aa 86 /// update course module record - from now on this instance properly exists and all function may be used
bf8e93d7 87 $DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
e4aa175a 88
f7b5c6aa 89 /// reload scorm instance
783f1486 90 $record = $DB->get_record('scorm', array('id'=>$id));
9528568b 91
f7b5c6aa 92 /// store the package and verify
783f1486 93 if ($record->scormtype === SCORM_TYPE_LOCAL) {
9528568b 94 if ($mform) {
95 $filename = $mform->get_new_filename('packagefile');
96 if ($filename !== false) {
97 $fs = get_file_storage();
64f93798
PS
98 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
99 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
783f1486 100 $record->reference = $filename;
8ba4f1e0 101 }
e4aa175a 102 }
103
783f1486
DC
104 } else if ($record->scormtype === SCORM_TYPE_LOCALSYNC) {
105 $record->reference = $scorm->packageurl;
e4aa175a 106
783f1486
DC
107 } else if ($record->scormtype === SCORM_TYPE_EXTERNAL) {
108 $record->reference = $scorm->packageurl;
9528568b 109
783f1486
DC
110 } else if ($record->scormtype === SCORM_TYPE_IMSREPOSITORY) {
111 $record->reference = $scorm->packageurl;
531fa830 112
a679d64d 113 } else {
9528568b 114 return false;
e4aa175a 115 }
9528568b 116
117 // save reference
783f1486 118 $DB->update_record('scorm', $record);
9528568b 119
f7b5c6aa 120 /// extra fields required in grade related functions
783f1486
DC
121 $record->course = $courseid;
122 $record->cmidnumber = $cmidnumber;
123 $record->cmid = $cmid;
9528568b 124
783f1486 125 scorm_parse($record, true);
9528568b 126
783f1486 127 scorm_grade_item_update($record);
9528568b 128
783f1486 129 return $record->id;
e4aa175a 130}
131
132/**
28f672b2 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.
136 *
137 * @global stdClass
138 * @global object
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
146 * @return bool
147 */
9528568b 148function scorm_update_instance($scorm, $mform=null) {
c18269c7 149 global $CFG, $DB;
e4aa175a 150
86996ffe 151 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
76ea4fb4 152
413900b4 153 if (empty($scorm->timeopen)) {
d54e2145 154 $scorm->timeopen = 0;
413900b4
DM
155 }
156 if (empty($scorm->timeclose)) {
d54e2145 157 $scorm->timeclose = 0;
158 }
159
9528568b 160 $cmid = $scorm->coursemodule;
161 $cmidnumber = $scorm->cmidnumber;
162 $courseid = $scorm->course;
163
164 $scorm->id = $scorm->instance;
165
166 $context = get_context_instance(CONTEXT_MODULE, $cmid);
167
168 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
169 if ($mform) {
170 $filename = $mform->get_new_filename('packagefile');
171 if ($filename !== false) {
172 $scorm->reference = $filename;
173 $fs = get_file_storage();
64f93798
PS
174 $fs->delete_area_files($context->id, 'mod_scorm', 'package');
175 $mform->save_stored_file('packagefile', $context->id, 'mod_scorm', 'package', 0, '/', $filename);
a679d64d 176 }
76ea4fb4 177 }
76ea4fb4 178
9528568b 179 } else if ($scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
180 $scorm->reference = $scorm->packageurl;
181
182 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL) {
183 $scorm->reference = $scorm->packageurl;
184
185 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY) {
186 $scorm->reference = $scorm->packageurl;
187
188 } else {
189 return false;
190 }
bfe8c2f0 191
e4aa175a 192 $scorm = scorm_option2text($scorm);
f7b5c6aa
DM
193 $scorm->width = (int)str_replace('%', '', $scorm->width);
194 $scorm->height = (int)str_replace('%', '', $scorm->height);
9528568b 195 $scorm->timemodified = time();
e4aa175a 196
b3659259 197 if (!isset($scorm->whatgrade)) {
198 $scorm->whatgrade = 0;
199 }
a30b6819 200
a8c31db2 201 $DB->update_record('scorm', $scorm);
531fa830 202
9528568b 203 $scorm = $DB->get_record('scorm', array('id'=>$scorm->id));
204
f7b5c6aa 205 /// extra fields required in grade related functions
9528568b 206 $scorm->course = $courseid;
207 $scorm->idnumber = $cmidnumber;
208 $scorm->cmid = $cmid;
209
210 scorm_parse($scorm, (bool)$scorm->updatefreq);
211
212 scorm_grade_item_update($scorm);
50412dc1 213 scorm_update_grades($scorm);
9528568b 214
215 return true;
e4aa175a 216}
217
218/**
28f672b2 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.
222 *
223 * @global stdClass
224 * @global object
225 * @param int $id Scorm instance id
226 * @return boolean
227 */
e4aa175a 228function scorm_delete_instance($id) {
c18269c7 229 global $CFG, $DB;
e4aa175a 230
c18269c7 231 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
e4aa175a 232 return false;
233 }
234
235 $result = true;
236
e4aa175a 237 // Delete any dependent records
c18269c7 238 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
e4aa175a 239 $result = false;
240 }
c18269c7 241 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
b3659259 242 foreach ($scoes as $sco) {
c18269c7 243 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
b3659259 244 $result = false;
245 }
9528568b 246 }
c18269c7 247 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
b3659259 248 } else {
e4aa175a 249 $result = false;
250 }
c18269c7 251 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
e4aa175a 252 $result = false;
253 }
a30b6819 254
c18269c7 255 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
e4aa175a 256 $result = false;
257 }
c18269c7 258 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
e4aa175a 259 $result = false;
260 }
c18269c7 261 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
e4aa175a 262 $result = false;
263 }
c18269c7 264 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 265 $result = false;
266 }
c18269c7 267 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 268 $result = false;
269 }
c18269c7 270 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 271 $result = false;
272 }
c18269c7 273 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 274 $result = false;
9528568b 275 }*/
531fa830 276
c18269c7 277 scorm_grade_item_delete($scorm);
9528568b 278
e4aa175a 279 return $result;
280}
281
282/**
28f672b2 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.
286 *
287 * @global stdClass
288 * @param int $course Course id
289 * @param int $user User id
290 * @param int $mod
291 * @param int $scorm The scorm id
292 * @return mixed
293 */
9528568b 294function scorm_user_outline($course, $user, $mod, $scorm) {
531fa830 295 global $CFG;
86996ffe 296 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a30b6819 297
1a96363a
NC
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);
39790bd8 302 $result = new stdClass();
1a96363a 303 $result->info = get_string('grade') . ': '. $grade->str_long_grade;
4433f871
AD
304
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
94a74f54 307 //TODO: move this copied & pasted code somewhere in the grades API. See MDL-26704
4433f871
AD
308 if ($grade->usermodified == $user->id || empty($grade->datesubmitted)) {
309 $result->time = $grade->dategraded;
310 } else {
311 $result->time = $grade->datesubmitted;
312 }
313
1a96363a
NC
314 return $result;
315 }
316 return null;
e4aa175a 317}
318
319/**
28f672b2 320 * Print a detailed representation of what a user has done with
321 * a given particular instance of this module, for user activity reports.
322 *
323 * @global stdClass
324 * @global object
325 * @param object $course
326 * @param object $user
327 * @param object $mod
328 * @param object $scorm
329 * @return boolean
330 */
e4aa175a 331function scorm_user_complete($course, $user, $mod, $scorm) {
d436d197 332 global $CFG, $DB, $OUTPUT;
1a96363a 333 require_once("$CFG->libdir/gradelib.php");
e4aa175a 334
335 $liststyle = 'structlist';
e4aa175a 336 $now = time();
337 $firstmodify = $now;
338 $lastmodify = 0;
339 $sometoreport = false;
340 $report = '';
9895302c 341
c86a91d5 342 // First Access and Last Access dates for SCOs
86996ffe 343 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
c86a91d5
PH
344 $timetracks = scorm_get_sco_runtime($scorm->id, false, $user->id);
345 $firstmodify = $timetracks->start;
346 $lastmodify = $timetracks->finish;
9895302c 347
1a96363a
NC
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);
354 }
355 }
356
84ab3289
DM
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),
f7b5c6aa 360 array($scorm->id), 'id', 'id,identifier,title')) {
e4aa175a 361 if (count($orgs) <= 1) {
362 unset($orgs);
363 $orgs[]->identifier = '';
364 }
365 $report .= '<div class="mod-scorm">'."\n";
366 foreach ($orgs as $org) {
bf347041 367 $conditions = array();
e4aa175a 368 $currentorg = '';
369 if (!empty($org->identifier)) {
370 $report .= '<div class="orgtitle">'.$org->title.'</div>';
371 $currentorg = $org->identifier;
bf347041 372 $conditions['organization'] = $currentorg;
e4aa175a 373 }
374 $report .= "<ul id='0' class='$liststyle'>";
bf347041 375 $conditions['scorm'] = $scorm->id;
f7b5c6aa 376 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")) {
9fb2de4e 377 // drop keys so that we can access array sequentially
9528568b 378 $scoes = array_values($scoes);
e4aa175a 379 $level=0;
380 $sublist=1;
381 $parents[$level]='/';
f7b5c6aa 382 foreach ($scoes as $pos => $sco) {
e4aa175a 383 if ($parents[$level]!=$sco->parent) {
384 if ($level>0 && $parents[$level-1]==$sco->parent) {
385 $report .= "\t\t</ul></li>\n";
386 $level--;
387 } else {
388 $i = $level;
389 $closelist = '';
390 while (($i > 0) && ($parents[$level] != $sco->parent)) {
391 $closelist .= "\t\t</ul></li>\n";
392 $i--;
393 }
394 if (($i == 0) && ($sco->parent != $currentorg)) {
395 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
396 $level++;
397 } else {
398 $report .= $closelist;
399 $level = $i;
400 }
401 $parents[$level]=$sco->parent;
402 }
403 }
404 $report .= "\t\t<li>";
9fb2de4e 405 if (isset($scoes[$pos+1])) {
406 $nextsco = $scoes[$pos+1];
407 } else {
408 $nextsco = false;
409 }
e4aa175a 410 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
411 $sublist++;
412 } else {
485f4ce6 413 $report .= '<img src="'.$OUTPUT->pix_url('spacer', 'scorm').'" alt="" />';
e4aa175a 414 }
415
416 if ($sco->launch) {
e4aa175a 417 $score = '';
418 $totaltime = '';
f7b5c6aa 419 if ($usertrack=scorm_get_tracks($sco->id, $user->id)) {
e4aa175a 420 if ($usertrack->status == '') {
421 $usertrack->status = 'notattempted';
422 }
f7b5c6aa 423 $strstatus = get_string($usertrack->status, 'scorm');
485f4ce6 424 $report .= "<img src='".$OUTPUT->pix_url($usertrack->status, 'scorm')."' alt='$strstatus' title='$strstatus' />";
e4aa175a 425 } else {
426 if ($sco->scormtype == 'sco') {
f7b5c6aa 427 $report .= '<img src="'.$OUTPUT->pix_url('notattempted', 'scorm').'" alt="'.get_string('notattempted', 'scorm').'" title="'.get_string('notattempted', 'scorm').'" />';
e4aa175a 428 } else {
f7b5c6aa 429 $report .= '<img src="'.$OUTPUT->pix_url('asset', 'scorm').'" alt="'.get_string('asset', 'scorm').'" title="'.get_string('asset', 'scorm').'" />';
e4aa175a 430 }
431 }
432 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
433 if ($usertrack !== false) {
434 $sometoreport = true;
435 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
f7b5c6aa
DM
436 foreach ($usertrack as $element => $value) {
437 if (substr($element, 0, 3) == 'cmi') {
a7ab614d 438 $report .= '<li>'.$element.' => '.s($value).'</li>';
e4aa175a 439 }
440 }
441 $report .= "\t\t\t</ul></li>\n";
9528568b 442 }
e4aa175a 443 } else {
444 $report .= "&nbsp;$sco->title</li>\n";
445 }
446 }
f7b5c6aa 447 for ($i=0; $i<$level; $i++) {
e4aa175a 448 $report .= "\t\t</ul></li>\n";
449 }
450 }
451 $report .= "\t</ul><br />\n";
452 }
453 $report .= "</div>\n";
454 }
455 if ($sometoreport) {
456 if ($firstmodify < $now) {
457 $timeago = format_time($now - $firstmodify);
f7b5c6aa 458 echo get_string('firstaccess', 'scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
e4aa175a 459 }
460 if ($lastmodify > 0) {
461 $timeago = format_time($now - $lastmodify);
f7b5c6aa 462 echo get_string('lastaccess', 'scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
e4aa175a 463 }
f7b5c6aa 464 echo get_string('report', 'scorm').":<br />\n";
e4aa175a 465 echo $report;
466 } else {
f7b5c6aa 467 print_string('noactivity', 'scorm');
e4aa175a 468 }
469
470 return true;
471}
472
e4aa175a 473/**
28f672b2 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 ...
477 *
478 * @global stdClass
479 * @global object
480 * @return boolean
481 */
e4aa175a 482function scorm_cron () {
bf347041 483 global $CFG, $DB;
a679d64d 484
86996ffe 485 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
a679d64d 486
487 $sitetimezone = $CFG->timezone;
9528568b 488 /// Now see if there are any scorm updates to be done
489
bfe8c2f0 490 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
491 set_config('scorm_updatetimelast', 0);
492 }
493
a679d64d 494 $timenow = time();
c5b3ba6a 495 $updatetime = usergetmidnight($timenow, $sitetimezone);
a679d64d 496
497 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
498
bfe8c2f0 499 set_config('scorm_updatetimelast', $timenow);
e4aa175a 500
376c9c70 501 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 502
bf347041 503 $scormsupdate = $DB->get_records('scorm', array('updatefreq'=>UPDATE_EVERYDAY));
f7b5c6aa 504 foreach ($scormsupdate as $scormupdate) {
9528568b 505 scorm_parse($scormupdate, true);
a679d64d 506 }
507 }
508
e4aa175a 509 return true;
510}
511
512/**
531fa830 513 * Return grade for given user or all users.
514 *
28f672b2 515 * @global stdClass
516 * @global object
531fa830 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
520 */
521function scorm_get_user_grades($scorm, $userid=0) {
bf347041 522 global $CFG, $DB;
86996ffe 523 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
531fa830 524
525 $grades = array();
526 if (empty($userid)) {
bf347041 527 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
531fa830 528 foreach ($scousers as $scouser) {
39790bd8 529 $grades[$scouser->userid] = new stdClass();
531fa830 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);
533 }
534 } else {
535 return false;
536 }
e4aa175a 537
531fa830 538 } else {
bf347041 539 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
531fa830 540 return false; //no attempt yet
541 }
39790bd8 542 $grades[$userid] = new stdClass();
531fa830 543 $grades[$userid]->id = $userid;
544 $grades[$userid]->userid = $userid;
545 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
e4aa175a 546 }
e4aa175a 547
531fa830 548 return $grades;
549}
550
551/**
552 * Update grades in central gradebook
553 *
28f672b2 554 * @global stdClass
555 * @global object
775f811a 556 * @param object $scorm
531fa830 557 * @param int $userid specific user only, 0 mean all
28f672b2 558 * @param bool $nullifnone
531fa830 559 */
775f811a 560function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
bf347041 561 global $CFG, $DB;
775f811a 562 require_once($CFG->libdir.'/gradelib.php');
531fa830 563
775f811a 564 if ($grades = scorm_get_user_grades($scorm, $userid)) {
565 scorm_grade_item_update($scorm, $grades);
531fa830 566
775f811a 567 } else if ($userid and $nullifnone) {
39790bd8 568 $grade = new stdClass();
775f811a 569 $grade->userid = $userid;
f7b5c6aa 570 $grade->rawgrade = null;
775f811a 571 scorm_grade_item_update($scorm, $grade);
531fa830 572
e4aa175a 573 } else {
775f811a 574 scorm_grade_item_update($scorm);
575 }
576}
577
578/**
579 * Update all grades in gradebook.
28f672b2 580 *
581 * @global object
775f811a 582 */
583function scorm_upgrade_grades() {
584 global $DB;
585
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);
590
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";
676874df
EL
594 $rs = $DB->get_recordset_sql($sql);
595 if ($rs->valid()) {
775f811a 596 $pbar = new progress_bar('scormupgradegrades', 500, true);
597 $i=0;
598 foreach ($rs as $scorm) {
599 $i++;
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).");
531fa830 603 }
e4aa175a 604 }
676874df 605 $rs->close();
531fa830 606}
e4aa175a 607
531fa830 608/**
609 * Update/create grade item for given scorm
610 *
28f672b2 611 * @global stdClass
612 * @global object
613 * @uses GRADE_TYPE_VALUE
614 * @uses GRADE_TYPE_NONE
531fa830 615 * @param object $scorm object with extra cmidnumber
28f672b2 616 * @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
531fa830 617 * @return object grade_item
618 */
f7b5c6aa 619function scorm_grade_item_update($scorm, $grades=null) {
bf347041 620 global $CFG, $DB;
0b9e4fdc 621 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
531fa830 622 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
623 require_once($CFG->libdir.'/gradelib.php');
624 }
625
7ef16bf9 626 $params = array('itemname'=>$scorm->name);
627 if (isset($scorm->cmidnumber)) {
628 $params['idnumber'] = $scorm->cmidnumber;
629 }
9528568b 630
50412dc1 631 if ($scorm->grademethod == GRADESCOES) {
84ab3289 632 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true), array($scorm->id))) {
531fa830 633 $params['gradetype'] = GRADE_TYPE_VALUE;
634 $params['grademax'] = $maxgrade;
635 $params['grademin'] = 0;
636 } else {
637 $params['gradetype'] = GRADE_TYPE_NONE;
e4aa175a 638 }
531fa830 639 } else {
640 $params['gradetype'] = GRADE_TYPE_VALUE;
641 $params['grademax'] = $scorm->maxgrade;
642 $params['grademin'] = 0;
e4aa175a 643 }
531fa830 644
0b5a80a1 645 if ($grades === 'reset') {
646 $params['reset'] = true;
f7b5c6aa 647 $grades = null;
0b5a80a1 648 }
649
650 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
531fa830 651}
652
653/**
654 * Delete grade item for given scorm
655 *
28f672b2 656 * @global stdClass
531fa830 657 * @param object $scorm object
658 * @return object grade_item
659 */
660function scorm_grade_item_delete($scorm) {
661 global $CFG;
662 require_once($CFG->libdir.'/gradelib.php');
663
f7b5c6aa 664 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, null, array('deleted'=>1));
e4aa175a 665}
666
28f672b2 667/**
668 * @return array
669 */
e4aa175a 670function scorm_get_view_actions() {
f7b5c6aa 671 return array('pre-view', 'view', 'view all', 'report');
e4aa175a 672}
673
28f672b2 674/**
675 * @return array
676 */
e4aa175a 677function scorm_get_post_actions() {
678 return array();
679}
680
28f672b2 681/**
682 * @param object $scorm
683 * @return object $scorm
684 */
e4aa175a 685function scorm_option2text($scorm) {
1adc77e6 686 $scorm_popoup_options = scorm_get_popup_options_array();
e5dd8e3b 687
e4aa175a 688 if (isset($scorm->popup)) {
76ea4fb4 689 if ($scorm->popup == 1) {
e4aa175a 690 $optionlist = array();
1adc77e6 691 foreach ($scorm_popoup_options as $name => $option) {
e4aa175a 692 if (isset($scorm->$name)) {
693 $optionlist[] = $name.'='.$scorm->$name;
694 } else {
695 $optionlist[] = $name.'=0';
696 }
9528568b 697 }
e4aa175a 698 $scorm->options = implode(',', $optionlist);
699 } else {
700 $scorm->options = '';
9528568b 701 }
e4aa175a 702 } else {
703 $scorm->popup = 0;
704 $scorm->options = '';
705 }
706 return $scorm;
707}
708
0b5a80a1 709/**
710 * Implementation of the function for printing the form elements that control
711 * whether the course reset functionality affects the scorm.
e5dd8e3b 712 *
28f672b2 713 * @param object $mform form passed by reference
0b5a80a1 714 */
715function scorm_reset_course_form_definition(&$mform) {
716 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
f7b5c6aa 717 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts', 'scorm'));
0b5a80a1 718}
719
720/**
721 * Course reset form defaults.
28f672b2 722 *
723 * @return array
0b5a80a1 724 */
725function scorm_reset_course_form_defaults($course) {
726 return array('reset_scorm'=>1);
727}
728
729/**
730 * Removes all grades from gradebook
28f672b2 731 *
732 * @global stdClass
733 * @global object
0b5a80a1 734 * @param int $courseid
735 * @param string optional type
736 */
737function scorm_reset_gradebook($courseid, $type='') {
bf347041 738 global $CFG, $DB;
0b5a80a1 739
740 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
bf347041 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=?";
0b5a80a1 743
bf347041 744 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 745 foreach ($scorms as $scorm) {
746 scorm_grade_item_update($scorm, 'reset');
747 }
748 }
749}
750
751/**
72d2982e 752 * Actual implementation of the reset course functionality, delete all the
0b5a80a1 753 * scorm attempts for course $data->courseid.
28f672b2 754 *
755 * @global stdClass
756 * @global object
757 * @param object $data the data submitted from the reset course.
0b5a80a1 758 * @return array status array
759 */
760function scorm_reset_userdata($data) {
bf347041 761 global $CFG, $DB;
0b5a80a1 762
763 $componentstr = get_string('modulenameplural', 'scorm');
764 $status = array();
765
766 if (!empty($data->reset_scorm)) {
767 $scormssql = "SELECT s.id
bf347041 768 FROM {scorm} s
769 WHERE s.course=?";
0b5a80a1 770
bf347041 771 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
0b5a80a1 772
773 // remove all grades from gradebook
774 if (empty($data->reset_gradebook_grades)) {
775 scorm_reset_gradebook($data->courseid);
776 }
777
778 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
779 }
780
781 // no dates to shift here
782
783 return $status;
784}
785
f432bebf 786/**
787 * Returns all other caps used in module
28f672b2 788 *
789 * @return array
f432bebf 790 */
791function scorm_get_extra_capabilities() {
792 return array('moodle/site:accessallgroups');
793}
794
9528568b 795/**
796 * Lists all file areas current user may browse
28f672b2 797 *
798 * @param object $course
799 * @param object $cm
800 * @param object $context
801 * @return array
9528568b 802 */
803function scorm_get_file_areas($course, $cm, $context) {
804 $areas = array();
64f93798
PS
805 $areas['content'] = get_string('areacontent', 'scorm');
806 $areas['package'] = get_string('areapackage', 'scorm');
9528568b 807 return $areas;
808}
809
810/**
9895302c 811 * File browsing support for SCORM file areas
e5dd8e3b 812 *
9895302c
DC
813 * @param stdclass $browser
814 * @param stdclass $areas
815 * @param stdclass $course
816 * @param stdclass $cm
817 * @param stdclass $context
818 * @param string $filearea
819 * @param int $itemid
820 * @param string $filepath
821 * @param string $filename
822 * @return stdclass file_info instance or null if not found
9528568b 823 */
824function scorm_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
825 global $CFG;
826
827 if (!has_capability('moodle/course:managefiles', $context)) {
828 return null;
829 }
830
831 // no writing for now!
832
833 $fs = get_file_storage();
834
64f93798 835 if ($filearea === 'content') {
9528568b 836
837 $filepath = is_null($filepath) ? '/' : $filepath;
838 $filename = is_null($filename) ? '.' : $filename;
e5dd8e3b 839
9528568b 840 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 841 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'content', 0, $filepath, $filename)) {
9528568b 842 if ($filepath === '/' and $filename === '.') {
64f93798 843 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'content', 0);
9528568b 844 } else {
845 // not found
846 return null;
847 }
848 }
64f93798 849 require_once("$CFG->dirroot/mod/scorm/locallib.php");
8546def3 850 return new scorm_package_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, false, false);
9528568b 851
64f93798 852 } else if ($filearea === 'package') {
9528568b 853 $filepath = is_null($filepath) ? '/' : $filepath;
854 $filename = is_null($filename) ? '.' : $filename;
e5dd8e3b 855
9528568b 856 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 857 if (!$storedfile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, $filepath, $filename)) {
9528568b 858 if ($filepath === '/' and $filename === '.') {
64f93798 859 $storedfile = new virtual_root_file($context->id, 'mod_scorm', 'package', 0);
9528568b 860 } else {
861 // not found
862 return null;
863 }
864 }
9895302c 865 return new file_info_stored($browser, $context, $storedfile, $urlbase, $areas[$filearea], false, true, false, false);
9528568b 866 }
867
868 // scorm_intro handled in file_browser
869
870 return false;
871}
872
873/**
874 * Serves scorm content, introduction images and packages. Implements needed access control ;-)
28f672b2 875 *
28f672b2 876 * @param object $course
64f93798 877 * @param object $cm
28f672b2 878 * @param object $context
879 * @param string $filearea
880 * @param array $args
98edf7b6 881 * @param bool $forcedownload
86900a93 882 * @return bool false if file not found, does not return if found - just send the file
9528568b 883 */
64f93798 884function scorm_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
9528568b 885 global $CFG;
886
64f93798 887 if ($context->contextlevel != CONTEXT_MODULE) {
0a8a7b6c 888 return false;
889 }
e5dd8e3b 890
0a8a7b6c 891 require_login($course, true, $cm);
892
64f93798
PS
893 $lifetime = isset($CFG->filelifetime) ? $CFG->filelifetime : 86400;
894
895 if ($filearea === 'content') {
9528568b 896 $revision = (int)array_shift($args); // prevents caching problems - ignored here
64f93798 897 $relativepath = implode('/', $args);
4cec22dc 898 $fullpath = "/$context->id/mod_scorm/content/0/$relativepath";
9528568b 899 // TODO: add any other access restrictions here if needed!
900
64f93798 901 } else if ($filearea === 'package') {
9528568b 902 if (!has_capability('moodle/course:manageactivities', $context)) {
903 return false;
904 }
64f93798
PS
905 $relativepath = implode('/', $args);
906 $fullpath = "/$context->id/mod_scorm/package/0/$relativepath";
9528568b 907 $lifetime = 0; // no caching here
908
909 } else {
910 return false;
911 }
912
913 $fs = get_file_storage();
914 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
915 return false;
916 }
917
918 // finally send the file
919 send_stored_file($file, $lifetime, 0, false);
920}
921
42f103be 922/**
28f672b2 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
42f103be 930 * @param string $feature FEATURE_xx constant for requested feature
28f672b2 931 * @return mixed True if module supports feature, false if not, null if doesn't know
42f103be 932 */
933function scorm_supports($feature) {
934 switch($feature) {
935 case FEATURE_GROUPS: return false;
936 case FEATURE_GROUPINGS: return false;
937 case FEATURE_GROUPMEMBERSONLY: return true;
dc5c2bd9 938 case FEATURE_MOD_INTRO: return true;
42f103be 939 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
940 case FEATURE_GRADE_HAS_GRADE: return true;
941 case FEATURE_GRADE_OUTCOMES: return true;
02488b86 942 case FEATURE_BACKUP_MOODLE2: return true;
42f103be 943
944 default: return null;
945 }
946}
0a1f8f8f
SH
947
948/**
792881f0 949 * This function extends the global navigation for the site.
0a1f8f8f
SH
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
952 * available
953 *
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
792881f0 957 * @param stdClass $cm The course module instance returned from the DB
0a1f8f8f
SH
958 */
959function scorm_extend_navigation($navigation, $course, $module, $cm) {
960 /**
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
963 * you content.
964 */
965 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
02488b86 966}
a4c4962f
DM
967
968/**
969 * Get the filename for a temp log file
970 *
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
91421f3e 973 * @return string The filename as an absolute path
f7b5c6aa 974 */
a4c4962f
DM
975function scorm_debug_log_filename($type, $scoid) {
976 global $CFG, $USER;
977
978 $logpath = $CFG->dataroot.'/temp/scormlogs';
979 $logfile = $logpath.'/'.$type.'debug_'.$USER->id.'_'.$scoid.'.log';
980 return $logfile;
981}
982
169a204c
DM
983/**
984 * writes log output to a temp log file
985 *
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.
989 */
a4c4962f 990function scorm_debug_log_write($type, $text, $scoid) {
169a204c 991
3b4b776a 992 $debugenablelog = get_config('scorm', 'allowapidebug');
169a204c 993 if (!$debugenablelog || empty($text)) {
f7b5c6aa 994 return;
169a204c
DM
995 }
996 if (make_upload_directory('temp/scormlogs/')) {
a4c4962f 997 $logfile = scorm_debug_log_filename($type, $scoid);
169a204c
DM
998 @file_put_contents($logfile, date('Y/m/d H:i:s O')." DEBUG $text\r\n", FILE_APPEND);
999 }
1000}
a4c4962f 1001
f7b5c6aa 1002/**
a4c4962f
DM
1003 * Remove debug log file
1004 *
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
1008 */
1009function scorm_debug_log_remove($type, $scoid) {
1010
3b4b776a 1011 $debugenablelog = get_config('scorm', 'allowapidebug');
a4c4962f
DM
1012 $logfile = scorm_debug_log_filename($type, $scoid);
1013 if (!$debugenablelog || !file_exists($logfile)) {
1014 return false;
1015 }
1016
1017 return @unlink($logfile);
1018}
1019
d860e4f8
DM
1020/**
1021 * writes overview info for course_overview block - displays upcoming scorm objects that have a due date
1022 *
1023 * @param object $type - type of log(aicc,scorm12,scorm13) used as prefix for filename
1024 * @param array $htmlarray
64f93798 1025 * @return mixed
d860e4f8
DM
1026 */
1027function scorm_print_overview($courses, &$htmlarray) {
1028 global $USER, $CFG, $DB;
1029
1030 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1031 return array();
1032 }
1033
f7b5c6aa 1034 if (!$scorms = get_all_instances_in_courses('scorm', $courses)) {
d860e4f8
DM
1035 return;
1036 }
1037
1038 $scormids = array();
1039
1040 // Do scorm::isopen() here without loading the whole thing for speed
1041 foreach ($scorms as $key => $scorm) {
1042 $time = time();
1043 if ($scorm->timeopen) {
1044 $isopen = ($scorm->timeopen <= $time && $time <= $scorm->timeclose);
1045 }
1a70a647 1046 if (empty($scorm->displayattemptstatus) && (empty($isopen) || empty($scorm->timeclose))) {
d860e4f8 1047 unset($scorms[$key]);
f7b5c6aa 1048 } else {
d860e4f8
DM
1049 $scormids[] = $scorm->id;
1050 }
1051 }
1052
f7b5c6aa 1053 if (empty($scormids)) {
91421f3e 1054 // no scorms to look at - we're done
d860e4f8
DM
1055 return true;
1056 }
1057 $strscorm = get_string('modulename', 'scorm');
1058 $strduedate = get_string('duedate', 'scorm');
1059
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.
bdea587b 1064 '/mod/scorm/view.php?id='.$scorm->coursemodule.'">'.
d860e4f8
DM
1065 $scorm->name.'</a></div>';
1066 if ($scorm->timeclose) {
1067 $str .= '<div class="info">'.$strduedate.': '.userdate($scorm->timeclose).'</div>';
1068 }
6257f664
DM
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>';
1072 }
d860e4f8
DM
1073 $str .= '</div>';
1074 if (empty($htmlarray[$scorm->course]['scorm'])) {
1075 $htmlarray[$scorm->course]['scorm'] = $str;
1076 } else {
1077 $htmlarray[$scorm->course]['scorm'] .= $str;
1078 }
1079 }
1080}
b1627a92
DC
1081
1082/**
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
1087 */
b38e2e28 1088function scorm_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
1089 $module_pagetype = array('mod-scorm-*'=>get_string('page-mod-scorm-x', 'scorm'));
1090 return $module_pagetype;
1091}
e6402b54
DM
1092
1093/**
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.
1098 */
1099function 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') {
1103 return SCORM_12;
1104 }
1105 if (!empty($version)) {
1106 return false;
1107 }
1108 }
1109 if (empty($version) || $version == SCORM_13) {
1110 if ($scormversion == 'scorm_13' || $scormversion == 'scorm_1.3') {
1111 return SCORM_13;
1112 }
1113 if (!empty($version)) {
1114 return false;
1115 }
1116 }
1117 if (empty($version) || $version == SCORM_AICC) {
1118 if (strpos($scormversion, 'aicc')) {
1119 return SCORM_AICC;
1120 }
1121 if (!empty($version)) {
1122 return false;
1123 }
1124 }
1125 return false;
1126}