Skip to content

Commit

Permalink
Fix grade breakdown report
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusso committed Oct 3, 2023
1 parent 354b1b5 commit ce3d04a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 47 deletions.
59 changes: 35 additions & 24 deletions grade/report/grade_breakdown/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
$gradeid = optional_param('grade', null, PARAM_INT);
$groupid = optional_param('group', null, PARAM_INT);

$PAGE->set_url(new moodle_url('/grade/report/grade_breakdown/index.php', array(
'id' => $courseid, 'grade' => $gradeid, 'group' => $groupid
)));

if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
}
Expand All @@ -46,12 +42,12 @@
require_capability('gradereport/grade_breakdown:view', $context);

// Are they a teacher?
$is_teacher = has_capability('moodle/grade:viewall', $context);
$isteacher = has_capability('moodle/grade:viewall', $context);

// Graded roles
$gradedroles = explode(',', $CFG->gradebookroles);

$has_access = false;
$hasaccess = false;
// If the user is a "student" (graded role), and the teacher allowed them
// to view the report

Expand All @@ -61,42 +57,58 @@
$CFG->grade_report_grade_breakdown_allowstudents
);

if (!$is_teacher && $allowstudents) {
$user_roles = get_user_roles($context, $USER->id);
if (!$isteacher && $allowstudents) {
$userroles = get_user_roles($context, $USER->id);

foreach ($user_roles as $role) {
foreach ($userroles as $role) {
if (in_array($role->roleid, $gradedroles)) {
$has_access = true;
$hasaccess = true;
break;
}
}
}
// End permission

$_s = function($key, $a = null) {
$s = function($key, $a = null) {
return get_string($key, 'gradereport_grade_breakdown');
};

$gpr = new grade_plugin_return(array(
'type' => 'report', 'plugin' => 'grade_breakdown', 'courseid' => $courseid
));

$reportname = $s('pluginname');

$PAGE->set_context($context);
$url = new moodle_url('/grade/report/grade_breakdown/index.php', ['id' => $courseid]);
$PAGE->set_url($url);

if (!isset($USER->grade_last_report)) {
$USER->grade_last_report = array();
}
$USER->grade_last_report[$course->id] = 'grade_breakdown';

grade_regrade_final_grades($courseid);

$reportname = $_s('pluginname');

$PAGE->set_context($context);
grade_regrade_final_grades($course->id);

print_grade_page_head(
$course->id,
'report',
$active_plugin = 'grade_breakdown',
$heading = $reportname,
$return = false,
$buttons = false,
$shownavigation = true,
$headerhelpidentifier = null,
$headerhelpcomponent = null,
$user = null,
$actionbar = null,
$showtitle = true
);

print_grade_page_head($course->id, 'report', 'grade_breakdown', $reportname, false);

// The current user does not have access to view this report
if (!$has_access && !$is_teacher) {
echo $OUTPUT->heading($_s('teacher_disabled'));
if (!$hasaccess && !$isteacher) {
echo $OUTPUT->heading($s('teacher_disabled'));
echo $OUTPUT->footer();
exit;
}
Expand All @@ -118,13 +130,12 @@
'u.id', 'u.lastname, u.firstname', null, $groupid
);
}

$num_users = count($users);
$numusers = count($users);

// The student has access, but they still are unable to view it
// if there is 10 or less student enrollments in the class
if (!$is_teacher && $num_users <= 10) {
echo $OUTPUT->heading($_s('size_disabled'));
if (!$isteacher && $numusers <= 10) {
echo $OUTPUT->heading($s('size_disabled'));
echo $OUTPUT->footer();
exit;
}
Expand All @@ -137,7 +148,7 @@
$report->setup_groups();

echo '<div class="selectors">
'. ($is_teacher ? $report->group_selector : '') . $report->grade_selector . '
'. ($isteacher ? $report->group_selector : '') . $report->grade_selector . '
</div>';

$report->print_table();
Expand Down
71 changes: 48 additions & 23 deletions grade/report/grade_breakdown/letter_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
$bound = required_param('bound', PARAM_RAW);
$gradeid = required_param('grade', PARAM_INT);
$groupid = optional_param('group', 0, PARAM_INT);
$groupid = isset($groupid) ? $groupid : 0;

$parms = array(
'id' => $courseid,
'bound' => $bound,
'grade' => $gradeid,
'group' => $groupid
);

if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('nocourseid');
Expand All @@ -50,18 +58,28 @@
$strgrades = get_string('grades');
$reportname = get_string('pluginname', 'gradereport_grade_breakdown');

$PAGE->set_url(new moodle_url('/grade/report/grade_breakdown/letter_report.php',
array(
'id' => $courseid,
'bound' => $bound,
'grade' => $gradeid,
'group' => $groupid
)
));
//$url = new moodle_url('/grade/report/grade_breakdown/letter_report.php', $parms);
$url = new moodle_url('/grade/report/grade_breakdown/index.php', ['id' => $courseid]);

$PAGE->set_context($context);

print_grade_page_head($course->id, 'report', 'grade_breakdown', $reportname, false);
$PAGE->set_url($url);

$abar = new \core_grades\output\general_action_bar($PAGE->context, $PAGE->url, 'report', 'grade_breakdown');

print_grade_page_head(
$course->id,
'report',
$active_plugin = 'grade_breakdown',
$heading = $reportname,
$return = false,
$buttons = false,
$shownavigation = true,
$headerhelpidentifier = null,
$headerhelpcomponent = null,
$user = null,
$actionbar = $abar,
$showtitle = true
);

// This grade report has the functionality to print the right
// group selector
Expand Down Expand Up @@ -123,15 +141,19 @@
'courseid' => $courseid
));

$decimals = $grade_item->decimals;

$letters = grade_get_letters($context);

$high = 100;
$high = $CFG->unlimitedgrades = 1 ? 10000 : 100;

foreach($letters as $boundary => $letter) {
// Found it!
if ($boundary == $bound) {
break;
}
$high = $boundary - (1 / (pow(10, $decimals)));
$val = (1 / (pow(10, $decimals)));
$high = $boundary - $val;
}

// In the event that we're looking at the max, students actually have the
Expand All @@ -143,7 +165,7 @@

$query_params = array(
'courseid' => $courseid,
'gradeid' => $gradeid,
'gradeid' => $grade_item->id,
'real_high' => $real_high,
'real_low' => $real_low
);
Expand All @@ -163,9 +185,13 @@

// Get all the grades for the users within the range specified with $real_high and $real_low

$mainuserfields = user_picture::fields('u', array('id'), 'userid');
$userfields = \core_user\fields::for_name()->get_required_fields();

$mainuserfields = implode(',u.', $userfields);

$sql = "SELECT u.id, $mainuserfields, g.id AS gradeid, g.finalgrade
$highselect = $real_high >= 100 ? "" : "AND g.finalgrade <= $real_high";

$sql = "SELECT u.id, $mainuserfields, g.id AS gradesid, g.finalgrade
$group_name
FROM {grade_grades} g,
{user} u
Expand All @@ -174,7 +200,7 @@
$group_where
AND g.itemid = :gradeid
AND g.userid IN ({$userids})
AND g.finalgrade <= :real_high
$highselect
AND g.finalgrade >= :real_low
ORDER BY g.finalgrade DESC";

Expand All @@ -189,17 +215,16 @@

// Get the Moodle version of this grade item
$item_params = array('id' => $gradeid);
$grade_item = grade_item::fetch($item_params);
$gradeitem = grade_item::fetch($item_params);

$name = $grade_item->get_name();
$name = $gradeitem->get_name();

$g_params = array('id' => $groupid);
$groupname = ($groupid) ? ' in ' . $DB->get_field('groups', 'name', $g_params) : '';

echo $OUTPUT->heading(get_string('user_grades', 'gradereport_grade_breakdown') .
$letters[$bound] . ' for ' . $name . $groupname);


$numusers = count($graded_users);

// Build the data
Expand All @@ -214,15 +239,15 @@

$line[] = html_writer::link($url, fullname($gr));

$line[] = grade_format_gradevalue($gr->finalgrade, $grade_item, true,
$line[] = grade_format_gradevalue($gr->finalgrade, $gradeitem, true,
GRADE_DISPLAY_TYPE_REAL);

$line[] = grade_format_gradevalue($gr->finalgrade, $grade_item, true,
$line[] = grade_format_gradevalue($gr->finalgrade, $gradeitem, true,
GRADE_DISPLAY_TYPE_PERCENTAGE);

$line[] = find_rank($context, $grade_item, $gr, $groupid) . '/' . $numusers;
$line[] = find_rank($context, $gradeitem, $gr, $groupid) . '/' . $numusers;

$line[] = print_edit_link($courseid, $grade_item, $gr->gradeid);
$line[] = print_edit_link($courseid, $gradeitem, $gr->gradesid);
$data[] = $line;
}

Expand Down

0 comments on commit ce3d04a

Please sign in to comment.