2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Page for editing questions.
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(__DIR__
. '/../config.php');
28 require_once(__DIR__
. '/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_COMPONENT
);
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');
47 $url->param('id', $id);
50 $url->param('makecopy', $makecopy);
53 $url->param('qtype', $qtype);
55 if ($categoryid !== 0) {
56 $url->param('category', $categoryid);
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);
74 $url->param('inpopup', $inpopup);
77 $url->param('scrollpos', $scrollpos);
82 $questionbankurl = new moodle_url('/question/edit.php', array('cmid' => $cmid));
84 $questionbankurl = new moodle_url('/question/edit.php', array('courseid' => $courseid));
86 navigation_node
::override_active_url($questionbankurl);
88 if ($originalreturnurl) {
89 if (strpos($originalreturnurl, '/') !== 0) {
90 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
92 $returnurl = new moodle_url($originalreturnurl);
94 $returnurl = $questionbankurl;
97 $returnurl->param('scrollpos', $scrollpos);
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);
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);
120 if (!$question = $DB->get_record('question', array('id' => $id))) {
121 print_error('questiondoesnotexist', 'question', $returnurl);
123 // We can use $COURSE here because it's been initialised as part of the
124 // require_login above. Passing it as the third parameter tells the function
125 // to filter the course tags by that course.
126 get_question_options($question, true, [$COURSE]);
128 } else if ($categoryid && $qtype) { // only for creating new questions
129 $question = new stdClass();
130 $question->category
= $categoryid;
131 $question->qtype
= $qtype;
132 $question->createdby
= $USER->id
;
134 // Check that users are allowed to create this question type at the moment.
135 if (!question_bank
::qtype_enabled($qtype)) {
136 print_error('cannotenable', 'question', $returnurl, $qtype);
139 } else if ($categoryid) {
140 // Category, but no qtype. They probably came from the addquestion.php
141 // script without choosing a question type. Send them back.
142 $addurl = new moodle_url('/question/addquestion.php', $url->params());
143 $addurl->param('validationerror', 1);
147 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
150 $qtypeobj = question_bank
::get_qtype($question->qtype
);
152 if (isset($question->categoryobject
)) {
153 $category = $question->categoryobject
;
155 // Validate the question category.
156 if (!$category = $DB->get_record('question_categories', array('id' => $question->category
))) {
157 print_error('categorydoesnotexist', 'question', $returnurl);
162 $question->formoptions
= new stdClass();
164 $categorycontext = context
::instance_by_id($category->contextid
);
165 $question->contextid
= $category->contextid
;
166 $addpermission = has_capability('moodle/question:add', $categorycontext);
169 $question->formoptions
->canedit
= question_has_capability_on($question, 'edit');
170 $question->formoptions
->canmove
= $addpermission && question_has_capability_on($question, 'move');
171 $question->formoptions
->cansaveasnew
= $addpermission &&
172 (question_has_capability_on($question, 'view') ||
$question->formoptions
->canedit
);
173 $question->formoptions
->repeatelements
= $question->formoptions
->canedit ||
$question->formoptions
->cansaveasnew
;
174 $formeditable = $question->formoptions
->canedit ||
$question->formoptions
->cansaveasnew ||
$question->formoptions
->canmove
;
175 if (!$formeditable) {
176 question_require_capability_on($question, 'view');
179 // If we are duplicating a question, add some indication to the question name.
180 $question->name
= get_string('questionnamecopy', 'question', $question->name
);
181 $question->idnumber
= core_question_find_next_unused_idnumber($question->idnumber
, $category->id
);
182 $question->beingcopied
= true;
185 } else { // creating a new question
186 $question->formoptions
->canedit
= question_has_capability_on($question, 'edit');
187 $question->formoptions
->canmove
= (question_has_capability_on($question, 'move') && $addpermission);
188 $question->formoptions
->cansaveasnew
= false;
189 $question->formoptions
->repeatelements
= true;
190 $formeditable = true;
191 require_capability('moodle/question:add', $categorycontext);
193 $question->formoptions
->mustbeusable
= (bool) $appendqnumstring;
195 // Validate the question type.
196 $PAGE->set_pagetype('question-type-' . $question->qtype
);
198 // Create the question editing form.
199 if ($wizardnow !== '') {
200 $mform = $qtypeobj->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
202 $mform = $qtypeobj->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
204 $toform = fullclone($question); // send the question object and a few more parameters to the form
205 $toform->category
= "{$category->id},{$category->contextid}";
206 $toform->scrollpos
= $scrollpos;
207 if ($formeditable && $id){
208 $toform->categorymoveto
= $toform->category
;
211 $toform->appendqnumstring
= $appendqnumstring;
212 $toform->returnurl
= $originalreturnurl;
213 $toform->makecopy
= $makecopy;
215 $toform->cmid
= $cm->id
;
216 $toform->courseid
= $cm->course
;
218 $toform->courseid
= $COURSE->id
;
221 $toform->inpopup
= $inpopup;
223 $mform->set_data($toform);
225 if ($mform->is_cancelled()) {
229 redirect($returnurl);
232 } else if ($fromform = $mform->get_data()) {
233 // If we are saving as a copy, break the connection to the old question.
236 $question->hidden
= 0; // Copies should not be hidden.
239 /// Process the combination of usecurrentcat, categorymoveto and category form
240 /// fields, so the save_question method only has to consider $fromform->category
241 if (!empty($fromform->usecurrentcat
)) {
242 // $fromform->category is the right category to save in.
244 if (!empty($fromform->categorymoveto
)) {
245 $fromform->category
= $fromform->categorymoveto
;
247 // $fromform->category is the right category to save in.
251 /// If we are moving a question, check we have permission to move it from
252 /// whence it came. (Where we are moving to is validated by the form.)
253 list($newcatid, $newcontextid) = explode(',', $fromform->category
);
254 if (!empty($question->id
) && $newcatid != $question->category
) {
255 $contextid = $newcontextid;
256 question_require_capability_on($question, 'move');
258 $contextid = $category->contextid
;
261 // Ensure we redirect back to the category the question is being saved into.
262 $returnurl->param('category', $fromform->category
);
264 // We are actually saving the question.
265 if (!empty($question->id
)) {
266 question_require_capability_on($question, 'edit');
268 require_capability('moodle/question:add', context
::instance_by_id($contextid));
269 if (!empty($fromform->makecopy
) && !$question->formoptions
->cansaveasnew
) {
270 print_error('nopermissions', '', '', 'edit');
274 // If this is a new question, save defaults for user in user_preferences table.
275 if (empty($question->id
)) {
276 $qtypeobj->save_defaults_for_new_questions($fromform);
278 $question = $qtypeobj->save_question($question, $fromform);
279 if (isset($fromform->tags
)) {
280 // If we have any question context level tags then set those tags now.
281 core_tag_tag
::set_item_tags('core_question', 'question', $question->id
,
282 context
::instance_by_id($contextid), $fromform->tags
, 0);
285 if (isset($fromform->coursetags
)) {
286 // If we have and course context level tags then set those now.
287 core_tag_tag
::set_item_tags('core_question', 'question', $question->id
,
288 context_course
::instance($fromform->courseid
), $fromform->coursetags
, 0);
291 // Purge this question from the cache.
292 question_bank
::notify_question_edited($question->id
);
294 // If we are saving and continuing to edit the question.
295 if (!empty($fromform->updatebutton
)) {
296 $url->param('id', $question->id
);
297 $url->remove_params('makecopy');
301 if ($qtypeobj->finished_edit_wizard($fromform)) {
303 echo $OUTPUT->notification(get_string('changessaved'), '');
306 $returnurl->param('lastchanged', $question->id
);
307 if ($appendqnumstring) {
308 $returnurl->param($appendqnumstring, $question->id
);
309 $returnurl->param('sesskey', sesskey());
310 $returnurl->param('cmid', $cmid);
312 redirect($returnurl);
316 $nexturlparams = array(
317 'returnurl' => $originalreturnurl,
318 'appendqnumstring' => $appendqnumstring,
319 'scrollpos' => $scrollpos);
320 if (isset($fromform->nextpageparam
) && is_array($fromform->nextpageparam
)){
321 //useful for passing data to the next page which is not saved in the database.
322 $nexturlparams +
= $fromform->nextpageparam
;
324 $nexturlparams['id'] = $question->id
;
325 $nexturlparams['wizardnow'] = $fromform->wizard
;
326 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
328 $nexturl->param('cmid', $cmid);
330 $nexturl->param('courseid', $COURSE->id
);
337 $streditingquestion = $qtypeobj->get_heading();
338 $PAGE->set_title($streditingquestion);
339 $PAGE->set_heading($COURSE->fullname
);
340 $PAGE->navbar
->add($streditingquestion);
342 // Display a heading, question editing form and possibly some extra content needed for
343 // for this question type.
344 echo $OUTPUT->header();
345 $qtypeobj->display_question_editing_page($mform, $question, $wizardnow);
346 echo $OUTPUT->footer();