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