From 1046e06e832127da33c25e08d7a4ed9b92896c8d Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Sat, 8 Oct 2022 00:26:20 +0800 Subject: [PATCH] MDL-75513 gradereport_user: Create trigger element for the user selector Creates a renderer method and a template for the element that would trigger the user selector. --- grade/report/user/renderer.php | 49 ++++++++++++ grade/report/user/styles.css | 5 ++ .../user/templates/user_selector.mustache | 74 +++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 grade/report/user/templates/user_selector.mustache diff --git a/grade/report/user/renderer.php b/grade/report/user/renderer.php index f5b3a86fbea..068bc1b41e9 100644 --- a/grade/report/user/renderer.php +++ b/grade/report/user/renderer.php @@ -80,4 +80,53 @@ class gradereport_user_renderer extends plugin_renderer_base { return $output; } + /** + * Renders the user selector trigger element. + * + * @param object $course The course object. + * @param int|null $userid The user ID. + * @param int|null $groupid The group ID. + * @return string The raw HTML to render. + * @throws coding_exception + */ + public function users_selector(object $course, ?int $userid = null, ?int $groupid = null): string { + + $data = [ + 'courseid' => $course->id, + 'groupid' => $groupid ?? 0, + ]; + + // If a particular option is selected (not in zero state). + if (!is_null($userid)) { + if ($userid) { // A single user selected. + $user = core_user::get_user($userid); + $data['selectedoption'] = [ + 'image' => $this->user_picture($user, ['size' => 40, 'link' => false]), + 'text' => fullname($user), + 'additionaltext' => $user->email, + ]; + } else { // All users selected. + // Get the total number of users. + $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol); + $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol); + $showonlyactiveenrol = $showonlyactiveenrol || + !has_capability('moodle/course:viewsuspendedusers', context_course::instance($course->id)); + $gui = new graded_users_iterator($course, null, $groupid); + $gui->require_active_enrolment($showonlyactiveenrol); + $gui->init(); + $totalusersnum = 0; + while ($userdata = $gui->next_user()) { + $totalusersnum++; + } + $gui->close(); + + $data['selectedoption'] = [ + 'text' => get_string('allusersnum', 'gradereport_user', $totalusersnum), + ]; + } + } + + $this->page->requires->js_call_amd('gradereport_user/user', 'init'); + return $this->render_from_template('gradereport_user/user_selector', $data); + } } diff --git a/grade/report/user/styles.css b/grade/report/user/styles.css index a8d9214b73f..9cd34f63c6f 100644 --- a/grade/report/user/styles.css +++ b/grade/report/user/styles.css @@ -127,6 +127,11 @@ align-items: center; } +.path-grade-report-user .user-selector .userinitials { + width: 40px; + height: 40px; +} + @media print { .path-grade-report-user .user-grade td.category { border-left: none; diff --git a/grade/report/user/templates/user_selector.mustache b/grade/report/user/templates/user_selector.mustache new file mode 100644 index 00000000000..985fa0c0b50 --- /dev/null +++ b/grade/report/user/templates/user_selector.mustache @@ -0,0 +1,74 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template gradereport_user/user_selector + + The user selector trigger element. + + Context variables required for this template: + * courseid - The course ID. + * groupid - The group ID. + * selectedoption - (optional) Object containing information about the selected option. + * image - The image corresponding to the selected option. + * text - The text of the selected option. + * additionaltext - (optional) Additional text displayed below the selected option (e.g. the user email) + + Example context (json): + { + "courseid": "2", + "groupid": "2", + "selectedoption": { + "image": "\"\"", + "text": "John Doe", + "additionaltext": "johndoe@example.com" + } + } +}} +
+ +