diff -ruN /home/dan/moodle/moodle/mod/choice/lib.php ./lib.php --- /home/dan/moodle/moodle/mod/choice/lib.php 2008-01-09 14:44:24.000000000 +0000 +++ ./lib.php 2008-01-09 20:31:08.000000000 +0000 @@ -319,67 +319,18 @@ } $groupmode = groups_get_activity_groupmode($cm); + $allanswers = choice_get_response_data($choice, $cm, $groupmode); - if ($groupmode > 0) { - $currentgroup = groups_get_activity_group($cm); - } else { - $currentgroup = 0; - } - - $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true); - - 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) { + if($allanswers === false){ print_heading(get_string("nousersyet")); + return; } - $answers = array () ; - if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) { - 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); + ksort($allanswers); switch ($forcepublish) { case CHOICE_PUBLISH_NAMES: - $tablewidth = (int) (100.0 / count($useranswer)); + $tablewidth = (int) (100.0 / count($allanswers)); if (has_capability('mod/choice:readresponses', $context)) { echo '
'; echo '
'; @@ -392,7 +343,7 @@ echo ""; $count = 0; $columncount = array(); // number of votes in each column - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $answerlist) { $columncount[$optionid] = 0; // init counters if ($optionid) { echo ""; @@ -408,7 +359,7 @@ echo ""; $count = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if ($optionid) { echo ""; } else if ($choice->showunanswered) { @@ -421,18 +372,18 @@ // we do not get
erro from w3c validator // MDL-7861 echo ""; - foreach ($userlist as $user) { - if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) { + foreach ($optionanswers as $ans) { + if ($optionid!=0 or has_capability('mod/choice:choose', $context, $ans->user->id, false)) { $columncount[$optionid] += 1; echo ""; if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) { - echo ''; + echo ''; } echo ""; } @@ -444,7 +395,7 @@ } echo ""; $count = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if (!$optionid and !$choice->showunanswered) { continue; } @@ -488,12 +439,12 @@ case CHOICE_PUBLISH_ANONYMOUS: - $tablewidth = (int) (100.0 / count($useranswer)); + $tablewidth = (int) (100.0 / count($allanswers)); echo "
"; - print_user_picture($user->id, $course->id, $user->picture); + print_user_picture($ans->user->id, $course->id, $ans->user->picture); echo ""; - echo "wwwroot/user/view.php?id=$user->id&course=$course->id\">"; - echo fullname($user, has_capability('moodle/site:viewfullnames', $context)); + echo "wwwroot/user/view.php?id={$ans->user->id}&course=$course->id\">"; + echo fullname($ans->user, has_capability('moodle/site:viewfullnames', $context)); echo ""; echo "
"; echo ""; $count = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if ($optionid) { echo ""; $maxcolumn = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if (!$optionid and !$choice->showunanswered) { continue; } $column[$optionid] = 0; - foreach ($userlist as $user) { - if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) { + foreach ($optionanswers as $ans) { + if ($optionid!=0 or has_capability('mod/choice:choose', $context, $ans->user->id, false)) { $column[$optionid]++; } } @@ -525,7 +476,7 @@ echo ""; $count = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if (!$optionid and !$choice->showunanswered) { continue; } @@ -542,7 +493,7 @@ echo ""; $count = 0; - foreach ($useranswer as $optionid => $userlist) { + foreach ($allanswers as $optionid => $optionanswers) { if (!$optionid and !$choice->showunanswered) { continue; } @@ -712,4 +663,49 @@ return $status; } +function choice_get_response_data($choice, $cm, $groupmode){ + global $CFG; + + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + + if ($groupmode > 0) { + $currentgroup = groups_get_activity_group($cm); + } else { + $currentgroup = 0; + } + + $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true); + + if(empty($users)){ + return false; + } + + $userids = implode(', ', array_keys($users)); + + $answers = get_records_select('choice_answers', "choiceid = {$choice->id} AND userid IN ( $userids )",'', 'userid, id, optionid'); + + $allanswers = array(); + + foreach($choice->option as $optionid => $optionanme){ + $allanswers[$optionid] = array(); + } + + if(!empty($answers)){ + foreach($answers as $ans){ + $ans->user = $users[$ans->userid]; + $allanswers[$ans->optionid][] = $ans; + unset($users[$ans->userid]); + } + } + + foreach($users as $user){ + $rec = new object(); + $rec->user = $user; + $allanswers[0][] = $rec; + } + + return $allanswers; + +} + ?> diff -ruN /home/dan/moodle/moodle/mod/choice/report.php ./report.php --- /home/dan/moodle/moodle/mod/choice/report.php 2008-01-08 11:57:13.000000000 +0000 +++ ./report.php 2008-01-09 20:32:59.000000000 +0000 @@ -50,52 +50,12 @@ } else { $groupmode = groups_get_activity_groupmode($cm); groups_get_activity_group($cm, true); + $option_responses = choice_get_response_data($choice, $cm, $groupmode); } - $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); + if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) { require_once("$CFG->libdir/odslib.class.php"); @@ -119,42 +79,36 @@ /// 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; - } - } - $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++; + + foreach($option_responses as $optionid => $response){ + if(empty($response)){ + continue; + } + foreach($response as $r){ + $myxls->write_string($row,0,$r->user->lastname); + $myxls->write_string($row,1,$r->user->firstname); + $studentid=(!empty($r->user->idnumber) ? $r->user->idnumber : " "); + $myxls->write_string($row,2,$studentid); + + $ug2 =''; + if ($usergrps = groups_get_all_groups($course->id, $r->user->id)) { + foreach ($usergrps as $ug) { + $ug2 = $ug2. $ug->name; } - $pos=4; } + $myxls->write_string($row,3,$ug2); + + $useroption = choice_get_option_text($choice, $optionid); + if (isset($useroption)) { + $myxls->write_string($row,4,format_string($useroption,true)); + } + $row++; } - - /// Close the workbook - $workbook->close(); - - exit; - } - } + } + /// Close the workbook + $workbook->close(); + exit; + } //print spreadsheet if one is asked for: @@ -181,41 +135,37 @@ /// 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; - } - } - $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++; + + foreach($option_responses as $optionid => $response){ + if(empty($response)){ + continue; + } + foreach($response as $r){ + $myxls->write_string($row,0,$r->user->lastname); + $myxls->write_string($row,1,$r->user->firstname); + $studentid=(!empty($r->user->idnumber) ? $r->user->idnumber : " "); + $myxls->write_string($row,2,$studentid); + + $ug2 =''; + if ($usergrps = groups_get_all_groups($course->id, $r->user->id)) { + foreach ($usergrps as $ug) { + $ug2 = $ug2. $ug->name; } - $pos=4; } + $myxls->write_string($row,3,$ug2); + + $useroption = choice_get_option_text($choice, $optionid); + if (isset($useroption)) { + $myxls->write_string($row,4,format_string($useroption,true)); + } + $row++; } + } - /// Close the workbook - $workbook->close(); + /// Close the workbook + $workbook->close(); - exit; - } + exit; } // print text file if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) { @@ -233,30 +183,28 @@ echo get_string("group"). "\t"; echo get_string("choice","choice"). "\n"; - /// 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; + foreach($option_responses as $optionid => $response){ + if(empty($response)){ + continue; + } + foreach($response as $r){ + echo $r->user->lastname; + echo "\t".$r->user->firstname; $studentid = " "; - if (!empty($user->idnumber)) { - $studentid = $user->idnumber; + if (!empty($r->user->idnumber)) { + $studentid = $r->user->idnumber; } echo "\t". $studentid."\t"; $ug2 = ''; - if ($usergrps = groups_get_all_groups($course->id, $user->id)) { + if ($usergrps = groups_get_all_groups($course->id, $r->user->id)) { foreach ($usergrps as $ug) { $ug2 = $ug2. $ug->name; } } echo $ug2. "\t"; - echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n"; + echo format_string(choice_get_option_text($choice, $optionid),true). "\n"; } - $row++; - } + } exit; }
"; } else if ($choice->showunanswered) { @@ -508,13 +459,13 @@ echo "