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 core_cohort\customfield;
18
 
19
use core_customfield\handler;
20
use core_customfield\field_controller;
21
 
22
/**
23
 * Cohort handler for custom fields.
24
 *
25
 * @package   core_cohort
26
 * @copyright 2023 Dmitrii Metelkin <dmitriim@catalyst-au.net>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class cohort_handler extends handler {
30
 
31
    /**
32
     * @var cohort_handler
33
     */
34
    static protected $singleton;
35
 
36
    /**
37
     * Returns a singleton.
38
     *
39
     * @param int $itemid
40
     * @return \core_customfield\handler
41
     */
42
    public static function create(int $itemid = 0): handler {
43
        if (static::$singleton === null) {
44
            self::$singleton = new static(0);
45
        }
46
        return self::$singleton;
47
    }
48
 
49
    /**
50
     * Run reset code after unit tests to reset the singleton usage.
51
     */
52
    public static function reset_caches(): void {
53
        if (!PHPUNIT_TEST) {
54
            throw new \coding_exception('This feature is only intended for use in unit tests');
55
        }
56
 
57
        static::$singleton = null;
58
    }
59
 
60
    /**
61
     * The current user can configure custom fields on this component.
62
     *
63
     * @return bool true if the current can configure custom fields, false otherwise
64
     */
65
    public function can_configure(): bool {
66
        return has_capability('moodle/cohort:configurecustomfields', $this->get_configuration_context());
67
    }
68
 
69
    /**
70
     * The current user can edit custom fields on the given cohort.
71
     *
72
     * @param field_controller $field
73
     * @param int $instanceid id of the cohort to test edit permission
74
     * @return bool true if the current can edit custom field, false otherwise
75
     */
76
    public function can_edit(field_controller $field, int $instanceid = 0): bool {
77
        return has_capability('moodle/cohort:manage', $this->get_instance_context($instanceid));
78
    }
79
 
80
    /**
81
     * The current user can view custom fields on the given cohort.
82
     *
83
     * @param field_controller $field
84
     * @param int $instanceid id of the cohort to test edit permission
85
     * @return bool true if the current can view custom field, false otherwise
86
     */
87
    public function can_view(field_controller $field, int $instanceid): bool {
88
        return has_any_capability(['moodle/cohort:manage', 'moodle/cohort:view'], $this->get_instance_context($instanceid));
89
    }
90
 
91
    /**
92
     * Context that should be used for new categories created by this handler.
93
     *
94
     * @return \context the context for configuration
95
     */
96
    public function get_configuration_context(): \context {
97
        return \context_system::instance();
98
    }
99
 
100
    /**
101
     * URL for configuration of the fields on this handler.
102
     *
103
     * @return \moodle_url The URL to configure custom fields for this component
104
     */
105
    public function get_configuration_url(): \moodle_url {
106
        return new \moodle_url('/cohort/customfield.php');
107
    }
108
 
109
    /**
110
     * Returns the context for the data associated with the given instanceid.
111
     *
112
     * @param int $instanceid id of the record to get the context for
113
     * @return \context the context for the given record
114
     */
115
    public function get_instance_context(int $instanceid = 0): \context {
116
        global $DB;
117
        if ($instanceid > 0) {
118
            $cohort = $DB->get_record('cohort', ['id' => $instanceid], '*', MUST_EXIST);
119
            return \context::instance_by_id($cohort->contextid, MUST_EXIST);
120
        } else {
121
            return \context_system::instance();
122
        }
123
    }
124
}