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 */
|
|
|
45 |
private $section;
|
|
|
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'),
|
|
|
161 |
$this->get_option_data(null, 'cmNoGroups', $this->mod->id)
|
|
|
162 |
);
|
|
|
163 |
$choice->add_option(
|
|
|
164 |
SEPARATEGROUPS,
|
|
|
165 |
get_string('groupsseparate', 'group'),
|
|
|
166 |
$this->get_option_data('groupsseparate', 'cmSeparateGroups', $this->mod->id)
|
|
|
167 |
);
|
|
|
168 |
$choice->add_option(
|
|
|
169 |
VISIBLEGROUPS,
|
|
|
170 |
get_string('groupsvisible', 'group'),
|
|
|
171 |
$this->get_option_data('groupsvisible', 'cmVisibleGroups', $this->mod->id)
|
|
|
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
|
|
|
180 |
* @param string $action the state action of the option
|
|
|
181 |
* @param int $id the id of the module
|
|
|
182 |
* @return array
|
|
|
183 |
*/
|
|
|
184 |
private function get_option_data(?string $name, string $action, int $id): array {
|
|
|
185 |
return [
|
|
|
186 |
'description' => ($name) ? get_string("groupmode_{$name}_help", 'group') : null,
|
|
|
187 |
// The dropdown icons are decorative, so we don't need to provide alt text.
|
|
|
188 |
'icon' => $this->get_action_icon($action),
|
|
|
189 |
'extras' => [
|
|
|
190 |
'data-id' => $id,
|
|
|
191 |
'data-action' => $action,
|
|
|
192 |
]
|
|
|
193 |
];
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Get the group mode icon.
|
|
|
198 |
* @param string $groupmode the group mode
|
|
|
199 |
* @param string $groupalt the alt text
|
|
|
200 |
* @return pix_icon
|
|
|
201 |
*/
|
|
|
202 |
protected function get_action_icon(string $groupmode, string $groupalt = ''): pix_icon {
|
|
|
203 |
$icons = [
|
|
|
204 |
'cmNoGroups' => 'i/groupn',
|
|
|
205 |
'cmSeparateGroups' => 'i/groups',
|
|
|
206 |
'cmVisibleGroups' => 'i/groupv',
|
|
|
207 |
];
|
|
|
208 |
return new pix_icon($icons[$groupmode], $groupalt);
|
|
|
209 |
}
|
|
|
210 |
}
|