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