Commit | Line | Data |
---|---|---|
efa5155a RM |
1 | <?php |
2 | /** | |
3 | * This class controls from which category questions are listed. | |
4 | * | |
5 | * @package moodlecore | |
6 | * @subpackage questionbank | |
7 | * @copyright 2013 Tim Hunt, Ray Morris and others {@link http://moodle.com} | |
8 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
9 | */ | |
10 | class core_question_bank_search_condition_category extends core_question_bank_search_condition { | |
11 | protected $category; | |
12 | protected $recurse; | |
13 | protected $where; | |
14 | protected $params; | |
15 | protected $cat; | |
16 | ||
17 | /** | |
18 | * Constructor | |
19 | * @param string $cat categoryID,contextID as used with question_bank_view->display() | |
20 | * @param boolean $recurse Whether to include questions from sub-categories | |
21 | * @param array $contexts Context objects as used by question_category_options() | |
22 | * @param moodle_url $baseurl The URL the form is submitted to | |
23 | * @param stdClass $course Course record | |
24 | * @param integer $maxinfolength The maximum displayed length of the category info | |
25 | */ | |
26 | public function __construct($cat = null, $recurse = false, $contexts, $baseurl, $course, $maxinfolength = null) { | |
27 | $this->cat = $cat; | |
28 | $this->recurse = $recurse; | |
29 | $this->contexts = $contexts; | |
30 | $this->baseurl = $baseurl; | |
31 | $this->course = $course; | |
32 | $this->init(); | |
33 | $this->maxinfolength = $maxinfolength; | |
34 | } | |
35 | ||
36 | /** | |
37 | * Initialize the object so it will be ready to return where() and params() | |
38 | */ | |
39 | private function init() { | |
40 | global $DB; | |
41 | if (!$this->category = $this->get_current_category($this->cat)) { | |
42 | return; | |
43 | } | |
44 | if ($this->recurse) { | |
45 | $categoryids = question_categorylist($this->category->id); | |
46 | } else { | |
47 | $categoryids = array($this->category->id); | |
48 | } | |
49 | list($catidtest, $this->params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cat'); | |
50 | $this->where = 'q.category ' . $catidtest; | |
51 | } | |
52 | ||
53 | /** | |
54 | * @returns string SQL fragment to be ANDed to the where clause to select which category of questions to display | |
55 | */ | |
56 | public function where() { | |
57 | return $this->where; | |
58 | } | |
59 | ||
60 | /** | |
61 | * @returns array Parameters to be bound to the SQL query to select which category of questions to display | |
62 | */ | |
63 | public function params() { | |
64 | return $this->params; | |
65 | } | |
66 | ||
67 | /** | |
68 | * Called by question_bank_view to display the GUI for selecting a category | |
69 | */ | |
70 | public function display_options() { | |
71 | $this->display_category_form($this->contexts, $this->baseurl, $this->cat); | |
72 | $this->print_category_info($this->category); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Displays the recursion checkbox GUI. | |
77 | * question_bank_view places this within the section that is hidden by default | |
78 | */ | |
79 | public function display_options_adv() { | |
80 | echo '<div>'; | |
81 | echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'recurse', | |
82 | 'value' => 0, 'id' => 'recurse_off')); | |
83 | echo html_writer::checkbox('recurse', '1', $this->recurse, get_string('includesubcategories', 'question'), | |
84 | array('id' => 'recurse_on', 'class' => 'searchoptions')); | |
85 | echo "</div>\n"; | |
86 | ||
87 | } | |
88 | ||
89 | /** | |
90 | * Display the drop down to select the category | |
91 | */ | |
92 | protected function display_category_form($contexts, $pageurl, $current) { | |
93 | global $OUTPUT; | |
94 | ||
95 | echo '<div class="choosecategory">'; | |
96 | $catmenu = question_category_options($contexts, false, 0, true); | |
97 | $select = new single_select($this->baseurl, 'category', $catmenu, $current, null, 'catmenu'); | |
98 | $select->set_label(get_string('selectacategory', 'question')); | |
99 | echo $OUTPUT->render($select); | |
100 | echo "</div>\n"; | |
101 | ||
102 | } | |
103 | ||
104 | /** | |
105 | * Look up the category record based on cateogry ID and context | |
106 | * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display() | |
107 | * @return stdClass The category record | |
108 | */ | |
109 | protected function get_current_category($categoryandcontext) { | |
110 | global $DB, $OUTPUT; | |
111 | list($categoryid, $contextid) = explode(',', $categoryandcontext); | |
112 | if (!$categoryid) { | |
113 | $this->print_choose_category_message($categoryandcontext); | |
114 | return false; | |
115 | } | |
116 | ||
117 | if (!$category = $DB->get_record('question_categories', | |
118 | array('id' => $categoryid, 'contextid' => $contextid))) { | |
119 | echo $OUTPUT->box_start('generalbox questionbank'); | |
120 | echo $OUTPUT->notification('Category not found!'); | |
121 | echo $OUTPUT->box_end(); | |
122 | return false; | |
123 | } | |
124 | ||
125 | return $category; | |
126 | } | |
127 | ||
128 | /** | |
129 | * Print the category description | |
130 | */ | |
131 | protected function print_category_info($category) { | |
132 | $formatoptions = new stdClass(); | |
133 | $formatoptions->noclean = true; | |
134 | $formatoptions->overflowdiv = true; | |
135 | echo '<div class="boxaligncenter categoryinfo">'; | |
136 | if (isset($this->maxinfolength)) { | |
137 | echo shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id), | |
138 | $this->maxinfolength); | |
139 | } else { | |
140 | echo format_text($category->info, $category->infoformat, $formatoptions, $this->course->id); | |
141 | } | |
142 | echo "</div>\n"; | |
143 | } | |
144 | ||
145 | } | |
146 |