removed an unrequired require_once
[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
b3659259 202/**
203* Returns an object containing all datas relative to the given sco ID
204*
205* @param integer $id The sco ID
206* @return mixed (false if sco id does not exists)
207*/
208function scorm_get_sco($id,$what=SCO_ALL) {
209 if ($sco = get_record('scorm_scoes','id',$id)) {
210 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
211 if (($what != SCO_ONLY) && ($scodatas = get_records('scorm_scoes_data','scoid',$id))) {
212 foreach ($scodatas as $scodata) {
e73457e4 213 $sco->{$scodata->name} = $scodata->value;
b3659259 214 }
e73457e4 215 }
9d759a0e 216 elseif (($what != SCO_ONLY) && (!($scodatas = get_records('scorm_scoes_data','scoid',$id)))){
217 $sco->parameters = '';
b3659259 218 }
219 return $sco;
220 } else {
221 return false;
222 }
223}
224
e4aa175a 225function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
e4aa175a 226 $id = null;
227 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
228 $track->value = $value;
229 $track->timemodified = time();
e4aa175a 230 $id = update_record('scorm_scoes_track',$track);
231 } else {
232 $track->userid = $userid;
233 $track->scormid = $scormid;
234 $track->scoid = $scoid;
235 $track->attempt = $attempt;
236 $track->element = $element;
237 $track->value = addslashes($value);
238 $track->timemodified = time();
e4aa175a 239 $id = insert_record('scorm_scoes_track',$track);
240 }
241 return $id;
242}
243
e4aa175a 244function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 245/// Gets all tracks of specified sco and user
246 global $CFG;
247
248 if (empty($attempt)) {
249 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
250 $attempt = scorm_get_last_attempt($scormid,$userid);
251 } else {
252 $attempt = 1;
253 }
254 }
255 $attemptsql = ' AND attempt=' . $attempt;
256 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
257 $usertrack->userid = $userid;
258 $usertrack->scoid = $scoid;
a30b6819 259 // Defined in order to unify scorm1.2 and scorm2004
e4aa175a 260 $usertrack->score_raw = '';
e4aa175a 261 $usertrack->status = '';
e4aa175a 262 $usertrack->total_time = '00:00:00';
263 $usertrack->session_time = '00:00:00';
264 $usertrack->timemodified = 0;
265 foreach ($tracks as $track) {
266 $element = $track->element;
267 $usertrack->{$element} = $track->value;
268 switch ($element) {
f69db63e 269 case 'cmi.core.lesson_status':
270 case 'cmi.completion_status':
271 if ($track->value == 'not attempted') {
272 $track->value = 'notattempted';
273 }
274 $usertrack->status = $track->value;
275 break;
e4aa175a 276 case 'cmi.core.score.raw':
277 case 'cmi.score.raw':
278 $usertrack->score_raw = $track->value;
279 break;
e4aa175a 280 case 'cmi.core.session_time':
281 case 'cmi.session_time':
282 $usertrack->session_time = $track->value;
283 break;
284 case 'cmi.core.total_time':
285 case 'cmi.total_time':
286 $usertrack->total_time = $track->value;
287 break;
288 }
289 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
290 $usertrack->timemodified = $track->timemodified;
291 }
292 }
293 return $usertrack;
294 } else {
295 return false;
296 }
297}
298
2b3447c3 299function scorm_get_user_data($userid) {
300/// Gets user info required to display the table of scorm results
301/// for report.php
e4aa175a 302
2b3447c3 303 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
304}
e4aa175a 305
a30b6819 306function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
307 $attemptscore = NULL;
308 $attemptscore->scoes = 0;
309 $attemptscore->values = 0;
310 $attemptscore->max = 0;
311 $attemptscore->sum = 0;
312 $attemptscore->lastmodify = 0;
313
314 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
315 return NULL;
e4aa175a 316 }
e4aa175a 317
a30b6819 318 $grademethod = $scorm->grademethod % 10;
319
2b3447c3 320 foreach ($scoes as $sco) {
321 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
322 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
a30b6819 323 $attemptscore->scoes++;
2b3447c3 324 }
325 if (!empty($userdata->score_raw)) {
a30b6819 326 $attemptscore->values++;
327 $attemptscore->sum += $userdata->score_raw;
328 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
329 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
330 $attemptscore->lastmodify = $userdata->timemodified;
331 } else {
332 $attemptscore->lastmodify = 0;
333 }
2b3447c3 334 }
335 }
e4aa175a 336 }
2b3447c3 337 switch ($grademethod) {
a30b6819 338 case GRADEHIGHEST:
339 $score = $attemptscore->max;
2b3447c3 340 break;
a30b6819 341 case GRADEAVERAGE:
342 if ($attemptscore->values > 0) {
343 $score = $attemptscore->sum/$attemptscore->values;
5c1ac70c 344 } else {
a30b6819 345 $score = 0;
2b3447c3 346 }
347 break;
a30b6819 348 case GRADESUM:
349 $score = $attemptscore->sum;
2b3447c3 350 break;
a30b6819 351 case GRADESCOES:
352 $score = $attemptscore->scoes;
2b3447c3 353 break;
5c1ac70c 354 }
a30b6819 355
356 if ($time) {
357 $result = new stdClass();
358 $result->score = $score;
359 $result->time = $attemptscore->lastmodify;
360 } else {
361 $result = $score;
362 }
363
364 return $result;
365}
366
367function scorm_grade_user($scorm, $userid, $time=false) {
368
369 $whatgrade = intval($scorm->grademethod / 10);
370
371 switch ($whatgrade) {
372 case FIRSTATTEMPT:
373 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
374 break;
375 case LASTATTEMPT:
376 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
377 break;
378 case HIGHESTATTEMPT:
379 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
380 $maxscore = 0;
381 $attempttime = 0;
382 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
383 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
384 if ($time) {
385 if ($attemptscore->score > $maxscore) {
386 $maxscore = $attemptscore->score;
387 $attempttime = $attemptscore->time;
388 }
389 } else {
390 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
391 }
392 }
393 if ($time) {
394 $result = new stdClass();
395 $result->score = $maxscore;
396 $result->time = $attempttime;
397 return $result;
398 } else {
399 return $maxscore;
400 }
401 break;
402 case AVERAGEATTEMPT:
403 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
404 $sumscore = 0;
405 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
406 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
407 if ($time) {
408 $sumscore += $attemptscore->score;
409 } else {
410 $sumscore += $attemptscore;
411 }
412 }
413
414 if ($lastattempt > 0) {
415 $score = $sumscore / $lastattempt;
416 } else {
417 $score = 0;
418 }
419
420 if ($time) {
421 $result = new stdClass();
422 $result->score = $score;
423 $result->time = $attemptscore->time;
424 return $result;
425 } else {
426 return $score;
427 }
428 break;
429 }
e4aa175a 430}
431
8e45ba45 432function scorm_count_launchable($scormid,$organization='') {
433 $strorganization = '';
434 if (!empty($organization)) {
435 $strorganization = " AND organization='$organization'";
436 }
437 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>''");
e4aa175a 438}
439
2b3447c3 440function scorm_get_last_attempt($scormid, $userid) {
441/// Find the last attempt number for the given user id and scorm id
442 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
443 if (empty($lastattempt->a)) {
444 return '1';
445 } else {
446 return $lastattempt->a;
e4aa175a 447 }
448 }
e4aa175a 449}
450
e4aa175a 451function scorm_course_format_display($user,$course) {
452 global $CFG;
453
454 $strupdate = get_string('update');
455 $strmodule = get_string('modulename','scorm');
77bf0c29 456 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 457
458 echo '<div class="mod-scorm">';
459 if ($scorms = get_all_instances_in_course('scorm', $course)) {
460 // The module SCORM activity with the least id is the course
461 $scorm = current($scorms);
462 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
8949f8df 463 error('Course Module ID was incorrect');
e4aa175a 464 }
465 $colspan = '';
466 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 467 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 468 if (isediting($course->id)) {
469 // Display update icon
470 $path = $CFG->wwwroot.'/course';
471 $headertext .= '<span class="commands">'.
472 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
0d905d9f 473 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
e4aa175a 474 }
475 $headertext .= '</td>';
476 // Display report link
477 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id, '', '', '', '', 'count(distinct(userid)) as c');
478 if ($trackedusers->c > 0) {
479 $headertext .= '<td class="reportlink">'.
fa738731 480 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
e4aa175a 481 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
482 } else {
483 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
484 }
485 $colspan = ' colspan="2"';
486 }
487 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
488 print_simple_box($headertext,'','100%');
489 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
490 } else {
0d699c24 491 if (has_capability('moodle/course:update', $context)) {
e4aa175a 492 // Create a new activity
2b3447c3 493 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 494 } else {
495 notify('Could not find a scorm course here');
496 }
497 }
498 echo '</div>';
499}
500
2b3447c3 501function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
e4aa175a 502 global $CFG;
ab3b00e1 503
504 if ($scorm->updatefreq == UPDATE_EVERYTIME){
505 $scorm->instance = $scorm->id;
506 scorm_update_instance($scorm);
507 }
508
e4aa175a 509 $organization = optional_param('organization', '', PARAM_INT);
510
2b3447c3 511 print_simple_box_start('center',$boxwidth);
e4aa175a 512?>
29a43013 513 <div class="structurehead"><?php print_string('contents','scorm') ?></div>
e4aa175a 514<?php
515 if (empty($organization)) {
516 $organization = $scorm->launch;
517 }
518 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
519 if (count($orgs) > 1) {
520 ?>
521 <div class='center'>
522 <?php print_string('organizations','scorm') ?>
b7dc2256 523 <form id='changeorg' method='post' action='<?php echo $action ?>'>
e4aa175a 524 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
525 </form>
526 </div>
527<?php
528 }
529 }
530 $orgidentifier = '';
b3659259 531 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
532 if (($sco->organization == '') && ($sco->launch == '')) {
533 $orgidentifier = $sco->identifier;
e4aa175a 534 } else {
b3659259 535 $orgidentifier = $sco->organization;
e4aa175a 536 }
537 }
28ffbff3 538
539/*
540 $orgidentifier = '';
541 if ($org = get_record('scorm_scoes','id',$organization)) {
542 if (($org->organization == '') && ($org->launch == '')) {
543 $orgidentifier = $org->identifier;
544 } else {
545 $orgidentifier = $org->organization;
546 }
547 }*/
548
2b3447c3 549 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
dbe7e6f6 550 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
551 $scorm->version = 'scorm_12';
552 }
2b3447c3 553 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
554
e4aa175a 555 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
556 $incomplete = $result->incomplete;
e4aa175a 557 echo $result->toc;
e4aa175a 558 print_simple_box_end();
28ffbff3 559
e4aa175a 560?>
561 <div class="center">
04bc70fc 562 <form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?scoid=<?php echo $sco->id ?>&amp;id=<?php echo $cm->id ?>"<?php echo $scorm->popup == 1?' target="newwin"':'' ?>>
e4aa175a 563 <?php
e4aa175a 564 if ($scorm->hidebrowse == 0) {
8949f8df 565 print_string('mode','scorm');
e73457e4 566 echo '<input type="hidden" name="scoid" value="$sco->id" />'."\n";
e4aa175a 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 />
28ffbff3 581 <input type="hidden" name="scoid"/>
e4aa175a 582 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
2b600d16 583 <input type="submit" value="<?php print_string('enter','scorm') ?>" />
e4aa175a 584 </form>
585 </div>
586<?php
587}
28ffbff3 588function scorm_simple_play($scorm,$user) {
589 $result = false;
590
591 $scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>\'\'');
28ffbff3 592
593 if (count($scoes) == 1) {
28ffbff3 594 if ($scorm->skipview >= 1) {
595 $sco = current($scoes);
596 if (scorm_get_tracks($sco->id,$user->id) === false) {
e73457e4 597 header('Location: player.php?a='.$scorm->id.'&scoid= '.$sco->id);
28ffbff3 598 $result = true;
599 } else if ($scorm->skipview == 2) {
28ffbff3 600 header('Location: player.php?a='.$scorm->id.'&scoid= '.$sco->id);
601 $result = true;
602 }
603 }
604 }
28ffbff3 605 return $result;
606}
607/*
8e45ba45 608function scorm_simple_play($scorm,$user) {
609 $result = false;
76ea4fb4 610 if ($scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>""')) {
611 if (count($scoes) == 1) {
612 if ($scorm->skipview >= 1) {
613 $sco = current($scoes);
614 if (scorm_get_tracks($sco->id,$user->id) === false) {
615 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
616 $result = true;
617 } else if ($scorm->skipview == 2) {
618 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
619 $result = true;
620 }
8e45ba45 621 }
622 }
623 }
624 return $result;
625}
28ffbff3 626*/
a30b6819 627function scorm_parse($scorm) {
628 global $CFG,$repositoryconfigfile;
8949f8df 629
630 if ($scorm->reference[0] == '#') {
631 $reference = $CFG->repository.substr($scorm->reference,1);
632 } else {
633 $reference = $scorm->dir.'/'.$scorm->id;
634 }
a30b6819 635 // Parse scorm manifest
636 if ($scorm->pkgtype == 'AICC') {
637 require_once('datamodels/aicclib.php');
8949f8df 638 $scorm->launch = scorm_parse_aicc($reference, $scorm->id);
a30b6819 639 } else {
640 require_once('datamodels/scormlib.php');
a30b6819 641 if ($scorm->reference[0] == '#') {
642 require_once($repositoryconfigfile);
a30b6819 643 }
644
8949f8df 645 $scorm->launch = scorm_parse_scorm($reference,$scorm->id);
a30b6819 646 }
647 return $scorm->launch;
648}
649
76ea4fb4 650/**
651* Given a manifest path, this function will check if the manifest is valid
652*
653* @param string $manifest The manifest file
654* @return object
655*/
656function scorm_validate_manifest($manifest) {
657 $validation = new stdClass();
658 if (is_file($manifest)) {
a679d64d 659 $validation->result = true;
76ea4fb4 660 } else {
a679d64d 661 $validation->result = false;
662 $validation->errors['reference'] = get_string('nomanifest','scorm');
76ea4fb4 663 }
664 return $validation;
665}
666
667/**
668* Given a aicc package directory, this function will check if the course structure is valid
669*
670* @param string $packagedir The aicc package directory path
671* @return object
672*/
673function scorm_validate_aicc($packagedir) {
674 $validation = new stdClass();
a679d64d 675 $validation->result = false;
76ea4fb4 676 if (is_dir($packagedir)) {
677 if ($handle = opendir($packagedir)) {
678 while (($file = readdir($handle)) !== false) {
679 $ext = substr($file,strrpos($file,'.'));
680 if (strtolower($ext) == '.cst') {
a679d64d 681 $validation->result = true;
76ea4fb4 682 break;
683 }
684 }
685 closedir($handle);
686 }
687 }
a679d64d 688 if ($validation->result == false) {
689 $validation->errors['reference'] = get_string('nomanifest','scorm');
76ea4fb4 690 }
691 return $validation;
692}
693
694
695function scorm_validate($data) {
696 global $CFG;
697
a679d64d 698 $validation = new stdClass();
699 $validation->errors = array();
700
701 if (!isset($data['course']) || empty($data['course'])) {
702 $validation->errors['reference'] = get_string('missingparam','scorm');
703 $validation->result = false;
704 return $validation;
705 }
706 $courseid = $data['course']; // Course Module ID
707
708 if (!isset($data['reference']) || empty($data['reference'])) {
709 $validation->errors['reference'] = get_string('packagefile','scorm');
710 $validation->result = false;
711 return $validation;
712 }
713 $reference = $data['reference']; // Package/manifest path/location
714
715 $scormid = $data['instance']; // scorm ID
716 $scorm = new stdClass();
717 if (!empty($scormid)) {
8949f8df 718 if (!$scorm = get_record('scorm','id',$scormid)) {
a679d64d 719 $validation->errors['reference'] = get_string('missingparam','scorm');
720 $validation->result = false;
721 return $validation;
722 }
723 }
724
725 if ($reference[0] == '#') {
726 require_once($repositoryconfigfile);
727 if ($CFG->repositoryactivate) {
728 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
729 } else {
730 $validation->errors['reference'] = get_string('badpackage','scorm');
731 $validation->result = false;
732 return $validation;
733 }
734 } else if (!scorm_external_link($reference)) {
735 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
736 }
737
738 // Create a temporary directory to unzip package or copy manifest and validate package
739 $tempdir = '';
740 $scormdir = '';
741 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
742 if ($tempdir = scorm_tempdir($scormdir)) {
8949f8df 743 $localreference = $tempdir.'/'.basename($reference);
a679d64d 744 copy ("$reference", $localreference);
745 if (!is_file($localreference)) {
746 $validation->errors['reference'] = get_string('badpackage','scorm');
747 $validation->result = false;
748 } else {
749 $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
750 switch ($ext) {
751 case '.pif':
752 case '.zip':
753 if (!unzip_file($localreference, $tempdir, false)) {
754 $validation->errors['reference'] = get_string('unziperror','scorm');
755 $validation->result = false;
756 } else {
757 unlink ($localreference);
758 if (is_file($tempdir.'/imsmanifest.xml')) {
759 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
760 $validation->pkgtype = 'SCORM';
761 } else {
762 $validation = scorm_validate_aicc($tempdir);
763 if (($validation->result == 'regular') || ($validation->result == 'found')) {
764 $validation->pkgtype = 'AICC';
765 } else {
766 $validation->errors['reference'] = get_string('nomanifest','scorm');
767 $validation->result = false;
768 }
769 }
770 }
771 break;
772 case '.xml':
773 if (basename($localreference) == 'imsmanifest.xml') {
774 $validation = scorm_validate_manifest($localreference);
775 } else {
776 $validation->errors['reference'] = get_string('nomanifest','scorm');
777 $validation->result = false;
778 }
779 break;
780 default:
781 $validation->errors['reference'] = get_string('badpackage','scorm');
782 $validation->result = false;
783 break;
784 }
785 }
786 if (is_dir($tempdir)) {
787 // Delete files and temporary directory
788 scorm_delete_files($tempdir);
789 }
790 } else {
791 $validation->errors['reference'] = get_string('packagedir','scorm');
792 $validation->result = false;
793 }
794 } else {
795 $validation->errors['reference'] = get_string('datadir','scorm');
796 $validation->result = false;
797 }
798 return $validation;
799}
800
801function scorm_check_package($data) {
ab3b00e1 802 global $CFG, $COURSE;
a679d64d 803
76ea4fb4 804 $courseid = $data->course; // Course Module ID
805 $reference = $data->reference; // Package path
806 $scormid = $data->instance; // scorm ID
807
808 $validation = new stdClass();
a679d64d 809
810 if (!empty($courseid) && !empty($reference)) {
811 $externalpackage = scorm_external_link($reference);
812
76ea4fb4 813 $validation->launch = 0;
76ea4fb4 814 $referencefield = $reference;
815 if (empty($reference)) {
a679d64d 816 $validation = null;
76ea4fb4 817 } else if ($reference[0] == '#') {
818 require_once($repositoryconfigfile);
819 if ($CFG->repositoryactivate) {
820 $referencefield = $reference.'/imsmanfest.xml';
821 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
822 } else {
a679d64d 823 $validation = null;
76ea4fb4 824 }
a679d64d 825 } else if (!$externalpackage) {
76ea4fb4 826 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
827 }
828
829 if (!empty($scormid)) {
830 //
831 // SCORM Update
832 //
a679d64d 833 if ((!empty($validation)) && (is_file($reference) || $externalpackage)){
8aee93f1 834
a679d64d 835 if (!$externalpackage) {
836 $mdcheck = md5_file($reference);
837 } else if ($externalpackage){
838 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
839 if ($tempdir = scorm_tempdir($scormdir)) {
8949f8df 840 copy ("$reference", $tempdir.'/'.basename($reference));
841 $mdcheck = md5_file($tempdir.'/'.basename($reference));
a679d64d 842 scorm_delete_files($tempdir);
843 }
844 }
845 }
8aee93f1 846
8949f8df 847 if ($scorm = get_record('scorm','id',$scormid)) {
76ea4fb4 848 if ($scorm->reference[0] == '#') {
849 require_once($repositoryconfigfile);
850 if ($CFG->repositoryactivate) {
851 $oldreference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
852 } else {
853 $oldreference = $scorm->reference;
854 }
a679d64d 855 } else if (!scorm_external_link($scorm->reference)) {
76ea4fb4 856 $oldreference = $CFG->dataroot.'/'.$courseid.'/'.$scorm->reference;
a679d64d 857 } else {
858 $oldreference = $scorm->reference;
859 }
76ea4fb4 860 $validation->launch = $scorm->launch;
a679d64d 861 if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash)) || ($oldreference != $reference)) {
76ea4fb4 862 // This is a new or a modified package
863 $validation->launch = 0;
864 } else {
865 // Old package already validated
76ea4fb4 866 if (strpos($scorm->version,'AICC') !== false) {
867 $validation->pkgtype = 'AICC';
868 } else {
869 $validation->pkgtype = 'SCORM';
870 }
871 }
872 } else {
a679d64d 873 $validation = null;
76ea4fb4 874 }
875 } else {
a679d64d 876 $validation = null;
76ea4fb4 877 }
878 }
879 //$validation->launch = 0;
a679d64d 880 if (($validation != null) && ($validation->launch == 0)) {
76ea4fb4 881 //
882 // Package must be validated
883 //
884 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
a679d64d 885 $tempdir = '';
76ea4fb4 886 switch ($ext) {
887 case '.pif':
888 case '.zip':
889 // Create a temporary directory to unzip package and validate package
76ea4fb4 890 $scormdir = '';
891 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
a679d64d 892 if ($tempdir = scorm_tempdir($scormdir)) {
8949f8df 893 copy ("$reference", $tempdir.'/'.basename($reference));
894 unzip_file($tempdir.'/'.basename($reference), $tempdir, false);
82e34576 895 if (!$externalpackage) {
8949f8df 896 unlink ($tempdir.'/'.basename($reference));
82e34576 897 }
76ea4fb4 898 if (is_file($tempdir.'/imsmanifest.xml')) {
899 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
900 $validation->pkgtype = 'SCORM';
901 } else {
902 $validation = scorm_validate_aicc($tempdir);
903 $validation->pkgtype = 'AICC';
904 }
905 } else {
a679d64d 906 $validation = null;
76ea4fb4 907 }
908 } else {
a679d64d 909 $validation = null;
76ea4fb4 910 }
911 break;
912 case '.xml':
913 if (basename($reference) == 'imsmanifest.xml') {
a679d64d 914 if ($externalpackage) {
915 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
916 if ($tempdir = scorm_tempdir($scormdir)) {
8949f8df 917 copy ("$reference", $tempdir.'/'.basename($reference));
918 if (is_file($tempdir.'/'.basename($reference))) {
ab3b00e1 919 $validation = scorm_validate_manifest($tempdir.'/'.basename($reference));
920 } else {
921 $validation = null;
922 }
a679d64d 923 }
924 }
925 } else {
ab3b00e1 926 $validation = scorm_validate_manifest($CFG->dataroot.'/'.$COURSE->id.'/'.$reference);
a679d64d 927 }
ab3b00e1 928 $validation->pkgtype = 'SCORM';
76ea4fb4 929 } else {
a679d64d 930 $validation = null;
76ea4fb4 931 }
932 break;
933 default:
a679d64d 934 $validation = null;
76ea4fb4 935 break;
936 }
a679d64d 937 if ($validation == null) {
76ea4fb4 938 if (is_dir($tempdir)) {
939 // Delete files and temporary directory
940 scorm_delete_files($tempdir);
941 }
76ea4fb4 942 } else {
ab3b00e1 943 if (($ext == '.xml') && (!$externalpackage)) {
76ea4fb4 944 $validation->datadir = dirname($referencefield);
945 } else {
946 $validation->datadir = substr($tempdir,strlen($scormdir));
947 }
948 $validation->launch = 0;
949 }
950 }
951 } else {
a679d64d 952 $validation = null;
76ea4fb4 953 }
954 return $validation;
955}
956
e4aa175a 957?>