Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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_courseformat;
18
 
19
use action_menu;
20
use renderer_base;
21
use section_info;
22
use core_courseformat\stateupdates;
23
use core_courseformat\output\local\content\section\controlmenu;
24
use core_courseformat\base as course_format;
1441 ariadna 25
use stdClass;
1 efrain 26
 
27
/**
28
 * Section delegate base class.
29
 *
30
 * Plugins using delegate sections must extend this class into
31
 * their PLUGINNAME\courseformat\sectiondelegate class.
32
 *
33
 * @package    core_courseformat
34
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
abstract class sectiondelegate {
38
 
39
    /**
40
     * Constructor.
41
     * @param section_info $sectioninfo
42
     */
43
    public function __construct(
44
        protected section_info $sectioninfo
45
    ) {
46
    }
47
 
48
    /**
49
     * Get the section info instance if available.
50
     *
51
     * @param section_info $sectioninfo
52
     * @return section_info|null
53
     */
54
    public static function instance(section_info $sectioninfo): ?self {
55
        if (empty($sectioninfo->component)) {
56
            return null;
57
        }
58
        $classname = self::get_delegate_class_name($sectioninfo->component);
59
        if ($classname === null) {
60
            return null;
61
        }
1441 ariadna 62
        $instance = new $classname($sectioninfo);
63
        if (!$instance->is_enabled()) {
64
            return null;
65
        }
66
        return $instance;
1 efrain 67
    }
68
 
69
    /**
70
     * Return the delgate class name of a plugin, if any.
71
     * @param string $pluginname
72
     * @return string|null the delegate class name or null if not found.
73
     */
74
    protected static function get_delegate_class_name(string $pluginname): ?string {
75
        $classname = $pluginname . '\courseformat\sectiondelegate';
76
        if (!class_exists($classname)) {
77
            return null;
78
        }
79
        return $classname;
80
    }
81
 
82
    /**
83
     * Check if a plugin has a delegate class.
84
     * @param string $pluginname
85
     * @return bool
86
     */
87
    public static function has_delegate_class(string $pluginname): bool {
88
        return self::get_delegate_class_name($pluginname) !== null;
89
    }
90
 
91
    /**
1441 ariadna 92
     * Check if the delegate is enabled.
93
     *
94
     * Usually this happens when the delegate plugin is disabled.
95
     * @return bool
96
     */
97
    public function is_enabled(): bool {
98
        return true;
99
    }
100
 
101
    /**
1 efrain 102
     * Define the section final name.
103
     *
104
     * This method can process the section name and return the validated new name.
105
     *
106
     * @param section_info $section
107
     * @param string|null $newname the new name value to store in the database
108
     * @return string|null the name value to store in the database
109
     */
110
    public function preprocess_section_name(section_info $section, ?string $newname): ?string {
111
        return $newname;
112
    }
113
 
114
    /**
115
     * Add extra state updates when put or create a section.
116
     *
117
     * This method is called every time the backend sends a delegated section
118
     * state update to the UI.
119
     *
120
     * @param section_info $section the affected section.
121
     * @param stateupdates $updates the state updates object to notify the UI.
122
     */
123
    public function put_section_state_extra_updates(section_info $section, stateupdates $updates): void {
124
        // By default, do nothing.
125
    }
126
 
127
    /**
128
     * Allow delegate plugin to modify the available section menu.
129
     *
130
     * @param course_format $format The course format instance.
131
     * @param controlmenu $controlmenu The control menu instance.
132
     * @param renderer_base $output The renderer instance.
133
     * @return action_menu|null The new action menu with the list of edit control items or null if no action menu is available.
134
     */
135
    public function get_section_action_menu(
136
        course_format $format,
137
        controlmenu $controlmenu,
138
        renderer_base $output,
139
    ): ?action_menu {
140
        return $controlmenu->get_default_action_menu($output);
141
    }
1441 ariadna 142
 
143
    /**
144
     * Get the parent section of the current delegated section if any.
145
     *
146
     * @return section_info|null
147
     */
148
    public function get_parent_section(): ?section_info {
149
        return null;
150
    }
151
 
152
    /**
153
     * Handler executed when a section has been updated.
154
     *
155
     * This method uses a record instead of a section_info object because
156
     * section updates can be done in batch and the course_info may not be yet updated.
157
     *
158
     * This method does not need to recalculate the section_info object.
159
     *
160
     * @param stdClass $sectionrecord the new section data
161
     */
162
    public function section_updated(stdClass $sectionrecord): void {
163
        // By default, do nothing.
164
    }
1 efrain 165
}