From e77df1e29fdabbeaf91bce2bef1efed3f9cab88d Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 20 Nov 2024 15:10:57 +0100 Subject: [PATCH] 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. --- index.php | 61 +++++++++++++++++++++++++++-------- lang/en/report_usergrades.php | 2 ++ version.php | 2 +- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/index.php b/index.php index 56aaff7..fe9d149 100644 --- a/index.php +++ b/index.php @@ -1,25 +1,56 @@ 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'); +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'); +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() { +class usergrades_form extends moodleform +{ + public function definition() + { $mform = $this->_form; // User selector $user_selector = new user_selector('userid', array('multiselect' => false)); - $user_selector->set_label(get_string('user')); - $user_selector->get_view($mform); + $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')); @@ -32,7 +63,11 @@ echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('usergradesreport', 'report_usergrades')); // 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; // 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); $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_headers(array('Course', 'Activity', 'Grade')); + $table->define_headers(array(get_string('course'), get_string('activity'), get_string('grade', 'report_usergrades'))); $table->setup(); @@ -53,7 +88,7 @@ if ($data = $mform->get_data()) { foreach ($grade_items as $item) { $grade = grade_grade::fetch(array('itemid' => $item->id, 'userid' => $user->id)); $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)); } diff --git a/lang/en/report_usergrades.php b/lang/en/report_usergrades.php index 9ab6bde..4f136e1 100644 --- a/lang/en/report_usergrades.php +++ b/lang/en/report_usergrades.php @@ -5,3 +5,5 @@ $string['usergradesreport'] = 'User Grades Report'; $string['nouserselected'] = 'No user selected. Please select a user to view their grades.'; $string['showgrades'] = 'Show Grades'; $string['user'] = 'User'; +$string['selectuser'] = 'Select User'; +$string['grade'] = 'Grade'; \ No newline at end of file diff --git a/version.php b/version.php index 6129719..01d9cad 100644 --- a/version.php +++ b/version.php @@ -2,7 +2,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'report_usergrades'; -$plugin->version = 2024111900; +$plugin->version = 2024112000; $plugin->requires = 2022041900; $plugin->maturity = MATURITY_ALPHA; $plugin->release = 'v1.0';