MDL-44316 core: updated the 'tag_instance' table to store the component, contextid...
[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
8011be18 34$makecopy = optional_param('makecopy', 0, PARAM_BOOL);
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}
8011be18 49if ($makecopy) {
e0c41591
K
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);
8011be18 168 $question->beingcopied = true;
271e6dec 169 }
170
271e6dec 171} else { // creating a new question
fe6ce234
DC
172 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
173 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
51c5e605 174 $question->formoptions->cansaveasnew = false;
271e6dec 175 $question->formoptions->repeatelements = true;
51c5e605
TH
176 $formeditable = true;
177 require_capability('moodle/question:add', $categorycontext);
add2d7ac 178}
76cf77e4 179$question->formoptions->mustbeusable = (bool) $appendqnumstring;
add2d7ac 180
181// Validate the question type.
d529807a 182$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 183
add2d7ac 184// Create the question editing form.
435d3279 185if ($wizardnow !== '') {
06f8ed54 186 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 187} else {
06f8ed54 188 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 189}
ba18b21d 190$toform = fullclone($question); // send the question object and a few more parameters to the form
191$toform->category = "$category->id,$category->contextid";
a13d4fbd 192$toform->scrollpos = $scrollpos;
ba18b21d 193if ($formeditable && $id){
194 $toform->categorymoveto = $toform->category;
195}
fa583f5f 196
197$toform->appendqnumstring = $appendqnumstring;
90c7912e 198$toform->returnurl = $originalreturnurl;
e0c41591 199$toform->makecopy = $makecopy;
9ab75b2b 200if ($cm !== null){
201 $toform->cmid = $cm->id;
271e6dec 202 $toform->courseid = $cm->course;
203} else {
204 $toform->courseid = $COURSE->id;
9ab75b2b 205}
fe6ce234 206
11a60967 207$toform->inpopup = $inpopup;
fe6ce234 208
add2d7ac 209$mform->set_data($toform);
210
06f8ed54 211if ($mform->is_cancelled()) {
11a60967 212 if ($inpopup) {
213 close_window();
214 } else {
a13d4fbd 215 redirect($returnurl);
11a60967 216 }
06f8ed54 217
5d548d3e 218} else if ($fromform = $mform->get_data()) {
e0c41591
K
219 // If we are saving as a copy, break the connection to the old question.
220 if ($makecopy) {
24e8b9b6 221 $question->id = 0;
e0c41591 222 $question->hidden = 0; // Copies should not be hidden.
add2d7ac 223 }
24e8b9b6 224
225 /// Process the combination of usecurrentcat, categorymoveto and category form
226 /// fields, so the save_question method only has to consider $fromform->category
227 if (!empty($fromform->usecurrentcat)) {
228 // $fromform->category is the right category to save in.
229 } else {
230 if (!empty($fromform->categorymoveto)) {
231 $fromform->category = $fromform->categorymoveto;
232 } else {
233 // $fromform->category is the right category to save in.
234 }
235 }
236
237 /// If we are moving a question, check we have permission to move it from
238 /// whence it came. (Where we are moving to is validated by the form.)
51c5e605 239 list($newcatid, $newcontextid) = explode(',', $fromform->category);
24e8b9b6 240 if (!empty($question->id) && $newcatid != $question->category) {
241 question_require_capability_on($question, 'move');
242 }
243
5d548d3e 244 // Ensure we redirect back to the category the question is being saved into.
24e8b9b6 245 $returnurl->param('category', $fromform->category);
24e8b9b6 246
435d3279
K
247 // We are acutally saving the question.
248 if (!empty($question->id)) {
249 question_require_capability_on($question, 'edit');
5d548d3e 250 } else {
435d3279
K
251 require_capability('moodle/question:add', context::instance_by_id($newcontextid));
252 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
253 print_error('nopermissions', '', '', 'edit');
5d548d3e 254 }
c599a682 255 }
435d3279
K
256 $question = $qtypeobj->save_question($question, $fromform);
257 if (!empty($CFG->usetags) && isset($fromform->tags)) {
258 // A wizardpage from multipe pages questiontype like calculated may not
259 // allow editing the question tags, hence the isset($fromform->tags) test.
260 require_once($CFG->dirroot.'/tag/lib.php');
261 tag_set('question', $question->id, $fromform->tags);
262 }
c599a682 263
a560d636
TH
264 // Purge this question from the cache.
265 question_bank::notify_question_edited($question->id);
266
7756e2bc
K
267 // If we are saving and continuing to edit the question.
268 if (!empty($fromform->updatebutton)) {
8011be18
TH
269 $url->param('id', $question->id);
270 $url->remove_params('makecopy');
7756e2bc
K
271 redirect($url);
272 }
273
435d3279 274 if ($qtypeobj->finished_edit_wizard($fromform)) {
79bb7202 275 if ($inpopup) {
fef8f84e 276 echo $OUTPUT->notification(get_string('changessaved'), '');
add2d7ac 277 close_window(3);
516cf3eb 278 } else {
fb6dcdab
TH
279 $returnurl->param('lastchanged', $question->id);
280 if ($appendqnumstring) {
281 $returnurl->param($appendqnumstring, $question->id);
282 $returnurl->param('sesskey', sesskey());
283 $returnurl->param('cmid', $cmid);
fa583f5f 284 }
fb6dcdab 285 redirect($returnurl);
516cf3eb 286 }
5d548d3e 287
add2d7ac 288 } else {
fb6dcdab
TH
289 $nexturlparams = array(
290 'returnurl' => $originalreturnurl,
a13d4fbd
TH
291 'appendqnumstring' => $appendqnumstring,
292 'scrollpos' => $scrollpos);
79bb7202 293 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
fb6dcdab
TH
294 //useful for passing data to the next page which is not saved in the database.
295 $nexturlparams += $fromform->nextpageparam;
add2d7ac 296 }
79bb7202 297 $nexturlparams['id'] = $question->id;
ba18b21d 298 $nexturlparams['wizardnow'] = $fromform->wizard;
fb6dcdab 299 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
79bb7202 300 if ($cmid){
301 $nexturl->param('cmid', $cmid);
302 } else {
303 $nexturl->param('courseid', $COURSE->id);
304 }
7a567a92 305 redirect($nexturl);
516cf3eb 306 }
add2d7ac 307
5d548d3e 308} else {
06f8ed54 309 $streditingquestion = $qtypeobj->get_heading();
d412354c 310 $PAGE->set_title($streditingquestion);
0f56fde2 311 $PAGE->set_heading($COURSE->fullname);
9ab75b2b 312 if ($cm !== null) {
479cca8a 313 $strmodule = get_string('modulename', $cm->modname);
479cca8a 314 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
a6855934
PS
315 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
316 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
479cca8a 317 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
318 //don't need this link if returnurl returns to view.php
d412354c 319 $PAGE->navbar->add($streditingmodule, $returnurl);
479cca8a 320 }
d412354c 321 $PAGE->navbar->add($streditingquestion);
d412354c 322 echo $OUTPUT->header();
271e6dec 323
8c2550c8 324 } else {
5e8a85aa
TH
325 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
326 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
d412354c 327 $PAGE->navbar->add($streditingquestion);
328 echo $OUTPUT->header();
8c2550c8 329 }
516cf3eb 330
add2d7ac 331 // Display a heading, question editing form and possibly some extra content needed for
332 // for this question type.
06f8ed54 333 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
9b59580b 334 echo $OUTPUT->footer();
add2d7ac 335}