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');
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,
39 foreach ($SCORM_POPUP_OPTIONS as $popupopt => $value) {
40 $stdoptions .= $popupopt.'='.$value;
41 if ($popupopt != 'status') {
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
79 * This function will permanently delete the given
80 * directory and all files and subdirectories.
82 * @param string $directory The directory to remove
85 function scorm_delete_files($directory) {
86 if (is_dir($directory)) {
87 $files=scorm_scandir($directory);
89 foreach($files as $file) {
90 if (($file != '.') && ($file != '..')) {
91 if (!is_dir($directory.'/'.$file)) {
92 unlink($directory.'/'.$file);
94 scorm_delete_files($directory.'/'.$file);
105 * Given a diretory path returns the file list
107 * @param string $directory
110 function scorm_scandir($directory) {
111 if (version_compare(phpversion(),'5.0.0','>=')) {
112 return scandir($directory);
115 if ($dh = opendir($directory)) {
116 while (($file = readdir($dh)) !== false) {
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)
135 if (is_dir($strPath)) {
137 // Create a random string of 8 chars
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;
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;
160 function scorm_array_search($item, $needle, $haystacks, $strict=false) {
161 if (!empty($haystacks)) {
162 foreach ($haystacks as $key => $element) {
164 if ($element->{$item} === $needle) {
168 if ($element->{$item} == $needle) {
177 function scorm_repeater($what, $times) {
182 for ($i=0; $i<$times;$i++
) {
188 function scorm_external_link($link) {
189 // check if a link is external
191 $link = strtolower($link);
192 if (substr($link,0,7) == 'http://') {
194 } else if (substr($link,0,8) == 'https://') {
196 } else if (substr($link,0,4) == 'www.') {
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
= '';
224 function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
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);
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);
243 function scorm_get_tracks($scoid,$userid,$attempt='') {
244 /// Gets all tracks of specified sco and user
247 if (empty($attempt)) {
248 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
249 $attempt = scorm_get_last_attempt($scormid,$userid);
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
;
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
;
275 case 'cmi.core.score.raw':
276 case 'cmi.score.raw':
277 $usertrack->score_raw
= $track->value
;
279 case 'cmi.core.session_time':
280 case 'cmi.session_time':
281 $usertrack->session_time
= $track->value
;
283 case 'cmi.core.total_time':
284 case 'cmi.total_time':
285 $usertrack->total_time
= $track->value
;
288 if (isset($track->timemodified
) && ($track->timemodified
> $usertrack->timemodified
)) {
289 $usertrack->timemodified
= $track->timemodified
;
298 function scorm_get_user_data($userid) {
299 /// Gets user info required to display the table of scorm results
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
)) {
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
;
331 $attemptscore->lastmodify
= 0;
336 switch ($grademethod) {
338 $score = $attemptscore->max
;
341 if ($attemptscore->values
> 0) {
342 $score = $attemptscore->sum
/$attemptscore->values
;
348 $score = $attemptscore->sum
;
351 $score = $attemptscore->scoes
;
356 $result = new stdClass();
357 $result->score
= $score;
358 $result->time
= $attemptscore->lastmodify
;
366 function scorm_grade_user($scorm, $userid, $time=false) {
368 $whatgrade = intval($scorm->grademethod
/ 10);
370 switch ($whatgrade) {
372 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
375 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id
, $userid), $time);
378 $lastattempt = scorm_get_last_attempt($scorm->id
, $userid);
381 for ($attempt = 1; $attempt <= $lastattempt; $attempt++
) {
382 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
384 if ($attemptscore->score
> $maxscore) {
385 $maxscore = $attemptscore->score
;
386 $attempttime = $attemptscore->time
;
389 $maxscore = $attemptscore > $maxscore ?
$attemptscore: $maxscore;
393 $result = new stdClass();
394 $result->score
= $maxscore;
395 $result->time
= $attempttime;
402 $lastattempt = scorm_get_last_attempt($scorm->id
, $userid);
404 for ($attempt = 1; $attempt <= $lastattempt; $attempt++
) {
405 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
407 $sumscore +
= $attemptscore->score
;
409 $sumscore +
= $attemptscore;
413 if ($lastattempt > 0) {
414 $score = $sumscore / $lastattempt;
420 $result = new stdClass();
421 $result->score
= $score;
422 $result->time
= $attemptscore->time
;
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
)) {
445 return $lastattempt->a
;
450 function scorm_course_format_display($user,$course) {
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');
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
.'&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>';
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%');
490 if (has_capability('moodle/course:update', $context)) {
491 // Create a new activity
492 redirect($CFG->wwwroot
.'/course/mod.php?id='.$course->id
.'&section=0&sesskey='.sesskey().'&add=scorm');
494 notify('Could not find a scorm course here');
500 function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
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
>
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) {
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()') ?
>
530 if ($sco = scorm_get_sco($organization, SCO_ONLY
)) {
531 if (($sco->organization
== '') && ($sco->launch
== '')) {
532 $orgidentifier = $sco->identifier
;
534 $orgidentifier = $sco->organization
;
540 if ($org = get_record('scorm_scoes','id',$organization)) {
541 if (($org->organization == '') && ($org->launch == '')) {
542 $orgidentifier = $org->identifier;
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
;
557 print_simple_box_end();
561 <form id
="theform" method
="post" action
="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?scoid=<?php echo $sco->id ?>&id=<?php echo $cm->id ?>"<?php
echo $scorm->popup
== 1?
' target="newwin"':'' ?
>>
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";
569 echo '<input type="hidden" name="mode" value="normal" />'."\n";
571 if (($incomplete === false) && (($result->attemptleft
> 0)||
($scorm->maxattempt
== 0))) {
574 <input type
="checkbox" id
="a" name
="newattempt" />
575 <label
for="a"><?php
print_string('newattempt','scorm') ?
></label
>
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') ?>" />
587 function scorm_simple_play($scorm,$user) {
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
);
598 } else if ($scorm->skipview
== 2) {
599 header('Location: player.php?a='.$scorm->id
.'&scoid='.$sco->id
);
607 function scorm_simple_play($scorm,$user) {
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);
616 } else if ($scorm->skipview == 2) {
617 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
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);
635 if ((!scorm_external_link($scorm->reference
)) && (basename($scorm->reference
) == 'imsmanifest.xml')) {
636 $referencedir = $CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->datadir
;
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
);
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
662 function scorm_validate_manifest($manifest) {
663 $validation = new stdClass();
664 if (is_file($manifest)) {
665 $validation->result
= true;
667 $validation->result
= false;
668 $validation->errors
['reference'] = get_string('nomanifest','scorm');
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
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;
694 if ($validation->result
== false) {
695 $validation->errors
['reference'] = get_string('nomanifest','scorm');
701 function scorm_validate($data) {
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;
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;
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;
731 if ($reference[0] == '#') {
732 require_once($repositoryconfigfile);
733 if ($CFG->repositoryactivate
) {
734 $reference = $CFG->repository
.substr($reference,1).'/imsmanifest.xml';
736 $validation->errors
['reference'] = get_string('badpackage','scorm');
737 $validation->result
= false;
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
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;
755 $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
759 if (!unzip_file($localreference, $tempdir, false)) {
760 $validation->errors
['reference'] = get_string('unziperror','scorm');
761 $validation->result
= false;
763 unlink ($localreference);
764 if (is_file($tempdir.'/imsmanifest.xml')) {
765 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
766 $validation->pkgtype
= 'SCORM';
768 $validation = scorm_validate_aicc($tempdir);
769 if (($validation->result
== 'regular') ||
($validation->result
== 'found')) {
770 $validation->pkgtype
= 'AICC';
772 $validation->errors
['reference'] = get_string('nomanifest','scorm');
773 $validation->result
= false;
779 if (basename($localreference) == 'imsmanifest.xml') {
780 $validation = scorm_validate_manifest($localreference);
782 $validation->errors
['reference'] = get_string('nomanifest','scorm');
783 $validation->result
= false;
787 $validation->errors
['reference'] = get_string('badpackage','scorm');
788 $validation->result
= false;
792 if (is_dir($tempdir)) {
793 // Delete files and temporary directory
794 scorm_delete_files($tempdir);
797 $validation->errors
['reference'] = get_string('packagedir','scorm');
798 $validation->result
= false;
801 $validation->errors
['reference'] = get_string('datadir','scorm');
802 $validation->result
= false;
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)) {
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';
831 } else if (!$externalpackage) {
832 $reference = $CFG->dataroot
.'/'.$courseid.'/'.$reference;
835 if (!empty($scormid)) {
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';
859 $oldreference = $scorm->reference
;
861 } else if (!scorm_external_link($scorm->reference
)) {
862 $oldreference = $CFG->dataroot
.'/'.$courseid.'/'.$scorm->reference
;
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;
871 // Old package already validated
872 if (strpos($scorm->version
,'AICC') !== false) {
873 $validation->pkgtype
= 'AICC';
875 $validation->pkgtype
= 'SCORM';
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),'.')));
895 // Create a temporary directory to unzip package and validate package
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';
908 $validation = scorm_validate_aicc($tempdir);
909 $validation->pkgtype
= 'AICC';
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));
932 $validation = scorm_validate_manifest($reference);
934 $validation->pkgtype
= 'SCORM';
943 if ($validation == null) {
944 if (is_dir($tempdir)) {
945 // Delete files and temporary directory
946 scorm_delete_files($tempdir);
949 if (($ext == '.xml') && (!$externalpackage)) {
950 $validation->datadir
= dirname($referencefield);
952 $validation->datadir
= substr($tempdir,strlen($scormdir));
954 $validation->launch
= 0;
964 function scorm_get_count_users($scormid, $groupingid=null) {
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
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));