MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / scorm / locallib.php
blob95c29fa158f049c2b6b68e8d02fa0fcfc837f261
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 set_time_limit(0);
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);
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)
209 function 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) {
214 $sco->{$scodata->name} = $scodata->value;
216 } else if (($what != SCO_ONLY) && (!($scodatas = get_records('scorm_scoes_data','scoid',$id)))) {
217 $sco->parameters = '';
219 return $sco;
220 } else {
221 return false;
224 function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
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();
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();
238 $id = insert_record('scorm_scoes_track',$track);
240 return $id;
243 function scorm_get_tracks($scoid,$userid,$attempt='') {
244 /// Gets all tracks of specified sco and user
245 global $CFG;
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;
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;
258 // Defined in order to unify scorm1.2 and scorm2004
259 $usertrack->score_raw = '';
260 $usertrack->status = '';
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) {
268 case 'cmi.core.lesson_status':
269 case 'cmi.completion_status':
270 if ($track->value == 'not attempted') {
271 $track->value = 'notattempted';
273 $usertrack->status = $track->value;
274 break;
275 case 'cmi.core.score.raw':
276 case 'cmi.score.raw':
277 $usertrack->score_raw = $track->value;
278 break;
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;
288 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
289 $usertrack->timemodified = $track->timemodified;
292 return $usertrack;
293 } else {
294 return false;
298 function scorm_get_user_data($userid) {
299 /// Gets user info required to display the table of scorm results
300 /// for report.php
302 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
305 function 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;
313 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
314 return NULL;
317 $grademethod = $scorm->grademethod % 10;
319 foreach ($scoes as $sco) {
320 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
321 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
322 $attemptscore->scoes++;
324 if (!empty($userdata->score_raw)) {
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;
336 switch ($grademethod) {
337 case GRADEHIGHEST:
338 $score = $attemptscore->max;
339 break;
340 case GRADEAVERAGE:
341 if ($attemptscore->values > 0) {
342 $score = $attemptscore->sum/$attemptscore->values;
343 } else {
344 $score = 0;
346 break;
347 case GRADESUM:
348 $score = $attemptscore->sum;
349 break;
350 case GRADESCOES:
351 $score = $attemptscore->scoes;
352 break;
355 if ($time) {
356 $result = new stdClass();
357 $result->score = $score;
358 $result->time = $attemptscore->lastmodify;
359 } else {
360 $result = $score;
363 return $result;
366 function scorm_grade_user($scorm, $userid, $time=false) {
368 $whatgrade = intval($scorm->grademethod / 10);
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;
388 } else {
389 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
392 if ($time) {
393 $result = new stdClass();
394 $result->score = $maxscore;
395 $result->time = $attempttime;
396 return $result;
397 } else {
398 return $maxscore;
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;
413 if ($lastattempt > 0) {
414 $score = $sumscore / $lastattempt;
415 } else {
416 $score = 0;
419 if ($time) {
420 $result = new stdClass();
421 $result->score = $score;
422 $result->time = $attemptscore->time;
423 return $result;
424 } else {
425 return $score;
427 break;
431 function scorm_count_launchable($scormid,$organization='') {
432 $strorganization = '';
433 if (!empty($organization)) {
434 $strorganization = " AND organization='$organization'";
436 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>''");
439 function 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;
450 function scorm_course_format_display($user,$course) {
451 global $CFG;
453 $strupdate = get_string('update');
454 $strmodule = get_string('modulename','scorm');
455 $context = get_context_instance(CONTEXT_COURSE,$course->id);
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)) {
462 error('Course Module ID was incorrect');
464 $colspan = '';
465 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
466 if (has_capability('moodle/course:manageactivities', $context)) {
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().'">'.
472 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
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">'.
479 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
480 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
481 } else {
482 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
484 $colspan = ' colspan="2"';
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 {
490 if (has_capability('moodle/course:update', $context)) {
491 // Create a new activity
492 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
493 } else {
494 notify('Could not find a scorm course here');
497 echo '</div>';
500 function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
501 global $CFG;
503 if ($scorm->updatefreq == UPDATE_EVERYTIME){
504 $scorm->instance = $scorm->id;
505 scorm_update_instance($scorm);
508 $organization = optional_param('organization', '', PARAM_INT);
510 print_simple_box_start('center',$boxwidth);
512 <div class="structurehead"><?php print_string('contents','scorm') ?></div>
513 <?php
514 if (empty($organization)) {
515 $organization = $scorm->launch;
517 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
518 if (count($orgs) > 1) {
520 <div class='center'>
521 <?php print_string('organizations','scorm') ?>
522 <form id='changeorg' method='post' action='<?php echo $action ?>'>
523 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
524 </form>
525 </div>
526 <?php
529 $orgidentifier = '';
530 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
531 if (($sco->organization == '') && ($sco->launch == '')) {
532 $orgidentifier = $sco->identifier;
533 } else {
534 $orgidentifier = $sco->organization;
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;
548 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
549 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
550 $scorm->version = 'scorm_12';
552 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
554 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
555 $incomplete = $result->incomplete;
556 echo $result->toc;
557 print_simple_box_end();
560 <div class="center">
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"':'' ?>>
562 <?php
563 if ($scorm->hidebrowse == 0) {
564 print_string('mode','scorm');
565 echo '<input type="hidden" name="scoid" value="'.$sco->id.'" />'."\n";
566 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
567 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
568 } else {
569 echo '<input type="hidden" name="mode" value="normal" />'."\n";
571 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
573 <br />
574 <input type="checkbox" id="a" name="newattempt" />
575 <label for="a"><?php print_string('newattempt','scorm') ?></label>
576 <?php
579 <br />
580 <input type="hidden" name="scoid"/>
581 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
582 <input type="submit" value="<?php print_string('enter','scorm') ?>" />
583 </form>
584 </div>
585 <?php
587 function scorm_simple_play($scorm,$user) {
588 $result = false;
590 $scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>\'\'');
592 if (count($scoes) == 1) {
593 if ($scorm->skipview >= 1) {
594 $sco = current($scoes);
595 if (scorm_get_tracks($sco->id,$user->id) === false) {
596 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
597 $result = true;
598 } else if ($scorm->skipview == 2) {
599 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
600 $result = true;
604 return $result;
607 function scorm_simple_play($scorm,$user) {
608 $result = false;
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;
623 return $result;
626 function scorm_parse($scorm) {
627 global $CFG,$repositoryconfigfile;
629 if ($scorm->reference[0] == '#') {
630 require_once($repositoryconfigfile);
631 if ($CFG->repositoryactivate) {
632 $referencedir = $CFG->repository.substr($scorm->reference,1);
634 } else {
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;
642 // Parse scorm manifest
643 if ($scorm->pkgtype == 'AICC') {
644 require_once('datamodels/aicclib.php');
645 $scorm->launch = scorm_parse_aicc($referencedir, $scorm->id);
646 } else {
647 require_once('datamodels/scormlib.php');
648 if ($scorm->reference[0] == '#') {
649 require_once($repositoryconfigfile);
651 $scorm->launch = scorm_parse_scorm($referencedir,$scorm->id);
653 return $scorm->launch;
657 * Given a manifest path, this function will check if the manifest is valid
659 * @param string $manifest The manifest file
660 * @return object
662 function scorm_validate_manifest($manifest) {
663 $validation = new stdClass();
664 if (is_file($manifest)) {
665 $validation->result = true;
666 } else {
667 $validation->result = false;
668 $validation->errors['reference'] = get_string('nomanifest','scorm');
670 return $validation;
674 * Given a aicc package directory, this function will check if the course structure is valid
676 * @param string $packagedir The aicc package directory path
677 * @return object
679 function scorm_validate_aicc($packagedir) {
680 $validation = new stdClass();
681 $validation->result = false;
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') {
687 $validation->result = true;
688 break;
691 closedir($handle);
694 if ($validation->result == false) {
695 $validation->errors['reference'] = get_string('nomanifest','scorm');
697 return $validation;
701 function scorm_validate($data) {
702 global $CFG;
704 $validation = new stdClass();
705 $validation->errors = array();
707 if (!isset($data['course']) || empty($data['course'])) {
708 $validation->errors['reference'] = get_string('missingparam','scorm');
709 $validation->result = false;
710 return $validation;
712 $courseid = $data['course']; // Course Module ID
714 if (!isset($data['reference']) || empty($data['reference'])) {
715 $validation->errors['reference'] = get_string('packagefile','scorm');
716 $validation->result = false;
717 return $validation;
719 $reference = $data['reference']; // Package/manifest path/location
721 $scormid = $data['instance']; // scorm ID
722 $scorm = new stdClass();
723 if (!empty($scormid)) {
724 if (!$scorm = get_record('scorm','id',$scormid)) {
725 $validation->errors['reference'] = get_string('missingparam','scorm');
726 $validation->result = false;
727 return $validation;
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;
740 } else if (!scorm_external_link($reference)) {
741 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
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)) {
749 $localreference = $tempdir.'/'.basename($reference);
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;
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;
785 break;
786 default:
787 $validation->errors['reference'] = get_string('badpackage','scorm');
788 $validation->result = false;
789 break;
792 if (is_dir($tempdir)) {
793 // Delete files and temporary directory
794 scorm_delete_files($tempdir);
796 } else {
797 $validation->errors['reference'] = get_string('packagedir','scorm');
798 $validation->result = false;
800 } else {
801 $validation->errors['reference'] = get_string('datadir','scorm');
802 $validation->result = false;
804 return $validation;
807 function scorm_check_package($data) {
808 global $CFG, $COURSE;
810 $courseid = $data->course; // Course Module ID
811 $reference = $data->reference; // Package path
812 $scormid = $data->instance; // scorm ID
814 $validation = new stdClass();
816 if (!empty($courseid) && !empty($reference)) {
817 $externalpackage = scorm_external_link($reference);
819 $validation->launch = 0;
820 $referencefield = $reference;
821 if (empty($reference)) {
822 $validation = null;
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 {
829 $validation = null;
831 } else if (!$externalpackage) {
832 $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
835 if (!empty($scormid)) {
837 // SCORM Update
839 if ((!empty($validation)) && (is_file($reference) || $externalpackage)){
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)) {
846 copy ("$reference", $tempdir.'/'.basename($reference));
847 $mdcheck = md5_file($tempdir.'/'.basename($reference));
848 scorm_delete_files($tempdir);
853 if ($scorm = get_record('scorm','id',$scormid)) {
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;
861 } else if (!scorm_external_link($scorm->reference)) {
862 $oldreference = $CFG->dataroot.'/'.$courseid.'/'.$scorm->reference;
863 } else {
864 $oldreference = $scorm->reference;
866 $validation->launch = $scorm->launch;
867 if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash)) || ($oldreference != $reference)) {
868 // This is a new or a modified package
869 $validation->launch = 0;
870 } else {
871 // Old package already validated
872 if (strpos($scorm->version,'AICC') !== false) {
873 $validation->pkgtype = 'AICC';
874 } else {
875 $validation->pkgtype = 'SCORM';
878 } else {
879 $validation = null;
881 } else {
882 $validation = null;
885 //$validation->launch = 0;
886 if (($validation != null) && ($validation->launch == 0)) {
888 // Package must be validated
890 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
891 $tempdir = '';
892 switch ($ext) {
893 case '.pif':
894 case '.zip':
895 // Create a temporary directory to unzip package and validate package
896 $scormdir = '';
897 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
898 if ($tempdir = scorm_tempdir($scormdir)) {
899 copy ("$reference", $tempdir.'/'.basename($reference));
900 unzip_file($tempdir.'/'.basename($reference), $tempdir, false);
901 if (!$externalpackage) {
902 unlink ($tempdir.'/'.basename($reference));
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';
911 } else {
912 $validation = null;
914 } else {
915 $validation = null;
917 break;
918 case '.xml':
919 if (basename($reference) == 'imsmanifest.xml') {
920 if ($externalpackage) {
921 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
922 if ($tempdir = scorm_tempdir($scormdir)) {
923 copy ("$reference", $tempdir.'/'.basename($reference));
924 if (is_file($tempdir.'/'.basename($reference))) {
925 $validation = scorm_validate_manifest($tempdir.'/'.basename($reference));
926 } else {
927 $validation = null;
931 } else {
932 $validation = scorm_validate_manifest($reference);
934 $validation->pkgtype = 'SCORM';
935 } else {
936 $validation = null;
938 break;
939 default:
940 $validation = null;
941 break;
943 if ($validation == null) {
944 if (is_dir($tempdir)) {
945 // Delete files and temporary directory
946 scorm_delete_files($tempdir);
948 } else {
949 if (($ext == '.xml') && (!$externalpackage)) {
950 $validation->datadir = dirname($referencefield);
951 } else {
952 $validation->datadir = substr($tempdir,strlen($scormdir));
954 $validation->launch = 0;
957 } else {
958 $validation = null;
960 return $validation;
964 function scorm_get_count_users($scormid, $groupingid=null) {
966 global $CFG;
968 if (!empty($CFG->enablegroupings) && !empty($groupingid)) {
969 $sql = "SELECT COUNT(DISTINCT st.userid)
970 FROM {$CFG->prefix}scorm_scoes_track st
971 INNER JOIN {$CFG->prefix}groups_members gm ON st.userid = gm.userid
972 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
973 WHERE st.scormid = $scormid AND gg.groupingid = $groupingid
975 } else {
976 $sql = "SELECT COUNT(DISTINCT st.userid)
977 FROM {$CFG->prefix}scorm_scoes_track st
978 WHERE st.scormid = $scormid
982 return(count_records_sql($sql));