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
namespace tool_usertours\local\filter;
18
 
19
use tool_usertours\tour;
20
use context;
21
 
22
/**
23
 * Filter base.
24
 *
25
 * @package    tool_usertours
26
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
abstract class base {
30
    /**
31
     * Any Value.
32
     */
33
    const ANYVALUE = '__ANYVALUE__';
34
 
35
    /**
36
     * The name of the filter.
37
     *
38
     * @return  string
39
     */
40
    public static function get_filter_name() {
41
        throw new \coding_exception('get_filter_name() must be defined');
42
    }
43
 
44
    /**
45
     * Retrieve the list of available filter options.
46
     *
47
     * @return  array                   An array whose keys are the valid options
48
     */
49
    public static function get_filter_options() {
50
        return [];
51
    }
52
 
53
    /**
54
     * Check whether the filter matches the specified tour and/or context.
55
     *
56
     * @param   tour        $tour       The tour to check
57
     * @param   context     $context    The context to check
58
     * @return  boolean
59
     */
60
    public static function filter_matches(tour $tour, context $context) {
61
        return true;
62
    }
63
 
64
    /**
65
     * Add the form elements for the filter to the supplied form.
66
     *
67
     * @param   MoodleQuickForm $mform      The form to add filter settings to.
68
     */
69
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
70
        $options = [
71
            static::ANYVALUE   => get_string('all'),
72
        ];
73
        $options += static::get_filter_options();
74
 
75
        $filtername = static::get_filter_name();
76
        $key = "filter_{$filtername}";
77
 
78
        $mform->addElement('select', $key, get_string($key, 'tool_usertours'), $options, [
79
                'multiple' => true,
80
            ]);
81
        $mform->setDefault($key, static::ANYVALUE);
82
        $mform->addHelpButton($key, $key, 'tool_usertours');
83
    }
84
 
85
    /**
86
     * Prepare the filter values for the form.
87
     *
88
     * @param   tour            $tour       The tour to prepare values from
89
     * @param   stdClass        $data       The data value
90
     * @return  stdClass
91
     */
92
    public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
93
        $filtername = static::get_filter_name();
94
 
95
        $key = "filter_{$filtername}";
96
        $values = $tour->get_filter_values($filtername);
97
        if (empty($values)) {
98
            $values = static::ANYVALUE;
99
        }
100
        $data->$key = $values;
101
 
102
        return $data;
103
    }
104
 
105
    /**
106
     * Save the filter values from the form to the tour.
107
     *
108
     * @param   tour            $tour       The tour to save values to
109
     * @param   stdClass        $data       The data submitted in the form
110
     */
111
    public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
112
        $filtername = static::get_filter_name();
113
 
114
        $key = "filter_{$filtername}";
115
 
116
        $newvalue = $data->$key;
117
        foreach ($data->$key as $value) {
118
            if ($value === static::ANYVALUE) {
119
                $newvalue = [];
120
                break;
121
            }
122
        }
123
 
124
        $tour->set_filter_values($filtername, $newvalue);
125
    }
126
}