MDL-45214 always try to use ID sort in get_events_select()
[moodle.git] / question / question.php
blob45cf3e6ea16827a6cbef8f2a037d9d70a2589d07
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Page for editing questions.
20 * @package moodlecore
21 * @subpackage questionbank
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(__FILE__) . '/../config.php');
28 require_once(dirname(__FILE__) . '/editlib.php');
29 require_once($CFG->libdir . '/filelib.php');
30 require_once($CFG->libdir . '/formslib.php');
32 // Read URL parameters telling us which question to edit.
33 $id = optional_param('id', 0, PARAM_INT); // question id
34 $makecopy = optional_param('makecopy', 0, PARAM_BOOL);
35 $qtype = optional_param('qtype', '', PARAM_FILE);
36 $categoryid = optional_param('category', 0, PARAM_INT);
37 $cmid = optional_param('cmid', 0, PARAM_INT);
38 $courseid = optional_param('courseid', 0, PARAM_INT);
39 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
40 $originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
41 $appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
42 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
43 $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
45 $url = new moodle_url('/question/question.php');
46 if ($id !== 0) {
47 $url->param('id', $id);
49 if ($makecopy) {
50 $url->param('makecopy', $makecopy);
52 if ($qtype !== '') {
53 $url->param('qtype', $qtype);
55 if ($categoryid !== 0) {
56 $url->param('category', $categoryid);
58 if ($cmid !== 0) {
59 $url->param('cmid', $cmid);
61 if ($courseid !== 0) {
62 $url->param('courseid', $courseid);
64 if ($wizardnow !== '') {
65 $url->param('wizardnow', $wizardnow);
67 if ($originalreturnurl !== 0) {
68 $url->param('returnurl', $originalreturnurl);
70 if ($appendqnumstring !== '') {
71 $url->param('appendqnumstring', $appendqnumstring);
73 if ($inpopup !== 0) {
74 $url->param('inpopup', $inpopup);
76 if ($scrollpos) {
77 $url->param('scrollpos', $scrollpos);
79 $PAGE->set_url($url);
81 if ($originalreturnurl) {
82 if (strpos($originalreturnurl, '/') !== 0) {
83 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
85 $returnurl = new moodle_url($originalreturnurl);
86 } else if ($cmid) {
87 $returnurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
88 } else {
89 $returnurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
91 if ($scrollpos) {
92 $returnurl->param('scrollpos', $scrollpos);
95 if ($cmid){
96 list($module, $cm) = get_module_from_cmid($cmid);
97 require_login($cm->course, false, $cm);
98 $thiscontext = context_module::instance($cmid);
99 } elseif ($courseid) {
100 require_login($courseid, false);
101 $thiscontext = context_course::instance($courseid);
102 $module = null;
103 $cm = null;
104 } else {
105 print_error('missingcourseorcmid', 'question');
107 $contexts = new question_edit_contexts($thiscontext);
108 $PAGE->set_pagelayout('admin');
110 if (optional_param('addcancel', false, PARAM_BOOL)) {
111 redirect($returnurl);
114 if ($id) {
115 if (!$question = $DB->get_record('question', array('id' => $id))) {
116 print_error('questiondoesnotexist', 'question', $returnurl);
118 get_question_options($question, true);
120 } else if ($categoryid && $qtype) { // only for creating new questions
121 $question = new stdClass();
122 $question->category = $categoryid;
123 $question->qtype = $qtype;
124 $question->createdby = $USER->id;
126 // Check that users are allowed to create this question type at the moment.
127 if (!question_bank::qtype_enabled($qtype)) {
128 print_error('cannotenable', 'question', $returnurl, $qtype);
131 } else if ($categoryid) {
132 // Category, but no qtype. They probably came from the addquestion.php
133 // script without choosing a question type. Send them back.
134 $addurl = new moodle_url('/question/addquestion.php', $url->params());
135 $addurl->param('validationerror', 1);
136 redirect($addurl);
138 } else {
139 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
142 $qtypeobj = question_bank::get_qtype($question->qtype);
144 // Validate the question category.
145 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
146 print_error('categorydoesnotexist', 'question', $returnurl);
149 // Check permissions
150 $question->formoptions = new stdClass();
152 $categorycontext = context::instance_by_id($category->contextid);
153 $addpermission = has_capability('moodle/question:add', $categorycontext);
155 if ($id) {
156 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
157 $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
158 $question->formoptions->cansaveasnew = $addpermission &&
159 (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
160 $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
161 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
162 if (!$formeditable) {
163 question_require_capability_on($question, 'view');
165 if ($makecopy) {
166 // If we are duplicating a question, add some indication to the question name.
167 $question->name = get_string('questionnamecopy', 'question', $question->name);
168 $question->beingcopied = true;
171 } else { // creating a new question
172 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
173 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
174 $question->formoptions->cansaveasnew = false;
175 $question->formoptions->repeatelements = true;
176 $formeditable = true;
177 require_capability('moodle/question:add', $categorycontext);
179 $question->formoptions->mustbeusable = (bool) $appendqnumstring;
181 // Validate the question type.
182 $PAGE->set_pagetype('question-type-' . $question->qtype);
184 // Create the question editing form.
185 if ($wizardnow !== '') {
186 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
187 } else {
188 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
190 $toform = fullclone($question); // send the question object and a few more parameters to the form
191 $toform->category = "$category->id,$category->contextid";
192 $toform->scrollpos = $scrollpos;
193 if ($formeditable && $id){
194 $toform->categorymoveto = $toform->category;
197 $toform->appendqnumstring = $appendqnumstring;
198 $toform->returnurl = $originalreturnurl;
199 $toform->makecopy = $makecopy;
200 if ($cm !== null){
201 $toform->cmid = $cm->id;
202 $toform->courseid = $cm->course;
203 } else {
204 $toform->courseid = $COURSE->id;
207 $toform->inpopup = $inpopup;
209 $mform->set_data($toform);
211 if ($mform->is_cancelled()) {
212 if ($inpopup) {
213 close_window();
214 } else {
215 redirect($returnurl);
218 } else if ($fromform = $mform->get_data()) {
219 // If we are saving as a copy, break the connection to the old question.
220 if ($makecopy) {
221 $question->id = 0;
222 $question->hidden = 0; // Copies should not be hidden.
225 /// Process the combination of usecurrentcat, categorymoveto and category form
226 /// fields, so the save_question method only has to consider $fromform->category
227 if (!empty($fromform->usecurrentcat)) {
228 // $fromform->category is the right category to save in.
229 } else {
230 if (!empty($fromform->categorymoveto)) {
231 $fromform->category = $fromform->categorymoveto;
232 } else {
233 // $fromform->category is the right category to save in.
237 /// If we are moving a question, check we have permission to move it from
238 /// whence it came. (Where we are moving to is validated by the form.)
239 list($newcatid, $newcontextid) = explode(',', $fromform->category);
240 if (!empty($question->id) && $newcatid != $question->category) {
241 $contextid = $newcontextid;
242 question_require_capability_on($question, 'move');
243 } else {
244 $contextid = $category->contextid;
247 // Ensure we redirect back to the category the question is being saved into.
248 $returnurl->param('category', $fromform->category);
250 // We are acutally saving the question.
251 if (!empty($question->id)) {
252 question_require_capability_on($question, 'edit');
253 } else {
254 require_capability('moodle/question:add', context::instance_by_id($contextid));
255 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
256 print_error('nopermissions', '', '', 'edit');
259 $question = $qtypeobj->save_question($question, $fromform);
260 if (!empty($CFG->usetags) && isset($fromform->tags)) {
261 // A wizardpage from multipe pages questiontype like calculated may not
262 // allow editing the question tags, hence the isset($fromform->tags) test.
263 require_once($CFG->dirroot.'/tag/lib.php');
264 tag_set('question', $question->id, $fromform->tags, 'core_question', $contextid);
267 // Purge this question from the cache.
268 question_bank::notify_question_edited($question->id);
270 // If we are saving and continuing to edit the question.
271 if (!empty($fromform->updatebutton)) {
272 $url->param('id', $question->id);
273 $url->remove_params('makecopy');
274 redirect($url);
277 if ($qtypeobj->finished_edit_wizard($fromform)) {
278 if ($inpopup) {
279 echo $OUTPUT->notification(get_string('changessaved'), '');
280 close_window(3);
281 } else {
282 $returnurl->param('lastchanged', $question->id);
283 if ($appendqnumstring) {
284 $returnurl->param($appendqnumstring, $question->id);
285 $returnurl->param('sesskey', sesskey());
286 $returnurl->param('cmid', $cmid);
288 redirect($returnurl);
291 } else {
292 $nexturlparams = array(
293 'returnurl' => $originalreturnurl,
294 'appendqnumstring' => $appendqnumstring,
295 'scrollpos' => $scrollpos);
296 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
297 //useful for passing data to the next page which is not saved in the database.
298 $nexturlparams += $fromform->nextpageparam;
300 $nexturlparams['id'] = $question->id;
301 $nexturlparams['wizardnow'] = $fromform->wizard;
302 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
303 if ($cmid){
304 $nexturl->param('cmid', $cmid);
305 } else {
306 $nexturl->param('courseid', $COURSE->id);
308 redirect($nexturl);
311 } else {
312 $streditingquestion = $qtypeobj->get_heading();
313 $PAGE->set_title($streditingquestion);
314 $PAGE->set_heading($COURSE->fullname);
315 if ($cm !== null) {
316 $strmodule = get_string('modulename', $cm->modname);
317 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
318 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
319 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
320 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
321 //don't need this link if returnurl returns to view.php
322 $PAGE->navbar->add($streditingmodule, $returnurl);
324 $PAGE->navbar->add($streditingquestion);
325 echo $OUTPUT->header();
327 } else {
328 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
329 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
330 $PAGE->navbar->add($streditingquestion);
331 echo $OUTPUT->header();
334 // Display a heading, question editing form and possibly some extra content needed for
335 // for this question type.
336 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
337 echo $OUTPUT->footer();