more fixes for lockoptions behaviour; changes to forum/mod_form.php; removed forum...
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
a30b6819 3/// Constants and settings for module scorm
4
5define('GRADESCOES', '0');
6define('GRADEHIGHEST', '1');
7define('GRADEAVERAGE', '2');
8define('GRADESUM', '3');
9$SCORM_GRADE_METHOD = array (GRADESCOES => get_string('gradescoes', 'scorm'),
10 GRADEHIGHEST => get_string('gradehighest', 'scorm'),
11 GRADEAVERAGE => get_string('gradeaverage', 'scorm'),
12 GRADESUM => get_string('gradesum', 'scorm'));
13
14define('HIGHESTATTEMPT', '0');
15define('AVERAGEATTEMPT', '1');
16define('FIRSTATTEMPT', '2');
17define('LASTATTEMPT', '3');
18$SCORM_WHAT_GRADE = array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'),
19 AVERAGEATTEMPT => get_string('averageattempt', 'scorm'),
20 FIRSTATTEMPT => get_string('firstattempt', 'scorm'),
21 LASTATTEMPT => get_string('lastattempt', 'scorm'));
22
23$SCORM_POPUP_OPTIONS = array('resizable'=>1,
24 'scrollbars'=>1,
25 'directories'=>0,
26 'location'=>0,
27 'menubar'=>0,
28 'toolbar'=>0,
29 'status'=>0);
30$stdoptions = '';
31foreach ($SCORM_POPUP_OPTIONS as $popupopt => $value) {
32 $stdoptions .= $popupopt.'='.$value;
33 if ($popupopt != 'status') {
34 $stdoptions .= ',';
35 }
36}
37
38if (!isset($CFG->scorm_maxattempts)) {
39 set_config('scorm_maxattempts','6');
40}
41
42if (!isset($CFG->scorm_frameheight)) {
43 set_config('scorm_frameheight','500');
44}
45
46if (!isset($CFG->scorm_framewidth)) {
47 set_config('scorm_framewidth','100%');
48}
49
50if (!isset($CFG->scorm_advancedsettings)) {
51 set_config('scorm_advancedsettings','0');
52}
53
54if (!isset($CFG->scorm_windowsettings)) {
55 set_config('scorm_windowsettings','0');
56}
57
58//
59// Repository configurations
60//
61$repositoryconfigfile = $CFG->dirroot.'/mod/resource/type/ims/repository_config.php';
62$repositorybrowser = '/mod/resource/type/ims/finder.php';
63
2b3447c3 64/// Local Library of functions for module scorm
f69db63e 65
5c1ac70c 66/**
67* This function will permanently delete the given
68* directory and all files and subdirectories.
69*
70* @param string $directory The directory to remove
71* @return boolean
72*/
73function scorm_delete_files($directory) {
74 if (is_dir($directory)) {
75 $files=scorm_scandir($directory);
76 foreach($files as $file) {
77 if (($file != '.') && ($file != '..')) {
78 if (!is_dir($directory.'/'.$file)) {
79 unlink($directory.'/'.$file);
80 } else {
81 scorm_delete_files($directory.'/'.$file);
82 }
f69db63e 83 }
5c1ac70c 84 set_time_limit(5);
f69db63e 85 }
5c1ac70c 86 rmdir($directory);
87 return true;
f69db63e 88 }
5c1ac70c 89 return false;
f69db63e 90}
91
5c1ac70c 92/**
93* Given a diretory path returns the file list
94*
95* @param string $directory
96* @return array
97*/
98function scorm_scandir($directory) {
99 if (version_compare(phpversion(),'5.0.0','>=')) {
100 return scandir($directory);
f69db63e 101 } else {
5c1ac70c 102 $files = array();
103 if ($dh = opendir($directory)) {
104 while (($file = readdir($dh)) !== false) {
105 $files[] = $file;
106 }
107 closedir($dh);
108 }
109 return $files;
f69db63e 110 }
f69db63e 111}
112
e4aa175a 113/**
114* Create a new temporary subdirectory with a random name in the given path
115*
116* @param string $strpath The scorm data directory
117* @return string/boolean
118*/
119function scorm_datadir($strPath)
120{
121 global $CFG;
122
123 if (is_dir($strPath)) {
124 do {
125 // Create a random string of 8 chars
126 $randstring = NULL;
127 $lchar = '';
128 $len = 8;
129 for ($i=0; $i<$len; $i++) {
130 $char = chr(rand(48,122));
131 while (!ereg('[a-zA-Z0-9]', $char)){
132 if ($char == $lchar) continue;
133 $char = chr(rand(48,90));
134 }
135 $randstring .= $char;
136 $lchar = $char;
137 }
138 $datadir='/'.$randstring;
139 } while (file_exists($strPath.$datadir));
140 mkdir($strPath.$datadir, $CFG->directorypermissions);
141 @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
142 return $strPath.$datadir;
143 } else {
144 return false;
145 }
146}
147
2b3447c3 148function scorm_array_search($item, $needle, $haystacks, $strict=false) {
149 if (!empty($haystacks)) {
150 foreach ($haystacks as $key => $element) {
151 if ($strict) {
152 if ($element->{$item} === $needle) {
153 return $key;
154 }
155 } else {
156 if ($element->{$item} == $needle) {
157 return $key;
e4aa175a 158 }
159 }
e4aa175a 160 }
161 }
2b3447c3 162 return false;
e4aa175a 163}
164
2b3447c3 165function scorm_repeater($what, $times) {
166 if ($times <= 0) {
167 return null;
168 }
169 $return = '';
170 for ($i=0; $i<$times;$i++) {
171 $return .= $what;
172 }
173 return $return;
174}
e4aa175a 175
2b3447c3 176function scorm_external_link($link) {
177// check if a link is external
178 $result = false;
179 $link = strtolower($link);
180 if (substr($link,0,7) == 'http://') {
181 $result = true;
182 } else if (substr($link,0,8) == 'https://') {
183 $result = true;
184 } else if (substr($link,0,4) == 'www.') {
185 $result = true;
186 }
187 return $result;
e4aa175a 188}
189
2b3447c3 190/**
191* Given a package directory, this function will check if the package is valid
192*
193* @param string $packagedir The package directory
194* @return mixed
195*/
196function scorm_validate($packagedir) {
197 $validation = new stdClass();
198 if (is_file($packagedir.'/imsmanifest.xml')) {
199 $validation->result = 'found';
200 $validation->pkgtype = 'SCORM';
201 } else {
202 if ($handle = opendir($packagedir)) {
203 while (($file = readdir($handle)) !== false) {
204 $ext = substr($file,strrpos($file,'.'));
205 if (strtolower($ext) == '.cst') {
206 $validation->result = 'found';
207 $validation->pkgtype = 'AICC';
208 break;
e4aa175a 209 }
e4aa175a 210 }
2b3447c3 211 closedir($handle);
212 }
213 if (!isset($validation->result)) {
214 $validation->result = 'nomanifest';
215 $validation->pkgtype = 'SCORM';
e4aa175a 216 }
e4aa175a 217 }
2b3447c3 218 return $validation;
e4aa175a 219}
220
e4aa175a 221function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
e4aa175a 222 $id = null;
223 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
224 $track->value = $value;
225 $track->timemodified = time();
e4aa175a 226 $id = update_record('scorm_scoes_track',$track);
227 } else {
228 $track->userid = $userid;
229 $track->scormid = $scormid;
230 $track->scoid = $scoid;
231 $track->attempt = $attempt;
232 $track->element = $element;
233 $track->value = addslashes($value);
234 $track->timemodified = time();
e4aa175a 235 $id = insert_record('scorm_scoes_track',$track);
236 }
237 return $id;
238}
239
e4aa175a 240function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 241/// Gets all tracks of specified sco and user
242 global $CFG;
243
244 if (empty($attempt)) {
245 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
246 $attempt = scorm_get_last_attempt($scormid,$userid);
247 } else {
248 $attempt = 1;
249 }
250 }
251 $attemptsql = ' AND attempt=' . $attempt;
252 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
253 $usertrack->userid = $userid;
254 $usertrack->scoid = $scoid;
a30b6819 255 // Defined in order to unify scorm1.2 and scorm2004
e4aa175a 256 $usertrack->score_raw = '';
e4aa175a 257 $usertrack->status = '';
e4aa175a 258 $usertrack->total_time = '00:00:00';
259 $usertrack->session_time = '00:00:00';
260 $usertrack->timemodified = 0;
261 foreach ($tracks as $track) {
262 $element = $track->element;
263 $usertrack->{$element} = $track->value;
264 switch ($element) {
f69db63e 265 case 'cmi.core.lesson_status':
266 case 'cmi.completion_status':
267 if ($track->value == 'not attempted') {
268 $track->value = 'notattempted';
269 }
270 $usertrack->status = $track->value;
271 break;
e4aa175a 272 case 'cmi.core.score.raw':
273 case 'cmi.score.raw':
274 $usertrack->score_raw = $track->value;
275 break;
e4aa175a 276 case 'cmi.core.session_time':
277 case 'cmi.session_time':
278 $usertrack->session_time = $track->value;
279 break;
280 case 'cmi.core.total_time':
281 case 'cmi.total_time':
282 $usertrack->total_time = $track->value;
283 break;
284 }
285 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
286 $usertrack->timemodified = $track->timemodified;
287 }
288 }
289 return $usertrack;
290 } else {
291 return false;
292 }
293}
294
2b3447c3 295function scorm_get_user_data($userid) {
296/// Gets user info required to display the table of scorm results
297/// for report.php
e4aa175a 298
2b3447c3 299 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
300}
e4aa175a 301
a30b6819 302function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
303 $attemptscore = NULL;
304 $attemptscore->scoes = 0;
305 $attemptscore->values = 0;
306 $attemptscore->max = 0;
307 $attemptscore->sum = 0;
308 $attemptscore->lastmodify = 0;
309
310 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
311 return NULL;
e4aa175a 312 }
e4aa175a 313
a30b6819 314 $grademethod = $scorm->grademethod % 10;
315
2b3447c3 316 foreach ($scoes as $sco) {
317 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
318 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
a30b6819 319 $attemptscore->scoes++;
2b3447c3 320 }
321 if (!empty($userdata->score_raw)) {
a30b6819 322 $attemptscore->values++;
323 $attemptscore->sum += $userdata->score_raw;
324 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
325 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
326 $attemptscore->lastmodify = $userdata->timemodified;
327 } else {
328 $attemptscore->lastmodify = 0;
329 }
2b3447c3 330 }
331 }
e4aa175a 332 }
2b3447c3 333 switch ($grademethod) {
a30b6819 334 case GRADEHIGHEST:
335 $score = $attemptscore->max;
2b3447c3 336 break;
a30b6819 337 case GRADEAVERAGE:
338 if ($attemptscore->values > 0) {
339 $score = $attemptscore->sum/$attemptscore->values;
5c1ac70c 340 } else {
a30b6819 341 $score = 0;
2b3447c3 342 }
343 break;
a30b6819 344 case GRADESUM:
345 $score = $attemptscore->sum;
2b3447c3 346 break;
a30b6819 347 case GRADESCOES:
348 $score = $attemptscore->scoes;
2b3447c3 349 break;
5c1ac70c 350 }
a30b6819 351
352 if ($time) {
353 $result = new stdClass();
354 $result->score = $score;
355 $result->time = $attemptscore->lastmodify;
356 } else {
357 $result = $score;
358 }
359
360 return $result;
361}
362
363function scorm_grade_user($scorm, $userid, $time=false) {
364
365 $whatgrade = intval($scorm->grademethod / 10);
366
367 switch ($whatgrade) {
368 case FIRSTATTEMPT:
369 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
370 break;
371 case LASTATTEMPT:
372 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
373 break;
374 case HIGHESTATTEMPT:
375 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
376 $maxscore = 0;
377 $attempttime = 0;
378 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
379 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
380 if ($time) {
381 if ($attemptscore->score > $maxscore) {
382 $maxscore = $attemptscore->score;
383 $attempttime = $attemptscore->time;
384 }
385 } else {
386 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
387 }
388 }
389 if ($time) {
390 $result = new stdClass();
391 $result->score = $maxscore;
392 $result->time = $attempttime;
393 return $result;
394 } else {
395 return $maxscore;
396 }
397 break;
398 case AVERAGEATTEMPT:
399 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
400 $sumscore = 0;
401 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
402 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
403 if ($time) {
404 $sumscore += $attemptscore->score;
405 } else {
406 $sumscore += $attemptscore;
407 }
408 }
409
410 if ($lastattempt > 0) {
411 $score = $sumscore / $lastattempt;
412 } else {
413 $score = 0;
414 }
415
416 if ($time) {
417 $result = new stdClass();
418 $result->score = $score;
419 $result->time = $attemptscore->time;
420 return $result;
421 } else {
422 return $score;
423 }
424 break;
425 }
e4aa175a 426}
427
8e45ba45 428function scorm_count_launchable($scormid,$organization='') {
429 $strorganization = '';
430 if (!empty($organization)) {
431 $strorganization = " AND organization='$organization'";
432 }
433 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>''");
e4aa175a 434}
435
2b3447c3 436function scorm_get_last_attempt($scormid, $userid) {
437/// Find the last attempt number for the given user id and scorm id
438 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
439 if (empty($lastattempt->a)) {
440 return '1';
441 } else {
442 return $lastattempt->a;
e4aa175a 443 }
444 }
e4aa175a 445}
446
e4aa175a 447function scorm_course_format_display($user,$course) {
448 global $CFG;
449
450 $strupdate = get_string('update');
451 $strmodule = get_string('modulename','scorm');
77bf0c29 452 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 453
454 echo '<div class="mod-scorm">';
455 if ($scorms = get_all_instances_in_course('scorm', $course)) {
456 // The module SCORM activity with the least id is the course
457 $scorm = current($scorms);
458 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
459 error("Course Module ID was incorrect");
460 }
461 $colspan = '';
462 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 463 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 464 if (isediting($course->id)) {
465 // Display update icon
466 $path = $CFG->wwwroot.'/course';
467 $headertext .= '<span class="commands">'.
468 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
469 '<img src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" alt="'.$strupdate.'" /></a></span>';
470 }
471 $headertext .= '</td>';
472 // Display report link
473 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id, '', '', '', '', 'count(distinct(userid)) as c');
474 if ($trackedusers->c > 0) {
475 $headertext .= '<td class="reportlink">'.
476 '<a target="'.$CFG->framename.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
477 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
478 } else {
479 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
480 }
481 $colspan = ' colspan="2"';
482 }
483 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
484 print_simple_box($headertext,'','100%');
485 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
486 } else {
0d699c24 487 if (has_capability('moodle/course:update', $context)) {
e4aa175a 488 // Create a new activity
2b3447c3 489 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 490 } else {
491 notify('Could not find a scorm course here');
492 }
493 }
494 echo '</div>';
495}
496
2b3447c3 497function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
e4aa175a 498 global $CFG;
5c1ac70c 499
e4aa175a 500 $organization = optional_param('organization', '', PARAM_INT);
501
2b3447c3 502 print_simple_box_start('center',$boxwidth);
e4aa175a 503?>
504 <div class="structurehead"><?php print_string('coursestruct','scorm') ?></div>
505<?php
506 if (empty($organization)) {
507 $organization = $scorm->launch;
508 }
509 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
510 if (count($orgs) > 1) {
511 ?>
512 <div class='center'>
513 <?php print_string('organizations','scorm') ?>
514 <form name='changeorg' method='post' action='<?php echo $action ?>'>
515 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
516 </form>
517 </div>
518<?php
519 }
520 }
521 $orgidentifier = '';
522 if ($org = get_record('scorm_scoes','id',$organization)) {
523 if (($org->organization == '') && ($org->launch == '')) {
524 $orgidentifier = $org->identifier;
525 } else {
526 $orgidentifier = $org->organization;
527 }
528 }
2b3447c3 529 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
530 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
531
e4aa175a 532 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
533 $incomplete = $result->incomplete;
e4aa175a 534 echo $result->toc;
e4aa175a 535 print_simple_box_end();
536?>
537 <div class="center">
538 <form name="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?id=<?php echo $cm->id ?>"<?php echo $scorm->popup == 1?' target="newwin"':'' ?>>
539 <?php
e4aa175a 540 if ($scorm->hidebrowse == 0) {
541 print_string("mode","scorm");
542 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
543 if ($incomplete === true) {
544 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
a30b6819 545 } else {
546 echo '<input type="radio" id="r" name="mode" value="review" checked="checked" /><label for="r">'.get_string('review','scorm')."</label>\n";
e4aa175a 547 }
548 } else {
549 if ($incomplete === true) {
550 echo '<input type="hidden" name="mode" value="normal" />'."\n";
551 } else {
552 echo '<input type="hidden" name="mode" value="review" />'."\n";
553 }
554 }
555 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
556?>
557 <br />
558 <input type="checkbox" id="a" name="newattempt" />
559 <label for="a"><?php print_string('newattempt','scorm') ?></label>
560<?php
561 }
562 ?>
563 <br />
564 <input type="hidden" name="scoid" />
565 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
566 <input type="submit" value="<?php print_string('entercourse','scorm') ?>" />
567 </form>
568 </div>
569<?php
570}
571
8e45ba45 572function scorm_simple_play($scorm,$user) {
573 $result = false;
574 $scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>""');
575 if (count($scoes) == 1) {
576 if ($scorm->skipview >= 1) {
577 $sco = current($scoes);
578 if (scorm_get_tracks($sco->id,$user->id) === false) {
579 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
580 $result = true;
581 } else if ($scorm->skipview == 2) {
582 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
583 $result = true;
584 }
585 }
586 }
587 return $result;
588}
589
a30b6819 590function scorm_parse($scorm) {
591 global $CFG,$repositoryconfigfile;
592
593 // Parse scorm manifest
594 if ($scorm->pkgtype == 'AICC') {
595 require_once('datamodels/aicclib.php');
596 $scorm->launch = scorm_parse_aicc($scorm->dir.'/'.$scorm->id,$scorm->id);
597 } else {
598 require_once('datamodels/scormlib.php');
599 $reference = $scorm->reference;
600 if ($scorm->reference[0] == '#') {
601 require_once($repositoryconfigfile);
602 $reference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
603 } else if (substr($reference,0,7) != 'http://') {
604 $reference = $CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference;
605 }
606
607 if (basename($reference) != 'imsmanifest.xml') {
608 $scorm->launch = scorm_parse_scorm($scorm->dir.'/'.$scorm->id,$scorm->id);
609 } else {
610 $scorm->launch = scorm_parse_scorm(dirname($reference),$scorm->id);
611 }
612 }
613 return $scorm->launch;
614}
615
e4aa175a 616?>