MDL-14679 fixed references to mod.html
[moodle.git] / mod / scorm / lib.php
CommitLineData
e4aa175a 1<?php // $Id$
2
e4aa175a 3/**
4* Given an object containing all the necessary data,
7cac0c4b 5* (defined by the form in mod_form.php) this function
e4aa175a 6* will create a new instance and return the id number
7* of the new instance.
8*
9* @param mixed $scorm Form data
10* @return int
11*/
23ab8e8d 12//require_once('locallib.php');
e4aa175a 13function scorm_add_instance($scorm) {
a679d64d 14 global $CFG;
15
16 require_once('locallib.php');
17
18 if (($packagedata = scorm_check_package($scorm)) != null) {
19 $scorm->pkgtype = $packagedata->pkgtype;
20 $scorm->datadir = $packagedata->datadir;
21 $scorm->launch = $packagedata->launch;
76ea4fb4 22 $scorm->parse = 1;
76ea4fb4 23
a679d64d 24 $scorm->timemodified = time();
25 if (!scorm_external_link($scorm->reference)) {
26 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
76ea4fb4 27 } else {
a679d64d 28 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
29 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
76ea4fb4 30 }
a679d64d 31
e4aa175a 32 $scorm = scorm_option2text($scorm);
33 $scorm->width = str_replace('%','',$scorm->width);
34 $scorm->height = str_replace('%','',$scorm->height);
35
36 //sanitize submitted values a bit
37 $scorm->width = clean_param($scorm->width, PARAM_INT);
38 $scorm->height = clean_param($scorm->height, PARAM_INT);
b3659259 39
40 if (!isset($scorm->whatgrade)) {
41 $scorm->whatgrade = 0;
42 }
a30b6819 43 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
e4aa175a 44
45 $id = insert_record('scorm', $scorm);
46
8949f8df 47 if (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#'))) {
e4aa175a 48 // Rename temp scorm dir to scorm id
49 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
50 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$id);
51 }
52
53 // Parse scorm manifest
5c1ac70c 54 if ($scorm->parse == 1) {
e4aa175a 55 $scorm->id = $id;
56 $scorm->launch = scorm_parse($scorm);
57 set_field('scorm','launch',$scorm->launch,'id',$scorm->id);
58 }
59
531fa830 60 scorm_grade_item_update(stripslashes_recursive($scorm));
61
e4aa175a 62 return $id;
a679d64d 63 } else {
7cac0c4b 64 print_error('badpackage','scorm');
e4aa175a 65 }
66}
67
68/**
69* Given an object containing all the necessary data,
7cac0c4b 70* (defined by the form in mod_form.php) this function
e4aa175a 71* will update an existing instance with new data.
72*
73* @param mixed $scorm Form data
74* @return int
75*/
76function scorm_update_instance($scorm) {
e4aa175a 77 global $CFG;
78
a679d64d 79 require_once('locallib.php');
76ea4fb4 80
23ab8e8d 81 $scorm->parse = 0;
a679d64d 82 if (($packagedata = scorm_check_package($scorm)) != null) {
83 $scorm->pkgtype = $packagedata->pkgtype;
84 if ($packagedata->launch == 0) {
85 $scorm->launch = $packagedata->launch;
86 $scorm->datadir = $packagedata->datadir;
76ea4fb4 87 $scorm->parse = 1;
a679d64d 88 if (!scorm_external_link($scorm->reference)) {
89 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
90 } else {
91 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
92 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
93 }
76ea4fb4 94 }
95 }
96
e4aa175a 97 $scorm->timemodified = time();
98 $scorm->id = $scorm->instance;
bfe8c2f0 99
e4aa175a 100 $scorm = scorm_option2text($scorm);
101 $scorm->width = str_replace('%','',$scorm->width);
102 $scorm->height = str_replace('%','',$scorm->height);
103
b3659259 104 if (!isset($scorm->whatgrade)) {
105 $scorm->whatgrade = 0;
106 }
a30b6819 107 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
108
e4aa175a 109 // Check if scorm manifest needs to be reparsed
5c1ac70c 110 if ($scorm->parse == 1) {
e4aa175a 111 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
5c1ac70c 112 if (is_dir($scorm->dir.'/'.$scorm->id)) {
e4aa175a 113 scorm_delete_files($scorm->dir.'/'.$scorm->id);
5c1ac70c 114 }
115 if (isset($scorm->datadir) && ($scorm->datadir != $scorm->id) &&
8949f8df 116 (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#')))) {
e4aa175a 117 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$scorm->id);
118 }
a30b6819 119
e4aa175a 120 $scorm->launch = scorm_parse($scorm);
a9d22c84 121 } else {
122 $oldscorm = get_record('scorm','id',$scorm->id);
123 $scorm->reference = $oldscorm->reference; // This fix a problem with Firefox when the teacher choose Cancel on overwrite question
e4aa175a 124 }
a9d22c84 125
531fa830 126 if ($result = update_record('scorm', $scorm)) {
127 scorm_grade_item_update(stripslashes_recursive($scorm));
128 }
129
130 return $result;
e4aa175a 131}
132
133/**
134* Given an ID of an instance of this module,
135* this function will permanently delete the instance
136* and any data that depends on it.
137*
138* @param int $id Scorm instance id
139* @return boolean
140*/
141function scorm_delete_instance($id) {
142
143 global $CFG;
144
145 if (! $scorm = get_record('scorm', 'id', $id)) {
146 return false;
147 }
148
149 $result = true;
150
2b3447c3 151 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
152 if (is_dir($scorm->dir.'/'.$scorm->id)) {
153 // Delete any dependent files
154 require_once('locallib.php');
155 scorm_delete_files($scorm->dir.'/'.$scorm->id);
156 }
e4aa175a 157
158 // Delete any dependent records
159 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
160 $result = false;
161 }
b3659259 162 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
163 foreach ($scoes as $sco) {
164 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id)) {
165 $result = false;
166 }
167 }
168 delete_records('scorm_scoes', 'scorm', $scorm->id);
169 } else {
e4aa175a 170 $result = false;
171 }
172 if (! delete_records('scorm', 'id', $scorm->id)) {
173 $result = false;
174 }
a30b6819 175
2b3447c3 176 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
e4aa175a 177 $result = false;
178 }
179 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
180 $result = false;
181 }
182 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
183 $result = false;
184 }
185 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
186 $result = false;
187 }
188 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
189 $result = false;
190 }
191 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
192 $result = false;
193 }
194 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
195 $result = false;
531fa830 196 }*/
197
198 scorm_grade_item_delete(stripslashes_recursive($scorm));
199
e4aa175a 200 return $result;
201}
202
203/**
204* Return a small object with summary information about what a
205* user has done with a given particular instance of this module
206* Used for user activity reports.
207*
208* @param int $course Course id
209* @param int $user User id
210* @param int $mod
211* @param int $scorm The scorm id
212* @return mixed
213*/
214function scorm_user_outline($course, $user, $mod, $scorm) {
531fa830 215 global $CFG;
a30b6819 216 require_once('locallib.php');
217
218 $return = scorm_grade_user($scorm, $user->id, true);
219
e4aa175a 220 return $return;
221}
222
223/**
224* Print a detailed representation of what a user has done with
225* a given particular instance of this module, for user activity reports.
226*
227* @param int $course Course id
228* @param int $user User id
229* @param int $mod
230* @param int $scorm The scorm id
231* @return boolean
232*/
233function scorm_user_complete($course, $user, $mod, $scorm) {
234 global $CFG;
235
236 $liststyle = 'structlist';
237 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
238 $now = time();
239 $firstmodify = $now;
240 $lastmodify = 0;
241 $sometoreport = false;
242 $report = '';
243
244 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
245 if (count($orgs) <= 1) {
246 unset($orgs);
247 $orgs[]->identifier = '';
248 }
249 $report .= '<div class="mod-scorm">'."\n";
250 foreach ($orgs as $org) {
251 $organizationsql = '';
252 $currentorg = '';
253 if (!empty($org->identifier)) {
254 $report .= '<div class="orgtitle">'.$org->title.'</div>';
255 $currentorg = $org->identifier;
256 $organizationsql = "AND organization='$currentorg'";
257 }
258 $report .= "<ul id='0' class='$liststyle'>";
259 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
9fb2de4e 260 // drop keys so that we can access array sequentially
261 $scoes = array_values($scoes);
e4aa175a 262 $level=0;
263 $sublist=1;
264 $parents[$level]='/';
9fb2de4e 265 foreach ($scoes as $pos=>$sco) {
e4aa175a 266 if ($parents[$level]!=$sco->parent) {
267 if ($level>0 && $parents[$level-1]==$sco->parent) {
268 $report .= "\t\t</ul></li>\n";
269 $level--;
270 } else {
271 $i = $level;
272 $closelist = '';
273 while (($i > 0) && ($parents[$level] != $sco->parent)) {
274 $closelist .= "\t\t</ul></li>\n";
275 $i--;
276 }
277 if (($i == 0) && ($sco->parent != $currentorg)) {
278 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
279 $level++;
280 } else {
281 $report .= $closelist;
282 $level = $i;
283 }
284 $parents[$level]=$sco->parent;
285 }
286 }
287 $report .= "\t\t<li>";
9fb2de4e 288 if (isset($scoes[$pos+1])) {
289 $nextsco = $scoes[$pos+1];
290 } else {
291 $nextsco = false;
292 }
e4aa175a 293 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
294 $sublist++;
295 } else {
7150b8ae 296 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
e4aa175a 297 }
298
299 if ($sco->launch) {
300 require_once('locallib.php');
301 $score = '';
302 $totaltime = '';
303 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
304 if ($usertrack->status == '') {
305 $usertrack->status = 'notattempted';
306 }
307 $strstatus = get_string($usertrack->status,'scorm');
308 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
309 if ($usertrack->timemodified != 0) {
310 if ($usertrack->timemodified > $lastmodify) {
311 $lastmodify = $usertrack->timemodified;
312 }
313 if ($usertrack->timemodified < $firstmodify) {
314 $firstmodify = $usertrack->timemodified;
315 }
316 }
317 } else {
318 if ($sco->scormtype == 'sco') {
319 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
320 } else {
321 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
322 }
323 }
324 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
325 if ($usertrack !== false) {
326 $sometoreport = true;
327 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
328 foreach($usertrack as $element => $value) {
329 if (substr($element,0,3) == 'cmi') {
330 $report .= '<li>'.$element.' => '.$value.'</li>';
331 }
332 }
333 $report .= "\t\t\t</ul></li>\n";
334 }
335 } else {
336 $report .= "&nbsp;$sco->title</li>\n";
337 }
338 }
339 for ($i=0;$i<$level;$i++) {
340 $report .= "\t\t</ul></li>\n";
341 }
342 }
343 $report .= "\t</ul><br />\n";
344 }
345 $report .= "</div>\n";
346 }
347 if ($sometoreport) {
348 if ($firstmodify < $now) {
349 $timeago = format_time($now - $firstmodify);
350 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
351 }
352 if ($lastmodify > 0) {
353 $timeago = format_time($now - $lastmodify);
354 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
355 }
356 echo get_string('report','scorm').":<br />\n";
357 echo $report;
358 } else {
dabfd0ed 359 print_string('noactivity','scorm');
e4aa175a 360 }
361
362 return true;
363}
364
e4aa175a 365/**
366* Function to be run periodically according to the moodle cron
367* This function searches for things that need to be done, such
368* as sending out mail, toggling flags etc ...
369*
370* @return boolean
371*/
372function scorm_cron () {
373
374 global $CFG;
a679d64d 375
376 require_once('locallib.php');
377
378 $sitetimezone = $CFG->timezone;
379 /// Now see if there are any digest mails waiting to be sent, and if we should send them
bfe8c2f0 380 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
381 set_config('scorm_updatetimelast', 0);
382 }
383
a679d64d 384 $timenow = time();
bfe8c2f0 385 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
a679d64d 386
387 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
388
bfe8c2f0 389 set_config('scorm_updatetimelast', $timenow);
e4aa175a 390
376c9c70 391 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 392
a679d64d 393 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
394 if (!empty($scormsupdate)) {
395 foreach($scormsupdate as $scormupdate) {
396 $scormupdate->instance = $scormupdate->id;
397 $id = scorm_update_instance($scormupdate);
398 }
399 }
400 }
401
e4aa175a 402 return true;
403}
404
405/**
531fa830 406 * Return grade for given user or all users.
407 *
408 * @param int $scormid id of scorm
409 * @param int $userid optional user id, 0 means all users
410 * @return array array of grades, false if none
411 */
412function scorm_get_user_grades($scorm, $userid=0) {
e4aa175a 413 global $CFG;
531fa830 414 require_once('locallib.php');
415
416 $grades = array();
417 if (empty($userid)) {
418 if ($scousers = get_records_select('scorm_scoes_track', "scormid='$scorm->id' GROUP BY userid", "", "userid,null")) {
419 foreach ($scousers as $scouser) {
420 $grades[$scouser->userid] = new object();
421 $grades[$scouser->userid]->id = $scouser->userid;
422 $grades[$scouser->userid]->userid = $scouser->userid;
423 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
424 }
425 } else {
426 return false;
427 }
e4aa175a 428
531fa830 429 } else {
430 if (!get_records_select('scorm_scoes_track', "scormid='$scorm->id' AND userid='$userid' GROUP BY userid", "", "userid,null")) {
431 return false; //no attempt yet
432 }
433 $grades[$userid] = new object();
434 $grades[$userid]->id = $userid;
435 $grades[$userid]->userid = $userid;
436 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
e4aa175a 437 }
e4aa175a 438
531fa830 439 return $grades;
440}
441
442/**
443 * Update grades in central gradebook
444 *
445 * @param object $scorm null means all scormbases
446 * @param int $userid specific user only, 0 mean all
447 */
448function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
449 global $CFG;
450 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
451 require_once($CFG->libdir.'/gradelib.php');
452 }
453
454 if ($scorm != null) {
455 if ($grades = scorm_get_user_grades($scorm, $userid)) {
50acc613 456 scorm_grade_item_update($scorm, $grades);
531fa830 457
458 } else if ($userid and $nullifnone) {
459 $grade = new object();
460 $grade->userid = $userid;
461 $grade->rawgrade = NULL;
eafb9d9e 462 scorm_grade_item_update($scorm, $grade);
463
464 } else {
465 scorm_grade_item_update($scorm);
e4aa175a 466 }
531fa830 467
e4aa175a 468 } else {
531fa830 469 $sql = "SELECT s.*, cm.idnumber as cmidnumber
470 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
471 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
472 if ($rs = get_recordset_sql($sql)) {
03cedd62 473 while ($scorm = rs_fetch_next_record($rs)) {
03cedd62 474 scorm_update_grades($scorm, 0, false);
531fa830 475 }
476 rs_close($rs);
477 }
e4aa175a 478 }
531fa830 479}
e4aa175a 480
531fa830 481/**
482 * Update/create grade item for given scorm
483 *
484 * @param object $scorm object with extra cmidnumber
0b5a80a1 485 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
531fa830 486 * @return object grade_item
487 */
0b5a80a1 488function scorm_grade_item_update($scorm, $grades=NULL) {
531fa830 489 global $CFG;
490 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
491 require_once($CFG->libdir.'/gradelib.php');
492 }
493
494 $params = array('itemname'=>$scorm->name, 'idnumber'=>$scorm->cmidnumber);
495
496 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
497 if ($maxgrade = count_records_select('scorm_scoes',"scorm='$scorm->id' AND launch<>''")) {
498 $params['gradetype'] = GRADE_TYPE_VALUE;
499 $params['grademax'] = $maxgrade;
500 $params['grademin'] = 0;
501 } else {
502 $params['gradetype'] = GRADE_TYPE_NONE;
e4aa175a 503 }
531fa830 504 } else {
505 $params['gradetype'] = GRADE_TYPE_VALUE;
506 $params['grademax'] = $scorm->maxgrade;
507 $params['grademin'] = 0;
e4aa175a 508 }
531fa830 509
0b5a80a1 510 if ($grades === 'reset') {
511 $params['reset'] = true;
512 $grades = NULL;
513 }
514
515 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
531fa830 516}
517
518/**
519 * Delete grade item for given scorm
520 *
521 * @param object $scorm object
522 * @return object grade_item
523 */
524function scorm_grade_item_delete($scorm) {
525 global $CFG;
526 require_once($CFG->libdir.'/gradelib.php');
527
528 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
e4aa175a 529}
530
531function scorm_get_view_actions() {
532 return array('pre-view','view','view all','report');
533}
534
535function scorm_get_post_actions() {
536 return array();
537}
538
e4aa175a 539function scorm_option2text($scorm) {
540 global $SCORM_POPUP_OPTIONS;
e4aa175a 541 if (isset($scorm->popup)) {
76ea4fb4 542 if ($scorm->popup == 1) {
e4aa175a 543 $optionlist = array();
544 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
545 if (isset($scorm->$name)) {
546 $optionlist[] = $name.'='.$scorm->$name;
547 } else {
548 $optionlist[] = $name.'=0';
549 }
550 }
551 $scorm->options = implode(',', $optionlist);
552 } else {
553 $scorm->options = '';
554 }
555 } else {
556 $scorm->popup = 0;
557 $scorm->options = '';
558 }
559 return $scorm;
560}
561
0b5a80a1 562/**
563 * Implementation of the function for printing the form elements that control
564 * whether the course reset functionality affects the scorm.
565 * @param $mform form passed by reference
566 */
567function scorm_reset_course_form_definition(&$mform) {
568 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
569 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
570}
571
572/**
573 * Course reset form defaults.
574 */
575function scorm_reset_course_form_defaults($course) {
576 return array('reset_scorm'=>1);
577}
578
579/**
580 * Removes all grades from gradebook
581 * @param int $courseid
582 * @param string optional type
583 */
584function scorm_reset_gradebook($courseid, $type='') {
585 global $CFG;
586
587 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
588 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
589 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=$courseid";
590
591 if ($scorms = get_records_sql($sql)) {
592 foreach ($scorms as $scorm) {
593 scorm_grade_item_update($scorm, 'reset');
594 }
595 }
596}
597
598/**
599 * Actual implementation of the rest coures functionality, delete all the
600 * scorm attempts for course $data->courseid.
601 * @param $data the data submitted from the reset course.
602 * @return array status array
603 */
604function scorm_reset_userdata($data) {
605 global $CFG;
606
607 $componentstr = get_string('modulenameplural', 'scorm');
608 $status = array();
609
610 if (!empty($data->reset_scorm)) {
611 $scormssql = "SELECT s.id
612 FROM {$CFG->prefix}scorm s
613 WHERE s.course={$data->courseid}";
614
615 delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)");
616
617 // remove all grades from gradebook
618 if (empty($data->reset_gradebook_grades)) {
619 scorm_reset_gradebook($data->courseid);
620 }
621
622 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
623 }
624
625 // no dates to shift here
626
627 return $status;
628}
629
5c1ac70c 630?>