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 1b68961 commit fc62206
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added more precision to the calculation of the position on the results.
### Added
- Added ci changes for Moodle 4.3.
- 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.
### Fixed
- Fix the margins in the results page and the voting options.
- Fix & character in the options.
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,
AVG(sa.position) 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
43 changes: 36 additions & 7 deletions templates/sort_voting_results.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,75 @@
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="table table-bordered">
<thead>
<tr>
<th class="w-25">{{#str}} position, mod_sortvoting {{/str}}</th>
<th class="w-25">
<div
data-toggle="tooltip"
data-placement="left"
class="text-info"
title="{{#str}} xvotesreceived, mod_sortvoting, {{maxvotescount}} {{/str}}"
>
{{#str}} position, mod_sortvoting {{/str}}
</div>
</th>
<th class="w-75">{{#str}} option, mod_sortvoting {{/str}}</th>
</tr>
</thead>
<tbody>
{{#votes}}
<tr>
<td>{{position}}</td>
<td>
<div {{^showvotescount}}
data-toggle="tooltip"
data-placement="left"
class="text-info"
title="{{#str}} xvotesreceived, mod_sortvoting, {{votescount}} {{/str}}"
{{/showvotescount}} >
{{position}}
</div>
</td>
<td>{{text}}</td>
</tr>
{{/votes}}
Expand Down

0 comments on commit fc62206

Please sign in to comment.