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
 * Course filter.
24
 *
25
 * @package    tool_usertours
26
 * @copyright  2017 The Open University
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class course extends base {
30
    /**
31
     * The name of the filter.
32
     *
33
     * @return  string
34
     */
35
    public static function get_filter_name() {
36
        return 'course';
37
    }
38
 
39
    /**
40
     * Overrides the base add form element with a course selector.
41
     *
42
     * @param \MoodleQuickForm $mform
43
     */
44
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
45
        $options = ['multiple' => true];
46
 
47
        $filtername = self::get_filter_name();
48
        $key = "filter_{$filtername}";
49
 
50
        $mform->addElement('course', $key, get_string($key, 'tool_usertours'), $options);
51
        $mform->setDefault($key, '0');
52
        $mform->addHelpButton($key, $key, 'tool_usertours');
53
    }
54
 
55
    /**
56
     * Check whether the filter matches the specified tour and/or context.
57
     *
58
     * @param   tour        $tour       The tour to check
59
     * @param   context     $context    The context to check
60
     * @return  boolean
61
     */
62
    public static function filter_matches(tour $tour, context $context) {
63
        global $COURSE;
64
        $values = $tour->get_filter_values(self::get_filter_name());
65
        if (empty($values) || empty($values[0])) {
66
            // There are no values configured, meaning all.
67
            return true;
68
        }
69
        if (empty($COURSE->id)) {
70
            return false;
71
        }
72
        return in_array($COURSE->id, $values);
73
    }
74
 
75
    /**
76
     * Overrides the base prepare the filter values for the form with an integer value.
77
     *
78
     * @param   tour            $tour       The tour to prepare values from
79
     * @param   stdClass        $data       The data value
80
     * @return  stdClass
81
     */
82
    public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
83
        $filtername = static::get_filter_name();
84
        $key = "filter_{$filtername}";
85
        $values = $tour->get_filter_values($filtername);
86
        if (empty($values)) {
87
            $values = 0;
88
        }
89
        $data->$key = $values;
90
        return $data;
91
    }
92
 
93
    /**
94
     * Overrides the base save the filter values from the form to the tour.
95
     *
96
     * @param   tour            $tour       The tour to save values to
97
     * @param   stdClass        $data       The data submitted in the form
98
     */
99
    public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
100
        $filtername = static::get_filter_name();
101
        $key = "filter_{$filtername}";
102
        $newvalue = $data->$key;
103
        if (empty($data->$key)) {
104
            $newvalue = [];
105
        }
106
        $tour->set_filter_values($filtername, $newvalue);
107
    }
108
}