Skip to content

Commit

Permalink
SV-35 Show number of votes on the results
Browse files Browse the repository at this point in the history
  • Loading branch information
odeialba committed Nov 10, 2023
1 parent 10c2159 commit 900cf47
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## 1.0.7 - 2023-11-10
### Added
- Added the number of received votes to the results table. If there is an option with less votes than the rest, the number of votes will be shown next to the position.

## 1.0.6 - 2023-10-27
### Added
Expand Down
3 changes: 2 additions & 1 deletion classes/output/sort_voting_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function __construct(\stdClass $sortvoting) {
*/
public function export_for_template(renderer_base $output): array {
$existingvotes = sortvoting_get_response_data($this->sortvoting);
$maxvotescount = (int) max(array_column($existingvotes, 'votescount'));

return ['votes' => $existingvotes];
return ['votes' => $existingvotes, 'maxvotescount' => $maxvotescount];
}
}
1 change: 1 addition & 0 deletions lang/en/sortvoting.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@
$string['sortvotingsettings'] = 'Voting name';
$string['voteerror'] = 'There was an error saving your vote.';
$string['votesuccess'] = 'Your vote has been saved successfully.';
$string['xvotesreceived'] = '({$a} vote(s) received)';
5 changes: 4 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ function sortvoting_get_response_data(stdClass $sortvoting, bool $onlyactive = t

$sql = "SELECT so.id,
ROUND(AVG(sa.position), 2) AS avg,
so.text
so.text,
COUNT(sa.id) AS votescount
FROM {sortvoting_answers} sa
JOIN {sortvoting_options} so
ON sa.optionid = so.id
Expand All @@ -385,11 +386,13 @@ function sortvoting_get_response_data(stdClass $sortvoting, bool $onlyactive = t

$position = 1;
$previousvote = null;
$maxvotescount = (int) max(array_column($existingvotes, 'votescount'));
foreach ($existingvotes as $key => $vote) {
if ($previousvote !== null && $previousvote->avg !== $vote->avg) {
$position++;
}
$existingvotes[$key]->position = $position;
$existingvotes[$key]->showvotescount = $maxvotescount === (int) $vote->votescount;
$previousvote = $vote;
}

Expand Down
25 changes: 18 additions & 7 deletions templates/sort_voting_results.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,57 @@
Example context (json):
{
"maxvotescount": "5",
"votes": [
{
"id": "1",
"position": "1",
"text": "A"
"text": "A",
"showvotescount": false,
"votescount": "5"
},
{
"id": "2",
"position": "2",
"text": "B"
"text": "B",
"showvotescount": false,
"votescount": "5"
},
{
"id": "3",
"position": "2",
"text": "C"
"text": "C",
"showvotescount": false,
"votescount": "5"
},
{
"id": "4",
"position": "3",
"text": "D"
"text": "D",
"showvotescount": true,
"votescount": "4"
},
{
"id": "5",
"position": "4",
"text": "E"
"text": "E",
"showvotescount": false,
"votescount": "5"
}
]
}
}}
<table class="m-3 table table-bordered">
<thead>
<tr>
<th class="w-25">{{#str}} position, mod_sortvoting {{/str}}</th>
<th class="w-25">{{#str}} position, mod_sortvoting {{/str}}<span class="small text-muted float-right">{{#str}} xvotesreceived, mod_sortvoting, {{maxvotescount}} {{/str}}</span></th>
<th class="w-75">{{#str}} option, mod_sortvoting {{/str}}</th>
</tr>
</thead>
<tbody>
{{#votes}}
<tr>
<td>{{position}}</td>
<td>{{position}}{{^showvotescount}}<span class="small text-muted float-right">{{#str}} xvotesreceived, mod_sortvoting, {{votescount}} {{/str}}</span>{{/showvotescount}}</td>
<td>{{text}}</td>
</tr>
{{/votes}}
Expand Down

0 comments on commit 900cf47

Please sign in to comment.