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