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