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