3 * Page for editing questions using the new form library.
5 * @author T.J.Hunt@open.ac.uk
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package questionbank
11 require_once(dirname(__FILE__
) . '/../config.php');
12 require_once(dirname(__FILE__
) . '/editlib.php');
13 require_once($CFG->libdir
. '/filelib.php');
14 require_once($CFG->libdir
. '/formslib.php');
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
);
20 $cmid = optional_param('cmid', 0, PARAM_INT
);
21 $courseid = optional_param('courseid', 0, PARAM_INT
);
22 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA
);
23 $movecontext = optional_param('movecontext', 0, PARAM_BOOL
); // Switch to make
24 // question uneditable - form is displayed to edit category only
25 $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL
);
26 $appendqnumstring = optional_param('appendqnumstring', '', PARAM_ALPHA
);
27 $inpopup = optional_param('inpopup', 0, PARAM_BOOL
);
29 $url = new moodle_url('/question/question.php');
31 $url->param('id', $id);
34 $url->param('qtype', $qtype);
36 if ($categoryid !== 0) {
37 $url->param('category', $categoryid);
40 $url->param('cmid', $cmid);
42 if ($courseid !== 0) {
43 $url->param('courseid', $courseid);
45 if ($wizardnow !== '') {
46 $url->param('wizardnow', $wizardnow);
48 if ($movecontext !== 0) {
49 $url->param('movecontext', $movecontext);
51 if ($returnurl !== 0) {
52 $url->param('returnurl', $returnurl);
54 if ($appendqnumstring !== '') {
55 $url->param('appendqnumstring', $appendqnumstring);
58 $url->param('inpopup', $inpopup);
62 if ($movecontext && !$id){
63 print_error('questiondoesnotexist', 'question', $returnurl);
67 list($module, $cm) = get_module_from_cmid($cmid);
68 require_login($cm->course
, false, $cm);
69 $thiscontext = get_context_instance(CONTEXT_MODULE
, $cmid);
70 } elseif ($courseid) {
71 require_login($courseid, false);
72 $thiscontext = get_context_instance(CONTEXT_COURSE
, $courseid);
76 print_error('missingcourseorcmid', 'question');
78 $contexts = new question_edit_contexts($thiscontext);
81 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
84 if (optional_param('addcancel', false, PARAM_BOOL
)) {
89 if (!$question = $DB->get_record('question', array('id' => $id))) {
90 print_error('questiondoesnotexist', 'question', $returnurl);
92 get_question_options($question, true);
94 } else if ($categoryid && $qtype) { // only for creating new questions
95 $question = new stdClass
;
96 $question->category
= $categoryid;
97 $question->qtype
= $qtype;
99 // Check that users are allowed to create this question type at the moment.
100 $allowedtypes = question_type_menu();
101 if (!isset($allowedtypes[$qtype])) {
102 print_error('cannotenable', 'question', $returnurl, $qtype);
105 } else if ($categoryid) {
106 // Category, but no qtype. They probably came from the addquestion.php
107 // script without choosing a question type. Send them back.
108 $addurl = new moodle_url('/question/addquestion.php', $url->params());
109 $addurl->param('validationerror', 1);
113 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
116 // Validate the question category.
117 if (!$category = $DB->get_record('question_categories', array('id' => $question->category
))) {
118 print_error('categorydoesnotexist', 'question', $returnurl);
122 $question->formoptions
= new stdClass
;
124 $categorycontext = get_context_instance_by_id($category->contextid
);
125 $addpermission = has_capability('moodle/question:add', $categorycontext);
128 $canview = question_has_capability_on($question, 'view');
130 $question->formoptions
->canedit
= false;
131 $question->formoptions
->canmove
= (question_has_capability_on($question, 'move') && $contexts->have_cap('moodle/question:add'));
132 $question->formoptions
->cansaveasnew
= false;
133 $question->formoptions
->repeatelements
= false;
134 $question->formoptions
->movecontext
= true;
135 $formeditable = true;
136 question_require_capability_on($question, 'view');
138 $question->formoptions
->canedit
= question_has_capability_on($question, 'edit');
139 $question->formoptions
->canmove
= (question_has_capability_on($question, 'move') && $addpermission);
140 $question->formoptions
->cansaveasnew
= (($canview ||
question_has_capability_on($question, 'edit')) && $addpermission);
141 $question->formoptions
->repeatelements
= ($question->formoptions
->canedit ||
$question->formoptions
->cansaveasnew
);
142 $formeditable = $question->formoptions
->canedit ||
$question->formoptions
->cansaveasnew ||
$question->formoptions
->canmove
;
143 $question->formoptions
->movecontext
= false;
145 question_require_capability_on($question, 'view');
149 } else { // creating a new question
150 require_capability('moodle/question:add', $categorycontext);
151 $formeditable = true;
152 $question->formoptions
->repeatelements
= true;
153 $question->formoptions
->movecontext
= false;
156 // Validate the question type.
157 if (!isset($QTYPES[$question->qtype
])) {
158 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype
);
160 $PAGE->set_pagetype('question-type-' . $question->qtype
);
162 // Create the question editing form.
163 if ($wizardnow!=='' && !$movecontext){
164 if (!method_exists($QTYPES[$question->qtype
], 'next_wizard_form')){
165 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
167 $mform = $QTYPES[$question->qtype
]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
170 $mform = $QTYPES[$question->qtype
]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
172 if ($mform === null) {
173 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype
.'"');
175 $toform = fullclone($question); // send the question object and a few more parameters to the form
176 $toform->category
= "$category->id,$category->contextid";
177 if ($formeditable && $id){
178 $toform->categorymoveto
= $toform->category
;
181 $toform->appendqnumstring
= $appendqnumstring;
182 $toform->returnurl
= $returnurl;
183 $toform->movecontext
= $movecontext;
185 $toform->cmid
= $cm->id
;
186 $toform->courseid
= $cm->course
;
188 $toform->courseid
= $COURSE->id
;
190 $toform->inpopup
= $inpopup;
191 $mform->set_data($toform);
193 if ($mform->is_cancelled()){
197 $nexturl = new moodle_url($returnurl);
198 $nexturl->param('lastchanged', $question->id
);
199 redirect($nexturl->out());
201 } elseif ($fromform = $mform->get_data()) {
202 /// If we are saving as a copy, break the connection to the old question.
203 if (!empty($fromform->makecopy
)) {
205 $question->hidden
= 0; // Copies should not be hidden
208 /// Process the combination of usecurrentcat, categorymoveto and category form
209 /// fields, so the save_question method only has to consider $fromform->category
210 if (!empty($fromform->usecurrentcat
)) {
211 // $fromform->category is the right category to save in.
213 if (!empty($fromform->categorymoveto
)) {
214 $fromform->category
= $fromform->categorymoveto
;
216 // $fromform->category is the right category to save in.
220 /// If we are moving a question, check we have permission to move it from
221 /// whence it came. (Where we are moving to is validated by the form.)
222 list($newcatid) = explode(',', $fromform->category
);
223 if (!empty($question->id
) && $newcatid != $question->category
) {
224 question_require_capability_on($question, 'move');
227 /// Ensure we redirect back to the category the question is being saved into.
228 $returnurl = new moodle_url($returnurl);
229 $returnurl->param('category', $fromform->category
);
230 // TODO: it is sloppy to pass arounf full URLs through page parameters and some servers do not like that
231 $returnurl = $returnurl->out(false);
233 /// Call the appropriate method.
235 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto
);
236 $tocontext = get_context_instance_by_id($tocontextid);
237 require_capability('moodle/question:add', $tocontext);
238 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
239 $movecontexturl = new moodle_url('/question/contextmoveq.php',
240 array('returnurl' => $returnurl,
241 'ids'=>$question->id
,
242 'tocatid'=> $tocatid));
244 $movecontexturl->param('cmid', $cmid);
246 $movecontexturl->param('courseid', $COURSE->id
);
248 redirect($movecontexturl);
252 $question = $QTYPES[$question->qtype
]->save_question($question, $fromform, $COURSE, $wizardnow, true);
253 // a wizardpage from multipe pages questiontype like calculated may not allow editing the question tags
254 if (!empty($CFG->usetags
) && isset($fromform->tags
)) {
255 require_once($CFG->dirroot
.'/tag/lib.php');
256 tag_set('question', $question->id
, $fromform->tags
);
259 if (($QTYPES[$question->qtype
]->finished_edit_wizard($fromform)) ||
$movecontext){
261 echo $OUTPUT->notification(get_string('changessaved'), '');
264 $nexturl = new moodle_url($returnurl);
265 $nexturl->param('lastchanged', $question->id
);
266 if($appendqnumstring) {
267 $nexturl->params(array($appendqnumstring=>($question->id
), "sesskey"=>sesskey(), "cmid"=>$cmid));
272 $nexturlparams = array('returnurl'=>$returnurl, 'appendqnumstring'=>$appendqnumstring);
273 if (isset($fromform->nextpageparam
) && is_array($fromform->nextpageparam
)){
274 $nexturlparams +
= $fromform->nextpageparam
;//useful for passing data to the next page which is not saved in the database
276 $nexturlparams['id'] = $question->id
;
277 $nexturlparams['wizardnow'] = $fromform->wizard
;
278 $nexturl = new moodle_url('question.php', $nexturlparams);
280 $nexturl->param('cmid', $cmid);
282 $nexturl->param('courseid', $COURSE->id
);
288 $streditingquestion = $QTYPES[$question->qtype
]->get_heading();
289 $PAGE->set_title($streditingquestion);
290 $PAGE->set_heading($COURSE->fullname
);
292 $strmodule = get_string('modulename', $cm->modname
);
293 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
294 $PAGE->navbar
->add(get_string('modulenameplural', $cm->modname
), new moodle_url('/mod/'.$cm->modname
.'/index.php', array('id'=>$cm->course
)));
295 $PAGE->navbar
->add(format_string($module->name
), new moodle_url('/mod/'.$cm->modname
.'/view.php', array('id'=>$cm->id
)));
296 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
297 //don't need this link if returnurl returns to view.php
298 $PAGE->navbar
->add($streditingmodule, $returnurl);
300 $PAGE->navbar
->add($streditingquestion);
301 echo $OUTPUT->header();
304 $strediting = '<a href="edit.php?courseid='.$COURSE->id
.'">'.get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
305 $PAGE->navbar
->add(get_string('editquestions', "quiz"), $returnurl);
306 $PAGE->navbar
->add($streditingquestion);
307 echo $OUTPUT->header();
310 // Display a heading, question editing form and possibly some extra content needed for
311 // for this question type.
312 $QTYPES[$question->qtype
]->display_question_editing_page($mform, $question, $wizardnow);
313 echo $OUTPUT->footer();