weekly release 2.7dev
[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
add2d7ac 27require_once(dirname(__FILE__) . '/../config.php');
28require_once(dirname(__FILE__) . '/editlib.php');
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
e0c41591 34$makecopy = optional_param('makecopy', 0, PARAM_INT);
add2d7ac 35$qtype = optional_param('qtype', '', PARAM_FILE);
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);
40$movecontext = optional_param('movecontext', 0, PARAM_BOOL); // Switch to make
41 // question uneditable - form is displayed to edit category only
90c7912e 42$originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
fa583f5f 43$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
79bb7202 44$inpopup = optional_param('inpopup', 0, PARAM_BOOL);
a13d4fbd 45$scrollpos = optional_param('scrollpos', 0, PARAM_INT);
79bb7202 46
a6855934 47$url = new moodle_url('/question/question.php');
b0f4e4e4 48if ($id !== 0) {
49 $url->param('id', $id);
50}
e0c41591
K
51if ($makecopy !== 0) {
52 $url->param('makecopy', $makecopy);
53}
b0f4e4e4 54if ($qtype !== '') {
55 $url->param('qtype', $qtype);
56}
57if ($categoryid !== 0) {
58 $url->param('category', $categoryid);
59}
60if ($cmid !== 0) {
61 $url->param('cmid', $cmid);
62}
63if ($courseid !== 0) {
64 $url->param('courseid', $courseid);
65}
66if ($wizardnow !== '') {
67 $url->param('wizardnow', $wizardnow);
68}
69if ($movecontext !== 0) {
70 $url->param('movecontext', $movecontext);
71}
90c7912e
TH
72if ($originalreturnurl !== 0) {
73 $url->param('returnurl', $originalreturnurl);
b0f4e4e4 74}
75if ($appendqnumstring !== '') {
76 $url->param('appendqnumstring', $appendqnumstring);
77}
78if ($inpopup !== 0) {
79 $url->param('inpopup', $inpopup);
80}
a13d4fbd
TH
81if ($scrollpos) {
82 $url->param('scrollpos', $scrollpos);
83}
b0f4e4e4 84$PAGE->set_url($url);
85
fb6dcdab
TH
86if ($originalreturnurl) {
87 if (strpos($originalreturnurl, '/') !== 0) {
88 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
89 }
90 $returnurl = new moodle_url($originalreturnurl);
91} else if ($cmid) {
92 $returnurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
93} else {
94 $returnurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
90c7912e 95}
a13d4fbd
TH
96if ($scrollpos) {
97 $returnurl->param('scrollpos', $scrollpos);
98}
90c7912e 99
271e6dec 100if ($movecontext && !$id){
101 print_error('questiondoesnotexist', 'question', $returnurl);
102}
add2d7ac 103
9ab75b2b 104if ($cmid){
271e6dec 105 list($module, $cm) = get_module_from_cmid($cmid);
106 require_login($cm->course, false, $cm);
21c08c63 107 $thiscontext = context_module::instance($cmid);
271e6dec 108} elseif ($courseid) {
109 require_login($courseid, false);
21c08c63 110 $thiscontext = context_course::instance($courseid);
9ab75b2b 111 $module = null;
112 $cm = null;
271e6dec 113} else {
0be2c858 114 print_error('missingcourseorcmid', 'question');
9ab75b2b 115}
271e6dec 116$contexts = new question_edit_contexts($thiscontext);
4bf1be35 117$PAGE->set_pagelayout('admin');
271e6dec 118
cd120b23 119if (optional_param('addcancel', false, PARAM_BOOL)) {
120 redirect($returnurl);
121}
122
add2d7ac 123if ($id) {
f34488b2 124 if (!$question = $DB->get_record('question', array('id' => $id))) {
add2d7ac 125 print_error('questiondoesnotexist', 'question', $returnurl);
126 }
c599a682 127 get_question_options($question, true);
a27aa5c6 128
add2d7ac 129} else if ($categoryid && $qtype) { // only for creating new questions
0ff4bd08 130 $question = new stdClass();
add2d7ac 131 $question->category = $categoryid;
132 $question->qtype = $qtype;
51c5e605 133 $question->createdby = $USER->id;
8ed358db 134
135 // Check that users are allowed to create this question type at the moment.
06f8ed54 136 if (!question_bank::qtype_enabled($qtype)) {
8ed358db 137 print_error('cannotenable', 'question', $returnurl, $qtype);
138 }
a27aa5c6
TH
139
140} else if ($categoryid) {
141 // Category, but no qtype. They probably came from the addquestion.php
fe6ce234 142 // script without choosing a question type. Send them back.
a27aa5c6
TH
143 $addurl = new moodle_url('/question/addquestion.php', $url->params());
144 $addurl->param('validationerror', 1);
145 redirect($addurl);
146
add2d7ac 147} else {
148 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
149}
150
06f8ed54
TH
151$qtypeobj = question_bank::get_qtype($question->qtype);
152
add2d7ac 153// Validate the question category.
f34488b2 154if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
add2d7ac 155 print_error('categorydoesnotexist', 'question', $returnurl);
156}
271e6dec 157
677f62b6 158// Check permissions
0ff4bd08 159$question->formoptions = new stdClass();
271e6dec 160
d197ea43 161$categorycontext = context::instance_by_id($category->contextid);
271e6dec 162$addpermission = has_capability('moodle/question:add', $categorycontext);
163
164if ($id) {
271e6dec 165 if ($movecontext){
166 $question->formoptions->canedit = false;
167 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
168 $question->formoptions->cansaveasnew = false;
169 $question->formoptions->repeatelements = false;
170 $question->formoptions->movecontext = true;
171 $formeditable = true;
51c5e605 172 question_require_capability_on($question, 'move');
271e6dec 173 } else {
174 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
51c5e605
TH
175 $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
176 $question->formoptions->cansaveasnew = $addpermission &&
177 (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
178 $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
271e6dec 179 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
180 $question->formoptions->movecontext = false;
51c5e605 181 if (!$formeditable) {
271e6dec 182 question_require_capability_on($question, 'view');
183 }
e0c41591
K
184 if ($makecopy) {
185 // If we are duplicating a question, add some indication to the question name.
186 $question->name = get_string('questionnamecopy', 'question', $question->name);
187 }
271e6dec 188 }
189
271e6dec 190} else { // creating a new question
fe6ce234
DC
191 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
192 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
51c5e605 193 $question->formoptions->cansaveasnew = false;
271e6dec 194 $question->formoptions->repeatelements = true;
195 $question->formoptions->movecontext = false;
51c5e605
TH
196 $formeditable = true;
197 require_capability('moodle/question:add', $categorycontext);
add2d7ac 198}
76cf77e4 199$question->formoptions->mustbeusable = (bool) $appendqnumstring;
add2d7ac 200
201// Validate the question type.
d529807a 202$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 203
add2d7ac 204// Create the question editing form.
42663bb7 205if ($wizardnow !== '' && !$movecontext){
06f8ed54 206 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 207} else {
06f8ed54 208 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 209}
ba18b21d 210$toform = fullclone($question); // send the question object and a few more parameters to the form
211$toform->category = "$category->id,$category->contextid";
a13d4fbd 212$toform->scrollpos = $scrollpos;
ba18b21d 213if ($formeditable && $id){
214 $toform->categorymoveto = $toform->category;
215}
fa583f5f 216
217$toform->appendqnumstring = $appendqnumstring;
90c7912e 218$toform->returnurl = $originalreturnurl;
271e6dec 219$toform->movecontext = $movecontext;
e0c41591 220$toform->makecopy = $makecopy;
9ab75b2b 221if ($cm !== null){
222 $toform->cmid = $cm->id;
271e6dec 223 $toform->courseid = $cm->course;
224} else {
225 $toform->courseid = $COURSE->id;
9ab75b2b 226}
fe6ce234 227
11a60967 228$toform->inpopup = $inpopup;
fe6ce234 229
add2d7ac 230$mform->set_data($toform);
231
06f8ed54 232if ($mform->is_cancelled()) {
11a60967 233 if ($inpopup) {
234 close_window();
235 } else {
a13d4fbd 236 redirect($returnurl);
11a60967 237 }
06f8ed54 238
5d548d3e 239} else if ($fromform = $mform->get_data()) {
e0c41591
K
240 // If we are saving as a copy, break the connection to the old question.
241 if ($makecopy) {
24e8b9b6 242 $question->id = 0;
e0c41591 243 $question->hidden = 0; // Copies should not be hidden.
add2d7ac 244 }
24e8b9b6 245
246 /// Process the combination of usecurrentcat, categorymoveto and category form
247 /// fields, so the save_question method only has to consider $fromform->category
248 if (!empty($fromform->usecurrentcat)) {
249 // $fromform->category is the right category to save in.
250 } else {
251 if (!empty($fromform->categorymoveto)) {
252 $fromform->category = $fromform->categorymoveto;
253 } else {
254 // $fromform->category is the right category to save in.
255 }
256 }
257
258 /// If we are moving a question, check we have permission to move it from
259 /// whence it came. (Where we are moving to is validated by the form.)
51c5e605 260 list($newcatid, $newcontextid) = explode(',', $fromform->category);
24e8b9b6 261 if (!empty($question->id) && $newcatid != $question->category) {
262 question_require_capability_on($question, 'move');
263 }
264
5d548d3e 265 // Ensure we redirect back to the category the question is being saved into.
24e8b9b6 266 $returnurl->param('category', $fromform->category);
24e8b9b6 267
24e8b9b6 268 if ($movecontext) {
5d548d3e 269 // We are just moving the question to a different context.
ba18b21d 270 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
d197ea43 271 require_capability('moodle/question:add', context::instance_by_id($tocontextid));
5d548d3e 272 question_move_questions_to_category(array($question->id), $tocatid);
ba18b21d 273
5d548d3e
TH
274 } else {
275 // We are acutally saving the question.
51c5e605
TH
276 if (!empty($question->id)) {
277 question_require_capability_on($question, 'edit');
278 } else {
d197ea43 279 require_capability('moodle/question:add', context::instance_by_id($newcontextid));
51c5e605
TH
280 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
281 print_error('nopermissions', '', '', 'edit');
282 }
283 }
06f8ed54 284 $question = $qtypeobj->save_question($question, $fromform);
5d548d3e
TH
285 if (!empty($CFG->usetags) && isset($fromform->tags)) {
286 // A wizardpage from multipe pages questiontype like calculated may not
287 // allow editing the question tags, hence the isset($fromform->tags) test.
288 require_once($CFG->dirroot.'/tag/lib.php');
289 tag_set('question', $question->id, $fromform->tags);
290 }
c599a682 291 }
292
a560d636
TH
293 // Purge this question from the cache.
294 question_bank::notify_question_edited($question->id);
295
06f8ed54 296 if (($qtypeobj->finished_edit_wizard($fromform)) || $movecontext) {
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
5d548d3e 330} else {
06f8ed54 331 $streditingquestion = $qtypeobj->get_heading();
d412354c 332 $PAGE->set_title($streditingquestion);
0f56fde2 333 $PAGE->set_heading($COURSE->fullname);
9ab75b2b 334 if ($cm !== null) {
479cca8a 335 $strmodule = get_string('modulename', $cm->modname);
479cca8a 336 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
a6855934
PS
337 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
338 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
479cca8a 339 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
340 //don't need this link if returnurl returns to view.php
d412354c 341 $PAGE->navbar->add($streditingmodule, $returnurl);
479cca8a 342 }
d412354c 343 $PAGE->navbar->add($streditingquestion);
d412354c 344 echo $OUTPUT->header();
271e6dec 345
8c2550c8 346 } else {
5e8a85aa
TH
347 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
348 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
d412354c 349 $PAGE->navbar->add($streditingquestion);
350 echo $OUTPUT->header();
8c2550c8 351 }
516cf3eb 352
add2d7ac 353 // Display a heading, question editing form and possibly some extra content needed for
354 // for this question type.
06f8ed54 355 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
9b59580b 356 echo $OUTPUT->footer();
add2d7ac 357}