ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPollResultsHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
24 protected bool $sort_by_votes;
25 protected int $total_votes;
26
30 protected array $answer_percentages;
31
35 protected array $answer_totals;
36
37 public function __construct(
38 ilObjPoll $poll,
40 ) {
41 $this->sort_by_votes = $poll->getSortResultByVotes();
42 $this->answers = $answers;
43 $res = $poll->getVotePercentages();
44 $this->total_votes = (int) ($res['total'] ?? 0);
45 $res = (array) ($res['perc'] ?? []);
46 $this->answer_percentages = array_map(
47 fn(array $a) => (float) ($a['perc'] ?? 0),
48 $res
49 );
50 $this->answer_totals = array_map(
51 fn(array $a) => (int) ($a['abs'] ?? 0),
52 $res
53 );
54 }
55
59 public function getOrderedAnswerIds(): Generator
60 {
61 if ($this->sort_by_votes) {
62 $order = $this->answer_totals;
63 arsort($order);
64 $order = array_keys($order);
65
66 foreach ($this->answers->getAnswers() as $id => $answer) {
67 if (!in_array($id, $order)) {
68 $order[] = $id;
69 }
70 }
71
72 foreach ($order as $id) {
73 yield $id;
74 }
75 } else {
76 foreach ($this->answers->getAnswers() as $id => $answer) {
77 yield $id;
78 }
79 }
80 }
81
82 public function getTotalVotes(): int
83 {
84 return $this->total_votes;
85 }
86
87 public function getAnswerText(int $id): string
88 {
89 return $this->answers->getAnswer($id);
90 }
91
92 public function getAnswerPercentage(int $id): float
93 {
94 return $this->answer_percentages[$id] ?? 0;
95 }
96
97 public function getAnswerTotal(int $id): int
98 {
99 return $this->answer_totals[$id] ?? 0;
100 }
101}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilObjPoll.
__construct(ilObjPoll $poll, ilPollAnswersHandler $answers)
$res
Definition: ltiservices.php:69
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples