2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * functions used by AICC packages.
21 * @copyright 1999 onwards Roberto Pinna
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 function scorm_add_time($a, $b) {
26 $aes = explode(':', $a);
27 $bes = explode(':', $b);
28 $aseconds = explode('.', $aes[2]);
29 $bseconds = explode('.', $bes[2]);
32 $acents = 0; // Cents.
33 if (count($aseconds) > 1) {
34 $acents = $aseconds[1];
37 if (count($bseconds) > 1) {
38 $bcents = $bseconds[1];
40 $cents = $acents +
$bcents;
41 $change = floor($cents / 100);
42 $cents = $cents - ($change * 100);
43 if (floor($cents) < 10) {
47 $secs = $aseconds[0] +
$bseconds[0] +
$change; // Seconds.
48 $change = floor($secs / 60);
49 $secs = $secs - ($change * 60);
50 if (floor($secs) < 10) {
54 $mins = $aes[1] +
$bes[1] +
$change; // Minutes.
55 $change = floor($mins / 60);
56 $mins = $mins - ($change * 60);
61 $hours = $aes[0] +
$bes[0] +
$change; // Hours.
63 $hours = '0' . $hours;
67 return $hours . ":" . $mins . ":" . $secs . '.' . $cents;
69 return $hours . ":" . $mins . ":" . $secs;
74 * Take the header row of an AICC definition file
75 * and returns sequence of columns and a pointer to
76 * the sco identifier column.
78 * @param string $row AICC header row
79 * @param string $mastername AICC sco identifier column
82 function scorm_get_aicc_columns($row, $mastername='system_id') {
83 $tok = strtok(strtolower($row), "\",\n\r");
84 $result = new stdClass();
85 $result->columns
= array();
86 $result->mastercol
= 0;
90 $result->columns
[] = $tok;
91 if ($tok == $mastername) {
92 $result->mastercol
= $i;
96 $tok = strtok("\",\n\r");
102 * Given a colums array return a string containing the regular
103 * expression to match the columns in a text row.
105 * @param array $column The header columns
106 * @param string $remodule The regular expression module for a single column
109 function scorm_forge_cols_regexp($columns, $remodule='(".*")?,') {
111 foreach ($columns as $column) {
112 $regexp .= $remodule;
114 $regexp = substr($regexp, 0, -1) . '/';
119 * Sets up AICC packages
120 * Called whenever package changes
121 * @param object $scorm instance - fields are updated and changes saved into database
124 function scorm_parse_aicc(&$scorm) {
127 if ($scorm->scormtype
== SCORM_TYPE_AICCURL
) {
128 return scorm_aicc_generate_simple_sco($scorm);
130 if (!isset($scorm->cmid
)) {
131 $cm = get_coursemodule_from_instance('scorm', $scorm->id
);
132 $scorm->cmid
= $cm->id
;
134 $context = context_module
::instance($scorm->cmid
);
136 $fs = get_file_storage();
138 $files = $fs->get_area_files($context->id
, 'mod_scorm', 'content', 0, 'sortorder, itemid, filepath, filename', false);
143 $extaiccfiles = array('crs', 'des', 'au', 'cst', 'ort', 'pre', 'cmp');
145 foreach ($files as $file) {
146 $filename = $file->get_filename();
147 $ext = substr($filename, strrpos($filename, '.'));
148 $extension = strtolower(substr($ext, 1));
149 if (in_array($extension, $extaiccfiles)) {
150 $id = strtolower(basename($filename, $ext));
151 if (!isset($ids[$id])) {
152 $ids[$id] = new stdClass();
154 $ids[$id]->$extension = $file;
158 foreach ($ids as $courseid => $id) {
159 if (!isset($courses[$courseid])) {
160 $courses[$courseid] = new stdClass();
162 if (isset($id->crs
)) {
163 $contents = $id->crs
->get_content();
164 $rows = explode("\r\n", $contents);
165 if (is_array($rows)) {
166 foreach ($rows as $row) {
167 if (preg_match("/^(.+)=(.+)$/", $row, $matches)) {
168 switch (strtolower(trim($matches[1]))) {
170 $courses[$courseid]->id
= trim($matches[2]);
173 $courses[$courseid]->title
= trim($matches[2]);
176 $courses[$courseid]->version
= 'AICC_'.trim($matches[2]);
183 if (isset($id->des
)) {
184 $contents = $id->des
->get_content();
185 $rows = explode("\r\n", $contents);
186 $columns = scorm_get_aicc_columns($rows[0]);
187 $regexp = scorm_forge_cols_regexp($columns->columns
);
188 for ($i = 1; $i < count($rows); $i++
) {
189 if (preg_match($regexp, $rows[$i], $matches)) {
190 for ($j = 0; $j < count($columns->columns
); $j++
) {
191 $column = $columns->columns
[$j];
192 if (!isset($courses[$courseid]->elements
[substr(trim($matches[$columns->mastercol +
1]), 1 , -1)])) {
193 $courses[$courseid]->elements
[substr(trim($matches[$columns->mastercol +
1]), 1 , -1)] = new stdClass();
195 $courses[$courseid]->elements
[substr(trim($matches[$columns->mastercol +
1]), 1 , -1)]->$column = substr(trim($matches[$j +
1]), 1, -1);
200 if (isset($id->au
)) {
201 $contents = $id->au
->get_content();
202 $rows = explode("\r\n", $contents);
203 $columns = scorm_get_aicc_columns($rows[0]);
204 $regexp = scorm_forge_cols_regexp($columns->columns
);
205 for ($i = 1; $i < count($rows); $i++
) {
206 if (preg_match($regexp, $rows[$i], $matches)) {
207 for ($j = 0; $j < count($columns->columns
); $j++
) {
208 $column = $columns->columns
[$j];
209 $courses[$courseid]->elements
[substr(trim($matches[$columns->mastercol +
1]), 1, -1)]->$column = substr(trim($matches[$j +
1]), 1, -1);
214 if (isset($id->cst
)) {
215 $contents = $id->cst
->get_content();
216 $rows = explode("\r\n", $contents);
217 $columns = scorm_get_aicc_columns($rows[0], 'block');
218 $regexp = scorm_forge_cols_regexp($columns->columns
, '(.+)?,');
219 for ($i = 1; $i < count($rows); $i++
) {
220 if (preg_match($regexp, $rows[$i], $matches)) {
221 for ($j = 0; $j < count($columns->columns
); $j++
) {
222 if ($j != $columns->mastercol
) {
223 $element = substr(trim($matches[$j +
1]), 1 , -1);
224 if (!empty($element)) {
225 $courses[$courseid]->elements
[$element]->parent
= substr(trim($matches[$columns->mastercol +
1]), 1, -1);
232 if (isset($id->ort
)) {
233 $contents = $id->ort
->get_content();
234 $rows = explode("\r\n", $contents);
235 $columns = scorm_get_aicc_columns($rows[0], 'course_element');
236 $regexp = scorm_forge_cols_regexp($columns->columns
, '(.+)?,');
237 for ($i = 1; $i < count($rows); $i++
) {
238 if (preg_match($regexp, $rows[$i], $matches)) {
239 for ($j = 0; $j < count($matches) - 1; $j++
) {
240 if ($j != $columns->mastercol
) {
241 $courses[$courseid]->elements
[substr(trim($matches[$j +
1]), 1, -1)]->parent
= substr(trim($matches[$columns->mastercol +
1]), 1, -1);
247 if (isset($id->pre
)) {
248 $contents = $id->pre
->get_content();
249 $rows = explode("\r\n", $contents);
250 $columns = scorm_get_aicc_columns($rows[0], 'structure_element');
251 $regexp = scorm_forge_cols_regexp($columns->columns
, '(.+),');
252 for ($i = 1; $i < count($rows); $i++
) {
253 if (preg_match($regexp, $rows[$i], $matches)) {
254 $elementid = trim($matches[$columns->mastercol +
1]);
255 $elementid = trim(trim($elementid, '"'), "'"); // Remove any quotes.
257 $prereq = trim($matches[2 - $columns->mastercol
]);
258 $prereq = trim(trim($prereq, '"'), "'"); // Remove any quotes.
260 $courses[$courseid]->elements
[$elementid]->prerequisites
= $prereq;
264 if (isset($id->cmp
)) {
265 $contents = $id->cmp
->get_content();
266 $rows = explode("\r\n", $contents);
270 $oldscoes = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id
));
273 if (isset($courses)) {
274 foreach ($courses as $course) {
276 $sco = new stdClass();
277 $sco->identifier
= $course->id
;
278 $sco->scorm
= $scorm->id
;
279 $sco->organization
= '';
280 $sco->title
= $course->title
;
283 $sco->scormtype
= '';
284 $sco->sortorder
= $sortorder;
286 if ($ss = $DB->get_record('scorm_scoes', array('scorm' => $scorm->id
,
287 'identifier' => $sco->identifier
))) {
290 $DB->update_record('scorm_scoes', $sco);
291 unset($oldscoes[$id]);
293 $id = $DB->insert_record('scorm_scoes', $sco);
299 if (isset($course->elements
)) {
300 foreach ($course->elements
as $element) {
302 $sco = new stdClass();
303 $sco->identifier
= $element->system_id
;
304 $sco->scorm
= $scorm->id
;
305 $sco->organization
= $course->id
;
306 $sco->title
= $element->title
;
308 if (!isset($element->parent
)) {
310 } else if (strtolower($element->parent
) == 'root') {
311 $sco->parent
= $course->id
;
313 $sco->parent
= $element->parent
;
316 $sco->scormtype
= '';
320 // Is it an Assignable Unit (AU)?
321 if (isset($element->file_name
)) {
322 $sco->launch
= $element->file_name
;
323 $sco->scormtype
= 'sco';
325 if ($oldscoid = scorm_array_search('identifier', $sco->identifier
, $oldscoes)) {
326 $sco->id
= $oldscoid;
327 $DB->update_record('scorm_scoes', $sco);
329 $DB->delete_records('scorm_scoes_data', array('scoid' => $oldscoid));
330 unset($oldscoes[$oldscoid]);
332 $id = $DB->insert_record('scorm_scoes', $sco);
335 $scodata = new stdClass();
336 $scodata->scoid
= $id;
337 if (isset($element->web_launch
)) {
338 $scodata->name
= 'parameters';
339 $scodata->value
= $element->web_launch
;
340 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
342 if (isset($element->prerequisites
)) {
343 $scodata->name
= 'prerequisites';
344 $scodata->value
= $element->prerequisites
;
345 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
347 if (isset($element->max_time_allowed
)) {
348 $scodata->name
= 'max_time_allowed';
349 $scodata->value
= $element->max_time_allowed
;
350 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
352 if (isset($element->time_limit_action
)) {
353 $scodata->name
= 'time_limit_action';
354 $scodata->value
= $element->time_limit_action
;
355 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
357 if (isset($element->mastery_score
)) {
358 $scodata->name
= 'mastery_score';
359 $scodata->value
= $element->mastery_score
;
360 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
362 if (isset($element->core_vendor
)) {
363 $scodata->name
= 'datafromlms';
364 $scodata->value
= preg_replace('/<cr>/i', "\r\n", $element->core_vendor
);
365 $dataid = $DB->insert_record('scorm_scoes_data', $scodata);
375 if (!empty($oldscoes)) {
376 foreach ($oldscoes as $oldsco) {
377 $DB->delete_records('scorm_scoes', array('id' => $oldsco->id
));
378 $DB->delete_records('scorm_scoes_track', array('scoid' => $oldsco->id
));
382 // Find first launchable object.
383 $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
384 // We use get_records here as we need to pass a limit in the query that works cross db.
385 $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scorm->id
), 'sortorder', 'id', 0, 1);
386 if (!empty($scoes)) {
387 $sco = reset($scoes); // We only care about the first record - the above query only returns one.
388 $scorm->launch
= $sco->id
;
390 $scorm->launch
= $launch;
393 $scorm->version
= 'AICC';
399 * Given a scormid creates an AICC Session record to allow HACP
401 * @param int $scormid - id from scorm table
402 * @return string hacpsession
404 function scorm_aicc_get_hacp_session($scormid) {
405 global $USER, $DB, $SESSION;
406 $cfgscorm = get_config('scorm');
407 if (empty($cfgscorm->allowaicchacp
)) {
412 $hacpsession = $SESSION->scorm
;
413 $hacpsession->scormid
= $scormid;
414 $hacpsession->hacpsession
= random_string(20);
415 $hacpsession->userid
= $USER->id
;
416 $hacpsession->timecreated
= $now;
417 $hacpsession->timemodified
= $now;
418 $DB->insert_record('scorm_aicc_session', $hacpsession);
420 return $hacpsession->hacpsession
;
424 * Check the hacp_session for whether it is valid.
426 * @param string $hacpsession The hacpsession value to check (optional). Normally leave this blank
427 * and this function will do required_param('sesskey', ...).
428 * @return mixed - false if invalid, otherwise returns record from scorm_aicc_session table.
430 function scorm_aicc_confirm_hacp_session($hacpsession) {
432 $cfgscorm = get_config('scorm');
433 if (empty($cfgscorm->allowaicchacp
)) {
436 $time = time() - ($cfgscorm->aicchacptimeout
* 60);
437 $sql = "hacpsession = ? AND timemodified > ?";
438 $hacpsession = $DB->get_record_select('scorm_aicc_session', $sql, array($hacpsession, $time));
439 if (!empty($hacpsession)) { // Update timemodified as this is still an active session - resets the timeout.
440 $hacpsession->timemodified
= time();
441 $DB->update_record('scorm_aicc_session', $hacpsession);
447 * generate a simple single activity AICC object
448 * structure to wrap around and externally linked
451 * @param object $scorm package record
453 function scorm_aicc_generate_simple_sco($scorm) {
455 // Find the oldest one.
456 $scos = $DB->get_records('scorm_scoes', array('scorm' => $scorm->id
), 'id');
458 $sco = array_shift($scos);
460 $sco = new stdClass();
462 // Get rid of old ones.
463 foreach ($scos as $oldsco) {
464 $DB->delete_records('scorm_scoes', array('id' => $oldsco->id
));
465 $DB->delete_records('scorm_scoes_track', array('scoid' => $oldsco->id
));
468 $sco->identifier
= 'A1';
469 $sco->scorm
= $scorm->id
;
470 $sco->organization
= '';
471 $sco->title
= $scorm->name
;
473 // Add the HACP signal to the activity launcher.
474 if (preg_match('/\?/', $scorm->reference
)) {
475 $sco->launch
= $scorm->reference
.'&CMI=HACP';
477 $sco->launch
= $scorm->reference
.'?CMI=HACP';
479 $sco->scormtype
= 'sco';
480 if (isset($sco->id
)) {
481 $DB->update_record('scorm_scoes', $sco);
484 $id = $DB->insert_record('scorm_scoes', $sco);
490 * Sets up $userdata array and default values for AICC package.
492 * @param stdClass $userdata an empty stdClass variable that should be set up with user values
493 * @param object $scorm package record
494 * @param string $scoid SCO Id
495 * @param string $attempt attempt number for the user
496 * @param string $mode scorm display mode type
497 * @return array The default values that should be used for AICC package
499 function get_scorm_default (&$userdata, $scorm, $scoid, $attempt, $mode) {
501 $aiccuserid = get_config('scorm', 'aiccuserid');
502 if (!empty($aiccuserid)) {
503 $userdata->student_id
= $USER->id
;
505 $userdata->student_id
= $USER->username
;
507 $userdata->student_name
= $USER->lastname
.', '. $USER->firstname
;
509 if ($usertrack = scorm_get_tracks($scoid, $USER->id
, $attempt)) {
510 foreach ($usertrack as $key => $value) {
511 $userdata->$key = $value;
514 $userdata->status
= '';
515 $userdata->score_raw
= '';
518 if ($scodatas = scorm_get_sco($scoid, SCO_DATA
)) {
519 foreach ($scodatas as $key => $value) {
520 $userdata->$key = $value;
523 print_error('cannotfindsco', 'scorm');
525 if (!$sco = scorm_get_sco($scoid)) {
526 print_error('cannotfindsco', 'scorm');
529 $userdata->mode
= 'normal';
531 $userdata->mode
= $mode;
533 if ($userdata->mode
== 'normal') {
534 $userdata->credit
= 'credit';
536 $userdata->credit
= 'no-credit';
539 if (isset($userdata->status
)) {
540 if ($userdata->status
== '') {
541 $userdata->entry
= 'ab-initio';
543 if (isset($userdata->{'cmi.core.exit'}) && ($userdata->{'cmi.core.exit'} == 'suspend')) {
544 $userdata->entry
= 'resume';
546 $userdata->entry
= '';
552 $def['cmi.core.student_id'] = $userdata->student_id
;
553 $def['cmi.core.student_name'] = $userdata->student_name
;
554 $def['cmi.core.credit'] = $userdata->credit
;
555 $def['cmi.core.entry'] = $userdata->entry
;
556 $def['cmi.launch_data'] = scorm_isset($userdata, 'datafromlms');
557 $def['cmi.core.lesson_mode'] = $userdata->mode
;
558 $def['cmi.student_data.attempt_number'] = scorm_isset($userdata, 'cmi.student_data.attempt_number');
559 $def['cmi.student_data.mastery_score'] = scorm_isset($userdata, 'mastery_score');
560 $def['cmi.student_data.max_time_allowed'] = scorm_isset($userdata, 'max_time_allowed');
561 $def['cmi.student_data.time_limit_action'] = scorm_isset($userdata, 'time_limit_action');
562 $def['cmi.student_data.tries_during_lesson'] = scorm_isset($userdata, 'cmi.student_data.tries_during_lesson');
564 $def['cmi.core.lesson_location'] = scorm_isset($userdata, 'cmi.core.lesson_location');
565 $def['cmi.core.lesson_status'] = scorm_isset($userdata, 'cmi.core.lesson_status');
566 $def['cmi.core.exit'] = scorm_isset($userdata, 'cmi.core.exit');
567 $def['cmi.core.score.raw'] = scorm_isset($userdata, 'cmi.core.score.raw');
568 $def['cmi.core.score.max'] = scorm_isset($userdata, 'cmi.core.score.max');
569 $def['cmi.core.score.min'] = scorm_isset($userdata, 'cmi.core.score.min');
570 $def['cmi.core.total_time'] = scorm_isset($userdata, 'cmi.core.total_time', '00:00:00');
571 $def['cmi.suspend_data'] = scorm_isset($userdata, 'cmi.suspend_data');
572 $def['cmi.comments'] = scorm_isset($userdata, 'cmi.comments');