Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 *
1441 ariadna 28
 * This filter accepts a number of seconds to perform filtering on (note that the value will be cast to float prior to comparison)
1 efrain 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]);
1441 ariadna 80
        $mform->setType("{$this->name}_value", PARAM_LOCALISEDFLOAT);
1 efrain 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 {
1441 ariadna 110
        global $DB;
111
 
1 efrain 112
        $fieldsql = $this->filter->get_field_sql();
113
        $params = $this->filter->get_field_params();
114
 
1441 ariadna 115
        $operator = (int) ($values["{$this->name}_operator"] ?? self::DURATION_ANY);
116
 
1 efrain 117
        $durationvalue = unformat_float($values["{$this->name}_value"] ?? 0);
118
        $durationunit = (int) ($values["{$this->name}_unit"] ?? 0);
119
 
1441 ariadna 120
        $paramduration = database::generate_param_name();
121
        $params[$paramduration] = $durationvalue * $durationunit;
122
 
1 efrain 123
        switch ($operator) {
124
            case self::DURATION_MAXIMUM:
1441 ariadna 125
                $sql = $DB->sql_cast_char2real("({$fieldsql})") . " <= :{$paramduration}";
1 efrain 126
                break;
127
            case self::DURATION_MINIMUM:
1441 ariadna 128
                $sql = $DB->sql_cast_char2real("({$fieldsql})") . " >= :{$paramduration}";
1 efrain 129
                break;
130
            default:
131
                // Invalid or inactive filter.
132
                return ['', []];
133
        }
134
 
135
        return [$sql, $params];
136
    }
137
 
138
    /**
139
     * Return sample filter values
140
     *
141
     * @return array
142
     */
143
    public function get_sample_values(): array {
144
        return [
145
            "{$this->name}_operator" => self::DURATION_MAXIMUM,
146
            "{$this->name}_value" => 2,
147
            "{$this->name}_unit" => MINSECS,
148
        ];
149
    }
150
}