MDL-45144 convert book events to create_from_xxx() helpers
[moodle.git] / mod / scorm / loaddatamodel.php
blobf9d6ba1bff63e8b58ada9f7844f3331759145b66
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * This page loads the correct JS file based on package type
20 * @package mod_scorm
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../../config.php");
26 require_once('locallib.php');
28 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
29 $a = optional_param('a', 0, PARAM_INT); // scorm ID.
30 $scoid = required_param('scoid', PARAM_INT); // sco ID.
31 $mode = optional_param('mode', '', PARAM_ALPHA); // navigation mode.
32 $currentorg = optional_param('currentorg', '', PARAM_RAW); // Selected organization.
33 $attempt = required_param('attempt', PARAM_INT); // new attempt.
35 if (!empty($id)) {
36 $cm = get_coursemodule_from_id('scorm', $id, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
38 $scorm = $DB->get_record('scorm', array('id'=>$cm->instance), '*', MUST_EXIST);
39 } else if (!empty($a)) {
40 $scorm = $DB->get_record('scorm', array('id'=>$a), '*', MUST_EXIST);
41 $course = $DB->get_record('course', array('id'=>$scorm->course), '*', MUST_EXIST);
42 $cm = get_coursemodule_from_instance('scorm', $scorm->id, $course->id, false, MUST_EXIST);
43 } else {
44 print_error('missingparameter');
47 $PAGE->set_url('/mod/scorm/loaddatamodel.php', array('scoid'=>$scoid, 'id'=>$cm->id));
49 require_login($course, false, $cm);
51 $userdata = new stdClass();
52 if ($usertrack = scorm_get_tracks($scoid, $USER->id, $attempt)) {
53 // According to SCORM 2004 spec(RTE V1, 4.2.8), only cmi.exit==suspend should allow previous datamodel elements on re-launch.
54 if (!scorm_version_check($scorm->version, SCORM_13) ||
55 (isset($usertrack->{'cmi.exit'}) && ($usertrack->{'cmi.exit'} == 'suspend'))) {
56 foreach ($usertrack as $key => $value) {
57 $userdata->$key = addslashes_js($value);
59 } else {
60 $userdata->status = '';
61 $userdata->score_raw = '';
63 } else {
64 $userdata->status = '';
65 $userdata->score_raw = '';
67 $userdata->student_id = addslashes_js($USER->username);
68 $userdata->student_name = addslashes_js($USER->lastname .', '. $USER->firstname);
69 $userdata->mode = 'normal';
70 if (!empty($mode)) {
71 $userdata->mode = $mode;
73 if ($userdata->mode == 'normal') {
74 $userdata->credit = 'credit';
75 } else {
76 $userdata->credit = 'no-credit';
78 if ($scodatas = scorm_get_sco($scoid, SCO_DATA)) {
79 foreach ($scodatas as $key => $value) {
80 $userdata->$key = addslashes_js($value);
82 } else {
83 print_error('cannotfindsco', 'scorm');
85 if (!$sco = scorm_get_sco($scoid)) {
86 print_error('cannotfindsco', 'scorm');
88 if (scorm_version_check($scorm->version, SCORM_13)) {
89 $objectives = $DB->get_records('scorm_seq_objective', array('scoid' => $scoid));
90 $index = 0;
91 foreach ($objectives as $objective) {
92 if (!empty($objective->minnormalizedmeasure)) {
93 $userdata->{'cmi.scaled_passing_score'} = $objective->minnormalizedmeasure;
95 if (!empty($objective->objectiveid)) {
96 $userdata->{'cmi.objectives.N'.$index.'.id'} = $objective->objectiveid;
97 $index++;
102 header('Content-Type: text/javascript; charset=UTF-8');
104 $scorm->version = strtolower(clean_param($scorm->version, PARAM_SAFEDIR)); // Just to be safe.
105 if (file_exists($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php')) {
106 include_once($CFG->dirroot.'/mod/scorm/datamodels/'.$scorm->version.'.js.php');
107 } else {
108 include_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_12.js.php');
110 // Set the start time of this SCO.
111 scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, 'x.start.time', time());
115 var errorCode = "0";
116 function underscore(str) {
117 str = String(str).replace(/.N/g,".");
118 return str.replace(/\./g,"__");