Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 403]

   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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\local\filters;
  20  
  21  use core_course_category;
  22  use MoodleQuickForm;
  23  use core_reportbuilder\local\helpers\database;
  24  
  25  /**
  26   * Course category report filter
  27   *
  28   * The following optional array property can be passed to the {@see \core_reportbuilder\local\report\filter::set_options} method
  29   * when defining this filter, to define the capabilities passed to {@see \core_course_category::make_categories_list}
  30   *
  31   * ['requiredcapabilities' => '...']
  32   *
  33   * @package     core_reportbuilder
  34   * @copyright   2022 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class category extends base {
  38  
  39      /**
  40       * Setup form
  41       *
  42       * @param MoodleQuickForm $mform
  43       */
  44      public function setup_form(MoodleQuickForm $mform): void {
  45          $label = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
  46  
  47          // See MDL-74627: in order to set the default value to "No selection" we need to prepend an empty value.
  48          $requiredcapabilities = $this->filter->get_options()['requiredcapabilities'] ?? '';
  49          $categories = [0 => ''] + core_course_category::make_categories_list($requiredcapabilities);
  50  
  51          $mform->addElement('autocomplete', "{$this->name}_value", $label, $categories)->setHiddenLabel(true);
  52          $mform->addElement('advcheckbox', "{$this->name}_subcategories", get_string('viewallsubcategories'));
  53      }
  54  
  55      /**
  56       * Return filter SQL
  57       *
  58       * @param array $values
  59       * @return array
  60       */
  61      public function get_sql_filter(array $values): array {
  62          global $DB;
  63  
  64          [$fieldsql, $params] = $this->filter->get_field_sql_and_params();
  65  
  66          $category = (int) ($values["{$this->name}_value"] ?? 0);
  67          $subcategories = !empty($values["{$this->name}_subcategories"]);
  68  
  69          // Invalid or inactive filter.
  70          if (empty($category)) {
  71              return ['', []];
  72          }
  73  
  74          // Initial matching on selected category.
  75          $paramcategory = database::generate_param_name();
  76          $params[$paramcategory] = $category;
  77          $sql = "{$fieldsql} = :{$paramcategory}";
  78  
  79          // Sub-category matching on path of selected category.
  80          if ($subcategories) {
  81  
  82              // We need to re-use the original filter SQL here, while ensuring parameter uniqueness is preserved.
  83              [$fieldsql, $params1] = $this->filter->get_field_sql_and_params(1);
  84              $params = array_merge($params, $params1);
  85  
  86              $paramcategorypath = database::generate_param_name();
  87              $params[$paramcategorypath] = "%/{$category}/%";
  88              $sql .= " OR {$fieldsql} IN (
  89                  SELECT id
  90                    FROM {course_categories}
  91                   WHERE " . $DB->sql_like('path', ":{$paramcategorypath}") . "
  92              )";
  93          }
  94  
  95          return [$sql, $params];
  96      }
  97  
  98      /**
  99       * Return sample filter values
 100       *
 101       * @return array
 102       */
 103      public function get_sample_values(): array {
 104          return [
 105              "{$this->name}_value" => 1,
 106          ];
 107      }
 108  }