Skip to content

Commit

Permalink
MDL-80746 gradereport_user: fix action bar rendering
Browse files Browse the repository at this point in the history
This code was unnecessarily pre-rendering some context properties used
by the action_menu, which also meant the mustache template expected HTML
in the context, which should be avoided. Instead of pre-rendering, just
export_for_template() and adjust the mustache template to render the sub
template for the respective templatables.
  • Loading branch information
snake committed Sep 16, 2024
1 parent dcfe475 commit 1339df6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
39 changes: 30 additions & 9 deletions grade/report/user/classes/output/action_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,46 @@ public function export_for_template(\renderer_base $output): array {
// If the user has the capability to view all grades, display the group selector (if applicable), the user selector
// and the view mode selector (if applicable).
if (has_capability('moodle/grade:viewall', $this->context)) {
$userreportrenderer = $PAGE->get_renderer('gradereport_user');
$course = get_course($courseid);
if ($course->groupmode) {
$groupselector = new \core_course\output\actionbar\group_selector($this->context);
$data['groupselector'] = $PAGE->get_renderer('core_course')->render($groupselector);
$data['groupselector'] = $groupselector->export_for_template($output);
}

$resetlink = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'group' => 0]);
$baseurl = new moodle_url('/grade/report/user/index.php', ['id' => $courseid]);
$PAGE->requires->js_call_amd('gradereport_user/user', 'init', [$baseurl->out(false)]);

$userselector = new \core_course\output\actionbar\user_selector(
course: $course,
resetlink: $resetlink,
userid: $this->userid,
groupid: $this->currentgroupid,
usersearch: $this->usersearch
);
$data['userselector'] = [
'courseid' => $courseid,
'content' => $userreportrenderer->users_selector(
course: get_course($courseid),
userid: $this->userid,
groupid: $this->currentgroupid,
usersearch: $this->usersearch
),
'content' => $userselector->export_for_template($output),
];

// Do not output the 'view mode' selector when in zero state or when the current user is viewing its own report.
if (!is_null($this->userid) && $USER->id != $this->userid) {
$data['viewasselector'] = $userreportrenderer->view_mode_selector($this->userid, $this->userview, $courseid);
$viewasotheruser = new moodle_url('/grade/report/user/index.php',
['id' => $courseid, 'userid' => $this->userid, 'userview' => GRADE_REPORT_USER_VIEW_USER]);
$viewasmyself = new moodle_url('/grade/report/user/index.php',
['id' => $courseid, 'userid' => $this->userid, 'userview' => GRADE_REPORT_USER_VIEW_SELF]);

$selectoroptions = [
$viewasotheruser->out(false) => get_string('otheruser', 'core_grades'),
$viewasmyself->out(false) => get_string('myself', 'core_grades')
];

$selectoractiveurl = $this->userview === GRADE_REPORT_USER_VIEW_USER ? $viewasotheruser : $viewasmyself;

$viewasselect = new \core\output\select_menu('viewas', $selectoroptions, $selectoractiveurl->out(false));
$viewasselect->set_label(get_string('viewas', 'core_grades'));

$data['viewasselector'] = $viewasselect->export_for_template($output);
}
}

Expand Down
6 changes: 6 additions & 0 deletions grade/report/user/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ public function view_user_selector(int $userid, int $userview): string {
* @param string $usersearch Search string.
* @return string The raw HTML to render.
* @throws coding_exception
* @deprecated since Moodle 4.5. See user_selector use in \gradereport_singleview\output\action_bar::export_for_template.
*/
public function users_selector(object $course, ?int $userid = null, ?int $groupid = null, string $usersearch = ''): string {
debugging('users_selector is deprecated.', DEBUG_DEVELOPER);

$courserenderer = $this->page->get_renderer('core_course');
$resetlink = new moodle_url('/grade/report/user/index.php', ['id' => $course->id, 'group' => 0]);
$baseurl = new moodle_url('/grade/report/user/index.php', ['id' => $course->id]);
Expand Down Expand Up @@ -168,9 +171,12 @@ public function user_navigation(graded_users_iterator $gui, int $userid, int $co
* @param int $userview The current view user setting constant
* @param int $courseid The course ID.
* @return string The raw HTML to render.
* @deprecated since Moodle 4.5 See select_menu use in \gradereport_singleview\output\action_bar::export_for_template.
*/
public function view_mode_selector(int $userid, int $userview, int $courseid): string {

debugging('view_mode_selector is deprecated.', DEBUG_DEVELOPER);

$viewasotheruser = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
'userview' => GRADE_REPORT_USER_VIEW_USER]);
$viewasmyself = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'userid' => $userid,
Expand Down
6 changes: 3 additions & 3 deletions grade/report/user/templates/action_bar.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@
{{/generalnavselector}}
{{#groupselector}}
<div class="navitem">
{{{groupselector}}}
{{>core/comboboxsearch}}
</div>
<div class="navitem-divider"></div>
{{/groupselector}}
{{#userselector}}
<span class="d-none" data-region="courseid" data-courseid="{{courseid}}" aria-hidden="true"></span>
<div class="navitem flex-column">
{{#content}}
{{{.}}}
{{>core/comboboxsearch}}
{{/content}}
</div>
<div class="navitem-divider"></div>
Expand All @@ -97,7 +97,7 @@
{{#viewasselector}}
<div class="navitem-divider"></div>
<div class="navitem">
{{{viewasselector}}}
{{>gradereport_user/view_mode_selector}}
</div>
<div class="navitem-divider"></div>
{{/viewasselector}}
Expand Down

0 comments on commit 1339df6

Please sign in to comment.