1 |
efrain |
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 |
|
|
|
17 |
/**
|
|
|
18 |
* Rest endpoint for ajax editing of quiz structure.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_quiz
|
|
|
21 |
* @copyright 1999 Martin Dougiamas http://dougiamas.com
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use mod_quiz\quiz_settings;
|
|
|
26 |
|
|
|
27 |
if (!defined('AJAX_SCRIPT')) {
|
|
|
28 |
define('AJAX_SCRIPT', true);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
require_once(__DIR__ . '/../../config.php');
|
|
|
32 |
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
|
|
33 |
|
|
|
34 |
// Initialise ALL the incoming parameters here, up front.
|
|
|
35 |
$quizid = required_param('quizid', PARAM_INT);
|
|
|
36 |
$class = required_param('class', PARAM_ALPHA);
|
|
|
37 |
$field = optional_param('field', '', PARAM_ALPHA);
|
|
|
38 |
$instanceid = optional_param('instanceId', 0, PARAM_INT);
|
|
|
39 |
$sectionid = optional_param('sectionId', 0, PARAM_INT);
|
|
|
40 |
$previousid = optional_param('previousid', 0, PARAM_INT);
|
|
|
41 |
$value = optional_param('value', 0, PARAM_INT);
|
|
|
42 |
$column = optional_param('column', 0, PARAM_ALPHA);
|
|
|
43 |
$id = optional_param('id', 0, PARAM_INT);
|
|
|
44 |
$summary = optional_param('summary', '', PARAM_RAW);
|
|
|
45 |
$sequence = optional_param('sequence', '', PARAM_SEQUENCE);
|
|
|
46 |
$visible = optional_param('visible', 0, PARAM_INT);
|
|
|
47 |
$pageaction = optional_param('action', '', PARAM_ALPHA); // Used to simulate a DELETE command.
|
|
|
48 |
$maxmark = optional_param('maxmark', '', PARAM_FLOAT);
|
|
|
49 |
$newheading = optional_param('newheading', '', PARAM_TEXT);
|
|
|
50 |
$shuffle = optional_param('newshuffle', 0, PARAM_INT);
|
|
|
51 |
$page = optional_param('page', '', PARAM_INT);
|
|
|
52 |
$ids = optional_param('ids', '', PARAM_SEQUENCE);
|
|
|
53 |
$PAGE->set_url('/mod/quiz/edit-rest.php',
|
|
|
54 |
['quizid' => $quizid, 'class' => $class]);
|
|
|
55 |
|
|
|
56 |
require_sesskey();
|
|
|
57 |
$quizobj = quiz_settings::create($quizid);
|
|
|
58 |
$quiz = $quizobj->get_quiz();
|
|
|
59 |
$cm = $quizobj->get_cm();
|
|
|
60 |
$course = $quizobj->get_course();
|
|
|
61 |
require_login($course, false, $cm);
|
|
|
62 |
|
|
|
63 |
$structure = $quizobj->get_structure();
|
|
|
64 |
$gradecalculator = $quizobj->get_grade_calculator();
|
|
|
65 |
$modcontext = $quizobj->get_context();
|
|
|
66 |
|
|
|
67 |
echo $OUTPUT->header(); // Send headers.
|
|
|
68 |
|
|
|
69 |
// All these AJAX actions should be logically atomic.
|
|
|
70 |
$transaction = $DB->start_delegated_transaction();
|
|
|
71 |
|
|
|
72 |
// OK, now let's process the parameters and do stuff
|
|
|
73 |
// MDL-10221 the DELETE method is not allowed on some web servers,
|
|
|
74 |
// so we simulate it with the action URL param.
|
|
|
75 |
$requestmethod = $_SERVER['REQUEST_METHOD'];
|
|
|
76 |
if ($pageaction == 'DELETE') {
|
|
|
77 |
$requestmethod = 'DELETE';
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$result = null;
|
|
|
81 |
|
|
|
82 |
switch($requestmethod) {
|
|
|
83 |
case 'POST':
|
|
|
84 |
case 'GET': // For debugging.
|
|
|
85 |
switch ($class) {
|
|
|
86 |
case 'section':
|
|
|
87 |
$table = 'quiz_sections';
|
|
|
88 |
$section = $structure->get_section_by_id($id);
|
|
|
89 |
switch ($field) {
|
|
|
90 |
case 'getsectiontitle':
|
|
|
91 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
92 |
$result = ['instancesection' => $section->heading];
|
|
|
93 |
break;
|
|
|
94 |
case 'updatesectiontitle':
|
|
|
95 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
96 |
$structure->set_section_heading($id, $newheading);
|
|
|
97 |
$result = ['instancesection' => format_string($newheading)];
|
|
|
98 |
break;
|
|
|
99 |
case 'updateshufflequestions':
|
|
|
100 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
101 |
$structure->set_section_shuffle($id, $shuffle);
|
|
|
102 |
$result = ['instanceshuffle' => $section->shufflequestions];
|
|
|
103 |
break;
|
|
|
104 |
}
|
|
|
105 |
break;
|
|
|
106 |
|
|
|
107 |
case 'resource':
|
|
|
108 |
switch ($field) {
|
|
|
109 |
case 'move':
|
|
|
110 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
111 |
if (!$previousid) {
|
|
|
112 |
$section = $structure->get_section_by_id($sectionid);
|
|
|
113 |
if ($section->firstslot > 1) {
|
|
|
114 |
$previousid = $structure->get_slot_id_for_slot($section->firstslot - 1);
|
|
|
115 |
$page = $structure->get_page_number_for_slot($section->firstslot);
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
$structure->move_slot($id, $previousid, $page);
|
|
|
119 |
quiz_delete_previews($quiz);
|
|
|
120 |
$result = ['visible' => true];
|
|
|
121 |
break;
|
|
|
122 |
|
|
|
123 |
case 'getmaxmark':
|
|
|
124 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
125 |
$slot = $DB->get_record('quiz_slots', ['id' => $id], '*', MUST_EXIST);
|
|
|
126 |
$result = ['instancemaxmark' => quiz_format_question_grade($quiz, $slot->maxmark)];
|
|
|
127 |
break;
|
|
|
128 |
|
|
|
129 |
case 'updatemaxmark':
|
|
|
130 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
131 |
$slot = $structure->get_slot_by_id($id);
|
|
|
132 |
if ($structure->update_slot_maxmark($slot, $maxmark)) {
|
|
|
133 |
// Grade has really changed.
|
|
|
134 |
quiz_delete_previews($quiz);
|
|
|
135 |
$gradecalculator->recompute_quiz_sumgrades();
|
|
|
136 |
$gradecalculator->recompute_all_attempt_sumgrades();
|
|
|
137 |
$gradecalculator->recompute_all_final_grades();
|
|
|
138 |
quiz_update_grades($quiz, 0, true);
|
|
|
139 |
}
|
|
|
140 |
$result = ['instancemaxmark' => quiz_format_question_grade($quiz, $maxmark),
|
|
|
141 |
'newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades)];
|
|
|
142 |
break;
|
|
|
143 |
|
|
|
144 |
case 'updatepagebreak':
|
|
|
145 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
146 |
$slots = $structure->update_page_break($id, $value);
|
|
|
147 |
$json = [];
|
|
|
148 |
foreach ($slots as $slot) {
|
|
|
149 |
$json[$slot->slot] = ['id' => $slot->id, 'slot' => $slot->slot,
|
|
|
150 |
'page' => $slot->page];
|
|
|
151 |
}
|
|
|
152 |
quiz_delete_previews($quiz);
|
|
|
153 |
$result = ['slots' => $json];
|
|
|
154 |
break;
|
|
|
155 |
|
|
|
156 |
case 'deletemultiple':
|
|
|
157 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
158 |
|
|
|
159 |
$ids = explode(',', $ids);
|
|
|
160 |
foreach ($ids as $id) {
|
|
|
161 |
$slot = $DB->get_record('quiz_slots', ['quizid' => $quiz->id, 'id' => $id],
|
|
|
162 |
'*', MUST_EXIST);
|
|
|
163 |
if ($structure->has_use_capability($slot->slot)) {
|
|
|
164 |
$structure->remove_slot($slot->slot);
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
quiz_delete_previews($quiz);
|
|
|
168 |
$gradecalculator->recompute_quiz_sumgrades();
|
|
|
169 |
|
|
|
170 |
$result = ['newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades),
|
|
|
171 |
'deleted' => true, 'newnumquestions' => $structure->get_question_count()];
|
|
|
172 |
break;
|
|
|
173 |
|
|
|
174 |
case 'updatedependency':
|
|
|
175 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
176 |
$slot = $structure->get_slot_by_id($id);
|
|
|
177 |
$value = (bool) $value;
|
|
|
178 |
$structure->update_question_dependency($slot->id, $value);
|
|
|
179 |
$result = ['requireprevious' => $value];
|
|
|
180 |
break;
|
|
|
181 |
}
|
|
|
182 |
break;
|
|
|
183 |
}
|
|
|
184 |
break;
|
|
|
185 |
|
|
|
186 |
case 'DELETE':
|
|
|
187 |
switch ($class) {
|
|
|
188 |
case 'section':
|
|
|
189 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
190 |
$structure->remove_section_heading($id);
|
|
|
191 |
$result = ['deleted' => true];
|
|
|
192 |
break;
|
|
|
193 |
|
|
|
194 |
case 'resource':
|
|
|
195 |
require_capability('mod/quiz:manage', $modcontext);
|
|
|
196 |
if (!$slot = $DB->get_record('quiz_slots', ['quizid' => $quiz->id, 'id' => $id])) {
|
|
|
197 |
throw new moodle_exception('AJAX commands.php: Bad slot ID '.$id);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (!$structure->has_use_capability($slot->slot)) {
|
|
|
201 |
$slotdetail = $structure->get_slot_by_id($slot->id);
|
|
|
202 |
$context = context::instance_by_id($slotdetail->contextid);
|
|
|
203 |
throw new required_capability_exception($context,
|
|
|
204 |
'moodle/question:useall', 'nopermissions', '');
|
|
|
205 |
}
|
|
|
206 |
$structure->remove_slot($slot->slot);
|
|
|
207 |
quiz_delete_previews($quiz);
|
|
|
208 |
$gradecalculator->recompute_quiz_sumgrades();
|
|
|
209 |
$result = ['newsummarks' => quiz_format_grade($quiz, $quiz->sumgrades),
|
|
|
210 |
'deleted' => true, 'newnumquestions' => $structure->get_question_count()];
|
|
|
211 |
break;
|
|
|
212 |
}
|
|
|
213 |
break;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
$transaction->allow_commit();
|
|
|
217 |
echo json_encode($result);
|