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_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;
25
 
26
/**
27
 * Section delegate base class.
28
 *
29
 * Plugins using delegate sections must extend this class into
30
 * their PLUGINNAME\courseformat\sectiondelegate class.
31
 *
32
 * @package    core_courseformat
33
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
abstract class sectiondelegate {
37
 
38
    /**
39
     * Constructor.
40
     * @param section_info $sectioninfo
41
     */
42
    public function __construct(
43
        protected section_info $sectioninfo
44
    ) {
45
    }
46
 
47
    /**
48
     * Get the section info instance if available.
49
     *
50
     * @param section_info $sectioninfo
51
     * @return section_info|null
52
     */
53
    public static function instance(section_info $sectioninfo): ?self {
54
        if (empty($sectioninfo->component)) {
55
            return null;
56
        }
57
        $classname = self::get_delegate_class_name($sectioninfo->component);
58
        if ($classname === null) {
59
            return null;
60
        }
61
        return new $classname($sectioninfo);
62
    }
63
 
64
    /**
65
     * Return the delgate class name of a plugin, if any.
66
     * @param string $pluginname
67
     * @return string|null the delegate class name or null if not found.
68
     */
69
    protected static function get_delegate_class_name(string $pluginname): ?string {
70
        $classname = $pluginname . '\courseformat\sectiondelegate';
71
        if (!class_exists($classname)) {
72
            return null;
73
        }
74
        return $classname;
75
    }
76
 
77
    /**
78
     * Check if a plugin has a delegate class.
79
     * @param string $pluginname
80
     * @return bool
81
     */
82
    public static function has_delegate_class(string $pluginname): bool {
83
        return self::get_delegate_class_name($pluginname) !== null;
84
    }
85
 
86
    /**
87
     * Define the section final name.
88
     *
89
     * This method can process the section name and return the validated new name.
90
     *
91
     * @param section_info $section
92
     * @param string|null $newname the new name value to store in the database
93
     * @return string|null the name value to store in the database
94
     */
95
    public function preprocess_section_name(section_info $section, ?string $newname): ?string {
96
        return $newname;
97
    }
98
 
99
    /**
100
     * Add extra state updates when put or create a section.
101
     *
102
     * This method is called every time the backend sends a delegated section
103
     * state update to the UI.
104
     *
105
     * @param section_info $section the affected section.
106
     * @param stateupdates $updates the state updates object to notify the UI.
107
     */
108
    public function put_section_state_extra_updates(section_info $section, stateupdates $updates): void {
109
        // By default, do nothing.
110
    }
111
 
112
    /**
113
     * Allow delegate plugin to modify the available section menu.
114
     *
115
     * @param course_format $format The course format instance.
116
     * @param controlmenu $controlmenu The control menu instance.
117
     * @param renderer_base $output The renderer instance.
118
     * @return action_menu|null The new action menu with the list of edit control items or null if no action menu is available.
119
     */
120
    public function get_section_action_menu(
121
        course_format $format,
122
        controlmenu $controlmenu,
123
        renderer_base $output,
124
    ): ?action_menu {
125
        return $controlmenu->get_default_action_menu($output);
126
    }
127
}