Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
1 <?php 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 namespace core_question\bank\search; 27 28 use qbank_managecategories\helper; 29 30 /** 31 * This class controls from which category questions are listed. 32 * 33 * @copyright 2013 Ray Morris 34 * @author 2021 Safat Shahin <safatshahin@catalyst-au.net> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class category_condition extends condition { 38 /** @var \stdClass The course record. */ 39 protected $course; 40 41 /** @var \stdClass The category record. */ 42 protected $category; 43 44 /** @var array of contexts. */ 45 protected $contexts; 46 47 /** @var bool Whether to include questions from sub-categories. */ 48 protected $recurse; 49 50 /** @var string SQL fragment to add to the where clause. */ 51 protected $where; 52 53 /** @var array query param used in where. */ 54 protected $params; 55 56 /** @var string categoryID,contextID as used with question_bank_view->display(). */ 57 protected $cat; 58 59 /** @var int The maximum displayed length of the category info. */ 60 protected $maxinfolength; 61 62 /** @var \moodle_url The URL the form is submitted to. */ 63 protected $baseurl; 64 65 /** 66 * Constructor 67 * @param string $cat categoryID,contextID as used with question_bank_view->display() 68 * @param bool $recurse Whether to include questions from sub-categories 69 * @param array $contexts Context objects as used by question_category_options() 70 * @param \moodle_url $baseurl The URL the form is submitted to 71 * @param \stdClass $course Course record 72 * @param integer $maxinfolength The maximum displayed length of the category info. 73 */ 74 public function __construct($cat, $recurse, $contexts, $baseurl, $course, $maxinfolength = null) { 75 $this->cat = $cat; 76 $this->recurse = $recurse; 77 $this->contexts = $contexts; 78 $this->baseurl = $baseurl; 79 $this->course = $course; 80 $this->init(); 81 $this->maxinfolength = $maxinfolength; 82 } 83 84 /** 85 * Initialize the object so it will be ready to return where() and params() 86 */ 87 private function init() { 88 global $DB; 89 if (!$this->category = $this->get_current_category($this->cat)) { 90 return; 91 } 92 if ($this->recurse) { 93 $categoryids = question_categorylist($this->category->id); 94 } else { 95 $categoryids = [$this->category->id]; 96 } 97 list($catidtest, $this->params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cat'); 98 $this->where = 'qbe.questioncategoryid ' . $catidtest; 99 } 100 101 /** 102 * SQL fragment to add to the where clause. 103 * 104 * @return string 105 */ 106 public function where() { 107 return $this->where; 108 } 109 110 /** 111 * Return parameters to be bound to the above WHERE clause fragment. 112 * @return array parameter name => value. 113 */ 114 public function params() { 115 return $this->params; 116 } 117 118 /** 119 * Called by question_bank_view to display the GUI for selecting a category 120 */ 121 public function display_options() { 122 global $PAGE; 123 $displaydata = []; 124 $catmenu = helper::question_category_options($this->contexts, true, 0, 125 true, -1, false); 126 $displaydata['categoryselect'] = \html_writer::select($catmenu, 'category', $this->cat, [], 127 array('class' => 'searchoptions custom-select', 'id' => 'id_selectacategory')); 128 $displaydata['categorydesc'] = ''; 129 if ($this->category) { 130 $displaydata['categorydesc'] = $this->print_category_info($this->category); 131 } 132 return $PAGE->get_renderer('core_question', 'bank')->render_category_condition($displaydata); 133 } 134 135 /** 136 * Displays the recursion checkbox GUI. 137 * question_bank_view places this within the section that is hidden by default 138 */ 139 public function display_options_adv() { 140 global $PAGE; 141 $displaydata = []; 142 if ($this->recurse) { 143 $displaydata['checked'] = 'checked'; 144 } 145 return $PAGE->get_renderer('core_question', 'bank')->render_category_condition_advanced($displaydata); 146 } 147 148 /** 149 * Display the drop down to select the category. 150 * 151 * @param array $contexts of contexts that can be accessed from here. 152 * @param \moodle_url $pageurl the URL of this page. 153 * @param string $current 'categoryID,contextID'. 154 * @deprecated since Moodle 4.0 155 */ 156 protected function display_category_form($contexts, $pageurl, $current) { 157 debugging('Function display_category_form() is deprecated, 158 please use the core_question renderer instead.', DEBUG_DEVELOPER); 159 echo \html_writer::start_div('choosecategory'); 160 $catmenu = question_category_options($contexts, true, 0, true, -1, false); 161 echo \html_writer::label(get_string('selectacategory', 'question'), 'id_selectacategory', true, ["class" => "mr-1"]); 162 echo \html_writer::select($catmenu, 'category', $current, [], 163 array('class' => 'searchoptions custom-select', 'id' => 'id_selectacategory')); 164 echo \html_writer::end_div() . "\n"; 165 } 166 167 /** 168 * Look up the category record based on cateogry ID and context 169 * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display() 170 * @return \stdClass The category record 171 */ 172 protected function get_current_category($categoryandcontext) { 173 global $DB, $OUTPUT; 174 list($categoryid, $contextid) = explode(',', $categoryandcontext); 175 if (!$categoryid) { 176 $this->print_choose_category_message($categoryandcontext); 177 return false; 178 } 179 180 if (!$category = $DB->get_record('question_categories', ['id' => $categoryid, 'contextid' => $contextid])) { 181 echo $OUTPUT->box_start('generalbox questionbank'); 182 echo $OUTPUT->notification('Category not found!'); 183 echo $OUTPUT->box_end(); 184 return false; 185 } 186 187 return $category; 188 } 189 190 /** 191 * Print the category description 192 * @param \stdClass $category the category information form the database. 193 */ 194 protected function print_category_info($category): string { 195 $formatoptions = new \stdClass(); 196 $formatoptions->noclean = true; 197 $formatoptions->overflowdiv = true; 198 if (isset($this->maxinfolength)) { 199 return shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id), 200 $this->maxinfolength); 201 } else { 202 return format_text($category->info, $category->infoformat, $formatoptions, $this->course->id); 203 } 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body