db scheme note for search engine
[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 {
44 error('Need to pass courseid or cmid to this script.');
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) {
56 if (!$question = get_record('question', 'id', $id)) {
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.
69if (!$category = get_record('question_categories', 'id', $question->category)) {
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}
add2d7ac 143$mform->set_data($toform);
144
145if ($mform->is_cancelled()){
146 redirect($returnurl);
ba18b21d 147} elseif ($fromform = $mform->get_data()){
271e6dec 148 $returnurl = new moodle_url($returnurl);
149 //select category that question has been saved in / moved to when we return to question bank
ba18b21d 150 if (!empty($fromform->categorymoveto)){
151 $returnurl->param('category', $fromform->categorymoveto);
152 } else if (!empty($fromform->category)){
153 $returnurl->param('category', $fromform->category);
271e6dec 154 }
155 $returnurl = $returnurl->out();
ba18b21d 156 if (!empty($fromform->makecopy)) {
add2d7ac 157 $question->id = 0; // causes a new question to be created.
158 $question->hidden = 0; // Copies should not be hidden
159 }
271e6dec 160 if ($movecontext){
ba18b21d 161 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
271e6dec 162 $tocontext = get_context_instance_by_id($tocontextid);
163 require_capability('moodle/question:add', $tocontext);
164 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
165 $movecontexturl = new moodle_url($CFG->wwwroot.'/question/contextmoveq.php',
166 array('returnurl' => $returnurl,
167 'ids'=>$question->id,
168 'tocatid'=> $tocatid));
169 if ($cmid){
170 $movecontexturl->param('cmid', $cmid);
171 } else {
172 $movecontexturl->param('courseid', $COURSE->id);
173 }
174 redirect($movecontexturl->out());
175 }
176 }
ba18b21d 177
178 $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow);
79bb7202 179 if (($QTYPES[$question->qtype]->finished_edit_wizard($fromform)) || $movecontext){
180 if ($inpopup) {
add2d7ac 181 notify(get_string('changessaved'), '');
182 close_window(3);
516cf3eb 183 } else {
561fe61d 184 redirect($returnurl);
516cf3eb 185 }
add2d7ac 186 } else {
79bb7202 187 $nexturlparams = array('returnurl'=>$returnurl);
188 if (isset($fromform->nextpageparam) && is_array($fromform->nextpageparam)){
189 $nexturlparams += $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
add2d7ac 190 }
79bb7202 191 $nexturlparams['id'] = $question->id;
ba18b21d 192 $nexturlparams['wizardnow'] = $fromform->wizard;
271e6dec 193 $nexturl = new moodle_url('question.php', $nexturlparams);
79bb7202 194 if ($cmid){
195 $nexturl->param('cmid', $cmid);
196 } else {
197 $nexturl->param('courseid', $COURSE->id);
198 }
199 redirect($nexturl->out());
516cf3eb 200 }
add2d7ac 201} else {
202
9ab75b2b 203 list($streditingquestion,) = $QTYPES[$question->qtype]->get_heading();
204 if ($cm !== null) {
271e6dec 205 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
206 ? update_module_button($cm->id, $cm->course, get_string('modulename', $cm->modname))
9ab75b2b 207 : "";
3b27b0fe 208 $navlinks = array();
37d00ed4 209 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
0cbddd85 210 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
3b27b0fe 211 $navlinks[] = array('name' => get_string('editingquiz', 'quiz'), 'link' => $returnurl, 'type' => 'title');
212 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
213 $navigation = build_navigation($navlinks);
9ab75b2b 214 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
271e6dec 215
8c2550c8 216 } else {
3b27b0fe 217 $navlinks = array();
218 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
219 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
af49b389 220 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
add2d7ac 221 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
3b27b0fe 222 $navigation = build_navigation($navlinks);
9ab75b2b 223 print_header_simple($streditingquestion, '', $navigation);
8c2550c8 224 }
516cf3eb 225
271e6dec 226
add2d7ac 227 // Display a heading, question editing form and possibly some extra content needed for
228 // for this question type.
229 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
add2d7ac 230 print_footer($COURSE);
231}
516cf3eb 232?>