moodle-report_usergrades/index.php

60 lines
2.3 KiB
PHP
Raw Normal View History

<?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');
require_once('classes/user_selector.php');
require_once('classes/usergrades_form.php');
// Set up the page context and other configurations
admin_externalpage_setup('report_usergrades', '', null, '', array('capability' => 'report/usergrades:view'));
// 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();