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