Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / scorm / lib.php
blobdc200cbfb9980de6f0298ad3c51321e462800d34
1 <?php // $Id$
3 /**
4 * Given an object containing all the necessary data,
5 * (defined by the form in mod.html) this function
6 * will create a new instance and return the id number
7 * of the new instance.
9 * @param mixed $scorm Form data
10 * @return int
12 //require_once('locallib.php');
13 function scorm_add_instance($scorm) {
14 global $CFG;
16 require_once('locallib.php');
18 if (($packagedata = scorm_check_package($scorm)) != null) {
19 $scorm->pkgtype = $packagedata->pkgtype;
20 $scorm->datadir = $packagedata->datadir;
21 $scorm->launch = $packagedata->launch;
22 $scorm->parse = 1;
24 $scorm->timemodified = time();
25 if (!scorm_external_link($scorm->reference)) {
26 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
27 } else {
28 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
29 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
32 $scorm = scorm_option2text($scorm);
33 $scorm->width = str_replace('%','',$scorm->width);
34 $scorm->height = str_replace('%','',$scorm->height);
36 //sanitize submitted values a bit
37 $scorm->width = clean_param($scorm->width, PARAM_INT);
38 $scorm->height = clean_param($scorm->height, PARAM_INT);
40 if (!isset($scorm->whatgrade)) {
41 $scorm->whatgrade = 0;
43 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
45 $id = insert_record('scorm', $scorm);
47 if (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#'))) {
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);
53 // Parse scorm manifest
54 if ($scorm->parse == 1) {
55 $scorm->id = $id;
56 $scorm->launch = scorm_parse($scorm);
57 set_field('scorm','launch',$scorm->launch,'id',$scorm->id);
60 scorm_grade_item_update(stripslashes_recursive($scorm));
62 return $id;
63 } else {
64 print_error('badpackage','scorm');
68 /**
69 * Given an object containing all the necessary data,
70 * (defined by the form in mod.html) this function
71 * will update an existing instance with new data.
73 * @param mixed $scorm Form data
74 * @return int
76 function scorm_update_instance($scorm) {
77 global $CFG;
79 require_once('locallib.php');
81 $scorm->parse = 0;
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;
87 $scorm->parse = 1;
88 if (!scorm_external_link($scorm->reference) && $scorm->reference[0] != '#') { //dont set md5hash if this is from a repo.
89 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
90 } elseif($scorm->reference[0] != '#') { //dont set md5hash if this is from a repo.
91 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
92 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
97 $scorm->timemodified = time();
98 $scorm->id = $scorm->instance;
100 $scorm = scorm_option2text($scorm);
101 $scorm->width = str_replace('%','',$scorm->width);
102 $scorm->height = str_replace('%','',$scorm->height);
104 if (!isset($scorm->whatgrade)) {
105 $scorm->whatgrade = 0;
107 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
109 // Check if scorm manifest needs to be reparsed
110 if ($scorm->parse == 1) {
111 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
112 if (is_dir($scorm->dir.'/'.$scorm->id)) {
113 scorm_delete_files($scorm->dir.'/'.$scorm->id);
115 if (isset($scorm->datadir) && ($scorm->datadir != $scorm->id) &&
116 (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#')))) {
117 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$scorm->id);
120 $scorm->launch = scorm_parse($scorm);
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
126 if ($result = update_record('scorm', $scorm)) {
127 scorm_grade_item_update(stripslashes_recursive($scorm));
128 //scorm_grade_item_update($scorm); // John Macklins fix - dont think this is needed
131 return $result;
135 * Given an ID of an instance of this module,
136 * this function will permanently delete the instance
137 * and any data that depends on it.
139 * @param int $id Scorm instance id
140 * @return boolean
142 function scorm_delete_instance($id) {
144 global $CFG;
146 if (! $scorm = get_record('scorm', 'id', $id)) {
147 return false;
150 $result = true;
152 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
153 if (is_dir($scorm->dir.'/'.$scorm->id)) {
154 // Delete any dependent files
155 require_once('locallib.php');
156 scorm_delete_files($scorm->dir.'/'.$scorm->id);
159 // Delete any dependent records
160 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
161 $result = false;
163 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
164 foreach ($scoes as $sco) {
165 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id)) {
166 $result = false;
169 delete_records('scorm_scoes', 'scorm', $scorm->id);
170 } else {
171 $result = false;
173 if (! delete_records('scorm', 'id', $scorm->id)) {
174 $result = false;
177 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
178 $result = false;
180 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
181 $result = false;
183 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
184 $result = false;
186 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
187 $result = false;
189 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
190 $result = false;
192 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
193 $result = false;
195 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
196 $result = false;
197 }*/
199 scorm_grade_item_delete(stripslashes_recursive($scorm));
201 return $result;
205 * Return a small object with summary information about what a
206 * user has done with a given particular instance of this module
207 * Used for user activity reports.
209 * @param int $course Course id
210 * @param int $user User id
211 * @param int $mod
212 * @param int $scorm The scorm id
213 * @return mixed
215 function scorm_user_outline($course, $user, $mod, $scorm) {
216 global $CFG;
217 require_once('locallib.php');
219 $return = scorm_grade_user($scorm, $user->id, true);
221 return $return;
225 * Print a detailed representation of what a user has done with
226 * a given particular instance of this module, for user activity reports.
228 * @param int $course Course id
229 * @param int $user User id
230 * @param int $mod
231 * @param int $scorm The scorm id
232 * @return boolean
234 function scorm_user_complete($course, $user, $mod, $scorm) {
235 global $CFG;
237 $liststyle = 'structlist';
238 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
239 $now = time();
240 $firstmodify = $now;
241 $lastmodify = 0;
242 $sometoreport = false;
243 $report = '';
245 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
246 if (count($orgs) <= 1) {
247 unset($orgs);
248 $orgs[]->identifier = '';
250 $report .= '<div class="mod-scorm">'."\n";
251 foreach ($orgs as $org) {
252 $organizationsql = '';
253 $currentorg = '';
254 if (!empty($org->identifier)) {
255 $report .= '<div class="orgtitle">'.$org->title.'</div>';
256 $currentorg = $org->identifier;
257 $organizationsql = "AND organization='$currentorg'";
259 $report .= "<ul id='0' class='$liststyle'>";
260 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
261 // drop keys so that we can access array sequentially
262 $scoes = array_values($scoes);
263 $level=0;
264 $sublist=1;
265 $parents[$level]='/';
266 foreach ($scoes as $pos=>$sco) {
267 if ($parents[$level]!=$sco->parent) {
268 if ($level>0 && $parents[$level-1]==$sco->parent) {
269 $report .= "\t\t</ul></li>\n";
270 $level--;
271 } else {
272 $i = $level;
273 $closelist = '';
274 while (($i > 0) && ($parents[$level] != $sco->parent)) {
275 $closelist .= "\t\t</ul></li>\n";
276 $i--;
278 if (($i == 0) && ($sco->parent != $currentorg)) {
279 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
280 $level++;
281 } else {
282 $report .= $closelist;
283 $level = $i;
285 $parents[$level]=$sco->parent;
288 $report .= "\t\t<li>";
289 if (isset($scoes[$pos+1])) {
290 $nextsco = $scoes[$pos+1];
291 } else {
292 $nextsco = false;
294 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
295 $sublist++;
296 } else {
297 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
300 if ($sco->launch) {
301 require_once('locallib.php');
302 $score = '';
303 $totaltime = '';
304 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
305 if ($usertrack->status == '') {
306 $usertrack->status = 'notattempted';
308 $strstatus = get_string($usertrack->status,'scorm');
309 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
310 if ($usertrack->timemodified != 0) {
311 if ($usertrack->timemodified > $lastmodify) {
312 $lastmodify = $usertrack->timemodified;
314 if ($usertrack->timemodified < $firstmodify) {
315 $firstmodify = $usertrack->timemodified;
318 } else {
319 if ($sco->scormtype == 'sco') {
320 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
321 } else {
322 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
325 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
326 if ($usertrack !== false) {
327 $sometoreport = true;
328 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
329 foreach($usertrack as $element => $value) {
330 if (substr($element,0,3) == 'cmi') {
331 $report .= '<li>'.$element.' => '.$value.'</li>';
334 $report .= "\t\t\t</ul></li>\n";
336 } else {
337 $report .= "&nbsp;$sco->title</li>\n";
340 for ($i=0;$i<$level;$i++) {
341 $report .= "\t\t</ul></li>\n";
344 $report .= "\t</ul><br />\n";
346 $report .= "</div>\n";
348 if ($sometoreport) {
349 if ($firstmodify < $now) {
350 $timeago = format_time($now - $firstmodify);
351 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
353 if ($lastmodify > 0) {
354 $timeago = format_time($now - $lastmodify);
355 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
357 echo get_string('report','scorm').":<br />\n";
358 echo $report;
359 } else {
360 print_string('noactivity','scorm');
363 return true;
367 * Function to be run periodically according to the moodle cron
368 * This function searches for things that need to be done, such
369 * as sending out mail, toggling flags etc ...
371 * @return boolean
373 function scorm_cron () {
375 global $CFG;
377 require_once('locallib.php');
379 $sitetimezone = $CFG->timezone;
380 /// Now see if there are any digest mails waiting to be sent, and if we should send them
381 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
382 set_config('scorm_updatetimelast', 0);
385 $timenow = time();
386 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
388 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
390 set_config('scorm_updatetimelast', $timenow);
392 mtrace('Updating scorm packages which require daily update');//We are updating
394 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
395 if (!empty($scormsupdate)) {
396 foreach($scormsupdate as $scormupdate) {
397 $scormupdate->instance = $scormupdate->id;
398 $id = scorm_update_instance($scormupdate);
403 return true;
407 * Return grade for given user or all users.
409 * @param int $scormid id of scorm
410 * @param int $userid optional user id, 0 means all users
411 * @return array array of grades, false if none
413 function scorm_get_user_grades($scorm, $userid=0) {
414 global $CFG;
415 require_once('locallib.php');
417 $grades = array();
418 if (empty($userid)) {
419 if ($scousers = get_records_select('scorm_scoes_track', "scormid='$scorm->id' GROUP BY userid", "", "userid,null")) {
420 foreach ($scousers as $scouser) {
421 $grades[$scouser->userid] = new object();
422 $grades[$scouser->userid]->id = $scouser->userid;
423 $grades[$scouser->userid]->userid = $scouser->userid;
424 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
426 } else {
427 return false;
430 } else {
431 if (!get_records_select('scorm_scoes_track', "scormid='$scorm->id' AND userid='$userid' GROUP BY userid", "", "userid,null")) {
432 return false; //no attempt yet
434 $grades[$userid] = new object();
435 $grades[$userid]->id = $userid;
436 $grades[$userid]->userid = $userid;
437 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
440 return $grades;
444 * Update grades in central gradebook
446 * @param object $scorm null means all scormbases
447 * @param int $userid specific user only, 0 mean all
449 function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
450 global $CFG;
451 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
452 require_once($CFG->libdir.'/gradelib.php');
455 if ($scorm != null) {
456 if ($grades = scorm_get_user_grades($scorm, $userid)) {
457 scorm_grade_item_update($scorm, $grades);
459 } else if ($userid and $nullifnone) {
460 $grade = new object();
461 $grade->userid = $userid;
462 $grade->rawgrade = NULL;
463 scorm_grade_item_update($scorm, $grade);
465 } else {
466 scorm_grade_item_update($scorm);
469 } else {
470 $sql = "SELECT s.*, cm.idnumber as cmidnumber
471 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
472 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
473 if ($rs = get_recordset_sql($sql)) {
474 while ($scorm = rs_fetch_next_record($rs)) {
475 scorm_update_grades($scorm, 0, false);
477 rs_close($rs);
483 * Update/create grade item for given scorm
485 * @param object $scorm object with extra cmidnumber
486 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
487 * @return object grade_item
489 function scorm_grade_item_update($scorm, $grades=NULL) {
490 global $CFG;
491 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
492 require_once($CFG->libdir.'/gradelib.php');
495 $params = array('itemname'=>$scorm->name);
496 if (isset($scorm->cmidnumber)) {
497 $params['idnumber'] = $scorm->cmidnumber;
500 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
501 if ($maxgrade = count_records_select('scorm_scoes',"scorm='$scorm->id' AND launch<>'".sql_empty()."'")) {
502 $params['gradetype'] = GRADE_TYPE_VALUE;
503 $params['grademax'] = $maxgrade;
504 $params['grademin'] = 0;
505 } else {
506 $params['gradetype'] = GRADE_TYPE_NONE;
508 } else {
509 $params['gradetype'] = GRADE_TYPE_VALUE;
510 $params['grademax'] = $scorm->maxgrade;
511 $params['grademin'] = 0;
514 if ($grades === 'reset') {
515 $params['reset'] = true;
516 $grades = NULL;
519 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
523 * Delete grade item for given scorm
525 * @param object $scorm object
526 * @return object grade_item
528 function scorm_grade_item_delete($scorm) {
529 global $CFG;
530 require_once($CFG->libdir.'/gradelib.php');
532 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
535 function scorm_get_view_actions() {
536 return array('pre-view','view','view all','report');
539 function scorm_get_post_actions() {
540 return array();
543 function scorm_option2text($scorm) {
544 global $SCORM_POPUP_OPTIONS;
545 if (isset($scorm->popup)) {
546 if ($scorm->popup == 1) {
547 $optionlist = array();
548 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
549 if (isset($scorm->$name)) {
550 $optionlist[] = $name.'='.$scorm->$name;
551 } else {
552 $optionlist[] = $name.'=0';
555 $scorm->options = implode(',', $optionlist);
556 } else {
557 $scorm->options = '';
559 } else {
560 $scorm->popup = 0;
561 $scorm->options = '';
563 return $scorm;
567 * Implementation of the function for printing the form elements that control
568 * whether the course reset functionality affects the scorm.
569 * @param $mform form passed by reference
571 function scorm_reset_course_form_definition(&$mform) {
572 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
573 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
577 * Course reset form defaults.
579 function scorm_reset_course_form_defaults($course) {
580 return array('reset_scorm'=>1);
584 * Removes all grades from gradebook
585 * @param int $courseid
586 * @param string optional type
588 function scorm_reset_gradebook($courseid, $type='') {
589 global $CFG;
591 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
592 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
593 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=$courseid";
595 if ($scorms = get_records_sql($sql)) {
596 foreach ($scorms as $scorm) {
597 scorm_grade_item_update($scorm, 'reset');
603 * Actual implementation of the rest coures functionality, delete all the
604 * scorm attempts for course $data->courseid.
605 * @param $data the data submitted from the reset course.
606 * @return array status array
608 function scorm_reset_userdata($data) {
609 global $CFG;
611 $componentstr = get_string('modulenameplural', 'scorm');
612 $status = array();
614 if (!empty($data->reset_scorm)) {
615 $scormssql = "SELECT s.id
616 FROM {$CFG->prefix}scorm s
617 WHERE s.course={$data->courseid}";
619 delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)");
621 // remove all grades from gradebook
622 if (empty($data->reset_gradebook_grades)) {
623 scorm_reset_gradebook($data->courseid);
626 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
629 // no dates to shift here
631 return $status;
635 * Returns all other caps used in module
637 function scorm_get_extra_capabilities() {
638 return array('moodle/site:accessallgroups');