MDL-73975 course: Remove course_search_form template
[moodle.git] / grade / renderer.php
blob8beccd2549f7a9ddd9281526b1a03688b9e7a81a
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.
51 public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
52 global $USER;
54 if ($groupactionbaseurl !== null) {
55 debugging(
56 'The $groupactionbaseurl argument has been deprecated. Please remove it from your method calls.',
57 DEBUG_DEVELOPER,
60 // Make sure that group mode is enabled.
61 if (!$groupmode = $course->groupmode) {
62 return null;
65 $sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
66 'courseid' => $course->id,
67 'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
68 'instance' => rand(),
69 ]);
71 $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') : get_string('selectgroupsseparate');
73 $buttondata = ['label' => $label];
75 $context = context_course::instance($course->id);
77 if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
78 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
79 } else {
80 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
83 $activegroup = groups_get_course_group($course, true, $allowedgroups);
84 $buttondata['group'] = $activegroup;
86 if ($activegroup) {
87 $group = groups_get_group($activegroup);
88 $buttondata['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
89 } else if ($activegroup === 0) {
90 $buttondata['selectedgroup'] = get_string('allparticipants');
93 $groupdropdown = new comboboxsearch(
94 false,
95 $this->render_from_template('core_group/comboboxsearch/group_selector', $buttondata),
96 $sbody,
97 'group-search',
98 'groupsearchwidget',
99 'groupsearchdropdown overflow-auto',
100 null,
101 true,
102 $label,
103 'group',
104 $activegroup
106 return $this->render_from_template($groupdropdown->get_template(), $groupdropdown->export_for_template($this));
110 * Build the data to render the initials bar filter within the gradebook.
111 * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
112 * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
114 * @param object $course The course object.
115 * @param context $context Our current context.
116 * @param string $slug The slug for the report that called this function.
117 * @return stdClass The data to output.
119 public function initials_selector(
120 object $course,
121 context $context,
122 string $slug
123 ): stdClass {
124 global $SESSION, $COURSE;
125 // User search.
126 $searchvalue = optional_param('gpr_search', null, PARAM_NOTAGS);
127 $userid = optional_param('grp_userid', null, PARAM_INT);
128 $url = new moodle_url($slug, ['id' => $course->id]);
129 $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
130 $lastinitial = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
132 $renderer = $this->page->get_renderer('core_user');
133 $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
135 $currentfilter = '';
136 if ($firstinitial !== '' && $lastinitial !== '') {
137 $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
138 } else if ($firstinitial !== '') {
139 $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
140 } else if ($lastinitial !== '') {
141 $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
144 $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug, $userid, $searchvalue]);
146 $formdata = (object) [
147 'courseid' => $COURSE->id,
148 'initialsbars' => $initialsbar,
150 $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
152 return (object) [
153 'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
154 'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
155 'dropdowncontent' => $dropdowncontent,
160 * Creates and renders a custom user heading.
162 * @param stdClass $user The user object.
163 * @param int $courseid The course ID.
164 * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
165 * @return string The raw HTML to render.
167 public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true): string {
168 global $USER;
170 $headingdata = [
171 'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
172 'name' => fullname($user),
173 'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
176 if ($showbuttons) {
177 // Generate the data for the 'message' button.
178 $messagelinkattributes = array_map(function($name, $value) {
179 return ['name' => $name, 'value' => $value];
180 }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
181 $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
183 $headingdata['buttons'][] = [
184 'title' => get_string('message', 'message'),
185 'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
186 'icon' => ['name' => 't/message', 'component' => 'core'],
187 'linkattributes' => $messagelinkattributes
189 // Include js for messaging.
190 helper::messageuser_requirejs();
192 if ($USER->id != $user->id) {
193 // Generate the data for the 'contact' button.
194 $iscontact = api::is_contact($USER->id, $user->id);
195 $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
196 $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
197 $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
199 $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
200 $togglecontactlinkattributes = array_map(function($name, $value) {
201 if ($name === 'class') {
202 $value .= ' btn px-0';
204 return ['name' => $name, 'value' => $value];
205 }, array_keys($togglelinkparams), $togglelinkparams);
207 $headingdata['buttons'][] = [
208 'title' => get_string($contacttitle, 'message'),
209 'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
210 $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
211 'icon' => ['name' => $contacticon, 'component' => 'core'],
212 'linkattributes' => $togglecontactlinkattributes
214 // Include js for contact toggle.
215 helper::togglecontact_requirejs();
219 return $this->render_from_template('core_grades/user_heading', $headingdata);