Fix for reopened bug MDL-6685, "Arrows on course roles screen are displaying as squares"
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
a30b6819 3/// Constants and settings for module scorm
a679d64d 4define('UPDATE_NEVER', '0');
5define('UPDATE_ONCHANGE', '1');
6define('UPDATE_EVERYDAY', '2');
7define('UPDATE_EVERYTIME', '3');
8
b3659259 9define('SCO_ALL', 0);
10define('SCO_DATA', 1);
11define('SCO_ONLY', 2);
a30b6819 12
13define('GRADESCOES', '0');
14define('GRADEHIGHEST', '1');
15define('GRADEAVERAGE', '2');
16define('GRADESUM', '3');
17$SCORM_GRADE_METHOD = array (GRADESCOES => get_string('gradescoes', 'scorm'),
18 GRADEHIGHEST => get_string('gradehighest', 'scorm'),
19 GRADEAVERAGE => get_string('gradeaverage', 'scorm'),
20 GRADESUM => get_string('gradesum', 'scorm'));
21
22define('HIGHESTATTEMPT', '0');
23define('AVERAGEATTEMPT', '1');
24define('FIRSTATTEMPT', '2');
25define('LASTATTEMPT', '3');
26$SCORM_WHAT_GRADE = array (HIGHESTATTEMPT => get_string('highestattempt', 'scorm'),
27 AVERAGEATTEMPT => get_string('averageattempt', 'scorm'),
28 FIRSTATTEMPT => get_string('firstattempt', 'scorm'),
29 LASTATTEMPT => get_string('lastattempt', 'scorm'));
30
31$SCORM_POPUP_OPTIONS = array('resizable'=>1,
32 'scrollbars'=>1,
33 'directories'=>0,
34 'location'=>0,
35 'menubar'=>0,
36 'toolbar'=>0,
37 'status'=>0);
38$stdoptions = '';
39foreach ($SCORM_POPUP_OPTIONS as $popupopt => $value) {
40 $stdoptions .= $popupopt.'='.$value;
41 if ($popupopt != 'status') {
42 $stdoptions .= ',';
43 }
44}
45
46if (!isset($CFG->scorm_maxattempts)) {
47 set_config('scorm_maxattempts','6');
48}
49
50if (!isset($CFG->scorm_frameheight)) {
51 set_config('scorm_frameheight','500');
52}
53
54if (!isset($CFG->scorm_framewidth)) {
55 set_config('scorm_framewidth','100%');
56}
57
8aee93f1 58if (!isset($CFG->scorm_updatetime)) {
59 set_config('scorm_updatetime','2');
60}
61
a30b6819 62if (!isset($CFG->scorm_advancedsettings)) {
63 set_config('scorm_advancedsettings','0');
64}
65
66if (!isset($CFG->scorm_windowsettings)) {
67 set_config('scorm_windowsettings','0');
68}
69
70//
71// Repository configurations
72//
73$repositoryconfigfile = $CFG->dirroot.'/mod/resource/type/ims/repository_config.php';
74$repositorybrowser = '/mod/resource/type/ims/finder.php';
75
2b3447c3 76/// Local Library of functions for module scorm
f69db63e 77
5c1ac70c 78/**
79* This function will permanently delete the given
80* directory and all files and subdirectories.
81*
82* @param string $directory The directory to remove
83* @return boolean
84*/
85function scorm_delete_files($directory) {
86 if (is_dir($directory)) {
87 $files=scorm_scandir($directory);
88 foreach($files as $file) {
89 if (($file != '.') && ($file != '..')) {
90 if (!is_dir($directory.'/'.$file)) {
91 unlink($directory.'/'.$file);
92 } else {
93 scorm_delete_files($directory.'/'.$file);
94 }
f69db63e 95 }
5c1ac70c 96 set_time_limit(5);
f69db63e 97 }
5c1ac70c 98 rmdir($directory);
99 return true;
f69db63e 100 }
5c1ac70c 101 return false;
f69db63e 102}
103
5c1ac70c 104/**
105* Given a diretory path returns the file list
106*
107* @param string $directory
108* @return array
109*/
110function scorm_scandir($directory) {
111 if (version_compare(phpversion(),'5.0.0','>=')) {
112 return scandir($directory);
f69db63e 113 } else {
5c1ac70c 114 $files = array();
115 if ($dh = opendir($directory)) {
116 while (($file = readdir($dh)) !== false) {
117 $files[] = $file;
118 }
119 closedir($dh);
120 }
121 return $files;
f69db63e 122 }
f69db63e 123}
124
e4aa175a 125/**
126* Create a new temporary subdirectory with a random name in the given path
127*
128* @param string $strpath The scorm data directory
129* @return string/boolean
130*/
a679d64d 131function scorm_tempdir($strPath)
e4aa175a 132{
133 global $CFG;
134
135 if (is_dir($strPath)) {
136 do {
137 // Create a random string of 8 chars
138 $randstring = NULL;
139 $lchar = '';
140 $len = 8;
141 for ($i=0; $i<$len; $i++) {
142 $char = chr(rand(48,122));
143 while (!ereg('[a-zA-Z0-9]', $char)){
144 if ($char == $lchar) continue;
145 $char = chr(rand(48,90));
146 }
147 $randstring .= $char;
148 $lchar = $char;
149 }
150 $datadir='/'.$randstring;
151 } while (file_exists($strPath.$datadir));
152 mkdir($strPath.$datadir, $CFG->directorypermissions);
153 @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
154 return $strPath.$datadir;
155 } else {
156 return false;
157 }
158}
159
2b3447c3 160function scorm_array_search($item, $needle, $haystacks, $strict=false) {
161 if (!empty($haystacks)) {
162 foreach ($haystacks as $key => $element) {
163 if ($strict) {
164 if ($element->{$item} === $needle) {
165 return $key;
166 }
167 } else {
168 if ($element->{$item} == $needle) {
169 return $key;
e4aa175a 170 }
171 }
e4aa175a 172 }
173 }
2b3447c3 174 return false;
e4aa175a 175}
176
2b3447c3 177function scorm_repeater($what, $times) {
178 if ($times <= 0) {
179 return null;
180 }
181 $return = '';
182 for ($i=0; $i<$times;$i++) {
183 $return .= $what;
184 }
185 return $return;
186}
e4aa175a 187
2b3447c3 188function scorm_external_link($link) {
189// check if a link is external
190 $result = false;
191 $link = strtolower($link);
192 if (substr($link,0,7) == 'http://') {
193 $result = true;
194 } else if (substr($link,0,8) == 'https://') {
195 $result = true;
196 } else if (substr($link,0,4) == 'www.') {
197 $result = true;
198 }
199 return $result;
e4aa175a 200}
201
76ea4fb4 202function scorm_dirname($location) {
203 if (scorm_external_link($location)) {
204 return substr($location,0,strrpos($location,'/'));
2b3447c3 205 } else {
76ea4fb4 206 return dirname($location);
e4aa175a 207 }
e4aa175a 208}
209
a679d64d 210function scorm_basename($location) {
211 if (scorm_external_link($location)) {
212 return substr($location,strrpos($location,'/')+1);
213 } else {
214 return basename($location);
215 }
216}
217
b3659259 218/**
219* Returns an object containing all datas relative to the given sco ID
220*
221* @param integer $id The sco ID
222* @return mixed (false if sco id does not exists)
223*/
224function scorm_get_sco($id,$what=SCO_ALL) {
225 if ($sco = get_record('scorm_scoes','id',$id)) {
226 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
227 if (($what != SCO_ONLY) && ($scodatas = get_records('scorm_scoes_data','scoid',$id))) {
228 foreach ($scodatas as $scodata) {
229 $sco->{$scodata->name} = $scodata->value;
230 }
231 }
232 return $sco;
233 } else {
234 return false;
235 }
236}
237
e4aa175a 238function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
e4aa175a 239 $id = null;
240 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
241 $track->value = $value;
242 $track->timemodified = time();
e4aa175a 243 $id = update_record('scorm_scoes_track',$track);
244 } else {
245 $track->userid = $userid;
246 $track->scormid = $scormid;
247 $track->scoid = $scoid;
248 $track->attempt = $attempt;
249 $track->element = $element;
250 $track->value = addslashes($value);
251 $track->timemodified = time();
e4aa175a 252 $id = insert_record('scorm_scoes_track',$track);
253 }
254 return $id;
255}
256
e4aa175a 257function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 258/// Gets all tracks of specified sco and user
259 global $CFG;
260
261 if (empty($attempt)) {
262 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
263 $attempt = scorm_get_last_attempt($scormid,$userid);
264 } else {
265 $attempt = 1;
266 }
267 }
268 $attemptsql = ' AND attempt=' . $attempt;
269 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
270 $usertrack->userid = $userid;
271 $usertrack->scoid = $scoid;
a30b6819 272 // Defined in order to unify scorm1.2 and scorm2004
e4aa175a 273 $usertrack->score_raw = '';
e4aa175a 274 $usertrack->status = '';
e4aa175a 275 $usertrack->total_time = '00:00:00';
276 $usertrack->session_time = '00:00:00';
277 $usertrack->timemodified = 0;
278 foreach ($tracks as $track) {
279 $element = $track->element;
280 $usertrack->{$element} = $track->value;
281 switch ($element) {
f69db63e 282 case 'cmi.core.lesson_status':
283 case 'cmi.completion_status':
284 if ($track->value == 'not attempted') {
285 $track->value = 'notattempted';
286 }
287 $usertrack->status = $track->value;
288 break;
e4aa175a 289 case 'cmi.core.score.raw':
290 case 'cmi.score.raw':
291 $usertrack->score_raw = $track->value;
292 break;
e4aa175a 293 case 'cmi.core.session_time':
294 case 'cmi.session_time':
295 $usertrack->session_time = $track->value;
296 break;
297 case 'cmi.core.total_time':
298 case 'cmi.total_time':
299 $usertrack->total_time = $track->value;
300 break;
301 }
302 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
303 $usertrack->timemodified = $track->timemodified;
304 }
305 }
306 return $usertrack;
307 } else {
308 return false;
309 }
310}
311
2b3447c3 312function scorm_get_user_data($userid) {
313/// Gets user info required to display the table of scorm results
314/// for report.php
e4aa175a 315
2b3447c3 316 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
317}
e4aa175a 318
a30b6819 319function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
320 $attemptscore = NULL;
321 $attemptscore->scoes = 0;
322 $attemptscore->values = 0;
323 $attemptscore->max = 0;
324 $attemptscore->sum = 0;
325 $attemptscore->lastmodify = 0;
326
327 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
328 return NULL;
e4aa175a 329 }
e4aa175a 330
a30b6819 331 $grademethod = $scorm->grademethod % 10;
332
2b3447c3 333 foreach ($scoes as $sco) {
334 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
335 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
a30b6819 336 $attemptscore->scoes++;
2b3447c3 337 }
338 if (!empty($userdata->score_raw)) {
a30b6819 339 $attemptscore->values++;
340 $attemptscore->sum += $userdata->score_raw;
341 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
342 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
343 $attemptscore->lastmodify = $userdata->timemodified;
344 } else {
345 $attemptscore->lastmodify = 0;
346 }
2b3447c3 347 }
348 }
e4aa175a 349 }
2b3447c3 350 switch ($grademethod) {
a30b6819 351 case GRADEHIGHEST:
352 $score = $attemptscore->max;
2b3447c3 353 break;
a30b6819 354 case GRADEAVERAGE:
355 if ($attemptscore->values > 0) {
356 $score = $attemptscore->sum/$attemptscore->values;
5c1ac70c 357 } else {
a30b6819 358 $score = 0;
2b3447c3 359 }
360 break;
a30b6819 361 case GRADESUM:
362 $score = $attemptscore->sum;
2b3447c3 363 break;
a30b6819 364 case GRADESCOES:
365 $score = $attemptscore->scoes;
2b3447c3 366 break;
5c1ac70c 367 }
a30b6819 368
369 if ($time) {
370 $result = new stdClass();
371 $result->score = $score;
372 $result->time = $attemptscore->lastmodify;
373 } else {
374 $result = $score;
375 }
376
377 return $result;
378}
379
380function scorm_grade_user($scorm, $userid, $time=false) {
381
382 $whatgrade = intval($scorm->grademethod / 10);
383
384 switch ($whatgrade) {
385 case FIRSTATTEMPT:
386 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
387 break;
388 case LASTATTEMPT:
389 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
390 break;
391 case HIGHESTATTEMPT:
392 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
393 $maxscore = 0;
394 $attempttime = 0;
395 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
396 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
397 if ($time) {
398 if ($attemptscore->score > $maxscore) {
399 $maxscore = $attemptscore->score;
400 $attempttime = $attemptscore->time;
401 }
402 } else {
403 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
404 }
405 }
406 if ($time) {
407 $result = new stdClass();
408 $result->score = $maxscore;
409 $result->time = $attempttime;
410 return $result;
411 } else {
412 return $maxscore;
413 }
414 break;
415 case AVERAGEATTEMPT:
416 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
417 $sumscore = 0;
418 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
419 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
420 if ($time) {
421 $sumscore += $attemptscore->score;
422 } else {
423 $sumscore += $attemptscore;
424 }
425 }
426
427 if ($lastattempt > 0) {
428 $score = $sumscore / $lastattempt;
429 } else {
430 $score = 0;
431 }
432
433 if ($time) {
434 $result = new stdClass();
435 $result->score = $score;
436 $result->time = $attemptscore->time;
437 return $result;
438 } else {
439 return $score;
440 }
441 break;
442 }
e4aa175a 443}
444
8e45ba45 445function scorm_count_launchable($scormid,$organization='') {
446 $strorganization = '';
447 if (!empty($organization)) {
448 $strorganization = " AND organization='$organization'";
449 }
450 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>''");
e4aa175a 451}
452
2b3447c3 453function scorm_get_last_attempt($scormid, $userid) {
454/// Find the last attempt number for the given user id and scorm id
455 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
456 if (empty($lastattempt->a)) {
457 return '1';
458 } else {
459 return $lastattempt->a;
e4aa175a 460 }
461 }
e4aa175a 462}
463
e4aa175a 464function scorm_course_format_display($user,$course) {
465 global $CFG;
466
467 $strupdate = get_string('update');
468 $strmodule = get_string('modulename','scorm');
77bf0c29 469 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 470
471 echo '<div class="mod-scorm">';
472 if ($scorms = get_all_instances_in_course('scorm', $course)) {
473 // The module SCORM activity with the least id is the course
474 $scorm = current($scorms);
475 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
476 error("Course Module ID was incorrect");
477 }
478 $colspan = '';
479 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 480 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 481 if (isediting($course->id)) {
482 // Display update icon
483 $path = $CFG->wwwroot.'/course';
484 $headertext .= '<span class="commands">'.
485 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
0d905d9f 486 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
e4aa175a 487 }
488 $headertext .= '</td>';
489 // Display report link
490 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id, '', '', '', '', 'count(distinct(userid)) as c');
491 if ($trackedusers->c > 0) {
492 $headertext .= '<td class="reportlink">'.
fa738731 493 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
e4aa175a 494 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
495 } else {
496 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
497 }
498 $colspan = ' colspan="2"';
499 }
500 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
501 print_simple_box($headertext,'','100%');
502 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
503 } else {
0d699c24 504 if (has_capability('moodle/course:update', $context)) {
e4aa175a 505 // Create a new activity
2b3447c3 506 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 507 } else {
508 notify('Could not find a scorm course here');
509 }
510 }
511 echo '</div>';
512}
513
2b3447c3 514function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
e4aa175a 515 global $CFG;
ab3b00e1 516
517 if ($scorm->updatefreq == UPDATE_EVERYTIME){
518 $scorm->instance = $scorm->id;
519 scorm_update_instance($scorm);
520 }
521
e4aa175a 522 $organization = optional_param('organization', '', PARAM_INT);
523
2b3447c3 524 print_simple_box_start('center',$boxwidth);
e4aa175a 525?>
526 <div class="structurehead"><?php print_string('coursestruct','scorm') ?></div>
527<?php
528 if (empty($organization)) {
529 $organization = $scorm->launch;
530 }
531 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
532 if (count($orgs) > 1) {
533 ?>
534 <div class='center'>
535 <?php print_string('organizations','scorm') ?>
b7dc2256 536 <form id='changeorg' method='post' action='<?php echo $action ?>'>
e4aa175a 537 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
538 </form>
539 </div>
540<?php
541 }
542 }
543 $orgidentifier = '';
b3659259 544 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
545 if (($sco->organization == '') && ($sco->launch == '')) {
546 $orgidentifier = $sco->identifier;
e4aa175a 547 } else {
b3659259 548 $orgidentifier = $sco->organization;
e4aa175a 549 }
550 }
2b3447c3 551 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
dbe7e6f6 552 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
553 $scorm->version = 'scorm_12';
554 }
2b3447c3 555 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
556
e4aa175a 557 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
558 $incomplete = $result->incomplete;
e4aa175a 559 echo $result->toc;
e4aa175a 560 print_simple_box_end();
561?>
562 <div class="center">
b7dc2256 563 <form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?id=<?php echo $cm->id ?>"<?php echo $scorm->popup == 1?' target="newwin"':'' ?>>
e4aa175a 564 <?php
e4aa175a 565 if ($scorm->hidebrowse == 0) {
566 print_string("mode","scorm");
567 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
76ea4fb4 568 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
e4aa175a 569 } else {
76ea4fb4 570 echo '<input type="hidden" name="mode" value="normal" />'."\n";
e4aa175a 571 }
572 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
573?>
574 <br />
575 <input type="checkbox" id="a" name="newattempt" />
576 <label for="a"><?php print_string('newattempt','scorm') ?></label>
577<?php
578 }
579 ?>
580 <br />
581 <input type="hidden" name="scoid" />
582 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
583 <input type="submit" value="<?php print_string('entercourse','scorm') ?>" />
584 </form>
585 </div>
586<?php
587}
588
8e45ba45 589function scorm_simple_play($scorm,$user) {
590 $result = false;
76ea4fb4 591 if ($scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>""')) {
592 if (count($scoes) == 1) {
593 if ($scorm->skipview >= 1) {
594 $sco = current($scoes);
595 if (scorm_get_tracks($sco->id,$user->id) === false) {
596 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
597 $result = true;
598 } else if ($scorm->skipview == 2) {
599 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
600 $result = true;
601 }
8e45ba45 602 }
603 }
604 }
605 return $result;
606}
607
a30b6819 608function scorm_parse($scorm) {
609 global $CFG,$repositoryconfigfile;
610
611 // Parse scorm manifest
612 if ($scorm->pkgtype == 'AICC') {
613 require_once('datamodels/aicclib.php');
614 $scorm->launch = scorm_parse_aicc($scorm->dir.'/'.$scorm->id,$scorm->id);
615 } else {
616 require_once('datamodels/scormlib.php');
617 $reference = $scorm->reference;
618 if ($scorm->reference[0] == '#') {
619 require_once($repositoryconfigfile);
620 $reference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
621 } else if (substr($reference,0,7) != 'http://') {
622 $reference = $CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference;
623 }
624
625 if (basename($reference) != 'imsmanifest.xml') {
626 $scorm->launch = scorm_parse_scorm($scorm->dir.'/'.$scorm->id,$scorm->id);
627 } else {
76ea4fb4 628 $scorm->launch = scorm_parse_scorm(scorm_dirname($reference),$scorm->id);
a30b6819 629 }
630 }
631 return $scorm->launch;
632}
633
76ea4fb4 634/**
635* Given a manifest path, this function will check if the manifest is valid
636*
637* @param string $manifest The manifest file
638* @return object
639*/
640function scorm_validate_manifest($manifest) {
641 $validation = new stdClass();
642 if (is_file($manifest)) {
a679d64d 643 $validation->result = true;
76ea4fb4 644 } else {
a679d64d 645 $validation->result = false;
646 $validation->errors['reference'] = get_string('nomanifest','scorm');
76ea4fb4 647 }
648 return $validation;
649}
650
651/**
652* Given a aicc package directory, this function will check if the course structure is valid
653*
654* @param string $packagedir The aicc package directory path
655* @return object
656*/
657function scorm_validate_aicc($packagedir) {
658 $validation = new stdClass();
a679d64d 659 $validation->result = false;
76ea4fb4 660 if (is_dir($packagedir)) {
661 if ($handle = opendir($packagedir)) {
662 while (($file = readdir($handle)) !== false) {
663 $ext = substr($file,strrpos($file,'.'));
664 if (strtolower($ext) == '.cst') {
a679d64d 665 $validation->result = true;
76ea4fb4 666 break;
667 }
668 }
669 closedir($handle);
670 }
671 }
a679d64d 672 if ($validation->result == false) {
673 $validation->errors['reference'] = get_string('nomanifest','scorm');
76ea4fb4 674 }
675 return $validation;
676}
677
678
679function scorm_validate($data) {
680 global $CFG;
681
a679d64d 682 $validation = new stdClass();
683 $validation->errors = array();
684
685 if (!isset($data['course']) || empty($data['course'])) {
686 $validation->errors['reference'] = get_string('missingparam','scorm');
687 $validation->result = false;
688 return $validation;
689 }
690 $courseid = $data['course']; // Course Module ID
691
692 if (!isset($data['reference']) || empty($data['reference'])) {
693 $validation->errors['reference'] = get_string('packagefile','scorm');
694 $validation->result = false;
695 return $validation;
696 }
697 $reference = $data['reference']; // Package/manifest path/location
698
699 $scormid = $data['instance']; // scorm ID
700 $scorm = new stdClass();
701 if (!empty($scormid)) {
702 if (!$scorm = get_record("scorm","id",$scormid)) {
703 $validation->errors['reference'] = get_string('missingparam','scorm');
704 $validation->result = false;
705 return $validation;
706 }
707 }
708
709 if ($reference[0] == '#') {
710 require_once($repositoryconfigfile);
711 if ($CFG->repositoryactivate) {
712 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
713 } else {
714 $validation->errors['reference'] = get_string('badpackage','scorm');
715 $validation->result = false;
716 return $validation;
717 }
718 } else if (!scorm_external_link($reference)) {
719 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
720 }
721
722 // Create a temporary directory to unzip package or copy manifest and validate package
723 $tempdir = '';
724 $scormdir = '';
725 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
726 if ($tempdir = scorm_tempdir($scormdir)) {
727 $localreference = $tempdir."/".scorm_basename($reference);
728 copy ("$reference", $localreference);
729 if (!is_file($localreference)) {
730 $validation->errors['reference'] = get_string('badpackage','scorm');
731 $validation->result = false;
732 } else {
733 $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
734 switch ($ext) {
735 case '.pif':
736 case '.zip':
737 if (!unzip_file($localreference, $tempdir, false)) {
738 $validation->errors['reference'] = get_string('unziperror','scorm');
739 $validation->result = false;
740 } else {
741 unlink ($localreference);
742 if (is_file($tempdir.'/imsmanifest.xml')) {
743 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
744 $validation->pkgtype = 'SCORM';
745 } else {
746 $validation = scorm_validate_aicc($tempdir);
747 if (($validation->result == 'regular') || ($validation->result == 'found')) {
748 $validation->pkgtype = 'AICC';
749 } else {
750 $validation->errors['reference'] = get_string('nomanifest','scorm');
751 $validation->result = false;
752 }
753 }
754 }
755 break;
756 case '.xml':
757 if (basename($localreference) == 'imsmanifest.xml') {
758 $validation = scorm_validate_manifest($localreference);
759 } else {
760 $validation->errors['reference'] = get_string('nomanifest','scorm');
761 $validation->result = false;
762 }
763 break;
764 default:
765 $validation->errors['reference'] = get_string('badpackage','scorm');
766 $validation->result = false;
767 break;
768 }
769 }
770 if (is_dir($tempdir)) {
771 // Delete files and temporary directory
772 scorm_delete_files($tempdir);
773 }
774 } else {
775 $validation->errors['reference'] = get_string('packagedir','scorm');
776 $validation->result = false;
777 }
778 } else {
779 $validation->errors['reference'] = get_string('datadir','scorm');
780 $validation->result = false;
781 }
782 return $validation;
783}
784
785function scorm_check_package($data) {
ab3b00e1 786 global $CFG, $COURSE;
a679d64d 787
76ea4fb4 788 $courseid = $data->course; // Course Module ID
789 $reference = $data->reference; // Package path
790 $scormid = $data->instance; // scorm ID
791
792 $validation = new stdClass();
44ded097 793 $fstat=array("mtime"=>0);
a679d64d 794
795 if (!empty($courseid) && !empty($reference)) {
796 $externalpackage = scorm_external_link($reference);
797
76ea4fb4 798 $validation->launch = 0;
76ea4fb4 799 $referencefield = $reference;
800 if (empty($reference)) {
a679d64d 801 $validation = null;
76ea4fb4 802 } else if ($reference[0] == '#') {
803 require_once($repositoryconfigfile);
804 if ($CFG->repositoryactivate) {
805 $referencefield = $reference.'/imsmanfest.xml';
806 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
807 } else {
a679d64d 808 $validation = null;
76ea4fb4 809 }
a679d64d 810 } else if (!$externalpackage) {
76ea4fb4 811 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
812 }
813
814 if (!empty($scormid)) {
815 //
816 // SCORM Update
817 //
8aee93f1 818 //if (($validation->launch != -1) && is_file($reference)) {
a679d64d 819 if ((!empty($validation)) && (is_file($reference) || $externalpackage)){
8aee93f1 820
a679d64d 821 if (!$externalpackage) {
822 $mdcheck = md5_file($reference);
823 } else if ($externalpackage){
824 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
825 if ($tempdir = scorm_tempdir($scormdir)) {
8aee93f1 826 copy ("$reference", $tempdir."/".basename($reference));
a679d64d 827 $mdcheck = md5_file($tempdir."/".basename($reference));
828 scorm_delete_files($tempdir);
829 }
830 }
831 }
8aee93f1 832
76ea4fb4 833 if ($scorm = get_record("scorm","id",$scormid)) {
834 if ($scorm->reference[0] == '#') {
835 require_once($repositoryconfigfile);
836 if ($CFG->repositoryactivate) {
837 $oldreference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
838 } else {
839 $oldreference = $scorm->reference;
840 }
a679d64d 841 } else if (!scorm_external_link($scorm->reference)) {
76ea4fb4 842 $oldreference = $CFG->dataroot.'/'.$courseid.'/'.$scorm->reference;
a679d64d 843 } else {
844 $oldreference = $scorm->reference;
845 }
76ea4fb4 846 $validation->launch = $scorm->launch;
a679d64d 847 if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash)) || ($oldreference != $reference)) {
76ea4fb4 848 // This is a new or a modified package
849 $validation->launch = 0;
850 } else {
851 // Old package already validated
76ea4fb4 852 if (strpos($scorm->version,'AICC') !== false) {
853 $validation->pkgtype = 'AICC';
854 } else {
855 $validation->pkgtype = 'SCORM';
856 }
857 }
858 } else {
a679d64d 859 $validation = null;
76ea4fb4 860 }
861 } else {
a679d64d 862 $validation = null;
76ea4fb4 863 }
864 }
865 //$validation->launch = 0;
a679d64d 866 if (($validation != null) && ($validation->launch == 0)) {
76ea4fb4 867 //
868 // Package must be validated
869 //
870 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
a679d64d 871 $tempdir = '';
76ea4fb4 872 switch ($ext) {
873 case '.pif':
874 case '.zip':
875 // Create a temporary directory to unzip package and validate package
76ea4fb4 876 $scormdir = '';
877 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
a679d64d 878 if ($tempdir = scorm_tempdir($scormdir)) {
76ea4fb4 879 copy ("$reference", $tempdir."/".basename($reference));
880 unzip_file($tempdir."/".basename($reference), $tempdir, false);
82e34576 881 if (!$externalpackage) {
882 unlink ($tempdir."/".basename($reference));
883 }
76ea4fb4 884 if (is_file($tempdir.'/imsmanifest.xml')) {
885 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
886 $validation->pkgtype = 'SCORM';
887 } else {
888 $validation = scorm_validate_aicc($tempdir);
889 $validation->pkgtype = 'AICC';
890 }
891 } else {
a679d64d 892 $validation = null;
76ea4fb4 893 }
894 } else {
a679d64d 895 $validation = null;
76ea4fb4 896 }
897 break;
898 case '.xml':
899 if (basename($reference) == 'imsmanifest.xml') {
a679d64d 900 if ($externalpackage) {
901 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
902 if ($tempdir = scorm_tempdir($scormdir)) {
903 copy ("$reference", $tempdir."/".basename($reference));
ab3b00e1 904 if (is_file($tempdir."/".basename($reference))) {
905 $validation = scorm_validate_manifest($tempdir.'/'.basename($reference));
906 } else {
907 $validation = null;
908 }
a679d64d 909 }
910 }
911 } else {
ab3b00e1 912 $validation = scorm_validate_manifest($CFG->dataroot.'/'.$COURSE->id.'/'.$reference);
a679d64d 913 }
ab3b00e1 914 $validation->pkgtype = 'SCORM';
76ea4fb4 915 } else {
a679d64d 916 $validation = null;
76ea4fb4 917 }
918 break;
919 default:
a679d64d 920 $validation = null;
76ea4fb4 921 break;
922 }
a679d64d 923 if ($validation == null) {
76ea4fb4 924 if (is_dir($tempdir)) {
925 // Delete files and temporary directory
926 scorm_delete_files($tempdir);
927 }
76ea4fb4 928 } else {
ab3b00e1 929 if (($ext == '.xml') && (!$externalpackage)) {
76ea4fb4 930 $validation->datadir = dirname($referencefield);
931 } else {
932 $validation->datadir = substr($tempdir,strlen($scormdir));
933 }
934 $validation->launch = 0;
935 }
936 }
937 } else {
a679d64d 938 $validation = null;
76ea4fb4 939 }
940 return $validation;
941}
942
e4aa175a 943?>