user selector: MDL-16966 Nicer messages when there are too many users to show.
[moodle.git] / mod / scorm / locallib.php
CommitLineData
e4aa175a 1<?php // $Id$
f69db63e 2
9528568b 3require_once("$CFG->dirroot/mod/scorm/lib.php");
4
a30b6819 5/// Constants and settings for module scorm
a679d64d 6define('UPDATE_NEVER', '0');
7define('UPDATE_ONCHANGE', '1');
8define('UPDATE_EVERYDAY', '2');
9define('UPDATE_EVERYTIME', '3');
10
b3659259 11define('SCO_ALL', 0);
12define('SCO_DATA', 1);
13define('SCO_ONLY', 2);
a30b6819 14
15define('GRADESCOES', '0');
16define('GRADEHIGHEST', '1');
17define('GRADEAVERAGE', '2');
18define('GRADESUM', '3');
a30b6819 19
20define('HIGHESTATTEMPT', '0');
21define('AVERAGEATTEMPT', '1');
22define('FIRSTATTEMPT', '2');
23define('LASTATTEMPT', '3');
a30b6819 24
a30b6819 25
9528568b 26/// Local Library of functions for module scorm
a30b6819 27
9528568b 28/**
29 * Extracts scrom package, sets up all variables.
30 * Called whenever scorm changes
31 * @param object $scorm instance - fields are updated and changes saved into database
32 * @param bool $full force full update if true
33 * @return void
34 */
35function scorm_parse($scorm, $full) {
36 global $CFG, $DB;
8aee93f1 37
9528568b 38 if (!isset($scorm->cmid)) {
39 $cm = get_coursemodule_from_instance('scorm', $scorm->id);
40 $scorm->cmid = $cm->id;
41 }
42 $context = get_context_instance(CONTEXT_MODULE, $scorm->cmid);
43 $newhash = $scorm->sha1hash;
a30b6819 44
9528568b 45 if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
a30b6819 46
9528568b 47 $fs = get_file_storage();
48 $packagefile = false;
f69db63e 49
9528568b 50 if ($scorm->scormtype === SCORM_TYPE_LOCAL) {
51 if ($packagefile = $fs->get_file($context->id, 'scorm_package', 0, '/', $scorm->reference)) {
52 $newhash = $packagefile->get_contenthash();
53 } else {
54 $newhash = null;
55 }
56 } else {
57 if (!$CFG->scorm_allowtypelocalsync) {
58 // sorry - localsync disabled
59 return;
60 }
61 if ($scorm->reference !== '' and (!$full or $scorm->sha1hash !== sha1($scorm->reference))) {
62 $fs->delete_area_files($context->id, 'scorm_package');
63 $file_record = array('contextid'=>$context->id, 'filearea'=>'scorm_package', 'itemid'=>0, 'filepath'=>'/');
64 if ($packagefile = $fs->create_file_from_url($file_record, $scorm->reference)) {
65 $newhash = sha1($scorm->reference);
5c1ac70c 66 } else {
9528568b 67 $newhash = null;
5c1ac70c 68 }
f69db63e 69 }
70 }
f69db63e 71
9528568b 72 if ($packagefile) {
73 if (!$full and $packagefile and $scorm->sha1hash === $newhash) {
74 if (strpos($scorm->version, 'SCORM') !== false) {
75 if ($fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
76 // no need to update
77 return;
e4aa175a 78 }
9528568b 79 } else if (strpos($scorm->version, 'AICC') !== false) {
80 // TODO: add more sanity checks - something really exists in scorm_content area
81 return;
82 }
83 }
84
85 // now extract files
86 $fs->delete_area_files($context->id, 'scorm_content');
87
88 $packer = get_file_packer('application/zip');
89 $packagefile->extract_to_storage($packer, $context->id, 'scorm_content', 0, '/');
90
91 } else if (!$full) {
92 return;
93 }
94
95
96 if ($manifest = $fs->get_file($context->id, 'scorm_content', 0, '/', 'imsmanifest.xml')) {
97 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
98 // SCORM
99 if (!scorm_parse_scorm($scorm, $manifest)) {
100 $scorm->version = 'ERROR';
101 }
102 } else {
103 require_once("$CFG->dirroot/mod/scorm/datamodels/aicclib.php");
104 // AICC
105 if (!scorm_parse_aicc($scorm)) {
106 $scorm->version = 'ERROR';
107 }
108 }
109
110 } else if ($scorm->scormtype === SCORM_TYPE_EXTERNAL and $CFG->scorm_allowtypeexternal) {
111 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
112 return;
113 }
114 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
115 // SCORM only, AICC can not be external
116 if (!scorm_parse_scorm($scorm, $scorm->reference)) {
117 $scorm->version = 'ERROR';
118 }
119 $newhash = sha1($scorm->reference);
120
121 } else if ($scorm->scormtype === SCORM_TYPE_IMSREPOSITORY and !empty($CFG->repositoryactivate) and $CFG->scorm_allowtypeimsrepository) {
122 if (!$full and $scorm->sha1hash === sha1($scorm->reference)) {
123 return;
124 }
125 require_once("$CFG->dirroot/mod/scorm/datamodels/scormlib.php");
126 if (!scorm_parse_scorm($scorm, $CFG->repository.substr($scorm->reference,1).'/imsmanifest.xml')) {
127 $scorm->version = 'ERROR';
128 }
129 $newhash = sha1($scorm->reference);
130
e4aa175a 131 } else {
9528568b 132 // sorry, disabled type
133 return;
e4aa175a 134 }
9528568b 135
136 $scorm->revision++;
137 $scorm->sha1hash = $newhash;
138 $DB->update_record('scorm', $scorm);
e4aa175a 139}
140
9528568b 141
2b3447c3 142function scorm_array_search($item, $needle, $haystacks, $strict=false) {
143 if (!empty($haystacks)) {
144 foreach ($haystacks as $key => $element) {
145 if ($strict) {
146 if ($element->{$item} === $needle) {
147 return $key;
148 }
149 } else {
150 if ($element->{$item} == $needle) {
151 return $key;
e4aa175a 152 }
153 }
e4aa175a 154 }
155 }
2b3447c3 156 return false;
e4aa175a 157}
158
2b3447c3 159function scorm_repeater($what, $times) {
160 if ($times <= 0) {
161 return null;
162 }
163 $return = '';
164 for ($i=0; $i<$times;$i++) {
165 $return .= $what;
166 }
167 return $return;
168}
e4aa175a 169
2b3447c3 170function scorm_external_link($link) {
171// check if a link is external
172 $result = false;
173 $link = strtolower($link);
174 if (substr($link,0,7) == 'http://') {
175 $result = true;
176 } else if (substr($link,0,8) == 'https://') {
177 $result = true;
178 } else if (substr($link,0,4) == 'www.') {
179 $result = true;
180 }
181 return $result;
e4aa175a 182}
183
b3659259 184/**
185* Returns an object containing all datas relative to the given sco ID
186*
187* @param integer $id The sco ID
188* @return mixed (false if sco id does not exists)
189*/
bd3523a5 190
b3659259 191function scorm_get_sco($id,$what=SCO_ALL) {
bf347041 192 global $DB;
193
194 if ($sco = $DB->get_record('scorm_scoes', array('id'=>$id))) {
b3659259 195 $sco = ($what == SCO_DATA) ? new stdClass() : $sco;
bf347041 196 if (($what != SCO_ONLY) && ($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id)))) {
b3659259 197 foreach ($scodatas as $scodata) {
c31f631b 198 $sco->{$scodata->name} = $scodata->value;
b3659259 199 }
bf347041 200 } else if (($what != SCO_ONLY) && (!($scodatas = $DB->get_records('scorm_scoes_data', array('scoid'=>$id))))) {
9528568b 201 $sco->parameters = '';
b3659259 202 }
203 return $sco;
204 } else {
205 return false;
206 }
207}
82605bea 208
209/**
210* Returns an object (array) containing all the scoes data related to the given sco ID
211*
212* @param integer $id The sco ID
213* @param integer $organisation an organisation ID - defaults to false if not required
214* @return mixed (false if there are no scoes or an array)
215*/
216
217function scorm_get_scoes($id,$organisation=false) {
b44c704f 218 global $DB;
219
82605bea 220 $organizationsql = '';
b44c704f 221 $queryarray = array('scorm'=>$id);
82605bea 222 if (!empty($organisation)) {
b44c704f 223 $queryarray['organization'] = $organisation;
82605bea 224 }
b44c704f 225 if ($scoes = $DB->get_records('scorm_scoes', $queryarray, 'id ASC')) {
82605bea 226 // drop keys so that it is a simple array as expected
227 $scoes = array_values($scoes);
228 foreach ($scoes as $sco) {
b44c704f 229 if ($scodatas = $DB->get_records('scorm_scoes_data',array('scoid'=>$sco->id))) {
82605bea 230 foreach ($scodatas as $scodata) {
2fd0e9fe 231 $sco->{$scodata->name} = $scodata->value;
82605bea 232 }
233 }
234 }
235 return $scoes;
236 } else {
237 return false;
238 }
239}
240
6381fa56 241function scorm_insert_track($userid,$scormid,$scoid,$attempt,$element,$value,$forcecompleted=false) {
bf347041 242 global $DB;
243
e4aa175a 244 $id = null;
6381fa56 245
246 if ($forcecompleted) {
247 //TODO - this could be broadened to encompass SCORM 2004 in future
248 if (($element == 'cmi.core.lesson_status') && ($value == 'incomplete')) {
249 if ($track = $DB->get_record_select('scorm_scoes_track','userid=? AND scormid=? AND scoid=? AND attempt=? AND element=\'cmi.core.score.raw\'', array($userid, $scormid, $scoid, $attempt))) {
250 $value = 'completed';
251 }
252 }
253 if ($element == 'cmi.core.score.raw') {
254 if ($tracktest = $DB->get_record_select('scorm_scoes_track','userid=? AND scormid=? AND scoid=? AND attempt=? AND element=\'cmi.core.lesson_status\'', array($userid, $scormid, $scoid, $attempt))) {
255 if ($tracktest->value == "incomplete") {
256 $tracktest->value = "completed";
257 $idtest = $DB->update_record('scorm_scoes_track',$tracktest);
258 }
259 }
260 }
261 }
262
bf347041 263 if ($track = $DB->get_record('scorm_scoes_track',array('userid'=>$userid, 'scormid'=>$scormid, 'scoid'=>$scoid, 'attempt'=>$attempt, 'element'=>$element))) {
e4aa175a 264 $track->value = $value;
265 $track->timemodified = time();
bf347041 266 $id = $DB->update_record('scorm_scoes_track',$track);
e4aa175a 267 } else {
268 $track->userid = $userid;
269 $track->scormid = $scormid;
270 $track->scoid = $scoid;
271 $track->attempt = $attempt;
272 $track->element = $element;
bf347041 273 $track->value = $value;
e4aa175a 274 $track->timemodified = time();
bf347041 275 $id = $DB->insert_record('scorm_scoes_track',$track);
e4aa175a 276 }
9528568b 277
9528568b 278 if (strstr($element, '.score.raw') ||
a0b36684 279 (($element == 'cmi.core.lesson_status' || $element == 'cmi.completion_status') && ($track->value == 'completed' || $track->value == 'passed'))) {
280 $scorm = $DB->get_record('scorm', array('id' => $scormid));
281 $grademethod = $scorm->grademethod % 10;
7f7946fd 282 include_once('lib.php');
283 scorm_update_grades($scorm, $userid);
d23121ab 284 }
9528568b 285
e4aa175a 286 return $id;
287}
288
e4aa175a 289function scorm_get_tracks($scoid,$userid,$attempt='') {
e4aa175a 290/// Gets all tracks of specified sco and user
bf347041 291 global $CFG, $DB;
e4aa175a 292
293 if (empty($attempt)) {
bf347041 294 if ($scormid = $DB->get_field('scorm_scoes','scorm', array('id'=>$scoid))) {
e4aa175a 295 $attempt = scorm_get_last_attempt($scormid,$userid);
296 } else {
297 $attempt = 1;
298 }
299 }
bf347041 300 if ($tracks = $DB->get_records('scorm_scoes_track', array('userid'=>$userid, 'scoid'=>$scoid, 'attempt'=>$attempt),'element ASC')) {
e4aa175a 301 $usertrack->userid = $userid;
9528568b 302 $usertrack->scoid = $scoid;
a30b6819 303 // Defined in order to unify scorm1.2 and scorm2004
e4aa175a 304 $usertrack->score_raw = '';
e4aa175a 305 $usertrack->status = '';
e4aa175a 306 $usertrack->total_time = '00:00:00';
307 $usertrack->session_time = '00:00:00';
308 $usertrack->timemodified = 0;
309 foreach ($tracks as $track) {
310 $element = $track->element;
311 $usertrack->{$element} = $track->value;
312 switch ($element) {
f69db63e 313 case 'cmi.core.lesson_status':
314 case 'cmi.completion_status':
315 if ($track->value == 'not attempted') {
316 $track->value = 'notattempted';
9528568b 317 }
f69db63e 318 $usertrack->status = $track->value;
9528568b 319 break;
e4aa175a 320 case 'cmi.core.score.raw':
321 case 'cmi.score.raw':
322 $usertrack->score_raw = $track->value;
9528568b 323 break;
e4aa175a 324 case 'cmi.core.session_time':
325 case 'cmi.session_time':
326 $usertrack->session_time = $track->value;
9528568b 327 break;
e4aa175a 328 case 'cmi.core.total_time':
329 case 'cmi.total_time':
330 $usertrack->total_time = $track->value;
9528568b 331 break;
332 }
e4aa175a 333 if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) {
334 $usertrack->timemodified = $track->timemodified;
9528568b 335 }
3505e82b 336 }
9528568b 337 if (is_array($usertrack)) {
07b905ae 338 ksort($usertrack);
339 }
e4aa175a 340 return $usertrack;
341 } else {
342 return false;
343 }
344}
345
2b3447c3 346function scorm_get_user_data($userid) {
bf347041 347 global $DB;
2b3447c3 348/// Gets user info required to display the table of scorm results
349/// for report.php
e4aa175a 350
bf347041 351 return $DB->get_record('user', array('id'=>$userid),'firstname, lastname, picture');
2b3447c3 352}
e4aa175a 353
a30b6819 354function scorm_grade_user_attempt($scorm, $userid, $attempt=1, $time=false) {
bf347041 355 global $DB;
9528568b 356 $attemptscore = NULL;
a30b6819 357 $attemptscore->scoes = 0;
358 $attemptscore->values = 0;
359 $attemptscore->max = 0;
360 $attemptscore->sum = 0;
361 $attemptscore->lastmodify = 0;
9528568b 362
bf347041 363 if (!$scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
a30b6819 364 return NULL;
e4aa175a 365 }
e4aa175a 366
7ce6eb87 367 // this treatment is necessary as the whatgrade field was not in the DB
368 // and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
369 // and 1s are grademethod
a30b6819 370 $grademethod = $scorm->grademethod % 10;
371
9528568b 372 foreach ($scoes as $sco) {
2b3447c3 373 if ($userdata=scorm_get_tracks($sco->id, $userid,$attempt)) {
374 if (($userdata->status == 'completed') || ($userdata->status == 'passed')) {
a30b6819 375 $attemptscore->scoes++;
9528568b 376 }
2b3447c3 377 if (!empty($userdata->score_raw)) {
a30b6819 378 $attemptscore->values++;
379 $attemptscore->sum += $userdata->score_raw;
380 $attemptscore->max = ($userdata->score_raw > $attemptscore->max)?$userdata->score_raw:$attemptscore->max;
381 if (isset($userdata->timemodified) && ($userdata->timemodified > $attemptscore->lastmodify)) {
382 $attemptscore->lastmodify = $userdata->timemodified;
383 } else {
384 $attemptscore->lastmodify = 0;
385 }
9528568b 386 }
387 }
e4aa175a 388 }
2b3447c3 389 switch ($grademethod) {
a30b6819 390 case GRADEHIGHEST:
391 $score = $attemptscore->max;
9528568b 392 break;
a30b6819 393 case GRADEAVERAGE:
394 if ($attemptscore->values > 0) {
395 $score = $attemptscore->sum/$attemptscore->values;
5c1ac70c 396 } else {
a30b6819 397 $score = 0;
9528568b 398 }
399 break;
a30b6819 400 case GRADESUM:
401 $score = $attemptscore->sum;
9528568b 402 break;
a30b6819 403 case GRADESCOES:
404 $score = $attemptscore->scoes;
7ef16bf9 405 break;
406 default:
407 $score = $attemptscore->max; // Remote Learner GRADEHIGHEST is default
5c1ac70c 408 }
a30b6819 409
410 if ($time) {
411 $result = new stdClass();
412 $result->score = $score;
413 $result->time = $attemptscore->lastmodify;
414 } else {
415 $result = $score;
416 }
417
418 return $result;
419}
420
421function scorm_grade_user($scorm, $userid, $time=false) {
7ce6eb87 422 // this treatment is necessary as the whatgrade field was not in the DB
423 // and so whatgrade and grademethod are combined in grademethod 10s are whatgrade
424 // and 1s are grademethod
a30b6819 425 $whatgrade = intval($scorm->grademethod / 10);
426
7ce6eb87 427 // insure we dont grade user beyond $scorm->maxattempt settings
428 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
429 if($scorm->maxattempt != 0 && $lastattempt >= $scorm->maxattempt){
430 $lastattempt = $scorm->maxattempt;
431 }
432
a30b6819 433 switch ($whatgrade) {
434 case FIRSTATTEMPT:
435 return scorm_grade_user_attempt($scorm, $userid, 1, $time);
9528568b 436 break;
a30b6819 437 case LASTATTEMPT:
438 return scorm_grade_user_attempt($scorm, $userid, scorm_get_last_attempt($scorm->id, $userid), $time);
439 break;
440 case HIGHESTATTEMPT:
a30b6819 441 $maxscore = 0;
442 $attempttime = 0;
443 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
444 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
445 if ($time) {
446 if ($attemptscore->score > $maxscore) {
447 $maxscore = $attemptscore->score;
448 $attempttime = $attemptscore->time;
449 }
450 } else {
451 $maxscore = $attemptscore > $maxscore ? $attemptscore: $maxscore;
452 }
453 }
454 if ($time) {
455 $result = new stdClass();
456 $result->score = $maxscore;
457 $result->time = $attempttime;
458 return $result;
459 } else {
460 return $maxscore;
461 }
462 break;
463 case AVERAGEATTEMPT:
464 $lastattempt = scorm_get_last_attempt($scorm->id, $userid);
465 $sumscore = 0;
466 for ($attempt = 1; $attempt <= $lastattempt; $attempt++) {
467 $attemptscore = scorm_grade_user_attempt($scorm, $userid, $attempt, $time);
468 if ($time) {
469 $sumscore += $attemptscore->score;
470 } else {
471 $sumscore += $attemptscore;
472 }
473 }
474
475 if ($lastattempt > 0) {
476 $score = $sumscore / $lastattempt;
477 } else {
478 $score = 0;
479 }
480
481 if ($time) {
482 $result = new stdClass();
483 $result->score = $score;
484 $result->time = $attemptscore->time;
485 return $result;
486 } else {
487 return $score;
488 }
489 break;
490 }
e4aa175a 491}
492
8e45ba45 493function scorm_count_launchable($scormid,$organization='') {
bf347041 494 global $DB;
495
496 $sqlorganization = '';
497 $params = array($scormid);
8e45ba45 498 if (!empty($organization)) {
bf347041 499 $sqlorganization = " AND organization=?";
500 $params[] = $organization;
8e45ba45 501 }
6280b17a 502 $params []= ''; // empty launch
503 return $DB->count_records_select('scorm_scoes',"scorm = ? $sqlorganization AND launch <> ?", $params);
e4aa175a 504}
505
2b3447c3 506function scorm_get_last_attempt($scormid, $userid) {
bf347041 507 global $DB;
508
2b3447c3 509/// Find the last attempt number for the given user id and scorm id
bf347041 510 if ($lastattempt = $DB->get_record('scorm_scoes_track', array('userid'=>$userid, 'scormid'=>$scormid), 'max(attempt) as a')) {
2b3447c3 511 if (empty($lastattempt->a)) {
512 return '1';
513 } else {
514 return $lastattempt->a;
e4aa175a 515 }
6381fa56 516 } else {
517 return false;
e4aa175a 518 }
e4aa175a 519}
520
e4aa175a 521function scorm_course_format_display($user,$course) {
bf347041 522 global $CFG, $DB;
e4aa175a 523
524 $strupdate = get_string('update');
525 $strmodule = get_string('modulename','scorm');
77bf0c29 526 $context = get_context_instance(CONTEXT_COURSE,$course->id);
e4aa175a 527
528 echo '<div class="mod-scorm">';
529 if ($scorms = get_all_instances_in_course('scorm', $course)) {
9528568b 530 // The module SCORM activity with the least id is the course
e4aa175a 531 $scorm = current($scorms);
532 if (! $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id)) {
08b56f93 533 print_error('invalidcoursemodule');
e4aa175a 534 }
535 $colspan = '';
536 $headertext = '<table width="100%"><tr><td class="title">'.get_string('name').': <b>'.format_string($scorm->name).'</b>';
2b3447c3 537 if (has_capability('moodle/course:manageactivities', $context)) {
e4aa175a 538 if (isediting($course->id)) {
539 // Display update icon
540 $path = $CFG->wwwroot.'/course';
541 $headertext .= '<span class="commands">'.
542 '<a title="'.$strupdate.'" href="'.$path.'/mod.php?update='.$cm->id.'&amp;sesskey='.sesskey().'">'.
0d905d9f 543 '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'.$strupdate.'" /></a></span>';
e4aa175a 544 }
545 $headertext .= '</td>';
546 // Display report link
bf347041 547 $trackedusers = $DB->get_record('scorm_scoes_track', array('scormid'=>$scorm->id), 'count(distinct(userid)) as c');
e4aa175a 548 if ($trackedusers->c > 0) {
549 $headertext .= '<td class="reportlink">'.
fa738731 550 '<a '.$CFG->frametarget.'" href="'.$CFG->wwwroot.'/mod/scorm/report.php?id='.$cm->id.'">'.
e4aa175a 551 get_string('viewallreports','scorm',$trackedusers->c).'</a>';
552 } else {
553 $headertext .= '<td class="reportlink">'.get_string('noreports','scorm');
554 }
555 $colspan = ' colspan="2"';
9528568b 556 }
e4aa175a 557 $headertext .= '</td></tr><tr><td'.$colspan.'>'.format_text(get_string('summary').':<br />'.$scorm->summary).'</td></tr></table>';
558 print_simple_box($headertext,'','100%');
559 scorm_view_display($user, $scorm, 'view.php?id='.$course->id, $cm, '100%');
560 } else {
0d699c24 561 if (has_capability('moodle/course:update', $context)) {
e4aa175a 562 // Create a new activity
2b3447c3 563 redirect($CFG->wwwroot.'/course/mod.php?id='.$course->id.'&amp;section=0&sesskey='.sesskey().'&amp;add=scorm');
e4aa175a 564 } else {
565 notify('Could not find a scorm course here');
566 }
567 }
568 echo '</div>';
569}
570
2b3447c3 571function scorm_view_display ($user, $scorm, $action, $cm, $boxwidth='') {
534792cd 572 global $CFG, $DB;
ab3b00e1 573
9528568b 574 if ($scorm->updatefreq == UPDATE_EVERYTIME) {
575 scorm_parse($scorm, false);
ab3b00e1 576 }
577
e4aa175a 578 $organization = optional_param('organization', '', PARAM_INT);
579
6381fa56 580 if($scorm->displaycoursestructure == 1) {
581 print_simple_box_start('center',$boxwidth);
e4aa175a 582?>
29a43013 583 <div class="structurehead"><?php print_string('contents','scorm') ?></div>
e4aa175a 584<?php
6381fa56 585 }
e4aa175a 586 if (empty($organization)) {
587 $organization = $scorm->launch;
588 }
07b905ae 589 if ($orgs = $DB->get_records_menu('scorm_scoes', array('scorm'=>$scorm->id, 'organization'=>'', 'launch'=>''), 'id', 'id,title')) {
e4aa175a 590 if (count($orgs) > 1) {
591 ?>
52a9a9b5 592 <div class='scorm-center'>
e4aa175a 593 <?php print_string('organizations','scorm') ?>
b7dc2256 594 <form id='changeorg' method='post' action='<?php echo $action ?>'>
e4aa175a 595 <?php choose_from_menu($orgs, 'organization', "$organization", '','submit()') ?>
596 </form>
597 </div>
598<?php
599 }
600 }
601 $orgidentifier = '';
b3659259 602 if ($sco = scorm_get_sco($organization, SCO_ONLY)) {
603 if (($sco->organization == '') && ($sco->launch == '')) {
604 $orgidentifier = $sco->identifier;
e4aa175a 605 } else {
b3659259 606 $orgidentifier = $sco->organization;
e4aa175a 607 }
608 }
28ffbff3 609
610/*
611 $orgidentifier = '';
bf347041 612 if ($org = $DB->get_record('scorm_scoes', array('id'=>$organization))) {
28ffbff3 613 if (($org->organization == '') && ($org->launch == '')) {
614 $orgidentifier = $org->identifier;
615 } else {
616 $orgidentifier = $org->organization;
617 }
618 }*/
619
2b3447c3 620 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe
dbe7e6f6 621 if (!file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php')) {
622 $scorm->version = 'scorm_12';
623 }
2b3447c3 624 require_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'lib.php');
625
e4aa175a 626 $result = scorm_get_toc($user,$scorm,'structlist',$orgidentifier);
627 $incomplete = $result->incomplete;
28ffbff3 628
6381fa56 629 // do we want the TOC to be displayed?
630 if($scorm->displaycoursestructure == 1) {
631 echo $result->toc;
632 print_simple_box_end();
633 }
634
635 // is this the first attempt ?
636 $attemptcount = scorm_get_attempt_count($user, $scorm);
637
638 // do not give the player launch FORM if the SCORM object is locked after the final attempt
639 if ($scorm->lastattemptlock == 0 || $result->attemptleft > 0) {
e4aa175a 640?>
52a9a9b5 641 <div class="scorm-center">
4675994a 642 <form id="theform" method="post" action="<?php echo $CFG->wwwroot ?>/mod/scorm/player.php">
e4aa175a 643 <?php
e4aa175a 644 if ($scorm->hidebrowse == 0) {
8949f8df 645 print_string('mode','scorm');
e4aa175a 646 echo ': <input type="radio" id="b" name="mode" value="browse" /><label for="b">'.get_string('browse','scorm').'</label>'."\n";
76ea4fb4 647 echo '<input type="radio" id="n" name="mode" value="normal" checked="checked" /><label for="n">'.get_string('normal','scorm')."</label>\n";
e4aa175a 648 } else {
76ea4fb4 649 echo '<input type="hidden" name="mode" value="normal" />'."\n";
e4aa175a 650 }
6381fa56 651 if ($scorm->forcenewattempt == 1) {
652 if ($incomplete === false) {
653 echo '<input type="hidden" name="newattempt" value="on" />'."\n";
654 }
655 } elseif ($attemptcount != 0 && ($incomplete === false) && (($result->attemptleft > 0)||($scorm->maxattempt == 0))) {
e4aa175a 656?>
6381fa56 657 <br />
658 <input type="checkbox" id="a" name="newattempt" />
659 <label for="a"><?php print_string('newattempt','scorm') ?></label>
e4aa175a 660<?php
661 }
662 ?>
663 <br />
4675994a 664 <input type="hidden" name="scoid"/>
df7ba610 665 <input type="hidden" name="id" value="<?php echo $cm->id ?>"/>
e4aa175a 666 <input type="hidden" name="currentorg" value="<?php echo $orgidentifier ?>" />
2b600d16 667 <input type="submit" value="<?php print_string('enter','scorm') ?>" />
e4aa175a 668 </form>
669 </div>
670<?php
6381fa56 671 }
e4aa175a 672}
6280b17a 673
28ffbff3 674function scorm_simple_play($scorm,$user) {
bf347041 675 global $DB;
676
28ffbff3 677 $result = false;
9528568b 678
5b4b959b 679 $scoes = $DB->get_records_select('scorm_scoes', 'scorm = ? AND launch <> ?', array($scorm->id, $DB->sql_empty()));
9528568b 680
fa6eb1dc 681 if ($scoes && (count($scoes) == 1)) {
28ffbff3 682 if ($scorm->skipview >= 1) {
683 $sco = current($scoes);
684 if (scorm_get_tracks($sco->id,$user->id) === false) {
45698041 685 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
28ffbff3 686 $result = true;
687 } else if ($scorm->skipview == 2) {
45698041 688 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
28ffbff3 689 $result = true;
690 }
691 }
692 }
28ffbff3 693 return $result;
694}
695/*
8e45ba45 696function scorm_simple_play($scorm,$user) {
bf347041 697 global $DB;
8e45ba45 698 $result = false;
bf347041 699 if ($scoes = $DB->get_records_select('scorm_scoes','scorm=? AND launch<>""', array($scorm->id))) {
76ea4fb4 700 if (count($scoes) == 1) {
701 if ($scorm->skipview >= 1) {
702 $sco = current($scoes);
703 if (scorm_get_tracks($sco->id,$user->id) === false) {
704 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
705 $result = true;
706 } else if ($scorm->skipview == 2) {
707 header('Location: player.php?a='.$scorm->id.'&scoid='.$sco->id);
708 $result = true;
709 }
8e45ba45 710 }
711 }
712 }
713 return $result;
714}
28ffbff3 715*/
d8c9d8a1 716
717function scorm_get_count_users($scormid, $groupingid=null) {
bf347041 718 global $CFG, $DB;
9528568b 719
d8c9d8a1 720 if (!empty($CFG->enablegroupings) && !empty($groupingid)) {
721 $sql = "SELECT COUNT(DISTINCT st.userid)
bf347041 722 FROM {scorm_scoes_track} st
723 INNER JOIN {groups_members} gm ON st.userid = gm.userid
9528568b 724 INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
bf347041 725 WHERE st.scormid = ? AND gg.groupingid = ?
d8c9d8a1 726 ";
bf347041 727 $params = array($scormid, $groupingid);
d8c9d8a1 728 } else {
729 $sql = "SELECT COUNT(DISTINCT st.userid)
9528568b 730 FROM {scorm_scoes_track} st
bf347041 731 WHERE st.scormid = ?
d8c9d8a1 732 ";
bf347041 733 $params = array($scormid);
d8c9d8a1 734 }
9528568b 735
bf347041 736 return ($DB->count_records_sql($sql, $params));
d8c9d8a1 737}
738
527af457 739/**
740* Build up the JavaScript representation of an array element
741*
742* @param string $sversion SCORM API version
743* @param array $userdata User track data
744* @param string $element_name Name of array element to get values for
745* @param array $children list of sub elements of this array element that also need instantiating
746* @return None
747*/
748function scorm_reconstitute_array_element($sversion, $userdata, $element_name, $children) {
749 // reconstitute comments_from_learner and comments_from_lms
750 $current = '';
bf34ac0f 751 $current_subelement = '';
752 $current_sub = '';
527af457 753 $count = 0;
bf34ac0f 754 $count_sub = 0;
527af457 755
756 // filter out the ones we want
757 $element_list = array();
758 foreach($userdata as $element => $value){
759 if (substr($element,0,strlen($element_name)) == $element_name) {
760 $element_list[$element] = $value;
761 }
762 }
763
764 // sort elements in .n array order
765 uksort($element_list, "scorm_element_cmp");
766
767 // generate JavaScript
768 foreach($element_list as $element => $value){
769 if ($sversion == 'scorm_13') {
b6293233 770 $element = preg_replace('/\.(\d+)\./', ".N\$1.", $element);
527af457 771 preg_match('/\.(N\d+)\./', $element, $matches);
772 } else {
b6293233 773 $element = preg_replace('/\.(\d+)\./', "_\$1.", $element);
527af457 774 preg_match('/\_(\d+)\./', $element, $matches);
775 }
776 if (count($matches) > 0 && $current != $matches[1]) {
bf34ac0f 777 if ($count_sub > 0) {
778 echo ' '.$element_name.'_'.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
779 }
527af457 780 $current = $matches[1];
781 $count++;
bf34ac0f 782 $current_subelement = '';
783 $current_sub = '';
784 $count_sub = 0;
527af457 785 $end = strpos($element,$matches[1])+strlen($matches[1]);
786 $subelement = substr($element,0,$end);
787 echo ' '.$subelement." = new Object();\n";
788 // now add the children
789 foreach ($children as $child) {
790 echo ' '.$subelement.".".$child." = new Object();\n";
791 echo ' '.$subelement.".".$child."._children = ".$child."_children;\n";
792 }
793 }
bf34ac0f 794
795 // now - flesh out the second level elements if there are any
796 if ($sversion == 'scorm_13') {
b6293233 797 $element = preg_replace('/(.*?\.N\d+\..*?)\.(\d+)\./', "\$1.N\$2.", $element);
bf34ac0f 798 preg_match('/.*?\.N\d+\.(.*?)\.(N\d+)\./', $element, $matches);
799 } else {
b6293233 800 $element = preg_replace('/(.*?\_\d+\..*?)\.(\d+)\./', "\$1_\$2.", $element);
bf34ac0f 801 preg_match('/.*?\_\d+\.(.*?)\_(\d+)\./', $element, $matches);
802 }
803
804 // check the sub element type
805 if (count($matches) > 0 && $current_subelement != $matches[1]) {
806 if ($count_sub > 0) {
807 echo ' '.$element_name.'_'.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
808 }
809 $current_subelement = $matches[1];
810 $current_sub = '';
811 $count_sub = 0;
812 $end = strpos($element,$matches[1])+strlen($matches[1]);
813 $subelement = substr($element,0,$end);
814 echo ' '.$subelement." = new Object();\n";
815 }
816
817 // now check the subelement subscript
818 if (count($matches) > 0 && $current_sub != $matches[2]) {
819 $current_sub = $matches[2];
820 $count_sub++;
821 $end = strrpos($element,$matches[2])+strlen($matches[2]);
822 $subelement = substr($element,0,$end);
823 echo ' '.$subelement." = new Object();\n";
824 }
825
527af457 826 echo ' '.$element.' = \''.$value."';\n";
827 }
bf34ac0f 828 if ($count_sub > 0) {
829 echo ' '.$element_name.'_'.$current.'.'.$current_subelement.'._count = '.$count_sub.";\n";
830 }
527af457 831 if ($count > 0) {
832 echo ' '.$element_name.'._count = '.$count.";\n";
833 }
834}
835
836/**
837* Build up the JavaScript representation of an array element
838*
839* @param string $a left array element
840* @param string $b right array element
841* @return comparator - 0,1,-1
842*/
843function scorm_element_cmp($a, $b) {
bf34ac0f 844 preg_match('/.*?(\d+)\./', $a, $matches);
527af457 845 $left = intval($matches[1]);
bf34ac0f 846 preg_match('/.?(\d+)\./', $b, $matches);
527af457 847 $right = intval($matches[1]);
848 if ($left < $right) {
849 return -1; // smaller
850 } elseif ($left > $right) {
851 return 1; // bigger
852 } else {
bf34ac0f 853 // look for a second level qualifier eg cmi.interactions_0.correct_responses_0.pattern
854 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $a, $matches)) {
855 $leftterm = intval($matches[2]);
856 $left = intval($matches[3]);
857 if (preg_match('/.*?(\d+)\.(.*?)\.(\d+)\./', $b, $matches)) {
858 $rightterm = intval($matches[2]);
859 $right = intval($matches[3]);
860 if ($leftterm < $rightterm) {
861 return -1; // smaller
862 } elseif ($leftterm > $rightterm) {
863 return 1; // bigger
864 } else {
865 if ($left < $right) {
866 return -1; // smaller
867 } elseif ($left > $right) {
868 return 1; // bigger
869 }
870 }
871 }
872 }
873 // fall back for no second level matches or second level matches are equal
527af457 874 return 0; // equal to
875 }
876}
6381fa56 877
878/**
879* Generate the user attempt status string
880*
881* @param object $user Current context user
882* @param object $scorm a moodle scrom object - mdl_scorm
883* @return string - Attempt status string
884*/
885function scorm_get_attempt_status($user, $scorm) {
886 global $DB;
887
888 $attempts = $DB->get_records_select('scorm_scoes_track',"element='cmi.core.score.raw' AND userid=? AND scormid=?", array($user->id, $scorm->id),'attempt','attempt AS attemptnumber, value AS grade');
889 if(empty($attempts)) {
890 $attemptcount = 0;
891 } else {
892 $attemptcount = count($attempts);
893 }
894
895 $result = '<p>'.get_string('noattemptsallowed', 'scorm').': ';
896 if ($scorm->maxattempt > 0) {
897 $result .= $scorm->maxattempt . '<BR>';
898 } else {
899 $result .= get_string('unlimited').'<BR>';
900 }
901 $result .= get_string('noattemptsmade', 'scorm').': ' . $attemptcount . '<BR>';
902
903 $gradereported = 0;
904 $gradesum = 0;
905 switch ($scorm->grademethod) {
906 case GRADEHIGHEST:
907 $grademethod = get_string('gradehighest', 'scorm');
908 break;
909 case GRADEAVERAGE:
910 $grademethod = get_string('gradeaverage', 'scorm');
911 break;
912 case GRADESUM:
913 $grademethod = get_string('gradesum', 'scorm');
914 break;
915 case GRADESCOES:
916 $grademethod = get_string('gradescoes', 'scorm');
917 break;
918 }
919
920 if(!empty($attempts)) {
921 foreach($attempts as $attempt) {
922 $gradereported = scorm_grade_user_attempt($scorm, $user->id, $attempt->attemptnumber);
923 $result .= get_string('gradeforattempt', 'scorm').' ' . $attempt->attemptnumber . ': ' . $attempt->grade .'%<BR>';
924 }
925 }
926
927 $result .= get_string('grademethod', 'scorm'). ': ' . $grademethod;
928 if(empty($attempts)) {
929 $result .= '<BR>' . get_string('gradereported','scorm') . ': ' . get_string('none') . '<BR>';
930 } else {
931 $result .= '<BR>' . get_string('gradereported','scorm') . ': ' . $gradereported . ($scorm->grademethod == GRADESCOES ? '' : '%') .'<BR>';
932 }
933 $result .= '</p>';
934 if ($attemptcount >= $scorm->maxattempt and $scorm->maxattempt > 0) {
935 $result .= '<p><font color="#cc0000">'.get_string('exceededmaxattempts','scorm').'</font></p>';
936 }
937 return $result;
938}
939
940/**
941* Get SCORM attempt count
942*
943* @param object $user Current context user
944* @param object $scorm a moodle scrom object - mdl_scorm
945* @return int - no. of attempts so far
946*/
947function scorm_get_attempt_count($user, $scorm) {
948 global $DB;
949 $attemptcount = 0;
950 $element = 'cmi.core.score.raw';
951 if ($scorm->version == 'scorm1_3') {
952 $element = 'cmi.score.raw';
953 }
954 $attempts = $DB->get_records_select('scorm_scoes_track',"element=? AND userid=? AND scormid=?", array($element, $user->id, $scorm->id),'attempt','attempt AS attemptnumber, value AS grade');
955 if(!empty($attempts)) {
956 $attemptcount = count($attempts);
957 }
958 return $attemptcount;
959}
1881df27 960
961/**
962* Figure out with this is a debug situation
963*
964* @param object $scorm a moodle scrom object - mdl_scorm
965* @return boolean - debugging true/false
966*/
967function scorm_debugging($scorm) {
968 global $CFG, $USER;
969 if (!$CFG->scorm_allowapidebug) {
970 return false;
971 }
972 $identifier = $USER->username.':'.$scorm->name;
973 $test = $CFG->scorm_apidebugmask;
974 // check the regex is only a short list of safe characters
975 if (!preg_match('/^[\w\s\*\.\?\+\:\_\\\]+$/', $test)) {
976 return false;
977 }
978 $res = false;
979 eval('$res = preg_match(\'/^'.$test.'/\', $identifier) ? true : false;');
980 return $res;
981}
527af457 982?>