MDL-61138 question: add question exporters
[moodle.git] / question / question.php
CommitLineData
aeb15530 1<?php
d3603157
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
16750bcd 17/**
d3603157 18 * Page for editing questions.
16750bcd 19 *
d3603157
TH
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
87391656 26
1fcf0ca8
RS
27require_once(__DIR__ . '/../config.php');
28require_once(__DIR__ . '/editlib.php');
add2d7ac 29require_once($CFG->libdir . '/filelib.php');
30require_once($CFG->libdir . '/formslib.php');
31
add2d7ac 32// Read URL parameters telling us which question to edit.
33$id = optional_param('id', 0, PARAM_INT); // question id
8011be18 34$makecopy = optional_param('makecopy', 0, PARAM_BOOL);
81f43bc6 35$qtype = optional_param('qtype', '', PARAM_COMPONENT);
add2d7ac 36$categoryid = optional_param('category', 0, PARAM_INT);
271e6dec 37$cmid = optional_param('cmid', 0, PARAM_INT);
38$courseid = optional_param('courseid', 0, PARAM_INT);
24e8b9b6 39$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
90c7912e 40$originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
fa583f5f 41$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
79bb7202 42$inpopup = optional_param('inpopup', 0, PARAM_BOOL);
a13d4fbd 43$scrollpos = optional_param('scrollpos', 0, PARAM_INT);
79bb7202 44
a6855934 45$url = new moodle_url('/question/question.php');
b0f4e4e4 46if ($id !== 0) {
47 $url->param('id', $id);
48}
8011be18 49if ($makecopy) {
e0c41591
K
50 $url->param('makecopy', $makecopy);
51}
b0f4e4e4 52if ($qtype !== '') {
53 $url->param('qtype', $qtype);
54}
55if ($categoryid !== 0) {
56 $url->param('category', $categoryid);
57}
58if ($cmid !== 0) {
59 $url->param('cmid', $cmid);
60}
61if ($courseid !== 0) {
62 $url->param('courseid', $courseid);
63}
64if ($wizardnow !== '') {
65 $url->param('wizardnow', $wizardnow);
66}
90c7912e
TH
67if ($originalreturnurl !== 0) {
68 $url->param('returnurl', $originalreturnurl);
b0f4e4e4 69}
70if ($appendqnumstring !== '') {
71 $url->param('appendqnumstring', $appendqnumstring);
72}
73if ($inpopup !== 0) {
74 $url->param('inpopup', $inpopup);
75}
a13d4fbd
TH
76if ($scrollpos) {
77 $url->param('scrollpos', $scrollpos);
78}
b0f4e4e4 79$PAGE->set_url($url);
80
04bf7ac8
TH
81if ($cmid) {
82 $questionbankurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
83} else {
84 $questionbankurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
85}
86navigation_node::override_active_url($questionbankurl);
87
fb6dcdab
TH
88if ($originalreturnurl) {
89 if (strpos($originalreturnurl, '/') !== 0) {
90 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
91 }
92 $returnurl = new moodle_url($originalreturnurl);
fb6dcdab 93} else {
04bf7ac8 94 $returnurl = $questionbankurl;
90c7912e 95}
a13d4fbd
TH
96if ($scrollpos) {
97 $returnurl->param('scrollpos', $scrollpos);
98}
90c7912e 99
9ab75b2b 100if ($cmid){
271e6dec 101 list($module, $cm) = get_module_from_cmid($cmid);
102 require_login($cm->course, false, $cm);
21c08c63 103 $thiscontext = context_module::instance($cmid);
271e6dec 104} elseif ($courseid) {
105 require_login($courseid, false);
21c08c63 106 $thiscontext = context_course::instance($courseid);
9ab75b2b 107 $module = null;
108 $cm = null;
271e6dec 109} else {
0be2c858 110 print_error('missingcourseorcmid', 'question');
9ab75b2b 111}
271e6dec 112$contexts = new question_edit_contexts($thiscontext);
4bf1be35 113$PAGE->set_pagelayout('admin');
271e6dec 114
cd120b23 115if (optional_param('addcancel', false, PARAM_BOOL)) {
116 redirect($returnurl);
117}
118
add2d7ac 119if ($id) {
f34488b2 120 if (!$question = $DB->get_record('question', array('id' => $id))) {
add2d7ac 121 print_error('questiondoesnotexist', 'question', $returnurl);
122 }
081eb156
RW
123 // We can use $COURSE here because it's been initialised as part of the
124 // require_login above. Passing it as the third parameter tells the function
125 // to filter the course tags by that course.
126 get_question_options($question, true, [$COURSE]);
a27aa5c6 127
add2d7ac 128} else if ($categoryid && $qtype) { // only for creating new questions
0ff4bd08 129 $question = new stdClass();
add2d7ac 130 $question->category = $categoryid;
131 $question->qtype = $qtype;
51c5e605 132 $question->createdby = $USER->id;
8ed358db 133
134 // Check that users are allowed to create this question type at the moment.
06f8ed54 135 if (!question_bank::qtype_enabled($qtype)) {
8ed358db 136 print_error('cannotenable', 'question', $returnurl, $qtype);
137 }
a27aa5c6
TH
138
139} else if ($categoryid) {
140 // Category, but no qtype. They probably came from the addquestion.php
fe6ce234 141 // script without choosing a question type. Send them back.
a27aa5c6
TH
142 $addurl = new moodle_url('/question/addquestion.php', $url->params());
143 $addurl->param('validationerror', 1);
144 redirect($addurl);
145
add2d7ac 146} else {
147 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
148}
149
06f8ed54
TH
150$qtypeobj = question_bank::get_qtype($question->qtype);
151
081eb156
RW
152if (isset($question->categoryobject)) {
153 $category = $question->categoryobject;
154} else {
155 // Validate the question category.
156 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
157 print_error('categorydoesnotexist', 'question', $returnurl);
158 }
add2d7ac 159}
271e6dec 160
677f62b6 161// Check permissions
0ff4bd08 162$question->formoptions = new stdClass();
271e6dec 163
d197ea43 164$categorycontext = context::instance_by_id($category->contextid);
2cf7bde8 165$question->contextid = $category->contextid;
271e6dec 166$addpermission = has_capability('moodle/question:add', $categorycontext);
167
168if ($id) {
435d3279
K
169 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
170 $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
171 $question->formoptions->cansaveasnew = $addpermission &&
172 (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
173 $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
174 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
175 if (!$formeditable) {
176 question_require_capability_on($question, 'view');
177 }
178 if ($makecopy) {
179 // If we are duplicating a question, add some indication to the question name.
180 $question->name = get_string('questionnamecopy', 'question', $question->name);
8011be18 181 $question->beingcopied = true;
271e6dec 182 }
183
271e6dec 184} else { // creating a new question
fe6ce234
DC
185 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
186 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
51c5e605 187 $question->formoptions->cansaveasnew = false;
271e6dec 188 $question->formoptions->repeatelements = true;
51c5e605
TH
189 $formeditable = true;
190 require_capability('moodle/question:add', $categorycontext);
add2d7ac 191}
76cf77e4 192$question->formoptions->mustbeusable = (bool) $appendqnumstring;
add2d7ac 193
194// Validate the question type.
d529807a 195$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 196
add2d7ac 197// Create the question editing form.
435d3279 198if ($wizardnow !== '') {
06f8ed54 199 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 200} else {
06f8ed54 201 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 202}
ba18b21d 203$toform = fullclone($question); // send the question object and a few more parameters to the form
f4fe3968 204$toform->category = "{$category->id},{$category->contextid}";
a13d4fbd 205$toform->scrollpos = $scrollpos;
ba18b21d 206if ($formeditable && $id){
207 $toform->categorymoveto = $toform->category;
208}
fa583f5f 209
210$toform->appendqnumstring = $appendqnumstring;
90c7912e 211$toform->returnurl = $originalreturnurl;
e0c41591 212$toform->makecopy = $makecopy;
9ab75b2b 213if ($cm !== null){
214 $toform->cmid = $cm->id;
271e6dec 215 $toform->courseid = $cm->course;
216} else {
217 $toform->courseid = $COURSE->id;
9ab75b2b 218}
fe6ce234 219
11a60967 220$toform->inpopup = $inpopup;
fe6ce234 221
add2d7ac 222$mform->set_data($toform);
223
06f8ed54 224if ($mform->is_cancelled()) {
11a60967 225 if ($inpopup) {
226 close_window();
227 } else {
a13d4fbd 228 redirect($returnurl);
11a60967 229 }
06f8ed54 230
5d548d3e 231} else if ($fromform = $mform->get_data()) {
e0c41591
K
232 // If we are saving as a copy, break the connection to the old question.
233 if ($makecopy) {
24e8b9b6 234 $question->id = 0;
e0c41591 235 $question->hidden = 0; // Copies should not be hidden.
add2d7ac 236 }
24e8b9b6 237
238 /// Process the combination of usecurrentcat, categorymoveto and category form
239 /// fields, so the save_question method only has to consider $fromform->category
240 if (!empty($fromform->usecurrentcat)) {
241 // $fromform->category is the right category to save in.
242 } else {
243 if (!empty($fromform->categorymoveto)) {
244 $fromform->category = $fromform->categorymoveto;
245 } else {
246 // $fromform->category is the right category to save in.
247 }
248 }
249
250 /// If we are moving a question, check we have permission to move it from
251 /// whence it came. (Where we are moving to is validated by the form.)
51c5e605 252 list($newcatid, $newcontextid) = explode(',', $fromform->category);
24e8b9b6 253 if (!empty($question->id) && $newcatid != $question->category) {
cc033d48 254 $contextid = $newcontextid;
24e8b9b6 255 question_require_capability_on($question, 'move');
cc033d48
MN
256 } else {
257 $contextid = $category->contextid;
24e8b9b6 258 }
259
5d548d3e 260 // Ensure we redirect back to the category the question is being saved into.
24e8b9b6 261 $returnurl->param('category', $fromform->category);
24e8b9b6 262
435d3279
K
263 // We are acutally saving the question.
264 if (!empty($question->id)) {
265 question_require_capability_on($question, 'edit');
5d548d3e 266 } else {
cc033d48 267 require_capability('moodle/question:add', context::instance_by_id($contextid));
435d3279
K
268 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
269 print_error('nopermissions', '', '', 'edit');
5d548d3e 270 }
c599a682 271 }
081eb156 272
435d3279 273 $question = $qtypeobj->save_question($question, $fromform);
b355a1c9 274 if (isset($fromform->tags)) {
081eb156
RW
275 // If we have any question context level tags then set those tags now.
276 core_tag_tag::set_item_tags('core_question', 'question', $question->id,
277 context::instance_by_id($contextid), $fromform->tags, 0);
278 }
279
280 if (isset($fromform->coursetags)) {
281 // If we have and course context level tags then set those now.
b355a1c9 282 core_tag_tag::set_item_tags('core_question', 'question', $question->id,
081eb156 283 context_course::instance($fromform->courseid), $fromform->coursetags, 0);
435d3279 284 }
c599a682 285
a560d636
TH
286 // Purge this question from the cache.
287 question_bank::notify_question_edited($question->id);
288
7756e2bc
K
289 // If we are saving and continuing to edit the question.
290 if (!empty($fromform->updatebutton)) {
8011be18
TH
291 $url->param('id', $question->id);
292 $url->remove_params('makecopy');
7756e2bc
K
293 redirect($url);
294 }
295
435d3279 296 if ($qtypeobj->finished_edit_wizard($fromform)) {
79bb7202 297 if ($inpopup) {
fef8f84e 298 echo $OUTPUT->notification(get_string('changessaved'), '');
add2d7ac 299 close_window(3);
516cf3eb 300 } else {
fb6dcdab
TH
301 $returnurl->param('lastchanged', $question->id);
302 if ($appendqnumstring) {
303 $returnurl->param($appendqnumstring, $question->id);
304 $returnurl->param('sesskey', sesskey());
305 $returnurl->param('cmid', $cmid);
fa583f5f 306 }
fb6dcdab 307 redirect($returnurl);
516cf3eb 308 }
5d548d3e 309
add2d7ac 310 } else {
fb6dcdab
TH
311 $nexturlparams = array(
312 'returnurl' => $originalreturnurl,
a13d4fbd
TH
313 'appendqnumstring' => $appendqnumstring,
314 'scrollpos' => $scrollpos);
79bb7202 315 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
fb6dcdab
TH
316 //useful for passing data to the next page which is not saved in the database.
317 $nexturlparams += $fromform->nextpageparam;
add2d7ac 318 }
79bb7202 319 $nexturlparams['id'] = $question->id;
ba18b21d 320 $nexturlparams['wizardnow'] = $fromform->wizard;
fb6dcdab 321 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
79bb7202 322 if ($cmid){
323 $nexturl->param('cmid', $cmid);
324 } else {
325 $nexturl->param('courseid', $COURSE->id);
326 }
7a567a92 327 redirect($nexturl);
516cf3eb 328 }
add2d7ac 329
add2d7ac 330}
50b5dde8
TH
331
332$streditingquestion = $qtypeobj->get_heading();
333$PAGE->set_title($streditingquestion);
334$PAGE->set_heading($COURSE->fullname);
335$PAGE->navbar->add($streditingquestion);
336
337// Display a heading, question editing form and possibly some extra content needed for
338// for this question type.
339echo $OUTPUT->header();
340$qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
341echo $OUTPUT->footer();