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
= '';
226 * Returns an object (array) containing all the scoes data related to the given sco ID
228 * @param integer $id The sco ID
229 * @param integer $organisation an organisation ID - defaults to false if not required
230 * @return mixed (false if there are no scoes or an array)
233 function scorm_get_scoes($id,$organisation=false) {
234 $organizationsql = '';
235 if (!empty($organisation)) {
236 $organizationsql = "AND organization='$organisation'";
238 if ($scoes = get_records_select('scorm_scoes',"scorm='$id' $organizationsql order by id ASC")) {
239 // drop keys so that it is a simple array as expected
240 $scoes = array_values($scoes);
241 foreach ($scoes as $sco) {
242 if ($scodatas = get_records('scorm_scoes_data','scoid',$sco->id
)) {
243 foreach ($scodatas as $scodata) {
244 $sco->{$scodata->name
} = stripslashes_safe($scodata->value
);
254 function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
256 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
257 $track->value
= $value;
258 $track->timemodified
= time();
259 $id = update_record('scorm_scoes_track',$track);
261 $track->userid
= $userid;
262 $track->scormid
= $scormid;
263 $track->scoid
= $scoid;
264 $track->attempt
= $attempt;
265 $track->element
= $element;
266 $track->value
= addslashes($value);
267 $track->timemodified
= time();
268 $id = insert_record('scorm_scoes_track',$track);
271 // MDL-9552, update the gradebook everything raw score is sent
272 if (strstr($element, '.score.raw')) {
273 $scorm = get_record('scorm', 'id', $scormid);
274 include_once('lib.php');
275 scorm_update_grades($scorm, $userid);
281 function scorm_get_tracks($scoid,$userid,$attempt='') {
282 /// Gets all tracks of specified sco and user
285 if (empty($attempt)) {
286 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
287 $attempt = scorm_get_last_attempt($scormid,$userid);
292 $attemptsql = ' AND attempt=' . $attempt;
293 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
294 $usertrack->userid
= $userid;
295 $usertrack->scoid
= $scoid;
296 // Defined in order to unify scorm1.2 and scorm2004
297 $usertrack->score_raw
= '';
298 $usertrack->status
= '';
299 $usertrack->total_time
= '00:00:00';
300 $usertrack->session_time
= '00:00:00';
301 $usertrack->timemodified
= 0;
302 foreach ($tracks as $track) {
303 $element = $track->element
;
304 $usertrack->{$element} = $track->value
;
306 case 'cmi.core.lesson_status':
307 case 'cmi.completion_status':
308 if ($track->value
== 'not attempted') {
309 $track->value
= 'notattempted';
311 $usertrack->status
= $track->value
;
313 case 'cmi.core.score.raw':
314 case 'cmi.score.raw':
315 $usertrack->score_raw
= $track->value
;
317 case 'cmi.core.session_time':
318 case 'cmi.session_time':
319 $usertrack->session_time
= $track->value
;
321 case 'cmi.core.total_time':
322 case 'cmi.total_time':
323 $usertrack->total_time
= $track->value
;
326 if (isset($track->timemodified
) && ($track->timemodified
> $usertrack->timemodified
)) {
327 $usertrack->timemodified
= $track->timemodified
;
330 if (is_array($usertrack)) {
339 function scorm_get_user_data($userid) {
340 /// Gets user info required to display the table of scorm results
343 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
346 function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
347 $attemptscore = NULL;
348 $attemptscore->scoes
= 0;
349 $attemptscore->values
= 0;
350 $attemptscore->max
= 0;
351 $attemptscore->sum
= 0;
352 $attemptscore->lastmodify
= 0;
354 if (!$scoes = get_records('scorm_scoes','scorm',$scorm->id
)) {
358 $grademethod = $scorm->grademethod %
10;
360 foreach ($scoes as $sco) {
361 if ($userdata = scorm_get_tracks($sco->id
, $userid, $attempt)) {
362 if (($userdata->status
== 'completed') ||
($userdata->status
== 'passed')) {
363 $attemptscore->scoes++
;
365 if (!empty($userdata->score_raw
)) {
366 $attemptscore->values++
;
367 $attemptscore->sum +
= $userdata->score_raw
;
368 $attemptscore->max
= ($userdata->score_raw
> $attemptscore->max
)?
$userdata->score_raw
:$attemptscore->max
;
369 if (isset($userdata->timemodified
) && ($userdata->timemodified
> $attemptscore->lastmodify
)) {
370 $attemptscore->lastmodify
= $userdata->timemodified
;
372 $attemptscore->lastmodify
= 0;
377 switch ($grademethod) {
379 $score = $attemptscore->max
;
382 if ($attemptscore->values
> 0) {
383 $score = $attemptscore->sum
/$attemptscore->values
;
389 $score = $attemptscore->sum
;
392 $score = $attemptscore->scoes
;
397 $result = new stdClass();
398 $result->score
= $score;
399 $result->time
= $attemptscore->lastmodify
;
407 function scorm_grade_user($scorm, $userid, $time=false) {
409 $whatgrade = intval($scorm->grademethod
/ 10);
411 switch ($whatgrade) {
413 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
416 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id
, $userid), $time);
419 $lastattempt = scorm_get_last_attempt($scorm->id
, $userid);
422 for ($attempt = 1; $attempt <= $lastattempt; $attempt++
) {
423 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
425 if ($attemptscore->score
> $maxscore) {
426 $maxscore = $attemptscore->score
;
427 $attempttime = $attemptscore->time
;
430 $maxscore = $attemptscore > $maxscore ?
$attemptscore: $maxscore;
434 $result = new stdClass();
435 $result->score
= $maxscore;
436 $result->time
= $attempttime;
443 $lastattempt = scorm_get_last_attempt($scorm->id
, $userid);
445 for ($attempt = 1; $attempt <= $lastattempt; $attempt++
) {
446 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
448 $sumscore +
= $attemptscore->score
;
450 $sumscore +
= $attemptscore;
454 if ($lastattempt > 0) {
455 $score = $sumscore / $lastattempt;
461 $result = new stdClass();
462 $result->score
= $score;
463 $result->time
= $attemptscore->time
;
472 function scorm_count_launchable($scormid,$organization='') {
473 $strorganization = '';
474 if (!empty($organization)) {
475 $strorganization = " AND organization='$organization'";
477 return count_records_select('scorm_scoes',"scorm=$scormid$strorganization AND launch<>'".sql_empty()."'");
480 function scorm_get_last_attempt($scormid, $userid) {
481 /// Find the last attempt number for the given user id and scorm id
482 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
483 if (empty($lastattempt->a
)) {
486 return $lastattempt->a
;
491 function scorm_course_format_display($user,$course) {
494 $strupdate = get_string('update');
495 $strmodule = get_string('modulename','scorm');
496 $context = get_context_instance(CONTEXT_COURSE
,$course->id
);
498 echo '<div class="mod-scorm">';
499 if ($scorms = get_all_instances_in_course('scorm', $course)) {
500 // The module SCORM activity with the least id is the course
501 $scorm = current($scorms);
502 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id
, $course->id
)) {
503 error('Course Module ID was incorrect');
506 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name
).'</b>';
507 if (has_capability('moodle/course:manageactivities', $context)) {
508 if (isediting($course->id
)) {
509 // Display update icon
510 $path = $CFG->wwwroot
.'/course';
511 $headertext .= '<span class="commands">'.
512 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id
.'&sesskey='.sesskey().'">'.
513 '<img src="'.$CFG->pixpath
.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
515 $headertext .= '</td>';
516 // Display report link
517 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id
, '', '', '', '', 'count(distinct(userid)) as c');
518 if ($trackedusers->c
> 0) {
519 $headertext .= '<td class="reportlink">'.
520 '<a '.$CFG->frametarget
.'" href="'.$CFG->wwwroot
.'/mod/scorm/report.php?id='.$cm->id
.'">'.
521 get_string('viewallreports','scorm',$trackedusers->c
).'</a>';
523 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
525 $colspan = ' colspan="2"';
527 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary
).'</td></tr></table>';
528 print_simple_box($headertext,'','100%');
529 scorm_view_display($user, $scorm, 'view.php?id='.$course->id
, $cm, '100%');
531 if (has_capability('moodle/course:update', $context)) {
532 // Create a new activity
533 redirect($CFG->wwwroot
.'/course/mod.php?id='.$course->id
.'&section=0&sesskey='.sesskey().'&add=scorm');
535 notify('Could not find a scorm course here');
541 function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
544 if ($scorm->updatefreq
== UPDATE_EVERYTIME
){
545 require_once($CFG->dirroot
.'/mod/scorm/lib.php');
547 $scorm->instance
= $scorm->id
;
548 scorm_update_instance($scorm);
551 $organization = optional_param('organization', '', PARAM_INT
);
553 print_simple_box_start('center',$boxwidth);
555 <div
class="structurehead"><?php
print_string('contents','scorm') ?
></div
>
557 if (empty($organization)) {
558 $organization = $scorm->launch
;
560 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
561 if (count($orgs) > 1) {
563 <div
class='scorm-center'>
564 <?php
print_string('organizations','scorm') ?
>
565 <form id
='changeorg' method
='post' action
='<?php echo $action ?>'>
566 <?php
choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?
>
573 if ($sco = scorm_get_sco($organization, SCO_ONLY
)) {
574 if (($sco->organization
== '') && ($sco->launch
== '')) {
575 $orgidentifier = $sco->identifier
;
577 $orgidentifier = $sco->organization
;
583 if ($org = get_record('scorm_scoes','id',$organization)) {
584 if (($org->organization == '') && ($org->launch == '')) {
585 $orgidentifier = $org->identifier;
587 $orgidentifier = $org->organization;
591 $scorm->version
= strtolower(clean_param($scorm->version
, PARAM_SAFEDIR
)); // Just to be safe
592 if (!file_exists($CFG->dirroot
.'/mod/scorm/datamodels/'.$scorm->version
.'lib.php')) {
593 $scorm->version
= 'scorm_12';
595 require_once($CFG->dirroot
.'/mod/scorm/datamodels/'.$scorm->version
.'lib.php');
597 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
598 $incomplete = $result->incomplete
;
600 print_simple_box_end();
603 <div
class="scorm-center">
604 <form id
="theform" method
="post" action
="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?scoid=<?php echo $sco->id ?>&id=<?php echo $cm->id ?>">
606 if ($scorm->hidebrowse
== 0) {
607 print_string('mode','scorm');
608 echo '<input type="hidden" name="scoid" value="'.$sco->id
.'" />'."\n";
609 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
610 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
612 echo '<input type="hidden" name="mode" value="normal" />'."\n";
614 if (($incomplete === false) && (($result->attemptleft
> 0)||
($scorm->maxattempt
== 0))) {
617 <input type
="checkbox" id
="a" name
="newattempt" />
618 <label
for="a"><?php
print_string('newattempt','scorm') ?
></label
>
623 <input type
="hidden" name
="scoid"/>
624 <input type
="hidden" name
="currentorg" value
="<?php echo $orgidentifier ?>" />
625 <input type
="submit" value
="<?php print_string('enter','scorm') ?>" />
630 function scorm_simple_play($scorm,$user) {
633 $scoes = get_records_select('scorm_scoes','scorm='.$scorm->id
.' AND launch<>\''.sql_empty().'\'');
635 if ($scoes && (count($scoes) == 1)) {
636 if ($scorm->skipview
>= 1) {
637 $sco = current($scoes);
638 if (scorm_get_tracks($sco->id
,$user->id
) === false) {
639 header('Location: player.php?a='.$scorm->id
.'&scoid='.$sco->id
);
641 } else if ($scorm->skipview
== 2) {
642 header('Location: player.php?a='.$scorm->id
.'&scoid='.$sco->id
);
650 function scorm_simple_play($scorm,$user) {
652 if ($scoes = get_records_select('scorm_scoes','scorm='.$scorm->id.' AND launch<>""')) {
653 if (count($scoes) == 1) {
654 if ($scorm->skipview >= 1) {
655 $sco = current($scoes);
656 if (scorm_get_tracks($sco->id,$user->id) === false) {
657 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
659 } else if ($scorm->skipview == 2) {
660 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
669 function scorm_parse($scorm) {
670 global $CFG,$repositoryconfigfile;
672 if ($scorm->reference
[0] == '#') {
673 require_once($repositoryconfigfile);
674 if ($CFG->repositoryactivate
) {
675 $referencedir = $CFG->repository
.substr($scorm->reference
,1);
678 if ((!scorm_external_link($scorm->reference
)) && (basename($scorm->reference
) == 'imsmanifest.xml')) {
679 $referencedir = $CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->datadir
;
681 $referencedir = $CFG->dataroot
.'/'.$scorm->course
.'/moddata/scorm/'.$scorm->id
;
685 // Parse scorm manifest
686 if ($scorm->pkgtype
== 'AICC') {
687 require_once('datamodels/aicclib.php');
688 $scorm->launch
= scorm_parse_aicc($referencedir, $scorm->id
);
690 require_once('datamodels/scormlib.php');
691 if ($scorm->reference
[0] == '#') {
692 require_once($repositoryconfigfile);
694 $scorm->launch
= scorm_parse_scorm($referencedir,$scorm->id
);
696 return $scorm->launch
;
700 * Given a manifest path, this function will check if the manifest is valid
702 * @param string $manifest The manifest file
705 function scorm_validate_manifest($manifest) {
706 $validation = new stdClass();
707 if (is_file($manifest)) {
708 $validation->result
= true;
710 $validation->result
= false;
711 $validation->errors
['reference'] = get_string('nomanifest','scorm');
717 * Given a aicc package directory, this function will check if the course structure is valid
719 * @param string $packagedir The aicc package directory path
722 function scorm_validate_aicc($packagedir) {
723 $validation = new stdClass();
724 $validation->result
= false;
725 if (is_dir($packagedir)) {
726 if ($handle = opendir($packagedir)) {
727 while (($file = readdir($handle)) !== false) {
728 $ext = substr($file,strrpos($file,'.'));
729 if (strtolower($ext) == '.cst') {
730 $validation->result
= true;
737 if ($validation->result
== false) {
738 $validation->errors
['reference'] = get_string('nomanifest','scorm');
744 function scorm_validate($data) {
747 $validation = new stdClass();
748 $validation->errors
= array();
750 if (!isset($data['course']) ||
empty($data['course'])) {
751 $validation->errors
['reference'] = get_string('missingparam','scorm');
752 $validation->result
= false;
755 $courseid = $data['course']; // Course Module ID
757 if (!isset($data['reference']) ||
empty($data['reference'])) {
758 $validation->errors
['reference'] = get_string('packagefile','scorm');
759 $validation->result
= false;
762 $reference = $data['reference']; // Package/manifest path/location
764 $scormid = $data['instance']; // scorm ID
765 $scorm = new stdClass();
766 if (!empty($scormid)) {
767 if (!$scorm = get_record('scorm','id',$scormid)) {
768 $validation->errors
['reference'] = get_string('missingparam','scorm');
769 $validation->result
= false;
774 if ($reference[0] == '#') {
775 require_once($repositoryconfigfile);
776 if ($CFG->repositoryactivate
) {
777 $reference = $CFG->repository
.substr($reference,1).'/imsmanifest.xml';
779 $validation->errors
['reference'] = get_string('badpackage','scorm');
780 $validation->result
= false;
783 } else if (!scorm_external_link($reference)) {
784 $reference = $CFG->dataroot
.'/'.$courseid.'/'.$reference;
787 // Create a temporary directory to unzip package or copy manifest and validate package
790 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
791 if ($tempdir = scorm_tempdir($scormdir)) {
792 $localreference = $tempdir.'/'.basename($reference);
793 copy ("$reference", $localreference);
794 if (!is_file($localreference)) {
795 $validation->errors
['reference'] = get_string('badpackage','scorm');
796 $validation->result
= false;
798 $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
802 if (!unzip_file($localreference, $tempdir, false)) {
803 $validation->errors
['reference'] = get_string('unziperror','scorm');
804 $validation->result
= false;
806 unlink ($localreference);
807 if (is_file($tempdir.'/imsmanifest.xml')) {
808 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
809 $validation->pkgtype
= 'SCORM';
811 $validation = scorm_validate_aicc($tempdir);
812 if (($validation->result
== 'regular') ||
($validation->result
== 'found')) {
813 $validation->pkgtype
= 'AICC';
815 $validation->errors
['reference'] = get_string('nomanifest','scorm');
816 $validation->result
= false;
822 if (basename($localreference) == 'imsmanifest.xml') {
823 $validation = scorm_validate_manifest($localreference);
825 $validation->errors
['reference'] = get_string('nomanifest','scorm');
826 $validation->result
= false;
830 $validation->errors
['reference'] = get_string('badpackage','scorm');
831 $validation->result
= false;
835 if (is_dir($tempdir)) {
836 // Delete files and temporary directory
837 scorm_delete_files($tempdir);
840 $validation->errors
['reference'] = get_string('packagedir','scorm');
841 $validation->result
= false;
844 $validation->errors
['reference'] = get_string('datadir','scorm');
845 $validation->result
= false;
850 function scorm_check_package($data) {
851 global $CFG, $COURSE;
853 $courseid = $data->course
; // Course Module ID
854 $reference = $data->reference
; // Package path
855 $scormid = $data->instance
; // scorm ID
857 $validation = new stdClass();
859 if (!empty($courseid) && !empty($reference)) {
860 $externalpackage = scorm_external_link($reference);
862 $validation->launch
= 0;
863 $referencefield = $reference;
864 if (empty($reference)) {
866 } else if ($reference[0] == '#') {
867 require_once($repositoryconfigfile);
868 if ($CFG->repositoryactivate
) {
869 $referencefield = $reference.'/imsmanifest.xml';
870 $reference = $CFG->repository
.substr($reference,1).'/imsmanifest.xml';
874 } else if (!$externalpackage) {
875 $reference = $CFG->dataroot
.'/'.$courseid.'/'.$reference;
878 if (!empty($scormid)) {
882 if ((!empty($validation)) && (is_file($reference) ||
$externalpackage)){
884 if (!$externalpackage) {
885 $mdcheck = md5_file($reference);
886 } else if ($externalpackage){
887 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
888 if ($tempdir = scorm_tempdir($scormdir)) {
889 copy ("$reference", $tempdir.'/'.basename($reference));
890 $mdcheck = md5_file($tempdir.'/'.basename($reference));
891 scorm_delete_files($tempdir);
896 if ($scorm = get_record('scorm','id',$scormid)) {
897 if ($scorm->reference
[0] == '#') {
898 require_once($repositoryconfigfile);
899 if ($CFG->repositoryactivate
) {
900 $oldreference = $CFG->repository
.substr($scorm->reference
,1).'/imsmanifest.xml';
902 $oldreference = $scorm->reference
;
904 } else if (!scorm_external_link($scorm->reference
)) {
905 $oldreference = $CFG->dataroot
.'/'.$courseid.'/'.$scorm->reference
;
907 $oldreference = $scorm->reference
;
909 $validation->launch
= $scorm->launch
;
910 if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash
)) ||
($oldreference != $reference)) {
911 // This is a new or a modified package
912 $validation->launch
= 0;
914 // Old package already validated
915 if (strpos($scorm->version
,'AICC') !== false) {
916 $validation->pkgtype
= 'AICC';
918 $validation->pkgtype
= 'SCORM';
928 //$validation->launch = 0;
929 if (($validation != null) && ($validation->launch
== 0)) {
931 // Package must be validated
933 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
938 // Create a temporary directory to unzip package and validate package
940 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
941 if ($tempdir = scorm_tempdir($scormdir)) {
942 copy ("$reference", $tempdir.'/'.basename($reference));
943 unzip_file($tempdir.'/'.basename($reference), $tempdir, false);
944 if (!$externalpackage) {
945 unlink ($tempdir.'/'.basename($reference));
947 if (is_file($tempdir.'/imsmanifest.xml')) {
948 $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
949 $validation->pkgtype
= 'SCORM';
951 $validation = scorm_validate_aicc($tempdir);
952 $validation->pkgtype
= 'AICC';
962 if (basename($reference) == 'imsmanifest.xml') {
963 if ($externalpackage) {
964 if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
965 if ($tempdir = scorm_tempdir($scormdir)) {
966 copy ("$reference", $tempdir.'/'.basename($reference));
967 if (is_file($tempdir.'/'.basename($reference))) {
968 $validation = scorm_validate_manifest($tempdir.'/'.basename($reference));
975 $validation = scorm_validate_manifest($reference);
977 $validation->pkgtype
= 'SCORM';
986 if ($validation == null) {
987 if (is_dir($tempdir)) {
988 // Delete files and temporary directory
989 scorm_delete_files($tempdir);
992 if (($ext == '.xml') && (!$externalpackage)) {
993 $validation->datadir
= dirname($referencefield);
995 $validation->datadir
= substr($tempdir,strlen($scormdir));
997 $validation->launch
= 0;
1007 function scorm_get_count_users($scormid, $groupingid=null) {
1011 if (!empty($CFG->enablegroupings
) && !empty($groupingid)) {
1012 $sql = "SELECT COUNT(DISTINCT st.userid)
1013 FROM {$CFG->prefix}scorm_scoes_track st
1014 INNER JOIN {$CFG->prefix}groups_members gm ON st.userid = gm.userid
1015 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
1016 WHERE st.scormid = $scormid AND gg.groupingid = $groupingid
1019 $sql = "SELECT COUNT(DISTINCT st.userid)
1020 FROM {$CFG->prefix}scorm_scoes_track st
1021 WHERE st.scormid = $scormid
1025 return(count_records_sql($sql));