MDL-61107 quiz editing: handle invalid Maximum grade input
[moodle.git] / mod / quiz / edit.php
CommitLineData
83192608 1<?php
f9b0500f
TH
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
f4b879dd 17
ee1fb969 18/**
7ca4613b 19 * Page to edit quizzes
20 *
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
27 *
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
37 *
01773a6d 38 * @package mod_quiz
ba643847
TH
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
f9b0500f
TH
41 */
42
f4b879dd 43
16fa272e
TH
44require_once(__DIR__ . '/../../config.php');
45require_once($CFG->dirroot . '/mod/quiz/locallib.php');
94dbfb3a 46require_once($CFG->dirroot . '/mod/quiz/addrandomform.php');
16fa272e 47require_once($CFG->dirroot . '/question/editlib.php');
324d6576 48require_once($CFG->dirroot . '/question/category_class.php');
fa583f5f 49
9e83f3d1
TH
50// These params are only passed from page request to request while we stay on
51// this page otherwise they would go in question_edit_setup.
fd214b59 52$scrollpos = optional_param('scrollpos', '', PARAM_INT);
56ed242b 53
fa583f5f 54list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
453b28d8 55 question_edit_setup('editq', '/mod/quiz/edit.php', true);
fa583f5f 56
57$defaultcategoryobj = question_make_default_categories($contexts->all());
94dbfb3a 58$defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid;
324d6576 59
3e10e429 60$quizhasattempts = quiz_has_attempts($quiz->id);
fa583f5f 61
eb02301a 62$PAGE->set_url($thispageurl);
fa583f5f 63
fa583f5f 64// Get the course object and related bits.
e1a2d0d9
CC
65$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
66$quizobj = new quiz($quiz, $cm, $course);
67$structure = $quizobj->get_structure();
971c6fca 68
324d6576 69// You need mod/quiz:manage in addition to question capabilities to access this page.
fa583f5f 70require_capability('mod/quiz:manage', $contexts->lowest());
7bd1aa1d 71
5e8f7365
MN
72// Log this visit.
73$params = array(
74 'courseid' => $course->id,
75 'context' => $contexts->lowest(),
76 'other' => array(
77 'quizid' => $quiz->id
78 )
79);
80$event = \mod_quiz\event\edit_page_viewed::create($params);
81$event->trigger();
82
e1a2d0d9 83// Process commands ============================================================.
4299df1d 84
85// Get the list of question ids had their check-boxes ticked.
ccba5b88 86$selectedslots = array();
4299df1d 87$params = (array) data_submitted();
88foreach ($params as $key => $value) {
89 if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
ccba5b88 90 $selectedslots[] = $matches[1];
6a952ce7 91 }
fa583f5f 92}
93
fd214b59
TH
94$afteractionurl = new moodle_url($thispageurl);
95if ($scrollpos) {
96 $afteractionurl->param('scrollpos', $scrollpos);
97}
4299df1d 98
99if (optional_param('repaginate', false, PARAM_BOOL) && confirm_sesskey()) {
9e83f3d1 100 // Re-paginate the quiz.
6870a6b8 101 $structure->check_can_be_edited();
4299df1d 102 $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage, PARAM_INT);
ccba5b88 103 quiz_repaginate_questions($quiz->id, $questionsperpage );
f9b0500f 104 quiz_delete_previews($quiz);
fd214b59 105 redirect($afteractionurl);
fa583f5f 106}
324d6576 107
108if (($addquestion = optional_param('addquestion', 0, PARAM_INT)) && confirm_sesskey()) {
9e83f3d1 109 // Add a single question to the current quiz.
6870a6b8 110 $structure->check_can_be_edited();
76cf77e4 111 quiz_require_question_use($addquestion);
324d6576 112 $addonpage = optional_param('addonpage', 0, PARAM_INT);
fa583f5f 113 quiz_add_quiz_question($addquestion, $quiz, $addonpage);
4299df1d 114 quiz_delete_previews($quiz);
f9b0500f 115 quiz_update_sumgrades($quiz);
455d765f 116 $thispageurl->param('lastchanged', $addquestion);
fd214b59 117 redirect($afteractionurl);
fa583f5f 118}
119
324d6576 120if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
6870a6b8 121 $structure->check_can_be_edited();
e1a2d0d9 122 $addonpage = optional_param('addonpage', 0, PARAM_INT);
9e83f3d1 123 // Add selected questions to the current quiz.
fa583f5f 124 $rawdata = (array) data_submitted();
9e83f3d1 125 foreach ($rawdata as $key => $value) { // Parse input for question ids.
fa583f5f 126 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
127 $key = $matches[1];
76cf77e4 128 quiz_require_question_use($key);
e1a2d0d9 129 quiz_add_quiz_question($key, $quiz, $addonpage);
fef9b51d 130 }
fef9b51d 131 }
4299df1d 132 quiz_delete_previews($quiz);
f9b0500f 133 quiz_update_sumgrades($quiz);
fd214b59 134 redirect($afteractionurl);
fa583f5f 135}
136
32498c28 137if ($addsectionatpage = optional_param('addsectionatpage', false, PARAM_INT)) {
5d949702 138 // Add a section to the quiz.
32498c28
K
139 $structure->check_can_be_edited();
140 $structure->add_section_heading($addsectionatpage);
5d949702
K
141 quiz_delete_previews($quiz);
142 redirect($afteractionurl);
143}
144
94dbfb3a 145if ((optional_param('addrandom', false, PARAM_BOOL)) && confirm_sesskey()) {
9e83f3d1 146 // Add random questions to the quiz.
6870a6b8 147 $structure->check_can_be_edited();
fa583f5f 148 $recurse = optional_param('recurse', 0, PARAM_BOOL);
324d6576 149 $addonpage = optional_param('addonpage', 0, PARAM_INT);
94dbfb3a
TH
150 $categoryid = required_param('categoryid', PARAM_INT);
151 $randomcount = required_param('randomcount', PARAM_INT);
152 quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
6a952ce7 153
4299df1d 154 quiz_delete_previews($quiz);
f9b0500f 155 quiz_update_sumgrades($quiz);
fd214b59 156 redirect($afteractionurl);
fa583f5f 157}
324d6576 158
324d6576 159if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
3e10e429 160
9e83f3d1 161 // If rescaling is required save the new maximum.
d2e1aa76
TH
162 $maxgrade = unformat_float(optional_param('maxgrade', '', PARAM_RAW_TRIMMED), true);
163 if (is_float($maxgrade) && $maxgrade >= 0) {
3e10e429 164 quiz_set_grade($maxgrade, $quiz);
f9b0500f
TH
165 quiz_update_all_final_grades($quiz);
166 quiz_update_grades($quiz, 0, true);
167 }
080839c4 168
fd214b59 169 redirect($afteractionurl);
fa583f5f 170}
6551bb89 171
e1a2d0d9 172// Get the question bank view.
8aa5e0cf 173$questionbank = new mod_quiz\question\bank\custom_view($contexts, $thispageurl, $course, $cm, $quiz);
e1a2d0d9 174$questionbank->set_quiz_has_attempts($quizhasattempts);
f4b879dd 175$questionbank->process_actions($thispageurl, $cm);
b37cacae 176
e1a2d0d9
CC
177// End of process commands =====================================================.
178
179$PAGE->set_pagelayout('incourse');
180$PAGE->set_pagetype('mod-quiz-edit');
181
182$output = $PAGE->get_renderer('mod_quiz', 'edit');
fa583f5f 183
4c7f377d 184$PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name)));
56ed242b 185$PAGE->set_heading($course->fullname);
eb02301a
TH
186$node = $PAGE->settingsnav->find('mod_quiz_edit', navigation_node::TYPE_SETTING);
187if ($node) {
188 $node->make_active();
189}
03da0c39 190echo $OUTPUT->header();
6f030b0f 191
1372b5d2 192// Initialise the JavaScript.
0ff4bd08 193$quizeditconfig = new stdClass();
b9bc2019 194$quizeditconfig->url = $thispageurl->out(true, array('qbanktool' => '0'));
3e10e429 195$quizeditconfig->dialoglisteners = array();
ccba5b88
TH
196$numberoflisteners = $DB->get_field_sql("
197 SELECT COALESCE(MAX(page), 1)
198 FROM {quiz_slots}
199 WHERE quizid = ?", array($quiz->id));
200
1372b5d2 201for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++) {
202 $quizeditconfig->dialoglisteners[] = 'addrandomdialoglaunch_' . $pageiter;
203}
e1a2d0d9 204
cf615522 205$PAGE->requires->data_for_js('quiz_edit_config', $quizeditconfig);
fd214b59 206$PAGE->requires->js('/question/qengine.js');
16cad79b 207
e1a2d0d9
CC
208// Questions wrapper start.
209echo html_writer::start_tag('div', array('class' => 'mod-quiz-edit-content'));
fa583f5f 210
e1a2d0d9 211echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
fa583f5f 212
e1a2d0d9
CC
213// Questions wrapper end.
214echo html_writer::end_tag('div');
56ed242b 215
867847e3 216echo $OUTPUT->footer();