ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollAnswersHandler.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 {
24  protected string $vote_url;
25  protected string $vote_cmd;
26  protected int $poll_id;
27 
31  protected array $answers;
32  protected int $answer_limit;
33 
34  public function __construct(
35  ilObjPoll $poll,
36  string $vote_url,
37  string $vote_cmd
38  ) {
39  $this->vote_url = $vote_url;
40  $this->vote_cmd = $vote_cmd;
41  $this->answer_limit = $poll->getMaxNumberOfAnswers();
42  $this->poll_id = $poll->getId();
43 
44  $answers = [];
45  foreach ($poll->getAnswers() as $answer) {
46  $id = (int) ($answer['id'] ?? 0);
47  $text = (string) ($answer['answer'] ?? '');
48  $answers[$id] = $text;
49  }
50  $this->answers = $answers;
51  }
52 
56  public function popLastVoteFromSession(): ?array
57  {
58  $session_last_poll_vote = ilSession::get('last_poll_vote');
59  if (isset($session_last_poll_vote[$this->poll_id])) {
60  $last_vote = $session_last_poll_vote[$this->poll_id];
61  unset($session_last_poll_vote[$this->poll_id]);
62  ilSession::set('last_poll_vote', $session_last_poll_vote);
63  return $last_vote;
64  }
65  return null;
66  }
67 
71  public function getAnswers(): Generator
72  {
73  foreach ($this->answers as $id => $answer) {
74  yield $id => $answer;
75  }
76  }
77 
78  public function getAnswer(int $id): string
79  {
80  return (string) ($this->answers[$id] ?? '');
81  }
82 
83  public function getNumberOfAnswers(): int
84  {
85  return count($this->answers);
86  }
87 
88  public function getAnswerLimitForInfo(): ?int
89  {
90  $single_answer = $this->getAnswerLimit() === 1;
91  $below_max = $this->getNumberOfAnswers() > $this->getAnswerLimit();
92  if ($below_max && !$single_answer) {
93  return $this->getAnswerLimit();
94  }
95  return null;
96  }
97 
98  public function getAnswerLimit(): int
99  {
100  return $this->answer_limit;
101  }
102 
103  public function getVoteURL(): string
104  {
105  return $this->vote_url;
106  }
107 
108  public function getVoteCommand(): string
109  {
110  return $this->vote_cmd;
111  }
112 }
static get(string $a_var)
__construct(ilObjPoll $poll, string $vote_url, string $vote_cmd)
Class ilObjPoll.
popLastVoteFromSession()
TODO session handling should get its own class.
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
static set(string $a_var, $a_val)
Set a value.