Fixed problem with cmi.interactions.n.id duplicated ids
[moodle-linuxchix.git] / mod / scorm / locallib.php
blob986912e89f0647832994a0a7aaa49a81da2e6950
1 <?php // $Id$
3 /// Constants and settings for module scorm
4 define('UPDATE_NEVER', '0');
5 define('UPDATE_ONCHANGE', '1');
6 define('UPDATE_EVERYDAY', '2');
7 define('UPDATE_EVERYTIME', '3');
9 define('SCO_ALL', 0);
10 define('SCO_DATA', 1);
11 define('SCO_ONLY', 2);
13 define('GRADESCOES', '0');
14 define('GRADEHIGHEST', '1');
15 define('GRADEAVERAGE', '2');
16 define('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'));
22 define('HIGHESTATTEMPT', '0');
23 define('AVERAGEATTEMPT', '1');
24 define('FIRSTATTEMPT', '2');
25 define('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'));
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 = '';
39 foreach ($SCORM_POPUP_OPTIONS as $popupopt => $value) {
40 $stdoptions .= $popupopt.'='.$value;
41 if ($popupopt != 'status') {
42 $stdoptions .= ',';
46 if (!isset($CFG->scorm_maxattempts)) {
47 set_config('scorm_maxattempts','6');
50 if (!isset($CFG->scorm_frameheight)) {
51 set_config('scorm_frameheight','500');
54 if (!isset($CFG->scorm_framewidth)) {
55 set_config('scorm_framewidth','100%');
58 if (!isset($CFG->scorm_updatetime)) {
59 set_config('scorm_updatetime','2');
62 if (!isset($CFG->scorm_advancedsettings)) {
63 set_config('scorm_advancedsettings','0');
66 if (!isset($CFG->scorm_windowsettings)) {
67 set_config('scorm_windowsettings','0');
71 // Repository configurations
73 $repositoryconfigfile = $CFG->dirroot.'/mod/resource/type/ims/repository_config.php';
74 $repositorybrowser = '/mod/resource/type/ims/finder.php';
76 /// Local Library of functions for module scorm
78 /**
79 * This function will permanently delete the given
80 * directory and all files and subdirectories.
82 * @param string $directory The directory to remove
83 * @return boolean
85 function 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);
96 set_time_limit(5);
98 rmdir($directory);
99 return true;
101 return false;
105 * Given a diretory path returns the file list
107 * @param string $directory
108 * @return array
110 function scorm_scandir($directory) {
111 if (version_compare(phpversion(),'5.0.0','>=')) {
112 return scandir($directory);
113 } else {
114 $files = array();
115 if ($dh = opendir($directory)) {
116 while (($file = readdir($dh)) !== false) {
117 $files[] = $file;
119 closedir($dh);
121 return $files;
126 * Create a new temporary subdirectory with a random name in the given path
128 * @param string $strpath The scorm data directory
129 * @return string/boolean
131 function scorm_tempdir($strPath)
133 global $CFG;
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));
147 $randstring .= $char;
148 $lchar = $char;
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;
160 function 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;
167 } else {
168 if ($element->{$item} == $needle) {
169 return $key;
174 return false;
177 function scorm_repeater($what, $times) {
178 if ($times <= 0) {
179 return null;
181 $return = '';
182 for ($i=0; $i<$times;$i++) {
183 $return .= $what;
185 return $return;
188 function 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;
199 return $result;
203 * Returns an object containing all datas relative to the given sco ID
205 * @param integer $id The sco ID
206 * @return mixed (false if sco id does not exists)
208 function 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) {
213 $sco->{$scodata->name} = $scodata->value;
216 elseif (($what != SCO_ONLY) && (!($scodatas = get_records('scorm_scoes_data','scoid',$id)))){
217 $sco->parameters = '';
219 return $sco;
220 } else {
221 return false;
225 function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
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();
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();
239 $id = insert_record('scorm_scoes_track',$track);
241 return $id;
244 function scorm_get_tracks($scoid,$userid,$attempt='') {
245 /// Gets all tracks of specified sco and user
246 global $CFG;
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;
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;
259 // Defined in order to unify scorm1.2 and scorm2004
260 $usertrack->score_raw = '';
261 $usertrack->status = '';
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) {
269 case 'cmi.core.lesson_status':
270 case 'cmi.completion_status':
271 if ($track->value == 'not attempted') {
272 $track->value = 'notattempted';
274 $usertrack->status = $track->value;
275 break;
276 case 'cmi.core.score.raw':
277 case 'cmi.score.raw':
278 $usertrack->score_raw = $track->value;
279 break;
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;
289 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
290 $usertrack->timemodified = $track->timemodified;
293 return $usertrack;
294 } else {
295 return false;
299 function scorm_get_user_data($userid) {
300 /// Gets user info required to display the table of scorm results
301 /// for report.php
303 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
306 function 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;
314 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
315 return NULL;
318 $grademethod = $scorm->grademethod % 10;
320 foreach ($scoes as $sco) {
321 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
322 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
323 $attemptscore->scoes++;
325 if (!empty($userdata->score_raw)) {
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;
337 switch ($grademethod) {
338 case GRADEHIGHEST:
339 $score = $attemptscore->max;
340 break;
341 case GRADEAVERAGE:
342 if ($attemptscore->values > 0) {
343 $score = $attemptscore->sum/$attemptscore->values;
344 } else {
345 $score = 0;
347 break;
348 case GRADESUM:
349 $score = $attemptscore->sum;
350 break;
351 case GRADESCOES:
352 $score = $attemptscore->scoes;
353 break;
356 if ($time) {
357 $result = new stdClass();
358 $result->score = $score;
359 $result->time = $attemptscore->lastmodify;
360 } else {
361 $result = $score;
364 return $result;
367 function scorm_grade_user($scorm, $userid, $time=false) {
369 $whatgrade = intval($scorm->grademethod / 10);
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;
389 } else {
390 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
393 if ($time) {
394 $result = new stdClass();
395 $result->score = $maxscore;
396 $result->time = $attempttime;
397 return $result;
398 } else {
399 return $maxscore;
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;
414 if ($lastattempt > 0) {
415 $score = $sumscore / $lastattempt;
416 } else {
417 $score = 0;
420 if ($time) {
421 $result = new stdClass();
422 $result->score = $score;
423 $result->time = $attemptscore->time;
424 return $result;
425 } else {
426 return $score;
428 break;
432 function scorm_count_launchable($scormid,$organization='') {
433 $strorganization = '';
434 if (!empty($organization)) {
435 $strorganization = " AND organization='$organization'";
437 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>''");
440 function 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;
451 function scorm_course_format_display($user,$course) {
452 global $CFG;
454 $strupdate = get_string('update');
455 $strmodule = get_string('modulename','scorm');
456 $context = get_context_instance(CONTEXT_COURSE,$course->id);
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)) {
463 error('Course Module ID was incorrect');
465 $colspan = '';
466 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
467 if (has_capability('moodle/course:manageactivities', $context)) {
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().'">'.
473 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
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">'.
480 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
481 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
482 } else {
483 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
485 $colspan = ' colspan="2"';
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 {
491 if (has_capability('moodle/course:update', $context)) {
492 // Create a new activity
493 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
494 } else {
495 notify('Could not find a scorm course here');
498 echo '</div>';
501 function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
502 global $CFG;
504 if ($scorm->updatefreq == UPDATE_EVERYTIME){
505 $scorm->instance = $scorm->id;
506 scorm_update_instance($scorm);
509 $organization = optional_param('organization', '', PARAM_INT);
511 print_simple_box_start('center',$boxwidth);
513 <div class="structurehead"><?php print_string('contents','scorm') ?></div>
514 <?php
515 if (empty($organization)) {
516 $organization = $scorm->launch;
518 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
519 if (count($orgs) > 1) {
521 <div class='center'>
522 <?php print_string('organizations','scorm') ?>
523 <form id='changeorg' method='post' action='<?php echo $action ?>'>
524 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
525 </form>
526 </div>
527 <?php
530 $orgidentifier = '';
531 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
532 if (($sco->organization == '') && ($sco->launch == '')) {
533 $orgidentifier = $sco->identifier;
534 } else {
535 $orgidentifier = $sco->organization;
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;
549 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
550 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
551 $scorm->version = 'scorm_12';
553 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
555 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
556 $incomplete = $result->incomplete;
557 echo $result->toc;
558 print_simple_box_end();
561 <div class="center">
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"':'' ?>>
563 <?php
564 if ($scorm->hidebrowse == 0) {
565 print_string('mode','scorm');
566 echo '<input type="hidden" name="scoid" value="$sco->id" />'."\n";
567 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
568 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
569 } else {
570 echo '<input type="hidden" name="mode" value="normal" />'."\n";
572 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
574 <br />
575 <input type="checkbox" id="a" name="newattempt" />
576 <label for="a"><?php print_string('newattempt','scorm') ?></label>
577 <?php
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('enter','scorm') ?>" />
584 </form>
585 </div>
586 <?php
588 function scorm_simple_play($scorm,$user) {
589 $result = false;
591 $scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>\'\'');
593 if (count($scoes) == 1) {
594 if ($scorm->skipview >= 1) {
595 $sco = current($scoes);
596 if (scorm_get_tracks($sco->id,$user->id) === false) {
597 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
598 $result = true;
599 } else if ($scorm->skipview == 2) {
600 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
601 $result = true;
605 return $result;
608 function scorm_simple_play($scorm,$user) {
609 $result = false;
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;
624 return $result;
627 function scorm_parse($scorm) {
628 global $CFG,$repositoryconfigfile;
630 if ($scorm->reference[0] == '#') {
631 require_once($repositoryconfigfile);
632 if ($CFG->repositoryactivate) {
633 $referencedir = $CFG->repository.substr($scorm->reference,1);
635 } else {
636 if ((!scorm_external_link($scorm->reference)) && (basename($scorm->reference) == 'imsmanifest.xml')) {
637 $referencedir = $CFG->dataroot.'/'.$scorm->course.'/'.$scorm->datadir;
638 } else {
639 $referencedir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm/'.$scorm->id;
643 // Parse scorm manifest
644 if ($scorm->pkgtype == 'AICC') {
645 require_once('datamodels/aicclib.php');
646 $scorm->launch = scorm_parse_aicc($referencedir, $scorm->id);
647 } else {
648 require_once('datamodels/scormlib.php');
649 if ($scorm->reference[0] == '#') {
650 require_once($repositoryconfigfile);
652 $scorm->launch = scorm_parse_scorm($referencedir,$scorm->id);
654 return $scorm->launch;
658 * Given a manifest path, this function will check if the manifest is valid
660 * @param string $manifest The manifest file
661 * @return object
663 function scorm_validate_manifest($manifest) {
664 $validation = new stdClass();
665 if (is_file($manifest)) {
666 $validation->result = true;
667 } else {
668 $validation->result = false;
669 $validation->errors['reference'] = get_string('nomanifest','scorm');
671 return $validation;
675 * Given a aicc package directory, this function will check if the course structure is valid
677 * @param string $packagedir The aicc package directory path
678 * @return object
680 function scorm_validate_aicc($packagedir) {
681 $validation = new stdClass();
682 $validation->result = false;
683 if (is_dir($packagedir)) {
684 if ($handle = opendir($packagedir)) {
685 while (($file = readdir($handle)) !== false) {
686 $ext = substr($file,strrpos($file,'.'));
687 if (strtolower($ext) == '.cst') {
688 $validation->result = true;
689 break;
692 closedir($handle);
695 if ($validation->result == false) {
696 $validation->errors['reference'] = get_string('nomanifest','scorm');
698 return $validation;
702 function scorm_validate($data) {
703 global $CFG;
705 $validation = new stdClass();
706 $validation->errors = array();
708 if (!isset($data['course']) || empty($data['course'])) {
709 $validation->errors['reference'] = get_string('missingparam','scorm');
710 $validation->result = false;
711 return $validation;
713 $courseid = $data['course']; // Course Module ID
715 if (!isset($data['reference']) || empty($data['reference'])) {
716 $validation->errors['reference'] = get_string('packagefile','scorm');
717 $validation->result = false;
718 return $validation;
720 $reference = $data['reference']; // Package/manifest path/location
722 $scormid = $data['instance']; // scorm ID
723 $scorm = new stdClass();
724 if (!empty($scormid)) {
725 if (!$scorm = get_record('scorm','id',$scormid)) {
726 $validation->errors['reference'] = get_string('missingparam','scorm');
727 $validation->result = false;
728 return $validation;
732 if ($reference[0] == '#') {
733 require_once($repositoryconfigfile);
734 if ($CFG->repositoryactivate) {
735 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
736 } else {
737 $validation->errors['reference'] = get_string('badpackage','scorm');
738 $validation->result = false;
739 return $validation;
741 } else if (!scorm_external_link($reference)) {
742 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
745 // Create a temporary directory to unzip package or copy manifest and validate package
746 $tempdir = '';
747 $scormdir = '';
748 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
749 if ($tempdir = scorm_tempdir($scormdir)) {
750 $localreference = $tempdir.'/'.basename($reference);
751 copy ("$reference", $localreference);
752 if (!is_file($localreference)) {
753 $validation->errors['reference'] = get_string('badpackage','scorm');
754 $validation->result = false;
755 } else {
756 $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
757 switch ($ext) {
758 case '.pif':
759 case '.zip':
760 if (!unzip_file($localreference, $tempdir, false)) {
761 $validation->errors['reference'] = get_string('unziperror','scorm');
762 $validation->result = false;
763 } else {
764 unlink ($localreference);
765 if (is_file($tempdir.'/imsmanifest.xml')) {
766 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
767 $validation->pkgtype = 'SCORM';
768 } else {
769 $validation = scorm_validate_aicc($tempdir);
770 if (($validation->result == 'regular') || ($validation->result == 'found')) {
771 $validation->pkgtype = 'AICC';
772 } else {
773 $validation->errors['reference'] = get_string('nomanifest','scorm');
774 $validation->result = false;
778 break;
779 case '.xml':
780 if (basename($localreference) == 'imsmanifest.xml') {
781 $validation = scorm_validate_manifest($localreference);
782 } else {
783 $validation->errors['reference'] = get_string('nomanifest','scorm');
784 $validation->result = false;
786 break;
787 default:
788 $validation->errors['reference'] = get_string('badpackage','scorm');
789 $validation->result = false;
790 break;
793 if (is_dir($tempdir)) {
794 // Delete files and temporary directory
795 scorm_delete_files($tempdir);
797 } else {
798 $validation->errors['reference'] = get_string('packagedir','scorm');
799 $validation->result = false;
801 } else {
802 $validation->errors['reference'] = get_string('datadir','scorm');
803 $validation->result = false;
805 return $validation;
808 function scorm_check_package($data) {
809 global $CFG, $COURSE;
811 $courseid = $data->course; // Course Module ID
812 $reference = $data->reference; // Package path
813 $scormid = $data->instance; // scorm ID
815 $validation = new stdClass();
817 if (!empty($courseid) && !empty($reference)) {
818 $externalpackage = scorm_external_link($reference);
820 $validation->launch = 0;
821 $referencefield = $reference;
822 if (empty($reference)) {
823 $validation = null;
824 } else if ($reference[0] == '#') {
825 require_once($repositoryconfigfile);
826 if ($CFG->repositoryactivate) {
827 $referencefield = $reference.'/imsmanfest.xml';
828 $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
829 } else {
830 $validation = null;
832 } else if (!$externalpackage) {
833 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
836 if (!empty($scormid)) {
838 // SCORM Update
840 if ((!empty($validation)) && (is_file($reference) || $externalpackage)){
842 if (!$externalpackage) {
843 $mdcheck = md5_file($reference);
844 } else if ($externalpackage){
845 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
846 if ($tempdir = scorm_tempdir($scormdir)) {
847 copy ("$reference", $tempdir.'/'.basename($reference));
848 $mdcheck = md5_file($tempdir.'/'.basename($reference));
849 scorm_delete_files($tempdir);
854 if ($scorm = get_record('scorm','id',$scormid)) {
855 if ($scorm->reference[0] == '#') {
856 require_once($repositoryconfigfile);
857 if ($CFG->repositoryactivate) {
858 $oldreference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
859 } else {
860 $oldreference = $scorm->reference;
862 } else if (!scorm_external_link($scorm->reference)) {
863 $oldreference = $CFG->dataroot.'/'.$courseid.'/'.$scorm->reference;
864 } else {
865 $oldreference = $scorm->reference;
867 $validation->launch = $scorm->launch;
868 if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash)) || ($oldreference != $reference)) {
869 // This is a new or a modified package
870 $validation->launch = 0;
871 } else {
872 // Old package already validated
873 if (strpos($scorm->version,'AICC') !== false) {
874 $validation->pkgtype = 'AICC';
875 } else {
876 $validation->pkgtype = 'SCORM';
879 } else {
880 $validation = null;
882 } else {
883 $validation = null;
886 //$validation->launch = 0;
887 if (($validation != null) && ($validation->launch == 0)) {
889 // Package must be validated
891 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
892 $tempdir = '';
893 switch ($ext) {
894 case '.pif':
895 case '.zip':
896 // Create a temporary directory to unzip package and validate package
897 $scormdir = '';
898 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
899 if ($tempdir = scorm_tempdir($scormdir)) {
900 copy ("$reference", $tempdir.'/'.basename($reference));
901 unzip_file($tempdir.'/'.basename($reference), $tempdir, false);
902 if (!$externalpackage) {
903 unlink ($tempdir.'/'.basename($reference));
905 if (is_file($tempdir.'/imsmanifest.xml')) {
906 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
907 $validation->pkgtype = 'SCORM';
908 } else {
909 $validation = scorm_validate_aicc($tempdir);
910 $validation->pkgtype = 'AICC';
912 } else {
913 $validation = null;
915 } else {
916 $validation = null;
918 break;
919 case '.xml':
920 if (basename($reference) == 'imsmanifest.xml') {
921 if ($externalpackage) {
922 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
923 if ($tempdir = scorm_tempdir($scormdir)) {
924 copy ("$reference", $tempdir.'/'.basename($reference));
925 if (is_file($tempdir.'/'.basename($reference))) {
926 $validation = scorm_validate_manifest($tempdir.'/'.basename($reference));
927 } else {
928 $validation = null;
932 } else {
933 $validation = scorm_validate_manifest($reference);
935 $validation->pkgtype = 'SCORM';
936 } else {
937 $validation = null;
939 break;
940 default:
941 $validation = null;
942 break;
944 if ($validation == null) {
945 if (is_dir($tempdir)) {
946 // Delete files and temporary directory
947 scorm_delete_files($tempdir);
949 } else {
950 if (($ext == '.xml') && (!$externalpackage)) {
951 $validation->datadir = dirname($referencefield);
952 } else {
953 $validation->datadir = substr($tempdir,strlen($scormdir));
955 $validation->launch = 0;
958 } else {
959 $validation = null;
961 return $validation;