2024-11-19 15:06:49 +00:00
|
|
|
<?php
|
|
|
|
require_once('../../config.php');
|
2024-11-20 14:10:57 +00:00
|
|
|
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');
|
2024-11-19 15:06:49 +00:00
|
|
|
|
|
|
|
// Set up the page context and other configurations
|
|
|
|
admin_externalpage_setup('report_usergrades');
|
|
|
|
|
2024-11-20 14:10:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 15:06:49 +00:00
|
|
|
/**
|
|
|
|
* Form class for user selection
|
|
|
|
*/
|
2024-11-20 14:10:57 +00:00
|
|
|
class usergrades_form extends moodleform
|
|
|
|
{
|
|
|
|
public function definition()
|
|
|
|
{
|
2024-11-19 15:06:49 +00:00
|
|
|
$mform = $this->_form;
|
|
|
|
|
|
|
|
// User selector
|
|
|
|
$user_selector = new user_selector('userid', array('multiselect' => false));
|
2024-11-20 14:10:57 +00:00
|
|
|
$users = $user_selector->find_users('');
|
|
|
|
|
|
|
|
$mform->addElement('select', 'userid', get_string('selectuser', 'report_usergrades'), $users[get_string('users')]);
|
2024-11-19 15:06:49 +00:00
|
|
|
|
|
|
|
// 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
|
2024-11-20 14:10:57 +00:00
|
|
|
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);
|
2024-11-19 15:06:49 +00:00
|
|
|
$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');
|
2024-11-20 14:10:57 +00:00
|
|
|
$table->define_baseurl($CFG->wwwroot . '/report/usergrades/index.php');
|
2024-11-19 15:06:49 +00:00
|
|
|
$table->define_columns(array('course', 'activity', 'grade'));
|
2024-11-20 14:10:57 +00:00
|
|
|
$table->define_headers(array(get_string('course'), get_string('activity'), get_string('grade', 'report_usergrades')));
|
2024-11-19 15:06:49 +00:00
|
|
|
|
|
|
|
$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();
|
2024-11-20 14:10:57 +00:00
|
|
|
$grade_display = $grade ? $grade->finalgrade . ' / ' . $grade->rawgrademax : '-';
|
2024-11-19 15:06:49 +00:00
|
|
|
|
|
|
|
$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();
|