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
/**
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
        }
1441 ariadna 81
        if (!$this->format->show_activity_editor_options($this->mod)) {
82
            return null;
83
        }
1 efrain 84
        $format = $this->format;
85
        // In rare legacy cases, the section could be stealth (orphaned) but they are not editable.
86
        if (!$format->show_editor()
87
            || !has_capability('moodle/course:activityvisibility', $this->mod->context)) {
88
            return $this->build_static_data($output);
89
        } else {
90
            return $this->build_editor_data($output);
91
        }
92
    }
93
 
94
    /**
95
     * Check if the visibility is displayed.
96
     * @return bool
97
     */
98
    protected function show_visibility(): bool {
99
        return !$this->mod->visible || $this->mod->is_stealth();
100
    }
101
 
102
    /**
103
     * Get the icon for the section visibility.
104
     * @param string $selected the visibility selected value
105
     * @return pix_icon
106
     */
107
    protected function get_icon(string $selected): pix_icon {
108
        if ($selected === 'hide') {
109
            return new pix_icon('t/show', '');
110
        } else if ($selected === 'stealth') {
111
            return new pix_icon('t/stealth', '');
112
        } else {
113
            return new pix_icon('t/hide', '');
114
        }
115
    }
116
 
117
    /**
118
     * Build the data for the editor.
119
     * @param \renderer_base $output typically, the renderer that's calling this function
120
     * @return stdClass|null data context for a mustache template
121
     */
122
    public function build_editor_data(\renderer_base $output): ?stdClass {
123
        $choice = $this->get_choice_list();
124
        return $this->get_dropdown_data($output, $choice);
125
    }
126
 
127
    /**
128
     * Build the data for the interactive dropdown.
129
     * @param \renderer_base $output
130
     * @param choicelist $choice the choice list
131
     * @return stdClass
132
     */
133
    protected function get_dropdown_data(
134
        \renderer_base $output,
135
        choicelist $choice,
136
    ): stdClass {
1441 ariadna 137
        $badgetext = $output->visually_hidden_text(get_string('availability'));
1 efrain 138
 
139
        if (!$this->mod->visible) {
140
            $badgetext .= get_string('hiddenfromstudents');
141
            $icon = $this->get_icon('hide');
142
        } else if ($this->mod->is_stealth()) {
143
            $badgetext .= get_string('hiddenoncoursepage');
144
            $icon = $this->get_icon('stealth');
145
        } else {
146
            $badgetext .= get_string("availability_show", 'core_courseformat');
147
            $icon = $this->get_icon('show');
148
        }
149
        $dropdown = new status(
150
            $output->render($icon) . ' ' . $badgetext,
151
            $choice,
152
            ['dialogwidth' => status::WIDTH['big']],
153
        );
154
        return (object) [
155
            'isInteractive' => true,
156
            'dropwdown' => $dropdown->export_for_template($output),
157
        ];
158
    }
159
 
160
    /**
161
     * Get the availability choice list.
162
     * @return choicelist
163
     */
164
    public function get_choice_list(): choicelist {
165
        $choice = $this->create_choice_list();
166
        $choice->set_selected_value($this->get_selected_choice_value());
167
        return $choice;
168
    }
169
 
170
    /**
171
     * Return the cm availability menu item.
172
     *
173
     * By default, the cm availability is displayed as a menu item subpanel.
174
     * However, it can be simplified when there is only one option and
175
     * it is not stealth (stealth require a subpanel to inform the user).
176
     *
177
     * @return action_menu_link_secondary|action_menu_subpanel|null
178
     */
179
    public function get_menu_item(): action_menu_link_secondary|action_menu_subpanel|null {
180
        $choice = $this->get_choice_list();
181
        $selectableoptions = $choice->get_selectable_options();
182
 
183
        if (count($selectableoptions) === 0) {
184
            return null;
185
        }
186
 
187
        // Visible activities in hidden sections are always considered stealth.
188
        if ($this->section->visible && count($selectableoptions) === 1) {
189
            $option = reset($selectableoptions);
190
            $actionlabel = $option->value === 'show' ? 'modshow' : 'modhide';
191
            return new action_menu_link_secondary(
192
                $option->url,
193
                $option->icon,
194
                get_string($actionlabel, 'moodle'),
195
                $choice->get_option_extras($option->value)
196
            );
197
        }
198
 
199
        return new action_menu_subpanel(
200
            get_string('availability', 'moodle'),
201
            $choice,
202
            ['class' => 'editing_availability'],
203
            new pix_icon('t/hide', '', 'moodle', ['class' => 'iconsmall'])
204
        );
205
    }
206
 
207
    /**
208
     * Get the selected choice value depending on the course, section and stealth settings.
209
     * @return string
210
     */
211
    protected function get_selected_choice_value(): string {
212
        if (!$this->mod->visible) {
213
            return 'hide';
214
        }
215
        if (!$this->mod->is_stealth()) {
216
            return 'show';
217
        }
218
        if (!$this->section->visible) {
219
            // All visible activities in a hidden sections are considered stealth
220
            // but they don't use the stealth attribute for it. It is just implicit.
221
            return 'show';
222
        }
223
        return 'stealth';
224
    }
225
 
226
    /**
227
     * Create a choice list for the dropdown.
228
     * @return choicelist the choice list
229
     */
230
    protected function create_choice_list(): choicelist {
231
        global $CFG;
232
 
233
        $choice = new choicelist();
234
        if ($this->section->visible || $this->mod->has_view()) {
235
            $label = $this->section->visible ? 'show' : 'stealth';
236
            $choice->add_option(
237
                'show',
238
                get_string("availability_{$label}", 'core_courseformat'),
1441 ariadna 239
                $this->get_option_data($label, 'cmShow', 'cm_show')
1 efrain 240
            );
241
        }
242
        $choice->add_option(
243
            'hide',
244
            get_string('availability_hide', 'core_courseformat'),
1441 ariadna 245
            $this->get_option_data('hide', 'cmHide', 'cm_hide')
1 efrain 246
        );
247
 
248
        if ($CFG->allowstealth && $this->format->allow_stealth_module_visibility($this->mod, $this->section)) {
249
            $choice->add_option(
250
                'stealth',
251
                get_string('availability_stealth', 'core_courseformat'),
1441 ariadna 252
                $this->get_option_data('stealth', 'cmStealth', 'cm_stealth')
1 efrain 253
            );
254
        }
255
        return $choice;
256
    }
257
 
258
    /**
259
     * Get the data for the option.
260
     * @param string $name the name of the option
1441 ariadna 261
     * @param string $mutation the mutation name
262
     * @param string $stateaction the state action name
1 efrain 263
     * @return array
264
     */
1441 ariadna 265
    private function get_option_data(string $name, string $mutation, string $stateaction): array {
266
        $format = $this->format;
267
        $nonajaxurl = $format->get_update_url(
268
            action: $stateaction,
269
            ids: [$this->mod->id],
270
            returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
271
        );
272
 
1 efrain 273
        return [
274
            'description' => get_string("availability_{$name}_help", 'core_courseformat'),
275
            'icon' => $this->get_icon($name),
1441 ariadna 276
            'url' => $nonajaxurl,
1 efrain 277
            'extras' => [
278
                'data-id' => $this->mod->id,
1441 ariadna 279
                'data-action' => $mutation,
1 efrain 280
            ]
281
        ];
282
    }
283
 
284
    /**
285
     * Build the static badges data.
286
     * @param \renderer_base $output typically, the renderer that's calling this function
287
     * @return stdClass|null data context for a mustache template
288
     */
289
    public function build_static_data(\renderer_base $output): ?stdClass {
290
        $data = (object) [
291
            'isInteractive' => false,
292
        ];
293
 
294
        if (!$this->mod->visible) {
295
            $data->modhiddenfromstudents = true;
296
        } else if ($this->mod->is_stealth()) {
297
            $data->modstealth = true;
298
        }
299
        return $data;
300
    }
301
}