MDL-45144 convert book events to create_from_xxx() helpers
[moodle.git] / mod / scorm / datamodel.php
blobe176308cb9b7f4f311ff2c669cd662c75022e701
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 require_once('../../config.php');
18 require_once($CFG->dirroot.'/mod/scorm/locallib.php');
20 $id = optional_param('id', '', PARAM_INT); // Course Module ID, or
21 $a = optional_param('a', '', PARAM_INT); // scorm ID
22 $scoid = required_param('scoid', PARAM_INT); // sco ID
23 $attempt = required_param('attempt', PARAM_INT); // attempt number
25 if (!empty($id)) {
26 if (! $cm = get_coursemodule_from_id('scorm', $id)) {
27 print_error('invalidcoursemodule');
29 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
30 print_error('coursemisconf');
32 if (! $scorm = $DB->get_record("scorm", array("id"=>$cm->instance))) {
33 print_error('invalidcoursemodule');
35 } else if (!empty($a)) {
36 if (! $scorm = $DB->get_record("scorm", array("id"=>$a))) {
37 print_error('invalidcoursemodule');
39 if (! $course = $DB->get_record("course", array("id"=>$scorm->course))) {
40 print_error('coursemisconf');
42 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
43 print_error('invalidcoursemodule');
45 } else {
46 print_error('missingparameter');
49 $PAGE->set_url('/mod/scorm/datamodel.php', array('scoid'=>$scoid, 'attempt'=>$attempt, 'id'=>$cm->id));
51 require_login($course, false, $cm);
53 if (confirm_sesskey() && (!empty($scoid))) {
54 $result = true;
55 $request = null;
56 if (has_capability('mod/scorm:savetrack', context_module::instance($cm->id))) {
57 // Preload all current tracking data.
58 $trackdata = $DB->get_records('scorm_scoes_track', array('userid' => $USER->id, 'scormid' => $scorm->id, 'scoid' => $scoid,
59 'attempt' => $attempt), '', 'element, id, value, timemodified');
60 foreach (data_submitted() as $element => $value) {
61 $element = str_replace('__', '.', $element);
62 if (substr($element, 0, 3) == 'cmi') {
63 $netelement = preg_replace('/\.N(\d+)\./', "\.\$1\.", $element);
64 $result = scorm_insert_track($USER->id, $scorm->id, $scoid, $attempt, $element, $value, $scorm->forcecompleted,
65 $trackdata) && $result;
67 if (substr($element, 0, 15) == 'adl.nav.request') {
68 // SCORM 2004 Sequencing Request
69 require_once($CFG->dirroot.'/mod/scorm/datamodels/scorm_13lib.php');
71 $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@', '@exit@', '@exitAll@', '@abandon@', '@abandonAll@');
72 $replace = array('continue_', 'previous_', '\1', 'exit_', 'exitall_', 'abandon_', 'abandonall');
73 $action = preg_replace($search, $replace, $value);
75 if ($action != $value) {
76 // Evaluating navigation request
77 $valid = scorm_seq_overall ($scoid, $USER->id, $action, $attempt);
78 $valid = 'true';
80 // Set valid request
81 $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@');
82 $replace = array('true', 'true', 'true');
83 $matched = preg_replace($search, $replace, $value);
84 if ($matched == 'true') {
85 $request = 'adl.nav.request_valid["'.$action.'"] = "'.$valid.'";';
91 if ($result) {
92 echo "true\n0";
93 } else {
94 echo "false\n101";
96 if ($request != null) {
97 echo "\n".$request;