commit 8362a726d51c1df48955ff45d14581b804135248 Author: trevor-c Date: Fri Jun 20 15:08:58 2014 -0600 fix rank calculation - no longer include grade items from users no longer enrolled in the course diff --git a/grade/report/overview/lib.php b/grade/report/overview/lib.php index 8c279f7..4812f41 100644 --- a/grade/report/overview/lib.php +++ b/grade/report/overview/lib.php @@ -181,7 +181,10 @@ class grade_report_overview extends grade_report { } else if ($this->showrank[$course->id] && !is_null($finalgrade)) { /// find the number of users with a higher grade /// please note this can not work if hidden grades involved :-( to be fixed in 2.0 - $params = array($finalgrade, $course_item->id); + + /* Correct issue with rank gathering grades from users who are no longer enrolled + //WRONG - includes grade history or unenrolled users*/ + /*$params = array($finalgrade, $course_item->id); $sql = "SELECT COUNT(DISTINCT(userid)) FROM {grade_grades} WHERE finalgrade IS NOT NULL AND finalgrade > ? @@ -189,7 +192,35 @@ class grade_report_overview extends grade_report { $rank = $DB->count_records_sql($sql, $params) + 1; $data[] = "$rank/$numusers"; - + */ + + $context = get_context_instance(CONTEXT_COURSE, $course->id); + + // This is the correct way find the number of users with a higher grade thet ARE STILL ENROLLED in the course. + $sql = "SELECT COUNT(DISTINCT(g.userid)) + FROM {grade_grades} g + INNER JOIN {role_assignments} r + ON r.userid = g.userid + WHERE finalgrade IS NOT NULL AND finalgrade > ? + AND g.itemid = ? + AND(r.contextid = ?) + AND r.roleid IN (?)"; + + $rank = $DB->count_records_sql($sql, array($finalgrade, $course_item->id, $context->id,$CFG->gradebookroles)) + 1; + + // Counts the sql for gradeable users in the course + $sql = "SELECT count(u.id) + FROM {role_assignments} r + JOIN {user} u + ON u.id = r.userid + WHERE (r.contextid = ?) + AND r.roleid IN (?) + AND u.deleted = 0"; + + $numusers = $DB->count_records_sql($sql, array($context->id,$CFG->gradebookroles)); + + $data[] = "$rank/$numusers"; // total course users (still enrolled) + } else { // No grade, no rank. // Or this course wants rank hidden.