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
43 use mod_quiz\output\edit_nav_actions
;
44 use mod_quiz\quiz_settings
;
46 require_once(__DIR__
. '/../../config.php');
47 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
48 require_once($CFG->dirroot
. '/question/editlib.php');
50 $mdlscrollto = optional_param('mdlscrollto', '', PARAM_INT
);
52 list($thispageurl, $contexts, $cmid, $cm, $quiz, $pagevars) =
53 question_edit_setup('editq', '/mod/quiz/edit.php', true);
55 $PAGE->set_url($thispageurl);
56 $PAGE->set_secondary_active_tab("mod_quiz_edit");
58 // You need mod/quiz:manage in addition to question capabilities to access this page.
59 require_capability('mod/quiz:manage', $contexts->lowest());
61 // Get the course object and related bits.
62 $course = get_course($quiz->course
);
63 $quizobj = new quiz_settings($quiz, $cm, $course);
64 $structure = $quizobj->get_structure();
65 $gradecalculator = $quizobj->get_grade_calculator();
67 $defaultcategoryobj = question_make_default_categories($contexts->all());
68 $defaultcategory = $defaultcategoryobj->id
. ',' . $defaultcategoryobj->contextid
;
70 $quizhasattempts = quiz_has_attempts($quiz->id
);
72 // Process commands ============================================================.
74 // Get the list of question ids had their check-boxes ticked.
76 $params = (array) data_submitted();
77 foreach ($params as $key => $value) {
78 if (preg_match('!^s([0-9]+)$!', $key, $matches)) {
79 $selectedslots[] = $matches[1];
83 $afteractionurl = new moodle_url($thispageurl);
85 if (optional_param('repaginate', false, PARAM_BOOL
) && confirm_sesskey()) {
86 // Re-paginate the quiz.
87 $structure->check_can_be_edited();
88 $questionsperpage = optional_param('questionsperpage', $quiz->questionsperpage
, PARAM_INT
);
89 quiz_repaginate_questions($quiz->id
, $questionsperpage );
90 quiz_delete_previews($quiz);
91 redirect($afteractionurl);
95 $afteractionurl->param('mdlscrollto', $mdlscrollto);
98 if (($addquestion = optional_param('addquestion', 0, PARAM_INT
)) && confirm_sesskey()) {
99 // Add a single question to the current quiz.
100 $structure->check_can_be_edited();
101 quiz_require_question_use($addquestion);
102 $addonpage = optional_param('addonpage', 0, PARAM_INT
);
103 quiz_add_quiz_question($addquestion, $quiz, $addonpage);
104 quiz_delete_previews($quiz);
105 $gradecalculator->recompute_quiz_sumgrades();
106 $thispageurl->param('lastchanged', $addquestion);
107 redirect($afteractionurl);
110 if (optional_param('add', false, PARAM_BOOL
) && confirm_sesskey()) {
111 $structure->check_can_be_edited();
112 $addonpage = optional_param('addonpage', 0, PARAM_INT
);
113 // Add selected questions to the current quiz.
114 $rawdata = (array) data_submitted();
115 foreach ($rawdata as $key => $value) { // Parse input for question ids.
116 if (preg_match('!^q([0-9]+)$!', $key, $matches)) {
118 quiz_require_question_use($key);
119 quiz_add_quiz_question($key, $quiz, $addonpage);
122 quiz_delete_previews($quiz);
123 $gradecalculator->recompute_quiz_sumgrades();
124 redirect($afteractionurl);
127 if ($addsectionatpage = optional_param('addsectionatpage', false, PARAM_INT
)) {
128 // Add a section to the quiz.
129 $structure->check_can_be_edited();
130 $structure->add_section_heading($addsectionatpage);
131 quiz_delete_previews($quiz);
132 redirect($afteractionurl);
135 if ((optional_param('addrandom', false, PARAM_BOOL
)) && confirm_sesskey()) {
136 // Add random questions to the quiz.
137 $structure->check_can_be_edited();
138 $recurse = optional_param('recurse', 0, PARAM_BOOL
);
139 $addonpage = optional_param('addonpage', 0, PARAM_INT
);
140 $categoryid = required_param('categoryid', PARAM_INT
);
141 $randomcount = required_param('randomcount', PARAM_INT
);
142 quiz_add_random_questions($quiz, $addonpage, $categoryid, $randomcount, $recurse);
144 quiz_delete_previews($quiz);
145 $gradecalculator->recompute_quiz_sumgrades();
146 redirect($afteractionurl);
149 if (optional_param('savechanges', false, PARAM_BOOL
) && confirm_sesskey()) {
151 // If rescaling is required save the new maximum.
152 $maxgrade = unformat_float(optional_param('maxgrade', '', PARAM_RAW_TRIMMED
), true);
153 if (is_float($maxgrade) && $maxgrade >= 0) {
154 $gradecalculator->update_quiz_maximum_grade($maxgrade);
157 redirect($afteractionurl);
161 $event = \mod_quiz\event\edit_page_viewed
::create([
162 'courseid' => $course->id
,
163 'context' => $contexts->lowest(),
165 'quizid' => $quiz->id
170 // End of process commands =====================================================.
172 $PAGE->set_pagelayout('incourse');
173 $PAGE->set_pagetype('mod-quiz-edit');
175 $output = $PAGE->get_renderer('mod_quiz', 'edit');
177 $PAGE->set_title(get_string('editingquizx', 'quiz', format_string($quiz->name
)));
178 $PAGE->set_heading($course->fullname
);
179 $PAGE->activityheader
->disable();
180 $node = $PAGE->settingsnav
->find('mod_quiz_edit', navigation_node
::TYPE_SETTING
);
182 $node->make_active();
185 // Add random question - result message.
186 if ($message = optional_param('message', '', PARAM_TEXT
)) {
187 core\notification
::add($message, core\notification
::SUCCESS
);
190 $tertiarynav = new edit_nav_actions($cmid, edit_nav_actions
::SUMMARY
);
193 echo $output->header();
194 echo $output->render($tertiarynav);
196 // Initialise the JavaScript.
197 $quizeditconfig = new stdClass();
198 $quizeditconfig->url
= $thispageurl->out(true, ['qbanktool' => '0']);
199 $quizeditconfig->dialoglisteners
= [];
200 $numberoflisteners = $DB->get_field_sql("
201 SELECT COALESCE(MAX(page), 1)
203 WHERE quizid = ?", [$quiz->id
]);
205 for ($pageiter = 1; $pageiter <= $numberoflisteners; $pageiter++
) {
206 $quizeditconfig->dialoglisteners
[] = 'addrandomdialoglaunch_' . $pageiter;
209 $PAGE->requires
->data_for_js('quiz_edit_config', $quizeditconfig);
210 $PAGE->requires
->js_call_amd('core_question/question_engine');
212 // Questions wrapper start.
213 echo html_writer
::start_tag('div', ['class' => 'mod-quiz-edit-content']);
215 echo $output->edit_page($quizobj, $structure, $contexts, $thispageurl, $pagevars);
217 // Questions wrapper end.
218 echo html_writer
::end_tag('div');
220 echo $output->footer();