Merge branch 'wip-MDL-23532-master' of git://github.com/samhemelryk/moodle
[moodle.git] / question / question.php
blob00414134b6fedae2011c8ff16694d9dd09f55ce0
1 <?php
2 /**
3 * Page for editing questions using the new form library.
5 * @author T.J.Hunt@open.ac.uk
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package questionbank
8 *//** */
10 // Includes.
11 require_once(dirname(__FILE__) . '/../config.php');
12 require_once(dirname(__FILE__) . '/editlib.php');
13 require_once($CFG->libdir . '/filelib.php');
14 require_once($CFG->libdir . '/formslib.php');
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);
20 $cmid = optional_param('cmid', 0, PARAM_INT);
21 $courseid = optional_param('courseid', 0, PARAM_INT);
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
25 $originalreturnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
26 $appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
27 $inpopup = optional_param('inpopup', 0, PARAM_BOOL);
29 $url = new moodle_url('/question/question.php');
30 if ($id !== 0) {
31 $url->param('id', $id);
33 if ($qtype !== '') {
34 $url->param('qtype', $qtype);
36 if ($categoryid !== 0) {
37 $url->param('category', $categoryid);
39 if ($cmid !== 0) {
40 $url->param('cmid', $cmid);
42 if ($courseid !== 0) {
43 $url->param('courseid', $courseid);
45 if ($wizardnow !== '') {
46 $url->param('wizardnow', $wizardnow);
48 if ($movecontext !== 0) {
49 $url->param('movecontext', $movecontext);
51 if ($originalreturnurl !== 0) {
52 $url->param('returnurl', $originalreturnurl);
54 if ($appendqnumstring !== '') {
55 $url->param('appendqnumstring', $appendqnumstring);
57 if ($inpopup !== 0) {
58 $url->param('inpopup', $inpopup);
60 $PAGE->set_url($url);
62 if ($originalreturnurl) {
63 if (strpos($originalreturnurl, '/') !== 0) {
64 throw new coding_exception("returnurl must be a local URL starting with '/'. $originalreturnurl was given.");
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));
73 if ($movecontext && !$id){
74 print_error('questiondoesnotexist', 'question', $returnurl);
77 if ($cmid){
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);
83 $PAGE->set_pagelayout('course');
84 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
85 $module = null;
86 $cm = null;
87 } else {
88 print_error('missingcourseorcmid', 'question');
90 $contexts = new question_edit_contexts($thiscontext);
92 if (optional_param('addcancel', false, PARAM_BOOL)) {
93 redirect($returnurl);
96 if ($id) {
97 if (!$question = $DB->get_record('question', array('id' => $id))) {
98 print_error('questiondoesnotexist', 'question', $returnurl);
100 get_question_options($question, true);
102 } else if ($categoryid && $qtype) { // only for creating new questions
103 $question = new stdClass;
104 $question->category = $categoryid;
105 $question->qtype = $qtype;
107 // Check that users are allowed to create this question type at the moment.
108 $allowedtypes = question_type_menu();
109 if (!isset($allowedtypes[$qtype])) {
110 print_error('cannotenable', 'question', $returnurl, $qtype);
113 } else if ($categoryid) {
114 // Category, but no qtype. They probably came from the addquestion.php
115 // script without choosing a question type. Send them back.
116 $addurl = new moodle_url('/question/addquestion.php', $url->params());
117 $addurl->param('validationerror', 1);
118 redirect($addurl);
120 } else {
121 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
124 // Validate the question category.
125 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
126 print_error('categorydoesnotexist', 'question', $returnurl);
129 // Check permissions
130 $question->formoptions = new stdClass;
132 $categorycontext = get_context_instance_by_id($category->contextid);
133 $addpermission = has_capability('moodle/question:add', $categorycontext);
135 if ($id) {
136 $canview = question_has_capability_on($question, 'view');
137 if ($movecontext){
138 $question->formoptions->canedit = false;
139 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
140 $question->formoptions->cansaveasnew = false;
141 $question->formoptions->repeatelements = false;
142 $question->formoptions->movecontext = true;
143 $formeditable = true;
144 question_require_capability_on($question, 'view');
145 } else {
146 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
147 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
148 $question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission);
149 $question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew);
150 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
151 $question->formoptions->movecontext = false;
152 if (!$formeditable){
153 question_require_capability_on($question, 'view');
157 } else { // creating a new question
158 require_capability('moodle/question:add', $categorycontext);
159 $formeditable = true;
160 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
161 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
162 $question->formoptions->repeatelements = true;
163 $question->formoptions->movecontext = false;
166 // Validate the question type.
167 if (!isset($QTYPES[$question->qtype])) {
168 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
170 $PAGE->set_pagetype('question-type-' . $question->qtype);
172 // Create the question editing form.
173 if ($wizardnow!=='' && !$movecontext){
174 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
175 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
176 } else {
177 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
179 } else {
180 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
182 if ($mform === null) {
183 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
185 $toform = fullclone($question); // send the question object and a few more parameters to the form
186 $toform->category = "$category->id,$category->contextid";
187 if ($formeditable && $id){
188 $toform->categorymoveto = $toform->category;
191 $toform->appendqnumstring = $appendqnumstring;
192 $toform->returnurl = $originalreturnurl;
193 $toform->movecontext = $movecontext;
194 if ($cm !== null){
195 $toform->cmid = $cm->id;
196 $toform->courseid = $cm->course;
197 } else {
198 $toform->courseid = $COURSE->id;
201 $toform->inpopup = $inpopup;
203 $mform->set_data($toform);
205 if ($mform->is_cancelled()){
206 if ($inpopup) {
207 close_window();
208 } else {
209 if (!empty($question->id)) {
210 $returnurl->param('lastchanged', $question->id);
212 redirect($returnurl->out(false));
214 } else if ($fromform = $mform->get_data()) {
215 /// If we are saving as a copy, break the connection to the old question.
216 if (!empty($fromform->makecopy)) {
217 $question->id = 0;
218 $question->hidden = 0; // Copies should not be hidden
221 /// Process the combination of usecurrentcat, categorymoveto and category form
222 /// fields, so the save_question method only has to consider $fromform->category
223 if (!empty($fromform->usecurrentcat)) {
224 // $fromform->category is the right category to save in.
225 } else {
226 if (!empty($fromform->categorymoveto)) {
227 $fromform->category = $fromform->categorymoveto;
228 } else {
229 // $fromform->category is the right category to save in.
233 /// If we are moving a question, check we have permission to move it from
234 /// whence it came. (Where we are moving to is validated by the form.)
235 list($newcatid) = explode(',', $fromform->category);
236 if (!empty($question->id) && $newcatid != $question->category) {
237 question_require_capability_on($question, 'move');
240 // Ensure we redirect back to the category the question is being saved into.
241 $returnurl->param('category', $fromform->category);
243 if ($movecontext) {
244 // We are just moving the question to a different context.
245 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
246 require_capability('moodle/question:add', get_context_instance_by_id($tocontextid));
247 question_move_questions_to_category(array($question->id), $tocatid);
249 } else {
250 // We are acutally saving the question.
251 $question = $QTYPES[$question->qtype]->save_question($question, $fromform);
252 if (!empty($CFG->usetags) && isset($fromform->tags)) {
253 // A wizardpage from multipe pages questiontype like calculated may not
254 // allow editing the question tags, hence the isset($fromform->tags) test.
255 require_once($CFG->dirroot.'/tag/lib.php');
256 tag_set('question', $question->id, $fromform->tags);
260 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext) {
261 if ($inpopup) {
262 echo $OUTPUT->notification(get_string('changessaved'), '');
263 close_window(3);
264 } else {
265 $returnurl->param('lastchanged', $question->id);
266 if ($appendqnumstring) {
267 $returnurl->param($appendqnumstring, $question->id);
268 $returnurl->param('sesskey', sesskey());
269 $returnurl->param('cmid', $cmid);
271 redirect($returnurl);
274 } else {
275 $nexturlparams = array(
276 'returnurl' => $originalreturnurl,
277 'appendqnumstring' => $appendqnumstring);
278 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
279 //useful for passing data to the next page which is not saved in the database.
280 $nexturlparams += $fromform->nextpageparam;
282 $nexturlparams['id'] = $question->id;
283 $nexturlparams['wizardnow'] = $fromform->wizard;
284 $nexturl = new moodle_url('/question/question.php', $nexturlparams);
285 if ($cmid){
286 $nexturl->param('cmid', $cmid);
287 } else {
288 $nexturl->param('courseid', $COURSE->id);
290 redirect($nexturl);
293 } else {
294 $streditingquestion = $QTYPES[$question->qtype]->get_heading();
295 $PAGE->set_title($streditingquestion);
296 $PAGE->set_heading($COURSE->fullname);
297 if ($cm !== null) {
298 $strmodule = get_string('modulename', $cm->modname);
299 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
300 $PAGE->navbar->add(get_string('modulenameplural', $cm->modname), new moodle_url('/mod/'.$cm->modname.'/index.php', array('id'=>$cm->course)));
301 $PAGE->navbar->add(format_string($module->name), new moodle_url('/mod/'.$cm->modname.'/view.php', array('id'=>$cm->id)));
302 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
303 //don't need this link if returnurl returns to view.php
304 $PAGE->navbar->add($streditingmodule, $returnurl);
306 $PAGE->navbar->add($streditingquestion);
307 echo $OUTPUT->header();
309 } else {
310 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
311 $PAGE->navbar->add(get_string('editquestions', 'quiz'), $returnurl);
312 $PAGE->navbar->add($streditingquestion);
313 echo $OUTPUT->header();
316 // Display a heading, question editing form and possibly some extra content needed for
317 // for this question type.
318 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
319 echo $OUTPUT->footer();