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