ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjPollGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once "./Services/Object/classes/class.ilObject2GUI.php";
6 
19 {
20  function __construct($a_id = 0, $a_id_type = self::REPOSITORY_NODE_ID, $a_parent_node_id = 0)
21  {
22  global $lng;
23 
24  parent::__construct($a_id, $a_id_type, $a_parent_node_id);
25 
26  $lng->loadLanguageModule("poll");
27  }
28 
29  function getType()
30  {
31  return "poll";
32  }
33 
34  protected function afterSave(ilObject $a_new_object)
35  {
36  global $ilCtrl;
37 
38  ilUtil::sendSuccess($this->lng->txt("object_added"), true);
39  $ilCtrl->redirect($this, "render");
40  }
41 
42  protected function initEditCustomForm(ilPropertyFormGUI $a_form)
43  {
44  global $lng;
45 
46  // activation
47 
48  include_once "Services/Object/classes/class.ilObjectActivation.php";
49  $this->lng->loadLanguageModule('rep');
50 
52  $section->setTitle($this->lng->txt('rep_activation_availability'));
53  $a_form->addItem($section);
54 
55  // additional info only with multiple references
56  $act_obj_info = $act_ref_info = "";
57  if(sizeof(ilObject::_getAllReferences($this->object->getId())) > 1)
58  {
59  $act_obj_info = ' '.$this->lng->txt('rep_activation_online_object_info');
60  $act_ref_info = $this->lng->txt('rep_activation_access_ref_info');
61  }
62 
63  $online = new ilCheckboxInputGUI($this->lng->txt('rep_activation_online'),'online');
64  $online->setInfo($this->lng->txt('poll_activation_online_info').$act_obj_info);
65  $a_form->addItem($online);
66 
67  $act_type = new ilCheckboxInputGUI($this->lng->txt('rep_visibility_until'),'access_type');
68  // $act_type->setInfo($this->lng->txt('poll_availability_until_info'));
69 
70  $this->tpl->addJavaScript('./Services/Form/js/date_duration.js');
71  include_once "Services/Form/classes/class.ilDateDurationInputGUI.php";
72  $dur = new ilDateDurationInputGUI($this->lng->txt('rep_time_period'), "access_period");
73  $dur->setShowTime(true);
74  $date = $this->object->getAccessBegin();
75  $dur->setStart(new ilDateTime($date ? $date : time(), IL_CAL_UNIX));
76  $dur->setStartText($this->lng->txt('rep_activation_limited_start'));
77  $date = $this->object->getAccessEnd();
78  $dur->setEnd(new ilDateTime($date ? $date : time(), IL_CAL_UNIX));
79  $dur->setEndText($this->lng->txt('rep_activation_limited_end'));
80  $act_type->addSubItem($dur);
81 
82  $a_form->addItem($act_type);
83 
84 
85  // period/results
86 
88  $section->setTitle($this->lng->txt('poll_voting_period_and_results'));
89  $a_form->addItem($section);
90 
91  $prd = new ilCheckboxInputGUI($this->lng->txt('poll_voting_period_limited'),'period');
92 
93  $vdur = new ilDateDurationInputGUI($this->lng->txt('rep_time_period'), "voting_period");
94  $vdur->setShowTime(true);
95  $date = $this->object->getVotingPeriodBegin();
96  $vdur->setStart(new ilDateTime($date ? $date : time(), IL_CAL_UNIX));
97  $vdur->setStartText($this->lng->txt('poll_voting_period_start'));
98  $date = $this->object->getVotingPeriodEnd();
99  $vdur->setEnd(new ilDateTime($date ? $date : time(), IL_CAL_UNIX));
100  $vdur->setEndText($this->lng->txt('poll_voting_period_end'));
101  $prd->addSubItem($vdur);
102 
103  $a_form->addItem($prd);
104 
105  $results = new ilRadioGroupInputGUI($lng->txt("poll_view_results"), "results");
106  $results->setRequired(true);
107  $results->addOption(new ilRadioOption($lng->txt("poll_view_results_always"),
109  $results->addOption(new ilRadioOption($lng->txt("poll_view_results_never"),
111  $results->addOption(new ilRadioOption($lng->txt("poll_view_results_after_vote"),
113  $results->addOption(new ilRadioOption($lng->txt("poll_view_results_after_period"),
115  $a_form->addItem($results);
116 
117  $show_result_as = new ilRadioGroupInputGUI($lng->txt("poll_show_results_as"), "show_results_as");
118  $show_result_as->setRequired(true);
119  $result_bar = new ilRadioOption($lng->txt("poll_barchart"),
121  $show_result_as->addOption($result_bar);
122  $show_result_as->addOption(new ilRadioOption($lng->txt("poll_piechart"),
124  $a_form->addItem($show_result_as);
125 
126  $sort = new ilRadioGroupInputGUI($lng->txt("poll_result_sorting"), "sort");
127  $sort->setRequired(true);
128  $sort->addOption(new ilRadioOption($lng->txt("poll_result_sorting_answers"), 0));
129  $sort->addOption(new ilRadioOption($lng->txt("poll_result_sorting_votes"), 1));
130  $a_form->addItem($sort);
131 
133  $section->setTitle($this->lng->txt('poll_comments'));
134  $a_form->addItem($section);
135 
136  $comment = new ilCheckboxInputGUI($this->lng->txt('poll_comments'), 'comment');
137  //$comment->setInfo($this->lng->txt('poll_comments_info'));
138  $a_form->addItem($comment);
139  }
140 
141  protected function getEditFormCustomValues(array &$a_values)
142  {
143  include_once "Services/Object/classes/class.ilObjectActivation.php";
144 
145  $a_values["online"] = $this->object->IsOnline();
146  $a_values["results"] = $this->object->getViewResults();
147  $a_values["access_type"] = ($this->object->getAccessType() == ilObjectActivation::TIMINGS_ACTIVATION);
148  $a_values["period"] = $this->object->getVotingPeriod();
149  $a_values["sort"] = $this->object->getSortResultByVotes();
150  $a_values["comment"] = $this->object->getShowComments();
151  $a_values["show_results_as"] = $this->object->getShowResultsAs();
152  }
153 
154  protected function validateCustom(ilPropertyFormGUI $a_form)
155  {
156  // #14606
157  if(!$a_form->getInput("period") &&
158  $a_form->getInput("results") == ilObjPoll::VIEW_RESULTS_AFTER_PERIOD)
159  {
160  ilUtil::sendFailure($this->lng->txt("form_input_not_valid"));
161  $a_form->getItemByPostVar("results")->setAlert($this->lng->txt("poll_view_results_after_period_impossible"));
162  return false;
163  }
164  return parent::validateCustom($a_form);
165  }
166 
167  protected function updateCustom(ilPropertyFormGUI $a_form)
168  {
169  $this->object->setViewResults($a_form->getInput("results"));
170  $this->object->setOnline($a_form->getInput("online"));
171  $this->object->setSortResultByVotes($a_form->getInput("sort"));
172  $this->object->setShowComments($a_form->getInput("comment"));
173  $this->object->setShowResultsAs($a_form->getInput("show_results_as"));
174 
175  include_once "Services/Object/classes/class.ilObjectActivation.php";
176  if($a_form->getInput("access_type"))
177  {
178  $this->object->setAccessType(ilObjectActivation::TIMINGS_ACTIVATION);
179 
180  $period = $a_form->getItemByPostVar("access_period");
181  $this->object->setAccessBegin($period->getStart()->get(IL_CAL_UNIX));
182  $this->object->setAccessEnd($period->getEnd()->get(IL_CAL_UNIX));
183  }
184  else
185  {
186  $this->object->setAccessType(ilObjectActivation::TIMINGS_DEACTIVATED);
187  }
188 
189  if($a_form->getInput("period"))
190  {
191  $this->object->setVotingPeriod(1);
192 
193  $period = $a_form->getItemByPostVar("voting_period");
194  $this->object->setVotingPeriodBegin($period->getStart()->get(IL_CAL_UNIX));
195  $this->object->setVotingPeriodEnd($period->getEnd()->get(IL_CAL_UNIX));
196  }
197  else
198  {
199  $this->object->setVotingPeriod(0);
200  }
201  }
202 
203  function setTabs()
204  {
205  global $lng, $ilHelp;
206 
207  $ilHelp->setScreenIdComponent("poll");
208 
209  if ($this->checkPermissionBool("write"))
210  {
211  $this->tabs_gui->addTab("content",
212  $lng->txt("content"),
213  $this->ctrl->getLinkTarget($this, ""));
214  }
215 
216  if ($this->checkPermissionBool("write"))
217  {
218  $this->tabs_gui->addTab("settings",
219  $lng->txt("settings"),
220  $this->ctrl->getLinkTarget($this, "edit"));
221 
222  $this->tabs_gui->addTab("participants",
223  $lng->txt("poll_result"),
224  $this->ctrl->getLinkTarget($this, "showParticipants"));
225 
226  $this->tabs_gui->addTab("export",
227  $lng->txt("export"),
228  $this->ctrl->getLinkTargetByClass("ilexportgui", ""));
229  }
230 
231  // will add permissions if needed
232  parent::setTabs();
233  }
234 
235  function executeCommand()
236  {
237  global $ilCtrl, $tpl, $ilTabs, $ilNavigationHistory;
238 
239  $next_class = $ilCtrl->getNextClass($this);
240  $cmd = $ilCtrl->getCmd();
241 
242  $tpl->getStandardTemplate();
243 
244  // add entry to navigation history
245  if(!$this->getCreationMode() &&
246  $this->getAccessHandler()->checkAccess("read", "", $this->node_id))
247  {
248  $link = $ilCtrl->getLinkTargetByClass("ilrepositorygui", "frameset");
249  $ilNavigationHistory->addItem($this->node_id, $link, "poll");
250  }
251 
252  switch($next_class)
253  {
254  case "ilinfoscreengui":
255  $this->prepareOutput();
256  $this->infoScreenForward();
257  break;
258 
259  case "ilcommonactiondispatchergui":
260  include_once("Services/Object/classes/class.ilCommonActionDispatcherGUI.php");
262  $this->ctrl->forwardCommand($gui);
263  break;
264 
265  case "ilpermissiongui":
266  $this->prepareOutput();
267  $ilTabs->activateTab("id_permissions");
268  include_once("Services/AccessControl/classes/class.ilPermissionGUI.php");
269  $perm_gui = new ilPermissionGUI($this);
270  $this->ctrl->forwardCommand($perm_gui);
271  break;
272 
273  case "ilobjectcopygui":
274  include_once "./Services/Object/classes/class.ilObjectCopyGUI.php";
275  $cp = new ilObjectCopyGUI($this);
276  $cp->setType("poll");
277  $this->ctrl->forwardCommand($cp);
278  break;
279 
280  case 'ilexportgui':
281  $this->prepareOutput();
282  $ilTabs->activateTab("export");
283  include_once("./Services/Export/classes/class.ilExportGUI.php");
284  $exp_gui = new ilExportGUI($this);
285  $exp_gui->addFormat("xml");
286  $ilCtrl->forwardCommand($exp_gui);
287  break;
288 
289  default:
290  return parent::executeCommand();
291  }
292 
293  return true;
294  }
295 
296 
297  // --- ObjectGUI End
298 
299 
303  function render($a_form = null)
304  {
305  global $tpl, $ilTabs, $ilCtrl, $lng, $ilToolbar, $ilUser;
306 
307  if(!$this->checkPermissionBool("write"))
308  {
309  ilUtil::sendInfo($lng->txt("no_permission"));
310  return;
311  }
312 
313  $ilTabs->activateTab("content");
314 
315  if(!$a_form)
316  {
317  if($this->object->countVotes())
318  {
319  $url = $ilCtrl->getLinkTarget($this, "showParticipants");
320  ilUtil::sendInfo($lng->txt("poll_votes_no_edit").
321  " <a href=\"".$url."\">&raquo;".$lng->txt("poll_result")."</a>");
322  }
323 
324  $a_form = $this->initQuestionForm($this->object->countVotes());
325  }
326 
327  $tpl->setPermanentLink('poll', $this->node_id);
328 
329  $tpl->setContent($a_form->getHTML());
330  }
331 
332  protected function initQuestionForm($a_read_only = false)
333  {
334  global $lng, $ilCtrl;
335 
336  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
337  $form = new ilPropertyFormGUI();
338  $form->setFormAction($ilCtrl->getFormAction($this, "saveQuestion"));
339  $form->setTitle($lng->txt("obj_poll"));
340 
341  $question = new ilTextAreaInputGUI($lng->txt("poll_question"), "question");
342  $question->setRequired(true);
343  $question->setCols(40);
344  $question->setRows(2);
345  $question->setValue($this->object->getQuestion());
346  $question->setDisabled($a_read_only);
347  $form->addItem($question);
348 
349  $dimensions = " (".ilObjPoll::getImageSize()."px)";
350  $img = new ilImageFileInputGUI($lng->txt("poll_image").$dimensions, "image");
351  $img->setDisabled($a_read_only);
352  $form->addItem($img);
353 
354  // show existing file
355  $file = $this->object->getImageFullPath(true);
356  if($file)
357  {
358  $img->setImage($file);
359  }
360 
361  $anonymous = new ilRadioGroupInputGUI($lng->txt("poll_mode"), "mode");
362  $anonymous->setRequired(true);
363  $option = new ilRadioOption($lng->txt("poll_mode_anonymous"), 0);
364  $option->setInfo($lng->txt("poll_mode_anonymous_info"));
365  $anonymous->addOption($option);
366  $option = new ilRadioOption($lng->txt("poll_mode_personal"), 1);
367  $option->setInfo($lng->txt("poll_mode_personal_info"));
368  $anonymous->addOption($option);
369  $anonymous->setValue($this->object->getNonAnonymous());
370  $anonymous->setDisabled($a_read_only);
371  $form->addItem($anonymous);
372 
373  $nanswers = new ilNumberInputGUI($lng->txt("poll_max_number_of_answers"), "nanswers");
374  $nanswers->setRequired(true);
375  $nanswers->setMinValue(1);
376  $nanswers->setSize(3);
377  $nanswers->setValue($this->object->getMaxNumberOfAnswers());
378  $nanswers->setDisabled($a_read_only);
379  $form->addItem($nanswers);
380 
381  $answers = new ilTextInputGUI($lng->txt("poll_answers"), "answers");
382  $answers->setRequired(true);
383  $answers->setMulti(true, true);
384  $answers->setDisabled($a_read_only);
385  $form->addItem($answers);
386 
387  $multi_answers = array();
388  foreach($this->object->getAnswers() as $idx => $item)
389  {
390  if(!$idx)
391  {
392  $answers->setValue($item["answer"]);
393  }
394  $multi_answers[] = $item["answer"];
395  }
396  $answers->setMultiValues($multi_answers);
397 
398  if(!$a_read_only)
399  {
400  $form->addCommandButton("saveQuestion", $lng->txt("save"));
401  }
402 
403  return $form;
404  }
405 
406  function saveQuestion()
407  {
408  $form = $this->initQuestionForm();
409  if($form->checkInput())
410  {
411  $this->object->setQuestion($form->getInput("question"));
412  $this->object->setNonAnonymous($form->getInput("mode"));
413 
414  $image = $form->getItemByPostVar("image");
415  if($_FILES["image"]["tmp_name"])
416  {
417  $this->object->uploadImage($_FILES["image"]);
418  }
419  else if($image->getDeletionFlag())
420  {
421  $this->object->deleteImage();
422  }
423 
424  $nr_of_anwers = $this->object->saveAnswers($form->getInput("answers"));
425 
426  // #15073
427  $this->object->setMaxNumberOfAnswers(min($form->getInput("nanswers"), $nr_of_anwers));
428 
429  if($this->object->update())
430  {
431  ilUtil::sendSuccess($this->lng->txt("settings_saved"), true);
432  $this->ctrl->redirect($this, "render");
433  }
434  }
435 
436  $form->setValuesByPost();
437  $this->render($form);
438  }
439 
440  protected function setParticipantsSubTabs($a_active)
441  {
442  global $ilTabs, $lng, $ilCtrl;
443 
444  if(!$this->object->getNonAnonymous())
445  {
446  return;
447  }
448 
449  $ilTabs->addSubTab("result_answers", $lng->txt("poll_result_answers"),
450  $ilCtrl->getLinkTarget($this, "showParticipants"));
451  $ilTabs->addSubTab("result_users", $lng->txt("poll_result_users"),
452  $ilCtrl->getLinkTarget($this, "showParticipantVotes"));
453 
454  $ilTabs->activateSubTab($a_active);
455  }
456 
457  function showParticipants()
458  {
459  global $lng, $ilTabs, $tpl;
460 
461  if(!$this->checkPermissionBool("write"))
462  {
463  ilUtil::sendInfo($lng->txt("no_permission"));
464  return;
465  }
466 
467  $ilTabs->activateTab("participants");
468  $this->setParticipantsSubTabs("result_answers");
469 
470  include_once "Modules/Poll/classes/class.ilPollAnswerTableGUI.php";
471  $tbl = new ilPollAnswerTableGUI($this, "showParticipants");
472  $tpl->setContent($tbl->getHTML());
473  }
474 
476  {
477  global $ilTabs, $lng, $tpl;
478 
479  if(!$this->checkPermissionBool("write") ||
480  !$this->object->getNonAnonymous())
481  {
482  ilUtil::sendInfo($lng->txt("no_permission"));
483  return;
484  }
485 
486  $ilTabs->activateTab("participants");
487  $this->setParticipantsSubTabs("result_users");
488 
489  include_once "Modules/Poll/classes/class.ilPollUserTableGUI.php";
490  $tbl = new ilPollUserTableGUI($this, "showParticipantVotes");
491  $tpl->setContent($tbl->getHTML());
492  }
493 
495  {
496  global $lng, $tpl, $ilTabs;
497 
498  if(!$this->checkPermissionBool("write"))
499  {
500  ilUtil::sendInfo($lng->txt("no_permission"));
501  return;
502  }
503 
504  $ilTabs->activateTab("participants");
505 
506  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
507  $cgui = new ilConfirmationGUI();
508  $cgui->setHeaderText($lng->txt("poll_delete_votes_sure"));
509 
510  $cgui->setFormAction($this->ctrl->getFormAction($this));
511  $cgui->setCancel($lng->txt("cancel"), "showParticipants");
512  $cgui->setConfirm($lng->txt("confirm"), "deleteAllVotes");
513 
514  $tpl->setContent($cgui->getHTML());
515  }
516 
517  function deleteAllVotes()
518  {
519  global $ilCtrl, $lng;
520 
521  if(!$this->checkPermissionBool("write"))
522  {
523  ilUtil::sendInfo($lng->txt("no_permission"));
524  return;
525  }
526 
527  $this->object->deleteAllVotes();
528 
529  $ilCtrl->redirect($this, "showParticipants");
530  }
531 
532  function vote()
533  {
534  global $tree, $ilUser;
535 
536  $valid = true;
537  if($this->object->getMaxNumberOfAnswers() > 1)
538  {
539  if(sizeof($_POST["aw"]) > $this->object->getMaxNumberOfAnswers())
540  {
541  $valid = false;
542  }
543  if(!sizeof($_POST["aw"]))
544  {
545  $valid = false;
546  }
547  }
548  else
549  {
550  if((int)!$_POST["aw"])
551  {
552  $valid = false;
553  }
554  }
555 
556  if($valid)
557  {
558  unset($_SESSION["last_poll_vote"][$this->object->getId()]);
559  $this->object->saveVote($ilUser->getId(), $_POST["aw"]);
560 
561  $this->sendNotifications();
562  }
563  else
564  {
565  $_SESSION["last_poll_vote"][$this->object->getId()] = $_POST["aw"];
566  }
567 
568  include_once "Services/Link/classes/class.ilLink.php";
569  ilUtil::redirect(ilLink::_getLink($tree->getParentId($this->ref_id)));
570  }
571 
572  function subscribe()
573  {
574  global $ilUser, $tree, $lng;
575 
576  include_once "./Services/Notification/classes/class.ilNotification.php";
577  ilNotification::setNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->object->getId(), true);
578 
579  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
580  include_once "Services/Link/classes/class.ilLink.php";
581  ilUtil::redirect(ilLink::_getLink($tree->getParentId($this->ref_id)));
582  }
583 
584  function unsubscribe()
585  {
586  global $ilUser, $tree, $lng;
587 
588  include_once "./Services/Notification/classes/class.ilNotification.php";
589  ilNotification::setNotification(ilNotification::TYPE_POLL, $ilUser->getId(), $this->object->getId(), false);
590 
591  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
592  include_once "Services/Link/classes/class.ilLink.php";
593  ilUtil::redirect(ilLink::_getLink($tree->getParentId($this->ref_id)));
594  }
595 
596  protected function sendNotifications()
597  {
598  global $ilUser;
599 
600  // recipients
601  include_once "./Services/Notification/classes/class.ilNotification.php";
603  $this->object->getId(), null, true);
604  if(!sizeof($users))
605  {
606  return;
607  }
608 
609  include_once "./Services/Notification/classes/class.ilSystemNotification.php";
610  $ntf = new ilSystemNotification();
611  $ntf->setLangModules(array("poll"));
612  $ntf->setRefId($this->ref_id);
613 
614  if($this->object->getNonAnonymous())
615  {
616  $ntf->setChangedByUserId($ilUser->getId());
617  }
618 
619  $ntf->setSubjectLangId('poll_vote_notification_subject');
620  $ntf->setIntroductionLangId('poll_vote_notification_body');
621  $ntf->setGotoLangId('poll_vote_notification_link');
622  $ntf->setReasonLangId('poll_vote_notification_reason');
623 
624  $notified = $ntf->sendMail($users, null, "read");
625 
626  ilNotification::updateNotificationTime(ilNotification::TYPE_POLL, $this->object->getId(), $notified);
627  }
628 
634  function getHTML()
635  {
636 
637  }
638 
639  function addLocatorItems()
640  {
641  global $ilLocator;
642 
643  if (is_object($this->object))
644  {
645  $ilLocator->addItem($this->object->getTitle(), $this->ctrl->getLinkTarget($this, ""), "", $this->node_id);
646  }
647  }
648 
654  public static function _goto($a_target)
655  {
656  global $tree, $ilAccess;
657 
658  $id = explode("_", $a_target);
659  $ref_id = $id[0];
660 
661  // #13728 - used in notification mostly
662  if ($ilAccess->checkAccess("write", "", $ref_id))
663  {
664  $_GET["baseClass"] = "ilRepositoryGUI";
665  $_GET["ref_id"] = $ref_id;
666  $_GET["cmd"] = "showParticipants";
667  include("ilias.php");
668  exit;
669  }
670  else
671  {
672  // is sideblock: so show parent instead
673  $container_id = $tree->getParentId($ref_id);
674 
675  // #11810
676  include_once "Services/Link/classes/class.ilLink.php";
677  ilUtil::redirect(ilLink::_getLink($container_id).
678  "#poll".ilObject::_lookupObjId($id[0]));
679  }
680  }
681 }
682 
683 ?>