3 * Shows the question bank editing interface. To be included by other pages
5 * The script also processes a number of actions:
7 * Actions affecting the question pool:
8 * move Moves a question to a different category
9 * deleteselected Deletes the selected questions from the category
11 * cat Chooses the category
12 * 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
18 * @package questionbank
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';
50 $SESSION->fromurl
= $FULLME;
52 /// 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 ($categoryid = optional_param('cat', 0, PARAM_INT
)) { /// coming from category selection drop-down menu
145 $SESSION->questioncat
= $categoryid;
147 $SESSION->questionpage
= 0;
150 if (empty($SESSION->questioncat
) or !count_records_select("question_categories", "id = '{$SESSION->questioncat}' AND (course = '{$course->id}' OR publish = '1')")) {
151 $category = get_default_question_category($course->id
);
152 $SESSION->questioncat
= $category->id
;
155 if(($recurse = optional_param('recurse', -1, PARAM_BOOL
)) != -1) {
156 $SESSION->questionrecurse
= $recurse;
158 if (!isset($SESSION->questionrecurse
)) {
159 $SESSION->questionrecurse
= 1;
162 if(($showhidden = optional_param('showhidden', -1, PARAM_BOOL
)) != -1) {
163 $SESSION->questionshowhidden
= $showhidden;
165 if (!isset($SESSION->questionshowhidden
)) {
166 $SESSION->questionshowhidden
= 0;
169 if(($showquestiontext = optional_param('showquestiontext', -1, PARAM_BOOL
)) != -1) {
170 $SESSION->questionshowquestiontext
= $showquestiontext;
172 if (!isset($SESSION->questionshowquestiontext
)) {
173 $SESSION->questionshowquestiontext
= 0;
176 // starts with category selection form
177 print_box_start('generalbox questionbank');
178 print_heading(get_string('questionbank', 'question'), '', 2);
179 question_category_form($course, $SESSION->questioncat
, $SESSION->questionrecurse
,
180 $SESSION->questionshowhidden
, $SESSION->questionshowquestiontext
);
182 // continues with list of questions
183 question_list($course, $SESSION->questioncat
, isset($modform->instance
) ?
$modform->instance
: 0,
184 $SESSION->questionrecurse
, $page, $perpage, $SESSION->questionshowhidden
, $sortorder,
185 $SESSION->questionshowquestiontext
);