Merge branch 'MDL-77656' of https://github.com/timhunt/moodle
[moodle.git] / grade / renderer.php
blobbe3e4e2d0149a99af596c6ca3eed4e2f43afc900
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_grades\output\action_bar;
20 use core_message\helper;
21 use core_message\api;
23 /**
24 * Renderer class for the grade pages.
26 * @package core_grades
27 * @copyright 2021 Mihail Geshoski <mihail@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 class core_grades_renderer extends plugin_renderer_base {
32 /**
33 * Renders the action bar for a given page.
35 * @param action_bar $actionbar
36 * @return string The HTML output
38 public function render_action_bar(action_bar $actionbar): string {
39 $data = $actionbar->export_for_template($this);
40 return $this->render_from_template($actionbar->get_template(), $data);
43 /**
44 * Renders the group selector trigger element.
46 * @param object $course The course object.
47 * @param string|null $groupactionbaseurl The base URL for the group action.
48 * @return string|null The raw HTML to render.
50 public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
51 global $USER;
53 // Make sure that group mode is enabled.
54 if (!$groupmode = $course->groupmode) {
55 return null;
58 $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') :
59 get_string('selectgroupsseparate');
61 $data = [
62 'name' => 'group',
63 'label' => $label,
64 'courseid' => $course->id,
65 'groupactionbaseurl' => $groupactionbaseurl
68 $context = context_course::instance($course->id);
70 if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
71 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
72 } else {
73 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
76 $activegroup = groups_get_course_group($course, true, $allowedgroups);
77 $data['group'] = $activegroup;
79 if ($activegroup) {
80 $group = groups_get_group($activegroup);
81 $data['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
82 } else if ($activegroup === 0) {
83 $data['selectedgroup'] = get_string('allparticipants');
86 $this->page->requires->js_call_amd('core_grades/searchwidget/group', 'init');
87 return $this->render_from_template('core_grades/group_selector', $data);
90 /**
91 * Build the data to render the initials bar filter within the gradebook.
92 * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
93 * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
95 * @param object $course The course object.
96 * @param context $context Our current context.
97 * @param string $slug The slug for the report that called this function.
98 * @return stdClass The data to output.
100 public function initials_selector(
101 object $course,
102 context $context,
103 string $slug
104 ): stdClass {
105 global $SESSION, $COURSE;
106 // User search.
107 $url = new moodle_url($slug, ['id' => $course->id]);
108 $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
109 $lastinitial = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
111 $renderer = $this->page->get_renderer('core_user');
112 $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
114 $currentfilter = '';
115 if ($firstinitial !== '' && $lastinitial !== '') {
116 $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
117 } else if ($firstinitial !== '') {
118 $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
119 } else if ($lastinitial !== '') {
120 $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
123 $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug]);
125 $formdata = (object) [
126 'courseid' => $COURSE->id,
127 'initialsbars' => $initialsbar,
129 $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
131 return (object) [
132 'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
133 'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
134 'dropdowncontent' => $dropdowncontent,
139 * Creates and renders a custom user heading.
141 * @param stdClass $user The user object.
142 * @param int $courseid The course ID.
143 * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
144 * @return string The raw HTML to render.
146 public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
147 global $USER;
149 $headingdata = [
150 'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
151 'name' => fullname($user),
152 'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
155 if ($showbuttons) {
156 // Generate the data for the 'message' button.
157 $messagelinkattributes = array_map(function($name, $value) {
158 return ['name' => $name, 'value' => $value];
159 }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
160 $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
162 $headingdata['buttons'][] = [
163 'title' => get_string('message', 'message'),
164 'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
165 'icon' => ['name' => 't/message', 'component' => 'core'],
166 'linkattributes' => $messagelinkattributes
168 // Include js for messaging.
169 helper::messageuser_requirejs();
171 if ($USER->id != $user->id) {
172 // Generate the data for the 'contact' button.
173 $iscontact = api::is_contact($USER->id, $user->id);
174 $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
175 $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
176 $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
178 $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
179 $togglecontactlinkattributes = array_map(function($name, $value) {
180 if ($name === 'class') {
181 $value .= ' btn px-0';
183 return ['name' => $name, 'value' => $value];
184 }, array_keys($togglelinkparams), $togglelinkparams);
186 $headingdata['buttons'][] = [
187 'title' => get_string($contacttitle, 'message'),
188 'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
189 $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
190 'icon' => ['name' => $contacticon, 'component' => 'core'],
191 'linkattributes' => $togglecontactlinkattributes
193 // Include js for contact toggle.
194 helper::togglecontact_requirejs();
198 return $this->render_from_template('core_grades/user_heading', $headingdata);