ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  static $block_type = "poll";
20 
21  protected $poll_block; // [ilPollBlock]
22 
23  static $js_init = false;
24 
28  function __construct()
29  {
30  global $lng;
31 
33 
34  $lng->loadLanguageModule("poll");
35  $this->setRowTemplate("tpl.block.html", "Modules/Poll");
36  }
37 
43  static function getBlockType()
44  {
45  return self::$block_type;
46  }
47 
53  static function isRepositoryObject()
54  {
55  return true;
56  }
57 
63  protected function getRepositoryObjectGUIName()
64  {
65  return "ilobjpollgui";
66  }
67 
71  static function getScreenMode()
72  {
73  return IL_SCREEN_SIDE;
74  }
75 
79  function setBlock($a_block)
80  {
81  $this->setBlockId($a_block->getId());
82  $this->poll_block = $a_block;
83  }
84 
88  function &executeCommand()
89  {
90  global $ilCtrl;
91 
92  $next_class = $ilCtrl->getNextClass();
93  $cmd = $ilCtrl->getCmd("getHTML");
94 
95  switch ($next_class)
96  {
97  default:
98  return $this->$cmd();
99  }
100  }
101 
102  function fillRow($a_poll)
103  {
104  global $ilCtrl, $lng, $ilUser, $tpl;
105 
106 
107  // handle messages
108 
109  $mess = $this->poll_block->getMessage($ilUser->getId());
110  if($mess)
111  {
112  $this->tpl->setVariable("TXT_QUESTION", $mess);
113  return;
114  }
115 
116 
117  // nested form problem
118  if(!$_SESSION["il_cont_admin_panel"])
119  {
120  // vote
121 
122  if($this->poll_block->mayVote($ilUser->getId()))
123  {
124  $this->tpl->setCurrentBlock("mode_info_bl");
125  if($this->poll_block->getPoll()->getNonAnonymous())
126  {
127  $mode_info = $lng->txt("poll_non_anonymous_warning");
128  }
129  else
130  {
131  $mode_info = $lng->txt("poll_anonymous_warning");
132  }
133  $this->tpl->setVariable("MODE_INFO", $mode_info);
134  $this->tpl->parseCurrentBlock();
135 
136  $is_multi_answer = ($this->poll_block->getPoll()->getMaxNumberOfAnswers() > 1);
137 
138  if(isset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()]))
139  {
140  $last_vote = $_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()];
141  unset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()]);
142 
143  if($is_multi_answer)
144  {
145  $error = sprintf($lng->txt("poll_vote_error_multi"),
146  $this->poll_block->getPoll()->getMaxNumberOfAnswers());
147  }
148  else
149  {
150  $error = $lng->txt("poll_vote_error_single");
151  }
152 
153  $this->tpl->setCurrentBlock("error_bl");
154  $this->tpl->setVariable("FORM_ERROR", $error);
155  $this->tpl->parseCurrentBlock();
156  }
157 
158  $this->tpl->setCurrentBlock("answer");
159  foreach($a_poll->getAnswers() as $item)
160  {
161  if(!$is_multi_answer)
162  {
163  $this->tpl->setVariable("ANSWER_INPUT", "radio");
164  $this->tpl->setVariable("ANSWER_NAME", "aw");
165  }
166  else
167  {
168  $this->tpl->setVariable("ANSWER_INPUT", "checkbox");
169  $this->tpl->setVariable("ANSWER_NAME", "aw[]");
170 
171  if(is_array($last_vote) && in_array($item["id"], $last_vote))
172  {
173  $this->tpl->setVariable("ANSWER_STATUS", 'checked="checked"');
174  }
175  }
176  $this->tpl->setVariable("VALUE_ANSWER", $item["id"]);
177  $this->tpl->setVariable("TXT_ANSWER_VOTE", nl2br($item["answer"]));
178  $this->tpl->parseCurrentBlock();
179  }
180 
181  $ilCtrl->setParameterByClass($this->getRepositoryObjectGUIName(),
182  "ref_id", $this->getRefId());
183  $url = $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
184  "vote");
185  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
186 
187  $url .= "#poll".$a_poll->getID();
188 
189  $this->tpl->setVariable("URL_FORM", $url);
190  $this->tpl->setVariable("CMD_FORM", "vote");
191  $this->tpl->setVariable("TXT_SUBMIT", $lng->txt("poll_vote"));
192 
193  if($this->poll_block->getPoll()->getVotingPeriod())
194  {
195  $this->tpl->setVariable("TXT_VOTING_PERIOD",
196  sprintf($lng->txt("poll_voting_period_info"),
197  ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))));
198  }
199  }
200 
201 
202  // result
203  if ($this->poll_block->maySeeResults($ilUser->getId()))
204  {
205  if (!$this->poll_block->mayNotResultsYet($ilUser->getId()))
206  {
207  $answers = array();
208  foreach ($a_poll->getAnswers() as $item)
209  {
210  $answers[$item["id"]] = $item["answer"];
211  }
212 
213  $perc = $this->poll_block->getPoll()->getVotePercentages();
214  $total = $perc["total"];
215  $perc = $perc["perc"];
216 
217  $this->tpl->setVariable("TOTAL_ANSWERS", sprintf($lng->txt("poll_population"), $total));
218 
219  if($total)
220  {
221  // sort results by votes / original position
222  if ($this->poll_block->getPoll()->getSortResultByVotes())
223  {
224  $order = array_keys(ilUtil::sortArray($perc, "abs", "desc", true, true));
225 
226  foreach (array_keys($answers) as $answer_id)
227  {
228  if (!in_array($answer_id, $order))
229  {
230  $order[] = $answer_id;
231  }
232  }
233  }
234  else
235  {
236  $order = array_keys($answers);
237  }
238 
239  // pie chart
240  if ($this->poll_block->showResultsAs() == ilObjPoll::SHOW_RESULTS_AS_PIECHART)
241  {
242 
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  {
253  $chart_data->addPoint(
254  round($perc[$answer_id]["perc"]),
255  nl2br($answers[$answer_id])
256  );
257  }
258 
259  // disable legend, use inner labels - currently not preferred
260  // $chart_data->setLabelRadius(0.8);
261 
262  $chart->addData($chart_data);
263 
264  $pie_legend_id = "poll_legend_".$this->getRefId();
265  $legend = new ilChartLegend();
266  $legend->setContainer($pie_legend_id);
267  $chart->setLegend($legend);
268 
269  $this->tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
270  $this->tpl->setVariable("PIE_CHART", $chart->getHTML());
271  }
272  // bar chart
273  else
274  {
275  include_once "Services/UIComponent/ProgressBar/classes/class.ilProgressBar.php";
276 
277  $this->tpl->setCurrentBlock("answer_result");
278  foreach ($order as $answer_id)
279  {
280  $pbar = ilProgressBar::getInstance();
281  $pbar->setCurrent(round($perc[$answer_id]["perc"]));
282  $this->tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
283  $this->tpl->setVariable("TXT_ANSWER_RESULT", nl2br($answers[$answer_id]));
284  $this->tpl->parseCurrentBlock();
285  }
286  }
287  }
288  }
289  else
290  {
293  $end = $this->poll_block->getPoll()->getVotingPeriodEnd();
296 
297  // #14607
298  $info = "";
299  if($this->poll_block->getPoll()->hasUserVoted($ilUser->getId()))
300  {
301  $info .= $lng->txt("poll_block_message_already_voted")." ";
302  }
303 
304  $this->tpl->setVariable("TOTAL_ANSWERS", $info.
305  sprintf($lng->txt("poll_block_results_available_on"), $end));
306  }
307  }
308  else if($this->poll_block->getPoll()->hasUserVoted($ilUser->getId()))
309  {
310  $this->tpl->setVariable("TOTAL_ANSWERS", $lng->txt("poll_block_message_already_voted"));
311  }
312  }
313 
314 
315  $this->tpl->setVariable("ANCHOR_ID", $a_poll->getID());
316  $this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
317 
318  $desc = trim($a_poll->getDescription());
319  if($desc)
320  {
321  $this->tpl->setVariable("TXT_DESC", nl2br($desc));
322  }
323 
324  $img = $a_poll->getImageFullPath();
325  if($img)
326  {
327  $this->tpl->setVariable("URL_IMAGE", $img);
328  }
329 
330  if ($this->poll_block->showComments()) {
331  $this->tpl->setCurrentBlock("comment_link");
332  $this->tpl->setVariable("LANG_COMMENTS", $lng->txt('poll_comments'));
333  $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
334  $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
335 
336  $comments_count = $this->getNumberOfComments($this->getRefId());
337 
338  if($comments_count > 0)
339  {
340  $this->tpl->setVariable("COMMENTS_COUNT", "(".$comments_count.")");
341  }
342 
343  if(!self::$js_init)
344  {
345  $redraw_url = $ilCtrl->getLinkTarget($this, "getNumberOfCommentsForRedraw",
346  "", true, false);
347  $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
348 
349  $tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
350  self::$js_init = true;
351  }
352  }
353 
354  }
355 
359  function getHTML()
360  {
361  global $ilCtrl, $lng, $ilAccess, $ilUser;
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  if(!$this->may_write && !$this->has_content)
368  {
369  return "";
370  }
371 
372  $poll_obj = $this->poll_block->getPoll();
373  $this->setTitle($poll_obj->getTitle());
374  $this->setData(array($poll_obj));
375 
376  $ilCtrl->setParameterByClass($this->getRepositoryObjectGUIName(),
377  "ref_id", $this->getRefId());
378 
379  if(!$this->poll_block->getMessage($ilUser->getId()))
380  {
381  // notification
382  include_once "./Services/Notification/classes/class.ilNotification.php";
383  if(ilNotification::hasNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->poll_block->getPoll()->getId()))
384  {
385  $this->addBlockCommand(
386  $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
387  "unsubscribe"),
388  $lng->txt("poll_notification_unsubscribe"));
389  }
390  else
391  {
392  $this->addBlockCommand(
393  $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
394  "subscribe"),
395  $lng->txt("poll_notification_subscribe"));
396  }
397  }
398 
399  if ($this->may_write)
400  {
401  // edit
402  $this->addBlockCommand(
403  $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
404  "render"),
405  $lng->txt("edit_content"));
406  $this->addBlockCommand(
407  $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
408  "edit"),
409  $lng->txt("settings"));
410 
411  /* delete (#10993 - see ilBlockGUI)
412  $parent_id = $tree->getParentId($this->getRefId());
413  $type = ilObject::_lookupType($parent_id, true);
414  $class = $objDefinition->getClassName($type);
415  if($class)
416  {
417  $class = "ilobj".strtolower($class)."gui";
418  $ilCtrl->setParameterByClass($class, "ref_id", $parent_id);
419  $ilCtrl->setParameterByClass($class, "item_ref_id", $this->getRefId());
420  $this->addBlockCommand(
421  $ilCtrl->getLinkTargetByClass($class, "delete"),
422  $lng->txt("delete"));
423  }
424  */
425  }
426 
427  $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
428 
429  return parent::getHTML();
430  }
431 
437  private function commentJSCall()
438  {
439  include_once("./Services/Notes/classes/class.ilNoteGUI.php");
440  include_once("./Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
441 
442  $refId = $this->getRefId();
443  $objectId = ilObject2::_lookupObjectId($refId);
444 
446  ilCommonActionDispatcherGUI::TYPE_REPOSITORY, $refId, "poll", $objectId);
447 
448 
449  $comment = new ilNoteGUI();
450  $jsCall = $comment->getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(".$refId.");");
451 
452  return $jsCall;
453  }
454 
459  {
460  $number = $this->getNumberOfComments($_GET["poll_id"]);
461 
462  if($number > 0)
463  {
464  echo "(".$number.")";
465  }
466  else
467  {
468  echo "";
469  }
470 
471  exit();
472  }
473 
480  public function getNumberOfComments($ref_id)
481  {
482  include_once("./Services/Notes/classes/class.ilNote.php");
483 
485  $number = ilNote::_countNotesAndComments($obj_id);
486 
487  if(count($number) == 0)
488  {
489  return 0;
490  }
491 
492  return $number[$obj_id][IL_NOTE_PUBLIC];
493  }
494 }
495 
496 ?>