ILIAS  release_8 Revision v8.24
class.ilPollBlockGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
32{
33 public static string $block_type = "poll";
34 protected \ILIAS\Notes\Service $notes;
36 public static bool $js_init = false;
41 protected bool $new_rendering = true;
43 protected Factory $refinery;
44
45 public function __construct()
46 {
47 global $DIC;
48
49 $this->lng = $DIC->language();
50 $this->ctrl = $DIC->ctrl();
51 $this->user = $DIC->user();
52 $this->access = $DIC->access();
53
55
56 $this->lng->loadLanguageModule("poll");
57 $this->setRowTemplate("tpl.block.html", "Modules/Poll");
58
59 $this->container_view_manager = $DIC
60 ->container()
61 ->internal()
62 ->domain()
63 ->content()
64 ->view();
65 $this->http = $DIC->http();
66 $this->refinery = $DIC->refinery();
67 $this->notes = $DIC->notes();
68 }
69
70 public function getBlockType(): string
71 {
72 return self::$block_type;
73 }
74
78 protected function isRepositoryObject(): bool
79 {
80 return true;
81 }
82
83 protected function getRepositoryObjectGUIName(): string
84 {
85 return "ilobjpollgui";
86 }
87
88 public function setBlock(ilPollBlock $a_block): void
89 {
90 $this->setBlockId((string) $a_block->getId());
91 $this->poll_block = $a_block;
92 }
93
94 public function executeCommand(): void
95 {
96 $next_class = $this->ctrl->getNextClass();
97 $cmd = $this->ctrl->getCmd("getHTML");
98
99 switch ($next_class) {
100 default:
101 $this->$cmd();
102 break;
103 }
104 }
105
106 public function fillRow(array $a_set): void
107 {
108 // todo: Refactoring needed
109 $a_set = $a_set[0];
110
111 // handle messages
112
113 $mess = $this->poll_block->getMessage($this->user->getId());
114 if ($mess) {
115 $this->tpl->setVariable("TXT_QUESTION", $mess);
116 return;
117 }
118
119
120 // nested form problem
121 if (!$this->container_view_manager->isAdminView()) {
122 // vote
123
124 if ($this->poll_block->maySeeQuestion($this->user->getId())) {
125 $this->tpl->setCurrentBlock("mode_info_bl");
126 if ($this->poll_block->getPoll()->getNonAnonymous()) {
127 $mode_info = $this->lng->txt("poll_non_anonymous_warning");
128 } else {
129 $mode_info = $this->lng->txt("poll_anonymous_warning");
130 }
131 $this->tpl->setVariable("MODE_INFO", $mode_info);
132 $this->tpl->parseCurrentBlock();
133
134 $is_multi_answer = ($this->poll_block->getPoll()->getMaxNumberOfAnswers() > 1);
135
136 $session_last_poll_vote = ilSession::get('last_poll_vote');
137 if (isset($session_last_poll_vote[$this->poll_block->getPoll()->getId()])) {
138 $last_vote = $session_last_poll_vote[$this->poll_block->getPoll()->getId()];
139 unset($session_last_poll_vote[$this->poll_block->getPoll()->getId()]);
140 ilSession::set('last_poll_vote', $session_last_poll_vote);
141
142 if ($is_multi_answer) {
143 $error = sprintf(
144 $this->lng->txt("poll_vote_error_multi"),
145 $this->poll_block->getPoll()->getMaxNumberOfAnswers()
146 );
147 } else {
148 $error = $this->lng->txt("poll_vote_error_single");
149 }
150
151 $this->tpl->setCurrentBlock("error_bl");
152 $this->tpl->setVariable("FORM_ERROR", $error);
153 $this->tpl->parseCurrentBlock();
154 }
155
156 $this->tpl->setCurrentBlock("answer");
157 foreach ($a_set->getAnswers() as $item) {
158 $status = [];
159 $id = (int) ($item['id'] ?? 0);
160 $answer = (string) ($item['answer'] ?? 0);
161 if (!$is_multi_answer) {
162 $this->tpl->setVariable("ANSWER_INPUT", "radio");
163 $this->tpl->setVariable("ANSWER_NAME", "aw");
164 } else {
165 $this->tpl->setVariable("ANSWER_INPUT", "checkbox");
166 $this->tpl->setVariable("ANSWER_NAME", "aw[]");
167
168 if (!empty($last_vote) && is_array($last_vote) && in_array($id, $last_vote)) {
169 $status[] = 'checked="checked"';
170 }
171 }
172
173 if (!$this->poll_block->mayVote($this->user->getId())) {
174 $status[] = 'disabled';
175 }
176
177 if (!empty($status)) {
178 $this->tpl->setVariable("ANSWER_STATUS", implode(' ', $status));
179 }
180
181 $this->tpl->setVariable("VALUE_ANSWER", $id);
182 $this->tpl->setVariable("TXT_ANSWER_VOTE", $this->specialCharsAsEntities(nl2br($answer)));
183 $this->tpl->parseCurrentBlock();
184 }
185
186 if ($this->poll_block->mayVote($this->user->getId())) {
187 $this->ctrl->setParameterByClass(
189 "ref_id",
190 $this->getRefId()
191 );
192 $url = $this->ctrl->getLinkTargetByClass(
193 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
194 "vote"
195 );
196 $this->ctrl->clearParametersByClass($this->getRepositoryObjectGUIName());
197
198 $url .= "#poll" . $a_set->getID();
199
200 $this->tpl->setVariable("URL_FORM", $url);
201 $this->tpl->setVariable("CMD_FORM", "vote");
202 $this->tpl->setVariable("TXT_SUBMIT", $this->lng->txt("poll_vote"));
203 }
204
205 if ($this->poll_block->getPoll()->getVotingPeriod()) {
206 $this->tpl->setVariable(
207 "TXT_VOTING_END_PERIOD",
208 sprintf(
209 $this->lng->txt("poll_voting_period_info"),
210 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
211 )
212 );
213 }
214 }
215
216
217 // result
218 if ($this->poll_block->maySeeResults($this->user->getId())) {
219 if (!$this->poll_block->mayNotResultsYet()) {
220 $answers = [];
221 foreach ($a_set->getAnswers() as $item) {
222 $id = (int) ($item['id'] ?? 0);
223 $answers[$id] = (string) ($item['answer'] ?? 0);
224 }
225
226 $perc = $this->poll_block->getPoll()->getVotePercentages();
227 $total = (int) ($perc['total'] ?? 0);
228 $perc = (array) ($perc['perc'] ?? []);
229
230 $this->tpl->setVariable("TOTAL_ANSWERS", sprintf($this->lng->txt("poll_population"), $total));
231
232 if ($total) {
233 // sort results by votes / original position
234 if ($this->poll_block->getPoll()->getSortResultByVotes()) {
235 $order = array_keys(ilArrayUtil::sortArray($perc, "abs", "desc", true, true));
236
237 foreach (array_keys($answers) as $answer_id) {
238 if (!in_array($answer_id, $order)) {
239 $order[] = $answer_id;
240 }
241 }
242 } else {
243 $order = array_keys($answers);
244 }
245
246 // pie chart
247 if ($this->poll_block->showResultsAs() === ilObjPoll::SHOW_RESULTS_AS_PIECHART) {
248 $chart = ilChart::getInstanceByType(ilChart::TYPE_PIE, "poll_results_pie_" . $this->getRefId());
249 $chart->setSize("400", "200");
250 $chart->setAutoResize(true);
251
252 $chart_data = $chart->getDataInstance();
253
254 foreach ($order as $answer_id) {
255 $chart_data->addPiePoint(
256 (int) round((float) ($perc[$answer_id]["perc"] ?? 0)),
257 nl2br((string) ($answers[$answer_id] ?? ''))
258 );
259 }
260
261 // disable legend, use inner labels - currently not preferred
262 // $chart_data->setLabelRadius(0.8);
263
264 $chart->addData($chart_data);
265
266 $pie_legend_id = "poll_legend_" . $this->getRefId();
267 $legend = new ilChartLegend();
268 $legend->setContainer($pie_legend_id);
269 $chart->setLegend($legend);
270
271 $this->tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
272 $this->tpl->setVariable("PIE_CHART", $chart->getHTML());
273 } // bar chart
274 else {
275 $this->tpl->setCurrentBlock("answer_result");
276 foreach ($order as $answer_id) {
278 $pbar->setCurrent(round((float) ($perc[$answer_id]["perc"] ?? 0)));
279 $this->tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
280 $this->tpl->setVariable("TXT_ANSWER_RESULT", nl2br((string) ($answers[$answer_id] ?? '')));
281 $this->tpl->parseCurrentBlock();
282 }
283 }
284 }
285 } else {
288 $end = $this->poll_block->getPoll()->getVotingPeriodEnd();
291
292 // #14607
293 $info = "";
294 if ($this->poll_block->getPoll()->hasUserVoted($this->user->getId())) {
295 $info .= $this->lng->txt("poll_block_message_already_voted") . " ";
296 }
297
298 $this->tpl->setVariable("TOTAL_ANSWERS", $info .
299 sprintf($this->lng->txt("poll_block_results_available_on"), $end));
300 }
301 } elseif ($this->poll_block->getPoll()->hasUserVoted($this->user->getId())) {
302 $this->tpl->setVariable("TOTAL_ANSWERS", $this->lng->txt("poll_block_message_already_voted"));
303 }
304 }
305
306 if (!$this->poll_block->maySeeQuestion($this->user->getId()) && !$this->poll_block->getPoll()->hasUserVoted($this->user->getId())) {
307 if ($this->poll_block->getPoll()->getVotingPeriod()) {
308 $this->tpl->setVariable(
309 "TXT_VOTING_PERIOD",
310 sprintf(
311 $this->lng->txt("poll_voting_period_full_info"),
312 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodBegin(), IL_CAL_UNIX)),
313 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
314 )
315 );
316 }
317 } else {
318 $this->tpl->setVariable("TXT_QUESTION", $this->specialCharsAsEntities(nl2br(trim($a_set->getQuestion()))));
319
320 $img = $a_set->getImageFullPath();
321 if ($img) {
322 $this->tpl->setVariable("URL_IMAGE", ilWACSignedPath::signFile($img));
323 }
324 }
325
326
327 $this->tpl->setVariable("ANCHOR_ID", $a_set->getID());
328
329 $desc = trim($a_set->getDescription());
330 if ($desc) {
331 $this->tpl->setVariable("TXT_DESC", $this->specialCharsAsEntities(nl2br($desc)));
332 }
333
334
335 if ($this->user->getId() == ANONYMOUS_USER_ID) {
336 $this->tpl->setCurrentBlock("anon_warning");
337 $this->tpl->setVariable("ANON_WARNING", $this->lng->txt('no_access_item_public'));
338 $this->tpl->parseCurrentBlock();
339 }
340
341
342 if ($this->poll_block->showComments()) {
343 $this->tpl->setCurrentBlock("comment_link");
344 $this->tpl->setVariable("LANG_COMMENTS", $this->lng->txt('poll_comments'));
345 $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
346 $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
347
348 $comments_count = $this->getNumberOfComments($this->getRefId());
349
350 if ($comments_count > 0) {
351 $this->tpl->setVariable("COMMENTS_COUNT", "(" . $comments_count . ")");
352 }
353
354 if (!self::$js_init) {
355 $redraw_url = $this->ctrl->getLinkTarget(
356 $this,
357 "getNumberOfCommentsForRedraw",
358 "",
359 true
360 );
361 $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
362
363 $this->main_tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
364 self::$js_init = true;
365 }
366 }
367 }
368
369 public function getHTML(): string
370 {
371 $this->poll_block->setRefId($this->getRefId());
372 $may_write = $this->access->checkAccess("write", "", $this->getRefId());
373 $has_content = $this->poll_block->hasAnyContent($this->user->getId(), $this->getRefId());
374
375 #22078 and 22079 it always contains something.
376 /*if(!$may_write && !$has_content)
377 {
378 return "";
379 }*/
380
381 $poll_obj = $this->poll_block->getPoll();
382 $this->setTitle($poll_obj->getTitle());
383 $this->setData(array(array($poll_obj)));
384
385 $this->ctrl->setParameterByClass(
387 "ref_id",
388 $this->getRefId()
389 );
390
391 if (
392 !$this->poll_block->getMessage($this->user->getId()) &&
393 !$this->user->isAnonymous()
394 ) {
395 // notification
396 if (ilNotification::hasNotification(ilNotification::TYPE_POLL, $this->user->getId(), $this->poll_block->getPoll()->getId())) {
397 $this->addBlockCommand(
398 $this->ctrl->getLinkTargetByClass(
399 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
400 "unsubscribe"
401 ),
402 $this->lng->txt("poll_notification_unsubscribe")
403 );
404 } else {
405 $this->addBlockCommand(
406 $this->ctrl->getLinkTargetByClass(
407 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
408 "subscribe"
409 ),
410 $this->lng->txt("poll_notification_subscribe")
411 );
412 }
413 }
414
415 if ($may_write) {
416 // edit
417 $this->addBlockCommand(
418 $this->ctrl->getLinkTargetByClass(
419 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
420 "render"
421 ),
422 $this->lng->txt("edit_content")
423 );
424 $this->addBlockCommand(
425 $this->ctrl->getLinkTargetByClass(
426 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
427 "edit"
428 ),
429 $this->lng->txt("settings")
430 );
431 }
432
433 $this->ctrl->clearParametersByClass($this->getRepositoryObjectGUIName());
434
435 return parent::getHTML();
436 }
437
441 private function commentJSCall(): string
442 {
443 $refId = $this->getRefId();
445
448 $refId,
449 "poll",
450 $objectId
451 );
452
453
454 return ilNoteGUI::getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(" . $refId . ");");
455 }
456
457 public function getNumberOfCommentsForRedraw(): void
458 {
459 global $DIC;
460
461 $poll_id = 0;
462 if ($this->http->wrapper()->query()->has('poll_id')) {
463 $poll_id = $this->http->wrapper()->query()->retrieve(
464 'poll_id',
465 $this->refinery->kindlyTo()->int()
466 );
467 }
468
469 $number = $this->getNumberOfComments($poll_id);
470
471 if ($number > 0) {
472 echo "(" . $number . ")";
473 } else {
474 echo "";
475 }
476
477 exit();
478 }
479
480 public function getNumberOfComments(int $ref_id): int
481 {
483 $context = $this->notes->data()->context($obj_id, 0, "poll");
484 return $this->notes->domain()->getNrOfCommentsForContext($context);
485 }
486
487 public function fillDataSection(): void
488 {
489 $this->setDataSection($this->getLegacyContent());
490 }
491
492 //
493 // New rendering
494 //
495
499 protected function getLegacyContent(): string
500 {
501 $this->tpl = new ilTemplate(
502 $this->getRowTemplateName(),
503 true,
504 true,
505 $this->getRowTemplateDir()
506 );
507 $this->fillRow(current($this->getData()));
508 return $this->tpl->get();
509 }
510}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
This class represents a block method of a block.
setData(array $a_data)
setDataSection(string $a_content)
Call this from overwritten fillDataSection(), if standard row based data is not used.
specialCharsAsEntities(string $string)
setTitle(string $a_title)
addBlockCommand(string $a_href, string $a_text, string $a_onclick="")
setBlockId(string $a_block_id="0")
setRowTemplate(string $a_rowtemplatename, string $a_rowtemplatedir="")
Set Row Template Name.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByType(int $a_type, string $a_id)
const TYPE_PIE
static buildAjaxHash(int $node_type, ?int $node_id, string $obj_type, int $obj_id, string $sub_type=null, int $sub_id=null, int $news_id=0)
Build ajax hash.
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
static getListCommentsJSCall(string $a_hash, string $a_update_code=null)
Get list comments js call.
static hasNotification(int $type, int $user_id, int $id)
Check notification status for object and user.
const SHOW_RESULTS_AS_PIECHART
static _lookupObjectId(int $ref_id)
BlockGUI class for polls.
GlobalHttpState $http
getLegacyContent()
Get legacy content.string
setBlock(ilPollBlock $a_block)
fillDataSection()
Standard implementation for row based data.
ViewManager $container_view_manager
ILIAS Notes Service $notes
isRepositoryObject()
Returns whether block has a corresponding repository object.
getNumberOfComments(int $ref_id)
static string $block_type
commentJSCall()
Builds JavaScript Call to open CommentLayer via html link.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
special template class to simplify handling of ITX/PEAR
static signFile(string $path_to_file)
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $DIC
Definition: feed.php:28
$img
Definition: imgupload.php:83
Interface GlobalHttpState.
exit
Definition: login.php:28
$ref_id
Definition: ltiauth.php:67
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$url
$context
Definition: webdav.php:29
$refId
Definition: xapitoken.php:58