ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollStateInfo.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 {
24  protected array $cached_user_status = [];
25 
26  public function isOfflineOrUnavailable(ilObjPoll $poll): bool
27  {
28  $offline = $poll->getOfflineStatus();
29  $before_start = false;
30  $after_end = false;
32  $before_start = $poll->getAccessBegin() > time();
33  $after_end = $poll->getAccessEnd() < time();
34  }
35  return $offline || $before_start || $after_end;
36  }
37 
38  public function hasQuestion(ilObjPoll $poll): bool
39  {
40  if (count($poll->getAnswers())) {
41  return true;
42  }
43  return false;
44  }
45 
49  public function hasVotingPeriodNotStarted(
50  ilObjPoll $poll,
51  bool $default
52  ): bool {
53  if (!$poll->getVotingPeriod()) {
54  return $default;
55  }
56  if ($poll->getVotingPeriodBegin() > time()) {
57  return true;
58  }
59  return false;
60  }
61 
65  public function hasVotingPeriodEnded(
66  ilObjPoll $poll,
67  bool $default
68  ): bool {
69  if (!$poll->getVotingPeriod()) {
70  return $default;
71  }
72  if ($poll->getVotingPeriodEnd() < time()) {
73  return true;
74  }
75  return false;
76  }
77 
78  public function hasUserAlreadyVoted(int $user_id, ilObjPoll $poll): bool
79  {
80  if (!array_key_exists($user_id, $this->cached_user_status)) {
81  $this->cached_user_status[$user_id] = $poll->hasUserVoted($user_id);
82  }
83  return $this->cached_user_status[$user_id];
84  }
85 
86  public function mayUserVote(int $user_id): bool
87  {
88  return !$this->isUserAnonymous($user_id);
89  }
90 
91  public function isUserAnonymous(int $user_id): bool
92  {
93  return $user_id === ANONYMOUS_USER_ID;
94  }
95 
96  public function areResultsVisible(int $user_id, ilObjPoll $poll): bool
97  {
98  switch ($poll->getViewResults()) {
100  return false;
101 
103  return true;
104 
106  return $this->hasVotingPeriodEnded($poll, true);
107 
109  if ($this->hasUserAlreadyVoted($user_id, $poll)) {
110  return true;
111  }
112  return false;
113  }
114  return false;
115  }
116 
117  public function willResultsBeShown(ilObjPoll $poll): bool
118  {
119  if (
121  $this->hasVotingPeriodEnded($poll, false)
122  ) {
123  return false;
124  }
125  return true;
126  }
127 }
const VIEW_RESULTS_AFTER_PERIOD
const VIEW_RESULTS_AFTER_VOTE
const VIEW_RESULTS_NEVER
const ANONYMOUS_USER_ID
Definition: constants.php:27
hasUserVoted(int $a_user_id)
hasQuestion(ilObjPoll $poll)
const VIEW_RESULTS_ALWAYS
isOfflineOrUnavailable(ilObjPoll $poll)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
willResultsBeShown(ilObjPoll $poll)
hasUserAlreadyVoted(int $user_id, ilObjPoll $poll)
isUserAnonymous(int $user_id)
mayUserVote(int $user_id)
hasVotingPeriodEnded(ilObjPoll $poll, bool $default)
returns default if no voting period is set
Class ilObjPoll.
hasVotingPeriodNotStarted(ilObjPoll $poll, bool $default)
returns default if no voting period is set
areResultsVisible(int $user_id, ilObjPoll $poll)