Moodle release 4.1
[moodle.git] / grade / renderer.php
blob49aba778eb433410514cb0fac738d1658af1f1a4
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 'label' => $label,
63 'courseid' => $course->id,
64 'groupactionbaseurl' => $groupactionbaseurl
67 $context = context_course::instance($course->id);
69 if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
70 $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
71 } else {
72 $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
75 $activegroup = groups_get_course_group($course, true, $allowedgroups);
77 if ($activegroup) {
78 $group = groups_get_group($activegroup);
79 $data['selectedgroup'] = $group->name;
80 } else if ($activegroup === 0) {
81 $data['selectedgroup'] = get_string('allparticipants');
84 $this->page->requires->js_call_amd('core_grades/searchwidget/group', 'init');
85 return $this->render_from_template('core_grades/group_selector', $data);
88 /**
89 * Creates and renders a custom user heading.
91 * @param stdClass $user The user object.
92 * @param int $courseid The course ID.
93 * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
94 * @return string The raw HTML to render.
96 public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true) : string {
97 global $USER;
99 $headingdata = [
100 'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
101 'name' => fullname($user),
102 'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
105 if ($showbuttons) {
106 // Generate the data for the 'message' button.
107 $messagelinkattributes = array_map(function($name, $value) {
108 return ['name' => $name, 'value' => $value];
109 }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
110 $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
112 $headingdata['buttons'][] = [
113 'title' => get_string('message', 'message'),
114 'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
115 'icon' => ['name' => 't/message', 'component' => 'core'],
116 'linkattributes' => $messagelinkattributes
118 // Include js for messaging.
119 helper::messageuser_requirejs();
121 if ($USER->id != $user->id) {
122 // Generate the data for the 'contact' button.
123 $iscontact = api::is_contact($USER->id, $user->id);
124 $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
125 $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
126 $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
128 $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
129 $togglecontactlinkattributes = array_map(function($name, $value) {
130 if ($name === 'class') {
131 $value .= ' btn px-0';
133 return ['name' => $name, 'value' => $value];
134 }, array_keys($togglelinkparams), $togglelinkparams);
136 $headingdata['buttons'][] = [
137 'title' => get_string($contacttitle, 'message'),
138 'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
139 $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
140 'icon' => ['name' => $contacticon, 'component' => 'core'],
141 'linkattributes' => $togglecontactlinkattributes
143 // Include js for contact toggle.
144 helper::togglecontact_requirejs();
148 return $this->render_from_template('core_grades/user_heading', $headingdata);