fix: Enhances user grade selection and display

Adds a custom user selector class to streamline user search
and selection based on name or email.

Improves grade display by showing the final grade and maximum
possible grade, enhancing clarity for end-users.

Includes new language strings for improved user interface
text clarity during user selection and grade reporting.

Updates the plugin version to reflect substantive changes.
This commit is contained in:
Kumi 2024-11-20 15:10:57 +01:00
parent a0d65305ab
commit e77df1e29f
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 51 additions and 14 deletions

View file

@ -1,25 +1,56 @@
<?php <?php
require_once('../../config.php'); require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir.'/gradelib.php'); require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->libdir.'/tablelib.php'); require_once($CFG->libdir . '/tablelib.php');
require_once($CFG->libdir.'/formslib.php'); require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot.'/user/selector/lib.php'); require_once($CFG->dirroot . '/user/selector/lib.php');
// Set up the page context and other configurations // Set up the page context and other configurations
admin_externalpage_setup('report_usergrades'); admin_externalpage_setup('report_usergrades');
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 * Form class for user selection
*/ */
class usergrades_form extends moodleform { class usergrades_form extends moodleform
public function definition() { {
public function definition()
{
$mform = $this->_form; $mform = $this->_form;
// User selector // User selector
$user_selector = new user_selector('userid', array('multiselect' => false)); $user_selector = new user_selector('userid', array('multiselect' => false));
$user_selector->set_label(get_string('user')); $users = $user_selector->find_users('');
$user_selector->get_view($mform);
$mform->addElement('select', 'userid', get_string('selectuser', 'report_usergrades'), $users[get_string('users')]);
// Add form action buttons // Add form action buttons
$this->add_action_buttons(false, get_string('showgrades', 'report_usergrades')); $this->add_action_buttons(false, get_string('showgrades', 'report_usergrades'));
@ -32,7 +63,11 @@ echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('usergradesreport', 'report_usergrades')); echo $OUTPUT->heading(get_string('usergradesreport', 'report_usergrades'));
// Form processing and displaying is done here // Form processing and displaying is done here
if ($data = $mform->get_data()) { if ($mform->is_cancelled()) {
// Handle form cancellation, if necessary
redirect(new moodle_url('/admin/report.php'));
} else if ($data = $mform->get_data()) {
print_r($data);
$userid = $data->userid; $userid = $data->userid;
// Fetch and display user grades if a user is selected // Fetch and display user grades if a user is selected
@ -41,9 +76,9 @@ if ($data = $mform->get_data()) {
$courses = enrol_get_users_courses($user->id, true); $courses = enrol_get_users_courses($user->id, true);
$table = new flexible_table('user-grades-report'); $table = new flexible_table('user-grades-report');
$table->define_baseurl($CFG->wwwroot.'/report/usergrades/index.php'); $table->define_baseurl($CFG->wwwroot . '/report/usergrades/index.php');
$table->define_columns(array('course', 'activity', 'grade')); $table->define_columns(array('course', 'activity', 'grade'));
$table->define_headers(array('Course', 'Activity', 'Grade')); $table->define_headers(array(get_string('course'), get_string('activity'), get_string('grade', 'report_usergrades')));
$table->setup(); $table->setup();
@ -53,7 +88,7 @@ if ($data = $mform->get_data()) {
foreach ($grade_items as $item) { foreach ($grade_items as $item) {
$grade = grade_grade::fetch(array('itemid' => $item->id, 'userid' => $user->id)); $grade = grade_grade::fetch(array('itemid' => $item->id, 'userid' => $user->id));
$gradename = $item->get_name(); $gradename = $item->get_name();
$grade_display = $grade ? $grade->str_grade : '-'; $grade_display = $grade ? $grade->finalgrade . ' / ' . $grade->rawgrademax : '-';
$table->add_data(array(format_string($course->fullname), format_string($gradename), $grade_display)); $table->add_data(array(format_string($course->fullname), format_string($gradename), $grade_display));
} }

View file

@ -5,3 +5,5 @@ $string['usergradesreport'] = 'User Grades Report';
$string['nouserselected'] = 'No user selected. Please select a user to view their grades.'; $string['nouserselected'] = 'No user selected. Please select a user to view their grades.';
$string['showgrades'] = 'Show Grades'; $string['showgrades'] = 'Show Grades';
$string['user'] = 'User'; $string['user'] = 'User';
$string['selectuser'] = 'Select User';
$string['grade'] = 'Grade';

View file

@ -2,7 +2,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->component = 'report_usergrades'; $plugin->component = 'report_usergrades';
$plugin->version = 2024111900; $plugin->version = 2024112000;
$plugin->requires = 2022041900; $plugin->requires = 2022041900;
$plugin->maturity = MATURITY_ALPHA; $plugin->maturity = MATURITY_ALPHA;
$plugin->release = 'v1.0'; $plugin->release = 'v1.0';