Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 lang_string;
22
use MoodleQuickForm;
23
use core_reportbuilder\local\helpers\database;
24
 
25
/**
26
 * Duration report filter
27
 *
28
 * This filter accepts a number of seconds to perform filtering on
29
 *
30
 * @package     core_reportbuilder
31
 * @copyright   2021 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class duration extends base {
35
 
36
    /** @var int Any value */
37
    public const DURATION_ANY = 0;
38
 
39
    /** @var int Maximum duration */
40
    public const DURATION_MAXIMUM = 1;
41
 
42
    /** @var int Minimum duration */
43
    public const DURATION_MINIMUM = 2;
44
 
45
 
46
    /**
47
     * Return an array of operators available for this filter
48
     *
49
     * @return lang_string[]
50
     */
51
    private function get_operators(): array {
52
        $operators = [
53
            self::DURATION_ANY => new lang_string('filterisanyvalue', 'core_reportbuilder'),
54
            self::DURATION_MAXIMUM => new lang_string('filterlessthan', 'core_reportbuilder'),
55
            self::DURATION_MINIMUM => new lang_string('filtergreaterthan', 'core_reportbuilder'),
56
        ];
57
 
58
        return $this->filter->restrict_limited_operators($operators);
59
    }
60
 
61
    /**
62
     * Setup form
63
     *
64
     * @param MoodleQuickForm $mform
65
     */
66
    public function setup_form(MoodleQuickForm $mform): void {
67
        $elements = [];
68
 
69
        // Operator.
70
        $operatorlabel = get_string('filterfieldoperator', 'core_reportbuilder', $this->get_header());
71
 
72
        $elements[] = $mform->createElement('select', "{$this->name}_operator", $operatorlabel, $this->get_operators());
73
        $mform->setType("{$this->name}_operator", PARAM_INT);
74
        $mform->setDefault("{$this->name}_operator", self::DURATION_ANY);
75
 
76
        // Value.
77
        $valuelabel = get_string('filterfieldvalue', 'core_reportbuilder', $this->get_header());
78
 
79
        $elements[] = $mform->createElement('text', "{$this->name}_value", $valuelabel, ['size' => 3]);
80
        $mform->setType("{$this->name}_value", PARAM_FLOAT);
81
        $mform->setDefault("{$this->name}_value", 0);
82
        $mform->hideIf("{$this->name}_value", "{$this->name}_operator", 'eq', self::DURATION_ANY);
83
 
84
        // Unit.
85
        $unitlabel = get_string('filterfieldunit', 'core_reportbuilder', $this->get_header());
86
        $units = [
87
            1 => get_string('filterdateseconds', 'core_reportbuilder'),
88
            MINSECS => get_string('filterdateminutes', 'core_reportbuilder'),
89
            HOURSECS => get_string('filterdatehours', 'core_reportbuilder'),
90
            DAYSECS => get_string('filterdatedays', 'core_reportbuilder'),
91
            WEEKSECS => get_string('filterdateweeks', 'core_reportbuilder'),
92
        ];
93
 
94
        $elements[] = $mform->createElement('select', "{$this->name}_unit", $unitlabel, $units);
95
        $mform->setType("{$this->name}_unit", PARAM_INT);
96
        $mform->setDefault("{$this->name}_unit", 1);
97
        $mform->hideIf("{$this->name}_unit", "{$this->name}_operator", 'eq', self::DURATION_ANY);
98
 
99
        $mform->addGroup($elements, "{$this->name}_group", $this->get_header(), '', false)
100
            ->setHiddenLabel(true);
101
    }
102
 
103
    /**
104
     * Return filter SQL
105
     *
106
     * @param array $values
107
     * @return array
108
     */
109
    public function get_sql_filter(array $values): array {
110
        $fieldsql = $this->filter->get_field_sql();
111
        $params = $this->filter->get_field_params();
112
 
113
        $durationvalue = unformat_float($values["{$this->name}_value"] ?? 0);
114
        $durationunit = (int) ($values["{$this->name}_unit"] ?? 0);
115
 
116
        $operator = $values["{$this->name}_operator"] ?? self::DURATION_ANY;
117
        switch ($operator) {
118
            case self::DURATION_MAXIMUM:
119
                $paramduration = database::generate_param_name();
120
 
121
                $sql = "{$fieldsql} <= :{$paramduration}";
122
                $params[$paramduration] = $durationvalue * $durationunit;
123
 
124
                break;
125
            case self::DURATION_MINIMUM:
126
                $paramduration = database::generate_param_name();
127
 
128
                $sql = "{$fieldsql} >= :{$paramduration}";
129
                $params[$paramduration] = $durationvalue * $durationunit;
130
 
131
                break;
132
            default:
133
                // Invalid or inactive filter.
134
                return ['', []];
135
        }
136
 
137
        return [$sql, $params];
138
    }
139
 
140
    /**
141
     * Return sample filter values
142
     *
143
     * @return array
144
     */
145
    public function get_sample_values(): array {
146
        return [
147
            "{$this->name}_operator" => self::DURATION_MAXIMUM,
148
            "{$this->name}_value" => 2,
149
            "{$this->name}_unit" => MINSECS,
150
        ];
151
    }
152
}