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