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
namespace tool_usertours\local\filter;
17
 
18
use context;
19
use tool_usertours\tour;
20
 
21
/**
22
 * Access date filter. Used to determine if USER should see a tour based on a particular access date.
23
 *
24
 * @package    tool_usertours
25
 * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class accessdate extends base {
29
    /**
30
     * Access date filtering constant for setting base date as account creation date.
31
     */
32
    const FILTER_ACCOUNT_CREATION = 'tool_usertours_accountcreation';
33
 
34
    /**
35
     * Access date filtering constant for setting base date as account first login date.
36
     */
37
    const FILTER_FIRST_LOGIN = 'tool_usertours_firstlogin';
38
 
39
    /**
40
     * Access date filtering constant for setting base date as account last login date.
41
     */
42
    const FILTER_LAST_LOGIN = 'tool_usertours_lastlogin';
43
 
44
    /**
45
     * Default this filter to not be enabled.
46
     */
47
    const FILTER_ENABLED_DEFAULT = 0;
48
 
49
    /**
50
     * The name of the filter.
51
     *
52
     * @return  string
53
     */
54
    public static function get_filter_name() {
55
        return 'accessdate';
56
    }
57
 
58
    /**
59
     * Retrieve the list of available filter options.
60
     *
61
     * @return  array  An array whose keys are the valid options
62
     *                 And whose values are the values to display
63
     * @throws \coding_exception
64
     */
65
    public static function get_filter_options() {
66
 
67
        return [
68
            self::FILTER_ACCOUNT_CREATION => get_string('filter_date_account_creation', 'tool_usertours'),
69
            self::FILTER_FIRST_LOGIN => get_string('filter_date_first_login', 'tool_usertours'),
70
            self::FILTER_LAST_LOGIN => get_string('filter_date_last_login', 'tool_usertours'),
71
        ];
72
    }
73
 
74
    /**
75
     * Add the form elements for the filter to the supplied form.
76
     *
77
     * @param \MoodleQuickForm $mform The form to add filter settings to.
78
     *
79
     * @throws \coding_exception
80
     */
81
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
82
 
83
        $filtername = static::get_filter_name();
84
        $key = "filter_{$filtername}";
85
        $range = "{$key}_range";
86
        $enabled = "{$key}_enabled";
87
 
88
        $mform->addElement(
89
            'advcheckbox',
90
            $enabled,
91
            get_string($key, 'tool_usertours'),
92
            get_string('filter_accessdate_enabled', 'tool_usertours'),
93
            null,
94
            [0, 1]
95
        );
96
        $mform->addHelpButton($enabled, $enabled, 'tool_usertours');
97
 
98
        $mform->addElement('select', $key, ' ', self::get_filter_options());
99
        $mform->setDefault($key, self::FILTER_ACCOUNT_CREATION);
100
        $mform->hideIf($key, $enabled, 'notchecked');
101
 
102
        $mform->addElement('duration', $range, null, [
103
            'optional' => false,
104
            'defaultunit' => DAYSECS,
105
        ]);
106
        $mform->setDefault($range, 90 * DAYSECS);
107
        $mform->hideIf($range, $enabled, 'notchecked');
108
    }
109
 
110
    /**
111
     * Prepare the filter values for the form.
112
     *
113
     * @param   tour            $tour       The tour to prepare values from
114
     * @param   stdClass        $data       The data value
115
     * @return  stdClass
116
     */
117
    public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
118
        $filtername = static::get_filter_name();
119
 
120
        $key = "filter_{$filtername}";
121
        $range = "{$key}_range";
122
        $enabled = "{$key}_enabled";
123
 
124
        $values = $tour->get_filter_values($filtername);
125
 
126
        // Prepare the advanced checkbox value and prepare filter values based on previously set values.
127
        if (!empty($values)) {
128
            $data->$enabled = $values->$enabled ? $values->$enabled : self::FILTER_ENABLED_DEFAULT;
129
            if ($data->$enabled) {
130
                if (isset($values->$key)) {
131
                    $data->$key = $values->$key;
132
                }
133
                if (isset($values->$range)) {
134
                    $data->$range = $values->$range;
135
                }
136
            }
137
        } else {
138
            $data->$enabled = self::FILTER_ENABLED_DEFAULT;
139
        }
140
        return $data;
141
    }
142
 
143
    /**
144
     * Save the filter values from the form to the tour.
145
     *
146
     * @param   tour            $tour       The tour to save values to
147
     * @param   \stdClass        $data       The data submitted in the form
148
     */
149
    public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
150
        $filtername = static::get_filter_name();
151
        $key = "filter_{$filtername}";
152
        $range = "{$key}_range";
153
        $enabled = "{$key}_enabled";
154
 
155
        $savedata = [];
156
        $savedata[$key] = $data->$key;
157
        $savedata[$range] = $data->$range;
158
        $savedata[$enabled] = $data->$enabled;
159
 
160
        $tour->set_filter_values($filtername, $savedata);
161
    }
162
 
163
    /**
164
     * Check whether the filter matches the specified tour and/or context.
165
     *
166
     * @param   tour        $tour       The tour to check
167
     * @param   context     $context    The context to check
168
     * @return  boolean
169
     */
170
    public static function filter_matches(tour $tour, context $context) {
171
        global $USER;
172
 
173
        $filtername = static::get_filter_name();
174
        $key = "filter_{$filtername}";
175
        $range = "{$key}_range";
176
        $enabled = "{$key}_enabled";
177
 
178
        // Default behaviour is to match filter.
179
        $result = true;
180
        $values = (array) $tour->get_filter_values(self::get_filter_name());
181
 
182
        // If the access date filter is not enabled, end here.
183
        if (empty($values[$enabled])) {
184
            return $result;
185
        }
186
 
187
        if (!empty($values[$key])) {
188
            switch ($values[$key]) {
189
                case (self::FILTER_ACCOUNT_CREATION):
190
                    $filterbasedate = (int) $USER->timecreated;
191
                    break;
192
                case (self::FILTER_FIRST_LOGIN):
193
                    $filterbasedate = (int) $USER->firstaccess;
194
                    break;
195
                case (self::FILTER_LAST_LOGIN):
196
                    $filterbasedate = (int) $USER->lastlogin;
197
                    break;
198
                default:
199
                    // Use account creation as default.
200
                    $filterbasedate = (int) $USER->timecreated;
201
                    break;
202
            }
203
            // If the base date has no value because a user hasn't accessed Moodle yet, default to account creation.
204
            if (empty($filterbasedate)) {
205
                $filterbasedate = (int) $USER->timecreated;
206
            }
207
 
208
            if (!empty($values[$range])) {
209
                $filterrange = (int) $values[$range];
210
            } else {
211
                $filterrange = 90 * DAYSECS;
212
            }
213
 
214
            // If we're outside the set range from the set base date, filter out tour.
215
            if ((time() > ($filterbasedate + $filterrange))) {
216
                $result = false;
217
            }
218
        }
219
        return $result;
220
    }
221
}