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