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