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_brickfield\local\tool;
18
 
19
use tool_brickfield\accessibility;
20
use tool_brickfield\manager;
21
 
22
/**
23
 * Class filter.
24
 *
25
 * @package tool_brickfield
26
 * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class filter {
30
 
31
    /** @var int Possible course id being filtered. */
32
    public $courseid;
33
 
34
    /** @var int Possible category id being filtered. */
35
    public $categoryid;
36
 
37
    /** @var string The tab (page) being accessed. */
38
    public $tab;
39
 
40
    /** @var int The page number if multiple pages. */
41
    public $page;
42
 
43
    /** @var int Number of items per page for multiple pages. */
44
    public $perpage;
45
 
46
    /** @var array Array of filtered course ids if more than one. */
47
    public $courseids;
48
 
49
    /** @var string The url of the page being accessed. */
50
    public $url;
51
 
52
    /** @var string The output target. */
53
    public $target;
54
 
55
    /** @var string Any error message if present. */
56
    protected $errormessage;
57
 
58
    /**
59
     * filter constructor.
60
     * @param int $courseid
61
     * @param int $categoryid
62
     * @param string $tab
63
     * @param int $page
64
     * @param int $perpage
65
     * @param string $url
66
     * @param string $target
67
     */
68
    public function __construct(int $courseid = 0, int $categoryid = 0, string $tab = '',
69
                                int $page = 0, int $perpage = 0, string $url = '', string $target = '') {
70
        $this->courseid = $courseid;
71
        $this->categoryid = $categoryid;
72
        $this->tab = $tab;
73
        $this->page = $page;
74
        $this->perpage = $perpage;
75
        $this->url = $url;
76
        $this->target = $target;
77
    }
78
 
79
    /**
80
     * Get any course and category sql fragment and parameters and return as an array for this filter. Return false if course
81
     * filters are invalid.
82
     * @param string $alias Optional field alias to prefix on the where condition.
83
     * @param bool $onlycondition Set to true if this is the only condition to be used in the SQL statement.
84
     * @return array|bool
85
     * @throws \coding_exception
86
     * @throws \dml_exception
87
     */
88
    public function get_course_sql(string $alias = '', bool $onlycondition = false) {
89
        global $DB;
90
 
91
        $params = [];
92
        if ($alias != '') {
93
            $alias .= '.';
94
        }
95
        if (!$onlycondition) {
96
            $sql = ' AND (';
97
        } else {
98
            $sql = '(';
99
        }
100
        if ($this->courseid != 0) {
101
            $sql .= $alias . 'courseid = ?)';
102
            $params[] = $this->courseid;
103
        } else if (($this->categoryid != 0) || !empty($this->courseids)) {
104
            if ($this->validate_filters()) {
105
                list($coursesql, $params) = $DB->get_in_or_equal($this->courseids);
106
                $sql .= $alias . 'courseid '.$coursesql . ')';
107
            } else {
108
                $sql = '';
109
            }
110
        } else {
111
            $sql = '';
112
        }
113
        return [$sql, $params];
114
    }
115
 
116
    /**
117
     * Validate the filters. Set an errormessage if invalid. No filters is also valid - in that case using entire system.
118
     * @return bool
119
     * @throws \coding_exception
120
     * @throws \dml_exception
121
     */
122
    public function validate_filters(): bool {
123
        if (!empty($this->courseid)) {
124
            return true;
125
        } else if (!empty($this->categoryid) && empty($this->courseids)) {
126
            $this->courseids = accessibility::get_category_courseids($this->categoryid);
127
            if ($this->courseids === null) {
128
                $this->errormessage = get_string('invalidcategoryid', manager::PLUGINNAME);
129
                return false;
130
            } else if (count($this->courseids) == 0) {
131
                $this->errormessage = get_string('emptycategory', manager::PLUGINNAME, $this->categoryid);
132
                return false;
133
            }
134
        }
135
        return true;
136
    }
137
 
138
    /**
139
     * Return true if filter has course filter data, and the data is valid. Note that the site uses courseid 1.
140
     * @return bool
141
     * @throws \coding_exception
142
     * @throws \dml_exception
143
     */
144
    public function has_course_filters(): bool {
145
        if ((!empty($this->courseid) && ($this->courseid > 1)) || !empty($this->categoryid) || !empty($this->courseids)) {
146
            return $this->validate_filters();
147
        }
148
        return false;
149
    }
150
 
151
    /**
152
     * Check whether the user has appropriate permissions on the supplied context. Determine the capability to check by the filters
153
     * that are set.
154
     * @param \context|null $context The context being viewed (e.g. system, course category, course).
155
     * @param string $capability An optional capability to check.
156
     * @return bool
157
     * @throws \coding_exception
158
     * @throws \dml_exception
159
     */
160
    public function can_access(\context $context = null, string $capability = ''): bool {
161
        if ($capability == '') {
162
            if ($this->has_course_filters()) {
163
                $capability = accessibility::get_capability_name('viewcoursetools');
164
            } else {
165
                $capability = accessibility::get_capability_name('viewsystemtools');
166
            }
167
        }
168
        return $this->has_capability_in_context($capability, $context);
169
    }
170
 
171
    /**
172
     * Check the specified capability against the filter's context, or the specified context with the filter's information.
173
     * @param string $capability
174
     * @param null $context
175
     * @return bool
176
     * @throws \coding_exception
177
     * @throws \dml_exception
178
     */
179
    public function has_capability_in_context(string $capability, \context $context = null): bool {
180
        $coursefiltersvalid = $this->has_course_filters();
181
        if ($context === null) {
182
            // If the filter is using a list of courses ($this->>courseids), use the system context.
183
            if ($coursefiltersvalid && !empty($this->courseid)) {
184
                if (!empty($this->categoryid)) {
185
                    $context = \context_coursecat::instance($this->categoryid);
186
                } else {
187
                    $context = \context_course::instance($this->courseid);
188
                }
189
            } else {
190
                $context = \context_system::instance();
191
            }
192
        }
193
 
194
        return has_capability($capability, $context);
195
    }
196
 
197
    /**
198
     * Return the error message data.
199
     * @return mixed
200
     */
201
    public function get_errormessage() {
202
        return $this->errormessage;
203
    }
204
}