Bumped to 1.6.8
[moodle.git] / question / showbank.php
blob786fef9e7417e4bc96a7825d9e4692489e8d2224
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 (!isteacheredit($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 redirect("edit.php?courseid=$course->id");
90 } else {
91 error("Confirmation string was incorrect");
94 } else { // teacher still has to confirm
95 // make a list of all the questions that are selected
96 $rawquestions = $_REQUEST;
97 $questionlist = ''; // comma separated list of ids of questions to be deleted
98 $questionnames = ''; // string with names of questions separated by <br /> with
99 // an asterix in front of those that are in use
100 $inuse = false; // set to true if at least one of the questions is in use
101 foreach ($rawquestions as $key => $value) { // Parse input for question ids
102 if (substr($key, 0, 1) == "q") {
103 $key = substr($key,1);
104 $questionlist .= $key.',';
105 if (record_exists('quiz_question_instances', 'question', $key) or
106 record_exists('question_states', 'originalquestion', $key)) {
107 $questionnames .= '* ';
108 $inuse = true;
110 $questionnames .= get_field('question', 'name', 'id', $key).'<br />';
113 if (!$questionlist) { // no questions were selected
114 redirect("edit.php?courseid=$course->id");
116 $questionlist = rtrim($questionlist, ',');
118 // Add an explanation about questions in use
119 if ($inuse) {
120 $questionnames .= get_string('questionsinuse', 'quiz');
122 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
123 "edit.php?courseid=$course->id&amp;sesskey=$USER->sesskey&amp;deleteselected=$questionlist&amp;confirm=".md5($questionlist), "edit.php?courseid=$course->id");
124 print_footer($course);
125 exit;
129 // Unhide a question
130 if(isset($_REQUEST['unhide']) && confirm_sesskey()) {
131 $unhide = required_param('unhide', PARAM_INT);
132 if(!set_field('question', 'hidden', 0, 'id', $unhide)) {
133 error("Failed to unhide the question.");
135 redirect("edit.php?courseid=$course->id");
138 if (isset($_REQUEST['cat'])) { /// coming from category selection drop-down menu
139 $SESSION->questioncat = required_param('cat', PARAM_INT);
140 $page = 0;
141 $SESSION->questionpage = 0;
144 if(isset($_REQUEST['recurse'])) {
145 $SESSION->questionrecurse = optional_param('recurse', 0, PARAM_BOOL);
148 if(isset($_REQUEST['showhidden'])) {
149 $SESSION->questionshowhidden = optional_param('showhidden', 0, PARAM_BOOL);
152 /// all commands have been dealt with, now print the page
154 if (empty($SESSION->questioncat) or !count_records_select("question_categories", "id = '{$SESSION->questioncat}' AND (course = '{$course->id}' OR publish = '1')")) {
155 $category = get_default_question_category($course->id);
156 $SESSION->questioncat = $category->id;
158 if (!isset($SESSION->questionrecurse)) {
159 $SESSION->questionrecurse = 1;
161 if (!isset($SESSION->questionshowhidden)) {
162 $SESSION->questionshowhidden = false;
165 // starts with category selection form
166 print_simple_box_start("center", "100%");
167 question_category_form($course, $SESSION->questioncat, $SESSION->questionrecurse, $SESSION->questionshowhidden);
168 print_simple_box_end();
170 print_spacer(5,1);
172 // continues with list of questions
173 print_simple_box_start("center", "100%");
174 question_list($course, $SESSION->questioncat, isset($modform->instance) ? $modform->instance : 0, $SESSION->questionrecurse, $page, $perpage, $SESSION->questionshowhidden, $sortorder);
175 print_simple_box_end();