From 7a1e5e1fa1c888f2493d10525e369d464547f366 Mon Sep 17 00:00:00 2001
From: Jonathon Fowler <fowlerj@usq.edu.au>
Date: Tue, 26 Feb 2008 11:15:37 +1000
Subject: [PATCH] grade/report/grader/lib.php, lib/accesslib.php: solution for SQL comparison of integer and string fields when sorting by lastname
---
 grade/report/grader/lib.php |   33 ++++++++++++++++++++-------------
 lib/accesslib.php           |   10 ++++++++--
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php
index 1c7ece7..06fea7e 100644
--- a/grade/report/grader/lib.php
+++ b/grade/report/grader/lib.php
@@ -347,19 +347,26 @@ class grade_report_grader extends grade_report {
     function get_numusers($groups=true) {
         global $CFG;
 
-        $countsql = "SELECT COUNT(DISTINCT u.id)
-                    FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
-                         {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
-                         LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid ";
-        if ($groups) {
-            $countsql .= $this->groupsql;
-        }
-        $countsql .= " WHERE ra.roleid in ($this->gradebookroles) ";
-        if ($groups) {
-            $countsql .= $this->groupwheresql;
-        }
-        $countsql .= " AND ra.contextid ".get_related_contexts_string($this->context);
-        return count_records_sql($countsql);
+        if (is_numeric($this->sortitemid)) {
+		$countsql = "SELECT COUNT(DISTINCT u.id)
+			    FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
+				 {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
+				 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid ";
+		if ($groups) {
+		    $countsql .= $this->groupsql;
+		}
+		$countsql .= " WHERE ra.roleid in ($this->gradebookroles) ";
+		if ($groups) {
+		    $countsql .= $this->groupwheresql;
+		}
+		$countsql .= " AND ra.contextid ".get_related_contexts_string($this->context);
+        	$count = count_records_sql($countsql);
+	} else {
+		$roles = explode(',', $this->gradebookroles);
+		$count = count_role_users($roles, $this->context, false);
+	}
+
+	return $count;
     }
 
     /**
diff --git a/lib/accesslib.php b/lib/accesslib.php
index ad4f40e..5f6de15 100644
--- a/lib/accesslib.php
+++ b/lib/accesslib.php
@@ -5020,7 +5020,7 @@ function get_role_users($roleid, $context, $parent=false, $fields='', $sort='u.l
 
 /**
  * Counts all the users assigned this role in this context or higher
- * @param int roleid
+ * @param int roleid (can also be an array of ints!)
  * @param int contextid
  * @param bool parent if true, get list of users assigned in higher context too
  * @return array()
@@ -5038,12 +5038,18 @@ function count_role_users($roleid, $context, $parent=false) {
         $parentcontexts = '';
     }
 
+    if (is_array($roleid)) {
+        $roleselect = ' AND r.roleid IN (' . implode(',',$roleid) .')';
+    } else { // should not test for int, because it can come in as a string
+        $roleselect = "AND r.roleid = $roleid";
+    }
+    
     $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 $parentcontexts)
-        AND r.roleid = $roleid
+        $roleselect
         AND u.deleted = 0";
 
     return count_records_sql($SQL);
-- 
1.5.4.34.g053d9-dirty

