ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollContentRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
24 
26 {
33  protected ilLanguage $lng;
36 
37  public function __construct(
38  ilLanguage $lng,
39  UIFactory $ui_factory,
40  UIRenderer $ui_renderer,
41  ilPollStateInfo $availability,
42  ilPollCommentsHandler $comments,
43  ilPollAnswersHandler $answers,
44  ilPollAnswersRenderer $answers_renderer,
45  ilPollResultsHandler $results,
46  ilPollResultsRenderer $results_renderer
47  ) {
48  $this->lng = $lng;
49  $this->ui_factory = $ui_factory;
50  $this->ui_renderer = $ui_renderer;
51  $this->state = $availability;
52  $this->comments = $comments;
53  $this->answers = $answers;
54  $this->answers_renderer = $answers_renderer;
55  $this->results = $results;
56  $this->results_renderer = $results_renderer;
57  }
58 
59  public function render(
60  ilTemplate $tpl,
61  int $ref_id,
62  int $user_id,
63  ilObjPoll $poll,
64  bool $admin_view = false
65  ): void {
66  $this->renderAnchor($tpl, $poll->getId());
67  $this->renderAvailability($tpl, $poll);
68  $this->renderDescription($tpl, $poll->getLongDescription());
69 
70  if (!$this->state->hasQuestion($poll)) {
71  $this->renderNoQuestionMessage($tpl);
72  } elseif ($this->state->hasVotingPeriodNotStarted($poll, false)) {
74  $tpl,
75  $poll->getVotingPeriodBegin(),
76  $poll->getVotingPeriodEnd()
77  );
78  } else {
79  $this->renderQuestion(
80  $tpl,
81  $poll->getQuestion(),
82  $poll->getImageFullPath()
83  );
84  if (!$admin_view) {
85  $this->renderAnswersAndResults($tpl, $poll, $user_id);
86  }
87  }
88 
89  if ($poll->getShowComments()) {
90  $this->renderComments($tpl, $ref_id);
91  }
92 
93  if ($this->state->isUserAnonymous($user_id)) {
94  $this->renderAlertForAnonymousUser($tpl);
95  }
96  }
97 
98  protected function renderAnswersAndResults(
99  ilTemplate $tpl,
100  ilObjPoll $poll,
101  int $user_id
102  ): void {
103  $either_is_shown = false;
104  if (
105  !$this->state->hasUserAlreadyVoted($user_id, $poll) &&
106  !$this->state->hasVotingPeriodEnded($poll, false)
107  ) {
108  $this->renderAnswers($tpl, $poll, $user_id);
109  $either_is_shown = true;
110  }
111 
112  if (
113  $this->state->areResultsVisible($user_id, $poll) &&
114  $this->results->getTotalVotes()
115  ) {
116  $this->renderResults($tpl, $poll);
118  $tpl,
119  $this->results->getTotalVotes()
120  );
121  $either_is_shown = true;
122  } elseif ($this->state->areResultsVisible($user_id, $poll)) {
124  $tpl,
125  $this->results->getTotalVotes()
126  );
127  }
128 
129  if (!$either_is_shown) {
131  $tpl,
132  $this->state->hasUserAlreadyVoted($user_id, $poll),
133  $this->state->willResultsBeShown($poll),
134  $poll->getVotingPeriodEnd()
135  );
136  }
137  }
138 
139  protected function renderAnswers(
140  ilTemplate $tpl,
141  ilObjPoll $poll,
142  int $user_id
143  ): void {
144  $this->renderMiscVoteInfo(
145  $tpl,
146  $this->answers->getAnswerLimitForInfo(),
147  $poll->getVotingPeriodEnd()
148  );
149  $this->answers_renderer->render(
150  $tpl,
151  $this->answers,
152  !$this->state->mayUserVote($user_id)
153  );
154  $this->renderAnonimityInfo($tpl, !$poll->getNonAnonymous());
155  }
156 
157  protected function renderResults(
158  ilTemplate $tpl,
159  ilObjPoll $poll
160  ): void {
161  $this->results_renderer->render(
162  $tpl,
163  $this->results,
164  $poll->getShowResultsAs()
165  );
166  }
167 
168  protected function renderTotalParticipantsInfo(
169  ilTemplate $tpl,
170  int $total
171  ): void {
172  if ($total === 1) {
173  $tpl->setVariable(
174  "TOTAL_ANSWERS",
175  $this->lng->txt("poll_population_singular")
176  );
177  return;
178  }
179  $tpl->setVariable(
180  "TOTAL_ANSWERS",
181  sprintf($this->lng->txt("poll_population"), $total)
182  );
183  }
184 
185  protected function renderNotAbleToVoteMessage(
186  ilTemplate $tpl,
187  bool $has_voted,
188  bool $results_in_future,
189  int $voting_end_date
190  ): void {
191  $messages = [];
192 
193  if ($has_voted) {
194  $messages[] = $this->lng->txt("poll_block_message_already_voted");
195  } else {
196  $messages[] = sprintf(
197  $this->lng->txt("poll_voting_period_ended_info"),
198  $this->getFormattedDate($voting_end_date)
199  );
200  }
201 
202  if ($results_in_future && $has_voted) {
203  $messages[] = sprintf(
204  $this->lng->txt("poll_block_results_available_on"),
205  $this->getFormattedDate($voting_end_date)
206  );
207  }
208 
209  $tpl->setVariable(
210  "MESSAGE_BOX",
211  $this->getMessageBox(
212  implode(' ', $messages)
213  )
214  );
215  }
216 
217  protected function renderQuestion(
218  ilTemplate $tpl,
219  string $text,
220  ?string $img_path
221  ): void {
222  $tpl->setVariable(
223  "TXT_QUESTION",
224  $this->specialCharsAsEntities(nl2br(trim($text)))
225  );
226  if ($img_path) {
227  $tpl->setVariable(
228  "URL_IMAGE",
229  ilWACSignedPath::signFile($img_path)
230  );
231  }
232  }
233 
234  protected function renderMiscVoteInfo(
235  ilTemplate $tpl,
236  ?int $answer_limit,
237  int $deadline
238  ): void {
239  $infos = [];
240 
241  if ($answer_limit) {
242  $infos[] = sprintf(
243  $this->lng->txt('poll_max_number_of_answers_info'),
244  $answer_limit
245  );
246  }
247 
248  if ($deadline) {
249  $infos[] = sprintf(
250  $this->lng->txt("poll_voting_period_info"),
251  $this->getFormattedDate($deadline)
252  );
253  }
254 
255  if ($infos) {
256  $tpl->setVariable('VOTE_INFO', implode(' ', $infos));
257  }
258  }
259 
260  protected function renderAnonimityInfo(
261  ilTemplate $tpl,
262  bool $anonymous
263  ): void {
264  if ($anonymous) {
265  $info = $this->lng->txt("poll_anonymous_warning");
266  } else {
267  $info = $this->lng->txt("poll_non_anonymous_warning");
268  }
269  $tpl->setVariable("ANONIMITY_INFO", $info);
270  }
271 
273  ilTemplate $tpl,
274  int $vote_start,
275  int $vote_end
276  ): void {
277  $message = sprintf(
278  $this->lng->txt("poll_voting_period_full_info"),
279  $this->getFormattedDate($vote_start),
280  $this->getFormattedDate($vote_end)
281  );
282  $tpl->setVariable("MESSAGE_BOX", $this->getMessageBox($message));
283  }
284 
285  protected function renderNoQuestionMessage(ilTemplate $tpl): void
286  {
287  $tpl->setVariable(
288  "MESSAGE_BOX",
289  $this->getMessageBox(
290  $this->lng->txt("poll_block_message_no_answers")
291  )
292  );
293  }
294 
295  protected function renderAnchor(ilTemplate $tpl, int $obj_id): void
296  {
297  $tpl->setVariable("ANCHOR_ID", $obj_id);
298  }
299 
300  protected function renderDescription(
301  ilTemplate $tpl,
302  string $description
303  ): void {
304  $description = trim($description);
305  if ($description) {
306  $tpl->setVariable(
307  "TXT_DESC",
308  nl2br($this->specialCharsAsEntities($description))
309  );
310  }
311  }
312 
313  protected function renderAvailability(ilTemplate $tpl, ilObjPoll $poll): void
314  {
315  if ($this->state->isOfflineOrUnavailable($poll)) {
316  $tpl->setVariable("TXT_OFFLINE", $this->lng->txt('offline'));
317  }
318  }
319 
320  protected function renderAlertForAnonymousUser(ilTemplate $tpl): void
321  {
322  $tpl->setVariable("TXT_ANON", $this->lng->txt('no_access_item_public'));
323  }
324 
325  protected function renderComments(ilTemplate $tpl, int $ref_id): void
326  {
327  $tpl->setVariable("LANG_COMMENTS", $this->lng->txt('poll_comments'));
328  $tpl->setVariable("COMMENT_JSCALL", $this->comments->commentJSCall($ref_id));
329  $tpl->setVariable("COMMENTS_COUNT_ID", $ref_id);
330 
331  $comments_count = $this->comments->getNumberOfComments($ref_id);
332 
333  if ($comments_count > 0) {
334  $tpl->setVariable("COMMENTS_COUNT", "(" . $comments_count . ")");
335  }
336 
337  $tpl->setVariable("COMMENTS_REDRAW_URL", $this->comments->getRedrawURL());
338  }
339 
340  protected function getMessageBox(string $message): string
341  {
342  return $this->ui_renderer->render(
343  $this->ui_factory->messageBox()->info($message)
344  );
345  }
346 
347  protected function getFormattedDate(int $date): string
348  {
350  new ilDateTime($date, IL_CAL_UNIX)
351  );
352  }
353 
354  protected function specialCharsAsEntities(string $string): string
355  {
356  // Should be replaced by a proper refinery transformation once https://github.com/ILIAS-eLearning/ILIAS/pull/6314 is merged
357  return htmlspecialchars(
358  $string,
359  ENT_QUOTES | ENT_SUBSTITUTE,
360  'utf-8'
361  );
362  }
363 }
renderNotAbleToVoteMessage(ilTemplate $tpl, bool $has_voted, bool $results_in_future, int $voting_end_date)
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...
renderResults(ilTemplate $tpl, ilObjPoll $poll)
__construct(ilLanguage $lng, UIFactory $ui_factory, UIRenderer $ui_renderer, ilPollStateInfo $availability, ilPollCommentsHandler $comments, ilPollAnswersHandler $answers, ilPollAnswersRenderer $answers_renderer, ilPollResultsHandler $results, ilPollResultsRenderer $results_renderer)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
renderNotWithinVotingPeriodMessage(ilTemplate $tpl, int $vote_start, int $vote_end)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
renderTotalParticipantsInfo(ilTemplate $tpl, int $total)
const IL_CAL_UNIX
renderDescription(ilTemplate $tpl, string $description)
renderAnchor(ilTemplate $tpl, int $obj_id)
renderAnswers(ilTemplate $tpl, ilObjPoll $poll, int $user_id)
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: xapiexit.php:22
$ref_id
Definition: ltiauth.php:67
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
renderMiscVoteInfo(ilTemplate $tpl, ?int $answer_limit, int $deadline)
renderQuestion(ilTemplate $tpl, string $text, ?string $img_path)
Class ilObjPoll.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilPollResultsRenderer $results_renderer
renderComments(ilTemplate $tpl, int $ref_id)
getLongDescription()
get object long description (stored in object_description)
renderAvailability(ilTemplate $tpl, ilObjPoll $poll)
static signFile(string $path_to_file)
renderAnswersAndResults(ilTemplate $tpl, ilObjPoll $poll, int $user_id)
$message
Definition: xapiexit.php:32
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...
render(ilTemplate $tpl, int $ref_id, int $user_id, ilObjPoll $poll, bool $admin_view=false)
renderAnonimityInfo(ilTemplate $tpl, bool $anonymous)
renderAlertForAnonymousUser(ilTemplate $tpl)
ilPollAnswersRenderer $answers_renderer
getImageFullPath(bool $a_as_thumb=false)