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 core_contentbank\customfield;
18
 
19
use core_customfield\api;
20
use core_customfield\field_controller;
21
 
22
/**
23
 * Content handler for content bank custom fields
24
 *
25
 * @package   core_contentbank
26
 * @copyright 2024 Daniel Neis Araujo <daniel@adapta.online>
27
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class content_handler extends \core_customfield\handler {
30
 
31
    /**
32
     * @var content_handler
33
     */
34
    static protected $singleton;
35
 
36
    /**
37
     * @var \context
38
     */
39
    protected $parentcontext;
40
 
41
    /** @var int Field is displayed in the content bank edit page, visible to everybody */
42
    const VISIBLETOALL = 2;
43
    /** @var int Field is not displayed in the content bank edit page */
44
    const NOTVISIBLE = 0;
45
 
46
    /**
47
     * Returns a singleton
48
     *
49
     * @param int $itemid
50
     * @return \core_contentbank\customfield\content_handler
51
     */
52
    public static function create(int $itemid = 0): \core_contentbank\customfield\content_handler {
53
        if (static::$singleton === null) {
54
            self::$singleton = new static(0);
55
        }
56
        return self::$singleton;
57
    }
58
 
59
    /**
60
     * Run reset code after unit tests to reset the singleton usage.
61
     */
62
    public static function reset_caches(): void {
63
        if (!PHPUNIT_TEST) {
64
            throw new \coding_exception('This feature is only intended for use in unit tests');
65
        }
66
 
67
        static::$singleton = null;
68
    }
69
 
70
    /**
71
     * The current user can configure custom fields on this component.
72
     *
73
     * @return bool true if the current can configure custom fields, false otherwise
74
     */
75
    public function can_configure(): bool {
76
        return has_capability('moodle/contentbank:configurecustomfields', $this->get_configuration_context());
77
    }
78
 
79
    /**
80
     * The current user can edit custom fields on the content bank.
81
     *
82
     * @param field_controller $field
83
     * @param int $instanceid id of the course to test edit permission
84
     * @return bool true if the current can edit custom fields, false otherwise
85
     */
86
    public function can_edit(field_controller $field, int $instanceid = 0): bool {
87
        $context = $this->get_instance_context($instanceid);
88
        return (!$field->get_configdata_property('locked') ||
89
                has_capability('moodle/contentbank:changelockedcustomfields', $context));
90
    }
91
 
92
    /**
93
     * The current user can view custom fields on the given course.
94
     *
95
     * @param field_controller $field
96
     * @param int $instanceid id of the course to test edit permission
97
     * @return bool true if the current can edit custom fields, false otherwise
98
     */
99
    public function can_view(field_controller $field, int $instanceid): bool {
100
        $visibility = $field->get_configdata_property('visibility');
101
        if ($visibility == self::NOTVISIBLE) {
102
            return false;
103
        } else {
104
            return true;
105
        }
106
    }
107
 
108
    /**
109
     * Context that should be used for new categories created by this handler
110
     *
111
     * @return \context the context for configuration
112
     */
113
    public function get_configuration_context(): \context {
114
        return \context_system::instance();
115
    }
116
 
117
    /**
118
     * URL for configuration of the fields on this handler.
119
     *
120
     * @return \moodle_url The URL to configure custom fields for this component
121
     */
122
    public function get_configuration_url(): \moodle_url {
123
        return new \moodle_url('/contentbank/customfield.php');
124
    }
125
 
126
    /**
127
     * Returns the context for the data associated with the given instanceid.
128
     *
129
     * @param int $instanceid id of the record to get the context for
130
     * @return \context the context for the given record
131
     */
132
    public function get_instance_context(int $instanceid = 0): \context {
133
        global $DB;
134
        $contextid = $DB->get_field('contentbank_content', 'contextid', ['id' => $instanceid]);
135
        if (!$contextid || $contextid == SITEID) {
136
            return \context_system::instance();
137
        } else {
138
            return \context::instance_by_id($contextid);
139
        }
140
    }
141
 
142
    /**
143
     * Allows to add custom controls to the field configuration form that will be saved in configdata
144
     *
145
     * @param \MoodleQuickForm $mform
146
     */
147
    public function config_form_definition(\MoodleQuickForm $mform) {
148
        $mform->addElement('header', 'content_handler_header', get_string('customfieldsettings', 'core_course'));
149
        $mform->setExpanded('content_handler_header', true);
150
 
151
        // If field is locked.
152
        $mform->addElement('selectyesno', 'configdata[locked]', get_string('customfield_islocked', 'core_course'));
153
        $mform->addHelpButton('configdata[locked]', 'customfield_islocked', 'core_course');
154
 
155
        // Field data visibility.
156
        $visibilityoptions = [self::VISIBLETOALL => get_string('customfield_visibletoall', 'core_course'),
157
            self::NOTVISIBLE => get_string('customfield_notvisible', 'core_course')];
158
        $mform->addElement('select', 'configdata[visibility]', get_string('customfield_visibility', 'core_course'),
159
            $visibilityoptions);
160
        $mform->addHelpButton('configdata[visibility]', 'customfield_visibility', 'core_course');
161
    }
162
 
163
    /**
164
     * Creates or updates custom field data.
165
     *
166
     * @param \restore_task $task
167
     * @param array $data
168
     */
169
    public function restore_instance_data_from_backup(\restore_task $task, array $data) {
170
        $courseid = $task->get_courseid();
171
        $context = $this->get_instance_context($courseid);
172
        $editablefields = $this->get_editable_fields($courseid);
173
        $records = api::get_instance_fields_data($editablefields, $courseid);
174
        $target = $task->get_target();
175
        $override = ($target != \backup::TARGET_CURRENT_ADDING && $target != \backup::TARGET_EXISTING_ADDING);
176
 
177
        foreach ($records as $d) {
178
            $field = $d->get_field();
179
            if ($field->get('shortname') === $data['shortname'] && $field->get('type') === $data['type']) {
180
                if (!$d->get('id') || $override) {
181
                    $d->set($d->datafield(), $data['value']);
182
                    $d->set('value', $data['value']);
183
                    $d->set('valueformat', $data['valueformat']);
184
                    $d->set('contextid', $context->id);
185
                    $d->save();
186
                }
187
                return;
188
            }
189
        }
190
    }
191
}