merging MOODLE_19_QUESTIONS with HEAD
[moodle.git] / question / question.php
CommitLineData
4323d029 1<?php // $Id$
16750bcd 2/**
add2d7ac 3 * Page for editing questions using the new form library.
16750bcd 4 *
add2d7ac 5 * @author T.J.Hunt@open.ac.uk
16750bcd 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 7 * @package questionbank
16750bcd 8 *//** */
87391656 9
add2d7ac 10// Includes.
11require_once(dirname(__FILE__) . '/../config.php');
12require_once(dirname(__FILE__) . '/editlib.php');
13require_once($CFG->libdir . '/filelib.php');
14require_once($CFG->libdir . '/formslib.php');
15
7cd4fda6 16$returnurl = optional_param('returnurl', 0, PARAM_LOCALURL);
64eeaf47 17
add2d7ac 18// Read URL parameters telling us which question to edit.
19$id = optional_param('id', 0, PARAM_INT); // question id
9ab75b2b 20$cmid = optional_param('cmid', 0, PARAM_INT);
add2d7ac 21$qtype = optional_param('qtype', '', PARAM_FILE);
22$categoryid = optional_param('category', 0, PARAM_INT);
23$wizardnow = optional_param('wizardnow', '', PARAM_ALPHA);
24
9ab75b2b 25if ($cmid){
26 list($module, $cm) = get_module_from_cmid($cmid);
27} else {
28 $module = null;
29 $cm = null;
30}
31
add2d7ac 32// Validate the URL parameters.
33if ($id) {
34 if (!$question = get_record('question', 'id', $id)) {
35 print_error('questiondoesnotexist', 'question', $returnurl);
36 }
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);
44}
45
46// Validate the question category.
47if (!$category = get_record('question_categories', 'id', $question->category)) {
48 print_error('categorydoesnotexist', 'question', $returnurl);
49}
50if (!$returnurl) {
51 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$category->course}";
add2d7ac 52}
53
54// Validate the question type.
55if (!isset($QTYPES[$question->qtype])) {
56 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
57}
58$CFG->pagepath = 'question/type/' . $question->qtype;
59
60// Check the user is logged in and has enough premissions.
61require_login($category->course, false);
62$coursecontext = get_context_instance(CONTEXT_COURSE, $category->course);
63require_capability('moodle/question:manage', $coursecontext);
64
65// Create the question editing form.
66if ($wizardnow!==''){
67 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
68 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 69 } else {
add2d7ac 70 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow);
71 }
72} else {
fe648028 73 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question);
add2d7ac 74}
75
76if ($mform === null) {
77 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
78}
79$toform = $question; // send the question object and a few more parameters to the form
80$toform->returnurl = $returnurl;
9ab75b2b 81if ($cm !== null){
82 $toform->cmid = $cm->id;
83}
add2d7ac 84$mform->set_data($toform);
85
86if ($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
92 }
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);
516cf3eb 98 } else {
561fe61d 99 redirect($returnurl);
516cf3eb 100 }
add2d7ac 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);
ee6c9355 108 }
516cf3eb 109 }
add2d7ac 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);
114 }
115 redirect($nexturl.'&wizardnow='.$data->wizard.$queryappend, '', 20);
516cf3eb 116 }
add2d7ac 117} else {
118
9ab75b2b 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 : "";
3b27b0fe 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);
9ab75b2b 130 print_header_simple($streditingquestion, '', $navigation, "", "", true, $strupdatemodule);
131
8c2550c8 132 } else {
3b27b0fe 133 $navlinks = array();
134 $navlinks[] = array('name' => get_string('editquestions', "quiz"), 'link' => $returnurl, 'type' => 'title');
135 $navlinks[] = array('name' => $streditingquestion, 'link' => '', 'type' => 'title');
add2d7ac 136 $strediting = '<a href="edit.php?courseid='.$category->course.'">'.
137 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
3b27b0fe 138 $navigation = build_navigation($navlinks);
9ab75b2b 139 print_header_simple($streditingquestion, '', $navigation);
8c2550c8 140 }
516cf3eb 141
add2d7ac 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);
516cf3eb 145
add2d7ac 146 print_footer($COURSE);
147}
516cf3eb 148?>