3 * Page for editing questions using the new form library.
5 * TODO: currently this still treats the quiz as special
7 * @author T.J.Hunt@open.ac.uk
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 require_once(dirname(__FILE__
) . '/../config.php');
14 require_once(dirname(__FILE__
) . '/editlib.php');
15 require_once($CFG->libdir
. '/filelib.php');
16 require_once($CFG->libdir
. '/formslib.php');
18 $returnurl = optional_param('returnurl', 0, PARAM_URL
);
19 if (!$returnurl && isset($SESSION->fromurl
)) {
20 $returnurl = $SESSION->fromurl
;
23 // Read URL parameters telling us which question to edit.
24 $id = optional_param('id', 0, PARAM_INT
); // question id
25 $qtype = optional_param('qtype', '', PARAM_FILE
);
26 $categoryid = optional_param('category', 0, PARAM_INT
);
27 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA
);
29 // Validate the URL parameters.
31 if (!$question = get_record('question', 'id', $id)) {
32 print_error('questiondoesnotexist', 'question', $returnurl);
34 get_question_options($question);
35 } else if ($categoryid && $qtype) { // only for creating new questions
36 $question = new stdClass
;
37 $question->category
= $categoryid;
38 $question->qtype
= $qtype;
40 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
43 // Validate the question category.
44 if (!$category = get_record('question_categories', 'id', $question->category
)) {
45 print_error('categorydoesnotexist', 'question', $returnurl);
48 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$category->course}";
49 $SESSION->fromurl
= $returnurl;
52 // Validate the question type.
53 if (!isset($QTYPES[$question->qtype
])) {
54 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype
);
56 $CFG->pagepath
= 'question/type/' . $question->qtype
;
58 // Check the user is logged in and has enough premissions.
59 require_login($category->course
, false);
60 $coursecontext = get_context_instance(CONTEXT_COURSE
, $category->course
);
61 require_capability('moodle/question:manage', $coursecontext);
63 // Create the question editing form.
65 if (!method_exists($QTYPES[$question->qtype
], 'next_wizard_form')){
66 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
68 $mform = $QTYPES[$question->qtype
]->next_wizard_form('question2.php', $question, $wizardnow);
71 $mform = $QTYPES[$question->qtype
]->create_editing_form('question2.php', $question, $category->course
);
74 if ($mform === null) {
75 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype
.'"');
77 $toform = $question; // send the question object and a few more parameters to the form
78 $toform->returnurl
= $returnurl;
79 $mform->set_data($toform);
81 if ($mform->is_cancelled()){
83 } elseif ($data = $mform->get_data()){
84 if (!empty($data->makecopy
)) {
85 $question->id
= 0; // causes a new question to be created.
86 $question->hidden
= 0; // Copies should not be hidden
88 $question = $QTYPES[$question->qtype
]->save_question($question, $data, $COURSE, $wizardnow);
89 if ($QTYPES[$qtype]->finished_edit_wizard($data)){
90 if (optional_param('inpopup', 0, PARAM_BOOL
)) {
91 notify(get_string('changessaved'), '');
94 redirect($SESSION->returnurl
);
98 //useful for passing data to the next page which is not saved in the database
100 if (isset($data->nextpageparam
)){
101 foreach ($data->nextpageparam
as $key => $param){
102 $queryappend .= "&".urlencode($key).'='.urlencode($param);
106 $nexturl = "question2.php?id=$question->id&returnurl=" . urlencode($returnurl);
107 } else { // only for creating new questions
108 $nexturl = "question2.php?category=$question->category&qtype=$question->qtype&returnurl=".urlencode($returnurl);
110 redirect($nexturl.'&wizardnow='.$data->wizard
.$queryappend, '', 20);
114 $streditingquestion = get_string('editingquestion', 'question');
115 if (isset($SESSION->modform
->instance
)) {
116 // TODO: remove restriction to quiz
117 $strediting = '<a href="' . $returnurl . '">' . get_string('editingquiz', 'quiz') . '</a> -> '.
120 $strediting = '<a href="edit.php?courseid='.$category->course
.'">'.
121 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
123 print_header_simple($streditingquestion, '', $strediting);
125 // Display a heading, question editing form and possibly some extra content needed for
126 // for this question type.
127 $QTYPES[$question->qtype
]->display_question_editing_page($mform, $question, $wizardnow);
129 print_footer($COURSE);