added a param to moodle_url constructor to provide params as an array just as a conve...
[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 *
5 * TODO: currently this still treats the quiz as special
16750bcd 6 *
add2d7ac 7 * @author T.J.Hunt@open.ac.uk
16750bcd 8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
4323d029 9 * @package questionbank
16750bcd 10 *//** */
87391656 11
add2d7ac 12// Includes.
13require_once(dirname(__FILE__) . '/../config.php');
14require_once(dirname(__FILE__) . '/editlib.php');
15require_once($CFG->libdir . '/filelib.php');
16require_once($CFG->libdir . '/formslib.php');
17
18$returnurl = optional_param('returnurl', 0, PARAM_URL);
19if (!$returnurl && isset($SESSION->fromurl)) {
20 $returnurl = $SESSION->fromurl;
21}
22
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);
28
29// Validate the URL parameters.
30if ($id) {
31 if (!$question = get_record('question', 'id', $id)) {
32 print_error('questiondoesnotexist', 'question', $returnurl);
33 }
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;
39} else {
40 print_error('notenoughdatatoeditaquestion', 'question', $returnurl);
41}
42
43// Validate the question category.
44if (!$category = get_record('question_categories', 'id', $question->category)) {
45 print_error('categorydoesnotexist', 'question', $returnurl);
46}
47if (!$returnurl) {
48 $returnurl = "{$CFG->wwwroot}/question/edit.php?courseid={$category->course}";
49 $SESSION->fromurl = $returnurl;
50}
51
52// Validate the question type.
53if (!isset($QTYPES[$question->qtype])) {
54 print_error('unknownquestiontype', 'question', $returnurl, $question->qtype);
55}
56$CFG->pagepath = 'question/type/' . $question->qtype;
57
58// Check the user is logged in and has enough premissions.
59require_login($category->course, false);
60$coursecontext = get_context_instance(CONTEXT_COURSE, $category->course);
61require_capability('moodle/question:manage', $coursecontext);
62
63// Create the question editing form.
64if ($wizardnow!==''){
65 if (!method_exists($QTYPES[$question->qtype], 'next_wizard_form')){
66 print_error('missingimportantcode', 'question', $returnurl, 'wizard form definition');
516cf3eb 67 } else {
add2d7ac 68 $mform = $QTYPES[$question->qtype]->next_wizard_form('question.php', $question, $wizardnow);
69 }
70} else {
fe648028 71 $mform = $QTYPES[$question->qtype]->create_editing_form('question.php', $question);
add2d7ac 72}
73
74if ($mform === null) {
75 print_error('missingimportantcode', 'question', $returnurl, 'question editing form definition for "'.$question->qtype.'"');
76}
77$toform = $question; // send the question object and a few more parameters to the form
78$toform->returnurl = $returnurl;
79$mform->set_data($toform);
80
81if ($mform->is_cancelled()){
82 redirect($returnurl);
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
87 }
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'), '');
92 close_window(3);
516cf3eb 93 } else {
add2d7ac 94 redirect($SESSION->returnurl);
516cf3eb 95 }
add2d7ac 96 die;
97 } else {
98 //useful for passing data to the next page which is not saved in the database
99 $queryappend = '';
100 if (isset($data->nextpageparam)){
101 foreach ($data->nextpageparam as $key => $param){
102 $queryappend .= "&".urlencode($key).'='.urlencode($param);
ee6c9355 103 }
516cf3eb 104 }
add2d7ac 105 if ($question->id) {
106 $nexturl = "question.php?id=$question->id&returnurl=" . urlencode($returnurl);
107 } else { // only for creating new questions
108 $nexturl = "question.php?category=$question->category&qtype=$question->qtype&returnurl=".urlencode($returnurl);
109 }
110 redirect($nexturl.'&wizardnow='.$data->wizard.$queryappend, '', 20);
516cf3eb 111 }
add2d7ac 112} else {
113
114 $streditingquestion = get_string('editingquestion', 'question');
8c2550c8 115 if (isset($SESSION->modform->instance)) {
add2d7ac 116 // TODO: remove restriction to quiz
117 $strediting = '<a href="' . $returnurl . '">' . get_string('editingquiz', 'quiz') . '</a> -> '.
118 $streditingquestion;
8c2550c8 119 } else {
add2d7ac 120 $strediting = '<a href="edit.php?courseid='.$category->course.'">'.
121 get_string("editquestions", "quiz").'</a> -> '.$streditingquestion;
8c2550c8 122 }
8c2550c8 123 print_header_simple($streditingquestion, '', $strediting);
516cf3eb 124
add2d7ac 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);
516cf3eb 128
add2d7ac 129 print_footer($COURSE);
130}
516cf3eb 131?>