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