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.
13//
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/>.
16
17require_once('../../config.php');
18require_once($CFG->dirroot.'/mod/scorm/locallib.php');
19
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.
24
25if (!empty($id)) {
26    if (! $cm = get_coursemodule_from_id('scorm', $id)) {
27        print_error('invalidcoursemodule');
28    }
29    if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
30        print_error('coursemisconf');
31    }
32    if (! $scorm = $DB->get_record("scorm", array("id" => $cm->instance))) {
33        print_error('invalidcoursemodule');
34    }
35} else if (!empty($a)) {
36    if (! $scorm = $DB->get_record("scorm", array("id" => $a))) {
37        print_error('invalidcoursemodule');
38    }
39    if (! $course = $DB->get_record("course", array("id" => $scorm->course))) {
40        print_error('coursemisconf');
41    }
42    if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
43        print_error('invalidcoursemodule');
44    }
45} else {
46    print_error('missingparameter');
47}
48
49$PAGE->set_url('/mod/scorm/datamodel.php', array('scoid' => $scoid, 'attempt' => $attempt, 'id' => $cm->id));
50
51require_login($course, false, $cm);
52
53if (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;
66            }
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');
70
71                $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@', '@exit@',
72                                    '@exitAll@', '@abandon@', '@abandonAll@');
73                $replace = array('continue_', 'previous_', '\1', 'exit_', 'exitall_', 'abandon_', 'abandonall');
74                $action = preg_replace($search, $replace, $value);
75
76                if ($action != $value) {
77                    // Evaluating navigation request.
78                    $valid = scorm_seq_overall ($scoid, $USER->id, $action, $attempt);
79                    $valid = 'true';
80
81                    // Set valid request.
82                    $search = array('@continue@', '@previous@', '@\{target=(\S+)\}choice@');
83                    $replace = array('true', 'true', 'true');
84                    $matched = preg_replace($search, $replace, $value);
85                    if ($matched == 'true') {
86                        $request = 'adl.nav.request_valid["'.$action.'"] = "'.$valid.'";';
87                    }
88                }
89            }
90        }
91    }
92    if ($result) {
93        echo "true\n0";
94    } else {
95        echo "false\n101";
96    }
97    if ($request != null) {
98        echo "\n".$request;
99    }
100}
101