weekly release 4.5dev
[moodle.git] / grade / renderer.php
blobd3f772d7f7bba14879f0be1142d7882c88cfcc64
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.
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/>.
17 defined('MOODLE_INTERNAL') || die;
19 use core\output\comboboxsearch;
20 use \core_grades\output\action_bar;
21 use core_message\helper;
22 use core_message\api;
24 /**
25 * Renderer class for the grade pages.
27 * @package core_grades
28 * @copyright 2021 Mihail Geshoski <mihail@moodle.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class core_grades_renderer extends plugin_renderer_base {
33 /**
34 * Renders the action bar for a given page.
36 * @param action_bar $actionbar
37 * @return string The HTML output
39 public function render_action_bar(action_bar $actionbar): string {
40 $data = $actionbar->export_for_template($this);
41 return $this->render_from_template($actionbar->get_template(), $data);
44 /**
45 * Renders the group selector trigger element.
47 * @param object $course The course object.
48 * @param string|null $groupactionbaseurl This parameter has been deprecated since 4.4 and should not be used anymore.
49 * @return string|null The raw HTML to render.
50 * @deprecated since 4.5. Use \core_course\output\actionbar\renderer' instead.
51 * @todo Final deprecation in Moodle 6.0. See MDL-82116.
53 #[\core\attribute\deprecated(
54 replacement: null,
55 since: '4.5',
56 reason: 'Moved to \core_course\output\actionbar\renderer.'
58 public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
59 global $USER;
61 \core\deprecation::emit_deprecation_if_present([$this, __FUNCTION__]);
63 if ($groupactionbaseurl !== null) {
64 debugging(
65 'The $groupactionbaseurl argument has been deprecated. Please remove it from your method calls.',
66 DEBUG_DEVELOPER,
69 // Make sure that group mode is enabled.
70 if (!$groupmode = $course->groupmode) {
71 return null;
74 $sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
75 'courseid' => $course->id,
76 'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
77 'instance' => rand(),
78 ]);
80 $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') : get_string('selectgroupsseparate');
82 $buttondata = ['label' => $label];
84 $context = context_course::instance($course->id);
86 if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
87 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
88 } else {
89 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
92 $activegroup = groups_get_course_group($course, true, $allowedgroups);
93 $buttondata['group'] = $activegroup;
95 if ($activegroup) {
96 $group = groups_get_group($activegroup);
97 $buttondata['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
98 } else if ($activegroup === 0) {
99 $buttondata['selectedgroup'] = get_string('allparticipants');
102 $groupdropdown = new comboboxsearch(
103 false,
104 $this->render_from_template('core_group/comboboxsearch/group_selector', $buttondata),
105 $sbody,
106 'group-search',
107 'groupsearchwidget',
108 'groupsearchdropdown overflow-auto',
109 null,
110 true,
111 $label,
112 'group',
113 $activegroup
115 return $this->render_from_template($groupdropdown->get_template(), $groupdropdown->export_for_template($this));
119 * Build the data to render the initials bar filter within the gradebook.
120 * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
121 * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
123 * @param object $course The course object.
124 * @param context $context Our current context.
125 * @param string $slug The slug for the report that called this function.
126 * @return stdClass The data to output.
128 public function initials_selector(
129 object $course,
130 context $context,
131 string $slug
132 ): stdClass {
133 global $SESSION, $COURSE;
134 // User search.
135 $searchvalue = optional_param('gpr_search', null, PARAM_NOTAGS);
136 $userid = optional_param('grp_userid', null, PARAM_INT);
137 $url = new moodle_url($slug, ['id' => $course->id]);
138 $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
139 $lastinitial = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
141 $renderer = $this->page->get_renderer('core_user');
142 $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
144 $currentfilter = '';
145 if ($firstinitial !== '' && $lastinitial !== '') {
146 $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
147 } else if ($firstinitial !== '') {
148 $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
149 } else if ($lastinitial !== '') {
150 $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
153 $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug, $userid, $searchvalue]);
155 $formdata = (object) [
156 'courseid' => $COURSE->id,
157 'initialsbars' => $initialsbar,
159 $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
161 return (object) [
162 'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
163 'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
164 'dropdowncontent' => $dropdowncontent,
169 * Creates and renders a custom user heading.
171 * @param stdClass $user The user object.
172 * @param int $courseid The course ID.
173 * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
174 * @return string The raw HTML to render.
176 public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true): string {
177 global $USER;
179 $headingdata = [
180 'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
181 'name' => fullname($user),
182 'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
185 if ($showbuttons) {
186 // Generate the data for the 'message' button.
187 $messagelinkattributes = array_map(function($name, $value) {
188 return ['name' => $name, 'value' => $value];
189 }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
190 $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
192 $headingdata['buttons'][] = [
193 'title' => get_string('message', 'message'),
194 'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
195 'icon' => ['name' => 't/message', 'component' => 'core'],
196 'linkattributes' => $messagelinkattributes
198 // Include js for messaging.
199 helper::messageuser_requirejs();
201 if ($USER->id != $user->id) {
202 // Generate the data for the 'contact' button.
203 $iscontact = api::is_contact($USER->id, $user->id);
204 $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
205 $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
206 $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
208 $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
209 $togglecontactlinkattributes = array_map(function($name, $value) {
210 if ($name === 'class') {
211 $value .= ' btn px-0';
213 return ['name' => $name, 'value' => $value];
214 }, array_keys($togglelinkparams), $togglelinkparams);
216 $headingdata['buttons'][] = [
217 'title' => get_string($contacttitle, 'message'),
218 'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
219 $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
220 'icon' => ['name' => $contacticon, 'component' => 'core'],
221 'linkattributes' => $togglecontactlinkattributes
223 // Include js for contact toggle.
224 helper::togglecontact_requirejs();
228 return $this->render_from_template('core_grades/user_heading', $headingdata);