Merge branch 'master_MDL-37714' of git://github.com/greg-or/moodle
[moodle.git] / question / question.php
blobc0927d52482205194c7f8ff8392a57202ac1a3d5
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_INT);
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 !== 0) {
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);
170 } else { // creating a new question
171 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
172 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
173 $question->formoptions->cansaveasnew = false;
174 $question->formoptions->repeatelements = true;
175 $formeditable = true;
176 require_capability('moodle/question:add', $categorycontext);
178 $question->formoptions->mustbeusable = (bool) $appendqnumstring;
180 // Validate the question type.
181 $PAGE->set_pagetype('question-type-' . $question->qtype);
183 // Create the question editing form.
184 if ($wizardnow !== '') {
185 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
186 } else {
187 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
189 $toform = fullclone($question); // send the question object and a few more parameters to the form
190 $toform->category = "$category->id,$category->contextid";
191 $toform->scrollpos = $scrollpos;
192 if ($formeditable && $id){
193 $toform->categorymoveto = $toform->category;
196 $toform->appendqnumstring = $appendqnumstring;
197 $toform->returnurl = $originalreturnurl;
198 $toform->makecopy = $makecopy;
199 if ($cm !== null){
200 $toform->cmid = $cm->id;
201 $toform->courseid = $cm->course;
202 } else {
203 $toform->courseid = $COURSE->id;
206 $toform->inpopup = $inpopup;
208 $mform->set_data($toform);
210 if ($mform->is_cancelled()) {
211 if ($inpopup) {
212 close_window();
213 } else {
214 redirect($returnurl);
217 } else if ($fromform = $mform->get_data()) {
218 // If we are saving as a copy, break the connection to the old question.
219 if ($makecopy) {
220 $question->id = 0;
221 $question->hidden = 0; // Copies should not be hidden.
224 /// Process the combination of usecurrentcat, categorymoveto and category form
225 /// fields, so the save_question method only has to consider $fromform->category
226 if (!empty($fromform->usecurrentcat)) {
227 // $fromform->category is the right category to save in.
228 } else {
229 if (!empty($fromform->categorymoveto)) {
230 $fromform->category = $fromform->categorymoveto;
231 } else {
232 // $fromform->category is the right category to save in.
236 /// If we are moving a question, check we have permission to move it from
237 /// whence it came. (Where we are moving to is validated by the form.)
238 list($newcatid, $newcontextid) = explode(',', $fromform->category);
239 if (!empty($question->id) && $newcatid != $question->category) {
240 question_require_capability_on($question, 'move');
243 // Ensure we redirect back to the category the question is being saved into.
244 $returnurl->param('category', $fromform->category);
246 // We are acutally saving the question.
247 if (!empty($question->id)) {
248 question_require_capability_on($question, 'edit');
249 } else {
250 require_capability('moodle/question:add', context::instance_by_id($newcontextid));
251 if (!empty($fromform->makecopy) && !$question->formoptions->cansaveasnew) {
252 print_error('nopermissions', '', '', 'edit');
255 $question = $qtypeobj->save_question($question, $fromform);
256 if (!empty($CFG->usetags) && isset($fromform->tags)) {
257 // A wizardpage from multipe pages questiontype like calculated may not
258 // allow editing the question tags, hence the isset($fromform->tags) test.
259 require_once($CFG->dirroot.'/tag/lib.php');
260 tag_set('question', $question->id, $fromform->tags);
263 // Purge this question from the cache.
264 question_bank::notify_question_edited($question->id);
266 if ($qtypeobj->finished_edit_wizard($fromform)) {
267 if ($inpopup) {
268 echo $OUTPUT->notification(get_string('changessaved'), '');
269 close_window(3);
270 } else {
271 $returnurl->param('lastchanged', $question->id);
272 if ($appendqnumstring) {
273 $returnurl->param($appendqnumstring, $question->id);
274 $returnurl->param('sesskey', sesskey());
275 $returnurl->param('cmid', $cmid);
277 redirect($returnurl);
280 } else {
281 $nexturlparams = array(
282 'returnurl' => $originalreturnurl,
283 'appendqnumstring' => $appendqnumstring,
284 'scrollpos' => $scrollpos);
285 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
286 //useful for passing data to the next page which is not saved in the database.
287 $nexturlparams += $fromform->nextpageparam;
289 $nexturlparams['id'] = $question->id;
290 $nexturlparams['wizardnow'] = $fromform->wizard;
291 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
292 if ($cmid){
293 $nexturl->param('cmid', $cmid);
294 } else {
295 $nexturl->param('courseid', $COURSE->id);
297 redirect($nexturl);
300 } else {
301 $streditingquestion = $qtypeobj->get_heading();
302 $PAGE->set_title($streditingquestion);
303 $PAGE->set_heading($COURSE->fullname);
304 if ($cm !== null) {
305 $strmodule = get_string('modulename', $cm->modname);
306 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
307 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
308 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
309 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
310 //don't need this link if returnurl returns to view.php
311 $PAGE->navbar->add($streditingmodule, $returnurl);
313 $PAGE->navbar->add($streditingquestion);
314 echo $OUTPUT->header();
316 } else {
317 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string('editquestions', 'question').'</a> -> '.$streditingquestion;
318 $PAGE->navbar->add(get_string('editquestions', 'question'), $returnurl);
319 $PAGE->navbar->add($streditingquestion);
320 echo $OUTPUT->header();
323 // Display a heading, question editing form and possibly some extra content needed for
324 // for this question type.
325 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
326 echo $OUTPUT->footer();