From 7a134d17ff54a609510081ccf08307ac3afc91b9 Mon Sep 17 00:00:00 2001 From: Odei Alba Date: Fri, 10 Nov 2023 15:36:05 +0100 Subject: [PATCH] SV-35 Show number of votes on the results --- CHANGELOG.md | 2 ++ classes/output/sort_voting_results.php | 3 +- lang/en/sortvoting.php | 1 + lib.php | 5 ++- templates/sort_voting_results.mustache | 43 +++++++++++++++++++++----- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f9acfa..c2cbd52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/classes/output/sort_voting_results.php b/classes/output/sort_voting_results.php index c7b6da7..bae614c 100644 --- a/classes/output/sort_voting_results.php +++ b/classes/output/sort_voting_results.php @@ -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]; } } diff --git a/lang/en/sortvoting.php b/lang/en/sortvoting.php index a508138..4529e08 100644 --- a/lang/en/sortvoting.php +++ b/lang/en/sortvoting.php @@ -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'; diff --git a/lib.php b/lib.php index f55634d..7e91cf8 100644 --- a/lib.php +++ b/lib.php @@ -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 @@ -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; } diff --git a/templates/sort_voting_results.mustache b/templates/sort_voting_results.mustache index 7768e68..ff1799e 100644 --- a/templates/sort_voting_results.mustache +++ b/templates/sort_voting_results.mustache @@ -21,31 +21,42 @@ 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" } ] } @@ -53,14 +64,32 @@ - + {{#votes}} - + {{/votes}}
{{#str}} position, mod_sortvoting {{/str}} +
+ {{#str}} position, mod_sortvoting {{/str}} +
+
{{#str}} option, mod_sortvoting {{/str}}
{{position}} +
+ {{position}} +
+
{{text}}