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
namespace core_courseformat\output\local\content\section;
17
 
18
use context_course;
19
use core\output\choicelist;
20
use core\output\local\dropdown\status;
21
use core\output\named_templatable;
22
use core_courseformat\base as course_format;
23
use core_courseformat\output\local\courseformat_named_templatable;
1441 ariadna 24
use core_courseformat\sectiondelegatemodule;
1 efrain 25
use pix_icon;
26
use renderable;
27
use section_info;
28
use stdClass;
29
 
30
/**
31
 * Base class to render a section visibility inside a course format.
32
 *
33
 * @package   core_courseformat
34
 * @copyright 2024 Laurent David <laurent.david@moodle.com>
35
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class visibility implements named_templatable, renderable {
38
    use courseformat_named_templatable;
39
 
40
    /** @var course_format the course format */
41
    protected $format;
42
 
43
    /** @var section_info the section object */
44
    protected $section;
45
 
46
    /**
47
     * Constructor.
48
     * @param course_format $format the course format
49
     * @param section_info $section the section info
50
     */
51
    public function __construct(course_format $format, section_info $section) {
52
        $this->format = $format;
53
        $this->section = $section;
54
    }
55
 
56
    /**
57
     * Export this data so it can be used as the context for a mustache template.
58
     *
59
     * @param \renderer_base $output typically, the renderer that's calling this function
60
     * @return stdClass|null data context for a mustache template
61
     */
62
    public function export_for_template(\renderer_base $output): ?stdClass {
63
        global $USER;
1441 ariadna 64
 
65
        if ($this->section->visible) {
66
            return null;
67
        }
68
 
1 efrain 69
        $context = context_course::instance($this->section->course);
70
        $data = new stdClass();
1441 ariadna 71
 
72
        if (!has_capability('moodle/course:sectionvisibility', $context, $USER)) {
1 efrain 73
            $data->notavailable = true;
1441 ariadna 74
            return $data;
1 efrain 75
        }
1441 ariadna 76
 
77
        $data->editing = $this->format->show_editor();
78
 
79
        if ($this->section->is_orphan()) {
80
            $data->editing = false;
81
        }
82
 
83
        $data->notavailable = false;
84
        $data->hiddenfromstudents = true;
85
        if ($data->editing && $this->is_section_visibility_editable()) {
86
            $data->dropwdown = $this->get_visibility_dropdown($output);
87
        } else {
88
            // The user is editing but cannot edit the visibility on this specific section,
89
            $data->editing = false;
90
        }
1 efrain 91
        return $data;
92
    }
93
 
94
    /**
1441 ariadna 95
     * Check if the section visibility is editable.
96
     *
97
     * @return bool
98
     */
99
    protected function is_section_visibility_editable(): bool {
100
        // Delegated section inside a hidden sections are not editable.
101
        $parentsection = $this->section->get_component_instance()?->get_parent_section();
102
        if ($parentsection && !$parentsection->visible) {
103
            return false;
104
        }
105
        return true;
106
    }
107
 
108
    /**
109
     * Get the section visibility dropdown.
110
     * @param \renderer_base $output typically, the renderer that's calling this function
111
     * @return array
112
     */
113
    protected function get_visibility_dropdown(\renderer_base $output): array {
114
        $badgetext = $output->visually_hidden_text(get_string('availability'));
115
        $badgetext .= get_string('hiddenfromstudents');
116
        $icon = $this->get_icon('hide');
117
 
118
        $choice = new choicelist();
119
        $choice->add_option(
120
            'show',
121
            get_string('availability_show', 'core_courseformat'),
122
            $this->get_option_data('show', 'sectionShow', 'section_show')
123
        );
124
        $choice->add_option(
125
            'hide',
126
            get_string('availability_hide', 'core_courseformat'),
127
            $this->get_option_data('hide', 'sectionHide', 'section_hide')
128
        );
129
        $choice->set_selected_value('hide');
130
 
131
        $dropdown = new status(
132
            $output->render($icon) . ' ' . $badgetext,
133
            $choice,
134
            ['dialogwidth' => status::WIDTH['big']],
135
        );
136
        return $dropdown->export_for_template($output);
137
    }
138
 
139
    /**
1 efrain 140
     * Get the data for the option.
141
     *
142
     * @param string $name the name of the option
1441 ariadna 143
     * @param string $mutation the mutation name
144
     * @param string $stateaction the state action name
1 efrain 145
     * @return array
146
     */
1441 ariadna 147
    private function get_option_data(string $name, string $mutation, string $stateaction): array {
148
        $format = $this->format;
149
        $nonajaxurl = $format->get_update_url(
150
            action: $stateaction,
151
            ids: [$this->section->id],
152
            returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
153
        );
1 efrain 154
 
155
        return [
156
            'description' => get_string("availability_{$name}_help", 'core_courseformat'),
157
            'icon' => $this->get_icon($name),
1441 ariadna 158
            'url' => $nonajaxurl,
1 efrain 159
            'extras' => [
160
                'data-id' => $this->section->id,
1441 ariadna 161
                'data-action' => $mutation,
1 efrain 162
            ],
163
        ];
164
    }
165
 
166
    /**
167
     * Get the icon for the section visibility.
168
     * @param string $selected the visibility selected value
169
     * @return pix_icon
170
     */
171
    protected function get_icon(string $selected): pix_icon {
172
        if ($selected === 'hide') {
173
            return new pix_icon('t/show', '');
174
        } else {
175
            return new pix_icon('t/hide', '');
176
        }
177
    }
178
}