Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 39 and 311]

   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  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   *  This class controls from which category questions are listed.
  31   *
  32   * @copyright 2013 Ray Morris
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class category_condition extends condition {
  36      /** @var \stdClass The course record. */
  37      protected $course;
  38  
  39      /** @var \stdClass The category record. */
  40      protected $category;
  41  
  42      /** @var array of contexts. */
  43      protected $contexts;
  44  
  45      /** @var bool Whether to include questions from sub-categories. */
  46      protected $recurse;
  47  
  48      /** @var string SQL fragment to add to the where clause. */
  49      protected $where;
  50  
  51      /** @var array query param used in where. */
  52      protected $params;
  53  
  54      /** @var string categoryID,contextID as used with question_bank_view->display(). */
  55      protected $cat;
  56  
  57      /** @var int The maximum displayed length of the category info. */
  58      protected $maxinfolength;
  59  
  60      /**
  61       * Constructor
  62       * @param string     $cat           categoryID,contextID as used with question_bank_view->display()
  63       * @param bool       $recurse       Whether to include questions from sub-categories
  64       * @param array      $contexts      Context objects as used by question_category_options()
  65       * @param \moodle_url $baseurl       The URL the form is submitted to
  66       * @param \stdClass   $course        Course record
  67       * @param integer    $maxinfolength The maximum displayed length of the category info.
  68       */
  69      public function __construct($cat, $recurse, $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  
  96      public function where() {
  97          return  $this->where;
  98      }
  99  
 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() {
 117          echo \html_writer::start_div();
 118          echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'recurse',
 119                                                 'value' => 0, 'id' => 'recurse_off'));
 120          echo \html_writer::checkbox('recurse', '1', $this->recurse, get_string('includesubcategories', 'question'),
 121                                         array('id' => 'recurse_on', 'class' => 'searchoptions mr-1'));
 122          echo \html_writer::end_div() . "\n";
 123      }
 124  
 125      /**
 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'.
 131       */
 132      protected function display_category_form($contexts, $pageurl, $current) {
 133          echo \html_writer::start_div('choosecategory');
 134          $catmenu = question_category_options($contexts, true, 0, true, -1, false);
 135          echo \html_writer::label(get_string('selectacategory', 'question'), 'id_selectacategory', true, array("class" => "mr-1"));
 136          echo \html_writer::select($catmenu, 'category', $current, array(),
 137                  array('class' => 'searchoptions custom-select', 'id' => 'id_selectacategory'));
 138          echo \html_writer::end_div() . "\n";
 139      }
 140  
 141      /**
 142       * Look up the category record based on cateogry ID and context
 143       * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display()
 144       * @return \stdClass The category record
 145       */
 146      protected function get_current_category($categoryandcontext) {
 147          global $DB, $OUTPUT;
 148          list($categoryid, $contextid) = explode(',', $categoryandcontext);
 149          if (!$categoryid) {
 150              $this->print_choose_category_message($categoryandcontext);
 151              return false;
 152          }
 153  
 154          if (!$category = $DB->get_record('question_categories',
 155                  array('id' => $categoryid, 'contextid' => $contextid))) {
 156              echo $OUTPUT->box_start('generalbox questionbank');
 157              echo $OUTPUT->notification('Category not found!');
 158              echo $OUTPUT->box_end();
 159              return false;
 160          }
 161  
 162          return $category;
 163      }
 164  
 165      /**
 166       * Print the category description
 167       * @param stdClass $category the category information form the database.
 168       */
 169      protected function print_category_info($category) {
 170          $formatoptions = new \stdClass();
 171          $formatoptions->noclean = true;
 172          $formatoptions->overflowdiv = true;
 173          echo \html_writer::start_div('boxaligncenter categoryinfo pl-0');
 174          if (isset($this->maxinfolength)) {
 175              echo shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id),
 176                                       $this->maxinfolength);
 177          } else {
 178              echo format_text($category->info, $category->infoformat, $formatoptions, $this->course->id);
 179          }
 180          echo \html_writer::end_div() . "\n";
 181      }
 182  }