formslib / tags: MDL-17889 debugging message if you try to create a tags field when...
[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
21$cmid = optional_param('cmid', 0, PARAM_INT);
22$courseid = optional_param('courseid', 0, PARAM_INT);
add2d7ac 23$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
271e6dec 24$movecontext = optional_param('movecontext', 0, PARAM_BOOL);//switch to make question
ba18b21d 25 //uneditable - form is displayed to edit category only
271e6dec 26$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
fa583f5f 27$appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA);
271e6dec 28
79bb7202 29$inpopup = optional_param('inpopup', 0, PARAM_BOOL);
30
271e6dec 31if ($movecontext && !$id){
32 print_error('questiondoesnotexist', 'question', $returnurl);
33}
add2d7ac 34
9ab75b2b 35if ($cmid){
271e6dec 36 list($module, $cm) = get_module_from_cmid($cmid);
37 require_login($cm->course, false, $cm);
38 $thiscontext = get_context_instance(CONTEXT_MODULE, $cmid);
39} elseif ($courseid) {
40 require_login($courseid, false);
41 $thiscontext = get_context_instance(CONTEXT_COURSE, $courseid);
9ab75b2b 42 $module = null;
43 $cm = null;
271e6dec 44} else {
0be2c858 45 print_error('missingcourseorcmid', 'question');
9ab75b2b 46}
271e6dec 47$contexts = new question_edit_contexts($thiscontext);
48
49
50if (!$returnurl) {
51 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
52}
53
54
9ab75b2b 55
add2d7ac 56if ($id) {
f34488b2 57 if (!$question = $DB->get_record('question', array('id' => $id))) {
add2d7ac 58 print_error('questiondoesnotexist', 'question', $returnurl);
59 }
60 get_question_options($question);
61} else if ($categoryid && $qtype) { // only for creating new questions
62 $question = new stdClass;
63 $question->category = $categoryid;
64 $question->qtype = $qtype;
65} else {
66 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
67}
68
69// Validate the question category.
f34488b2 70if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
add2d7ac 71 print_error('categorydoesnotexist', 'question', $returnurl);
72}
271e6dec 73
74//permissions
75$question->formoptions = new object();
271e6dec 76
77$categorycontext = get_context_instance_by_id($category->contextid);
78$addpermission = has_capability('moodle/question:add', $categorycontext);
79
80if ($id) {
81 $canview = question_has_capability_on($question, 'view');
82 if ($movecontext){
83 $question->formoptions->canedit = false;
84 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
85 $question->formoptions->cansaveasnew = false;
86 $question->formoptions->repeatelements = false;
87 $question->formoptions->movecontext = true;
88 $formeditable = true;
89 question_require_capability_on($question, 'view');
90 } else {
91 $question->formoptions->canedit = question_has_capability_on($question, 'edit');
92 $question->formoptions->canmove = (question_has_capability_on($question, 'move') && $addpermission);
93 $question->formoptions->cansaveasnew = (($canview ||question_has_capability_on($question, 'edit')) && $addpermission);
94 $question->formoptions->repeatelements = ($question->formoptions->canedit || $question->formoptions->cansaveasnew);
95 $formeditable = $question->formoptions->canedit || $question->formoptions->cansaveasnew || $question->formoptions->canmove;
96 $question->formoptions->movecontext = false;
97 if (!$formeditable){
98 question_require_capability_on($question, 'view');
99 }
100 }
101
102
103} else { // creating a new question
104 require_capability('moodle/question:add', $categorycontext);
105 $formeditable = true;
106 $question->formoptions->repeatelements = true;
107 $question->formoptions->movecontext = false;
add2d7ac 108}
109
ba18b21d 110
add2d7ac 111// Validate the question type.
112if (!isset($QTYPES[$question->qtype])) {
113 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
114}
115$CFG->pagepath = 'question/type/' . $question->qtype;
116
add2d7ac 117
118// Create the question editing form.
271e6dec 119if ($wizardnow!=='' && !$movecontext){
add2d7ac 120 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
121 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 122 } else {
271e6dec 123 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 124 }
125} else {
271e6dec 126 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 127}
add2d7ac 128if ($mform === null) {
129 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
130}
ba18b21d 131$toform = fullclone($question); // send the question object and a few more parameters to the form
132$toform->category = "$category->id,$category->contextid";
133if ($formeditable && $id){
134 $toform->categorymoveto = $toform->category;
135}
fa583f5f 136
137$toform->appendqnumstring = $appendqnumstring;
add2d7ac 138$toform->returnurl = $returnurl;
271e6dec 139$toform->movecontext = $movecontext;
9ab75b2b 140if ($cm !== null){
141 $toform->cmid = $cm->id;
271e6dec 142 $toform->courseid = $cm->course;
143} else {
144 $toform->courseid = $COURSE->id;
9ab75b2b 145}
11a60967 146$toform->inpopup = $inpopup;
add2d7ac 147$mform->set_data($toform);
148
149if ($mform->is_cancelled()){
11a60967 150 if ($inpopup) {
151 close_window();
152 } else {
153 redirect($returnurl);
154 }
ba18b21d 155} elseif ($fromform = $mform->get_data()){
271e6dec 156 $returnurl = new moodle_url($returnurl);
157 //select category that question has been saved in / moved to when we return to question bank
ba18b21d 158 if (!empty($fromform->categorymoveto)){
159 $returnurl->param('category', $fromform->categorymoveto);
160 } else if (!empty($fromform->category)){
161 $returnurl->param('category', $fromform->category);
271e6dec 162 }
163 $returnurl = $returnurl->out();
ba18b21d 164 if (!empty($fromform->makecopy)) {
add2d7ac 165 $question->id = 0; // causes a new question to be created.
166 $question->hidden = 0; // Copies should not be hidden
167 }
271e6dec 168 if ($movecontext){
ba18b21d 169 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
271e6dec 170 $tocontext = get_context_instance_by_id($tocontextid);
171 require_capability('moodle/question:add', $tocontext);
172 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
173 $movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
174 array('returnurl' => $returnurl,
175 'ids'=>$question->id,
176 'tocatid'=> $tocatid));
177 if ($cmid){
178 $movecontexturl->param('cmid', $cmid);
179 } else {
180 $movecontexturl->param('courseid', $COURSE->id);
181 }
182 redirect($movecontexturl->out());
183 }
184 }
ba18b21d 185
186 $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow);
79bb7202 187 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
188 if ($inpopup) {
add2d7ac 189 notify(get_string('changessaved'), '');
190 close_window(3);
516cf3eb 191 } else {
fa583f5f 192 if($appendqnumstring){
193 $returnurl_object=new moodle_url($returnurl);
194 $returnurl=$returnurl_object->out(false,array($appendqnumstring=>($question->id), "sesskey"=>sesskey(), "cmid"=>$cmid));
195 }
561fe61d 196 redirect($returnurl);
516cf3eb 197 }
add2d7ac 198 } else {
fa583f5f 199 $nexturlparams = array('returnurl'=>$returnurl, 'appendqnumstring'=>$appendqnumstring);
79bb7202 200 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
201 $nexturlparams += $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
add2d7ac 202 }
79bb7202 203 $nexturlparams['id'] = $question->id;
ba18b21d 204 $nexturlparams['wizardnow'] = $fromform->wizard;
271e6dec 205 $nexturl = new moodle_url('question.php', $nexturlparams);
79bb7202 206 if ($cmid){
207 $nexturl->param('cmid', $cmid);
208 } else {
209 $nexturl->param('courseid', $COURSE->id);
210 }
211 redirect($nexturl->out());
516cf3eb 212 }
add2d7ac 213} else {
214
9ab75b2b 215 list($streditingquestion,) = $QTYPES[$question->qtype]->get_heading();
216 if ($cm !== null) {
479cca8a 217 $strmodule = get_string('modulename', $cm->modname);
271e6dec 218 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
479cca8a 219 ? update_module_button($cm->id, $cm->course, $strmodule)
9ab75b2b 220 : "";
479cca8a 221
222 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
223
3b27b0fe 224 $navlinks = array();
37d00ed4 225 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
0cbddd85 226 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
479cca8a 227 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
228 //don't need this link if returnurl returns to view.php
229 $navlinks[] = array('name' => $streditingmodule, 'link' => $returnurl, 'type' => 'title');
230 }
3b27b0fe 231 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
232 $navigation = build_navigation($navlinks);
9ab75b2b 233 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
271e6dec 234
8c2550c8 235 } else {
3b27b0fe 236 $navlinks = array();
237 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
238 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
af49b389 239 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
add2d7ac 240 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
3b27b0fe 241 $navigation = build_navigation($navlinks);
9ab75b2b 242 print_header_simple($streditingquestion, '', $navigation);
8c2550c8 243 }
516cf3eb 244
271e6dec 245
add2d7ac 246 // Display a heading, question editing form and possibly some extra content needed for
247 // for this question type.
248 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
add2d7ac 249 print_footer($COURSE);
250}
516cf3eb 251?>