MDL-10680:
[moodle-linuxchix.git] / question / question.php
blob45e8ac9963ca3f941b2a11f348c36439eb2c54c1
1 <?php // $Id$
2 /**
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
8 *//** */
10 // Includes.
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 $returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
18 // Read URL parameters telling us which question to edit.
19 $id = optional_param('id', 0, PARAM_INT); // question id
20 $cmid = optional_param('cmid', 0, PARAM_INT);
21 $qtype = optional_param('qtype', '', PARAM_FILE);
22 $categoryid = optional_param('category', 0, PARAM_INT);
23 $wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
25 if ($cmid){
26 list($module, $cm) = get_module_from_cmid($cmid);
27 } else {
28 $module = null;
29 $cm = null;
32 // Validate the URL parameters.
33 if ($id) {
34 if (!$question = get_record('question', 'id', $id)) {
35 print_error('questiondoesnotexist', 'question', $returnurl);
37 get_question_options($question);
38 } else if ($categoryid && $qtype) { // only for creating new questions
39 $question = new stdClass;
40 $question->category = $categoryid;
41 $question->qtype = $qtype;
42 } else {
43 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
46 // Validate the question category.
47 if (!$category = get_record('question_categories', 'id', $question->category)) {
48 print_error('categorydoesnotexist', 'question', $returnurl);
50 if (!$returnurl) {
51 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$category->course}";
54 // Validate the question type.
55 if (!isset($QTYPES[$question->qtype])) {
56 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
58 $CFG->pagepath = 'question/type/' . $question->qtype;
60 // Check the user is logged in and has enough premissions.
61 require_login($category->course, false);
62 $coursecontext = get_context_instance(CONTEXT_COURSE, $category->course);
63 require_capability('moodle/question:manage', $coursecontext);
65 // Create the question editing form.
66 if ($wizardnow!==''){
67 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
68 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
69 } else {
70 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow);
72 } else {
73 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question);
76 if ($mform === null) {
77 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
79 $toform = $question; // send the question object and a few more parameters to the form
80 $toform->returnurl = $returnurl;
81 if ($cm !== null){
82 $toform->cmid = $cm->id;
84 $mform->set_data($toform);
86 if ($mform->is_cancelled()){
87 redirect($returnurl);
88 } elseif ($data = $mform->get_data()){
89 if (!empty($data->makecopy)) {
90 $question->id = 0; // causes a new question to be created.
91 $question->hidden = 0; // Copies should not be hidden
93 $question = $QTYPES[$question->qtype]->save_question($question, $data, $COURSE, $wizardnow);
94 if ($QTYPES[$qtype]->finished_edit_wizard($data)){
95 if (optional_param('inpopup', 0, PARAM_BOOL)) {
96 notify(get_string('changessaved'), '');
97 close_window(3);
98 } else {
99 redirect($returnurl);
101 die;
102 } else {
103 //useful for passing data to the next page which is not saved in the database
104 $queryappend = '';
105 if (isset($data->nextpageparam)){
106 foreach ($data->nextpageparam as $key => $param){
107 $queryappend .= "&".urlencode($key).'='.urlencode($param);
110 if ($question->id) {
111 $nexturl = "question.php?id=$question->id&returnurl=" . urlencode($returnurl);
112 } else { // only for creating new questions
113 $nexturl = "question.php?category=$question->category&qtype=$question->qtype&returnurl=".urlencode($returnurl);
115 redirect($nexturl.'&wizardnow='.$data->wizard.$queryappend, '', 20);
117 } else {
119 list($streditingquestion,) = $QTYPES[$question->qtype]->get_heading();
120 if ($cm !== null) {
121 $strupdatemodule = has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $category->course))
122 ? update_module_button($cm->id, $category->course, get_string('modulename', $cm->modname))
123 : "";
124 $navlinks = array();
125 $navlinks[] = array('name' => get_string('modulenameplural', $cm->modname), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$category->course", 'type' => 'activity');
126 $navlinks[] = array('name' => format_string($module->name), 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?cmid={$cm->id}", 'type' => 'title');
127 $navlinks[] = array('name' => get_string('editingquiz', 'quiz'), 'link' => $returnurl, 'type' => 'title');
128 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
129 $navigation = build_navigation($navlinks);
130 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
132 } else {
133 $navlinks = array();
134 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
135 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
136 $strediting = '<a href="edit.php?courseid='.$category->course.'">'.
137 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
138 $navigation = build_navigation($navlinks);
139 print_header_simple($streditingquestion, '', $navigation);
142 // Display a heading, question editing form and possibly some extra content needed for
143 // for this question type.
144 $QTYPES[$question->qtype]->display_question_editing_page($mform, $question, $wizardnow);
146 print_footer($COURSE);