Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 MoodleQuickForm;
  22  use core_reportbuilder\local\helpers\database;
  23  
  24  /**
  25   * Course selector filter class implementation
  26   *
  27   * @package     core_reportbuilder
  28   * @copyright   2021 David Matamoros <davidmc@moodle.com>.
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class course_selector extends base {
  32  
  33      /**
  34       * Setup form
  35       *
  36       * @param MoodleQuickForm $mform
  37       */
  38      public function setup_form(MoodleQuickForm $mform): void {
  39          $operatorlabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
  40          $options = [
  41              'multiple' => true,
  42          ];
  43  
  44          $mform->addElement('course', $this->name . '_values', $operatorlabel, $options)
  45              ->setHiddenLabel(true);
  46      }
  47  
  48      /**
  49       * Return filter SQL
  50       *
  51       * @param array $values
  52       * @return array
  53       */
  54      public function get_sql_filter(array $values): array {
  55          global $DB;
  56  
  57          $fieldsql = $this->filter->get_field_sql();
  58          $params = $this->filter->get_field_params();
  59  
  60          $courseids = $values["{$this->name}_values"] ?? [];
  61          if (empty($courseids)) {
  62              return ['', []];
  63          }
  64  
  65          $paramprefix = database::generate_param_name() . '_';
  66          [$courseselect, $courseparams] = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, $paramprefix);
  67  
  68          return ["{$fieldsql} $courseselect", array_merge($params, $courseparams)];
  69      }
  70  
  71      /**
  72       * Return sample filter values
  73       *
  74       * @return array
  75       */
  76      public function get_sample_values(): array {
  77          return [
  78              "{$this->name}_values" => [1],
  79          ];
  80      }
  81  }