ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPollBlock.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
28 {
29  protected ilLanguage $lng;
30  protected ilObjPoll $poll;
31  protected array $answers = [];
32  protected bool $visible = false;
33  protected bool $active = false;
34 
35  public function __construct(int $a_id = 0)
36  {
37  global $DIC;
38 
39  parent::__construct($a_id);
40  $this->lng = $DIC->language();
41  }
42 
46  public function setRefId(int $a_id): void
47  {
48  $this->poll = new ilObjPoll($a_id, true);
49  $this->answers = $this->poll->getAnswers();
50  }
51 
52  public function getPoll(): ilObjPoll
53  {
54  return $this->poll;
55  }
56 
60  public function hasAnyContent(int $a_user_id, int $a_ref_id): bool
61  {
62  if (!count($this->answers)) {
63  return false;
64  }
65 
66  $this->active = ilObjPollAccess::_isActivated($a_ref_id);
67  if (!$this->active) {
68  return false;
69  }
70 
71  if (!$this->maySeeQuestion($a_user_id) &&
72  !$this->maySeeResults($a_user_id)) {
73  return false;
74  }
75 
76  return true;
77  }
78 
79  public function maySeeQuestion($a_user_id): bool
80  {
81  if (!$this->active) {
82  return false;
83  }
84 
85  if ($this->poll->hasUserVoted($a_user_id)) {
86  return false;
87  }
88 
89  if ($this->poll->getVotingPeriod() &&
90  ($this->poll->getVotingPeriodBegin() > time() ||
91  $this->poll->getVotingPeriodEnd() < time())) {
92  return false;
93  }
94 
95  return true;
96  }
97 
98  public function mayVote($a_user_id): bool
99  {
100  return $this->maySeeQuestion($a_user_id) && $a_user_id != ANONYMOUS_USER_ID;
101  }
102 
103  public function mayNotResultsYet(): bool
104  {
105  if ($this->poll->getViewResults() === ilObjPoll::VIEW_RESULTS_AFTER_PERIOD &&
106  $this->poll->getVotingPeriod() &&
107  $this->poll->getVotingPeriodEnd() > time()) {
108  return true;
109  }
110  return false;
111  }
112 
113  public function maySeeResults(int $a_user_id): bool
114  {
115  if (!$this->active) {
116  return false;
117  }
118 
119  switch ($this->poll->getViewResults()) {
121  return false;
122 
124  // fallthrough
125 
126  // #12023 - see mayNotResultsYet()
128  return true;
129 
131  if ($this->poll->hasUserVoted($a_user_id)) {
132  return true;
133  }
134  return false;
135  }
136  return false;
137  }
138 
139  public function getMessage(int $a_user_id): ?string
140  {
141  if (!count($this->answers)) {
142  return $this->lng->txt("poll_block_message_no_answers");
143  }
144 
145  if (!$this->active) {
146  if ($this->poll->getOfflineStatus()) {
147  return $this->lng->txt("poll_block_message_offline");
148  }
149  if ($this->poll->getAccessBegin() > time()) {
150  $date = ilDatePresentation::formatDate(new ilDateTime($this->poll->getAccessBegin(), IL_CAL_UNIX));
151  return sprintf($this->lng->txt("poll_block_message_inactive"), $date);
152  }
153  }
154 
155  return null;
156  }
157 
161  public function showResultsAs(): int
162  {
163  return $this->poll->getShowResultsAs();
164  }
165 
169  public function showComments(): bool
170  {
171  return $this->poll->getShowComments();
172  }
173 }
maySeeQuestion($a_user_id)
const VIEW_RESULTS_AFTER_PERIOD
showComments()
Are comments enabled or disabled.
const VIEW_RESULTS_AFTER_VOTE
const VIEW_RESULTS_NEVER
getMessage(int $a_user_id)
const ANONYMOUS_USER_ID
Definition: constants.php:27
const VIEW_RESULTS_ALWAYS
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
mayVote($a_user_id)
maySeeResults(int $a_user_id)
const IL_CAL_UNIX
__construct(int $a_id=0)
static _isActivated(int $a_ref_id)
global $DIC
Definition: feed.php:28
setRefId(int $a_id)
Set ref id (needed for poll access)
showResultsAs()
Show Results as (Barchart or Piechart)
ilLanguage $lng
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
hasAnyContent(int $a_user_id, int $a_ref_id)
Check if user will see any content (vote/result)