ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
5include_once("./Services/Block/classes/class.ilBlockGUI.php");
6include_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
49 public static function getBlockType()
50 {
51 return self::$block_type;
52 }
53
59 public static function isRepositoryObject()
60 {
61 return true;
62 }
63
69 protected function getRepositoryObjectGUIName()
70 {
71 return "ilobjpollgui";
72 }
73
77 public static function getScreenMode()
78 {
79 return IL_SCREEN_SIDE;
80 }
81
85 public function setBlock($a_block)
86 {
87 $this->setBlockId($a_block->getId());
88 $this->poll_block = $a_block;
89 }
90
94 public function executeCommand()
95 {
97
98 $next_class = $ilCtrl->getNextClass();
99 $cmd = $ilCtrl->getCmd("getHTML");
100
101 switch ($next_class) {
102 default:
103 return $this->$cmd();
104 }
105 }
106
107 public function fillRow($a_poll)
108 {
113
114
115 // handle messages
116
117 $mess = $this->poll_block->getMessage($ilUser->getId());
118 if ($mess) {
119 $this->tpl->setVariable("TXT_QUESTION", $mess);
120 return;
121 }
122
123
124 // nested form problem
125 if (!$_SESSION["il_cont_admin_panel"]) {
126 // vote
127
128 if ($this->poll_block->mayVote($ilUser->getId())) {
129 $this->tpl->setCurrentBlock("mode_info_bl");
130 if ($this->poll_block->getPoll()->getNonAnonymous()) {
131 $mode_info = $lng->txt("poll_non_anonymous_warning");
132 } else {
133 $mode_info = $lng->txt("poll_anonymous_warning");
134 }
135 $this->tpl->setVariable("MODE_INFO", $mode_info);
136 $this->tpl->parseCurrentBlock();
137
138 $is_multi_answer = ($this->poll_block->getPoll()->getMaxNumberOfAnswers() > 1);
139
140 if (isset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()])) {
141 $last_vote = $_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()];
142 unset($_SESSION["last_poll_vote"][$this->poll_block->getPoll()->getId()]);
143
144 if ($is_multi_answer) {
145 $error = sprintf(
146 $lng->txt("poll_vote_error_multi"),
147 $this->poll_block->getPoll()->getMaxNumberOfAnswers()
148 );
149 } else {
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 if (!$is_multi_answer) {
161 $this->tpl->setVariable("ANSWER_INPUT", "radio");
162 $this->tpl->setVariable("ANSWER_NAME", "aw");
163 } else {
164 $this->tpl->setVariable("ANSWER_INPUT", "checkbox");
165 $this->tpl->setVariable("ANSWER_NAME", "aw[]");
166
167 if (is_array($last_vote) && in_array($item["id"], $last_vote)) {
168 $this->tpl->setVariable("ANSWER_STATUS", 'checked="checked"');
169 }
170 }
171 $this->tpl->setVariable("VALUE_ANSWER", $item["id"]);
172 $this->tpl->setVariable("TXT_ANSWER_VOTE", nl2br($item["answer"]));
173 $this->tpl->parseCurrentBlock();
174 }
175
176 $ilCtrl->setParameterByClass(
178 "ref_id",
179 $this->getRefId()
180 );
181 $url = $ilCtrl->getLinkTargetByClass(
182 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
183 "vote"
184 );
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 $this->tpl->setVariable(
195 "TXT_VOTING_END_PERIOD",
196 sprintf(
197 $lng->txt("poll_voting_period_info"),
198 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
199 )
200 );
201 }
202 }
203
204
205 // result
206 if ($this->poll_block->maySeeResults($ilUser->getId())) {
207 if (!$this->poll_block->mayNotResultsYet($ilUser->getId())) {
208 $answers = array();
209 foreach ($a_poll->getAnswers() as $item) {
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 // sort results by votes / original position
221 if ($this->poll_block->getPoll()->getSortResultByVotes()) {
222 $order = array_keys(ilUtil::sortArray($perc, "abs", "desc", true, true));
223
224 foreach (array_keys($answers) as $answer_id) {
225 if (!in_array($answer_id, $order)) {
226 $order[] = $answer_id;
227 }
228 }
229 } else {
230 $order = array_keys($answers);
231 }
232
233 // pie chart
234 if ($this->poll_block->showResultsAs() == ilObjPoll::SHOW_RESULTS_AS_PIECHART) {
235 include_once("./Services/Chart/classes/class.ilChart.php");
236
237 $chart = ilChart::getInstanceByType(ilCHart::TYPE_PIE, "poll_results_pie_" . $this->getRefId());
238 $chart->setSize("100%", 200);
239 $chart->setAutoResize(true);
240
241 $chart_data = $chart->getDataInstance();
242
243 foreach ($order as $answer_id) {
244 $chart_data->addPoint(
245 round($perc[$answer_id]["perc"]),
246 nl2br($answers[$answer_id])
247 );
248 }
249
250 // disable legend, use inner labels - currently not preferred
251 // $chart_data->setLabelRadius(0.8);
252
253 $chart->addData($chart_data);
254
255 $pie_legend_id = "poll_legend_" . $this->getRefId();
256 $legend = new ilChartLegend();
257 $legend->setContainer($pie_legend_id);
258 $chart->setLegend($legend);
259
260 $this->tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
261 $this->tpl->setVariable("PIE_CHART", $chart->getHTML());
262 }
263 // bar chart
264 else {
265 include_once "Services/UIComponent/ProgressBar/classes/class.ilProgressBar.php";
266
267 $this->tpl->setCurrentBlock("answer_result");
268 foreach ($order as $answer_id) {
270 $pbar->setCurrent(round($perc[$answer_id]["perc"]));
271 $this->tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
272 $this->tpl->setVariable("TXT_ANSWER_RESULT", nl2br($answers[$answer_id]));
273 $this->tpl->parseCurrentBlock();
274 }
275 }
276 }
277 } else {
280 $end = $this->poll_block->getPoll()->getVotingPeriodEnd();
283
284 // #14607
285 $info = "";
286 if ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
287 $info .= $lng->txt("poll_block_message_already_voted") . " ";
288 }
289
290 $this->tpl->setVariable("TOTAL_ANSWERS", $info .
291 sprintf($lng->txt("poll_block_results_available_on"), $end));
292 }
293 } elseif ($this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
294 $this->tpl->setVariable("TOTAL_ANSWERS", $lng->txt("poll_block_message_already_voted"));
295 }
296 }
297
298 if (!$this->poll_block->mayVote($ilUser->getId()) && !$this->poll_block->getPoll()->hasUserVoted($ilUser->getId())) {
299 if ($this->poll_block->getPoll()->getVotingPeriod()) {
300 $this->tpl->setVariable(
301 "TXT_VOTING_PERIOD",
302 sprintf(
303 $lng->txt("poll_voting_period_full_info"),
304 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodBegin(), IL_CAL_UNIX)),
305 ilDatePresentation::formatDate(new ilDateTime($this->poll_block->getPoll()->getVotingPeriodEnd(), IL_CAL_UNIX))
306 )
307 );
308 }
309 } else {
310 $this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
311
312 $img = $a_poll->getImageFullPath();
313 if ($img) {
314 require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
315 $this->tpl->setVariable("URL_IMAGE", ilWACSignedPath::signFile($img));
316 }
317 }
318
319
320 $this->tpl->setVariable("ANCHOR_ID", $a_poll->getID());
321 //$this->tpl->setVariable("TXT_QUESTION", nl2br(trim($a_poll->getQuestion())));
322
323 $desc = trim($a_poll->getDescription());
324 if ($desc) {
325 $this->tpl->setVariable("TXT_DESC", nl2br($desc));
326 }
327
328
329 if ($this->poll_block->showComments()) {
330 $this->tpl->setCurrentBlock("comment_link");
331 $this->tpl->setVariable("LANG_COMMENTS", $lng->txt('poll_comments'));
332 $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
333 $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
334
335 $comments_count = $this->getNumberOfComments($this->getRefId());
336
337 if ($comments_count > 0) {
338 $this->tpl->setVariable("COMMENTS_COUNT", "(" . $comments_count . ")");
339 }
340
341 if (!self::$js_init) {
342 $redraw_url = $ilCtrl->getLinkTarget(
343 $this,
344 "getNumberOfCommentsForRedraw",
345 "",
346 true,
347 false
348 );
349 $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
350
351 $tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
352 self::$js_init = true;
353 }
354 }
355 }
356
360 public function getHTML()
361 {
364 $ilAccess = $this->access;
366
367 $this->poll_block->setRefId($this->getRefId());
368 $this->may_write = $ilAccess->checkAccess("write", "", $this->getRefId());
369 $this->has_content = $this->poll_block->hasAnyContent($ilUser->getId(), $this->getRefId());
370
371 #22078 and 22079 it always contains something.
372 /*if(!$this->may_write && !$this->has_content)
373 {
374 return "";
375 }*/
376
377 $poll_obj = $this->poll_block->getPoll();
378 $this->setTitle($poll_obj->getTitle());
379 $this->setData(array($poll_obj));
380
381 $ilCtrl->setParameterByClass(
383 "ref_id",
384 $this->getRefId()
385 );
386
387 if (!$this->poll_block->getMessage($ilUser->getId())) {
388 // notification
389 include_once "./Services/Notification/classes/class.ilNotification.php";
390 if (ilNotification::hasNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->poll_block->getPoll()->getId())) {
391 $this->addBlockCommand(
392 $ilCtrl->getLinkTargetByClass(
393 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
394 "unsubscribe"
395 ),
396 $lng->txt("poll_notification_unsubscribe")
397 );
398 } else {
399 $this->addBlockCommand(
400 $ilCtrl->getLinkTargetByClass(
401 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
402 "subscribe"
403 ),
404 $lng->txt("poll_notification_subscribe")
405 );
406 }
407 }
408
409 if ($this->may_write) {
410 // edit
411 $this->addBlockCommand(
412 $ilCtrl->getLinkTargetByClass(
413 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
414 "render"
415 ),
416 $lng->txt("edit_content")
417 );
418 $this->addBlockCommand(
419 $ilCtrl->getLinkTargetByClass(
420 array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
421 "edit"
422 ),
423 $lng->txt("settings")
424 );
425
426 /* delete (#10993 - see ilBlockGUI)
427 $parent_id = $tree->getParentId($this->getRefId());
428 $type = ilObject::_lookupType($parent_id, true);
429 $class = $objDefinition->getClassName($type);
430 if($class)
431 {
432 $class = "ilobj".strtolower($class)."gui";
433 $ilCtrl->setParameterByClass($class, "ref_id", $parent_id);
434 $ilCtrl->setParameterByClass($class, "item_ref_id", $this->getRefId());
435 $this->addBlockCommand(
436 $ilCtrl->getLinkTargetByClass($class, "delete"),
437 $lng->txt("delete"));
438 }
439 */
440 }
441
442 $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
443
444 return parent::getHTML();
445 }
446
452 private function commentJSCall()
453 {
454 include_once("./Services/Notes/classes/class.ilNoteGUI.php");
455 include_once("./Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
456
457 $refId = $this->getRefId();
458 $objectId = ilObject2::_lookupObjectId($refId);
459
462 $refId,
463 "poll",
464 $objectId
465 );
466
467
468 $comment = new ilNoteGUI();
469 $jsCall = $comment->getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(" . $refId . ");");
470
471 return $jsCall;
472 }
473
478 {
479 $number = $this->getNumberOfComments($_GET["poll_id"]);
480
481 if ($number > 0) {
482 echo "(" . $number . ")";
483 } else {
484 echo "";
485 }
486
487 exit();
488 }
489
496 public function getNumberOfComments($ref_id)
497 {
498 include_once("./Services/Notes/classes/class.ilNote.php");
499
500 $obj_id = ilObject2::_lookupObjectId($ref_id);
501 $number = ilNote::_countNotesAndComments($obj_id);
502
503 if (count($number) == 0) {
504 return 0;
505 }
506
507 return $number[$obj_id][IL_NOTE_PUBLIC];
508 }
509}
sprintf('%.4f', $callTime)
user()
Definition: user.php:4
$total
Definition: Utf8Test.php:87
$comment
Definition: buildRTE.php:83
$_GET["client_id"]
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
const IL_SCREEN_SIDE
const IL_CAL_UNIX
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:5
This class represents a block method of a block.
setRowTemplate($a_rowtemplatename, $a_rowtemplatedir="")
Set Row Template Name.
setData($a_data)
Set Data.
setTitle($a_title)
Set Title.
setBlockId($a_block_id=0)
Set Block Id.
addBlockCommand( $a_href, $a_text, $a_target="", $a_img="", $a_right_aligned=false, $a_checked=false, $a_html="")
Add Block Command.
getRefId()
Get Ref Id (only used if isRepositoryObject() is true).
static getInstanceByType($a_type, $a_id)
Get type instance.
static buildAjaxHash($a_node_type, $a_node_id, $a_obj_type, $a_obj_id, $a_sub_type=null, $a_sub_id=null)
Build ajax hash.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date @access public.
static setUseRelativeDates($a_status)
set use relative dates
static useRelativeDates()
check if relative dates are used
@classDescription Date and time handling
Notes GUI class.
static _countNotesAndComments($a_rep_obj_id, $a_sub_obj_id=null)
Get all notes related to a specific object.
static hasNotification($type, $user_id, $id)
Check notification status for object and user.
const SHOW_RESULTS_AS_PIECHART
static _lookupObjectId($a_ref_id)
lookup object id
BlockGUI class for polls.
executeCommand()
execute command
__construct()
Constructor.
getNumberOfCommentsForRedraw()
Returns comment count for JS Redraw.
static getScreenMode()
Get Screen Mode for current command.
getRepositoryObjectGUIName()
Get repository object GUI name.
static getBlockType()
Get block type.
getNumberOfComments($ref_id)
Get comment count.
setBlock($a_block)
Do most of the initialisation.
getHTML()
Get block HTML code.
static isRepositoryObject()
Is block of repository object?
commentJSCall()
Builds JavaScript Call to open CommentLayer via html link.
static getInstance()
Factory.
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
static signFile($path_to_file)
global $ilCtrl
Definition: ilias.php:18
$error
Definition: Error.php:17
$end
Definition: saml1-acs.php:18
$info
Definition: index.php:5
$url
global $DIC
Definition: saml.php:7
$ilUser
Definition: imgupload.php:18