ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPollAnswersHandler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected string $vote_url;
24  protected string $vote_cmd;
25  protected int $poll_id;
26 
30  protected array $answers;
31  protected int $answer_limit;
32 
33  public function __construct(
34  ilObjPoll $poll,
35  string $vote_url,
36  string $vote_cmd
37  ) {
38  $this->vote_url = $vote_url;
39  $this->vote_cmd = $vote_cmd;
40  $this->answer_limit = $poll->getMaxNumberOfAnswers();
41  $this->poll_id = $poll->getId();
42 
43  $answers = [];
44  foreach ($poll->getAnswers() as $answer) {
45  $id = (int) ($answer['id'] ?? 0);
46  $text = (string) ($answer['answer'] ?? '');
47  $answers[$id] = $text;
48  }
49  $this->answers = $answers;
50  }
51 
55  public function popLastVoteFromSession(): ?array
56  {
57  $session_last_poll_vote = ilSession::get('last_poll_vote');
58  if (isset($session_last_poll_vote[$this->poll_id])) {
59  $last_vote = $session_last_poll_vote[$this->poll_id];
60  unset($session_last_poll_vote[$this->poll_id]);
61  ilSession::set('last_poll_vote', $session_last_poll_vote);
62  return $last_vote;
63  }
64  return null;
65  }
66 
70  public function getAnswers(): Generator
71  {
72  foreach ($this->answers as $id => $answer) {
73  yield $id => $answer;
74  }
75  }
76 
77  public function getAnswer(int $id): string
78  {
79  return (string) ($this->answers[$id] ?? '');
80  }
81 
82  public function getNumberOfAnswers(): int
83  {
84  return count($this->answers);
85  }
86 
87  public function getAnswerLimitForInfo(): ?int
88  {
89  $single_answer = $this->getAnswerLimit() === 1;
90  $below_max = $this->getNumberOfAnswers() > $this->getAnswerLimit();
91  if ($below_max && !$single_answer) {
92  return $this->getAnswerLimit();
93  }
94  return null;
95  }
96 
97  public function getAnswerLimit(): int
98  {
99  return $this->answer_limit;
100  }
101 
102  public function getVoteURL(): string
103  {
104  return $this->vote_url;
105  }
106 
107  public function getVoteCommand(): string
108  {
109  return $this->vote_cmd;
110  }
111 }
static get(string $a_var)
__construct(ilObjPoll $poll, string $vote_url, string $vote_cmd)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilObjPoll.
popLastVoteFromSession()
TODO session handling should get its own class.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static set(string $a_var, $a_val)
Set a value.