ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilPollBlockGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/Block/classes/class.ilBlockGUI.php");
6 include_once("./Modules/Poll/classes/class.ilObjPoll.php");
7 
18 {
19  public static $block_type = "poll";
20 
21  protected $poll_block; // [ilPollBlock]
22 
23  public static $js_init = false;
24 
28  public function __construct()
29  {
30  global $DIC;
31 
32  $this->lng = $DIC->language();
33  $this->ctrl = $DIC->ctrl();
34  $this->user = $DIC->user();
35  $this->access = $DIC->access();
36  $lng = $DIC->language();
37 
39 
40  $lng->loadLanguageModule("poll");
41  $this->setRowTemplate("tpl.block.html", "Modules/Poll");
42  }
43 
47  public function getBlockType() : string
48  {
49  return self::$block_type;
50  }
51 
55  protected function isRepositoryObject() : bool
56  {
57  return true;
58  }
59 
65  protected function getRepositoryObjectGUIName()
66  {
67  return "ilobjpollgui";
68  }
69 
73  public static function getScreenMode()
74  {
75  return IL_SCREEN_SIDE;
76  }
77 
81  public function setBlock($a_block)
82  {
83  $this->setBlockId($a_block->getId());
84  $this->poll_block = $a_block;
85  }
86 
90  public function executeCommand()
91  {
92  $ilCtrl = $this->ctrl;
93 
94  $next_class = $ilCtrl->getNextClass();
95  $cmd = $ilCtrl->getCmd("getHTML");
96 
97  switch ($next_class) {
98  default:
99  return $this->$cmd();
100  }
101  }
102 
103  public function fillRow($a_poll)
104  {
105  $ilCtrl = $this->ctrl;
106  $lng = $this->lng;
109 
110 
111  // handle messages
112 
113  $mess = $this->poll_block->getMessage($ilUser->getId());
114  if ($mess) {
115  $this->tpl->setVariable("TXT_QUESTION", $mess);
116  return;
117  }
118 
119 
120  // nested form problem
121  if (!$_SESSION["il_cont_admin_panel"]) {
122  // vote
123 
124  if ($this->poll_block->maySeeQuestion($ilUser->getId())) {
125  $this->tpl->setCurrentBlock("mode_info_bl");
126  if ($this->poll_block->getPoll()->getNonAnonymous()) {
127  $mode_info = $lng->txt("poll_non_anonymous_warning");
128  } else {
129  $mode_info = $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  if (isset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()])) {
137  $last_vote = $_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()];
138  unset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()]);
139 
140  if ($is_multi_answer) {
141  $error = sprintf(
142  $lng->txt("poll_vote_error_multi"),
143  $this->poll_block->getPoll()->getMaxNumberOfAnswers()
144  );
145  } else {
146  $error = $lng->txt("poll_vote_error_single");
147  }
148 
149  $this->tpl->setCurrentBlock("error_bl");
150  $this->tpl->setVariable("FORM_ERROR", $error);
151  $this->tpl->parseCurrentBlock();
152  }
153 
154  $this->tpl->setCurrentBlock("answer");
155  foreach ($a_poll->getAnswers() as $item) {
156  $status = [];
157  if (!$is_multi_answer) {
158  $this->tpl->setVariable("ANSWER_INPUT", "radio");
159  $this->tpl->setVariable("ANSWER_NAME", "aw");
160  } else {
161  $this->tpl->setVariable("ANSWER_INPUT", "checkbox");
162  $this->tpl->setVariable("ANSWER_NAME", "aw[]");
163 
164  if (is_array($last_vote) && in_array($item["id"], $last_vote)) {
165  $status[] = 'checked="checked"';
166  }
167  }
168 
169  if (!$this->poll_block->mayVote($ilUser->getId())) {
170  $status[] = 'disabled';
171  }
172 
173  if (!empty($status)) {
174  $this->tpl->setVariable("ANSWER_STATUS", implode(' ', $status));
175  }
176 
177  $this->tpl->setVariable("VALUE_ANSWER", $item["id"]);
178  $this->tpl->setVariable("TXT_ANSWER_VOTE", nl2br($item["answer"]));
179  $this->tpl->parseCurrentBlock();
180  }
181 
182  if ($this->poll_block->mayVote($ilUser->getId())) {
183  $ilCtrl->setParameterByClass(
185  "ref_id",
186  $this->getRefId()
187  );
188  $url = $ilCtrl->getLinkTargetByClass(
189  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
190  "vote"
191  );
192  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
193 
194  $url .= "#poll" . $a_poll->getID();
195 
196  $this->tpl->setVariable("URL_FORM", $url);
197  $this->tpl->setVariable("CMD_FORM", "vote");
198  $this->tpl->setVariable("TXT_SUBMIT", $lng->txt("poll_vote"));
199  }
200 
201  if ($this->poll_block->getPoll()->getVotingPeriod()) {
202  $this->tpl->setVariable(
203  "TXT_VOTING_END_PERIOD",
204  sprintf(
205  $lng->txt("poll_voting_period_info"),
206  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
207  )
208  );
209  }
210  }
211 
212 
213  // result
214  if ($this->poll_block->maySeeResults($ilUser->getId())) {
215  if (!$this->poll_block->mayNotResultsYet($ilUser->getId())) {
216  $answers = array();
217  foreach ($a_poll->getAnswers() as $item) {
218  $answers[$item["id"]] = $item["answer"];
219  }
220 
221  $perc = $this->poll_block->getPoll()->getVotePercentages();
222  $total = $perc["total"];
223  $perc = $perc["perc"];
224 
225  $this->tpl->setVariable("TOTAL_ANSWERS", sprintf($lng->txt("poll_population"), $total));
226 
227  if ($total) {
228  // sort results by votes / original position
229  if ($this->poll_block->getPoll()->getSortResultByVotes()) {
230  $order = array_keys(ilUtil::sortArray($perc, "abs", "desc", true, true));
231 
232  foreach (array_keys($answers) as $answer_id) {
233  if (!in_array($answer_id, $order)) {
234  $order[] = $answer_id;
235  }
236  }
237  } else {
238  $order = array_keys($answers);
239  }
240 
241  // pie chart
242  if ($this->poll_block->showResultsAs() == ilObjPoll::SHOW_RESULTS_AS_PIECHART) {
243  include_once("./Services/Chart/classes/class.ilChart.php");
244 
245  $chart = ilChart::getInstanceByType(ilCHart::TYPE_PIE, "poll_results_pie_" . $this->getRefId());
246  $chart->setSize("100%", 200);
247  $chart->setAutoResize(true);
248 
249  $chart_data = $chart->getDataInstance();
250 
251  foreach ($order as $answer_id) {
252  $chart_data->addPoint(
253  round($perc[$answer_id]["perc"]),
254  nl2br($answers[$answer_id])
255  );
256  }
257 
258  // disable legend, use inner labels - currently not preferred
259  // $chart_data->setLabelRadius(0.8);
260 
261  $chart->addData($chart_data);
262 
263  $pie_legend_id = "poll_legend_" . $this->getRefId();
264  $legend = new ilChartLegend();
265  $legend->setContainer($pie_legend_id);
266  $chart->setLegend($legend);
267 
268  $this->tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
269  $this->tpl->setVariable("PIE_CHART", $chart->getHTML());
270  } // bar chart
271  else {
272  include_once "Services/UIComponent/ProgressBar/classes/class.ilProgressBar.php";
273 
274  $this->tpl->setCurrentBlock("answer_result");
275  foreach ($order as $answer_id) {
276  $pbar = ilProgressBar::getInstance();
277  $pbar->setCurrent(round($perc[$answer_id]["perc"]));
278  $this->tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
279  $this->tpl->setVariable("TXT_ANSWER_RESULT", nl2br($answers[$answer_id]));
280  $this->tpl->parseCurrentBlock();
281  }
282  }
283  }
284  } else {
287  $end = $this->poll_block->getPoll()->getVotingPeriodEnd();
290 
291  // #14607
292  $info = "";
293  if ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
294  $info .= $lng->txt("poll_block_message_already_voted") . " ";
295  }
296 
297  $this->tpl->setVariable("TOTAL_ANSWERS", $info .
298  sprintf($lng->txt("poll_block_results_available_on"), $end));
299  }
300  } elseif ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
301  $this->tpl->setVariable("TOTAL_ANSWERS", $lng->txt("poll_block_message_already_voted"));
302  }
303  }
304 
305  if (!$this->poll_block->maySeeQuestion($ilUser->getId()) && !$this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
306  if ($this->poll_block->getPoll()->getVotingPeriod()) {
307  $this->tpl->setVariable(
308  "TXT_VOTING_PERIOD",
309  sprintf(
310  $lng->txt("poll_voting_period_full_info"),
311  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodBegin(), IL_CAL_UNIX)),
312  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
313  )
314  );
315  }
316  } else {
317  $this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
318 
319  $img = $a_poll->getImageFullPath();
320  if ($img) {
321  require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
322  $this->tpl->setVariable("URL_IMAGE", ilWACSignedPath::signFile($img));
323  }
324  }
325 
326 
327  $this->tpl->setVariable("ANCHOR_ID", $a_poll->getID());
328  //$this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
329 
330  $desc = trim($a_poll->getDescription());
331  if ($desc) {
332  $this->tpl->setVariable("TXT_DESC", nl2br($desc));
333  }
334 
335 
336  if ($ilUser->getId() == ANONYMOUS_USER_ID) {
337  $this->tpl->setCurrentBlock("anon_warning");
338  $this->tpl->setVariable("ANON_WARNING", $this->lng->txt('no_access_item_public'));
339  $this->tpl->parseCurrentBlock();
340  }
341 
342 
343  if ($this->poll_block->showComments()) {
344  $this->tpl->setCurrentBlock("comment_link");
345  $this->tpl->setVariable("LANG_COMMENTS", $lng->txt('poll_comments'));
346  $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
347  $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
348 
349  $comments_count = $this->getNumberOfComments($this->getRefId());
350 
351  if ($comments_count > 0) {
352  $this->tpl->setVariable("COMMENTS_COUNT", "(" . $comments_count . ")");
353  }
354 
355  if (!self::$js_init) {
356  $redraw_url = $ilCtrl->getLinkTarget(
357  $this,
358  "getNumberOfCommentsForRedraw",
359  "",
360  true,
361  false
362  );
363  $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
364 
365  $tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
366  self::$js_init = true;
367  }
368  }
369  }
370 
374  public function getHTML()
375  {
376  $ilCtrl = $this->ctrl;
377  $lng = $this->lng;
378  $ilAccess = $this->access;
380 
381  $this->poll_block->setRefId($this->getRefId());
382  $this->may_write = $ilAccess->checkAccess("write", "", $this->getRefId());
383  $this->has_content = $this->poll_block->hasAnyContent($ilUser->getId(), $this->getRefId());
384 
385  #22078 and 22079 it always contains something.
386  /*if(!$this->may_write && !$this->has_content)
387  {
388  return "";
389  }*/
390 
391  $poll_obj = $this->poll_block->getPoll();
392  $this->setTitle($poll_obj->getTitle());
393  $this->setData(array($poll_obj));
394 
395  $ilCtrl->setParameterByClass(
397  "ref_id",
398  $this->getRefId()
399  );
400 
401  if (!$this->poll_block->getMessage($ilUser->getId())) {
402  // notification
403  include_once "./Services/Notification/classes/class.ilNotification.php";
404  if (ilNotification::hasNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->poll_block->getPoll()->getId())) {
405  $this->addBlockCommand(
406  $ilCtrl->getLinkTargetByClass(
407  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
408  "unsubscribe"
409  ),
410  $lng->txt("poll_notification_unsubscribe")
411  );
412  } else {
413  $this->addBlockCommand(
414  $ilCtrl->getLinkTargetByClass(
415  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
416  "subscribe"
417  ),
418  $lng->txt("poll_notification_subscribe")
419  );
420  }
421  }
422 
423  if ($this->may_write) {
424  // edit
425  $this->addBlockCommand(
426  $ilCtrl->getLinkTargetByClass(
427  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
428  "render"
429  ),
430  $lng->txt("edit_content")
431  );
432  $this->addBlockCommand(
433  $ilCtrl->getLinkTargetByClass(
434  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
435  "edit"
436  ),
437  $lng->txt("settings")
438  );
439  }
440 
441  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
442 
443  return parent::getHTML();
444  }
445 
451  private function commentJSCall()
452  {
453  include_once("./Services/Notes/classes/class.ilNoteGUI.php");
454  include_once("./Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
455 
456  $refId = $this->getRefId();
457  $objectId = ilObject2::_lookupObjectId($refId);
458 
461  $refId,
462  "poll",
463  $objectId
464  );
465 
466 
467  $comment = new ilNoteGUI();
468  $jsCall = $comment->getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(" . $refId . ");");
469 
470  return $jsCall;
471  }
472 
477  {
478  $number = $this->getNumberOfComments($_GET["poll_id"]);
479 
480  if ($number > 0) {
481  echo "(" . $number . ")";
482  } else {
483  echo "";
484  }
485 
486  exit();
487  }
488 
495  public function getNumberOfComments($ref_id)
496  {
497  include_once("./Services/Notes/classes/class.ilNote.php");
498 
499  $obj_id = ilObject2::_lookupObjectId($ref_id);
500  $number = ilNote::_countNotesAndComments($obj_id);
501 
502  if (count($number) == 0) {
503  return 0;
504  }
505 
506  return $number[$obj_id][IL_NOTE_PUBLIC];
507  }
508 
512  public function fillDataSection()
513  {
514  $this->setDataSection($this->getLegacyContent());
515  }
516 
517  //
518  // New rendering
519  //
520 
521  protected $new_rendering = true;
522 
523 
527  protected function getLegacyContent() : string
528  {
529  $this->tpl = new ilTemplate(
530  $this->getRowTemplateName(),
531  true,
532  true,
533  $this->getRowTemplateDir()
534  );
535  $this->fillRow(current($this->getData()));
536  return $this->tpl->get();
537  }
538 }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
executeCommand()
execute command
static hasNotification($type, $user_id, $id)
Check notification status for object and user.
exit
Definition: login.php:29
fillDataSection()
Fill data section.
getRowTemplateDir()
Get Row Template Directory.
commentJSCall()
Builds JavaScript Call to open CommentLayer via html link.
const ANONYMOUS_USER_ID
Definition: constants.php:25
$_SESSION["AccountId"]
Chart legend.
setDataSection($a_content)
Call this from overwritten fillDataSection(), if standard row based data is not used.
$_GET["client_id"]
getNumberOfComments($ref_id)
Get comment count.
const SHOW_RESULTS_AS_PIECHART
getRepositoryObjectGUIName()
Get repository object GUI name.
static _lookupObjectId($a_ref_id)
static setUseRelativeDates($a_status)
set use relative dates
$refId
Definition: xapitoken.php:40
const IL_CAL_UNIX
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
user()
Definition: user.php:4
$total
Definition: Utf8Test.php:87
static useRelativeDates()
check if relative dates are used
getHTML()
Get block HTML code.
Notes GUI class.
static getInstance()
Factory.
setTitle($a_title)
Set Title.
static _countNotesAndComments( $a_rep_obj_id, $a_sub_obj_id=null, $a_obj_type="", $a_news_id=0)
Get all notes related to a specific object.
setBlockId($a_block_id=0)
Set Block Id.
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:6
setData($a_data)
Set Data.
global $DIC
Definition: goto.php:24
static signFile($path_to_file)
$comment
Definition: buildRTE.php:83
static buildAjaxHash( $a_node_type, $a_node_id, $a_obj_type, $a_obj_id, $a_sub_type=null, $a_sub_id=null, $a_news_id=0)
Build ajax hash.
$img
Definition: imgupload.php:57
getData()
Get Data.
getRefId()
Get Ref Id (only used if isRepositoryObject() is true).
BlockGUI class for polls.
__construct(Container $dic, ilPlugin $plugin)
getRowTemplateName()
Get Row Template Name.
getNumberOfCommentsForRedraw()
Returns comment count for JS Redraw.
This class represents a block method of a block.
$url
$ilUser
Definition: imgupload.php:18
addBlockCommand(string $a_href, string $a_text, string $a_onclick="")
Add Block Command.
const IL_SCREEN_SIDE
__construct()
Constructor.
setRowTemplate($a_rowtemplatename, $a_rowtemplatedir="")
Set Row Template Name.
static getScreenMode()
Get Screen Mode for current command.
static getInstanceByType($a_type, $a_id)
Get type instance.
setBlock($a_block)
Do most of the initialisation.