Merge branch 'w26_MDL-40254_m25_flatfile' of https://github.com/skodak/moodle into...
[moodle.git] / question / question.php
blob7577dc28fec0745422e61c1740d1c7d38935d381
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 $qtype = optional_param('qtype', '', PARAM_FILE);
35 $categoryid = optional_param('category', 0, PARAM_INT);
36 $cmid = optional_param('cmid', 0, PARAM_INT);
37 $courseid = optional_param('courseid', 0, PARAM_INT);
38 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
39 $movecontext = optional_param('movecontext', 0, PARAM_BOOL); // Switch to make
40 // question uneditable - form is displayed to edit category only
41 $originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
42 $appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
43 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
44 $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
46 $url = new moodle_url('/question/question.php');
47 if ($id !== 0) {
48 $url->param('id', $id);
50 if ($qtype !== '') {
51 $url->param('qtype', $qtype);
53 if ($categoryid !== 0) {
54 $url->param('category', $categoryid);
56 if ($cmid !== 0) {
57 $url->param('cmid', $cmid);
59 if ($courseid !== 0) {
60 $url->param('courseid', $courseid);
62 if ($wizardnow !== '') {
63 $url->param('wizardnow', $wizardnow);
65 if ($movecontext !== 0) {
66 $url->param('movecontext', $movecontext);
68 if ($originalreturnurl !== 0) {
69 $url->param('returnurl', $originalreturnurl);
71 if ($appendqnumstring !== '') {
72 $url->param('appendqnumstring', $appendqnumstring);
74 if ($inpopup !== 0) {
75 $url->param('inpopup', $inpopup);
77 if ($scrollpos) {
78 $url->param('scrollpos', $scrollpos);
80 $PAGE->set_url($url);
82 if ($originalreturnurl) {
83 if (strpos($originalreturnurl, '/') !== 0) {
84 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
86 $returnurl = new moodle_url($originalreturnurl);
87 } else if ($cmid) {
88 $returnurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
89 } else {
90 $returnurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
92 if ($scrollpos) {
93 $returnurl->param('scrollpos', $scrollpos);
96 if ($movecontext && !$id){
97 print_error('questiondoesnotexist', 'question', $returnurl);
100 if ($cmid){
101 list($module, $cm) = get_module_from_cmid($cmid);
102 require_login($cm->course, false, $cm);
103 $thiscontext = context_module::instance($cmid);
104 } elseif ($courseid) {
105 require_login($courseid, false);
106 $thiscontext = context_course::instance($courseid);
107 $module = null;
108 $cm = null;
109 } else {
110 print_error('missingcourseorcmid', 'question');
112 $contexts = new question_edit_contexts($thiscontext);
113 $PAGE->set_pagelayout('admin');
115 if (optional_param('addcancel', false, PARAM_BOOL)) {
116 redirect($returnurl);
119 if ($id) {
120 if (!$question = $DB->get_record('question', array('id' => $id))) {
121 print_error('questiondoesnotexist', 'question', $returnurl);
123 get_question_options($question, true);
125 } else if ($categoryid && $qtype) { // only for creating new questions
126 $question = new stdClass();
127 $question->category = $categoryid;
128 $question->qtype = $qtype;
129 $question->createdby = $USER->id;
131 // Check that users are allowed to create this question type at the moment.
132 if (!question_bank::qtype_enabled($qtype)) {
133 print_error('cannotenable', 'question', $returnurl, $qtype);
136 } else if ($categoryid) {
137 // Category, but no qtype. They probably came from the addquestion.php
138 // script without choosing a question type. Send them back.
139 $addurl = new moodle_url('/question/addquestion.php', $url->params());
140 $addurl->param('validationerror', 1);
141 redirect($addurl);
143 } else {
144 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
147 $qtypeobj = question_bank::get_qtype($question->qtype);
149 // Validate the question category.
150 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
151 print_error('categorydoesnotexist', 'question', $returnurl);
154 // Check permissions
155 $question->formoptions = new stdClass();
157 $categorycontext = context::instance_by_id($category->contextid);
158 $addpermission = has_capability('moodle/question:add', $categorycontext);
160 if ($id) {
161 if ($movecontext){
162 $question->formoptions->canedit = false;
163 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
164 $question->formoptions->cansaveasnew = false;
165 $question->formoptions->repeatelements = false;
166 $question->formoptions->movecontext = true;
167 $formeditable = true;
168 question_require_capability_on($question, 'move');
169 } else {
170 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
171 $question->formoptions->canmove = $addpermission && question_has_capability_on($question, 'move');
172 $question->formoptions->cansaveasnew = $addpermission &&
173 (question_has_capability_on($question, 'view') || $question->formoptions->canedit);
174 $question->formoptions->repeatelements = $question->formoptions->canedit || $question->formoptions->cansaveasnew;
175 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
176 $question->formoptions->movecontext = false;
177 if (!$formeditable) {
178 question_require_capability_on($question, 'view');
182 } else { // creating a new question
183 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
184 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
185 $question->formoptions->cansaveasnew = false;
186 $question->formoptions->repeatelements = true;
187 $question->formoptions->movecontext = false;
188 $formeditable = true;
189 require_capability('moodle/question:add', $categorycontext);
191 $question->formoptions->mustbeusable = (bool) $appendqnumstring;
193 // Validate the question type.
194 $PAGE->set_pagetype('question-type-' . $question->qtype);
196 // Create the question editing form.
197 if ($wizardnow !== '' && !$movecontext){
198 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
199 } else {
200 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
202 $toform = fullclone($question); // send the question object and a few more parameters to the form
203 $toform->category = "$category->id,$category->contextid";
204 $toform->scrollpos = $scrollpos;
205 if ($formeditable && $id){
206 $toform->categorymoveto = $toform->category;
209 $toform->appendqnumstring = $appendqnumstring;
210 $toform->returnurl = $originalreturnurl;
211 $toform->movecontext = $movecontext;
212 if ($cm !== null){
213 $toform->cmid = $cm->id;
214 $toform->courseid = $cm->course;
215 } else {
216 $toform->courseid = $COURSE->id;
219 $toform->inpopup = $inpopup;
221 $mform->set_data($toform);
223 if ($mform->is_cancelled()) {
224 if ($inpopup) {
225 close_window();
226 } else {
227 redirect($returnurl);
230 } else if ($fromform = $mform->get_data()) {
231 /// If we are saving as a copy, break the connection to the old question.
232 if (!empty($fromform->makecopy)) {
233 $question->id = 0;
234 $question->hidden = 0; // Copies should not be hidden
237 /// Process the combination of usecurrentcat, categorymoveto and category form
238 /// fields, so the save_question method only has to consider $fromform->category
239 if (!empty($fromform->usecurrentcat)) {
240 // $fromform->category is the right category to save in.
241 } else {
242 if (!empty($fromform->categorymoveto)) {
243 $fromform->category = $fromform->categorymoveto;
244 } else {
245 // $fromform->category is the right category to save in.
249 /// If we are moving a question, check we have permission to move it from
250 /// whence it came. (Where we are moving to is validated by the form.)
251 list($newcatid, $newcontextid) = explode(',', $fromform->category);
252 if (!empty($question->id) && $newcatid != $question->category) {
253 question_require_capability_on($question, 'move');
256 // Ensure we redirect back to the category the question is being saved into.
257 $returnurl->param('category', $fromform->category);
259 if ($movecontext) {
260 // We are just moving the question to a different context.
261 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
262 require_capability('moodle/question:add', context::instance_by_id($tocontextid));
263 question_move_questions_to_category(array($question->id), $tocatid);
265 } else {
266 // We are acutally saving the question.
267 if (!empty($question->id)) {
268 question_require_capability_on($question, 'edit');
269 } else {
270 require_capability('moodle/question:add', context::instance_by_id($newcontextid));
271 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
272 print_error('nopermissions', '', '', 'edit');
275 $question = $qtypeobj->save_question($question, $fromform);
276 if (!empty($CFG->usetags) && isset($fromform->tags)) {
277 // A wizardpage from multipe pages questiontype like calculated may not
278 // allow editing the question tags, hence the isset($fromform->tags) test.
279 require_once($CFG->dirroot.'/tag/lib.php');
280 tag_set('question', $question->id, $fromform->tags);
284 // Purge this question from the cache.
285 question_bank::notify_question_edited($question->id);
287 if (($qtypeobj->finished_edit_wizard($fromform)) || $movecontext) {
288 if ($inpopup) {
289 echo $OUTPUT->notification(get_string('changessaved'), '');
290 close_window(3);
291 } else {
292 $returnurl->param('lastchanged', $question->id);
293 if ($appendqnumstring) {
294 $returnurl->param($appendqnumstring, $question->id);
295 $returnurl->param('sesskey', sesskey());
296 $returnurl->param('cmid', $cmid);
298 redirect($returnurl);
301 } else {
302 $nexturlparams = array(
303 'returnurl' => $originalreturnurl,
304 'appendqnumstring' => $appendqnumstring,
305 'scrollpos' => $scrollpos);
306 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
307 //useful for passing data to the next page which is not saved in the database.
308 $nexturlparams += $fromform->nextpageparam;
310 $nexturlparams['id'] = $question->id;
311 $nexturlparams['wizardnow'] = $fromform->wizard;
312 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
313 if ($cmid){
314 $nexturl->param('cmid', $cmid);
315 } else {
316 $nexturl->param('courseid', $COURSE->id);
318 redirect($nexturl);
321 } else {
322 $streditingquestion = $qtypeobj->get_heading();
323 $PAGE->set_title($streditingquestion);
324 $PAGE->set_heading($COURSE->fullname);
325 if ($cm !== null) {
326 $strmodule = get_string('modulename', $cm->modname);
327 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
328 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
329 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
330 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
331 //don't need this link if returnurl returns to view.php
332 $PAGE->navbar->add($streditingmodule, $returnurl);
334 $PAGE->navbar->add($streditingquestion);
335 echo $OUTPUT->header();
337 } else {
338 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
339 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
340 $PAGE->navbar->add($streditingquestion);
341 echo $OUTPUT->header();
344 // Display a heading, question editing form and possibly some extra content needed for
345 // for this question type.
346 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
347 echo $OUTPUT->footer();