Possible fix to MDL-12093 : give window without heading and navigation special css...
[moodle.git] / mod / scorm / lib.php
CommitLineData
e4aa175a 1<?php // $Id$
2
e4aa175a 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.
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 {
64 error(get_string('badpackage','scorm'));
e4aa175a 65 }
66}
67
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.
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")){
260 $level=0;
261 $sublist=1;
262 $parents[$level]='/';
263 foreach ($scoes as $sco) {
264 if ($parents[$level]!=$sco->parent) {
265 if ($level>0 && $parents[$level-1]==$sco->parent) {
266 $report .= "\t\t</ul></li>\n";
267 $level--;
268 } else {
269 $i = $level;
270 $closelist = '';
271 while (($i > 0) && ($parents[$level] != $sco->parent)) {
272 $closelist .= "\t\t</ul></li>\n";
273 $i--;
274 }
275 if (($i == 0) && ($sco->parent != $currentorg)) {
276 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
277 $level++;
278 } else {
279 $report .= $closelist;
280 $level = $i;
281 }
282 $parents[$level]=$sco->parent;
283 }
284 }
285 $report .= "\t\t<li>";
286 $nextsco = next($scoes);
287 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
288 $sublist++;
289 } else {
7150b8ae 290 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
e4aa175a 291 }
292
293 if ($sco->launch) {
294 require_once('locallib.php');
295 $score = '';
296 $totaltime = '';
297 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
298 if ($usertrack->status == '') {
299 $usertrack->status = 'notattempted';
300 }
301 $strstatus = get_string($usertrack->status,'scorm');
302 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
303 if ($usertrack->timemodified != 0) {
304 if ($usertrack->timemodified > $lastmodify) {
305 $lastmodify = $usertrack->timemodified;
306 }
307 if ($usertrack->timemodified < $firstmodify) {
308 $firstmodify = $usertrack->timemodified;
309 }
310 }
311 } else {
312 if ($sco->scormtype == 'sco') {
313 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
314 } else {
315 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
316 }
317 }
318 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
319 if ($usertrack !== false) {
320 $sometoreport = true;
321 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
322 foreach($usertrack as $element => $value) {
323 if (substr($element,0,3) == 'cmi') {
324 $report .= '<li>'.$element.' => '.$value.'</li>';
325 }
326 }
327 $report .= "\t\t\t</ul></li>\n";
328 }
329 } else {
330 $report .= "&nbsp;$sco->title</li>\n";
331 }
332 }
333 for ($i=0;$i<$level;$i++) {
334 $report .= "\t\t</ul></li>\n";
335 }
336 }
337 $report .= "\t</ul><br />\n";
338 }
339 $report .= "</div>\n";
340 }
341 if ($sometoreport) {
342 if ($firstmodify < $now) {
343 $timeago = format_time($now - $firstmodify);
344 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
345 }
346 if ($lastmodify > 0) {
347 $timeago = format_time($now - $lastmodify);
348 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
349 }
350 echo get_string('report','scorm').":<br />\n";
351 echo $report;
352 } else {
dabfd0ed 353 print_string('noactivity','scorm');
e4aa175a 354 }
355
356 return true;
357}
358
359/**
360* Given a list of logs, assumed to be those since the last login
361* this function prints a short list of changes related to this module
362* If isteacher is true then perhaps additional information is printed.
363* This function is called from course/lib.php: print_recent_activity()
364*
365* @param reference $logs Logs reference
366* @param boolean $isteacher
367* @return boolean
368*/
369function scorm_print_recent_activity(&$logs, $isteacher=false) {
370 return false; // True if anything was printed, otherwise false
371}
372
373/**
374* Function to be run periodically according to the moodle cron
375* This function searches for things that need to be done, such
376* as sending out mail, toggling flags etc ...
377*
378* @return boolean
379*/
380function scorm_cron () {
381
382 global $CFG;
a679d64d 383
384 require_once('locallib.php');
385
386 $sitetimezone = $CFG->timezone;
387 /// Now see if there are any digest mails waiting to be sent, and if we should send them
bfe8c2f0 388 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
389 set_config('scorm_updatetimelast', 0);
390 }
391
a679d64d 392 $timenow = time();
bfe8c2f0 393 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
a679d64d 394
395 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
396
bfe8c2f0 397 set_config('scorm_updatetimelast', $timenow);
e4aa175a 398
376c9c70 399 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 400
a679d64d 401 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
402 if (!empty($scormsupdate)) {
403 foreach($scormsupdate as $scormupdate) {
404 $scormupdate->instance = $scormupdate->id;
405 $id = scorm_update_instance($scormupdate);
406 }
407 }
408 }
409
e4aa175a 410 return true;
411}
412
413/**
531fa830 414 * Return grade for given user or all users.
415 *
416 * @param int $scormid id of scorm
417 * @param int $userid optional user id, 0 means all users
418 * @return array array of grades, false if none
419 */
420function scorm_get_user_grades($scorm, $userid=0) {
e4aa175a 421 global $CFG;
531fa830 422 require_once('locallib.php');
423
424 $grades = array();
425 if (empty($userid)) {
426 if ($scousers = get_records_select('scorm_scoes_track', "scormid='$scorm->id' GROUP BY userid", "", "userid,null")) {
427 foreach ($scousers as $scouser) {
428 $grades[$scouser->userid] = new object();
429 $grades[$scouser->userid]->id = $scouser->userid;
430 $grades[$scouser->userid]->userid = $scouser->userid;
431 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
432 }
433 } else {
434 return false;
435 }
e4aa175a 436
531fa830 437 } else {
438 if (!get_records_select('scorm_scoes_track', "scormid='$scorm->id' AND userid='$userid' GROUP BY userid", "", "userid,null")) {
439 return false; //no attempt yet
440 }
441 $grades[$userid] = new object();
442 $grades[$userid]->id = $userid;
443 $grades[$userid]->userid = $userid;
444 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
e4aa175a 445 }
e4aa175a 446
531fa830 447 return $grades;
448}
449
450/**
451 * Update grades in central gradebook
452 *
453 * @param object $scorm null means all scormbases
454 * @param int $userid specific user only, 0 mean all
455 */
456function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
457 global $CFG;
458 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
459 require_once($CFG->libdir.'/gradelib.php');
460 }
461
462 if ($scorm != null) {
463 if ($grades = scorm_get_user_grades($scorm, $userid)) {
d23121ab 464 grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades[$userid]);
531fa830 465
466 } else if ($userid and $nullifnone) {
467 $grade = new object();
468 $grade->userid = $userid;
469 $grade->rawgrade = NULL;
470 grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grade);
e4aa175a 471 }
531fa830 472
e4aa175a 473 } else {
531fa830 474 $sql = "SELECT s.*, cm.idnumber as cmidnumber
475 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
476 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
477 if ($rs = get_recordset_sql($sql)) {
03cedd62 478 while ($scorm = rs_fetch_next_record($rs)) {
479 scorm_grade_item_update($scorm);
480 scorm_update_grades($scorm, 0, false);
531fa830 481 }
482 rs_close($rs);
483 }
e4aa175a 484 }
531fa830 485}
e4aa175a 486
531fa830 487/**
488 * Update/create grade item for given scorm
489 *
490 * @param object $scorm object with extra cmidnumber
491 * @return object grade_item
492 */
493function scorm_grade_item_update($scorm) {
494 global $CFG;
495 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
496 require_once($CFG->libdir.'/gradelib.php');
497 }
498
499 $params = array('itemname'=>$scorm->name, 'idnumber'=>$scorm->cmidnumber);
500
501 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
502 if ($maxgrade = count_records_select('scorm_scoes',"scorm='$scorm->id' AND launch<>''")) {
503 $params['gradetype'] = GRADE_TYPE_VALUE;
504 $params['grademax'] = $maxgrade;
505 $params['grademin'] = 0;
506 } else {
507 $params['gradetype'] = GRADE_TYPE_NONE;
e4aa175a 508 }
531fa830 509 } else {
510 $params['gradetype'] = GRADE_TYPE_VALUE;
511 $params['grademax'] = $scorm->maxgrade;
512 $params['grademin'] = 0;
e4aa175a 513 }
531fa830 514
515 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, $params);
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
5c1ac70c 562?>