MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / grade / renderer.php
blob287b8a6c1bb41089cb1a8414b383cbb32aa9a388
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 The base URL for the group action.
49 * @return string|null The raw HTML to render.
51 public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
52 global $USER;
54 // Make sure that group mode is enabled.
55 if (!$groupmode = $course->groupmode) {
56 return null;
59 $sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
60 'courseid' => $course->id,
61 'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
62 ]);
64 $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') :
65 get_string('selectgroupsseparate');
67 $data = [
68 'name' => 'group',
69 'label' => $label,
70 'courseid' => $course->id,
71 'groupactionbaseurl' => $groupactionbaseurl
74 $context = context_course::instance($course->id);
76 if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
77 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
78 } else {
79 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
82 $activegroup = groups_get_course_group($course, true, $allowedgroups);
83 $data['group'] = $activegroup;
85 if ($activegroup) {
86 $group = groups_get_group($activegroup);
87 $data['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
88 } else if ($activegroup === 0) {
89 $data['selectedgroup'] = get_string('allparticipants');
92 $groupdropdown = new comboboxsearch(
93 false,
94 $this->render_from_template('core_group/comboboxsearch/group_selector', $data),
95 $sbody,
96 'group-search',
97 'groupsearchwidget',
98 'groupsearchdropdown overflow-auto w-100',
100 return $this->render_from_template($groupdropdown->get_template(), $groupdropdown->export_for_template($this));
104 * Build the data to render the initials bar filter within the gradebook.
105 * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
106 * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
108 * @param object $course The course object.
109 * @param context $context Our current context.
110 * @param string $slug The slug for the report that called this function.
111 * @return stdClass The data to output.
113 public function initials_selector(
114 object $course,
115 context $context,
116 string $slug
117 ): stdClass {
118 global $SESSION, $COURSE;
119 // User search.
120 $url = new moodle_url($slug, ['id' => $course->id]);
121 $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
122 $lastinitial = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
124 $renderer = $this->page->get_renderer('core_user');
125 $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
127 $currentfilter = '';
128 if ($firstinitial !== '' && $lastinitial !== '') {
129 $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
130 } else if ($firstinitial !== '') {
131 $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
132 } else if ($lastinitial !== '') {
133 $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
136 $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug]);
138 $formdata = (object) [
139 'courseid' => $COURSE->id,
140 'initialsbars' => $initialsbar,
142 $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
144 return (object) [
145 'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
146 'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
147 'dropdowncontent' => $dropdowncontent,
152 * Creates and renders a custom user heading.
154 * @param stdClass $user The user object.
155 * @param int $courseid The course ID.
156 * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
157 * @return string The raw HTML to render.
159 public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
160 global $USER;
162 $headingdata = [
163 'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
164 'name' => fullname($user),
165 'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
168 if ($showbuttons) {
169 // Generate the data for the 'message' button.
170 $messagelinkattributes = array_map(function($name, $value) {
171 return ['name' => $name, 'value' => $value];
172 }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
173 $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
175 $headingdata['buttons'][] = [
176 'title' => get_string('message', 'message'),
177 'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
178 'icon' => ['name' => 't/message', 'component' => 'core'],
179 'linkattributes' => $messagelinkattributes
181 // Include js for messaging.
182 helper::messageuser_requirejs();
184 if ($USER->id != $user->id) {
185 // Generate the data for the 'contact' button.
186 $iscontact = api::is_contact($USER->id, $user->id);
187 $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
188 $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
189 $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
191 $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
192 $togglecontactlinkattributes = array_map(function($name, $value) {
193 if ($name === 'class') {
194 $value .= ' btn px-0';
196 return ['name' => $name, 'value' => $value];
197 }, array_keys($togglelinkparams), $togglelinkparams);
199 $headingdata['buttons'][] = [
200 'title' => get_string($contacttitle, 'message'),
201 'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
202 $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
203 'icon' => ['name' => $contacticon, 'component' => 'core'],
204 'linkattributes' => $togglecontactlinkattributes
206 // Include js for contact toggle.
207 helper::togglecontact_requirejs();
211 return $this->render_from_template('core_grades/user_heading', $headingdata);