MDL-19210 eliminate the generation of multiple dataitems having the same item number
[moodle.git] / question / question.php
CommitLineData
4323d029 1<?php // $Id$
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
271e6dec 29if ($movecontext && !$id){
30 print_error('questiondoesnotexist', 'question', $returnurl);
31}
add2d7ac 32
9ab75b2b 33if ($cmid){
271e6dec 34 list($module, $cm) = get_module_from_cmid($cmid);
35 require_login($cm->course, false, $cm);
36 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
37} elseif ($courseid) {
38 require_login($courseid, false);
39 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
9ab75b2b 40 $module = null;
41 $cm = null;
271e6dec 42} else {
0be2c858 43 print_error('missingcourseorcmid', 'question');
9ab75b2b 44}
271e6dec 45$contexts = new question_edit_contexts($thiscontext);
46
271e6dec 47if (!$returnurl) {
48 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
49}
50
cd120b23 51if (optional_param('addcancel', false, PARAM_BOOL)) {
52 redirect($returnurl);
53}
54
add2d7ac 55if ($id) {
f34488b2 56 if (!$question = $DB->get_record('question', array('id' => $id))) {
add2d7ac 57 print_error('questiondoesnotexist', 'question', $returnurl);
58 }
c599a682 59 get_question_options($question, true);
add2d7ac 60} else if ($categoryid && $qtype) { // only for creating new questions
61 $question = new stdClass;
62 $question->category = $categoryid;
63 $question->qtype = $qtype;
8ed358db 64
65 // Check that users are allowed to create this question type at the moment.
66 $allowedtypes = question_type_menu();
67 if (!isset($allowedtypes[$qtype])) {
68 print_error('cannotenable', 'question', $returnurl, $qtype);
69 }
add2d7ac 70} else {
71 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
72}
73
74// Validate the question category.
f34488b2 75if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
add2d7ac 76 print_error('categorydoesnotexist', 'question', $returnurl);
77}
271e6dec 78
677f62b6 79// Check permissions
c599a682 80$question->formoptions = new stdClass;
271e6dec 81
82$categorycontext = get_context_instance_by_id($category->contextid);
83$addpermission = has_capability('moodle/question:add', $categorycontext);
84
85if ($id) {
86 $canview = question_has_capability_on($question, 'view');
87 if ($movecontext){
88 $question->formoptions->canedit = false;
89 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
90 $question->formoptions->cansaveasnew = false;
91 $question->formoptions->repeatelements = false;
92 $question->formoptions->movecontext = true;
93 $formeditable = true;
94 question_require_capability_on($question, 'view');
95 } else {
96 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
97 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
98 $question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission);
99 $question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew);
100 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
101 $question->formoptions->movecontext = false;
102 if (!$formeditable){
103 question_require_capability_on($question, 'view');
104 }
105 }
106
271e6dec 107} else { // creating a new question
108 require_capability('moodle/question:add', $categorycontext);
109 $formeditable = true;
110 $question->formoptions->repeatelements = true;
111 $question->formoptions->movecontext = false;
add2d7ac 112}
113
114// Validate the question type.
115if (!isset($QTYPES[$question->qtype])) {
116 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
117}
d529807a 118$PAGE->set_pagetype('question-type-' . $question->qtype);
add2d7ac 119
add2d7ac 120// Create the question editing form.
271e6dec 121if ($wizardnow!=='' && !$movecontext){
add2d7ac 122 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
123 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 124 } else {
271e6dec 125 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 126 }
127} else {
271e6dec 128 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 129}
add2d7ac 130if ($mform === null) {
131 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
132}
ba18b21d 133$toform = fullclone($question); // send the question object and a few more parameters to the form
134$toform->category = "$category->id,$category->contextid";
135if ($formeditable && $id){
136 $toform->categorymoveto = $toform->category;
137}
fa583f5f 138
139$toform->appendqnumstring = $appendqnumstring;
add2d7ac 140$toform->returnurl = $returnurl;
271e6dec 141$toform->movecontext = $movecontext;
9ab75b2b 142if ($cm !== null){
143 $toform->cmid = $cm->id;
271e6dec 144 $toform->courseid = $cm->course;
145} else {
146 $toform->courseid = $COURSE->id;
9ab75b2b 147}
11a60967 148$toform->inpopup = $inpopup;
add2d7ac 149$mform->set_data($toform);
150
151if ($mform->is_cancelled()){
11a60967 152 if ($inpopup) {
153 close_window();
154 } else {
155 redirect($returnurl);
156 }
24e8b9b6 157} elseif ($fromform = $mform->get_data()) {
158 /// If we are saving as a copy, break the connection to the old question.
ba18b21d 159 if (!empty($fromform->makecopy)) {
24e8b9b6 160 $question->id = 0;
add2d7ac 161 $question->hidden = 0; // Copies should not be hidden
162 }
24e8b9b6 163
164 /// Process the combination of usecurrentcat, categorymoveto and category form
165 /// fields, so the save_question method only has to consider $fromform->category
166 if (!empty($fromform->usecurrentcat)) {
167 // $fromform->category is the right category to save in.
168 } else {
169 if (!empty($fromform->categorymoveto)) {
170 $fromform->category = $fromform->categorymoveto;
171 } else {
172 // $fromform->category is the right category to save in.
173 }
174 }
175
176 /// If we are moving a question, check we have permission to move it from
177 /// whence it came. (Where we are moving to is validated by the form.)
178 list($newcatid) = explode(',', $fromform->category);
179 if (!empty($question->id) && $newcatid != $question->category) {
180 question_require_capability_on($question, 'move');
181 }
182
183 /// Ensure we redirect back to the category the question is being saved into.
184 $returnurl = new moodle_url($returnurl);
185 $returnurl->param('category', $fromform->category);
186 $returnurl = $returnurl->out();
187
188 /// Call the appropriate method.
189 if ($movecontext) {
ba18b21d 190 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
271e6dec 191 $tocontext = get_context_instance_by_id($tocontextid);
192 require_capability('moodle/question:add', $tocontext);
193 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
194 $movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
195 array('returnurl' => $returnurl,
196 'ids'=>$question->id,
197 'tocatid'=> $tocatid));
198 if ($cmid){
199 $movecontexturl->param('cmid', $cmid);
200 } else {
201 $movecontexturl->param('courseid', $COURSE->id);
202 }
203 redirect($movecontexturl->out());
204 }
205 }
ba18b21d 206
c599a682 207 $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow, true);
208 if (!empty($CFG->usetags)) {
209 require_once($CFG->dirroot.'/tag/lib.php');
210 tag_set('question', $question->id, $fromform->tags);
211 }
212
79bb7202 213 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
214 if ($inpopup) {
add2d7ac 215 notify(get_string('changessaved'), '');
216 close_window(3);
516cf3eb 217 } else {
fa583f5f 218 if($appendqnumstring){
219 $returnurl_object=new moodle_url($returnurl);
220 $returnurl=$returnurl_object->out(false,array($appendqnumstring=>($question->id), "sesskey"=>sesskey(), "cmid"=>$cmid));
221 }
561fe61d 222 redirect($returnurl);
516cf3eb 223 }
add2d7ac 224 } else {
fa583f5f 225 $nexturlparams = array('returnurl'=>$returnurl, 'appendqnumstring'=>$appendqnumstring);
79bb7202 226 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
227 $nexturlparams += $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
add2d7ac 228 }
79bb7202 229 $nexturlparams['id'] = $question->id;
ba18b21d 230 $nexturlparams['wizardnow'] = $fromform->wizard;
271e6dec 231 $nexturl = new moodle_url('question.php', $nexturlparams);
79bb7202 232 if ($cmid){
233 $nexturl->param('cmid', $cmid);
234 } else {
235 $nexturl->param('courseid', $COURSE->id);
236 }
237 redirect($nexturl->out());
516cf3eb 238 }
add2d7ac 239} else {
240
cd120b23 241 $streditingquestion = $QTYPES[$question->qtype]->get_heading();
50da63eb 242 $headtags = get_editing_head_contributions($question);
9ab75b2b 243 if ($cm !== null) {
479cca8a 244 $strmodule = get_string('modulename', $cm->modname);
271e6dec 245 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
479cca8a 246 ? update_module_button($cm->id, $cm->course, $strmodule)
9ab75b2b 247 : "";
479cca8a 248
249 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
250
3b27b0fe 251 $navlinks = array();
37d00ed4 252 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
0cbddd85 253 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
479cca8a 254 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
255 //don't need this link if returnurl returns to view.php
256 $navlinks[] = array('name' => $streditingmodule, 'link' => $returnurl, 'type' => 'title');
257 }
3b27b0fe 258 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
259 $navigation = build_navigation($navlinks);
50da63eb 260 print_header_simple($streditingquestion, '', $navigation, '', $headtags, true, $strupdatemodule);
271e6dec 261
8c2550c8 262 } else {
3b27b0fe 263 $navlinks = array();
264 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
265 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
af49b389 266 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
add2d7ac 267 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
3b27b0fe 268 $navigation = build_navigation($navlinks);
50da63eb 269 print_header_simple($streditingquestion, '', $navigation, '', $headtags);
8c2550c8 270 }
516cf3eb 271
add2d7ac 272 // Display a heading, question editing form and possibly some extra content needed for
273 // for this question type.
274 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
add2d7ac 275 print_footer($COURSE);
276}
516cf3eb 277?>