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