Index: lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/lib.php,v
retrieving revision 1.59.2.7
diff -u -r1.59.2.7 lib.php
--- lib.php	11 Dec 2007 18:49:44 -0000	1.59.2.7
+++ lib.php	9 Jan 2008 14:26:41 -0000
@@ -283,28 +283,29 @@
 }
 
 
-function choice_show_reportlink($choice, $courseid, $cmid, $groupmode) {
-    //TODO: rewrite with SQL
-    $currentgroup = get_current_group($courseid);
-    if ($allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
-        $responsecount = 0;
-        foreach ($allanswers as $aa) {
-            $context = get_context_instance(CONTEXT_MODULE, $cmid);
-            if (has_capability('mod/choice:choose', $context, $aa->userid, false)) { //check to make sure user is enrolled/has this capability.
-                if ($groupmode and $currentgroup) {
-                    if (groups_is_member($currentgroup, $aa->userid)) {
-                        $responsecount++;
-                    }
-                } else {
-                    $responsecount++;
-                }
-            }
-        }
+function choice_show_reportlink($choice, $courseid, $cm, $groupmode) {
+
+    $groupmode = groups_get_activity_groupmode($cm);
+
+    if ($groupmode > 0) {
+        $currentgroup = groups_get_activity_group($cm);
     } else {
-        $responsecount = 0;
+        $currentgroup = 0;
+    }
+
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+    $availableusers = get_users_by_capability($context, 'mod/choice:choose', 'u.id', '','','',$currentgroup, '', false, true);
+
+    $responsecount = 0;
+
+    if(!empty($availableusers)){
+        // flatten the users to get a list of userids
+        $userids = implode( ', ', array_keys($availableusers));
+        $responsecount = count_records_select('choice_answers', "choiceid = {$choice->id} AND userid IN ( $userids );");
     }
+
     echo '<div class="reportlink">';
-    echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
+    echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
     echo '</div>';
 }
 
Index: view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/view.php,v
retrieving revision 1.102.2.2
diff -u -r1.102.2.2 view.php
--- view.php	22 Oct 2007 21:26:16 -0000	1.102.2.2
+++ view.php	9 Jan 2008 14:26:41 -0000
@@ -70,7 +70,7 @@
     groups_print_activity_menu($cm, 'view.php?id='.$id);
                                    
     if (has_capability('mod/choice:readresponses', $context)) {
-        choice_show_reportlink($choice, $course->id, $cm->id, $groupmode);
+        choice_show_reportlink($choice, $course->id, $cm, $groupmode);
     }
 
     echo '<div class="clearer"></div>';
