Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 25... Línea 25...
25
 * @package    tool_usertours
25
 * @package    tool_usertours
26
 * @copyright  2017 The Open University
26
 * @copyright  2017 The Open University
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
28
 */
29
class course extends base {
29
class course extends base {
-
 
30
    /** @var string Option to select all courses. */
-
 
31
    public const OPERATOR_ALL = 'all';
-
 
32
    /** @var string Option to select specific courses. */
-
 
33
    public const OPERATOR_SELECT = 'select';
-
 
34
    /** @var string Option to select all courses except specific courses. */
-
 
35
    public const OPERATOR_EXCEPT = 'except';
-
 
36
    /** @var string The filter operator key constant. */
-
 
37
    public const OPERATOR_KEY = 'course_operator';
-
 
38
    /** @var string The filter key constant. */
-
 
39
    public const FILTER_KEY = 'filter_course';
-
 
40
 
30
    /**
41
    /**
31
     * The name of the filter.
42
     * The name of the filter.
32
     *
43
     *
33
     * @return  string
44
     * @return  string
34
     */
45
     */
35
    public static function get_filter_name() {
46
    public static function get_filter_name(): string {
36
        return 'course';
47
        return 'course';
37
    }
48
    }
Línea 38... Línea 49...
38
 
49
 
39
    /**
50
    /**
40
     * Overrides the base add form element with a course selector.
51
     * Overrides the base add form element with a course selector.
41
     *
52
     *
42
     * @param \MoodleQuickForm $mform
53
     * @param \MoodleQuickForm $mform
43
     */
54
     */
44
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
55
    public static function add_filter_to_form(\MoodleQuickForm &$mform) {
45
        $options = ['multiple' => true];
-
 
46
 
56
        // Add the operator selector.
-
 
57
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
47
        $filtername = self::get_filter_name();
58
        $mform->addElement('select', $operatorkey, get_string($operatorkey, 'tool_usertours'), static::get_operator_options());
-
 
59
        $mform->setDefault($operatorkey, static::OPERATOR_ALL);
Línea -... Línea 60...
-
 
60
        $mform->addHelpButton($operatorkey, $operatorkey, 'tool_usertours');
-
 
61
 
-
 
62
        // Add the course selector.
48
        $key = "filter_{$filtername}";
63
        $key = self::FILTER_KEY;
49
 
64
        $options = ['multiple' => true];
50
        $mform->addElement('course', $key, get_string($key, 'tool_usertours'), $options);
65
        $mform->addElement("course", $key, get_string($key, 'tool_usertours'), $options);
-
 
66
        $mform->setDefault($key, '0');
-
 
67
        $mform->addHelpButton($key, $key, 'tool_usertours');
-
 
68
        $mform->hideIf($key, $operatorkey, 'eq', self::OPERATOR_ALL);
-
 
69
    }
-
 
70
 
-
 
71
    /**
-
 
72
     * Validate form data specific to the course filter.
-
 
73
     *
-
 
74
     * @param array $data The current form data.
-
 
75
     * @param array $files The current form files.
-
 
76
     * @return array Any validation errors for this filter.
-
 
77
     */
-
 
78
    public static function validate_form(array $data, array $files): array {
-
 
79
        $errors = [];
-
 
80
        $key = static::FILTER_KEY;
-
 
81
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
-
 
82
        if ($data[$operatorkey] !== static::OPERATOR_ALL && empty($data[$key])) {
-
 
83
            $errors[$key] = get_string('filter_course_error_course_selection', 'tool_usertours');
-
 
84
        }
51
        $mform->setDefault($key, '0');
85
 
Línea 52... Línea 86...
52
        $mform->addHelpButton($key, $key, 'tool_usertours');
86
        return $errors;
53
    }
87
    }
54
 
88
 
55
    /**
89
    /**
56
     * Check whether the filter matches the specified tour and/or context.
90
     * Check whether the filter matches the specified tour and/or context.
57
     *
91
     *
58
     * @param   tour        $tour       The tour to check
92
     * @param   tour        $tour       The tour to check
59
     * @param   context     $context    The context to check
93
     * @param   context     $context    The context to check
60
     * @return  boolean
94
     * @return  boolean
61
     */
95
     */
-
 
96
    public static function filter_matches(tour $tour, context $context): bool {
-
 
97
        global $COURSE;
62
    public static function filter_matches(tour $tour, context $context) {
98
        $values = $tour->get_filter_values(static::get_filter_name());
63
        global $COURSE;
-
 
64
        $values = $tour->get_filter_values(self::get_filter_name());
99
        $operator = $tour->get_filter_values(static::OPERATOR_KEY)[0] ?? static::OPERATOR_ALL;
65
        if (empty($values) || empty($values[0])) {
100
 
-
 
101
        if (empty($values) || empty($values[0])) {
66
            // There are no values configured, meaning all.
102
            return true;
67
            return true;
103
        }
68
        }
104
 
-
 
105
        if (empty($COURSE->id)) {
-
 
106
            return false;
69
        if (empty($COURSE->id)) {
107
        }
-
 
108
 
-
 
109
        return match ($operator) {
-
 
110
            static::OPERATOR_SELECT => in_array($COURSE->id, $values),
70
            return false;
111
            static::OPERATOR_EXCEPT => !in_array($COURSE->id, $values),
Línea 71... Línea 112...
71
        }
112
            default => true,
72
        return in_array($COURSE->id, $values);
113
        };
73
    }
114
    }
74
 
115
 
75
    /**
116
    /**
76
     * Overrides the base prepare the filter values for the form with an integer value.
117
     * Overrides the base prepare the filter values for the form with an integer value.
77
     *
118
     *
78
     * @param   tour            $tour       The tour to prepare values from
119
     * @param   tour            $tour       The tour to prepare values from
-
 
120
     * @param   stdClass        $data       The data value
-
 
121
     * @return  stdClass
-
 
122
     */
-
 
123
    public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
-
 
124
        // Prepare the operator value.
-
 
125
        $operatorfiltername = static::OPERATOR_KEY;
-
 
126
        $operatorkey = 'filter_' . $operatorfiltername;
79
     * @param   stdClass        $data       The data value
127
        $operator = $tour->get_filter_values($operatorfiltername)[0] ?? static::OPERATOR_ALL;
80
     * @return  stdClass
128
        $data->$operatorkey = $operator;
81
     */
129
 
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);
130
        // Prepare the course value.
-
 
131
        $filtername = static::get_filter_name();
86
        if (empty($values)) {
132
        $key = 'filter_' . $filtername;
87
            $values = 0;
133
        $values = $tour->get_filter_values($filtername) ?: 0;
Línea 88... Línea 134...
88
        }
134
        $data->$key = $data->$operatorkey === static::OPERATOR_ALL ? 0 : $values;
89
        $data->$key = $values;
135
 
90
        return $data;
136
        return $data;
91
    }
137
    }
92
 
138
 
93
    /**
139
    /**
94
     * Overrides the base save the filter values from the form to the tour.
140
     * Overrides the base save the filter values from the form to the tour.
-
 
141
     *
-
 
142
     * @param   tour            $tour       The tour to save values to
-
 
143
     * @param   stdClass        $data       The data submitted in the form
-
 
144
     */
-
 
145
    public static function save_filter_values_from_form(
-
 
146
        tour $tour,
95
     *
147
        \stdClass $data,
96
     * @param   tour            $tour       The tour to save values to
-
 
97
     * @param   stdClass        $data       The data submitted in the form
-
 
98
     */
148
    ) {
99
    public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
149
        $operatorfiltername = static::OPERATOR_KEY;
-
 
150
        $operatorkey = 'filter_' . $operatorfiltername;
-
 
151
        $tour->set_filter_values($operatorfiltername, [$data->$operatorkey]);
-
 
152
        $filtername = static::get_filter_name();
-
 
153
        if ($data->$operatorkey === static::OPERATOR_ALL) {
-
 
154
            $newvalue = [];
-
 
155
        } else {
100
        $filtername = static::get_filter_name();
156
            $key = 'filter_' . $filtername;
101
        $key = "filter_{$filtername}";
157
            $newvalue = $data->$key;
102
        $newvalue = $data->$key;
158
            if (empty($data->$key)) {
-
 
159
                $newvalue = [];
-
 
160
            }
-
 
161
        }
-
 
162
        $tour->set_filter_values($filtername, $newvalue);
-
 
163
    }
-
 
164
 
-
 
165
    /**
-
 
166
     * Retrieve the available operator options.
-
 
167
     *
-
 
168
     * @return string[] The available operator options.
-
 
169
     */
-
 
170
    public static function get_operator_options(): array {
-
 
171
        $operatorkey = 'filter_' . self::OPERATOR_KEY;
-
 
172
        return [
103
        if (empty($data->$key)) {
173
            static::OPERATOR_ALL => get_string($operatorkey . '_' . static::OPERATOR_ALL, 'tool_usertours'),