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