Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_reportbuilder\local\helpers\audience as audience_helper;
22
use core_reportbuilder\local\models\audience as audience_model;
23
 
24
/**
25
 * Report audience filter
26
 *
27
 * Specific to the report access list, to allow for filtering of the user list according to the audience they belong to
28
 *
29
 * In order to specify for which report we are viewing the access list for, the following options must be passed
30
 * to the filter {@see \core_reportbuilder\local\report\filter::set_options} method
31
 *
32
 * ['reportid' => '...']
33
 *
34
 * @package     core_reportbuilder
35
 * @copyright   2024 Paul Holden <paulh@moodle.com>
36
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class audience extends select {
39
 
40
    /**
41
     * Return the options for the filter as an array, to be used to populate the select input field
42
     *
43
     * @return array
44
     */
45
    protected function get_select_options(): array {
46
        $options = [];
47
 
48
        $audiences = audience_helper::get_base_records($this->filter->get_options()['reportid'] ?? 0);
49
        foreach ($audiences as $audience) {
50
            $persistent = $audience->get_persistent();
51
 
52
            // Check for a custom name, otherwise fall back to default.
53
            if ('' === $audiencelabel = $persistent->get_formatted_heading()) {
54
                $audiencelabel = $audience->get_name();
55
            }
56
 
57
            $options[$persistent->get('id')] = $audiencelabel;
58
        }
59
 
60
        return $options;
61
    }
62
 
63
    /**
64
     * Return filter SQL
65
     *
66
     * @param array $values
67
     * @return array
68
     */
69
    public function get_sql_filter(array $values): array {
70
        $reportid = $this->filter->get_options()['reportid'] ?? 0;
71
 
72
        $operator = (int) ($values["{$this->name}_operator"] ?? self::ANY_VALUE);
73
        $audienceid = (int) ($values["{$this->name}_value"] ?? 0);
74
 
75
        switch ($operator) {
76
            case self::EQUAL_TO:
77
            case self::NOT_EQUAL_TO:
78
                $audience = audience_model::get_record(['id' => $audienceid, 'reportid' => $reportid]);
79
                if ($audience === false) {
80
                    return ['', []];
81
                }
82
 
83
                // Generate audience SQL, invert it for "not equal to".
84
                [$select, $params] = audience_helper::user_audience_single_sql($audience, $this->filter->get_field_sql());
85
                if ($operator === self::NOT_EQUAL_TO) {
86
                    $select = "NOT {$select}";
87
                }
88
 
89
                break;
90
            default:
91
                return ['', []];
92
        }
93
 
94
        return [$select, $params];
95
    }
96
}