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
namespace qbank_viewcreator;
18
 
19
use core\exception\moodle_exception;
20
use core\plugininfo\filter;
21
use core_question\local\bank\condition;
22
 
23
/**
24
 * Filter condition for date/time modified
25
 *
26
 * @package   qbank_viewcreator
27
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
28
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class timemodified_condition extends condition {
32
    /**
33
     * @var string Search for times before the specified date.
34
     */
35
    const MODE_BEFORE = 'before';
36
 
37
    /**
38
     * @var string Search for times after the specified date.
39
     */
40
    const MODE_AFTER = 'after';
41
 
42
    /**
43
     * @var string Search for times between the specified dates.
44
     */
45
    const MODE_BETWEEN = 'between';
46
 
47
    #[\Override]
48
    public function get_title() {
49
        return get_string('timemodified', 'qbank_viewcreator');
50
    }
51
 
52
    #[\Override]
53
    public static function get_condition_key() {
54
        return 'timemodified';
55
    }
56
 
57
    #[\Override]
58
    public function get_filter_class() {
59
        return 'core/datafilter/filtertypes/datetime';
60
    }
61
 
62
    /**
63
     * Set a single valid jointype, so we don't display the jointype selector.
64
     *
65
     * We have a separate filter option to control how this condition is applied, Any/All/None doesn't apply here.
66
     *
67
     * @return array
68
     */
69
    public function get_join_list(): array {
70
        return [
71
            self::JOINTYPE_DEFAULT,
72
        ];
73
    }
74
 
75
    /**
76
     * Build an SQL WHERE condition to filter questions based on q.timemodified.
77
     *
78
     * $filter['values'][0] contains the datetime to search after, $filter['values'][1] contains the datetime
79
     * to search before. Whether to use these dates to search after, before, or between these dates is determined
80
     * by the value of $filter['fileroptions']['mode'].
81
     *
82
     * The datetime values are in the format YYYY-MM-DDTHH:mm, as provided by the datetime-local input type.
83
     *
84
     * @param array $filter ['values' => [$before, $after], 'filteroptions' => ['mode' => $mode]]
85
     * @return array
86
     * @throws moodle_exception If an invalid mode or range is provided.
87
     */
88
    public static function build_query_from_filter(array $filter): array {
89
        if (!isset($filter['filteroptions']['mode']) || empty($filter['values'])) {
90
            return ['', []];
91
        }
92
        $mode = $filter['filteroptions']['mode'];
93
        if (!in_array($mode, [self::MODE_AFTER, self::MODE_BEFORE, self::MODE_BETWEEN])) {
94
            throw new moodle_exception('invaliddatetimemode', 'error', a: $filter['filteroptions']['mode']);
95
        }
96
        $tz = new \DateTimeZone(\core_date::get_user_timezone(99));
97
        $datetimeafter = new \DateTime($filter['values'][0], $tz);
98
        $datetimebefore = new \DateTime($filter['values'][1], $tz);
99
        if ($mode === self::MODE_AFTER) {
100
            $conditions = 'q.timemodified > :timemodifiedafter';
101
            $params['timemodifiedafter'] = $datetimeafter->getTimestamp();
102
        } else if ($mode === self::MODE_BEFORE) {
103
            $conditions = 'q.timemodified < :timemodifiedbefore';
104
            $params['timemodifiedbefore'] = $datetimebefore->getTimestamp();
105
        } else {
106
            if ($datetimeafter > $datetimebefore) {
107
                throw new moodle_exception(
108
                    'invaliddatetimebetween',
109
                    'error',
110
                    a: (object) [
111
                        'before' => $datetimebefore->format('Y-m-d H:i'),
112
                        'after' => $datetimeafter->format('Y-m-d H:i'),
113
                    ],
114
                );
115
            }
116
            $conditions = 'q.timemodified > :timemodifiedafter AND q.timemodified < :timemodifiedbefore';
117
            $params = [
118
                'timemodifiedafter' => $datetimeafter->getTimestamp(),
119
                'timemodifiedbefore' => $datetimebefore->getTimestamp(),
120
            ];
121
        }
122
 
123
        return [$conditions, $params];
124
    }
125
 
126
    /**
127
     * Return the default datetime values for the filter.
128
     *
129
     * This generates values formatted for datetime-local fields. The first value returned is the current time,
130
     * for use as the default "before" datetime. The second is midnight 1 week ago, for use as the default "after"
131
     * datetime.
132
     *
133
     * @return array[]
134
     */
135
    public function get_initial_values(): array {
136
        $tz = new \DateTimeZone(\core_date::get_user_timezone());
137
        // Datetime format used by the <input type="datetime-local"> field.
138
        $format = 'Y-m-d\TH:i';
139
        $now = (new \DateTime('now', $tz))->format($format);
140
        $oneweek = (new \DateTime('midnight 1 week ago', $tz))->format($format);
141
        return [
142
            [
143
                'value' => $now,
144
                'title' => $now,
145
            ],
146
            [
147
                'value' => $oneweek,
148
                'title' => $oneweek,
149
            ],
150
        ];
151
    }
152
}