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/>.
19 * Page to edit quizzes
21 * This page generally has two columns:
22 * The right column lists all available questions in a chosen category and
23 * allows them to be edited or more to be added. This column is only there if
24 * the quiz does not already have student attempts
25 * The left column lists all questions that have been added to the current quiz.
26 * The lecturer can add questions from the right hand list to the quiz or remove them
28 * The script also processes a number of actions:
29 * Actions affecting a quiz:
30 * up and down Changes the order of questions and page breaks
31 * addquestion Adds a single question to the quiz
32 * add Adds several selected questions to the quiz
33 * addrandom Adds a certain number of random questions to the quiz
34 * repaginate Re-paginates the quiz
35 * delete Removes a question from the quiz
36 * savechanges Saves the order and grades for questions in the quiz
39 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 require_once('../../config.php');
45 require_once($CFG->dirroot . '/mod/quiz/editlib.php');
46 require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
47 require_once($CFG->dirroot . '/question/category_class.php');
49 // These params are only passed from page request to request while we stay on
50 // this page otherwise they would go in question_edit_setup.
51 $scrollpos = optional_param('scrollpos', '', PARAM_INT);
53 list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
54 question_edit_setup('editq', '/mod/quiz/edit.php', true);
56 $defaultcategoryobj = question_make_default_categories($contexts->all());
57 $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
59 $quizhasattempts = quiz_has_attempts($quiz->id);
61 $PAGE->set_url($thispageurl);
63 // Get the course object and related bits.
64 $course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
65 $quizobj = new quiz($quiz, $cm, $course);
66 $structure = $quizobj->get_structure();
68 // You need mod/quiz:manage in addition to question capabilities to access this page.
69 require_capability('mod/quiz:manage', $contexts->lowest());
73 'courseid' => $course->id,
74 'context' => $contexts->lowest(),
79 $event = \mod_quiz\event\edit_page_viewed::create($params);
82 // Process commands ============================================================.
84 // Get the list of question ids had their check-boxes ticked.
85 $selectedslots = array();
86 $params = (array) data_submitted();
87 foreach ($params as $key => $value) {
88 if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
89 $selectedslots[] = $matches[1];
93 $afteractionurl = new moodle_url($thispageurl);
95 $afteractionurl->param('scrollpos', $scrollpos);
98 if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
99 // Re-paginate the quiz.
100 $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
101 quiz_repaginate_questions($quiz->id, $questionsperpage );
102 quiz_delete_previews($quiz);
103 redirect($afteractionurl);
106 if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
107 // Add a single question to the current quiz.
108 quiz_require_question_use($addquestion);
109 $addonpage = optional_param('addonpage', 0, PARAM_INT);
110 quiz_add_quiz_question($addquestion, $quiz, $addonpage);
111 quiz_delete_previews($quiz);
112 quiz_update_sumgrades($quiz);
113 $thispageurl->param('lastchanged', $addquestion);
114 redirect($afteractionurl);
117 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
118 $addonpage = optional_param('addonpage', 0, PARAM_INT);
119 // Add selected questions to the current quiz.
120 $rawdata = (array) data_submitted();
121 foreach ($rawdata as $key => $value) { // Parse input for question ids.
122 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
124 quiz_require_question_use($key);
125 quiz_add_quiz_question($key, $quiz, $addonpage);
128 quiz_delete_previews($quiz);
129 quiz_update_sumgrades($quiz);
130 redirect($afteractionurl);
133 if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
134 // Add random questions to the quiz.
135 $recurse = optional_param('recurse', 0, PARAM_BOOL);
136 $addonpage = optional_param('addonpage', 0, PARAM_INT);
137 $categoryid = required_param('categoryid', PARAM_INT);
138 $randomcount = required_param('randomcount', PARAM_INT);
139 quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
141 quiz_delete_previews($quiz);
142 quiz_update_sumgrades($quiz);
143 redirect($afteractionurl);
146 if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
147 $deletepreviews = false;
148 $recomputesummarks = false;
150 $rawdata = (array) data_submitted();
151 $moveonpagequestions = array();
152 $moveselectedonpage = optional_param('moveselectedonpagetop', 0, PARAM_INT);
153 if (!$moveselectedonpage) {
154 $moveselectedonpage = optional_param('moveselectedonpagebottom', 0, PARAM_INT);
157 $newslotorder = array();
158 foreach ($rawdata as $key => $value) {
159 if (preg_match('!^g([0-9]+)$!', $key, $matches)) {
160 // Parse input for question -> grades.
161 $slotnumber = $matches[1];
162 $newgrade = unformat_float($value);
163 quiz_update_slot_maxmark($DB->get_record('quiz_slots',
164 array('quizid' => $quiz->id, 'slot' => $slotnumber), '*', MUST_EXIST), $newgrade);
165 $deletepreviews = true;
166 $recomputesummarks = true;
168 } else if (preg_match('!^o(pg)?([0-9]+)$!', $key, $matches)) {
169 // Parse input for ordering info.
170 $slotnumber = $matches[2];
171 // Make sure two questions don't overwrite each other. If we get a second
172 // question with the same position, shift the second one along to the next gap.
173 $value = clean_param($value, PARAM_INT);
174 while (array_key_exists($value, $newslotorder)) {
178 // This is a page-break entry.
179 $newslotorder[$value] = 0;
181 $newslotorder[$value] = $slotnumber;
183 $deletepreviews = true;
187 if ($moveselectedonpage) {
189 // Make up a $newslotorder, then let the next if statement do the work.
190 $oldslots = $DB->get_records('quiz_slots', array('quizid' => $quiz->id), 'slot');
192 $beforepage = array();
194 $afterpage = array();
195 foreach ($oldslots as $oldslot) {
196 if (in_array($oldslot->slot, $selectedslots)) {
197 $onpage[] = $oldslot;
198 } else if ($oldslot->page <= $moveselectedonpage) {
199 $beforepage[] = $oldslot;
201 $afterpage[] = $oldslot;
205 $newslotorder = array();
208 foreach ($beforepage as $slot) {
209 while ($currentpage < $slot->page) {
210 $newslotorder[$index] = 0;
214 $newslotorder[$index] = $slot->slot;
218 while ($currentpage < $moveselectedonpage) {
219 $newslotorder[$index] = 0;
223 foreach ($onpage as $slot) {
224 $newslotorder[$index] = $slot->slot;
228 foreach ($afterpage as $slot) {
229 while ($currentpage < $slot->page) {
230 $newslotorder[$index] = 0;
234 $newslotorder[$index] = $slot->slot;
239 // If ordering info was given, reorder the questions.
241 ksort($newslotorder);
244 $slotreorder = array();
245 $slotpages = array();
246 foreach ($newslotorder as $slotnumber) {
247 if ($slotnumber == 0) {
251 $slotreorder[$slotnumber] = $currentslot;
252 $slotpages[$currentslot] = $currentpage;
255 $trans = $DB->start_delegated_transaction();
256 update_field_with_unique_index('quiz_slots',
257 'slot', $slotreorder, array('quizid' => $quiz->id));
258 foreach ($slotpages as $slotnumber => $page) {
259 $DB->set_field('quiz_slots', 'page', $page, array('quizid' => $quiz->id, 'slot' => $slotnumber));
261 $trans->allow_commit();
262 $deletepreviews = true;
265 // If rescaling is required save the new maximum.
266 $maxgrade = unformat_float(optional_param('maxgrade', -1, PARAM_RAW));
267 if ($maxgrade >= 0) {
268 quiz_set_grade($maxgrade, $quiz);
271 if ($deletepreviews) {
272 quiz_delete_previews($quiz);
274 if ($recomputesummarks) {
275 quiz_update_sumgrades($quiz);
276 quiz_update_all_attempt_sumgrades($quiz);
277 quiz_update_all_final_grades($quiz);
278 quiz_update_grades($quiz, 0, true);
280 redirect($afteractionurl);
283 // Get the question bank view.
284 $questionbank = new quiz_question_bank_view($contexts, $thispageurl, $course, $cm, $quiz);
285 $questionbank->set_quiz_has_attempts($quizhasattempts);
286 $questionbank->process_actions($thispageurl, $cm);
288 // End of process commands =====================================================.
290 $PAGE->set_pagelayout('incourse');
291 $PAGE->set_pagetype('mod-quiz-edit');
293 $output = $PAGE->get_renderer('mod_quiz', 'edit');
295 $PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
296 $PAGE->set_heading($course->fullname);
297 $node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
299 $node->make_active();
301 echo $OUTPUT->header();
303 // Initialise the JavaScript.
304 $quizeditconfig = new stdClass();
305 $quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
306 $quizeditconfig->dialoglisteners = array();
307 $numberoflisteners = $DB->get_field_sql("
308 SELECT COALESCE(MAX(page), 1)
310 WHERE quizid = ?", array($quiz->id));
312 for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
313 $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
316 $PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
317 $PAGE->requires->js('/question/qengine.js');
319 // Questions wrapper start.
320 echo html_writer::start_tag('div', array('class' => 'mod-quiz-edit-content'));
322 echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
324 // Questions wrapper end.
325 echo html_writer::end_tag('div');
327 echo $OUTPUT->footer();