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