MDL-20636 todo list
[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 }
5d548d3e 214} else if ($fromform = $mform->get_data()) {
24e8b9b6 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
5d548d3e 240 // Ensure we redirect back to the category the question is being saved into.
24e8b9b6 241 $returnurl->param('category', $fromform->category);
24e8b9b6 242
24e8b9b6 243 if ($movecontext) {
5d548d3e 244 // We are just moving the question to a different context.
ba18b21d 245 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
5d548d3e
TH
246 require_capability('moodle/question:add', get_context_instance_by_id($tocontextid));
247 question_move_questions_to_category(array($question->id), $tocatid);
ba18b21d 248
5d548d3e
TH
249 } else {
250 // We are acutally saving the question.
94dbfb3a 251 $question = $QTYPES[$question->qtype]->save_question($question, $fromform);
5d548d3e
TH
252 if (!empty($CFG->usetags) && isset($fromform->tags)) {
253 // A wizardpage from multipe pages questiontype like calculated may not
254 // allow editing the question tags, hence the isset($fromform->tags) test.
255 require_once($CFG->dirroot.'/tag/lib.php');
256 tag_set('question', $question->id, $fromform->tags);
257 }
c599a682 258 }
259
5d548d3e 260 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext) {
79bb7202 261 if ($inpopup) {
fef8f84e 262 echo $OUTPUT->notification(get_string('changessaved'), '');
add2d7ac 263 close_window(3);
516cf3eb 264 } else {
fb6dcdab
TH
265 $returnurl->param('lastchanged', $question->id);
266 if ($appendqnumstring) {
267 $returnurl->param($appendqnumstring, $question->id);
268 $returnurl->param('sesskey', sesskey());
269 $returnurl->param('cmid', $cmid);
fa583f5f 270 }
fb6dcdab 271 redirect($returnurl);
516cf3eb 272 }
5d548d3e 273
add2d7ac 274 } else {
fb6dcdab
TH
275 $nexturlparams = array(
276 'returnurl' => $originalreturnurl,
277 'appendqnumstring' => $appendqnumstring);
79bb7202 278 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
fb6dcdab
TH
279 //useful for passing data to the next page which is not saved in the database.
280 $nexturlparams += $fromform->nextpageparam;
add2d7ac 281 }
79bb7202 282 $nexturlparams['id'] = $question->id;
ba18b21d 283 $nexturlparams['wizardnow'] = $fromform->wizard;
fb6dcdab 284 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
79bb7202 285 if ($cmid){
286 $nexturl->param('cmid', $cmid);
287 } else {
288 $nexturl->param('courseid', $COURSE->id);
289 }
7a567a92 290 redirect($nexturl);
516cf3eb 291 }
add2d7ac 292
5d548d3e 293} else {
cd120b23 294 $streditingquestion = $QTYPES[$question->qtype]->get_heading();
d412354c 295 $PAGE->set_title($streditingquestion);
0f56fde2 296 $PAGE->set_heading($COURSE->fullname);
9ab75b2b 297 if ($cm !== null) {
479cca8a 298 $strmodule = get_string('modulename', $cm->modname);
479cca8a 299 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
a6855934
PS
300 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
301 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
479cca8a 302 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
303 //don't need this link if returnurl returns to view.php
d412354c 304 $PAGE->navbar->add($streditingmodule, $returnurl);
479cca8a 305 }
d412354c 306 $PAGE->navbar->add($streditingquestion);
d412354c 307 echo $OUTPUT->header();
271e6dec 308
8c2550c8 309 } else {
d412354c 310 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
5d548d3e 311 $PAGE->navbar->add(get_string('editquestions', 'quiz'), $returnurl);
d412354c 312 $PAGE->navbar->add($streditingquestion);
313 echo $OUTPUT->header();
8c2550c8 314 }
516cf3eb 315
add2d7ac 316 // Display a heading, question editing form and possibly some extra content needed for
317 // for this question type.
318 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
9b59580b 319 echo $OUTPUT->footer();
add2d7ac 320}
aeb15530 321