ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
38  parent::__construct();
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  {
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  {
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->mayVote($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  if (!$is_multi_answer) {
157  $this->tpl->setVariable("ANSWER_INPUT", "radio");
158  $this->tpl->setVariable("ANSWER_NAME", "aw");
159  } else {
160  $this->tpl->setVariable("ANSWER_INPUT", "checkbox");
161  $this->tpl->setVariable("ANSWER_NAME", "aw[]");
162 
163  if (is_array($last_vote) && in_array($item["id"], $last_vote)) {
164  $this->tpl->setVariable("ANSWER_STATUS", 'checked="checked"');
165  }
166  }
167  $this->tpl->setVariable("VALUE_ANSWER", $item["id"]);
168  $this->tpl->setVariable("TXT_ANSWER_VOTE", nl2br($item["answer"]));
169  $this->tpl->parseCurrentBlock();
170  }
171 
172  $ilCtrl->setParameterByClass(
174  "ref_id",
175  $this->getRefId()
176  );
177  $url = $ilCtrl->getLinkTargetByClass(
178  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
179  "vote"
180  );
181  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
182 
183  $url .= "#poll" . $a_poll->getID();
184 
185  $this->tpl->setVariable("URL_FORM", $url);
186  $this->tpl->setVariable("CMD_FORM", "vote");
187  $this->tpl->setVariable("TXT_SUBMIT", $lng->txt("poll_vote"));
188 
189  if ($this->poll_block->getPoll()->getVotingPeriod()) {
190  $this->tpl->setVariable(
191  "TXT_VOTING_END_PERIOD",
192  sprintf(
193  $lng->txt("poll_voting_period_info"),
194  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
195  )
196  );
197  }
198  }
199 
200 
201  // result
202  if ($this->poll_block->maySeeResults($ilUser->getId())) {
203  if (!$this->poll_block->mayNotResultsYet($ilUser->getId())) {
204  $answers = array();
205  foreach ($a_poll->getAnswers() as $item) {
206  $answers[$item["id"]] = $item["answer"];
207  }
208 
209  $perc = $this->poll_block->getPoll()->getVotePercentages();
210  $total = $perc["total"];
211  $perc = $perc["perc"];
212 
213  $this->tpl->setVariable("TOTAL_ANSWERS", sprintf($lng->txt("poll_population"), $total));
214 
215  if ($total) {
216  // sort results by votes / original position
217  if ($this->poll_block->getPoll()->getSortResultByVotes()) {
218  $order = array_keys(ilUtil::sortArray($perc, "abs", "desc", true, true));
219 
220  foreach (array_keys($answers) as $answer_id) {
221  if (!in_array($answer_id, $order)) {
222  $order[] = $answer_id;
223  }
224  }
225  } else {
226  $order = array_keys($answers);
227  }
228 
229  // pie chart
230  if ($this->poll_block->showResultsAs() == ilObjPoll::SHOW_RESULTS_AS_PIECHART) {
231  include_once("./Services/Chart/classes/class.ilChart.php");
232 
233  $chart = ilChart::getInstanceByType(ilCHart::TYPE_PIE, "poll_results_pie_" . $this->getRefId());
234  $chart->setSize("100%", 200);
235  $chart->setAutoResize(true);
236 
237  $chart_data = $chart->getDataInstance();
238 
239  foreach ($order as $answer_id) {
240  $chart_data->addPoint(
241  round($perc[$answer_id]["perc"]),
242  nl2br($answers[$answer_id])
243  );
244  }
245 
246  // disable legend, use inner labels - currently not preferred
247  // $chart_data->setLabelRadius(0.8);
248 
249  $chart->addData($chart_data);
250 
251  $pie_legend_id = "poll_legend_" . $this->getRefId();
252  $legend = new ilChartLegend();
253  $legend->setContainer($pie_legend_id);
254  $chart->setLegend($legend);
255 
256  $this->tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
257  $this->tpl->setVariable("PIE_CHART", $chart->getHTML());
258  }
259  // bar chart
260  else {
261  include_once "Services/UIComponent/ProgressBar/classes/class.ilProgressBar.php";
262 
263  $this->tpl->setCurrentBlock("answer_result");
264  foreach ($order as $answer_id) {
265  $pbar = ilProgressBar::getInstance();
266  $pbar->setCurrent(round($perc[$answer_id]["perc"]));
267  $this->tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
268  $this->tpl->setVariable("TXT_ANSWER_RESULT", nl2br($answers[$answer_id]));
269  $this->tpl->parseCurrentBlock();
270  }
271  }
272  }
273  } else {
276  $end = $this->poll_block->getPoll()->getVotingPeriodEnd();
279 
280  // #14607
281  $info = "";
282  if ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
283  $info .= $lng->txt("poll_block_message_already_voted") . " ";
284  }
285 
286  $this->tpl->setVariable("TOTAL_ANSWERS", $info .
287  sprintf($lng->txt("poll_block_results_available_on"), $end));
288  }
289  } elseif ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
290  $this->tpl->setVariable("TOTAL_ANSWERS", $lng->txt("poll_block_message_already_voted"));
291  }
292  }
293 
294  if (!$this->poll_block->mayVote($ilUser->getId()) && !$this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
295  if ($this->poll_block->getPoll()->getVotingPeriod()) {
296  $this->tpl->setVariable(
297  "TXT_VOTING_PERIOD",
298  sprintf(
299  $lng->txt("poll_voting_period_full_info"),
300  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodBegin(), IL_CAL_UNIX)),
301  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
302  )
303  );
304  }
305  } else {
306  $this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
307 
308  $img = $a_poll->getImageFullPath();
309  if ($img) {
310  require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
311  $this->tpl->setVariable("URL_IMAGE", ilWACSignedPath::signFile($img));
312  }
313  }
314 
315 
316  $this->tpl->setVariable("ANCHOR_ID", $a_poll->getID());
317  //$this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
318 
319  $desc = trim($a_poll->getDescription());
320  if ($desc) {
321  $this->tpl->setVariable("TXT_DESC", nl2br($desc));
322  }
323 
324 
325  if ($this->poll_block->showComments()) {
326  $this->tpl->setCurrentBlock("comment_link");
327  $this->tpl->setVariable("LANG_COMMENTS", $lng->txt('poll_comments'));
328  $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
329  $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
330 
331  $comments_count = $this->getNumberOfComments($this->getRefId());
332 
333  if ($comments_count > 0) {
334  $this->tpl->setVariable("COMMENTS_COUNT", "(" . $comments_count . ")");
335  }
336 
337  if (!self::$js_init) {
338  $redraw_url = $ilCtrl->getLinkTarget(
339  $this,
340  "getNumberOfCommentsForRedraw",
341  "",
342  true,
343  false
344  );
345  $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
346 
347  $tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
348  self::$js_init = true;
349  }
350  }
351  }
352 
356  public function getHTML()
357  {
359  $lng = $this->lng;
360  $ilAccess = $this->access;
362 
363  $this->poll_block->setRefId($this->getRefId());
364  $this->may_write = $ilAccess->checkAccess("write", "", $this->getRefId());
365  $this->has_content = $this->poll_block->hasAnyContent($ilUser->getId(), $this->getRefId());
366 
367  #22078 and 22079 it always contains something.
368  /*if(!$this->may_write && !$this->has_content)
369  {
370  return "";
371  }*/
372 
373  $poll_obj = $this->poll_block->getPoll();
374  $this->setTitle($poll_obj->getTitle());
375  $this->setData(array($poll_obj));
376 
377  $ilCtrl->setParameterByClass(
379  "ref_id",
380  $this->getRefId()
381  );
382 
383  if (!$this->poll_block->getMessage($ilUser->getId())) {
384  // notification
385  include_once "./Services/Notification/classes/class.ilNotification.php";
386  if (ilNotification::hasNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->poll_block->getPoll()->getId())) {
387  $this->addBlockCommand(
388  $ilCtrl->getLinkTargetByClass(
389  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
390  "unsubscribe"
391  ),
392  $lng->txt("poll_notification_unsubscribe")
393  );
394  } else {
395  $this->addBlockCommand(
396  $ilCtrl->getLinkTargetByClass(
397  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
398  "subscribe"
399  ),
400  $lng->txt("poll_notification_subscribe")
401  );
402  }
403  }
404 
405  if ($this->may_write) {
406  // edit
407  $this->addBlockCommand(
408  $ilCtrl->getLinkTargetByClass(
409  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
410  "render"
411  ),
412  $lng->txt("edit_content")
413  );
414  $this->addBlockCommand(
415  $ilCtrl->getLinkTargetByClass(
416  array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
417  "edit"
418  ),
419  $lng->txt("settings")
420  );
421 
422  /* delete (#10993 - see ilBlockGUI)
423  $parent_id = $tree->getParentId($this->getRefId());
424  $type = ilObject::_lookupType($parent_id, true);
425  $class = $objDefinition->getClassName($type);
426  if($class)
427  {
428  $class = "ilobj".strtolower($class)."gui";
429  $ilCtrl->setParameterByClass($class, "ref_id", $parent_id);
430  $ilCtrl->setParameterByClass($class, "item_ref_id", $this->getRefId());
431  $this->addBlockCommand(
432  $ilCtrl->getLinkTargetByClass($class, "delete"),
433  $lng->txt("delete"));
434  }
435  */
436  }
437 
438  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
439 
440  return parent::getHTML();
441  }
442 
448  private function commentJSCall()
449  {
450  include_once("./Services/Notes/classes/class.ilNoteGUI.php");
451  include_once("./Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
452 
453  $refId = $this->getRefId();
454  $objectId = ilObject2::_lookupObjectId($refId);
455 
458  $refId,
459  "poll",
460  $objectId
461  );
462 
463 
464  $comment = new ilNoteGUI();
465  $jsCall = $comment->getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(" . $refId . ");");
466 
467  return $jsCall;
468  }
469 
474  {
475  $number = $this->getNumberOfComments($_GET["poll_id"]);
476 
477  if ($number > 0) {
478  echo "(" . $number . ")";
479  } else {
480  echo "";
481  }
482 
483  exit();
484  }
485 
492  public function getNumberOfComments($ref_id)
493  {
494  include_once("./Services/Notes/classes/class.ilNote.php");
495 
496  $obj_id = ilObject2::_lookupObjectId($ref_id);
497  $number = ilNote::_countNotesAndComments($obj_id);
498 
499  if (count($number) == 0) {
500  return 0;
501  }
502 
503  return $number[$obj_id][IL_NOTE_PUBLIC];
504  }
505 }
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.
commentJSCall()
Builds JavaScript Call to open CommentLayer via html link.
$_SESSION["AccountId"]
global $DIC
Definition: saml.php:7
Chart legend.
$_GET["client_id"]
$legend
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
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.
global $ilCtrl
Definition: ilias.php:18
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.
Date and time handling
$ilUser
Definition: imgupload.php:18
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.
addBlockCommand( $a_href, $a_text, $a_target="", $a_img="", $a_right_aligned=false, $a_checked=false, $a_html="")
Add Block Command.
exit
Definition: backend.php:16
getRefId()
Get Ref Id (only used if isRepositoryObject() is true).
BlockGUI class for polls.
getNumberOfCommentsForRedraw()
Returns comment count for JS Redraw.
This class represents a block method of a block.
$url
$info
Definition: index.php:5
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.