Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 mod_subsection;
18
 
19
use context_course;
20
use section_info;
21
 
22
/**
23
 * Class to check permissions for subsection module.
24
 *
25
 * @package    mod_subsection
26
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class permission {
30
    /**
31
     * Whether given user can add a subsection in a section
32
     *
33
     * @param section_info $section the course section
34
     * @param int|null $userid User ID to check, or the current user if omitted
35
     * @return bool
36
     */
37
    public static function can_add_subsection(section_info $section, ?int $userid = null): bool {
38
        // Until MDL-82349 is resolved, we need to skip the site course.
39
        if ($section->modinfo->get_course()->format == 'site') {
40
            return false;
41
        }
42
        if (!array_key_exists('subsection', \core_plugin_manager::instance()->get_enabled_plugins('mod'))) {
43
            return false;
44
        }
45
        if (!has_capability('mod/subsection:addinstance', context_course::instance($section->course), $userid)) {
46
            return false;
47
        }
48
        if ($section->is_delegated()) {
49
            return false;
50
        }
51
        $format = course_get_format($section->course);
52
        if ($format->get_last_section_number() >= $format->get_max_sections()) {
53
            return false;
54
        }
55
        if (!$format->supports_components()) {
56
            return false;
57
        }
58
        return true;
59
    }
60
}