forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
239 lines (209 loc) · 10.2 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die;
use core\output\comboboxsearch;
use \core_grades\output\action_bar;
use core_message\helper;
use core_message\api;
/**
* Renderer class for the grade pages.
*
* @package core_grades
* @copyright 2021 Mihail Geshoski <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grades_renderer extends plugin_renderer_base {
/**
* Renders the action bar for a given page.
*
* @param action_bar $actionbar
* @return string The HTML output
*/
public function render_action_bar(action_bar $actionbar): string {
$data = $actionbar->export_for_template($this);
return $this->render_from_template($actionbar->get_template(), $data);
}
/**
* Renders the group selector trigger element.
*
* @param object $course The course object.
* @param string|null $groupactionbaseurl This parameter has been deprecated since 4.4 and should not be used anymore.
* @return string|null The raw HTML to render.
* @deprecated since 4.5. See replacement renderable \core_course\output\actionbar\group_selector instead.
* @todo Final deprecation in Moodle 6.0. See MDL-82116.
*/
#[\core\attribute\deprecated(
replacement: null,
since: '4.5',
reason: 'See replacement renderable \core_course\output\actionbar\group_selector.'
)]
public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
global $USER;
\core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
if ($groupactionbaseurl !== null) {
debugging(
'The $groupactionbaseurl argument has been deprecated. Please remove it from your method calls.',
DEBUG_DEVELOPER,
);
}
// Make sure that group mode is enabled.
if (!$groupmode = $course->groupmode) {
return null;
}
$sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
'courseid' => $course->id,
'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
'instance' => rand(),
]);
$label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') : get_string('selectgroupsseparate');
$buttondata = ['label' => $label];
$context = context_course::instance($course->id);
if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
$allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
} else {
$allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
}
$activegroup = groups_get_course_group($course, true, $allowedgroups);
$buttondata['group'] = $activegroup;
if ($activegroup) {
$group = groups_get_group($activegroup);
$buttondata['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
} else if ($activegroup === 0) {
$buttondata['selectedgroup'] = get_string('allparticipants');
}
$groupdropdown = new comboboxsearch(
false,
$this->render_from_template('core_group/comboboxsearch/group_selector', $buttondata),
$sbody,
'group-search',
'groupsearchwidget',
'groupsearchdropdown overflow-auto',
null,
true,
$label,
'group',
$activegroup
);
return $this->render($groupdropdown);
}
/**
* Build the data to render the initials bar filter within the gradebook.
* Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
* If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
*
* @param object $course The course object.
* @param context $context Our current context.
* @param string $slug The slug for the report that called this function.
* @return stdClass The data to output.
* @deprecated since 4.5. See replacement renderable \core_course\output\actionbar\initials_selector instead.
* @todo Final deprecation in Moodle 6.0. See MDL-82421.
*/
#[\core\attribute\deprecated(
replacement: null,
since: '4.5',
reason: 'See replacement renderable \core_course\output\actionbar\initials_selector.'
)]
public function initials_selector(
object $course,
context $context,
string $slug
): stdClass {
global $SESSION, $COURSE;
\core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
// User search.
$searchvalue = optional_param('gpr_search', null, PARAM_NOTAGS);
$userid = optional_param('grp_userid', null, PARAM_INT);
$url = new moodle_url($slug, ['id' => $course->id]);
$firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
$lastinitial = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
$renderer = $this->page->get_renderer('core_user');
$initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
$currentfilter = '';
if ($firstinitial !== '' && $lastinitial !== '') {
$currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
} else if ($firstinitial !== '') {
$currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
} else if ($lastinitial !== '') {
$currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
}
$this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug, $userid, $searchvalue]);
$formdata = (object) [
'courseid' => $COURSE->id,
'initialsbars' => $initialsbar,
];
$dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
return (object) [
'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
'dropdowncontent' => $dropdowncontent,
];
}
/**
* Creates and renders a custom user heading.
*
* @param stdClass $user The user object.
* @param int $courseid The course ID.
* @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
* @return string The raw HTML to render.
*/
public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true): string {
global $USER;
$headingdata = [
'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
'name' => fullname($user),
'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
];
if ($showbuttons) {
// Generate the data for the 'message' button.
$messagelinkattributes = array_map(function($name, $value) {
return ['name' => $name, 'value' => $value];
}, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
$messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
$headingdata['buttons'][] = [
'title' => get_string('message', 'message'),
'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
'icon' => ['name' => 't/message', 'component' => 'core'],
'linkattributes' => $messagelinkattributes
];
// Include js for messaging.
helper::messageuser_requirejs();
if ($USER->id != $user->id) {
// Generate the data for the 'contact' button.
$iscontact = api::is_contact($USER->id, $user->id);
$contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
$contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
$contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
$togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
$togglecontactlinkattributes = array_map(function($name, $value) {
if ($name === 'class') {
$value .= ' btn px-0';
}
return ['name' => $name, 'value' => $value];
}, array_keys($togglelinkparams), $togglelinkparams);
$headingdata['buttons'][] = [
'title' => get_string($contacttitle, 'message'),
'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
$contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
'icon' => ['name' => $contacticon, 'component' => 'core'],
'linkattributes' => $togglecontactlinkattributes
];
// Include js for contact toggle.
helper::togglecontact_requirejs();
}
}
return $this->render_from_template('core_grades/user_heading', $headingdata);
}
}