ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollAnswersRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 {
24  protected ilLanguage $lng;
25 
26  public function __construct(ilLanguage $lng)
27  {
28  $this->lng = $lng;
29  }
30 
31  public function render(
32  ilTemplate $tpl,
33  ilPollAnswersHandler $answers,
34  bool $disable_input
35  ): void {
36  $single_answer = ($answers->getAnswerLimit() === 1);
37 
38  if (!is_null($last_vote = $answers->popLastVoteFromSession())) {
39  if (!$single_answer && empty($last_vote)) {
40  $error = $this->lng->txt("poll_vote_error_multi_no_answer");
41  } elseif (!$single_answer) {
42  $error = sprintf(
43  $this->lng->txt("poll_vote_error_multi"),
44  $answers->getAnswerLimit()
45  );
46  } else {
47  $error = $this->lng->txt("poll_vote_error_single");
48  }
49 
50  $tpl->setCurrentBlock("error_bl");
51  $tpl->setVariable("FORM_ERROR", $error);
52  $tpl->parseCurrentBlock();
53  }
54 
55  $tpl->setCurrentBlock('answer');
56  foreach ($answers->getAnswers() as $id => $answer) {
57  $this->renderAnswer(
58  $tpl,
59  $id,
60  $answer,
61  $last_vote,
62  $single_answer,
63  $disable_input
64  );
65  }
66 
67  if (!$disable_input) {
68  $tpl->setVariable("URL_FORM", $answers->getVoteURL());
69  $tpl->setVariable("CMD_FORM", $answers->getVoteCommand());
70  $tpl->setVariable("TXT_SUBMIT", $this->lng->txt("poll_vote"));
71  }
72  }
73 
74  protected function renderAnswer(
75  ilTemplate $tpl,
76  int $id,
77  string $answer,
78  ?array $last_vote,
79  bool $single_answer,
80  bool $disable_input
81  ): void {
82  if ($single_answer) {
83  $tpl->setVariable("ANSWER_INPUT", "radio");
84  $tpl->setVariable("ANSWER_NAME", "aw");
85  } else {
86  $tpl->setVariable("ANSWER_INPUT", "checkbox");
87  $tpl->setVariable("ANSWER_NAME", "aw[]");
88 
89  $status = [];
90  if (!empty($last_vote) && is_array($last_vote) && in_array($id, $last_vote)) {
91  $status[] = 'checked="checked"';
92  }
93  if ($disable_input) {
94  $status[] = 'disabled';
95  }
96  if ($status) {
97  $tpl->setVariable("ANSWER_STATUS", implode(' ', $status));
98  }
99  }
100  $tpl->setVariable("VALUE_ANSWER", $id);
101  $tpl->setVariable(
102  "TXT_ANSWER_VOTE",
103  $this->specialCharsAsEntities(nl2br($answer))
104  );
105  $tpl->parseCurrentBlock();
106  }
107 
108  protected function specialCharsAsEntities(string $string): string
109  {
110  // Should be replaced by a proper refinery transformation once https://github.com/ILIAS-eLearning/ILIAS/pull/6314 is merged
111  return htmlspecialchars(
112  $string,
113  ENT_QUOTES | ENT_SUBSTITUTE,
114  'utf-8'
115  );
116  }
117 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
renderAnswer(ilTemplate $tpl, int $id, string $answer, ?array $last_vote, bool $single_answer, bool $disable_input)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
render(ilTemplate $tpl, ilPollAnswersHandler $answers, bool $disable_input)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
popLastVoteFromSession()
TODO session handling should get its own class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23