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
);
21 $cmid = optional_param('cmid', 0, PARAM_INT
);
22 $courseid = optional_param('courseid', 0, PARAM_INT
);
23 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA
);
24 $movecontext = optional_param('movecontext', 0, PARAM_BOOL
);//switch to make question
25 //uneditable - form is displayed to edit category only
26 $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL
);
28 $inpopup = optional_param('inpopup', 0, PARAM_BOOL
);
30 if ($movecontext && !$id){
31 print_error('questiondoesnotexist', 'question', $returnurl);
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);
44 error('Need to pass courseid or cmid to this script.');
46 $contexts = new question_edit_contexts($thiscontext);
50 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$COURSE->id}";
56 if (!$question = get_record('question', 'id', $id)) {
57 print_error('questiondoesnotexist', 'question', $returnurl);
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;
65 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
68 // Validate the question category.
69 if (!$category = get_record('question_categories', 'id', $question->category
)) {
70 print_error('categorydoesnotexist', 'question', $returnurl);
74 $question->formoptions
= new object();
76 $categorycontext = get_context_instance_by_id($category->contextid
);
77 $addpermission = has_capability('moodle/question:add', $categorycontext);
80 $canview = question_has_capability_on($question, 'view');
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;
88 question_require_capability_on($question, 'view');
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;
97 question_require_capability_on($question, 'view');
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;
110 // Validate the question type.
111 if (!isset($QTYPES[$question->qtype
])) {
112 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype
);
114 $CFG->pagepath
= 'question/type/' . $question->qtype
;
117 // Create the question editing form.
118 if ($wizardnow!=='' && !$movecontext){
119 if (!method_exists($QTYPES[$question->qtype
], 'next_wizard_form')){
120 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
122 $mform = $QTYPES[$question->qtype
]->next_wizard_form('question.php', $question, $wizardnow, $formeditable);
125 $mform = $QTYPES[$question->qtype
]->create_editing_form('question.php', $question, $category, $contexts, $formeditable);
127 if ($mform === null) {
128 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype
.'"');
130 $toform = fullclone($question); // send the question object and a few more parameters to the form
131 $toform->category
= "$category->id,$category->contextid";
132 if ($formeditable && $id){
133 $toform->categorymoveto
= $toform->category
;
135 $toform->returnurl
= $returnurl;
136 $toform->movecontext
= $movecontext;
138 $toform->cmid
= $cm->id
;
139 $toform->courseid
= $cm->course
;
141 $toform->courseid
= $COURSE->id
;
143 $toform->inpopup
= $inpopup;
144 $mform->set_data($toform);
146 if ($mform->is_cancelled()){
150 redirect($returnurl);
152 } elseif ($fromform = $mform->get_data()){
153 $returnurl = new moodle_url($returnurl);
154 //select category that question has been saved in / moved to when we return to question bank
155 if (!empty($fromform->categorymoveto
)){
156 $returnurl->param('category', $fromform->categorymoveto
);
157 } else if (!empty($fromform->category
)){
158 $returnurl->param('category', $fromform->category
);
160 $returnurl = $returnurl->out();
161 if (!empty($fromform->makecopy
)) {
162 $question->id
= 0; // causes a new question to be created.
163 $question->hidden
= 0; // Copies should not be hidden
166 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto
);
167 $tocontext = get_context_instance_by_id($tocontextid);
168 require_capability('moodle/question:add', $tocontext);
169 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
170 $movecontexturl = new moodle_url($CFG->wwwroot
.'/question/contextmoveq.php',
171 array('returnurl' => $returnurl,
172 'ids'=>$question->id
,
173 'tocatid'=> $tocatid));
175 $movecontexturl->param('cmid', $cmid);
177 $movecontexturl->param('courseid', $COURSE->id
);
179 redirect($movecontexturl->out());
183 $question = $QTYPES[$question->qtype
]->save_question($question, $fromform, $COURSE, $wizardnow);
184 if (($QTYPES[$question->qtype
]->finished_edit_wizard($fromform)) ||
$movecontext){
186 notify(get_string('changessaved'), '');
189 redirect($returnurl);
192 $nexturlparams = array('returnurl'=>$returnurl);
193 if (isset($fromform->nextpageparam
) && is_array($fromform->nextpageparam
)){
194 $nexturlparams +
= $fromform->nextpageparam
;//useful for passing data to the next page which is not saved in the database
196 $nexturlparams['id'] = $question->id
;
197 $nexturlparams['wizardnow'] = $fromform->wizard
;
198 $nexturl = new moodle_url('question.php', $nexturlparams);
200 $nexturl->param('cmid', $cmid);
202 $nexturl->param('courseid', $COURSE->id
);
204 redirect($nexturl->out());
208 list($streditingquestion,) = $QTYPES[$question->qtype
]->get_heading();
210 $strmodule = get_string('modulename', $cm->modname
);
211 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE
, $COURSE->id
))
212 ?
update_module_button($cm->id
, $cm->course
, $strmodule)
215 $streditingmodule = get_string('editinga', 'moodle', $strmodule);
218 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
219 $navlinks[] = array('name' => format_string($module->name
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
220 if (stripos($returnurl, "$CFG->wwwroot/mod/{$cm->modname}/view.php")!== 0){
221 //don't need this link if returnurl returns to view.php
222 $navlinks[] = array('name' => $streditingmodule, 'link' => $returnurl, 'type' => 'title');
224 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
225 $navigation = build_navigation($navlinks);
226 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
230 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
231 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
232 $strediting = '<a href="edit.php?courseid='.$COURSE->id
.'">'.
233 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
234 $navigation = build_navigation($navlinks);
235 print_header_simple($streditingquestion, '', $navigation);
239 // Display a heading, question editing form and possibly some extra content needed for
240 // for this question type.
241 $QTYPES[$question->qtype
]->display_question_editing_page($mform, $question, $wizardnow);
242 print_footer($COURSE);