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