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