ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPollAnswersRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  protected ilLanguage $lng;
26  protected Refinery $refinery;
27 
28  public function __construct(
29  ilLanguage $lng,
30  Refinery $refinery
31  ) {
32  $this->lng = $lng;
33  $this->refinery = $refinery;
34  }
35 
36  public function render(
37  ilTemplate $tpl,
38  ilPollAnswersHandler $answers,
39  bool $disable_input
40  ): void {
41  $single_answer = ($answers->getAnswerLimit() === 1);
42 
43  if (!is_null($last_vote = $answers->popLastVoteFromSession())) {
44  if (!$single_answer && empty($last_vote)) {
45  $error = $this->lng->txt("poll_vote_error_multi_no_answer");
46  } elseif (!$single_answer) {
47  $error = sprintf(
48  $this->lng->txt("poll_vote_error_multi"),
49  $answers->getAnswerLimit()
50  );
51  } else {
52  $error = $this->lng->txt("poll_vote_error_single");
53  }
54 
55  $tpl->setCurrentBlock("error_bl");
56  $tpl->setVariable("FORM_ERROR", $error);
57  $tpl->parseCurrentBlock();
58  }
59 
60  $tpl->setCurrentBlock('answer');
61  foreach ($answers->getAnswers() as $id => $answer) {
62  $this->renderAnswer(
63  $tpl,
64  $id,
65  $answer,
66  $last_vote,
67  $single_answer,
68  $disable_input
69  );
70  }
71 
72  if (!$disable_input) {
73  $tpl->setVariable("URL_FORM", $answers->getVoteURL());
74  $tpl->setVariable("CMD_FORM", $answers->getVoteCommand());
75  $tpl->setVariable("TXT_SUBMIT", $this->lng->txt("poll_vote"));
76  }
77  }
78 
79  protected function renderAnswer(
80  ilTemplate $tpl,
81  int $id,
82  string $answer,
83  ?array $last_vote,
84  bool $single_answer,
85  bool $disable_input
86  ): void {
87  if ($single_answer) {
88  $tpl->setVariable("ANSWER_INPUT", "radio");
89  $tpl->setVariable("ANSWER_NAME", "aw");
90  } else {
91  $tpl->setVariable("ANSWER_INPUT", "checkbox");
92  $tpl->setVariable("ANSWER_NAME", "aw[]");
93 
94  $status = [];
95  if (!empty($last_vote) && is_array($last_vote) && in_array($id, $last_vote)) {
96  $status[] = 'checked="checked"';
97  }
98  if ($disable_input) {
99  $status[] = 'disabled';
100  }
101  if ($status) {
102  $tpl->setVariable("ANSWER_STATUS", implode(' ', $status));
103  }
104  }
105  $tpl->setVariable("VALUE_ANSWER", $id);
106  $tpl->setVariable(
107  "TXT_ANSWER_VOTE",
108  $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform(nl2br($answer))
109  );
110  $tpl->parseCurrentBlock();
111  }
112 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
renderAnswer(ilTemplate $tpl, int $id, string $answer, ?array $last_vote, bool $single_answer, bool $disable_input)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
render(ilTemplate $tpl, ilPollAnswersHandler $answers, bool $disable_input)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
popLastVoteFromSession()
TODO session handling should get its own class.
__construct(ilLanguage $lng, Refinery $refinery)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23