--- lib.php 2008-11-18 14:14:46.000000000 -0600
+++ lib.php 2008-11-18 14:31:14.000000000 -0600
@@ -114,7 +114,9 @@

         // MDL-11679, only show 'mycourses' instead of all courses
         if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname')) {
-            $numusers = $this->get_numusers(false);
+            // $numusers is only for THIS course, thus will fail if the number of gradable roles
+            // are different in other courses.
+            // $numusers = $this->get_numusers(false);

             foreach ($courses as $course) {
                 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
@@ -139,12 +141,32 @@
                     //nothing to do

                 } else if (!is_null($finalgrade)) {
+
                     /// find the number of users with a higher grade
-                    $sql = "SELECT COUNT(DISTINCT(userid))
-                              FROM {$CFG->prefix}grade_grades
-                             WHERE finalgrade IS NOT NULL AND finalgrade > $finalgrade
-                                   AND itemid = {$grade_item->id}";
-                    $rank = count_records_sql($sql) + 1;
+
+                    // Grab the context of the course in question
+                    $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 {$CFG->prefix}grade_grades g
+                                INNER JOIN {$CFG->prefix}role_assignments r
+                                  ON r.userid = g.userid
+                             WHERE finalgrade IS NOT NULL AND finalgrade > $grade->finalgrade
+                                AND itemid = {$grade_item->id}
+                                AND(r.contextid = $context->id)
+                                AND r.roleid IN ({$CFG->gradebookroles})";
+
+                    // Counts the sql for gradeable users in the course
+                    $sql = "SELECT count(u.id)
+                              FROM {$CFG->prefix}role_assignments r
+                                  JOIN {$CFG->prefix}user u
+                                  ON u.id = r.userid
+                              WHERE (r.contextid = $context->id)
+                                  AND r.roleid IN ({$CFG->gradebookroles})
+                                  AND u.deleted = 0";
+
+                    $numusers = count_records_sql($sql);

                     $data[] = "$rank/$numusers";

