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