moodle/grade/report/user/renderer.php

133 lines
5.0 KiB
PHP
Raw Normal View History

<?php
// 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 <http://www.gnu.org/licenses/>.
/**
* Renderer for the grade user report
*
* @package gradereport_user
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Custom renderer for the user grade report
*
* To get an instance of this use the following code:
* $renderer = $PAGE->get_renderer('gradereport_user');
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradereport_user_renderer extends plugin_renderer_base {
2022-09-13 11:01:31 +02:00
/**
* Small rendering function that helps with outputting the relevant user selector.
*
* @param string $report
* @param stdClass $course
* @param int $userid
* @param null|int $groupid
2022-09-13 11:01:31 +02:00
* @param bool $includeall
* @return string The raw HTML to render.
* @throws coding_exception
*/
public function graded_users_selector(string $report, stdClass $course, int $userid, ?int $groupid, bool $includeall): string {
$select = grade_get_graded_users_select($report, $course, $userid, $groupid, $includeall);
2022-09-13 11:01:31 +02:00
$output = html_writer::tag('div', $this->output->render($select), ['id' => 'graded_users_selector']);
$output .= html_writer::tag('p', '', ['style' => 'page-break-after: always;']);
return $output;
}
/**
* Creates and renders the single select box for the user view.
*
* @param int $userid The selected userid
* @param int $userview The current view user setting constant
* @return string
*/
2022-09-13 11:01:31 +02:00
public function view_user_selector(int $userid, int $userview): string {
global $USER;
$url = $this->page->url;
if ($userid != $USER->id) {
$url->param('userid', $userid);
}
2022-09-13 11:01:31 +02:00
$options = [
GRADE_REPORT_USER_VIEW_USER => get_string('otheruser', 'grades'),
GRADE_REPORT_USER_VIEW_SELF => get_string('myself', 'grades')
2022-09-13 11:01:31 +02:00
];
$select = new single_select($url, 'userview', $options, $userview, null);
$select->label = get_string('viewas', 'grades');
2022-09-13 11:01:31 +02:00
$output = html_writer::tag('div', $this->output->render($select), ['class' => 'view_users_selector']);
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);
}
}