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