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 $mform->set_data($toform);
145 if ($mform->is_cancelled()){
146 redirect($returnurl);
147 } elseif ($fromform = $mform->get_data()){
148 $returnurl = new moodle_url($returnurl);
149 //select category that question has been saved in / moved to when we return to question bank
150 if (!empty($fromform->categorymoveto
)){
151 $returnurl->param('category', $fromform->categorymoveto
);
152 } else if (!empty($fromform->category
)){
153 $returnurl->param('category', $fromform->category
);
155 $returnurl = $returnurl->out();
156 if (!empty($fromform->makecopy
)) {
157 $question->id
= 0; // causes a new question to be created.
158 $question->hidden
= 0; // Copies should not be hidden
161 list($tocatid, $tocontextid) = explode(',', $fromform->categorymoveto
);
162 $tocontext = get_context_instance_by_id($tocontextid);
163 require_capability('moodle/question:add', $tocontext);
164 if (get_filesdir_from_context($categorycontext) != get_filesdir_from_context($tocontext)){
165 $movecontexturl = new moodle_url($CFG->wwwroot
.'/question/contextmoveq.php',
166 array('returnurl' => $returnurl,
167 'ids'=>$question->id
,
168 'tocatid'=> $tocatid));
170 $movecontexturl->param('cmid', $cmid);
172 $movecontexturl->param('courseid', $COURSE->id
);
174 redirect($movecontexturl->out());
178 $question = $QTYPES[$question->qtype
]->save_question($question, $fromform, $COURSE, $wizardnow);
179 if (($QTYPES[$question->qtype
]->finished_edit_wizard($fromform)) ||
$movecontext){
181 notify(get_string('changessaved'), '');
184 redirect($returnurl);
187 $nexturlparams = array('returnurl'=>$returnurl);
188 if (isset($fromform->nextpageparam
) && is_array($fromform->nextpageparam
)){
189 $nexturlparams +
= $fromform->nextpageparam
;//useful for passing data to the next page which is not saved in the database
191 $nexturlparams['id'] = $question->id
;
192 $nexturlparams['wizardnow'] = $fromform->wizard
;
193 $nexturl = new moodle_url('question.php', $nexturlparams);
195 $nexturl->param('cmid', $cmid);
197 $nexturl->param('courseid', $COURSE->id
);
199 redirect($nexturl->out());
203 list($streditingquestion,) = $QTYPES[$question->qtype
]->get_heading();
205 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE
, $COURSE->id
))
206 ?
update_module_button($cm->id
, $cm->course
, get_string('modulename', $cm->modname
))
209 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$cm->course", 'type' => 'activity');
210 $navlinks[] = array('name' => format_string($module->name
), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 'type' => 'title');
211 $navlinks[] = array('name' => get_string('editingquiz', 'quiz'), 'link' => $returnurl, 'type' => 'title');
212 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
213 $navigation = build_navigation($navlinks);
214 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
218 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
219 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
220 $strediting = '<a href="edit.php?courseid='.$COURSE->id
.'">'.
221 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
222 $navigation = build_navigation($navlinks);
223 print_header_simple($streditingquestion, '', $navigation);
227 // Display a heading, question editing form and possibly some extra content needed for
228 // for this question type.
229 $QTYPES[$question->qtype
]->display_question_editing_page($mform, $question, $wizardnow);
230 print_footer($COURSE);