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