Output was missing utf8 meta tag.
[moodle-linuxchix.git] / question / showbank.php
blob14a19c777b8a2085acc155862131c8ce5b269c5f
1 <?php // $Id$
2 /**
3 * Shows the question bank editing interface. To be included by other pages
5 * The script also processes a number of actions:
6 * Actions affecting the question pool:
7 * move Moves a question to a different category
8 * deleteselected Deletes the selected questions from the category
9 * Other actions:
10 * cat Chooses the category
11 * displayoptions Sets display options
13 * @version $Id$
14 * @author Martin Dougiamas and many others. This has recently been extensively
15 * rewritten by Gustav Delius and other members of the Serving Mathematics project
16 * {@link http://maths.york.ac.uk/serving_maths}
17 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
18 * @package question
21 // Make sure this can only be used from within Moodle scripts
22 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
24 require_once($CFG->dirroot.'/question/editlib.php');
26 $page = optional_param('page', -1, PARAM_INT);
27 $perpage = optional_param('perpage', -1, PARAM_INT);
28 $sortorder = optional_param('sortorder', '');
29 if (preg_match("/[';]/", $sortorder)) {
30 error("Incorrect use of the parameter 'sortorder'");
33 if ($page > -1) {
34 $SESSION->questionpage = $page;
35 } else {
36 $page = isset($SESSION->questionpage) ? $SESSION->questionpage : 0;
39 if ($perpage > -1) {
40 $SESSION->questionperpage = $perpage;
41 } else {
42 $perpage = isset($SESSION->questionperpage) ? $SESSION->questionperpage : DEFAULT_QUESTIONS_PER_PAGE;
45 if ($sortorder) {
46 $SESSION->questionsortorder = $sortorder;
47 } else {
48 $sortorder = isset($SESSION->questionsortorder) ? $SESSION->questionsortorder : 'qtype, name ASC';
51 /// Now, check for commands on this page and modify variables as necessary
53 if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
54 $tocategoryid = required_param('category', PARAM_INT);
55 if (!$tocategory = get_record('question_categories', 'id', $tocategoryid)) {
56 error('Invalid category');
58 if (!has_capability('moodle/question:managecategory', get_context_instance(CONTEXT_COURSE, $tocategory->course))){
59 error(get_string('categorynoedit', 'quiz', $tocategory->name), 'edit.php?courseid=$course->id');
61 foreach ($_POST as $key => $value) { // Parse input for question ids
62 if (substr($key, 0, 1) == "q") {
63 $key = substr($key,1);
64 if (!set_field('question', 'category', $tocategory->id, 'id', $key)) {
65 error('Could not update category field');
71 if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category
73 if (isset($_REQUEST['confirm']) and confirm_sesskey()) { // teacher has already confirmed the action
74 $deleteselected = required_param('deleteselected');
75 if ($_REQUEST['confirm'] == md5($deleteselected)) {
76 if ($questionlist = explode(',', $deleteselected)) {
77 // for each question either hide it if it is in use or delete it
78 foreach ($questionlist as $questionid) {
79 if (record_exists('quiz_question_instances', 'question', $questionid) or
80 record_exists('question_states', 'originalquestion', $questionid)) {
81 if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
82 error('Was not able to hide question');
84 } else {
85 delete_question($questionid);
89 echo '</td></tr>';
90 echo '</table>';
91 echo '</div>';
92 redirect("edit.php?courseid=$course->id");
93 } else {
94 error("Confirmation string was incorrect");
97 } else { // teacher still has to confirm
98 // make a list of all the questions that are selected
99 $rawquestions = $_REQUEST;
100 $questionlist = ''; // comma separated list of ids of questions to be deleted
101 $questionnames = ''; // string with names of questions separated by <br /> with
102 // an asterix in front of those that are in use
103 $inuse = false; // set to true if at least one of the questions is in use
104 foreach ($rawquestions as $key => $value) { // Parse input for question ids
105 if (substr($key, 0, 1) == "q") {
106 $key = substr($key,1);
107 $questionlist .= $key.',';
108 if (record_exists('quiz_question_instances', 'question', $key) or
109 record_exists('question_states', 'originalquestion', $key)) {
110 $questionnames .= '* ';
111 $inuse = true;
113 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
116 if (!$questionlist) { // no questions were selected
117 redirect("edit.php?courseid=$course->id");
119 $questionlist = rtrim($questionlist, ',');
121 // Add an explanation about questions in use
122 if ($inuse) {
123 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
125 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
126 "edit.php?courseid=$course->id&amp;sesskey=$USER->sesskey&amp;deleteselected=$questionlist&amp;confirm=".md5($questionlist), "edit.php?courseid=$course->id");
128 echo '</td></tr>';
129 echo '</table>';
130 print_footer($course);
131 exit;
135 // Unhide a question
136 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
137 $unhide = required_param('unhide', PARAM_INT);
138 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
139 error("Failed to unhide the question.");
141 redirect("edit.php?courseid=$course->id");
144 if (isset($_REQUEST['cat'])) { /// coming from category selection drop-down menu
145 $SESSION->questioncat = required_param('cat', PARAM_INT);
146 $page = 0;
147 $SESSION->questionpage = 0;
150 if(isset($_REQUEST['recurse'])) {
151 $SESSION->questionrecurse = optional_param('recurse', 0, PARAM_BOOL);
154 if(isset($_REQUEST['showhidden'])) {
155 $SESSION->questionshowhidden = optional_param('showhidden', 0, PARAM_BOOL);
158 /// all commands have been dealt with, now print the page
160 if (empty($SESSION->questioncat) or !count_records_select("question_categories", "id = '{$SESSION->questioncat}' AND (course = '{$course->id}' OR publish = '1')")) {
161 $category = get_default_question_category($course->id);
162 $SESSION->questioncat = $category->id;
164 if (!isset($SESSION->questionrecurse)) {
165 $SESSION->questionrecurse = 1;
167 if (!isset($SESSION->questionshowhidden)) {
168 $SESSION->questionshowhidden = false;
171 // starts with category selection form
172 if (has_capability('moodle/question:managecategory', $context)) {
173 print_simple_box_start();
174 question_category_form($course, $SESSION->questioncat, $SESSION->questionrecurse, $SESSION->questionshowhidden);
175 print_simple_box_end();
177 print_spacer(5,1);
180 // continues with list of questions
181 print_simple_box_start();
182 question_list($course, $SESSION->questioncat, isset($modform->instance) ? $modform->instance : 0, $SESSION->questionrecurse, $page, $perpage, $SESSION->questionshowhidden, $sortorder);
183 print_simple_box_end();