MDL-26969 assignment : clarified phpdoc and fixed whitespace.
[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);
106 $thiscontext = get_context_instance(CONTEXT_COURSE, $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 }
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;
51c5e605 129 $question->createdby = $USER->id;
8ed358db 130
131 // Check that users are allowed to create this question type at the moment.
06f8ed54 132 if (!question_bank::qtype_enabled($qtype)) {
8ed358db 133 print_error('cannotenable', 'question', $returnurl, $qtype);
134 }
a27aa5c6
TH
135
136} else if ($categoryid) {
137 // Category, but no qtype. They probably came from the addquestion.php
fe6ce234 138 // script without choosing a question type. Send them back.
a27aa5c6
TH
139 $addurl = new moodle_url('/question/addquestion.php', $url->params());
140 $addurl->param('validationerror', 1);
141 redirect($addurl);
142
add2d7ac 143} else {
144 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
145}
146
06f8ed54
TH
147$qtypeobj = question_bank::get_qtype($question->qtype);
148
add2d7ac 149// Validate the question category.
f34488b2 150if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
add2d7ac 151 print_error('categorydoesnotexist', 'question', $returnurl);
152}
271e6dec 153
677f62b6 154// Check permissions
0ff4bd08 155$question->formoptions = new stdClass();
271e6dec 156
157$categorycontext = get_context_instance_by_id($category->contextid);
158$addpermission = has_capability('moodle/question:add', $categorycontext);
159
160if ($id) {
271e6dec 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;
51c5e605 168 question_require_capability_on($question, 'move');
271e6dec 169 } else {
170 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
51c5e605
TH
171 $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
172 $question->formoptions->cansaveasnew = $addpermission &&
173 (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
174 $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
271e6dec 175 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
176 $question->formoptions->movecontext = false;
51c5e605 177 if (!$formeditable) {
271e6dec 178 question_require_capability_on($question, 'view');
179 }
180 }
181
271e6dec 182} else { // creating a new question
fe6ce234
DC
183 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
184 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
51c5e605 185 $question->formoptions->cansaveasnew = false;
271e6dec 186 $question->formoptions->repeatelements = true;
187 $question->formoptions->movecontext = false;
51c5e605
TH
188 $formeditable = true;
189 require_capability('moodle/question:add', $categorycontext);
add2d7ac 190}
76cf77e4 191$question->formoptions->mustbeusable = (bool) $appendqnumstring;
add2d7ac 192
193// Validate the question type.
d529807a 194$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 195
add2d7ac 196// Create the question editing form.
42663bb7 197if ($wizardnow !== '' && !$movecontext){
06f8ed54 198 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 199} else {
06f8ed54 200 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 201}
ba18b21d 202$toform = fullclone($question); // send the question object and a few more parameters to the form
203$toform->category = "$category->id,$category->contextid";
a13d4fbd 204$toform->scrollpos = $scrollpos;
ba18b21d 205if ($formeditable && $id){
206 $toform->categorymoveto = $toform->category;
207}
fa583f5f 208
209$toform->appendqnumstring = $appendqnumstring;
90c7912e 210$toform->returnurl = $originalreturnurl;
271e6dec 211$toform->movecontext = $movecontext;
9ab75b2b 212if ($cm !== null){
213 $toform->cmid = $cm->id;
271e6dec 214 $toform->courseid = $cm->course;
215} else {
216 $toform->courseid = $COURSE->id;
9ab75b2b 217}
fe6ce234 218
11a60967 219$toform->inpopup = $inpopup;
fe6ce234 220
add2d7ac 221$mform->set_data($toform);
222
06f8ed54 223if ($mform->is_cancelled()) {
11a60967 224 if ($inpopup) {
225 close_window();
226 } else {
a13d4fbd 227 redirect($returnurl);
11a60967 228 }
06f8ed54 229
5d548d3e 230} else if ($fromform = $mform->get_data()) {
24e8b9b6 231 /// If we are saving as a copy, break the connection to the old question.
ba18b21d 232 if (!empty($fromform->makecopy)) {
24e8b9b6 233 $question->id = 0;
add2d7ac 234 $question->hidden = 0; // Copies should not be hidden
235 }
24e8b9b6 236
237 /// Process the combination of usecurrentcat, categorymoveto and category form
238 /// fields, so the save_question method only has to consider $fromform->category
239 if (!empty($fromform->usecurrentcat)) {
240 // $fromform->category is the right category to save in.
241 } else {
242 if (!empty($fromform->categorymoveto)) {
243 $fromform->category = $fromform->categorymoveto;
244 } else {
245 // $fromform->category is the right category to save in.
246 }
247 }
248
249 /// If we are moving a question, check we have permission to move it from
250 /// whence it came. (Where we are moving to is validated by the form.)
51c5e605 251 list($newcatid, $newcontextid) = explode(',', $fromform->category);
24e8b9b6 252 if (!empty($question->id) && $newcatid != $question->category) {
253 question_require_capability_on($question, 'move');
254 }
255
5d548d3e 256 // Ensure we redirect back to the category the question is being saved into.
24e8b9b6 257 $returnurl->param('category', $fromform->category);
24e8b9b6 258
24e8b9b6 259 if ($movecontext) {
5d548d3e 260 // We are just moving the question to a different context.
ba18b21d 261 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
5d548d3e
TH
262 require_capability('moodle/question:add', get_context_instance_by_id($tocontextid));
263 question_move_questions_to_category(array($question->id), $tocatid);
ba18b21d 264
5d548d3e
TH
265 } else {
266 // We are acutally saving the question.
51c5e605
TH
267 if (!empty($question->id)) {
268 question_require_capability_on($question, 'edit');
269 } else {
270 require_capability('moodle/question:add', get_context_instance_by_id($newcontextid));
271 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
272 print_error('nopermissions', '', '', 'edit');
273 }
274 }
06f8ed54 275 $question = $qtypeobj->save_question($question, $fromform);
5d548d3e
TH
276 if (!empty($CFG->usetags) && isset($fromform->tags)) {
277 // A wizardpage from multipe pages questiontype like calculated may not
278 // allow editing the question tags, hence the isset($fromform->tags) test.
279 require_once($CFG->dirroot.'/tag/lib.php');
280 tag_set('question', $question->id, $fromform->tags);
281 }
c599a682 282 }
283
06f8ed54 284 if (($qtypeobj->finished_edit_wizard($fromform)) || $movecontext) {
79bb7202 285 if ($inpopup) {
fef8f84e 286 echo $OUTPUT->notification(get_string('changessaved'), '');
add2d7ac 287 close_window(3);
516cf3eb 288 } else {
fb6dcdab
TH
289 $returnurl->param('lastchanged', $question->id);
290 if ($appendqnumstring) {
291 $returnurl->param($appendqnumstring, $question->id);
292 $returnurl->param('sesskey', sesskey());
293 $returnurl->param('cmid', $cmid);
fa583f5f 294 }
fb6dcdab 295 redirect($returnurl);
516cf3eb 296 }
5d548d3e 297
add2d7ac 298 } else {
fb6dcdab
TH
299 $nexturlparams = array(
300 'returnurl' => $originalreturnurl,
a13d4fbd
TH
301 'appendqnumstring' => $appendqnumstring,
302 'scrollpos' => $scrollpos);
79bb7202 303 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
fb6dcdab
TH
304 //useful for passing data to the next page which is not saved in the database.
305 $nexturlparams += $fromform->nextpageparam;
add2d7ac 306 }
79bb7202 307 $nexturlparams['id'] = $question->id;
ba18b21d 308 $nexturlparams['wizardnow'] = $fromform->wizard;
fb6dcdab 309 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
79bb7202 310 if ($cmid){
311 $nexturl->param('cmid', $cmid);
312 } else {
313 $nexturl->param('courseid', $COURSE->id);
314 }
7a567a92 315 redirect($nexturl);
516cf3eb 316 }
add2d7ac 317
5d548d3e 318} else {
06f8ed54 319 $streditingquestion = $qtypeobj->get_heading();
d412354c 320 $PAGE->set_title($streditingquestion);
0f56fde2 321 $PAGE->set_heading($COURSE->fullname);
9ab75b2b 322 if ($cm !== null) {
479cca8a 323 $strmodule = get_string('modulename', $cm->modname);
479cca8a 324 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
a6855934
PS
325 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
326 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
479cca8a 327 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
328 //don't need this link if returnurl returns to view.php
d412354c 329 $PAGE->navbar->add($streditingmodule, $returnurl);
479cca8a 330 }
d412354c 331 $PAGE->navbar->add($streditingquestion);
d412354c 332 echo $OUTPUT->header();
271e6dec 333
8c2550c8 334 } else {
5e8a85aa
TH
335 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
336 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
d412354c 337 $PAGE->navbar->add($streditingquestion);
338 echo $OUTPUT->header();
8c2550c8 339 }
516cf3eb 340
add2d7ac 341 // Display a heading, question editing form and possibly some extra content needed for
342 // for this question type.
06f8ed54 343 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
9b59580b 344 echo $OUTPUT->footer();
add2d7ac 345}