commit bffef852e4dc4408edf6a5fcb9387c7084cc0bd6 Author: trevor-c Date: Fri Jun 20 15:03:00 2014 -0600 fix rank calculation - no longer include grade items from users no longer enrolled in the course diff --git a/grade/report/user/lib.php b/grade/report/user/lib.php index c215827..d5df09a 100644 --- a/grade/report/user/lib.php +++ b/grade/report/user/lib.php @@ -497,6 +497,9 @@ class grade_report_user extends grade_report { } else { /// find the number of users with a higher grade + + /* Correct issue with rank gathering grades from users who are no longer enrolled + //WRONG - includes grade history or unenrolled users $sql = "SELECT COUNT(DISTINCT(userid)) FROM {grade_grades} WHERE finalgrade > ? @@ -506,6 +509,38 @@ class grade_report_user extends grade_report { $data['rank']['class'] = $class; $data['rank']['content'] = "$rank/".$this->get_numusers(false); // total course users + */ + + /* CORRECTED */ + // Grab the context of the course in question + $context = get_context_instance(CONTEXT_COURSE, $this->courseid); + + // 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($grade_grade->finalgrade, $grade_grade->grade_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']['class'] = $class; + $data['rank']['content'] = "$rank/$numusers"; // total course users (still enrolled) + } $data['rank']['headers'] = "$header_cat $header_row rank"; }