| 1 | efrain | 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 |  * Cohort selector filter class implementation
 | 
        
           |  |  | 26 |  *
 | 
        
           |  |  | 27 |  * @package     core_reportbuilder
 | 
        
           |  |  | 28 |  * @copyright   2024 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 29 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 30 |  */
 | 
        
           |  |  | 31 | class cohort extends base {
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 |     /**
 | 
        
           |  |  | 34 |      * Setup form
 | 
        
           |  |  | 35 |      *
 | 
        
           |  |  | 36 |      * @param MoodleQuickForm $mform
 | 
        
           |  |  | 37 |      */
 | 
        
           |  |  | 38 |     public function setup_form(MoodleQuickForm $mform): void {
 | 
        
           |  |  | 39 |         $mform->addElement(
 | 
        
           |  |  | 40 |             'cohort',
 | 
        
           |  |  | 41 |             "{$this->name}_values",
 | 
        
           |  |  | 42 |             get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header()),
 | 
        
           |  |  | 43 |             [
 | 
        
           |  |  | 44 |                 'multiple' => true,
 | 
        
           |  |  | 45 |             ],
 | 
        
           |  |  | 46 |         )->setHiddenLabel(true);
 | 
        
           |  |  | 47 |     }
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |     /**
 | 
        
           |  |  | 50 |      * Return filter SQL
 | 
        
           |  |  | 51 |      *
 | 
        
           |  |  | 52 |      * @param array $values
 | 
        
           |  |  | 53 |      * @return array
 | 
        
           |  |  | 54 |      */
 | 
        
           |  |  | 55 |     public function get_sql_filter(array $values): array {
 | 
        
           |  |  | 56 |         global $DB;
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |         $fieldsql = $this->filter->get_field_sql();
 | 
        
           |  |  | 59 |         $params = $this->filter->get_field_params();
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |         $cohortids = $values["{$this->name}_values"] ?? [];
 | 
        
           |  |  | 62 |         if (empty($cohortids)) {
 | 
        
           |  |  | 63 |             return ['', []];
 | 
        
           |  |  | 64 |         }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         [$cohortselect, $cohortparams] = $DB->get_in_or_equal(
 | 
        
           |  |  | 67 |             $cohortids,
 | 
        
           |  |  | 68 |             SQL_PARAMS_NAMED,
 | 
        
           |  |  | 69 |             database::generate_param_name('_'),
 | 
        
           |  |  | 70 |         );
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         return ["{$fieldsql} $cohortselect", array_merge($params, $cohortparams)];
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |     /**
 | 
        
           |  |  | 76 |      * Return sample filter values
 | 
        
           |  |  | 77 |      *
 | 
        
           |  |  | 78 |      * @return array
 | 
        
           |  |  | 79 |      */
 | 
        
           |  |  | 80 |     public function get_sample_values(): array {
 | 
        
           |  |  | 81 |         return [
 | 
        
           |  |  | 82 |             "{$this->name}_values" => [1],
 | 
        
           |  |  | 83 |         ];
 | 
        
           |  |  | 84 |     }
 | 
        
           |  |  | 85 | }
 |