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