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 availability information.
19
 *
20
 * @package   core_courseformat
21
 * @copyright 2023 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_link_secondary;
28
use core\output\local\action_menu\subpanel as action_menu_subpanel;
29
use cm_info;
30
use core_courseformat\base as course_format;
31
use core_courseformat\output\local\courseformat_named_templatable;
32
use core\output\choicelist;
33
use core\output\local\dropdown\status;
34
use core\output\named_templatable;
35
use pix_icon;
36
use renderable;
37
use section_info;
38
use stdClass;
39
 
40
/**
41
 * Base class to render a course module availability inside a course format.
42
 *
43
 * @package   core_courseformat
44
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
45
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
 */
47
class visibility implements named_templatable, renderable {
48
    use courseformat_named_templatable;
49
 
50
    /** @var course_format the course format */
51
    protected $format;
52
 
53
    /** @var section_info the section object */
54
    protected $section;
55
 
56
    /** @var cm_info the course module instance */
57
    protected $mod;
58
 
59
    /**
60
     * Constructor.
61
     * @param course_format $format the course format
62
     * @param section_info $section the section info
63
     * @param cm_info $mod the course module ionfo
64
     */
65
    public function __construct(course_format $format, section_info $section, cm_info $mod) {
66
        $this->format = $format;
67
        $this->section = $section;
68
        $this->mod = $mod;
69
    }
70
 
71
    /**
72
     * Export this data so it can be used as the context for a mustache template.
73
     *
74
     * @param \renderer_base $output typically, the renderer that's calling this function
75
     * @return stdClass|null data context for a mustache template
76
     */
77
    public function export_for_template(\renderer_base $output): ?stdClass {
78
        if (!$this->show_visibility()) {
79
            return null;
80
        }
81
        $format = $this->format;
82
        // In rare legacy cases, the section could be stealth (orphaned) but they are not editable.
83
        if (!$format->show_editor()
84
            || !has_capability('moodle/course:activityvisibility', $this->mod->context)) {
85
            return $this->build_static_data($output);
86
        } else {
87
            return $this->build_editor_data($output);
88
        }
89
    }
90
 
91
    /**
92
     * Check if the visibility is displayed.
93
     * @return bool
94
     */
95
    protected function show_visibility(): bool {
96
        return !$this->mod->visible || $this->mod->is_stealth();
97
    }
98
 
99
    /**
100
     * Get the icon for the section visibility.
101
     * @param string $selected the visibility selected value
102
     * @return pix_icon
103
     */
104
    protected function get_icon(string $selected): pix_icon {
105
        if ($selected === 'hide') {
106
            return new pix_icon('t/show', '');
107
        } else if ($selected === 'stealth') {
108
            return new pix_icon('t/stealth', '');
109
        } else {
110
            return new pix_icon('t/hide', '');
111
        }
112
    }
113
 
114
    /**
115
     * Build the data for the editor.
116
     * @param \renderer_base $output typically, the renderer that's calling this function
117
     * @return stdClass|null data context for a mustache template
118
     */
119
    public function build_editor_data(\renderer_base $output): ?stdClass {
120
        $choice = $this->get_choice_list();
121
        return $this->get_dropdown_data($output, $choice);
122
    }
123
 
124
    /**
125
     * Build the data for the interactive dropdown.
126
     * @param \renderer_base $output
127
     * @param choicelist $choice the choice list
128
     * @return stdClass
129
     */
130
    protected function get_dropdown_data(
131
        \renderer_base $output,
132
        choicelist $choice,
133
    ): stdClass {
134
        $badgetext = $output->sr_text(get_string('availability'));
135
 
136
        if (!$this->mod->visible) {
137
            $badgetext .= get_string('hiddenfromstudents');
138
            $icon = $this->get_icon('hide');
139
        } else if ($this->mod->is_stealth()) {
140
            $badgetext .= get_string('hiddenoncoursepage');
141
            $icon = $this->get_icon('stealth');
142
        } else {
143
            $badgetext .= get_string("availability_show", 'core_courseformat');
144
            $icon = $this->get_icon('show');
145
        }
146
        $dropdown = new status(
147
            $output->render($icon) . ' ' . $badgetext,
148
            $choice,
149
            ['dialogwidth' => status::WIDTH['big']],
150
        );
151
        return (object) [
152
            'isInteractive' => true,
153
            'dropwdown' => $dropdown->export_for_template($output),
154
        ];
155
    }
156
 
157
    /**
158
     * Get the availability choice list.
159
     * @return choicelist
160
     */
161
    public function get_choice_list(): choicelist {
162
        $choice = $this->create_choice_list();
163
        $choice->set_selected_value($this->get_selected_choice_value());
164
        return $choice;
165
    }
166
 
167
    /**
168
     * Return the cm availability menu item.
169
     *
170
     * By default, the cm availability is displayed as a menu item subpanel.
171
     * However, it can be simplified when there is only one option and
172
     * it is not stealth (stealth require a subpanel to inform the user).
173
     *
174
     * @return action_menu_link_secondary|action_menu_subpanel|null
175
     */
176
    public function get_menu_item(): action_menu_link_secondary|action_menu_subpanel|null {
177
        $choice = $this->get_choice_list();
178
        $selectableoptions = $choice->get_selectable_options();
179
 
180
        if (count($selectableoptions) === 0) {
181
            return null;
182
        }
183
 
184
        // Visible activities in hidden sections are always considered stealth.
185
        if ($this->section->visible && count($selectableoptions) === 1) {
186
            $option = reset($selectableoptions);
187
            $actionlabel = $option->value === 'show' ? 'modshow' : 'modhide';
188
            return new action_menu_link_secondary(
189
                $option->url,
190
                $option->icon,
191
                get_string($actionlabel, 'moodle'),
192
                $choice->get_option_extras($option->value)
193
            );
194
        }
195
 
196
        return new action_menu_subpanel(
197
            get_string('availability', 'moodle'),
198
            $choice,
199
            ['class' => 'editing_availability'],
200
            new pix_icon('t/hide', '', 'moodle', ['class' => 'iconsmall'])
201
        );
202
    }
203
 
204
    /**
205
     * Get the selected choice value depending on the course, section and stealth settings.
206
     * @return string
207
     */
208
    protected function get_selected_choice_value(): string {
209
        if (!$this->mod->visible) {
210
            return 'hide';
211
        }
212
        if (!$this->mod->is_stealth()) {
213
            return 'show';
214
        }
215
        if (!$this->section->visible) {
216
            // All visible activities in a hidden sections are considered stealth
217
            // but they don't use the stealth attribute for it. It is just implicit.
218
            return 'show';
219
        }
220
        return 'stealth';
221
    }
222
 
223
    /**
224
     * Create a choice list for the dropdown.
225
     * @return choicelist the choice list
226
     */
227
    protected function create_choice_list(): choicelist {
228
        global $CFG;
229
 
230
        $choice = new choicelist();
231
        if ($this->section->visible || $this->mod->has_view()) {
232
            $label = $this->section->visible ? 'show' : 'stealth';
233
            $choice->add_option(
234
                'show',
235
                get_string("availability_{$label}", 'core_courseformat'),
236
                $this->get_option_data($label, 'cmShow')
237
            );
238
        }
239
        $choice->add_option(
240
            'hide',
241
            get_string('availability_hide', 'core_courseformat'),
242
            $this->get_option_data('hide', 'cmHide')
243
        );
244
 
245
        if ($CFG->allowstealth && $this->format->allow_stealth_module_visibility($this->mod, $this->section)) {
246
            $choice->add_option(
247
                'stealth',
248
                get_string('availability_stealth', 'core_courseformat'),
249
                $this->get_option_data('stealth', 'cmStealth')
250
            );
251
        }
252
        return $choice;
253
    }
254
 
255
    /**
256
     * Get the data for the option.
257
     * @param string $name the name of the option
258
     * @param string $action the state action of the option
259
     * @return array
260
     */
261
    private function get_option_data(string $name, string $action): array {
262
        return [
263
            'description' => get_string("availability_{$name}_help", 'core_courseformat'),
264
            'icon' => $this->get_icon($name),
265
            // Non-ajax behat is not smart enough to discrimante hidden links
266
            // so we need to keep providing the non-ajax links.
267
            'url' => $this->format->get_non_ajax_cm_action_url($action, $this->mod),
268
            'extras' => [
269
                'data-id' => $this->mod->id,
270
                'data-action' => $action,
271
            ]
272
        ];
273
    }
274
 
275
    /**
276
     * Build the static badges data.
277
     * @param \renderer_base $output typically, the renderer that's calling this function
278
     * @return stdClass|null data context for a mustache template
279
     */
280
    public function build_static_data(\renderer_base $output): ?stdClass {
281
        $data = (object) [
282
            'isInteractive' => false,
283
        ];
284
 
285
        if (!$this->mod->visible) {
286
            $data->modhiddenfromstudents = true;
287
        } else if ($this->mod->is_stealth()) {
288
            $data->modstealth = true;
289
        }
290
        return $data;
291
    }
292
}