Kumi
79aa7978bb
Defines a new capability 'report/usergrades:view' with associated risks and context level. Grants managers permission to access this report. Updates page setup to check the new capability, ensuring appropriate access control. Increments plugin version to reflect these changes. Enhances security and manages user access more effectively.
105 lines
3.6 KiB
PHP
105 lines
3.6 KiB
PHP
<?php
|
|
require_once('../../config.php');
|
|
require_once($CFG->libdir . '/adminlib.php');
|
|
require_once($CFG->libdir . '/gradelib.php');
|
|
require_once($CFG->libdir . '/tablelib.php');
|
|
require_once($CFG->libdir . '/formslib.php');
|
|
require_once($CFG->dirroot . '/user/selector/lib.php');
|
|
|
|
// Set up the page context and other configurations
|
|
admin_externalpage_setup('report_usergrades', '', null, '', array('capability' => 'report/usergrades:view'));
|
|
|
|
class user_selector extends user_selector_base
|
|
{
|
|
|
|
public function find_users($search)
|
|
{
|
|
global $DB;
|
|
|
|
// Define the fields you want to select.
|
|
$fields = 'id, firstname, lastname, email';
|
|
|
|
// Create an SQL query to find users based on the search criteria.
|
|
$sql = "SELECT $fields
|
|
FROM {user}
|
|
WHERE CONCAT(firstname, ' ', lastname) LIKE ? OR email LIKE ?";
|
|
|
|
// Execute the SQL query and retrieve the users.
|
|
$users = $DB->get_records_sql($sql, array('%' . $search . '%', '%' . $search . '%'));
|
|
|
|
// Format the users into a format expected by the user selector.
|
|
$options = [];
|
|
foreach ($users as $user) {
|
|
$options[$user->id] = $user->firstname . ' ' . $user->lastname . ' (' . $user->email . ')';
|
|
}
|
|
|
|
return array(get_string('users') => $options);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Form class for user selection
|
|
*/
|
|
class usergrades_form extends moodleform
|
|
{
|
|
public function definition()
|
|
{
|
|
$mform = $this->_form;
|
|
|
|
// User selector
|
|
$user_selector = new user_selector('userid', array('multiselect' => false));
|
|
$users = $user_selector->find_users('');
|
|
|
|
$mform->addElement('select', 'userid', get_string('selectuser', 'report_usergrades'), $users[get_string('users')]);
|
|
|
|
// Add form action buttons
|
|
$this->add_action_buttons(false, get_string('showgrades', 'report_usergrades'));
|
|
}
|
|
}
|
|
|
|
// Instantiate the form
|
|
$mform = new usergrades_form();
|
|
echo $OUTPUT->header();
|
|
echo $OUTPUT->heading(get_string('usergradesreport', 'report_usergrades'));
|
|
|
|
// Form processing and displaying is done here
|
|
if ($mform->is_cancelled()) {
|
|
// Handle form cancellation, if necessary
|
|
redirect(new moodle_url('/admin/report.php'));
|
|
} else if ($data = $mform->get_data()) {
|
|
$userid = $data->userid;
|
|
|
|
// Fetch and display user grades if a user is selected
|
|
if (!empty($userid)) {
|
|
$user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
|
|
$courses = enrol_get_users_courses($user->id, true);
|
|
|
|
$table = new flexible_table('user-grades-report');
|
|
$table->define_baseurl($CFG->wwwroot . '/report/usergrades/index.php');
|
|
$table->define_columns(array('course', 'activity', 'grade'));
|
|
$table->define_headers(array(get_string('course'), get_string('activity'), get_string('grade', 'report_usergrades')));
|
|
|
|
$table->setup();
|
|
|
|
foreach ($courses as $course) {
|
|
$grade_items = grade_item::fetch_all(array('courseid' => $course->id));
|
|
|
|
foreach ($grade_items as $item) {
|
|
$grade = grade_grade::fetch(array('itemid' => $item->id, 'userid' => $user->id));
|
|
$gradename = $item->get_name();
|
|
$grade_display = $grade ? $grade->finalgrade . ' / ' . $grade->rawgrademax : '-';
|
|
|
|
$table->add_data(array(format_string($course->fullname), format_string($gradename), $grade_display));
|
|
}
|
|
}
|
|
|
|
$table->finish_output();
|
|
} else {
|
|
echo $OUTPUT->notification(get_string('nouserselected', 'report_usergrades'), 'notifyproblem');
|
|
}
|
|
} else {
|
|
// Display form if no data is submitted
|
|
$mform->display();
|
|
}
|
|
|
|
echo $OUTPUT->footer();
|