ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPollStateInfo.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected array $cached_user_status = [];
24 
25  public function isOfflineOrUnavailable(ilObjPoll $poll): bool
26  {
27  $offline = $poll->getOfflineStatus();
28  $before_start = false;
29  $after_end = false;
31  $before_start = $poll->getAccessBegin() > time();
32  $after_end = $poll->getAccessEnd() < time();
33  }
34  return $offline || $before_start || $after_end;
35  }
36 
37  public function hasQuestion(ilObjPoll $poll): bool
38  {
39  if (count($poll->getAnswers())) {
40  return true;
41  }
42  return false;
43  }
44 
48  public function hasVotingPeriodNotStarted(
49  ilObjPoll $poll,
50  bool $default
51  ): bool {
52  if (!$poll->getVotingPeriod()) {
53  return $default;
54  }
55  if ($poll->getVotingPeriodBegin() > time()) {
56  return true;
57  }
58  return false;
59  }
60 
64  public function hasVotingPeriodEnded(
65  ilObjPoll $poll,
66  bool $default
67  ): bool {
68  if (!$poll->getVotingPeriod()) {
69  return $default;
70  }
71  if ($poll->getVotingPeriodEnd() < time()) {
72  return true;
73  }
74  return false;
75  }
76 
77  public function hasUserAlreadyVoted(int $user_id, ilObjPoll $poll): bool
78  {
79  if (!array_key_exists($user_id, $this->cached_user_status)) {
80  $this->cached_user_status[$user_id] = $poll->hasUserVoted($user_id);
81  }
82  return $this->cached_user_status[$user_id];
83  }
84 
85  public function mayUserVote(int $user_id): bool
86  {
87  return !$this->isUserAnonymous($user_id);
88  }
89 
90  public function isUserAnonymous(int $user_id): bool
91  {
92  return $user_id === ANONYMOUS_USER_ID;
93  }
94 
95  public function areResultsVisible(int $user_id, ilObjPoll $poll): bool
96  {
97  switch ($poll->getViewResults()) {
99  return false;
100 
102  return true;
103 
105  return $this->hasVotingPeriodEnded($poll, true);
106 
108  if ($this->hasUserAlreadyVoted($user_id, $poll)) {
109  return true;
110  }
111  return false;
112  }
113  return false;
114  }
115 
116  public function willResultsBeShown(ilObjPoll $poll): bool
117  {
118  if (
120  $this->hasVotingPeriodEnded($poll, false)
121  ) {
122  return false;
123  }
124  return true;
125  }
126 }
const int VIEW_RESULTS_ALWAYS
const ANONYMOUS_USER_ID
Definition: constants.php:27
hasUserVoted(int $a_user_id)
hasQuestion(ilObjPoll $poll)
const int VIEW_RESULTS_AFTER_PERIOD
isOfflineOrUnavailable(ilObjPoll $poll)
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
const int VIEW_RESULTS_AFTER_VOTE
areResultsVisible(int $user_id, ilObjPoll $poll)
const int VIEW_RESULTS_NEVER