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
/**
18
 * Contains the default activity control menu.
19
 *
20
 * @package   core_courseformat
21
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_courseformat\output\local\content\cm;
26
 
27
use action_menu;
28
use action_menu_link;
29
use cm_info;
30
use core\output\named_templatable;
31
use core_courseformat\base as course_format;
32
use core_courseformat\output\local\courseformat_named_templatable;
33
use renderable;
34
use section_info;
35
use stdClass;
36
 
37
/**
38
 * Base class to render a course module menu inside a course format.
39
 *
40
 * @package   core_courseformat
41
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
42
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class controlmenu implements named_templatable, renderable {
45
 
46
    use courseformat_named_templatable;
47
 
48
    /** @var course_format the course format */
49
    protected $format;
50
 
51
    /** @var section_info the section object */
52
    private $section;
53
 
54
    /** @var action_menu the activity aciton menu */
55
    protected $menu;
56
 
57
    /** @var cm_info the course module instance */
58
    protected $mod;
59
 
60
    /** @var array optional display options */
61
    protected $displayoptions;
62
 
63
    /**
64
     * Constructor.
65
     *
66
     * @param course_format $format the course format
67
     * @param section_info $section the section info
68
     * @param cm_info $mod the course module info
69
     * @param array $displayoptions optional extra display options
70
     */
71
    public function __construct(course_format $format, section_info $section, cm_info $mod, array $displayoptions = []) {
72
        $this->format = $format;
73
        $this->section = $section;
74
        $this->mod = $mod;
75
        $this->displayoptions = $displayoptions;
76
    }
77
 
78
    /**
79
     * Export this data so it can be used as the context for a mustache template.
80
     *
81
     * @param \renderer_base $output typically, the renderer that's calling this function
82
     * @return stdClass data context for a mustache template
83
     */
84
    public function export_for_template(\renderer_base $output): stdClass {
85
 
86
        $mod = $this->mod;
87
 
88
        $menu = $this->get_action_menu($output);
89
 
90
        if (empty($menu)) {
91
            return new stdClass();
92
        }
93
 
94
        $data = (object)[
95
            'menu' => $menu->export_for_template($output),
96
            'hasmenu' => true,
97
            'id' => $mod->id,
98
        ];
99
 
100
        // After icons.
101
        if (!empty($mod->afterediticons)) {
102
            $data->afterediticons = $mod->afterediticons;
103
        }
104
 
105
        return $data;
106
    }
107
 
108
    /**
109
     * Generate the action menu element.
110
     *
111
     * This method is public in case some block needs to modify the menu before output it.
112
     * @param \renderer_base $output typically, the renderer that's calling this function
113
     * @return action_menu|null the activity action menu
114
     */
115
    public function get_action_menu(\renderer_base $output): ?action_menu {
116
 
117
        if (!empty($this->menu)) {
118
            return $this->menu;
119
        }
120
 
121
        $mod = $this->mod;
122
 
123
        $controls = $this->cm_control_items();
124
 
125
        if (empty($controls)) {
126
            return null;
127
        }
128
 
129
        // Convert control array into an action_menu.
130
        $menu = new action_menu();
131
        $menu->set_kebab_trigger(get_string('edit'));
132
        $menu->attributes['class'] .= ' section-cm-edit-actions commands';
133
 
134
        // Prioritise the menu ahead of all other actions.
135
        $menu->prioritise = true;
136
 
137
        $ownerselector = $this->displayoptions['ownerselector'] ?? '#module-' . $mod->id;
138
        $menu->set_owner_selector($ownerselector);
139
 
140
        foreach ($controls as $control) {
141
            if ($control instanceof action_menu_link) {
142
                $control->add_class('cm-edit-action');
143
            }
144
            $menu->add($control);
145
        }
146
 
147
        $this->menu = $menu;
148
 
149
        return $menu;
150
    }
151
 
152
    /**
153
     * Generate the edit control items of a course module.
154
     *
155
     * This method uses course_get_cm_edit_actions function to get the cm actions.
156
     * However, format plugins can override the method to add or remove elements
157
     * from the menu.
158
     *
159
     * @return array of edit control items
160
     */
161
    protected function cm_control_items() {
162
        $format = $this->format;
163
        $mod = $this->mod;
164
        $sectionreturn = $format->get_sectionnum();
165
        if (!empty($this->displayoptions['disableindentation']) || !$format->uses_indentation()) {
166
            $indent = -1;
167
        } else {
168
            $indent = $mod->indent;
169
        }
170
        return course_get_cm_edit_actions($mod, $indent, $sectionreturn);
171
    }
172
}