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
10 * cat Chooses the category
11 * displayoptions Sets display options
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
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'");
34 $SESSION->questionpage
= $page;
36 $page = isset($SESSION->questionpage
) ?
$SESSION->questionpage
: 0;
40 $SESSION->questionperpage
= $perpage;
42 $perpage = isset($SESSION->questionperpage
) ?
$SESSION->questionperpage
: DEFAULT_QUESTIONS_PER_PAGE
;
46 $SESSION->questionsortorder
= $sortorder;
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');
85 delete_question($questionid);
92 redirect("edit.php?courseid=$course->id");
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 .= '* ';
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
123 $questionnames .= '<br />'.get_string('questionsinuse', 'quiz');
125 notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
126 "edit.php?courseid=$course->id&sesskey=$USER->sesskey&deleteselected=$questionlist&confirm=".md5($questionlist), "edit.php?courseid=$course->id");
130 print_footer($course);
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
);
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();
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();