ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 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
32 parent::__construct();
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 {
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 require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
328 $this->tpl->setVariable("URL_IMAGE", ilWACSignedPath::signFile($img));
329 }
330
331 if ($this->poll_block->showComments()) {
332 $this->tpl->setCurrentBlock("comment_link");
333 $this->tpl->setVariable("LANG_COMMENTS", $lng->txt('poll_comments'));
334 $this->tpl->setVariable("COMMENT_JSCALL", $this->commentJSCall());
335 $this->tpl->setVariable("COMMENTS_COUNT_ID", $this->getRefId());
336
337 $comments_count = $this->getNumberOfComments($this->getRefId());
338
339 if($comments_count > 0)
340 {
341 $this->tpl->setVariable("COMMENTS_COUNT", "(".$comments_count.")");
342 }
343
344 if(!self::$js_init)
345 {
346 $redraw_url = $ilCtrl->getLinkTarget($this, "getNumberOfCommentsForRedraw",
347 "", true, false);
348 $this->tpl->setVariable("COMMENTS_REDRAW_URL", $redraw_url);
349
350 $tpl->addJavaScript("Modules/Poll/js/ilPoll.js");
351 self::$js_init = true;
352 }
353 }
354
355 }
356
360 function getHTML()
361 {
362 global $ilCtrl, $lng, $ilAccess, $ilUser;
363
364 $this->poll_block->setRefId($this->getRefId());
365 $this->may_write = $ilAccess->checkAccess("write", "", $this->getRefId());
366 $this->has_content = $this->poll_block->hasAnyContent($ilUser->getId(), $this->getRefId());
367
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($this->getRepositoryObjectGUIName(),
378 "ref_id", $this->getRefId());
379
380 if(!$this->poll_block->getMessage($ilUser->getId()))
381 {
382 // notification
383 include_once "./Services/Notification/classes/class.ilNotification.php";
384 if(ilNotification::hasNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->poll_block->getPoll()->getId()))
385 {
386 $this->addBlockCommand(
387 $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
388 "unsubscribe"),
389 $lng->txt("poll_notification_unsubscribe"));
390 }
391 else
392 {
393 $this->addBlockCommand(
394 $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
395 "subscribe"),
396 $lng->txt("poll_notification_subscribe"));
397 }
398 }
399
400 if ($this->may_write)
401 {
402 // edit
403 $this->addBlockCommand(
404 $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
405 "render"),
406 $lng->txt("edit_content"));
407 $this->addBlockCommand(
408 $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", $this->getRepositoryObjectGUIName()),
409 "edit"),
410 $lng->txt("settings"));
411
412 /* delete (#10993 - see ilBlockGUI)
413 $parent_id = $tree->getParentId($this->getRefId());
414 $type = ilObject::_lookupType($parent_id, true);
415 $class = $objDefinition->getClassName($type);
416 if($class)
417 {
418 $class = "ilobj".strtolower($class)."gui";
419 $ilCtrl->setParameterByClass($class, "ref_id", $parent_id);
420 $ilCtrl->setParameterByClass($class, "item_ref_id", $this->getRefId());
421 $this->addBlockCommand(
422 $ilCtrl->getLinkTargetByClass($class, "delete"),
423 $lng->txt("delete"));
424 }
425 */
426 }
427
428 $ilCtrl->clearParametersByClass($this->getRepositoryObjectGUIName());
429
430 return parent::getHTML();
431 }
432
438 private function commentJSCall()
439 {
440 include_once("./Services/Notes/classes/class.ilNoteGUI.php");
441 include_once("./Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
442
443 $refId = $this->getRefId();
444 $objectId = ilObject2::_lookupObjectId($refId);
445
447 ilCommonActionDispatcherGUI::TYPE_REPOSITORY, $refId, "poll", $objectId);
448
449
450 $comment = new ilNoteGUI();
451 $jsCall = $comment->getListCommentsJSCall($ajaxHash, "ilPoll.redrawComments(".$refId.");");
452
453 return $jsCall;
454 }
455
460 {
461 $number = $this->getNumberOfComments($_GET["poll_id"]);
462
463 if($number > 0)
464 {
465 echo "(".$number.")";
466 }
467 else
468 {
469 echo "";
470 }
471
472 exit();
473 }
474
482 {
483 include_once("./Services/Notes/classes/class.ilNote.php");
484
486 $number = ilNote::_countNotesAndComments($obj_id);
487
488 if(count($number) == 0)
489 {
490 return 0;
491 }
492
493 return $number[$obj_id][IL_NOTE_PUBLIC];
494 }
495}
496
497?>
global $tpl
Definition: ilias.php:8
$comment
Definition: buildRTE.php:83
$_GET["client_id"]
$_SESSION["AccountId"]
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.
addBlockCommand($a_href, $a_text, $a_target="", $a_img="", $a_right_aligned=false, $a_checked=false, $a_html="")
Add Block Command.
setTitle($a_title)
Set Title.
setBlockId($a_block_id=0)
Set Block Id.
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 setUseRelativeDates($a_status)
set use relative dates
static formatDate(ilDateTime $date)
Format a date @access public.
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.
__construct()
Constructor.
getNumberOfCommentsForRedraw()
Returns comment count for JS Redraw.
& executeCommand()
execute command
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)
$info
Definition: example_052.php:80
$legend
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:54
global $lng
Definition: privfeed.php:40
$cmd
Definition: sahs_server.php:35
$ref_id
Definition: sahs_server.php:39
$url
Definition: shib_logout.php:72
global $ilUser
Definition: imgupload.php:15