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