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 * Rest endpoint for ajax editing of quiz structure.
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 if (!defined('AJAX_SCRIPT')) {
26 define('AJAX_SCRIPT', true);
29 require_once(__DIR__
. '/../../config.php');
30 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
32 // Initialise ALL the incoming parameters here, up front.
33 $quizid = required_param('quizid', PARAM_INT
);
34 $class = required_param('class', PARAM_ALPHA
);
35 $field = optional_param('field', '', PARAM_ALPHA
);
36 $instanceid = optional_param('instanceId', 0, PARAM_INT
);
37 $sectionid = optional_param('sectionId', 0, PARAM_INT
);
38 $previousid = optional_param('previousid', 0, PARAM_INT
);
39 $value = optional_param('value', 0, PARAM_INT
);
40 $column = optional_param('column', 0, PARAM_ALPHA
);
41 $id = optional_param('id', 0, PARAM_INT
);
42 $summary = optional_param('summary', '', PARAM_RAW
);
43 $sequence = optional_param('sequence', '', PARAM_SEQUENCE
);
44 $visible = optional_param('visible', 0, PARAM_INT
);
45 $pageaction = optional_param('action', '', PARAM_ALPHA
); // Used to simulate a DELETE command.
46 $maxmark = optional_param('maxmark', '', PARAM_FLOAT
);
47 $newheading = optional_param('newheading', '', PARAM_TEXT
);
48 $shuffle = optional_param('newshuffle', 0, PARAM_INT
);
49 $page = optional_param('page', '', PARAM_INT
);
50 $PAGE->set_url('/mod/quiz/edit-rest.php',
51 array('quizid' => $quizid, 'class' => $class));
54 $quiz = $DB->get_record('quiz', array('id' => $quizid), '*', MUST_EXIST
);
55 $cm = get_coursemodule_from_instance('quiz', $quiz->id
, $quiz->course
);
56 $course = $DB->get_record('course', array('id' => $quiz->course
), '*', MUST_EXIST
);
57 require_login($course, false, $cm);
59 $quizobj = new quiz($quiz, $cm, $course);
60 $structure = $quizobj->get_structure();
61 $modcontext = context_module
::instance($cm->id
);
63 echo $OUTPUT->header(); // Send headers.
65 // OK, now let's process the parameters and do stuff
66 // MDL-10221 the DELETE method is not allowed on some web servers,
67 // so we simulate it with the action URL param.
68 $requestmethod = $_SERVER['REQUEST_METHOD'];
69 if ($pageaction == 'DELETE') {
70 $requestmethod = 'DELETE';
73 switch($requestmethod) {
75 case 'GET': // For debugging.
78 $table = 'quiz_sections';
80 case 'getsectiontitle':
81 require_capability('mod/quiz:manage', $modcontext);
82 $section = $structure->get_section_by_id($id);
83 echo json_encode(array('instancesection' => $section->heading
));
85 case 'updatesectiontitle':
86 require_capability('mod/quiz:manage', $modcontext);
87 $structure->set_section_heading($id, $newheading);
88 echo json_encode(array('instancesection' => format_string($newheading)));
90 case 'updateshufflequestions':
91 require_capability('mod/quiz:manage', $modcontext);
92 $structure->set_section_shuffle($id, $shuffle);
93 echo json_encode(array('instanceshuffle' => $section->shufflequestions
));
101 require_capability('mod/quiz:manage', $modcontext);
103 $section = $structure->get_section_by_id($sectionid);
104 if ($section->firstslot
> 1) {
105 $previousid = $structure->get_slot_id_for_slot($section->firstslot
- 1);
106 $page = $structure->get_page_number_for_slot($section->firstslot
);
109 $structure->move_slot($id, $previousid, $page);
110 quiz_delete_previews($quiz);
111 echo json_encode(array('visible' => true));
115 require_capability('mod/quiz:manage', $modcontext);
116 $slot = $DB->get_record('quiz_slots', array('id' => $id), '*', MUST_EXIST
);
117 echo json_encode(array('instancemaxmark' =>
118 quiz_format_question_grade($quiz, $slot->maxmark
)));
121 case 'updatemaxmark':
122 require_capability('mod/quiz:manage', $modcontext);
123 $slot = $structure->get_slot_by_id($id);
124 if ($structure->update_slot_maxmark($slot, $maxmark)) {
125 // Grade has really changed.
126 quiz_delete_previews($quiz);
127 quiz_update_sumgrades($quiz);
128 quiz_update_all_attempt_sumgrades($quiz);
129 quiz_update_all_final_grades($quiz);
130 quiz_update_grades($quiz, 0, true);
132 echo json_encode(array('instancemaxmark' => quiz_format_question_grade($quiz, $maxmark),
133 'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades
)));
136 case 'updatepagebreak':
137 require_capability('mod/quiz:manage', $modcontext);
138 $slots = $structure->update_page_break($id, $value);
140 foreach ($slots as $slot) {
141 $json[$slot->slot
] = array('id' => $slot->id
, 'slot' => $slot->slot
,
142 'page' => $slot->page
);
144 echo json_encode(array('slots' => $json));
147 case 'updatedependency':
148 require_capability('mod/quiz:manage', $modcontext);
149 $slot = $structure->get_slot_by_id($id);
150 $value = (bool) $value;
151 $structure->update_question_dependency($slot->id
, $value);
152 echo json_encode(array('requireprevious' => $value));
162 require_capability('mod/quiz:manage', $modcontext);
163 $structure->remove_section_heading($id);
164 echo json_encode(array('deleted' => true));
168 require_capability('mod/quiz:manage', $modcontext);
169 if (!$slot = $DB->get_record('quiz_slots', array('quizid' => $quiz->id
, 'id' => $id))) {
170 throw new moodle_exception('AJAX commands.php: Bad slot ID '.$id);
172 $structure->remove_slot($slot->slot
);
173 quiz_delete_previews($quiz);
174 quiz_update_sumgrades($quiz);
175 echo json_encode(array('newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades
),
176 'deleted' => true, 'newnumquestions' => $structure->get_question_count()));