fixed defaults
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
2b3447c3 3/// Local Library of functions for module scorm
f69db63e 4
5c1ac70c 5/**
6* This function will permanently delete the given
7* directory and all files and subdirectories.
8*
9* @param string $directory The directory to remove
10* @return boolean
11*/
12function scorm_delete_files($directory) {
13 if (is_dir($directory)) {
14 $files=scorm_scandir($directory);
15 foreach($files as $file) {
16 if (($file != '.') && ($file != '..')) {
17 if (!is_dir($directory.'/'.$file)) {
18 unlink($directory.'/'.$file);
19 } else {
20 scorm_delete_files($directory.'/'.$file);
21 }
f69db63e 22 }
5c1ac70c 23 set_time_limit(5);
f69db63e 24 }
5c1ac70c 25 rmdir($directory);
26 return true;
f69db63e 27 }
5c1ac70c 28 return false;
f69db63e 29}
30
5c1ac70c 31/**
32* Given a diretory path returns the file list
33*
34* @param string $directory
35* @return array
36*/
37function scorm_scandir($directory) {
38 if (version_compare(phpversion(),'5.0.0','>=')) {
39 return scandir($directory);
f69db63e 40 } else {
5c1ac70c 41 $files = array();
42 if ($dh = opendir($directory)) {
43 while (($file = readdir($dh)) !== false) {
44 $files[] = $file;
45 }
46 closedir($dh);
47 }
48 return $files;
f69db63e 49 }
f69db63e 50}
51
e4aa175a 52/**
53* Create a new temporary subdirectory with a random name in the given path
54*
55* @param string $strpath The scorm data directory
56* @return string/boolean
57*/
58function scorm_datadir($strPath)
59{
60 global $CFG;
61
62 if (is_dir($strPath)) {
63 do {
64 // Create a random string of 8 chars
65 $randstring = NULL;
66 $lchar = '';
67 $len = 8;
68 for ($i=0; $i<$len; $i++) {
69 $char = chr(rand(48,122));
70 while (!ereg('[a-zA-Z0-9]', $char)){
71 if ($char == $lchar) continue;
72 $char = chr(rand(48,90));
73 }
74 $randstring .= $char;
75 $lchar = $char;
76 }
77 $datadir='/'.$randstring;
78 } while (file_exists($strPath.$datadir));
79 mkdir($strPath.$datadir, $CFG->directorypermissions);
80 @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
81 return $strPath.$datadir;
82 } else {
83 return false;
84 }
85}
86
2b3447c3 87function scorm_array_search($item, $needle, $haystacks, $strict=false) {
88 if (!empty($haystacks)) {
89 foreach ($haystacks as $key => $element) {
90 if ($strict) {
91 if ($element->{$item} === $needle) {
92 return $key;
93 }
94 } else {
95 if ($element->{$item} == $needle) {
96 return $key;
e4aa175a 97 }
98 }
e4aa175a 99 }
100 }
2b3447c3 101 return false;
e4aa175a 102}
103
2b3447c3 104function scorm_repeater($what, $times) {
105 if ($times <= 0) {
106 return null;
107 }
108 $return = '';
109 for ($i=0; $i<$times;$i++) {
110 $return .= $what;
111 }
112 return $return;
113}
e4aa175a 114
2b3447c3 115function scorm_external_link($link) {
116// check if a link is external
117 $result = false;
118 $link = strtolower($link);
119 if (substr($link,0,7) == 'http://') {
120 $result = true;
121 } else if (substr($link,0,8) == 'https://') {
122 $result = true;
123 } else if (substr($link,0,4) == 'www.') {
124 $result = true;
125 }
126 return $result;
e4aa175a 127}
128
129function scorm_string_wrap($stringa, $len=15) {
130// Crop the given string into max $len characters lines
131 $textlib = textlib_get_instance();
132 if ($textlib->strlen($stringa, current_charset()) > $len) {
133 $words = explode(' ', $stringa);
134 $newstring = '';
135 $substring = '';
136 foreach ($words as $word) {
137 if (($textlib->strlen($substring, current_charset())+$textlib->strlen($word, current_charset())+1) < $len) {
138 $substring .= ' '.$word;
139 } else {
140 $newstring .= ' '.$substring.'<br />';
141 $substring = $word;
142 }
143 }
144 if (!empty($substring)) {
145 $newstring .= ' '.$substring;
146 }
147 return $newstring;
148 } else {
149 return $stringa;
150 }
151}
152
2b3447c3 153/**
154* Given a package directory, this function will check if the package is valid
155*
156* @param string $packagedir The package directory
157* @return mixed
158*/
159function scorm_validate($packagedir) {
160 $validation = new stdClass();
161 if (is_file($packagedir.'/imsmanifest.xml')) {
162 $validation->result = 'found';
163 $validation->pkgtype = 'SCORM';
164 } else {
165 if ($handle = opendir($packagedir)) {
166 while (($file = readdir($handle)) !== false) {
167 $ext = substr($file,strrpos($file,'.'));
168 if (strtolower($ext) == '.cst') {
169 $validation->result = 'found';
170 $validation->pkgtype = 'AICC';
171 break;
e4aa175a 172 }
e4aa175a 173 }
2b3447c3 174 closedir($handle);
175 }
176 if (!isset($validation->result)) {
177 $validation->result = 'nomanifest';
178 $validation->pkgtype = 'SCORM';
e4aa175a 179 }
e4aa175a 180 }
2b3447c3 181 return $validation;
e4aa175a 182}
183
e4aa175a 184function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value) {
e4aa175a 185 $id = null;
186 if ($track = get_record_select('scorm_scoes_track',"userid='$userid' AND scormid='$scormid' AND scoid='$scoid' AND attempt='$attempt' AND element='$element'")) {
187 $track->value = $value;
188 $track->timemodified = time();
e4aa175a 189 $id = update_record('scorm_scoes_track',$track);
190 } else {
191 $track->userid = $userid;
192 $track->scormid = $scormid;
193 $track->scoid = $scoid;
194 $track->attempt = $attempt;
195 $track->element = $element;
196 $track->value = addslashes($value);
197 $track->timemodified = time();
e4aa175a 198 $id = insert_record('scorm_scoes_track',$track);
199 }
200 return $id;
201}
202
e4aa175a 203function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 204/// Gets all tracks of specified sco and user
205 global $CFG;
206
207 if (empty($attempt)) {
208 if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {
209 $attempt = scorm_get_last_attempt($scormid,$userid);
210 } else {
211 $attempt = 1;
212 }
213 }
214 $attemptsql = ' AND attempt=' . $attempt;
215 if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
216 $usertrack->userid = $userid;
217 $usertrack->scoid = $scoid;
218 $usertrack->score_raw = '';
e4aa175a 219 $usertrack->status = '';
e4aa175a 220 $usertrack->total_time = '00:00:00';
221 $usertrack->session_time = '00:00:00';
222 $usertrack->timemodified = 0;
f69db63e 223 // Added by Pham Minh Duc
224 $usertrack->score_scaled = '';
225 $usertrack->success_status = '';
226 $usertrack->attempt_status = '';
227 $usertrack->satisfied_status = '';
228 // End Add
e4aa175a 229 foreach ($tracks as $track) {
230 $element = $track->element;
231 $usertrack->{$element} = $track->value;
232 switch ($element) {
f69db63e 233 // Added by Pham Minh Duc
e4aa175a 234 case 'cmi.attempt_status':
235 $usertrack->status = $track->value;
236 $usertrack->attempt_status = $track->value;
237 break;
e4aa175a 238 case 'cmi.success_status':
239 $usertrack->success_status = $track->value;
240 if ($track->value=='passed'){
241 $usertrack->satisfied_status = 'satisfied';
242 }
243 if ($track->value=='failed'){
244 $usertrack->satisfied_status = 'notSatisfied';
245 }
246 break;
f69db63e 247 case 'cmi.score.scaled':
248 $usertrack->score_scaled = $track->value;
249 break;
250 // End Add
251 case 'cmi.core.lesson_status':
252 case 'cmi.completion_status':
253 if ($track->value == 'not attempted') {
254 $track->value = 'notattempted';
255 }
256 $usertrack->status = $track->value;
257 break;
e4aa175a 258 case 'cmi.core.score.raw':
259 case 'cmi.score.raw':
260 $usertrack->score_raw = $track->value;
261 break;
e4aa175a 262 case 'cmi.core.session_time':
263 case 'cmi.session_time':
264 $usertrack->session_time = $track->value;
265 break;
266 case 'cmi.core.total_time':
267 case 'cmi.total_time':
268 $usertrack->total_time = $track->value;
269 break;
270 }
271 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
272 $usertrack->timemodified = $track->timemodified;
273 }
274 }
275 return $usertrack;
276 } else {
277 return false;
278 }
279}
280
2b3447c3 281function scorm_get_user_data($userid) {
282/// Gets user info required to display the table of scorm results
283/// for report.php
e4aa175a 284
2b3447c3 285 return get_record('user','id',$userid,'','','','','firstname, lastname, picture');
286}
e4aa175a 287
2b3447c3 288function scorm_grade_user($scoes, $userid, $grademethod=VALUESCOES) {
289 $scores = NULL;
290 $scores->scoes = 0;
291 $scores->values = 0;
292 $scores->max = 0;
293 $scores->sum = 0;
e4aa175a 294
2b3447c3 295 if (!$scoes) {
296 return '';
e4aa175a 297 }
e4aa175a 298
2b3447c3 299 $current = current($scoes);
300 $attempt = scorm_get_last_attempt($current->scorm, $userid);
301 foreach ($scoes as $sco) {
302 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
303 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
304 $scores->scoes++;
305 }
306 if (!empty($userdata->score_raw)) {
307 $scores->values++;
308 $scores->sum += $userdata->score_raw;
309 $scores->max = ($userdata->score_raw > $scores->max)?$userdata->score_raw:$scores->max;
310 }
311 }
e4aa175a 312 }
2b3447c3 313 switch ($grademethod) {
314 case VALUEHIGHEST:
315 return $scores->max;
316 break;
317 case VALUEAVERAGE:
318 if ($scores->values > 0) {
319 return $scores->sum/$scores->values;
5c1ac70c 320 } else {
2b3447c3 321 return 0;
322 }
323 break;
324 case VALUESUM:
325 return $scores->sum;
326 break;
327 case VALUESCOES:
328 return $scores->scoes;
329 break;
5c1ac70c 330 }
e4aa175a 331}
332
2b3447c3 333function scorm_count_launchable($scormid,$organization) {
334 return count_records_select('scorm_scoes',"scorm=$scormid AND organization='$organization' AND launch<>''");
e4aa175a 335}
336
2b3447c3 337function scorm_get_last_attempt($scormid, $userid) {
338/// Find the last attempt number for the given user id and scorm id
339 if ($lastattempt = get_record('scorm_scoes_track', 'userid', $userid, 'scormid', $scormid, '', '', 'max(attempt) as a')) {
340 if (empty($lastattempt->a)) {
341 return '1';
342 } else {
343 return $lastattempt->a;
e4aa175a 344 }
345 }
e4aa175a 346}
347
2b3447c3 348function scorm_parse($scorm) {
349 global $CFG,$repositoryconfigfile;
e4aa175a 350
2b3447c3 351 // Parse scorm manifest
352 if ($scorm->pkgtype == 'AICC') {
353 require_once('datamodels/aicclib.php');
354 $scorm->launch = scorm_parse_aicc($scorm->dir.'/'.$scorm->id,$scorm->id);
355 } else {
356 require_once('datamodels/scormlib.php');
357 $reference = $scorm->reference;
358 if ($scorm->reference[0] == '#') {
359 require_once($repositoryconfigfile);
360 $reference = $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml';
361 } else if (substr($reference,0,7) != 'http://') {
362 $reference = $CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference;
e4aa175a 363 }
e4aa175a 364
2b3447c3 365 if (basename($reference) != 'imsmanifest.xml') {
366 $scorm->launch = scorm_parse_scorm($scorm->dir.'/'.$scorm->id,$scorm->id);
367 } else {
368 $scorm->launch = scorm_parse_scorm(dirname($reference),$scorm->id);
5c1ac70c 369 }
370 }
2b3447c3 371 return $scorm->launch;
5c1ac70c 372}
373
e4aa175a 374function scorm_course_format_display($user,$course) {
375 global $CFG;
376
377 $strupdate = get_string('update');
378 $strmodule = get_string('modulename','scorm');
77bf0c29 379 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 380
381 echo '<div class="mod-scorm">';
382 if ($scorms = get_all_instances_in_course('scorm', $course)) {
383 // The module SCORM activity with the least id is the course
384 $scorm = current($scorms);
385 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
386 error("Course Module ID was incorrect");
387 }
388 $colspan = '';
389 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 390 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 391 if (isediting($course->id)) {
392 // Display update icon
393 $path = $CFG->wwwroot.'/course';
394 $headertext .= '<span class="commands">'.
395 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
396 '<img src="'.$CFG->pixpath.'/t/edit.gif" hspace="2" height="11" width="11" border="0" alt="'.$strupdate.'" /></a></span>';
397 }
398 $headertext .= '</td>';
399 // Display report link
400 $trackedusers = get_record('scorm_scoes_track', 'scormid', $scorm->id, '', '', '', '', 'count(distinct(userid)) as c');
401 if ($trackedusers->c > 0) {
402 $headertext .= '<td class="reportlink">'.
403 '<a target="'.$CFG->framename.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
404 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
405 } else {
406 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
407 }
408 $colspan = ' colspan="2"';
409 }
410 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
411 print_simple_box($headertext,'','100%');
412 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
413 } else {
0d699c24 414 if (has_capability('moodle/course:update', $context)) {
e4aa175a 415 // Create a new activity
2b3447c3 416 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 417 } else {
418 notify('Could not find a scorm course here');
419 }
420 }
421 echo '</div>';
422}
423
2b3447c3 424function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
e4aa175a 425 global $CFG;
5c1ac70c 426
e4aa175a 427 $organization = optional_param('organization', '', PARAM_INT);
428
2b3447c3 429 print_simple_box_start('center',$boxwidth);
e4aa175a 430?>
431 <div class="structurehead"><?php print_string('coursestruct','scorm') ?></div>
432<?php
433 if (empty($organization)) {
434 $organization = $scorm->launch;
435 }
436 if ($orgs = get_records_select_menu('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,title')) {
437 if (count($orgs) > 1) {
438 ?>
439 <div class='center'>
440 <?php print_string('organizations','scorm') ?>
441 <form name='changeorg' method='post' action='<?php echo $action ?>'>
442 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
443 </form>
444 </div>
445<?php
446 }
447 }
448 $orgidentifier = '';
449 if ($org = get_record('scorm_scoes','id',$organization)) {
450 if (($org->organization == '') && ($org->launch == '')) {
451 $orgidentifier = $org->identifier;
452 } else {
453 $orgidentifier = $org->organization;
454 }
455 }
2b3447c3 456 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
457 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
458
e4aa175a 459 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
460 $incomplete = $result->incomplete;
e4aa175a 461 echo $result->toc;
e4aa175a 462 print_simple_box_end();
463?>
464 <div class="center">
465 <form name="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php?id=<?php echo $cm->id ?>"<?php echo $scorm->popup == 1?' target="newwin"':'' ?>>
466 <?php
467
e4aa175a 468 if ($scorm->hidebrowse == 0) {
469 print_string("mode","scorm");
470 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
471 if ($incomplete === true) {
472 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
e4aa175a 473 }
474 } else {
475 if ($incomplete === true) {
476 echo '<input type="hidden" name="mode" value="normal" />'."\n";
477 } else {
478 echo '<input type="hidden" name="mode" value="review" />'."\n";
479 }
480 }
481 if (($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
482?>
483 <br />
484 <input type="checkbox" id="a" name="newattempt" />
485 <label for="a"><?php print_string('newattempt','scorm') ?></label>
486<?php
487 }
488 ?>
489 <br />
490 <input type="hidden" name="scoid" />
491 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
492 <input type="submit" value="<?php print_string('entercourse','scorm') ?>" />
493 </form>
494 </div>
495<?php
496}
497
e4aa175a 498?>