MDL-40313 question bank search classes should be namespaced.
[moodle.git] / question / classes / bank / search / category_condition.php
CommitLineData
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
d62382d1 26namespace core_question\bank\search;
e22e7490
TH
27defined('MOODLE_INTERNAL') || die();
28
efa5155a
RM
29/**
30 * This class controls from which category questions are listed.
31 *
e22e7490
TH
32 * @copyright 2013 Ray Morris
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
efa5155a 34 */
d62382d1
TH
35class category_condition extends condition {
36 /** @var \stdClass The course record. */
e22e7490
TH
37 protected $course;
38
d62382d1 39 /** @var \stdClass The category record. */
efa5155a 40 protected $category;
e22e7490
TH
41
42 /** @var array of contexts. */
43 protected $contexts;
44
45 /** @var bool Whether to include questions from sub-categories. */
efa5155a 46 protected $recurse;
e22e7490
TH
47
48 /** @var string SQL fragment to add to the where clause. */
efa5155a 49 protected $where;
e22e7490
TH
50
51 /** @var array query param used in where. */
efa5155a 52 protected $params;
e22e7490
TH
53
54 /** @var string categoryID,contextID as used with question_bank_view->display(). */
efa5155a
RM
55 protected $cat;
56
e22e7490
TH
57 /** @var int The maximum displayed length of the category info. */
58 protected $maxinfolength;
59
efa5155a
RM
60 /**
61 * Constructor
62 * @param string $cat categoryID,contextID as used with question_bank_view->display()
e22e7490 63 * @param bool $recurse Whether to include questions from sub-categories
efa5155a 64 * @param array $contexts Context objects as used by question_category_options()
d62382d1
TH
65 * @param \moodle_url $baseurl The URL the form is submitted to
66 * @param \stdClass $course Course record
e22e7490 67 * @param integer $maxinfolength The maximum displayed length of the category info.
efa5155a
RM
68 */
69 public function __construct($cat = null, $recurse = false, $contexts, $baseurl, $course, $maxinfolength = null) {
70 $this->cat = $cat;
71 $this->recurse = $recurse;
72 $this->contexts = $contexts;
73 $this->baseurl = $baseurl;
74 $this->course = $course;
75 $this->init();
76 $this->maxinfolength = $maxinfolength;
77 }
78
79 /**
80 * Initialize the object so it will be ready to return where() and params()
81 */
82 private function init() {
83 global $DB;
84 if (!$this->category = $this->get_current_category($this->cat)) {
85 return;
86 }
87 if ($this->recurse) {
88 $categoryids = question_categorylist($this->category->id);
89 } else {
90 $categoryids = array($this->category->id);
91 }
92 list($catidtest, $this->params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cat');
93 $this->where = 'q.category ' . $catidtest;
94 }
95
efa5155a
RM
96 public function where() {
97 return $this->where;
98 }
99
efa5155a
RM
100 public function params() {
101 return $this->params;
102 }
103
104 /**
105 * Called by question_bank_view to display the GUI for selecting a category
106 */
107 public function display_options() {
108 $this->display_category_form($this->contexts, $this->baseurl, $this->cat);
109 $this->print_category_info($this->category);
110 }
111
112 /**
113 * Displays the recursion checkbox GUI.
114 * question_bank_view places this within the section that is hidden by default
115 */
116 public function display_options_adv() {
d62382d1
TH
117 echo \html_writer::start_div();
118 echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'recurse',
efa5155a 119 'value' => 0, 'id' => 'recurse_off'));
d62382d1 120 echo \html_writer::checkbox('recurse', '1', $this->recurse, get_string('includesubcategories', 'question'),
efa5155a 121 array('id' => 'recurse_on', 'class' => 'searchoptions'));
d62382d1 122 echo \html_writer::end_div() . "\n";
efa5155a
RM
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.
d62382d1 129 * @param \moodle_url $pageurl the URL of this page.
e22e7490 130 * @param string $current 'categoryID,contextID'.
efa5155a
RM
131 */
132 protected function display_category_form($contexts, $pageurl, $current) {
133 global $OUTPUT;
134
d62382d1 135 echo \html_writer::start_div('choosecategory');
efa5155a 136 $catmenu = question_category_options($contexts, false, 0, true);
d62382d1 137 $select = new \single_select($this->baseurl, 'category', $catmenu, $current, null, 'catmenu');
efa5155a
RM
138 $select->set_label(get_string('selectacategory', 'question'));
139 echo $OUTPUT->render($select);
d62382d1 140 echo \html_writer::end_div() . "\n";
efa5155a
RM
141 }
142
143 /**
144 * Look up the category record based on cateogry ID and context
145 * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display()
d62382d1 146 * @return \stdClass The category record
efa5155a
RM
147 */
148 protected function get_current_category($categoryandcontext) {
149 global $DB, $OUTPUT;
150 list($categoryid, $contextid) = explode(',', $categoryandcontext);
151 if (!$categoryid) {
152 $this->print_choose_category_message($categoryandcontext);
153 return false;
154 }
155
156 if (!$category = $DB->get_record('question_categories',
157 array('id' => $categoryid, 'contextid' => $contextid))) {
158 echo $OUTPUT->box_start('generalbox questionbank');
159 echo $OUTPUT->notification('Category not found!');
160 echo $OUTPUT->box_end();
161 return false;
162 }
163
164 return $category;
165 }
166
167 /**
168 * Print the category description
e22e7490 169 * @param stdClass $category the category information form the database.
efa5155a
RM
170 */
171 protected function print_category_info($category) {
d62382d1 172 $formatoptions = new \stdClass();
efa5155a
RM
173 $formatoptions->noclean = true;
174 $formatoptions->overflowdiv = true;
d62382d1 175 echo \html_writer::start_div('boxaligncenter categoryinfo');
efa5155a
RM
176 if (isset($this->maxinfolength)) {
177 echo shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id),
178 $this->maxinfolength);
179 } else {
180 echo format_text($category->info, $category->infoformat, $formatoptions, $this->course->id);
181 }
d62382d1 182 echo \html_writer::end_div() . "\n";
efa5155a 183 }
efa5155a 184}