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_uploadcourse;
18
 
19
use context_course;
20
use context_coursecat;
21
use core_course_category;
22
use core_tag_tag;
23
use lang_string;
24
use tool_uploadcourse_course;
25
 
26
/**
27
 * Checks various permissions related to the course upload process.
28
 *
29
 * @package     tool_uploadcourse
30
 * @copyright   2019 Marina Glancy
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class permissions {
34
 
35
    /**
36
     * Check permission to use tool_uploadcourse in a given category.
37
     *
38
     * @param int $catid
39
     * @param lang_string|null $customerror
40
     * @return lang_string|null
41
     */
42
    protected static function check_permission_to_use_uploadcourse_tool(
43
        int $catid,
44
        ?lang_string $customerror = null
45
    ): ?lang_string {
46
        $category = core_course_category::get($catid, IGNORE_MISSING);
47
        if (!$category || !has_capability('tool/uploadcourse:use', $category->get_context())) {
48
            if ($customerror) {
49
                return $customerror;
50
            }
51
            return new lang_string('courseuploadnotallowed', 'tool_uploadcourse',
52
                $category ? $category->get_formatted_name() : $catid);
53
        }
54
        return null;
55
    }
56
 
57
    /**
58
     * Check capabilities to delete a course and to use tool_uploadcourse for it.
59
     *
60
     * @param string $shortname course shortname
61
     * @return lang_string|null
62
     */
63
    public static function check_permission_to_delete(string $shortname): ?lang_string {
64
        global $DB;
65
        $course = $DB->get_record('course', ['shortname' => $shortname]);
66
        if ($error = self::check_permission_to_use_uploadcourse_tool($course->category)) {
67
            return $error;
68
        }
69
 
70
        if (!has_capability('moodle/course:delete', context_course::instance($course->id))) {
71
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:delete'));
72
        }
73
        return null;
74
    }
75
 
76
    /**
77
     * Check capability in a course (that exists or is about to be created).
78
     *
79
     * @param int $do one of tool_uploadcourse_course::DO_UPDATE or tool_uploadcourse_course::DO_ADD
80
     * @param array $coursedata data to update/create course with, must contain either 'id' or 'category' respectively
81
     * @param string $capability capability to check
82
     * @return lang_string|null error string or null
83
     */
84
    protected static function check_capability(int $do, array $coursedata, string $capability): ?lang_string {
85
        if ($do == tool_uploadcourse_course::DO_UPDATE) {
86
            $context = context_course::instance($coursedata['id']);
87
            $hascap = has_capability($capability, $context);
88
        } else {
89
            $catcontext = context_coursecat::instance($coursedata['category']);
90
            $hascap = guess_if_creator_will_have_course_capability($capability, $catcontext);
91
        }
92
 
93
        if (!$hascap) {
94
            return new lang_string('nopermissions', 'error', get_capability_string($capability));
95
        }
96
 
97
        return null;
98
    }
99
 
100
    /**
101
     * Check permission to update the course.
102
     *
103
     * This checks capabilities:
104
     * - to use tool_uploadcourse in the category where course is in and in the category where it will be moved to (if applicable).
105
     * - to change course category (if applicable).
106
     * - to update course details.
107
     * - to force course language (if applicable).
108
     * - to change course idnumber, shortname, fullname, summary, visibility, tags (if applicable).
109
     *
110
     * @param array $coursedata data to update a course with, always contains 'id'
111
     * @return lang_string|null
112
     */
113
    public static function check_permission_to_update(array $coursedata): ?lang_string {
114
        $course = get_course($coursedata['id']);
115
 
116
        if ($error = self::check_permission_to_use_uploadcourse_tool($course->category,
117
                new lang_string('courseuploadupdatenotallowed', 'tool_uploadcourse'))) {
118
            return $error;
119
        }
120
 
121
        if (!has_capability('moodle/course:update', context_course::instance($course->id))) {
122
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:update'));
123
        }
124
 
125
        // If user requested to change course category check permissions to use tool in target category
126
        // and capabilities to change category.
127
        if (!empty($coursedata['category']) && $coursedata['category'] != $course->category) {
128
            if ($error = self::check_permission_to_use_uploadcourse_tool($coursedata['category'])) {
129
                return $error;
130
            }
131
 
132
            if (!has_capability('moodle/course:changecategory', context_coursecat::instance($course->category))) {
133
                return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changecategory'));
134
            }
135
 
136
            if (!has_capability('moodle/course:changecategory', context_coursecat::instance($coursedata['category']))) {
137
                return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changecategory'));
138
            }
139
        }
140
 
141
        $context = context_course::instance($coursedata['id']);
142
 
143
        // If lang is specified, check the user is allowed to set that field.
144
        if (!empty($coursedata['lang']) && $coursedata['lang'] !== $course->lang) {
145
            if (!has_capability('moodle/course:setforcedlanguage', $context)) {
146
                return new lang_string('cannotforcelang', 'tool_uploadcourse');
147
            }
148
        }
149
 
150
        // Check permission to change course idnumber.
151
        if (array_key_exists('idnumber', $coursedata) && $coursedata['idnumber'] !== $course->idnumber &&
152
                !has_capability('moodle/course:changeidnumber', $context)) {
153
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changeidnumber'));
154
        }
155
 
156
        // Check permission to change course shortname.
157
        if (array_key_exists('shortname', $coursedata) && $coursedata['shortname'] !== $course->shortname &&
158
                !has_capability('moodle/course:changeshortname', $context)) {
159
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changeshortname'));
160
        }
161
 
162
        // Check permission to change course fullname.
163
        if (array_key_exists('fullname', $coursedata) && $coursedata['fullname'] !== $course->fullname &&
164
                !has_capability('moodle/course:changefullname', $context)) {
165
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changefullname'));
166
        }
167
 
168
        // Check permission to change course summary.
169
        if (array_key_exists('summary', $coursedata) && $coursedata['summary'] !== $course->summary &&
170
                !has_capability('moodle/course:changesummary', $context)) {
171
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:changesummary'));
172
        }
173
 
174
        // Check permission to change course visibility.
175
        if (array_key_exists('visible', $coursedata) && $coursedata['visible'] !== $course->visible &&
176
                !has_capability('moodle/course:visibility', $context)) {
177
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:visibility'));
178
        }
179
 
180
        // If tags are specified and enabled check if user can updat them.
181
        if (core_tag_tag::is_enabled('core', 'course') &&
182
            (array_key_exists('tags', $coursedata) && strval($coursedata['tags']) !== '') &&
183
            ($error = self::check_capability(tool_uploadcourse_course::DO_UPDATE, $coursedata, 'moodle/course:tag'))) {
184
            return $error;
185
        }
186
 
187
        return null;
188
    }
189
 
190
    /**
191
     * Check permission to create course.
192
     *
193
     * This checks capabilities:
194
     * - to use tool_uploadcourse in the category where course will be created.
195
     * - to create a course.
196
     * - to force course language (if applicable).
197
     * - to set course tags (if applicable).
198
     *
199
     * @param array $coursedata data to create a course with, always contains 'category'
200
     * @return lang_string|null
201
     */
202
    public static function check_permission_to_create(array $coursedata): ?lang_string {
203
 
204
        if ($error = self::check_permission_to_use_uploadcourse_tool($coursedata['category'])) {
205
            return $error;
206
        }
207
 
208
        $catcontext = context_coursecat::instance($coursedata['category']);
209
 
210
        // Check user is allowed to create courses in this category.
211
        if (!has_capability('moodle/course:create', $catcontext)) {
212
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:create'));
213
        }
214
 
215
        // If lang is specified, check the user is allowed to set that field.
216
        if (!empty($coursedata['lang'])) {
217
            if (!guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $catcontext)) {
218
                return new lang_string('cannotforcelang', 'tool_uploadcourse');
219
            }
220
        }
221
 
222
        // Check permission to change course visibility.
223
        if (array_key_exists('visible', $coursedata) && !$coursedata['visible'] &&
224
                !guess_if_creator_will_have_course_capability('moodle/course:visibility', $catcontext)) {
225
            return new lang_string('nopermissions', 'error', get_capability_string('moodle/course:visibility'));
226
        }
227
 
228
        // If tags are specified and enabled check if user can updat them.
229
        if (core_tag_tag::is_enabled('core', 'course') &&
230
                (array_key_exists('tags', $coursedata) && strval($coursedata['tags']) !== '') &&
231
                ($error = self::check_capability(tool_uploadcourse_course::DO_CREATE, $coursedata, 'moodle/course:tag'))) {
232
            return $error;
233
        }
234
 
235
        return null;
236
    }
237
 
238
    /**
239
     * Check if the user is able to reset a course.
240
     *
241
     * Capability to use the tool and update the course is already checked earlier.
242
     *
243
     * @param array $coursedata data to update course with, always contains 'id'
244
     * @return lang_string|null error string or null
245
     */
246
    public static function check_permission_to_reset(array $coursedata): ?lang_string {
247
        return self::check_capability(tool_uploadcourse_course::DO_UPDATE, $coursedata, 'moodle/course:reset');
248
    }
249
 
250
    /**
251
     * Check if the user is able to restore the mbz into a course.
252
     *
253
     * This method does not need to check if the course can be updated/created, this is checked earlier.
254
     *
255
     * @param int $do one of tool_uploadcourse_course::DO_UPDATE or tool_uploadcourse_course::DO_ADD
256
     * @param array $coursedata data to update/create course with, must contain either 'id' or 'category' respectively
257
     * @return lang_string|null error string or null
258
     */
259
    public static function check_permission_to_restore(int $do, array $coursedata): ?lang_string {
260
        return self::check_capability($do, $coursedata, 'moodle/restore:restorecourse');
261
    }
262
}