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
namespace core_courseformat\output\local\content\cm;
18
 
19
use cm_info;
20
use core_courseformat\base as course_format;
21
use core_courseformat\output\local\courseformat_named_templatable;
22
use core\output\named_templatable;
23
use core\output\choicelist;
24
use core\output\local\dropdown\status;
25
use pix_icon;
26
use renderable;
27
use section_info;
28
use stdClass;
29
 
30
/**
31
 * Base class to render an activity group mode badge.
32
 *
33
 * @package   core_courseformat
34
 * @copyright 2023 Ferran Recio <ferran@moodle.com>
35
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class groupmode implements named_templatable, renderable {
38
 
39
    use courseformat_named_templatable;
40
 
41
    /** @var course_format the course format */
42
    protected $format;
43
 
44
    /** @var section_info the section object */
1441 ariadna 45
    protected $section;
1 efrain 46
 
47
    /** @var cm_info the course module instance */
48
    protected $mod;
49
 
50
    /**
51
     * Constructor.
52
     *
53
     * @param course_format $format the course format
54
     * @param section_info $section the section info
55
     * @param cm_info $mod the course module ionfo
56
     */
57
    public function __construct(
58
        course_format $format,
59
        section_info $section,
60
        cm_info $mod,
61
    ) {
62
        $this->format = $format;
63
        $this->section = $section;
64
        $this->mod = $mod;
65
    }
66
 
67
    /**
68
     * Export this data so it can be used as the context for a mustache template.
69
     *
70
     * @param \renderer_base $output typically, the renderer that's calling this function
71
     * @return stdClass|null data context for a mustache template
72
     */
73
    public function export_for_template(\renderer_base $output): ?stdClass {
74
        if (!$this->format->show_groupmode($this->mod)) {
75
            return null;
76
        }
77
        $usecomponents = $this->format->supports_components();
78
        if ($this->format->show_editor() && $usecomponents && !$this->mod->coursegroupmodeforce) {
79
            return $this->build_editor_data($output);
80
        }
81
        // If the group mode is not editable, the no groups badge is not displayed.
82
        if ($this->mod->effectivegroupmode === NOGROUPS) {
83
            return null;
84
        }
85
        return $this->build_static_data($output);
86
    }
87
 
88
    /**
89
     * Build the data for the static badge.
90
     * @param \renderer_base $output
91
     * @return stdClass
92
     */
93
    protected function build_static_data(\renderer_base $output): stdClass {
94
        switch ($this->mod->effectivegroupmode) {
95
            case SEPARATEGROUPS:
96
                $groupalt = get_string('groupsseparate', 'group');
97
                $groupicon = $this->get_action_icon('cmSeparateGroups', $groupalt);
98
                break;
99
            case VISIBLEGROUPS:
100
                $groupalt = get_string('groupsvisible', 'group');
101
                $groupicon = $this->get_action_icon('cmVisibleGroups', $groupalt);
102
                break;
103
            case NOGROUPS:
104
            default:
105
                $groupalt = get_string('groupsnone', 'group');
106
                $groupicon = $this->get_action_icon('cmNoGroups', $groupalt);
107
                break;
108
        }
109
        $data = (object) [
110
            'groupicon' => $output->render($groupicon),
111
            'groupalt' => $groupalt,
112
            'isInteractive' => false,
113
        ];
114
        return $data;
115
    }
116
 
117
    /**
118
     * Build the data for the interactive dropdown.
119
     * @param \renderer_base $output
120
     * @return stdClass
121
     */
122
    protected function build_editor_data(\renderer_base $output): stdClass {
123
        $choice = $this->get_choice_list();
124
        $result = $this->get_dropdown_data($output, $choice);
125
        $result->autohide = ($this->mod->effectivegroupmode === NOGROUPS);
126
        return $result;
127
    }
128
 
129
    /**
130
     * Build the data for the interactive dropdown.
131
     * @param \renderer_base $output
132
     * @param choicelist $choice the choice list
133
     * @return stdClass
134
     */
135
    protected function get_dropdown_data(\renderer_base $output, choicelist $choice): stdClass {
136
        $buttondata = $this->build_static_data($output);
137
        $dropdown = new status(
138
            $buttondata->groupicon,
139
            $choice,
140
            ['dialogwidth' => status::WIDTH['big']],
141
        );
142
        $dropdown->set_dialog_width(status::WIDTH['small']);
143
        $dropdown->set_position(status::POSITION['end']);
144
        return (object) [
145
            'isInteractive' => true,
146
            'groupicon' => $buttondata->groupicon,
147
            'groupalt' => $buttondata->groupalt,
148
            'dropwdown' => $dropdown->export_for_template($output),
149
        ];
150
    }
151
 
152
    /**
153
     * Create a choice list for the dropdown.
154
     * @return choicelist the choice list
155
     */
156
    public function get_choice_list(): choicelist {
157
        $choice = new choicelist();
158
        $choice->add_option(
159
            NOGROUPS,
160
            get_string('groupsnone', 'group'),
1441 ariadna 161
            $this->get_option_data(null, 'cmNoGroups', 'cm_nogroups')
1 efrain 162
        );
163
        $choice->add_option(
164
            SEPARATEGROUPS,
165
            get_string('groupsseparate', 'group'),
1441 ariadna 166
            $this->get_option_data('groupsseparate', 'cmSeparateGroups', 'cm_separategroups')
1 efrain 167
        );
168
        $choice->add_option(
169
            VISIBLEGROUPS,
170
            get_string('groupsvisible', 'group'),
1441 ariadna 171
            $this->get_option_data('groupsvisible', 'cmVisibleGroups', 'cm_visiblegroups')
1 efrain 172
        );
173
        $choice->set_selected_value($this->mod->effectivegroupmode);
174
        return $choice;
175
    }
176
 
177
    /**
178
     * Get the data for the option.
179
     * @param string|null $name the name of the option
1441 ariadna 180
     * @param string $mutation the mutation name
181
     * @param string $stateaction the state action name
1 efrain 182
     * @return array
183
     */
1441 ariadna 184
    private function get_option_data(?string $name, string $mutation, string $stateaction): array {
185
        $format = $this->format;
186
        $nonajaxurl = $format->get_update_url(
187
            action: $stateaction,
188
            ids: [$this->mod->id],
189
            returnurl: $format->get_view_url($format->get_sectionnum(), ['navigation' => true]),
190
        );
191
 
1 efrain 192
        return [
193
            'description' => ($name) ? get_string("groupmode_{$name}_help", 'group') : null,
194
            // The dropdown icons are decorative, so we don't need to provide alt text.
1441 ariadna 195
            'icon' => $this->get_action_icon($mutation),
196
            'url' => $nonajaxurl,
1 efrain 197
            'extras' => [
1441 ariadna 198
                'data-id' => $this->mod->id,
199
                'data-action' => $mutation,
1 efrain 200
            ]
201
        ];
202
    }
203
 
204
    /**
205
     * Get the group mode icon.
206
     * @param string $groupmode the group mode
207
     * @param string $groupalt the alt text
208
     * @return pix_icon
209
     */
210
    protected function get_action_icon(string $groupmode, string $groupalt = ''): pix_icon {
211
        $icons = [
212
            'cmNoGroups' => 'i/groupn',
213
            'cmSeparateGroups' => 'i/groups',
214
            'cmVisibleGroups' => 'i/groupv',
215
        ];
216
        return new pix_icon($icons[$groupmode], $groupalt);
217
    }
218
}