2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Provides user rendering functionality such as printing private files tree and displaying a search utility
21 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Provides user rendering functionality such as printing private files tree and displaying a search utility
29 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class core_user_renderer
extends plugin_renderer_base
{
35 * Prints user search utility that can search user by first initial of firstname and/or first initial of lastname
36 * Prints a header with a title and the number of users found within that subset
37 * @param string $url the url to return to, complete with any parameters needed for the return
38 * @param string $firstinitial the first initial of the firstname
39 * @param string $lastinitial the first initial of the lastname
40 * @param int $usercount the amount of users meeting the search criteria
41 * @param int $totalcount the amount of users of the set/subset being searched
42 * @param string $heading heading of the subset being searched, default is All Participants
43 * @return string html output
45 public function user_search($url, $firstinitial, $lastinitial, $usercount, $totalcount, $heading = null) {
47 if ($firstinitial !== 'all') {
48 set_user_preference('ifirst', $firstinitial);
50 if ($lastinitial !== 'all') {
51 set_user_preference('ilast', $lastinitial);
54 if (!isset($heading)) {
55 $heading = get_string('allparticipants');
58 $content = html_writer
::start_tag('form', array('action' => new moodle_url($url)));
59 $content .= html_writer
::start_tag('div');
61 // Search utility heading.
62 $content .= $this->output
->heading($heading.get_string('labelsep', 'langconfig').$usercount.'/'.$totalcount, 3);
65 $prefixfirst = 'sifirst';
66 $prefixlast = 'silast';
67 $content .= $this->output
->initials_bar($firstinitial, 'firstinitial', get_string('firstname'), $prefixfirst, $url);
68 $content .= $this->output
->initials_bar($lastinitial, 'lastinitial', get_string('lastname'), $prefixlast, $url);
70 $content .= html_writer
::end_tag('div');
71 $content .= html_writer
::tag('div', ' ');
72 $content .= html_writer
::end_tag('form');
78 * Construct a partial user search that'll require form handling implemented by the caller.
79 * This allows the developer to have an initials bar setup that does not automatically redirect.
81 * @param string $url the url to return to, complete with any parameters needed for the return
82 * @param string $firstinitial the first initial of the firstname
83 * @param string $lastinitial the first initial of the lastname
84 * @param bool $minirender Return a trimmed down view of the initials bar.
85 * @return string html output
86 * @throws coding_exception
88 public function partial_user_search(String $url, String $firstinitial, String $lastinitial, Bool $minirender = false): String {
92 if ($firstinitial !== 'all') {
93 set_user_preference('ifirst', $firstinitial);
95 if ($lastinitial !== 'all') {
96 set_user_preference('ilast', $lastinitial);
100 $prefixfirst = 'sifirst';
101 $prefixlast = 'silast';
102 $content .= $this->output
->initials_bar(
105 get_string('firstname'),
111 $content .= $this->output
->initials_bar(
114 get_string('lastname'),
125 * Displays the list of tagged users
127 * @param array $userlist
128 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
129 * are displayed on the page and the per-page limit may be bigger
132 public function user_list($userlist, $exclusivemode) {
133 $tagfeed = new core_tag\output\tagfeed
();
134 foreach ($userlist as $user) {
135 $userpicture = $this->output
->user_picture($user, array('size' => $exclusivemode ?
100 : 35));
136 $fullname = fullname($user);
137 if (user_can_view_profile($user)) {
138 $profilelink = new moodle_url('/user/view.php', array('id' => $user->id
));
139 $fullname = html_writer
::link($profilelink, $fullname);
141 $tagfeed->add($userpicture, $fullname);
144 $items = $tagfeed->export_for_template($this->output
);
146 if ($exclusivemode) {
147 $output = '<div><ul class="inline-list">';
148 foreach ($items['items'] as $item) {
149 $output .= '<li><div class="user-box">'. $item['img'] . $item['heading'] ."</div></li>\n";
151 $output .= "</ul></div>\n";
155 return $this->output
->render_from_template('core_tag/tagfeed', $items);
159 * Renders the unified filter element for the course participants page.
160 * @deprecated since 3.9
161 * @throws coding_exception
163 public function unified_filter() {
164 throw new coding_exception('unified_filter cannot be used any more, please use participants_filter instead');
169 * Render the data required for the participants filter on the course participants page.
171 * @param context $context The context of the course being displayed
172 * @param string $tableregionid Container of the table to be updated by this filter, is used to retrieve the table
175 public function participants_filter(context
$context, string $tableregionid): string {
176 $renderable = new \core_user\output\
participants_filter($context, $tableregionid);
177 $templatecontext = $renderable->export_for_template($this->output
);
179 return $this->output
->render_from_template('core_user/participantsfilter', $templatecontext);
183 * Returns a formatted filter option.
185 * @param int $filtertype The filter type (e.g. status, role, group, enrolment, last access).
186 * @param string $criteria The string label of the filter type.
187 * @param int $value The value for the filter option.
188 * @param string $label The string representation of the filter option's value.
189 * @return array The formatted option with the ['filtertype:value' => 'criteria: label'] format.
191 protected function format_filter_option($filtertype, $criteria, $value, $label) {
192 $optionlabel = get_string('filteroption', 'moodle', (object)['criteria' => $criteria, 'value' => $label]);
193 $optionvalue = "$filtertype:$value";
194 return [$optionvalue => $optionlabel];
198 * Handles cases when after reloading the applied filters are missing in the filter options.
200 * @param array $filtersapplied The applied filters.
201 * @param array $filteroptions The filter options.
202 * @return array The formatted options with the ['filtertype:value' => 'criteria: label'] format.
204 private function handle_missing_applied_filters($filtersapplied, $filteroptions) {
207 foreach ($filtersapplied as $filter) {
208 if (!array_key_exists($filter, $filteroptions)) {
209 $filtervalue = explode(':', $filter);
210 if (count($filtervalue) !== 2) {
213 $key = $filtervalue[0];
214 $value = $filtervalue[1];
217 case USER_FILTER_LAST_ACCESS
:
218 $now = usergetmidnight(time());
219 $criteria = get_string('usersnoaccesssince');
221 for ($i = 1; $i < 7; $i++
) {
222 $timestamp = strtotime('-' . $i . ' days', $now);
223 if ($timestamp < $value) {
226 $val = get_string('numdays', 'moodle', $i);
227 $filteroptions +
= $this->format_filter_option(USER_FILTER_LAST_ACCESS
, $criteria, $timestamp, $val);
230 for ($i = 1; $i < 10; $i++
) {
231 $timestamp = strtotime('-'.$i.' weeks', $now);
232 if ($timestamp < $value) {
235 $val = get_string('numweeks', 'moodle', $i);
236 $filteroptions +
= $this->format_filter_option(USER_FILTER_LAST_ACCESS
, $criteria, $timestamp, $val);
239 for ($i = 2; $i < 12; $i++
) {
240 $timestamp = strtotime('-'.$i.' months', $now);
241 if ($timestamp < $value) {
244 $val = get_string('nummonths', 'moodle', $i);
245 $filteroptions +
= $this->format_filter_option(USER_FILTER_LAST_ACCESS
, $criteria, $timestamp, $val);
248 $timestamp = strtotime('-1 year', $now);
249 if ($timestamp >= $value) {
250 $val = get_string('numyear', 'moodle', 1);
251 $filteroptions +
= $this->format_filter_option(USER_FILTER_LAST_ACCESS
, $criteria, $timestamp, $val);
254 case USER_FILTER_ROLE
:
255 $criteria = get_string('role');
256 if ($role = $DB->get_record('role', array('id' => $value))) {
257 $role = role_get_name($role);
258 $filteroptions +
= $this->format_filter_option(USER_FILTER_ROLE
, $criteria, $value, $role);
264 return $filteroptions;