"MDL-12304, fix double text"
[moodle-linuxchix.git] / mod / scorm / lib.php
blob3ba0bc0c4eafe0b60d44aa773fce67c37f35fe66
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)) {
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));
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));
130 return $result;
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.
138 * @param int $id Scorm instance id
139 * @return boolean
141 function scorm_delete_instance($id) {
143 global $CFG;
145 if (! $scorm = get_record('scorm', 'id', $id)) {
146 return false;
149 $result = true;
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);
158 // Delete any dependent records
159 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
160 $result = false;
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;
168 delete_records('scorm_scoes', 'scorm', $scorm->id);
169 } else {
170 $result = false;
172 if (! delete_records('scorm', 'id', $scorm->id)) {
173 $result = false;
176 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
177 $result = false;
179 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
180 $result = false;
182 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
183 $result = false;
185 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
186 $result = false;
188 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
189 $result = false;
191 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
192 $result = false;
194 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
195 $result = false;
196 }*/
198 scorm_grade_item_delete(stripslashes_recursive($scorm));
200 return $result;
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.
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
214 function scorm_user_outline($course, $user, $mod, $scorm) {
215 global $CFG;
216 require_once('locallib.php');
218 $return = scorm_grade_user($scorm, $user->id, true);
220 return $return;
224 * Print a detailed representation of what a user has done with
225 * a given particular instance of this module, for user activity reports.
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
233 function scorm_user_complete($course, $user, $mod, $scorm) {
234 global $CFG;
236 $liststyle = 'structlist';
237 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
238 $now = time();
239 $firstmodify = $now;
240 $lastmodify = 0;
241 $sometoreport = false;
242 $report = '';
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 = '';
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'";
258 $report .= "<ul id='0' class='$liststyle'>";
259 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
260 // drop keys so that we can access array sequentially
261 $scoes = array_values($scoes);
262 $level=0;
263 $sublist=1;
264 $parents[$level]='/';
265 foreach ($scoes as $pos=>$sco) {
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--;
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;
284 $parents[$level]=$sco->parent;
287 $report .= "\t\t<li>";
288 if (isset($scoes[$pos+1])) {
289 $nextsco = $scoes[$pos+1];
290 } else {
291 $nextsco = false;
293 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
294 $sublist++;
295 } else {
296 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
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';
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;
313 if ($usertrack->timemodified < $firstmodify) {
314 $firstmodify = $usertrack->timemodified;
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').'" />';
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>';
333 $report .= "\t\t\t</ul></li>\n";
335 } else {
336 $report .= "&nbsp;$sco->title</li>\n";
339 for ($i=0;$i<$level;$i++) {
340 $report .= "\t\t</ul></li>\n";
343 $report .= "\t</ul><br />\n";
345 $report .= "</div>\n";
347 if ($sometoreport) {
348 if ($firstmodify < $now) {
349 $timeago = format_time($now - $firstmodify);
350 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
352 if ($lastmodify > 0) {
353 $timeago = format_time($now - $lastmodify);
354 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
356 echo get_string('report','scorm').":<br />\n";
357 echo $report;
358 } else {
359 print_string('noactivity','scorm');
362 return true;
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 ...
370 * @return boolean
372 function scorm_cron () {
374 global $CFG;
376 require_once('locallib.php');
378 $sitetimezone = $CFG->timezone;
379 /// Now see if there are any digest mails waiting to be sent, and if we should send them
380 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
381 set_config('scorm_updatetimelast', 0);
384 $timenow = time();
385 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
387 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
389 set_config('scorm_updatetimelast', $timenow);
391 mtrace('Updating scorm packages which require daily update');//We are updating
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);
402 return true;
406 * Return grade for given user or all users.
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
412 function scorm_get_user_grades($scorm, $userid=0) {
413 global $CFG;
414 require_once('locallib.php');
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);
425 } else {
426 return false;
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
433 $grades[$userid] = new object();
434 $grades[$userid]->id = $userid;
435 $grades[$userid]->userid = $userid;
436 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
439 return $grades;
443 * Update grades in central gradebook
445 * @param object $scorm null means all scormbases
446 * @param int $userid specific user only, 0 mean all
448 function 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');
454 if ($scorm != null) {
455 if ($grades = scorm_get_user_grades($scorm, $userid)) {
456 scorm_grade_item_update($scorm, $grades);
458 } else if ($userid and $nullifnone) {
459 $grade = new object();
460 $grade->userid = $userid;
461 $grade->rawgrade = NULL;
462 scorm_grade_item_update($scorm, $grade);
464 } else {
465 scorm_grade_item_update($scorm);
468 } else {
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)) {
473 while ($scorm = rs_fetch_next_record($rs)) {
474 scorm_update_grades($scorm, 0, false);
476 rs_close($rs);
482 * Update/create grade item for given scorm
484 * @param object $scorm object with extra cmidnumber
485 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
486 * @return object grade_item
488 function scorm_grade_item_update($scorm, $grades=NULL) {
489 global $CFG;
490 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
491 require_once($CFG->libdir.'/gradelib.php');
494 $params = array('itemname'=>$scorm->name, 'idnumber'=>$scorm->cmidnumber);
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;
504 } else {
505 $params['gradetype'] = GRADE_TYPE_VALUE;
506 $params['grademax'] = $scorm->maxgrade;
507 $params['grademin'] = 0;
510 if ($grades === 'reset') {
511 $params['reset'] = true;
512 $grades = NULL;
515 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
519 * Delete grade item for given scorm
521 * @param object $scorm object
522 * @return object grade_item
524 function scorm_grade_item_delete($scorm) {
525 global $CFG;
526 require_once($CFG->libdir.'/gradelib.php');
528 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
531 function scorm_get_view_actions() {
532 return array('pre-view','view','view all','report');
535 function scorm_get_post_actions() {
536 return array();
539 function scorm_option2text($scorm) {
540 global $SCORM_POPUP_OPTIONS;
541 if (isset($scorm->popup)) {
542 if ($scorm->popup == 1) {
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';
551 $scorm->options = implode(',', $optionlist);
552 } else {
553 $scorm->options = '';
555 } else {
556 $scorm->popup = 0;
557 $scorm->options = '';
559 return $scorm;
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
567 function 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'));
573 * Course reset form defaults.
575 function scorm_reset_course_form_defaults($course) {
576 return array('reset_scorm'=>1);
580 * Removes all grades from gradebook
581 * @param int $courseid
582 * @param string optional type
584 function scorm_reset_gradebook($courseid, $type='') {
585 global $CFG;
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";
591 if ($scorms = get_records_sql($sql)) {
592 foreach ($scorms as $scorm) {
593 scorm_grade_item_update($scorm, 'reset');
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
604 function scorm_reset_userdata($data) {
605 global $CFG;
607 $componentstr = get_string('modulenameplural', 'scorm');
608 $status = array();
610 if (!empty($data->reset_scorm)) {
611 $scormssql = "SELECT s.id
612 FROM {$CFG->prefix}scorm s
613 WHERE s.course={$data->courseid}";
615 delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)");
617 // remove all grades from gradebook
618 if (empty($data->reset_gradebook_grades)) {
619 scorm_reset_gradebook($data->courseid);
622 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
625 // no dates to shift here
627 return $status;