quiz editing: MDL-22263 Editing UI messed up with JavaScript disabled.
[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
271e6dec 25$returnurl = 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}
51if ($returnurl !== 0) {
52 $url->param('returnurl', $returnurl);
53}
54if ($appendqnumstring !== '') {
55 $url->param('appendqnumstring', $appendqnumstring);
56}
57if ($inpopup !== 0) {
58 $url->param('inpopup', $inpopup);
59}
60$PAGE->set_url($url);
61
271e6dec 62if ($movecontext && !$id){
63 print_error('questiondoesnotexist', 'question', $returnurl);
64}
add2d7ac 65
9ab75b2b 66if ($cmid){
271e6dec 67 list($module, $cm) = get_module_from_cmid($cmid);
68 require_login($cm->course, false, $cm);
69 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
70} elseif ($courseid) {
71 require_login($courseid, false);
72 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
9ab75b2b 73 $module = null;
74 $cm = null;
271e6dec 75} else {
0be2c858 76 print_error('missingcourseorcmid', 'question');
9ab75b2b 77}
271e6dec 78$contexts = new question_edit_contexts($thiscontext);
79
271e6dec 80if (!$returnurl) {
81 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
82}
83
cd120b23 84if (optional_param('addcancel', false, PARAM_BOOL)) {
85 redirect($returnurl);
86}
87
add2d7ac 88if ($id) {
f34488b2 89 if (!$question = $DB->get_record('question', array('id' => $id))) {
add2d7ac 90 print_error('questiondoesnotexist', 'question', $returnurl);
91 }
c599a682 92 get_question_options($question, true);
a27aa5c6 93
add2d7ac 94} else if ($categoryid && $qtype) { // only for creating new questions
95 $question = new stdClass;
96 $question->category = $categoryid;
97 $question->qtype = $qtype;
8ed358db 98
99 // Check that users are allowed to create this question type at the moment.
100 $allowedtypes = question_type_menu();
101 if (!isset($allowedtypes[$qtype])) {
102 print_error('cannotenable', 'question', $returnurl, $qtype);
103 }
a27aa5c6
TH
104
105} else if ($categoryid) {
106 // Category, but no qtype. They probably came from the addquestion.php
107 // script without choosing a question type. Send them back.
108 $addurl = new moodle_url('/question/addquestion.php', $url->params());
109 $addurl->param('validationerror', 1);
110 redirect($addurl);
111
add2d7ac 112} else {
113 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
114}
115
116// Validate the question category.
f34488b2 117if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
add2d7ac 118 print_error('categorydoesnotexist', 'question', $returnurl);
119}
271e6dec 120
677f62b6 121// Check permissions
c599a682 122$question->formoptions = new stdClass;
271e6dec 123
124$categorycontext = get_context_instance_by_id($category->contextid);
125$addpermission = has_capability('moodle/question:add', $categorycontext);
126
127if ($id) {
128 $canview = question_has_capability_on($question, 'view');
129 if ($movecontext){
130 $question->formoptions->canedit = false;
131 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
132 $question->formoptions->cansaveasnew = false;
133 $question->formoptions->repeatelements = false;
134 $question->formoptions->movecontext = true;
135 $formeditable = true;
136 question_require_capability_on($question, 'view');
137 } else {
138 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
139 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
140 $question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission);
141 $question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew);
142 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
143 $question->formoptions->movecontext = false;
144 if (!$formeditable){
145 question_require_capability_on($question, 'view');
146 }
147 }
148
271e6dec 149} else { // creating a new question
150 require_capability('moodle/question:add', $categorycontext);
151 $formeditable = true;
152 $question->formoptions->repeatelements = true;
153 $question->formoptions->movecontext = false;
add2d7ac 154}
155
156// Validate the question type.
157if (!isset($QTYPES[$question->qtype])) {
158 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
159}
d529807a 160$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 161
add2d7ac 162// Create the question editing form.
271e6dec 163if ($wizardnow!=='' && !$movecontext){
add2d7ac 164 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
165 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 166 } else {
271e6dec 167 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 168 }
169} else {
271e6dec 170 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 171}
add2d7ac 172if ($mform === null) {
173 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
174}
ba18b21d 175$toform = fullclone($question); // send the question object and a few more parameters to the form
176$toform->category = "$category->id,$category->contextid";
177if ($formeditable && $id){
178 $toform->categorymoveto = $toform->category;
179}
fa583f5f 180
181$toform->appendqnumstring = $appendqnumstring;
add2d7ac 182$toform->returnurl = $returnurl;
271e6dec 183$toform->movecontext = $movecontext;
9ab75b2b 184if ($cm !== null){
185 $toform->cmid = $cm->id;
271e6dec 186 $toform->courseid = $cm->course;
187} else {
188 $toform->courseid = $COURSE->id;
9ab75b2b 189}
11a60967 190$toform->inpopup = $inpopup;
add2d7ac 191$mform->set_data($toform);
192
193if ($mform->is_cancelled()){
11a60967 194 if ($inpopup) {
195 close_window();
196 } else {
7846a624
TH
197 $nexturl = new moodle_url($returnurl);
198 $nexturl->param('lastchanged', $question->id);
199 redirect($nexturl->out());
11a60967 200 }
24e8b9b6 201} elseif ($fromform = $mform->get_data()) {
202 /// If we are saving as a copy, break the connection to the old question.
ba18b21d 203 if (!empty($fromform->makecopy)) {
24e8b9b6 204 $question->id = 0;
add2d7ac 205 $question->hidden = 0; // Copies should not be hidden
206 }
24e8b9b6 207
208 /// Process the combination of usecurrentcat, categorymoveto and category form
209 /// fields, so the save_question method only has to consider $fromform->category
210 if (!empty($fromform->usecurrentcat)) {
211 // $fromform->category is the right category to save in.
212 } else {
213 if (!empty($fromform->categorymoveto)) {
214 $fromform->category = $fromform->categorymoveto;
215 } else {
216 // $fromform->category is the right category to save in.
217 }
218 }
219
220 /// If we are moving a question, check we have permission to move it from
221 /// whence it came. (Where we are moving to is validated by the form.)
222 list($newcatid) = explode(',', $fromform->category);
223 if (!empty($question->id) && $newcatid != $question->category) {
224 question_require_capability_on($question, 'move');
225 }
226
227 /// Ensure we redirect back to the category the question is being saved into.
228 $returnurl = new moodle_url($returnurl);
229 $returnurl->param('category', $fromform->category);
340d4612 230 // TODO: it is sloppy to pass arounf full URLs through page parameters and some servers do not like that
b9bc2019 231 $returnurl = $returnurl->out(false);
24e8b9b6 232
233 /// Call the appropriate method.
234 if ($movecontext) {
ba18b21d 235 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
271e6dec 236 $tocontext = get_context_instance_by_id($tocontextid);
237 require_capability('moodle/question:add', $tocontext);
238 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
a6855934 239 $movecontexturl = new moodle_url('/question/contextmoveq.php',
271e6dec 240 array('returnurl' => $returnurl,
241 'ids'=>$question->id,
242 'tocatid'=> $tocatid));
243 if ($cmid){
244 $movecontexturl->param('cmid', $cmid);
245 } else {
246 $movecontexturl->param('courseid', $COURSE->id);
247 }
7a567a92 248 redirect($movecontexturl);
271e6dec 249 }
250 }
ba18b21d 251
c599a682 252 $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow, true);
aeb15530 253 // a wizardpage from multipe pages questiontype like calculated may not allow editing the question tags
3058e79d 254 if (!empty($CFG->usetags) && isset($fromform->tags)) {
c599a682 255 require_once($CFG->dirroot.'/tag/lib.php');
256 tag_set('question', $question->id, $fromform->tags);
257 }
258
79bb7202 259 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
260 if ($inpopup) {
fef8f84e 261 echo $OUTPUT->notification(get_string('changessaved'), '');
add2d7ac 262 close_window(3);
516cf3eb 263 } else {
aa9c6ecf 264 $nexturl = new moodle_url($returnurl);
265 $nexturl->param('lastchanged', $question->id);
266 if($appendqnumstring) {
267 $nexturl->params(array($appendqnumstring=>($question->id), "sesskey"=>sesskey(), "cmid"=>$cmid));
fa583f5f 268 }
7a567a92 269 redirect($nexturl);
516cf3eb 270 }
add2d7ac 271 } else {
fa583f5f 272 $nexturlparams = array('returnurl'=>$returnurl, 'appendqnumstring'=>$appendqnumstring);
79bb7202 273 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
274 $nexturlparams += $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
add2d7ac 275 }
79bb7202 276 $nexturlparams['id'] = $question->id;
ba18b21d 277 $nexturlparams['wizardnow'] = $fromform->wizard;
271e6dec 278 $nexturl = new moodle_url('question.php', $nexturlparams);
79bb7202 279 if ($cmid){
280 $nexturl->param('cmid', $cmid);
281 } else {
282 $nexturl->param('courseid', $COURSE->id);
283 }
7a567a92 284 redirect($nexturl);
516cf3eb 285 }
add2d7ac 286} else {
287
cd120b23 288 $streditingquestion = $QTYPES[$question->qtype]->get_heading();
d412354c 289 $PAGE->set_title($streditingquestion);
0f56fde2 290 $PAGE->set_heading($COURSE->fullname);
9ab75b2b 291 if ($cm !== null) {
479cca8a 292 $strmodule = get_string('modulename', $cm->modname);
479cca8a 293 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
a6855934
PS
294 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
295 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
479cca8a 296 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
297 //don't need this link if returnurl returns to view.php
d412354c 298 $PAGE->navbar->add($streditingmodule, $returnurl);
479cca8a 299 }
d412354c 300 $PAGE->navbar->add($streditingquestion);
d412354c 301 echo $OUTPUT->header();
271e6dec 302
8c2550c8 303 } else {
d412354c 304 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
305 $PAGE->navbar->add(get_string('editquestions', "quiz"), $returnurl);
306 $PAGE->navbar->add($streditingquestion);
307 echo $OUTPUT->header();
8c2550c8 308 }
516cf3eb 309
add2d7ac 310 // Display a heading, question editing form and possibly some extra content needed for
311 // for this question type.
312 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
9b59580b 313 echo $OUTPUT->footer();
add2d7ac 314}
aeb15530 315