]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13478 Fixing a botched merge
authormoodler <moodler>
Mon, 3 Mar 2008 04:42:01 +0000 (04:42 +0000)
committermoodler <moodler>
Mon, 3 Mar 2008 04:42:01 +0000 (04:42 +0000)
mod/choice/lib.php
mod/choice/report.php
mod/choice/view.php

index c1e44cb5e02c51c05b6f047aa76d7698bc9e2e4d..26e66cf50eeb988391b562ef7e3163deb60ad5cc 100644 (file)
@@ -122,7 +122,7 @@ function choice_update_instance($choice) {
 
 }
 
-function choice_show_form($choice, $user, $cm) {
+function choice_show_form($choice, $user, $cm, $allresponses) {
 
 //$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text  - text name of option.
 //                                                                            ->maxanswers -maxanswers for this option
@@ -131,45 +131,33 @@ function choice_show_form($choice, $user, $cm) {
 
     $aid = 0;
     $choicefull = false;
+    $cdisplay = array();
 
     if ($choice->limitanswers) { //set choicefull to true by default if limitanswers.
         $choicefull = true;
     }
+
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
     foreach ($choice->option as $optionid => $text) {
-        if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
-            $countanswers = (get_records("choice_answers", "optionid", $optionid));
-            $countans = 0;
-            if (!empty($countanswers)) {
-                foreach ($countanswers as $ca) { //only return enrolled users.
-                    if (has_capability('mod/choice:choose', $context, $ca->userid, false)) {
-                        $countans = $countans+1;
-                    }
-                }
-            }
-            if ($countanswers) {
-                $countanswers = count($countanswers);
-            } else {
-                $countanswers = 0;
-            }
-            $maxans = $choice->maxanswers[$optionid];
-
+        if (isset($text) && isset($allresponses[$optionid])) { //make sure there are no dud entries in the db with blank text values.
             $cdisplay[$aid]->optionid = $optionid;
             $cdisplay[$aid]->text = $text;
-            $cdisplay[$aid]->maxanswers = $maxans;
-            $cdisplay[$aid]->countanswers = $countans;
+            $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid];
+            $cdisplay[$aid]->countanswers = count($allresponses[$optionid]);
 
             if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) {
                 $cdisplay[$aid]->checked = ' checked="checked" ';
             } else {
                 $cdisplay[$aid]->checked = '';
             }
-            if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) {
+            if ( $choice->limitanswers && 
+                ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) && 
+                (empty($cdisplay[$aid]->checked)) ) {
                 $cdisplay[$aid]->disabled = ' disabled="disabled" ';
             } else {
                 $cdisplay[$aid]->disabled = '';
-                if ($choice->limitanswers && ($countans < $maxans)) {
+                if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) {
                     $choicefull = false; //set $choicefull to false - as the above condition hasn't been set.
                 }
             }
@@ -294,24 +282,12 @@ function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $
     }
 }
 
-
-function choice_show_reportlink($choice, $courseid, $cm, $groupmode) {
-
-    if ($groupmode > 0) {
-        $currentgroup = groups_get_activity_group($cm);
-    } else {
-        $currentgroup = 0;
-    }
-
-    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-    $availableusers = get_users_by_capability($context, 'mod/choice:choose', 'u.id', '','','',$currentgroup, '', true, 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 );");
+function choice_show_reportlink($user, $cm) {
+    $responsecount =0;
+    foreach($user as $optionid => $userlist) {
+        if ($optionid) {
+            $responsecount += count($userlist);
+        }
     }
 
     echo '<div class="reportlink">';
@@ -319,159 +295,106 @@ function choice_show_reportlink($choice, $courseid, $cm, $groupmode) {
     echo '</div>';
 }
 
-function choice_show_results($choice, $course, $cm, $forcepublish='') {
-
-    global $CFG, $COLUMN_HEIGHT, $USER;
-    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
+function choice_show_results($choice, $course, $cm, $users, $forcepublish='') {
+    global $CFG, $COLUMN_HEIGHT;
+    
     print_heading(get_string("responses", "choice"));
-
     if (empty($forcepublish)) { //alow the publish setting to be overridden
         $forcepublish = $choice->publish;
     }
 
-    $groupmode = groups_get_activity_groupmode($cm);
-
-    if ($groupmode > 0) {
-        $currentgroup = groups_get_activity_group($cm);
-    } else {
-        $currentgroup = 0;
-    }
-
-    $allresponses = get_records("choice_answers", "choiceid", $choice->id); //get all responses for this choice.
-    
-    if ($choice->showunanswered) {
-        $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', true, true);
-        $allusersnothidden = get_users_by_capability($context, 'mod/choice:choose', 'u.id', 'u.firstname ASC', '', '', $currentgroup, '', false, false); ///MDL-12331 ugly hack to prevent hidden users and admins from being displayed in the unanswered column. needs fixing!
-    } else { //as we are not showing unanswered column, we only need details on users who have submitted a choice - this should perform much better than the 2 get_users_by_capability calls.
-        foreach ($allresponses as $usr) {
-            if (has_capability('mod/choice:choose', $context, $usr->userid, false)) { //if this user is allowed to select a choice.
-                $users[$usr->userid] = get_record_sql('SELECT id, picture, firstname, lastname, idnumber FROM '. $CFG->prefix .'user WHERE id= '.$usr->userid);
-            }
-        }
-    }
-    if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) {
-        $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id');
-        foreach($users as $key => $user) {
-            if (!isset($groupingusers[$user->id])) {
-                unset($users[$key]);
-            }
-        }
-    }
-
     if (!$users) {
         print_heading(get_string("nousersyet"));
     }
+    
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-    $answers = array () ;
-    if (!empty($allresponses)) {
-        foreach ($allresponses as $aa) {
-            //TODO: rewrite with SQL
-            if ($groupmode and $currentgroup) {
-                if (groups_is_member($currentgroup, $aa->userid)) {
-                    $answers[$aa->userid] = $aa;
-                }
-            } else {
-                $answers[$aa->userid] = $aa;
-            }
-        }
-    }
-
-    $timenow = time();
-
-    foreach ($choice->option as $optionid => $text) {
-        $useranswer[$optionid] = array();
-    }
-    if (!empty($users)) {
-        foreach ($users as $user) {
-            if (!empty($user->id) and !empty($answers[$user->id])) {
-                $answer = $answers[$user->id];
-                $useranswer[(int)$answer->optionid][] = $user;
-            } else {
-                $useranswer[0][] = $user;
-            }
-        }
-    }
-    foreach ($choice->option as $optionid => $text) {
-        if (!$choice->option[$optionid]) {
-            unset($useranswer[$optionid]);     // Throw away any data that doesn't apply
-        }
-    }
-    ksort($useranswer);
+    $hascapfullnames = has_capability('moodle/site:viewfullnames', $context);
+    
+    $viewresponses = has_capability('mod/choice:readresponses', $context); 
     switch ($forcepublish) {
         case CHOICE_PUBLISH_NAMES:
-
-            $tablewidth = (int) (100.0 / count($useranswer));
-            if (has_capability('mod/choice:readresponses', $context)) {
                 echo '<div id="tablecontainer">';
                 echo '<form id="attemptsform" method="post" action="'.$_SERVER['PHP_SELF'].'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
                 echo '<div>';
                 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
                 echo '<input type="hidden" name="mode" value="overview" />';
-            }
 
             echo "<table cellpadding=\"5\" cellspacing=\"10\" class=\"results names\">";
             echo "<tr>";
-            $count = 0;
+  
             $columncount = array(); // number of votes in each column
-            foreach ($useranswer as $optionid => $userlist) {
+            if ($choice->showunanswered) {
+                $columncount[0] = 0;
+                echo "<th class=\"col0 header\" scope=\"col\">";
+                print_string('notanswered', 'choice');
+                echo "</th>";
+            }
+            $count = 1;
+            foreach ($choice->option as $optionid => $optiontext) {
                 $columncount[$optionid] = 0; // init counters
-                if ($optionid) {
-                    echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
-                } else if ($choice->showunanswered) {
-                    echo "<th class=\"col$count header\" style=\"width:$tablewidth%\" scope=\"col\">";
-                } else {
-                    continue;
-                }
-                echo format_string(choice_get_option_text($choice, $optionid));
+                echo "<th class=\"col$count header\" scope=\"col\">";
+                echo format_string($optiontext);
                 echo "</th>";
                 $count++;
             }
             echo "</tr><tr>";
 
-            $count = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if ($optionid) {
-                    echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
-                } else if ($choice->showunanswered) {
-                    echo "<td class=\"col$count data\" style=\"width:$tablewidth%;\">";
-                } else {
-                    continue;
-                }
-
+            if ($choice->showunanswered) {
+                echo "<td class=\"col$count data\" >";
                 // added empty row so that when the next iteration is empty,
                 // we do not get <table></table> erro from w3c validator
                 // MDL-7861
                 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
-                foreach ($userlist as $user) {
-                    if (!($optionid==0 AND empty($allusersnothidden[$user->id]))) { //don't show admins or hidden users
-                        $columncount[$optionid] += 1;
-                        echo "<tr>";
-                        if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) {
-                            echo '<td class="attemptcell"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" /></td>';
-                        }
-                        echo "<td class=\"picture\">";
-                        print_user_picture($user->id, $course->id, $user->picture);
-                        echo "</td><td class=\"fullname\">";
-                        echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
-                        echo fullname($user, has_capability('moodle/site:viewfullnames', $context));
-                        echo "</a>";
-                        echo "</td></tr>";
-                    }
+                foreach ($users[0] as $user) {
+                    echo "<tr>";
+                    echo "<td class=\"picture\">";
+                    print_user_picture($user->id, $course->id, $user->picture);
+                    echo "</td><td class=\"fullname\">";
+                    echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
+                    echo fullname($user, $hascapfullnames);
+                    echo "</a>";
+                    echo "</td></tr>";
                 }
-                $count++;
-                echo "</table>";
-
-                echo "</td>";
+                echo "</table></td>";
+            }
+            $count = 0;
+            foreach ($choice->option as $optionid => $optiontext) {
+                    echo '<td class="col'.$count.' data" >';
+
+                    // added empty row so that when the next iteration is empty,
+                    // we do not get <table></table> erro from w3c validator
+                    // MDL-7861
+                    echo '<table class="choiceresponse"><tr><td></td></tr>';
+                    if (isset($users[$optionid])) {
+                        foreach ($users[$optionid] as $user) {
+                            $columncount[$optionid] += 1;
+                            echo '<tr><td class="attemptcell">';
+                            if ($viewresponses) {
+                                echo '<input type="checkbox" name="attemptid[]" value="'. $user->id. '" />';
+                            }
+                            echo '</td><td class="picture">';
+                            print_user_picture($user->id, $course->id, $user->picture);
+                            echo '</td><td class="fullname">';
+                            echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
+                            echo fullname($user, $hascapfullnames);
+                            echo '</a>';
+                            echo '</td></tr>';
+                       }
+                    }
+                    $count++;
+                    echo '</table></td>';
             }
             echo "</tr><tr>";
             $count = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if (!$optionid and !$choice->showunanswered) {
-                    continue;
-                }
+            
+            if ($choice->showunanswered) {
+                echo "<td></td>";
+            }
+            
+            foreach ($choice->option as $optionid => $optiontext) {
                 echo "<td align=\"center\" class=\"count\">";
-                if ($choice->limitanswers && !$optionid==0) {
+                if ($choice->limitanswers) {
                     echo get_string("taken", "choice").":";
                     echo $columncount[$optionid];
                     echo "<br/>";
@@ -479,15 +402,17 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') {
                     $choice_option = get_record("choice_options", "id", $optionid);
                     echo $choice_option->maxanswers;
                 } else {
-                    echo $columncount[$optionid];
+                    if (isset($columncount[$optionid])) {
+                        echo $columncount[$optionid];
+                    }
                 }
                 echo "</td>";
                 $count++;
             }
             echo "</tr>";
-
+            
             /// Print "Select all" etc.
-            if (has_capability('mod/choice:readresponses', $context)) {
+            if ($viewresponses) {
                 echo '<tr><td></td><td>';
                 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
                 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
@@ -500,58 +425,62 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') {
                 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
                 echo '</td><td></td></tr>';
             }
-
+            
             echo "</table>";
-            if (has_capability('mod/choice:readresponses', $context)) {
+            if ($viewresponses) {
                 echo "</div></form></div>";
             }
             break;
-
-
+        
+        
         case CHOICE_PUBLISH_ANONYMOUS:
 
-            $tablewidth = (int) (100.0 / count($useranswer));
-
             echo "<table cellpadding=\"5\" cellspacing=\"0\" class=\"results anonymous\">";
             echo "<tr>";
-            $count = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if ($optionid) {
-                    echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
-                } else if ($choice->showunanswered) {
-                    echo "<th style=\"width:$tablewidth%\" class=\"col$count header\" scope=\"col\">";
-                } else {
-                    continue;
-                }
-                echo format_string(choice_get_option_text($choice, $optionid));
+            $maxcolumn = 0;
+            if ($choice->showunanswered) {
+                echo "<th  class=\"col0 header\" scope=\"col\">";
+                print_string('notanswered', 'choice');
                 echo "</th>";
-                $count++;
+                $column[0] = 0;
+                foreach ($users[0] as $user) {
+                    $column[0]++;
+                }
+                $maxcolumn = $column[0];
             }
-            echo "</tr>";
+            $count = 1;
 
-            $maxcolumn = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if (!$optionid and !$choice->showunanswered) {
-                    continue;
-                }
+            foreach ($choice->option as $optionid => $optiontext) {
+                echo "<th class=\"col$count header\" scope=\"col\">";
+                echo format_string($optiontext);
+                echo "</th>";
+                
                 $column[$optionid] = 0;
-                foreach ($userlist as $user) {
-                    if (!($optionid==0 AND empty($allusersnothidden[$user->id]))) { //don't show admins or hidden users
+                if (isset($users[$optionid])) {
+                    foreach ($users[$optionid] as $user) {
                         $column[$optionid]++;
                     }
-                }
-                if ($column[$optionid] > $maxcolumn) {
-                    $maxcolumn = $column[$optionid];
+                    if ($column[$optionid] > $maxcolumn) {
+                        $maxcolumn = $column[$optionid];
+                    }
+                } else {
+                    $column[$optionid] = 0;
                 }
             }
+            echo "</tr><tr>";
 
-            echo "<tr>";
-            $count = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if (!$optionid and !$choice->showunanswered) {
-                    continue;
+            $height = 0;
+
+            if ($choice->showunanswered) {
+                if ($maxcolumn) {
+                    $height = $COLUMN_HEIGHT * ((float)$column[0] / (float)$maxcolumn);
                 }
-                $height = 0;
+                echo "<td style=\"vertical-align:bottom\" align=\"center\" class=\"col0 data\">";
+                echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
+                echo "</td>";
+            }
+            $count = 1;
+            foreach ($choice->option as $optionid => $optiontext) {
                 if ($maxcolumn) {
                     $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
                 }
@@ -560,16 +489,16 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') {
                 echo "</td>";
                 $count++;
             }
-            echo "</tr>";
+            echo "</tr><tr>";
 
-            echo "<tr>";
-            $count = 0;
-            foreach ($useranswer as $optionid => $userlist) {
-                if (!$optionid and !$choice->showunanswered) {
-                    continue;
-                }
+
+            if ($choice->showunanswered) {
+                echo "<td></td>";
+            }
+            $count = 1;
+            foreach ($choice->option as $optionid => $optiontext) {
                 echo "<td align=\"center\" class=\"col$count count\">";
-                if ($choice->limitanswers && !$optionid==0) {
+                if ($choice->limitanswers) {
                     echo get_string("taken", "choice").":";
                     echo $column[$optionid];
                     echo "<br/>";
@@ -583,13 +512,13 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') {
                 $count++;
             }
             echo "</tr></table>";
-
-        break;
+            
+            break;
     }
 }
 
 
-function choice_delete_responses($attemptids) {
+function choice_delete_responses($attemptids, $choiceid) {
 
     if(!is_array($attemptids) || empty($attemptids)) {
         return false;
@@ -602,8 +531,8 @@ function choice_delete_responses($attemptids) {
     }
 
     foreach($attemptids as $attemptid) {
-        if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
-             delete_records('choice_answers', 'id', $attemptid);
+        if ($todelete = get_record('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid)) {
+            delete_records('choice_answers', 'choiceid', $choiceid, 'userid', $attemptid);
         }
     }
     return true;
@@ -734,4 +663,41 @@ function choice_reset_userdata($data) {
     return $status;
 }
 
+function choice_get_response_data($choice, $cm, $groupmode) {
+    global $CFG, $USER;
+
+    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+
+/// Get the current group
+    if ($groupmode > 0) {
+        $currentgroup = groups_get_activity_group($cm);
+    } else {
+        $currentgroup = 0;
+    }
+
+/// Initialise the returned array, which is a matrix:  $users[responseid][userid] = responseobject
+    $users = array();
+
+/// First get all the users who have access here
+/// To start with we assume they are all "unanswered" then move them later
+    $users[0] = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true);
+
+/// Get all the recorded responses for this choice
+    $allresponses = get_records('choice_answers', 'choiceid', $choice->id);
+
+/// Use the responses to move users into the correct column
+
+    if ($allresponses) {
+        foreach ($allresponses as $response) {
+            if (isset($users[0][$response->userid])) {   // This person is enrolled and in correct group
+                $users[0][$response->userid]->timemodified = $response->timemodified;
+                $users[$response->optionid][$response->userid] = clone($users[0][$response->userid]);
+                unset($users[0][$response->userid]);   // Remove from unanswered column
+            }
+        }
+    }
+
+    return $users;
+
+}
 ?>
index 842ff43fd0365f1e470823f620ddaa932b296410..6e0730f2c78bb23d6f76cdaa4d4651a814a71055 100644 (file)
       
     if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
         $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
-        choice_delete_responses($attemptids); //delete responses.
-        redirect("report.php?id=$cm->id");                      
+        choice_delete_responses($attemptids, $choice->id); //delete responses.
+        redirect("report.php?id=$cm->id");
     }
         
     if (!$download) {
 
-        $navigation = build_navigation($strresponses, $cm);    
+        $navigation = build_navigation($strresponses, $cm);
         print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true,
                   update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
         /// Check to see if groups are being used in this choice
         groups_print_activity_menu($cm, 'report.php?id='.$id);
     } else {
         $groupmode = groups_get_activity_groupmode($cm);
-        groups_get_activity_group($cm, true);
-    }
-
-    $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
-    
-    if (!empty($CFG->enablegroupings) && !empty($cm->groupingid) && !empty($users)) {
-        $groupingusers = groups_get_grouping_members($cm->groupingid, 'u.id', 'u.id');
-        foreach($users as $key => $user) {
-            if (!isset($groupingusers[$user->id])) {
-                unset($users[$key]);
-            }
-        }
-    }
-    
-    if (!$users) {
-        print_heading(get_string("nousersyet"));        
-    }
-
-    if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
-        foreach ($allresponses as $aa) {
-            $answers[$aa->userid] = $aa;
-        }
-    } else {
-        $answers = array () ;
     }
-
-    $timenow = time();
-
-    foreach ($choice->option as $optionid => $text) {
-        $useranswer[$optionid] = array();
-    }
-    foreach ($users as $user) {
-        if (!empty($user->id) and !empty($answers[$user->id])) {
-            $answer = $answers[$user->id];
-            $useranswer[(int)$answer->optionid][] = $user;
-        } else {
-            $useranswer[0][] = $user;
-        }
-    }
-    foreach ($choice->option as $optionid => $text) {
-        if (!$choice->option[$optionid]) {
-            unset($useranswer[$optionid]);     // Throw away any data that doesn't apply
-        }
-    }
-    ksort($useranswer);
-
+    $users = choice_get_response_data($choice, $cm, $groupmode);
 
     if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
         require_once("$CFG->libdir/odslib.class.php");
         $myxls->write_string(0,2,get_string("idnumber"));
         $myxls->write_string(0,3,get_string("group"));
         $myxls->write_string(0,4,get_string("choice","choice"));
-        
-              
+
     /// generate the data for the body of the spreadsheet
         $i=0;  
         $row=1;
         if ($users) {
-            foreach ($users as $user) {        
-                // this needs fixing
-                
-                if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
-                
-                    if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
-
-                        $myxls->write_string($row,0,$user->lastname);
-                        $myxls->write_string($row,1,$user->firstname);
-                        $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
-                        $myxls->write_string($row,2,$studentid);
-                        $ug2 = '';
-                        if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
-                            foreach ($usergrps as $ug) {
-                                $ug2 = $ug2. $ug->name;
-                            }
+            foreach ($users as $option => $userid) {
+                $option_text = choice_get_option_text($choice, $option);
+                foreach($userid as $user) {
+                    $myxls->write_string($row,0,$user->lastname);
+                    $myxls->write_string($row,1,$user->firstname);
+                    $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
+                    $myxls->write_string($row,2,$studentid);
+                    $ug2 = '';
+                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
+                        foreach ($usergrps as $ug) {
+                            $ug2 = $ug2. $ug->name;
                         }
-                        $myxls->write_string($row,3,$ug2);
-                    
-                        $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
-                        if (isset($useroption)) {
-                            $myxls->write_string($row,4,format_string($useroption,true));
-                        }                 
-                        $row++;
                     }
+                    $myxls->write_string($row,3,$ug2);
+
+                    if (isset($option_text)) {
+                        $myxls->write_string($row,4,format_string($useroption,true));
+                    }
+                    $row++;
                     $pos=4;
                 }
             }
+        }
+        /// Close the workbook
+        $workbook->close();
 
-    /// Close the workbook
-            $workbook->close();
-
-            exit;
-        } 
+        exit;
     }
 
-
     //print spreadsheet if one is asked for:
     if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
         require_once("$CFG->libdir/excellib.class.php");
         $i=0;  
         $row=1;
         if ($users) {
-            foreach ($users as $user) {        
-                // this needs fixing
-                
-                if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
-                
-                    if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
-
-                        $myxls->write_string($row,0,$user->lastname);
-                        $myxls->write_string($row,1,$user->firstname);
-                        $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
-                        $myxls->write_string($row,2,$studentid);
-                        $ug2 = '';
-                        if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
-                            foreach ($usergrps as $ug) {
-                                $ug2 = $ug2. $ug->name;
-                            }
+            foreach ($users as $option => $userid) {
+                $option_text = choice_get_option_text($choice, $option);
+                foreach($userid as $user) {
+                    $myxls->write_string($row,0,$user->lastname);
+                    $myxls->write_string($row,1,$user->firstname);
+                    $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
+                    $myxls->write_string($row,2,$studentid);
+                    $ug2 = '';
+                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
+                        foreach ($usergrps as $ug) {
+                            $ug2 = $ug2. $ug->name;
                         }
-                        $myxls->write_string($row,3,$ug2);
-                    
-                        $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
-                        if (isset($useroption)) {
-                            $myxls->write_string($row,4,format_string($useroption,true));
-                        }                 
-                        $row++;
                     }
-                    $pos=4;
+                    $myxls->write_string($row,3,$ug2);
+                    if (isset($option_text)) {
+                        $myxls->write_string($row,4,format_string($option_text,true));
+                    }
+                    $row++;
                 }
             }
-
-    /// Close the workbook
-            $workbook->close();
-
-            exit;
-        } 
+            $pos=4;
+        }
+        /// Close the workbook
+        $workbook->close();
+        exit;
     }
+
     // print text file  
     if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
 
         /// generate the data for the body of the spreadsheet
         $i=0;  
-        $row=1;
-        if ($users) foreach ($users as $user) {
-            if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
-
-                echo $user->lastname;
-                echo "\t".$user->firstname;
-                $studentid = " ";
-                if (!empty($user->idnumber)) {
-                    $studentid = $user->idnumber;
-                }              
-                echo "\t". $studentid."\t";
-                $ug2 = '';
-                if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
-                    foreach ($usergrps as $ug) {
-                        $ug2 = $ug2. $ug->name;
+        if ($users) {
+            foreach ($users as $option => $userid) {
+                $option_text = choice_get_option_text($choice, $option);
+                foreach($userid as $user) {
+                    echo $user->lastname;
+                    echo "\t".$user->firstname;
+                    $studentid = " ";
+                    if (!empty($user->idnumber)) {
+                        $studentid = $user->idnumber;
+                    }
+                    echo "\t". $studentid."\t";
+                    $ug2 = '';
+                    if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
+                        foreach ($usergrps as $ug) {
+                            $ug2 = $ug2. $ug->name;
+                        }
                     }
+                    echo $ug2. "\t";
+                    if (isset($option_text)) {
+                        echo format_string($option_text,true);
+                    }
+                    echo "\n";
                 }
-                echo $ug2. "\t";
-                echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
             }
-            $row++;
-        }      
+        }
         exit;
     }
+    choice_show_results($choice, $course, $cm, $users, $format); //show table with students responses.
 
-    choice_show_results($choice, $course, $cm, $format); //show table with students responses.
-   
    //now give links for downloading spreadsheets. 
     echo "<br />\n";
     echo "<table class=\"downloadreport\"><tr>\n";
index 383dd6cf4c62ed1c2165fca9c942cc5992fd0acc..b82864d4df9b3e4a31c8250182ab43e116ef8b56 100644 (file)
 
     /// Check to see if groups are being used in this choice
     $groupmode = groups_get_activity_groupmode($cm);
-    groups_get_activity_group($cm, true);
-    groups_print_activity_menu($cm, 'view.php?id='.$id);
-                                   
+    
+    if ($groupmode) {
+        groups_get_activity_group($cm, true);
+        groups_print_activity_menu($cm, 'view.php?id='.$id);
+    }
+    $allresponses = choice_get_response_data($choice, $cm, $groupmode);   // Big function, approx 6 SQL calls per user
+
+    
     if (has_capability('mod/choice:readresponses', $context)) {
-        choice_show_reportlink($choice, $course->id, $cm, $groupmode);
+        choice_show_reportlink($allresponses, $cm);
     }
 
     echo '<div class="clearer"></div>';
 
         echo '<form id="form" method="post" action="view.php">';        
 
-        choice_show_form($choice, $USER, $cm);
-        
+        choice_show_form($choice, $USER, $cm, $allresponses);
+
         echo '</form>';
 
         $choiceformshown = true;
         ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current ) or
         ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and $choice->timeclose <= time() ) )  {
 
-        choice_show_results($choice, $course, $cm);
+        choice_show_results($choice, $course, $cm, $allresponses); //show table with students responses.
 
     } else if (!$choiceformshown) {
         print_simple_box(get_string('noresultsviewable', 'choice'), 'center');