Commit | Line | Data |
---|---|---|
efa5155a | 1 | <?php |
e22e7490 TH |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | ||
18 | /** | |
19 | * A search class to control from which category questions are listed. | |
20 | * | |
21 | * @package core_question | |
22 | * @copyright 2013 Ray Morris | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
efa5155a RM |
28 | /** |
29 | * This class controls from which category questions are listed. | |
30 | * | |
e22e7490 TH |
31 | * @copyright 2013 Ray Morris |
32 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
efa5155a RM |
33 | */ |
34 | class core_question_bank_search_condition_category extends core_question_bank_search_condition { | |
e22e7490 TH |
35 | /** @var stdClass The course record. */ |
36 | protected $course; | |
37 | ||
38 | /** @var stdClass The category record. */ | |
efa5155a | 39 | protected $category; |
e22e7490 TH |
40 | |
41 | /** @var array of contexts. */ | |
42 | protected $contexts; | |
43 | ||
44 | /** @var bool Whether to include questions from sub-categories. */ | |
efa5155a | 45 | protected $recurse; |
e22e7490 TH |
46 | |
47 | /** @var string SQL fragment to add to the where clause. */ | |
efa5155a | 48 | protected $where; |
e22e7490 TH |
49 | |
50 | /** @var array query param used in where. */ | |
efa5155a | 51 | protected $params; |
e22e7490 TH |
52 | |
53 | /** @var string categoryID,contextID as used with question_bank_view->display(). */ | |
efa5155a RM |
54 | protected $cat; |
55 | ||
e22e7490 TH |
56 | /** @var int The maximum displayed length of the category info. */ |
57 | protected $maxinfolength; | |
58 | ||
efa5155a RM |
59 | /** |
60 | * Constructor | |
61 | * @param string $cat categoryID,contextID as used with question_bank_view->display() | |
e22e7490 | 62 | * @param bool $recurse Whether to include questions from sub-categories |
efa5155a RM |
63 | * @param array $contexts Context objects as used by question_category_options() |
64 | * @param moodle_url $baseurl The URL the form is submitted to | |
65 | * @param stdClass $course Course record | |
e22e7490 | 66 | * @param integer $maxinfolength The maximum displayed length of the category info. |
efa5155a RM |
67 | */ |
68 | public function __construct($cat = null, $recurse = false, $contexts, $baseurl, $course, $maxinfolength = null) { | |
69 | $this->cat = $cat; | |
70 | $this->recurse = $recurse; | |
71 | $this->contexts = $contexts; | |
72 | $this->baseurl = $baseurl; | |
73 | $this->course = $course; | |
74 | $this->init(); | |
75 | $this->maxinfolength = $maxinfolength; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Initialize the object so it will be ready to return where() and params() | |
80 | */ | |
81 | private function init() { | |
82 | global $DB; | |
83 | if (!$this->category = $this->get_current_category($this->cat)) { | |
84 | return; | |
85 | } | |
86 | if ($this->recurse) { | |
87 | $categoryids = question_categorylist($this->category->id); | |
88 | } else { | |
89 | $categoryids = array($this->category->id); | |
90 | } | |
91 | list($catidtest, $this->params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cat'); | |
92 | $this->where = 'q.category ' . $catidtest; | |
93 | } | |
94 | ||
efa5155a RM |
95 | public function where() { |
96 | return $this->where; | |
97 | } | |
98 | ||
efa5155a RM |
99 | public function params() { |
100 | return $this->params; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Called by question_bank_view to display the GUI for selecting a category | |
105 | */ | |
106 | public function display_options() { | |
107 | $this->display_category_form($this->contexts, $this->baseurl, $this->cat); | |
108 | $this->print_category_info($this->category); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Displays the recursion checkbox GUI. | |
113 | * question_bank_view places this within the section that is hidden by default | |
114 | */ | |
115 | public function display_options_adv() { | |
116 | echo '<div>'; | |
117 | echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'recurse', | |
118 | 'value' => 0, 'id' => 'recurse_off')); | |
119 | echo html_writer::checkbox('recurse', '1', $this->recurse, get_string('includesubcategories', 'question'), | |
120 | array('id' => 'recurse_on', 'class' => 'searchoptions')); | |
121 | echo "</div>\n"; | |
122 | ||
123 | } | |
124 | ||
125 | /** | |
e22e7490 TH |
126 | * Display the drop down to select the category. |
127 | * | |
128 | * @param array $contexts of contexts that can be accessed from here. | |
129 | * @param moodle_url $pageurl the URL of this page. | |
130 | * @param string $current 'categoryID,contextID'. | |
efa5155a RM |
131 | */ |
132 | protected function display_category_form($contexts, $pageurl, $current) { | |
133 | global $OUTPUT; | |
134 | ||
135 | echo '<div class="choosecategory">'; | |
136 | $catmenu = question_category_options($contexts, false, 0, true); | |
137 | $select = new single_select($this->baseurl, 'category', $catmenu, $current, null, 'catmenu'); | |
138 | $select->set_label(get_string('selectacategory', 'question')); | |
139 | echo $OUTPUT->render($select); | |
140 | echo "</div>\n"; | |
141 | ||
142 | } | |
143 | ||
144 | /** | |
145 | * Look up the category record based on cateogry ID and context | |
146 | * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display() | |
147 | * @return stdClass The category record | |
148 | */ | |
149 | protected function get_current_category($categoryandcontext) { | |
150 | global $DB, $OUTPUT; | |
151 | list($categoryid, $contextid) = explode(',', $categoryandcontext); | |
152 | if (!$categoryid) { | |
153 | $this->print_choose_category_message($categoryandcontext); | |
154 | return false; | |
155 | } | |
156 | ||
157 | if (!$category = $DB->get_record('question_categories', | |
158 | array('id' => $categoryid, 'contextid' => $contextid))) { | |
159 | echo $OUTPUT->box_start('generalbox questionbank'); | |
160 | echo $OUTPUT->notification('Category not found!'); | |
161 | echo $OUTPUT->box_end(); | |
162 | return false; | |
163 | } | |
164 | ||
165 | return $category; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Print the category description | |
e22e7490 | 170 | * @param stdClass $category the category information form the database. |
efa5155a RM |
171 | */ |
172 | protected function print_category_info($category) { | |
173 | $formatoptions = new stdClass(); | |
174 | $formatoptions->noclean = true; | |
175 | $formatoptions->overflowdiv = true; | |
176 | echo '<div class="boxaligncenter categoryinfo">'; | |
177 | if (isset($this->maxinfolength)) { | |
178 | echo shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id), | |
179 | $this->maxinfolength); | |
180 | } else { | |
181 | echo format_text($category->info, $category->infoformat, $formatoptions, $this->course->id); | |
182 | } | |
183 | echo "</div>\n"; | |
184 | } | |
185 | ||
186 | } | |
187 |