MDL-10383 - some more refactoring/cleanup, enablegroupings experimental admin setting...
[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
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();
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
99
100} else { // creating a new question
101 require_capability('moodle/question:add', $categorycontext);
102 $formeditable = true;
103 $question->formoptions->repeatelements = true;
104 $question->formoptions->movecontext = false;
add2d7ac 105}
106
ba18b21d 107
add2d7ac 108// Validate the question type.
109if (!isset($QTYPES[$question->qtype])) {
110 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
111}
112$CFG->pagepath = 'question/type/' . $question->qtype;
113
add2d7ac 114
115// Create the question editing form.
271e6dec 116if ($wizardnow!=='' && !$movecontext){
add2d7ac 117 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
118 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 119 } else {
271e6dec 120 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
add2d7ac 121 }
122} else {
271e6dec 123 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
add2d7ac 124}
add2d7ac 125if ($mform === null) {
126 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
127}
ba18b21d 128$toform = fullclone($question); // send the question object and a few more parameters to the form
129$toform->category = "$category->id,$category->contextid";
130if ($formeditable && $id){
131 $toform->categorymoveto = $toform->category;
132}
add2d7ac 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);
ba18b21d 145} elseif ($fromform = $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
ba18b21d 148 if (!empty($fromform->categorymoveto)){
149 $returnurl->param('category', $fromform->categorymoveto);
150 } else if (!empty($fromform->category)){
151 $returnurl->param('category', $fromform->category);
271e6dec 152 }
153 $returnurl = $returnurl->out();
ba18b21d 154 if (!empty($fromform->makecopy)) {
add2d7ac 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){
ba18b21d 159 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto);
271e6dec 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 }
ba18b21d 175
176 $question = $QTYPES[$question->qtype]->save_question($question, $fromform, $COURSE, $wizardnow);
177 if ($QTYPES[$qtype]->finished_edit_wizard($fromform) || $movecontext){
271e6dec 178
add2d7ac 179 if (optional_param('inpopup', 0, PARAM_BOOL)) {
180 notify(get_string('changessaved'), '');
181 close_window(3);
516cf3eb 182 } else {
561fe61d 183 redirect($returnurl);
516cf3eb 184 }
add2d7ac 185 } else {
271e6dec 186 $nexturlparams = array('returnurl'=>$returnurl)
ba18b21d 187 + $fromform->nextpageparam;//useful for passing data to the next page which is not saved in the database
add2d7ac 188 if ($question->id) {
271e6dec 189 $nexturlparams['id'] = $question->id;
add2d7ac 190 } else { // only for creating new questions
271e6dec 191 $nexturlparams['category'] = $question->category;
192 $nexturlparams['qtype'] =$question->qtype;
add2d7ac 193 }
ba18b21d 194 $nexturlparams['wizardnow'] = $fromform->wizard;
271e6dec 195 $nexturl = new moodle_url('question.php', $nexturlparams);
196 redirect($nexturl);
516cf3eb 197 }
add2d7ac 198} else {
199
9ab75b2b 200 list($streditingquestion,) = $QTYPES[$question->qtype]->get_heading();
201 if ($cm !== null) {
271e6dec 202 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $COURSE->id))
203 ? update_module_button($cm->id, $cm->course, get_string('modulename', $cm->modname))
9ab75b2b 204 : "";
3b27b0fe 205 $navlinks = array();
37d00ed4 206 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
0cbddd85 207 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
3b27b0fe 208 $navlinks[] = array('name' => get_string('editingquiz', 'quiz'), 'link' => $returnurl, 'type' => 'title');
209 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
210 $navigation = build_navigation($navlinks);
9ab75b2b 211 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
271e6dec 212
8c2550c8 213 } else {
3b27b0fe 214 $navlinks = array();
215 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
216 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
af49b389 217 $strediting = '<a href="edit.php?courseid='.$COURSE->id.'">'.
add2d7ac 218 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
3b27b0fe 219 $navigation = build_navigation($navlinks);
9ab75b2b 220 print_header_simple($streditingquestion, '', $navigation);
8c2550c8 221 }
516cf3eb 222
271e6dec 223
add2d7ac 224 // Display a heading, question editing form and possibly some extra content needed for
225 // for this question type.
226 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
add2d7ac 227 print_footer($COURSE);
228}
516cf3eb 229?>