ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilConsultationHoursGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once './Services/Calendar/classes/class.ilCalendarRecurrence.php';
25 include_once './Services/Booking/classes/class.ilBookingEntry.php';
26 include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
27 
35 {
36  const MODE_CREATE = 1;
37  const MODE_UPDATE = 2;
38  const MODE_MULTI = 3;
39 
40  protected $user_id;
41  protected $ctrl;
42 
43  protected $booking = null;
44 
48  public function __construct()
49  {
50  global $lng, $ilCtrl, $tpl, $ilUser;
51 
52  $user_id = (int)$_GET['user_id'];
53  if($user_id)
54  {
56  {
57  $this->user_id = $user_id;
58  }
59  else
60  {
61  $user_id = false;
62  }
63  }
64  if(!$user_id)
65  {
66  $this->user_id = $ilUser->getId();
67  }
68 
69  $this->ctrl = $ilCtrl;
70  $this->lng = $lng;
71  $this->tpl = $tpl;
72  }
73 
78  public function executeCommand()
79  {
80  global $ilUser, $ilCtrl, $tpl, $ilHelp, $ilTabs;
81 
82  $ilHelp->setScreenIdComponent("cal");
83 
84  switch($this->ctrl->getNextClass())
85  {
86  case "ilpublicuserprofilegui":
87  include_once('./Services/User/classes/class.ilPublicUserProfileGUI.php');
88  $profile = new ilPublicUserProfileGUI($this->user_id);
89  $profile->setBackUrl($this->getProfileBackUrl());
90  $ret = $ilCtrl->forwardCommand($profile);
91  $tpl->setContent($ret);
92  break;
93 
94  case 'ilrepositorysearchgui':
95 
96  include_once('./Services/Search/classes/class.ilRepositorySearchGUI.php');
97  $rep_search = new ilRepositorySearchGUI();
98 
99  if(isset($_REQUEST['assignM']))
100  {
101  $rep_search->setCallback(
102  $this,
103  'assignUsersToAppointments',
104  array()
105  );
106  $ilCtrl->setParameter($this,'assignM',1);
107  $ilCtrl->setReturn($this,'appointmentList');
108  $ilTabs->activateSubTab('cal_ch_app_list');
109  }
110  elseif(isset($_REQUEST['grp_id']))
111  {
112  $rep_search->setCallback(
113  $this,
114  'assignUsersToGroup',
115  array()
116  );
117  $ilCtrl->saveParameter($this,'grp_id');
118  $ilCtrl->setReturn($this,'groupList');
119  $ilTabs->activateSubTab('cal_ch_app_grp');
120  }
121  elseif(isset($_REQUEST['apps']))
122  {
123  $rep_search->setCallback(
124  $this,
125  'assignUsersToAppointment',
126  array()
127  );
128  $ilCtrl->saveParameter($this,'apps');
129  $ilCtrl->setReturn($this,'appointmentList');
130  $ilTabs->activateSubTab('cal_ch_app_list');
131  }
132  $ilCtrl->forwardCommand($rep_search);
133  break;
134 
135  default:
136  $tpl->setTitle($this->lng->txt("cal_ch_form_header")); // #12220
137 
138  $this->setTabs();
139  if($ilUser->getId() != $this->user_id)
140  {
141  $ilCtrl->setParameter($this, 'user_id', $this->user_id);
142  }
143 
144  $cmd = $this->ctrl->getCmd('appointmentList');
145  $this->$cmd();
146  }
147  }
148 
153  public function getUserId()
154  {
155  return $this->user_id;
156  }
157 
161  protected function searchUsersForAppointments()
162  {
163  global $ilCtrl, $ilTabs;
164 
165  $_SESSION['ch_apps'] = $_REQUEST['apps'];
166 
167  if(!count($_SESSION['ch_apps']))
168  {
169  ilUtil::sendFailure($this->lng->txt('select_one'),true);
170  $GLOBALS['ilCtrl']->redirect($this,'appointmentList');
171  }
172  $_REQUEST['assignM'] = 1;
173  $ilCtrl->setCmdClass('ilrepositorysearchgui');
174  $ilCtrl->setcmd('');
175  $this->executeCommand();
176  }
177 
182  protected function sendInfoAboutUnassignedUsers($unassigned)
183  {
184  if(!$unassigned)
185  {
186  return true;
187  }
188  $users = array();
189  foreach($unassigned as $user_id)
190  {
191  include_once './Services/User/classes/class.ilObjUser.php';
192  $users[] = ilObjUser::_lookupFullname($user_id);
193  }
194  ilUtil::sendInfo($this->lng->txt('cal_ch_user_assignment_failed_info').'<br />'.implode('<br />', $users),true);
195  return true;
196  }
197 
202  public function assignUsersToAppointments(array $users)
203  {
204  global $ilCtrl;
205 
206  $unassigned_users = array();
207  foreach($_SESSION['ch_apps'] as $app)
208  {
209  $unassigned_users = array_unique(array_merge($unassigned_users,$this->assignUsersToAppointment($users,$app,false)));
210  }
211 
212  $this->sendInfoAboutUnassignedUsers($unassigned_users);
213  $ilCtrl->redirect($this,'appointmentList');
214  }
215 
216 
222  public function assignUsersToAppointment(array $users, $a_app = 0, $a_redirect = true)
223  {
224  global $ilCtrl;
225 
226  if($a_app)
227  {
228  $app = $a_app;
229  }
230  else
231  {
232  $app = $_REQUEST['apps'];
233  }
234 
235  include_once './Services/Booking/classes/class.ilBookingEntry.php';
237 
238  $assigned_users = array();
239  foreach($users as $user)
240  {
241  if($booking->getCurrentNumberOfBookings($app) >= $booking->getNumberOfBookings())
242  {
243  break;
244  }
245  if(!ilBookingEntry::lookupBookingsOfUser((array) $app, $user))
246  {
247  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
249  $assigned_users[] = $user;
250  }
251  }
252 
253  $unassigned_users = array_diff($users, $assigned_users);
254 
255  if($a_redirect)
256  {
257  $this->sendInfoAboutUnassignedUsers($unassigned_users);
258  $ilCtrl->redirect($this,'appointmentList');
259  }
260  else
261  {
262  return $unassigned_users;
263  }
264 
265  }
266 
272  public function assignUsersToGroup(array $usr_ids)
273  {
274  global $ilCtrl;
275 
276  $group_id = (int) $_REQUEST['grp_id'];
277 
278  $tomorrow = new ilDateTime(time(),IL_CAL_UNIX);
279  $tomorrow->increment(IL_CAL_DAY,1);
280 
281  // Get all future consultation hours
282  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
283  include_once './Services/Booking/classes/class.ilBookingEntry.php';
285  $this->user_id,
286  $group_id,
287  $tomorrow
288  );
289  $users = $usr_ids;
290  $assigned_users = array();
291  foreach($apps as $app)
292  {
294  foreach($users as $user)
295  {
296  if($booking->getCurrentNumberOfBookings($app) >= $booking->getNumberOfBookings())
297  {
298  break;
299  }
300  if(!ilBookingEntry::lookupBookingsOfUser($apps, $user))
301  {
302  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
304  $assigned_users[] = $user;
305  }
306  }
307  }
308 
309  $this->sendInfoAboutUnassignedUsers(array_diff($users, $assigned_users));
310  $ilCtrl->redirect($this,'bookingList');
311  }
312 
313 
318  protected function groupList()
319  {
320  global $ilToolbar, $ilTabs, $tpl;
321 
322  $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
323  $ilToolbar->addButton($this->lng->txt('cal_ch_add_grp'),$this->ctrl->getLinkTarget($this,'addGroup'));
324 
325  $this->setSubTabs();
326  $ilTabs->activateSubTab('cal_ch_app_grp');
327 
328  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroupTableGUI.php';
329  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
330  $gtbl = new ilConsultationHourGroupTableGUI($this,'groupList',$this->getUserId());
332 
333  $tpl->setContent($gtbl->getHTML());
334  }
335 
341  protected function addGroup(ilPropertyFormGUI $form = null)
342  {
343  global $ilTabs, $tpl;
344 
345  $this->setSubTabs();
346  $ilTabs->activateSubTab('cal_ch_app_grp');
347 
348  if($form == null)
349  {
350  $form = $this->initGroupForm();
351  }
352  $tpl->setContent($form->getHTML());
353  }
354 
358  protected function saveGroup()
359  {
360  $form = $this->initGroupForm();
361  if($form->checkInput())
362  {
363  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
364  $group = new ilConsultationHourGroup();
365  $group->setTitle($form->getInput('title'));
366  $group->setMaxAssignments($form->getInput('multiple'));
367  $group->setUserId($this->getUserId());
368  $group->save();
369 
370  ilUtil::sendSuccess($GLOBALS['lng']->txt('settings_saved'),true);
371  $GLOBALS['ilCtrl']->redirect($this,'groupList');
372  }
373 
374  ilUtil::sendFailure($GLOBALS['lng']->txt('err_check_input'),true);
375  $this->addGroup($form);
376  }
377 
383  protected function editGroup(ilPropertyFormGUI $form = null)
384  {
385  global $ilCtrl, $tpl, $ilTabs;
386 
387  $ilCtrl->setParameter($this,'grp_id',(int) $_REQUEST['grp_id']);
388  $this->setSubTabs();
389  $ilTabs->activateSubTab('cal_ch_app_grp');
390 
391  if($form == null)
392  {
393  $form = $this->initGroupForm((int) $_REQUEST['grp_id']);
394  }
395  $tpl->setContent($form->getHTML());
396  }
397 
404  protected function updateGroup()
405  {
406  global $ilCtrl, $tpl, $ilTabs;
407 
408  $ilCtrl->setParameter($this,'grp_id',(int) $_REQUEST['grp_id']);
409 
410  $form = $this->initGroupForm((int) $_REQUEST['grp_id']);
411  if($form->checkInput())
412  {
413  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
414  $group = new ilConsultationHourGroup((int) $_REQUEST['grp_id']);
415  $group->setTitle($form->getInput('title'));
416  $group->setMaxAssignments($form->getInput('multiple'));
417  $group->setUserId($this->getUserId());
418  $group->update();
419 
420  ilUtil::sendSuccess($GLOBALS['lng']->txt('settings_saved'),true);
421  $GLOBALS['ilCtrl']->redirect($this,'groupList');
422  }
423 
424  ilUtil::sendFailure($GLOBALS['lng']->txt('err_check_input'),true);
425  $this->editGroup($form);
426  }
427 
433  protected function confirmDeleteGroup()
434  {
435  global $ilCtrl, $ilTabs, $tpl;
436 
437  $ilCtrl->setParameter($this,'grp_id',(int) $_REQUEST['grp_id']);
438  $groups = array((int) $_REQUEST['grp_id']);
439 
440  $this->setSubTabs();
441  $ilTabs->activateSubTab('cal_ch_app_grp');
442 
443 
444  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
445  $confirm = new ilConfirmationGUI();
446  $confirm->setFormAction($ilCtrl->getFormAction($this));
447  $confirm->setHeaderText($GLOBALS['lng']->txt('cal_ch_grp_delete_sure'));
448  $confirm->setConfirm($GLOBALS['lng']->txt('delete'), 'deleteGroup');
449  $confirm->setCancel($GLOBALS['lng']->txt('cancel'), 'groupList');
450 
451  foreach($groups as $grp_id)
452  {
453  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
454  $group = new ilConsultationHourGroup($grp_id);
455 
456  $confirm->addItem('groups[]', $grp_id, $group->getTitle());
457  }
458  $tpl->setContent($confirm->getHTML());
459  }
460 
464  protected function deleteGroup()
465  {
466  global $ilCtrl;
467 
468  foreach((array) $_REQUEST['groups'] as $grp_id)
469  {
470  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
471  $group = new ilConsultationHourGroup($grp_id);
472  $group->delete();
473  }
474  ilUtil::sendSuccess($GLOBALS['lng']->txt('cal_ch_grp_deleted'));
475  $ilCtrl->redirect($this,'groupList');
476  }
477 
481  protected function initGroupForm($a_group_id = 0)
482  {
483  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
484  $group = new ilConsultationHourGroup($a_group_id);
485 
486  include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
487  $form = new ilPropertyFormGUI();
488  $form->setFormAction($GLOBALS['ilCtrl']->getFormAction($this));
489 
490  if($a_group_id)
491  {
492  $form->setTitle($GLOBALS['lng']->txt('cal_ch_grp_update_tbl'));
493  $form->addCommandButton('updateGroup', $GLOBALS['lng']->txt('save'));
494  $form->addCommandButton('groupList', $GLOBALS['lng']->txt('cancel'));
495  }
496  else
497  {
498  $form->setTitle($GLOBALS['lng']->txt('cal_ch_grp_add_tbl'));
499  $form->addCommandButton('saveGroup', $GLOBALS['lng']->txt('save'));
500  $form->addCommandButton('appointmentList', $GLOBALS['lng']->txt('cancel'));
501  }
502 
503  $title = new ilTextInputGUI($GLOBALS['lng']->txt('title'),'title');
504  $title->setMaxLength(128);
505  $title->setSize(40);
506  $title->setRequired(true);
507  $title->setValue($group->getTitle());
508  $form->addItem($title);
509 
510  $multiple = new ilNumberInputGUI($GLOBALS['lng']->txt('cal_ch_grp_multiple'),'multiple');
511  $multiple->setRequired(true);
512  $multiple->setMinValue(1);
513  $multiple->setSize(1);
514  $multiple->setMaxLength(2);
515  $multiple->setInfo($GLOBALS['lng']->txt('cal_ch_grp_multiple_info'));
516  $multiple->setValue($group->getMaxAssignments());
517  $form->addItem($multiple);
518 
519  return $form;
520  }
521 
525  protected function bookingList()
526  {
527  global $ilToolbar, $ilTabs, $tpl;
528 
529  $this->setSubTabs();
530  $ilTabs->activateSubTab('cal_ch_app_bookings');
531 
532  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourBookingTableGUI.php';
533  $btable = new ilConsultationHourBookingTableGUI($this,'bookingList',$this->getUserId());
535  $tpl->setContent($btable->getHTML());
536  }
537 
541  protected function confirmDeleteBooking()
542  {
543  $this->confirmRejectBooking(false);
544  }
545 
549  protected function confirmRejectBooking($a_send_notification = true)
550  {
551  global $ilTabs, $tpl;
552 
553  $this->setSubTabs();
554  $ilTabs->activateSubTab('cal_ch_app_bookings');
555 
556  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
557 
558  $confirm = new ilConfirmationGUI();
559  $confirm->setFormAction($this->ctrl->getFormAction($this));
560 
561  if($a_send_notification)
562  {
563  ilUtil::sendInfo($this->lng->txt('cal_ch_cancel_booking_info'));
564  $confirm->setHeaderText($this->lng->txt('cal_ch_cancel_booking_sure'));
565  $confirm->setConfirm($this->lng->txt('cal_ch_reject_booking'), 'rejectBooking');
566  }
567  else
568  {
569  ilUtil::sendInfo($this->lng->txt('cal_ch_delete_booking_info'));
570  $confirm->setHeaderText($this->lng->txt('cal_ch_delete_booking_sure'));
571  $confirm->setConfirm($this->lng->txt('cal_ch_delete_booking'), 'deleteBooking');
572  }
573 
574  $confirm->setCancel($this->lng->txt('cancel'),'bookingList');
575 
576  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
577  foreach((array) $_REQUEST['bookuser'] as $bookuser)
578  {
579  $ids = explode('_',$bookuser);
580 
581  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
582  include_once './Services/User/classes/class.ilUserUtil.php';
583  $entry = new ilCalendarEntry($ids[0]);
584  $confirm->addItem(
585  'bookuser[]',
586  $bookuser,
588  $ids[1],
589  true,
590  false,
591  '',
592  true,
593  true
594  ).', '.ilDatePresentation::formatDate($entry->getStart())
595  );
596  }
597  $tpl->setContent($confirm->getHTML());
598  }
599 
603  protected function deleteBooking()
604  {
605  $this->rejectBooking(false);
606  }
607 
612  protected function rejectBooking($a_send_notification = true)
613  {
614  global $ilCtrl;
615 
616  foreach((array) $_REQUEST['bookuser'] as $bookuser)
617  {
618  $ids = explode('_',$bookuser);
619 
620  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
621  ilConsultationHourUtils::cancelBooking($ids[1],$ids[0],$a_send_notification);
622  }
623  if($a_send_notification)
624  {
625  ilUtil::sendSuccess($this->lng->txt('cal_ch_canceled_bookings'),true);
626  }
627  else
628  {
629  ilUtil::sendSuccess($this->lng->txt('cal_ch_deleted_bookings'),true);
630  }
631  $ilCtrl->redirect($this,'bookingList');
632  }
633 
639  protected function appointmentList()
640  {
641  global $ilToolbar, $ilHelp, $ilTabs;
642 
643  $ilHelp->setScreenId("consultation_hours");
644 
645  $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
646  $ilToolbar->addButton($this->lng->txt('cal_ch_add_sequence'),$this->ctrl->getLinkTarget($this,'createSequence'));
647 
648  $this->setSubTabs();
649  $ilTabs->activateSubTab('cal_ch_app_list');
650 
651  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHoursTableGUI.php';
652  $tbl = new ilConsultationHoursTableGUI($this,'appointmentList',$this->getUserId());
653  $tbl->parse();
654  $this->tpl->setContent($tbl->getHTML());
655  }
656 
661  protected function createSequence()
662  {
663  $this->initFormSequence(self::MODE_CREATE);
664 
665  $this->booking = new ilBookingEntry();
666  $this->form->getItemByPostVar('bo')->setValue($this->booking->getNumberOfBookings());
667  $this->form->getItemByPostVar('ap')->setValue(1);
668  $this->form->getItemByPostVar('du')->setMinutes(15);
669  $this->form->getItemByPostVar('st')->setDate(
670  new ilDateTime(mktime(8,0,0,date('n',time()),date('d',time()),date('Y',time())),IL_CAL_UNIX));
671 
672  $this->tpl->setContent($this->form->getHTML());
673  }
674 
680  protected function initFormSequence($a_mode)
681  {
682  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
683 
684  include_once('./Services/YUI/classes/class.ilYuiUtil.php');
686 
687  $this->form = new ilPropertyFormGUI();
688  $this->form->setFormAction($this->ctrl->getFormAction($this));
689 
690  switch($a_mode)
691  {
692  case self::MODE_CREATE:
693  $this->form->setTitle($this->lng->txt('cal_ch_add_sequence'));
694  $this->form->addCommandButton('saveSequence', $this->lng->txt('save'));
695  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
696  break;
697 
698  /*
699  case self::MODE_UPDATE:
700  $this->form->setTitle($this->lng->txt('cal_ch_edit_sequence'));
701  $this->form->addCommandButton('updateSequence', $this->lng->txt('save'));
702  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
703  break;
704  */
705 
706  case self::MODE_MULTI:
707  $this->form->setTitle($this->lng->txt('cal_ch_multi_edit_sequence'));
708  $this->form->addCommandButton('updateMulti', $this->lng->txt('save'));
709  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
710  break;
711  }
712 
713  // in case of existing groups show a selection
714  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
716  {
717  $group = new ilSelectInputGUI($this->lng->txt('cal_ch_grp_selection'),'grp');
718  $group->setOptions($options);
719  $group->setRequired(false);
720  $this->form->addItem($group);
721  }
722 
723  // Title
724  $ti = new ilTextInputGUI($this->lng->txt('title'),'ti');
725  $ti->setSize(32);
726  $ti->setMaxLength(128);
727  $ti->setRequired(true);
728  $this->form->addItem($ti);
729 
730  if($a_mode != self::MODE_MULTI)
731  {
732  // Start
733  include_once './Services/Form/classes/class.ilDateTimeInputGUI.php';
734  $dur = new ilDateTimeInputGUI($this->lng->txt('cal_start'),'st');
735  $dur->setShowTime(true);
736  $dur->setMinuteStepSize(5);
737  $this->form->addItem($dur);
738 
739  // Duration
740  $du = new ilDurationInputGUI($this->lng->txt('cal_ch_duration'),'du');
741  $du->setShowMinutes(true);
742  $du->setShowHours(true);
743  $this->form->addItem($du);
744 
745  // Number of appointments
746  $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_appointments'),'ap');
747  $nu->setInfo($this->lng->txt('cal_ch_num_appointments_info'));
748  $nu->setSize(2);
749  $nu->setMaxLength(2);
750  $nu->setRequired(true);
751  $nu->setMinValue(1);
752  $this->form->addItem($nu);
753 
754  // Recurrence
755  include_once('./Services/Calendar/classes/Form/class.ilRecurrenceInputGUI.php');
756  $rec = new ilRecurrenceInputGUI($this->lng->txt('cal_recurrences'),'frequence');
757  $rec->setEnabledSubForms(
758  array(
762  )
763  );
764  $this->form->addItem($rec);
765  }
766 
767  // Number of bookings
768  $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_bookings'),'bo');
769  $nu->setSize(2);
770  $nu->setMaxLength(2);
771  $nu->setMinValue(1);
772  $nu->setRequired(true);
773  $this->form->addItem($nu);
774 
775  // Deadline
776  $dead = new ilDurationInputGUI($this->lng->txt('cal_ch_deadline'),'dead');
777  $dead->setInfo($this->lng->txt('cal_ch_deadline_info'));
778  $dead->setShowMinutes(false);
779  $dead->setShowHours(true);
780  $dead->setShowDays(true);
781  $this->form->addItem($dead);
782 
783  // Location
784  $lo = new ilTextInputGUI($this->lng->txt('cal_where'),'lo');
785  $lo->setSize(32);
786  $lo->setMaxLength(128);
787  $this->form->addItem($lo);
788 
789  // Description
790  $de = new ilTextAreaInputGUI($this->lng->txt('description'),'de');
791  $de->setRows(10);
792  $de->setCols(60);
793  $this->form->addItem($de);
794 
795  // Target Object
796  $tgt = new ilTextInputGUI($this->lng->txt('cal_ch_target_object'),'tgt');
797  $tgt->setInfo($this->lng->txt('cal_ch_target_object_info'));
798  $tgt->setSize(16);
799  $tgt->setMaxLength(128);
800  $this->form->addItem($tgt);
801  }
802 
807  protected function saveSequence()
808  {
809  global $ilObjDataCache;
810 
811  $this->initFormSequence(self::MODE_CREATE);
812 
813  if($this->form->checkInput())
814  {
815  $this->form->setValuesByPost();
816 
817  $booking = new ilBookingEntry();
818  $booking->setObjId($this->getUserId());
819  $booking->setNumberOfBookings($this->form->getInput('bo'));
820 
821  $deadline = $this->form->getInput('dead');
822  $deadline = $deadline['dd'] * 24 + $deadline['hh'];
823  $booking->setDeadlineHours($deadline);
824 
825  // consultation hour group
826  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
828  {
829  $booking->setBookingGroup((int) $this->form->getInput('grp'));
830  }
831 
832  $tgt = explode(',',$this->form->getInput('tgt'));
833  $obj_ids = array();
834  foreach((array) $tgt as $ref_id)
835  {
836  if(!trim($ref_id))
837  {
838  continue;
839  }
840  $obj_id = $ilObjDataCache->lookupObjId($ref_id);
841  $type = ilObject::_lookupType($obj_id);
842  $valid_types = array('crs','grp');
843  if(!$obj_id or !in_array($type, $valid_types))
844  {
845  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
846  $this->tpl->setContent($this->form->getHTML());
847  return;
848  }
849 
850  $obj_ids[] = $obj_id;
851  }
852  $booking->setTargetObjIds($obj_ids);
853 
854  $booking->save();
856 
857  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
858  $this->ctrl->redirect($this,'appointmentList');
859  }
860  else
861  {
862  $this->form->setValuesByPost();
863  $this->tpl->setContent($this->form->getHTML());
864  }
865  }
866 
873  {
874  include_once './Services/Calendar/classes/class.ilDateList.php';
875  $concurrent_dates = new ilDateList(ilDateList::TYPE_DATETIME);
876  $start = clone $this->form->getItemByPostVar('st')->getDate();
877  for($i = 0; $i < $this->form->getItemByPostVar('ap')->getValue(); $i++)
878  {
879  $concurrent_dates->add(clone $start);
880 
881  $start->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes());
882  $start->increment(ilDateTime::HOUR,$this->form->getItemByPostVar('du')->getHours());
883  #$start = new ilDateTime(,IL_CAL_UNIX);
884  }
885 
886  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
887  $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH,$this->getUserId(),$this->lng->txt('cal_ch_personal_ch'),true);
888 
889  // Add calendar appointment for each
890  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
891  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
892  include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
893  include_once './Services/Booking/classes/class.ilBookingPeriod.php';
894  foreach($concurrent_dates as $dt)
895  {
896  $end = clone $dt;
897  $end->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes());
898  $end->increment(ilDateTime::HOUR,$this->form->getItemByPostVar('du')->getHours());
899 
900  $calc = new ilCalendarRecurrenceCalculator(
901  new ilBookingPeriod($dt,$end),
902  $this->form->getItemByPostVar('frequence')->getRecurrence()
903  );
904 
905  // Calculate with one year limit
906  $limit = clone $dt;
907  $limit->increment(ilDAteTime::YEAR,1);
908 
909  $date_list = $calc->calculateDateList($dt,$limit);
910 
911  foreach($date_list as $app_start)
912  {
913  $app_end = clone $app_start;
914  $app_end->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes());
915  $app_end->increment(ilDateTime::HOUR,$this->form->getItemByPostVar('du')->getHours());
916 
917 
918  $entry = new ilCalendarEntry();
919  $entry->setContextId($booking->getId());
920  $entry->setTitle($this->form->getInput('ti'));
921  $entry->setSubtitle("#consultationhour#"); // dynamic, see ilCalendarEntry
922  $entry->setDescription($this->form->getInput('de'));
923  $entry->setLocation($this->form->getInput('lo'));
924  $entry->setStart($app_start);
925  $entry->setEnd($app_end);
926 
927  $entry->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
928  $entry->save();
929 
930  $cat_assign = new ilCalendarCategoryAssignments($entry->getEntryId());
931  $cat_assign->addAssignment($def_cat->getCategoryID());
932  }
933  }
934  }
935 
940  protected function setTabs()
941  {
942  global $ilTabs, $ilUser, $ilCtrl;
943 
944  $ilCtrl->setParameter($this, 'user_id', '');
945  $ilTabs->addTab('consultation_hours_'.$ilUser->getId(), $this->lng->txt('cal_ch_ch'), $this->ctrl->getLinkTarget($this,'appointmentList'));
946 
948  {
949  $ilCtrl->setParameter($this, 'user_id', $user_id);
950  $ilTabs->addTab('consultation_hours_'.$user_id, $this->lng->txt('cal_ch_ch').': '.$login, $this->ctrl->getLinkTarget($this,'appointmentList'));
951  }
952  $ilCtrl->setParameter($this, 'user_id', '');
953 
954  $ilTabs->addTab('ch_settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this,'settings'));
955 
956  $ilTabs->activateTab('consultation_hours_'.$this->getUserId());
957  }
958 
964  protected function setSubTabs()
965  {
966  global $ilTabs, $ilCtrl;
967 
968  $ilCtrl->setParameter($this,'user_id',$this->getUserId());
969  $ilTabs->addSubTab('cal_ch_app_list',$this->lng->txt('cal_ch_app_list'),$ilCtrl->getLinkTarget($this,'appointmentList'));
970  $ilTabs->addSubTab('cal_ch_app_grp',$this->lng->txt('cal_ch_app_grp'),$ilCtrl->getLinkTarget($this,'groupList'));
971  $ilTabs->addSubTab('cal_ch_app_bookings',$this->lng->txt('cal_ch_app_bookings'),$ilCtrl->getLinkTarget($this,'bookingList'));
972  }
973 
977  public function edit()
978  {
979  global $ilTabs;
980 
981  if(!isset($_REQUEST['apps']))
982  {
983  ilUtil::sendFailure($this->lng->txt('select_one'));
984  return $this->appointmentList();
985  }
986 
987  $this->initFormSequence(self::MODE_MULTI);
988 
989  if($_REQUEST['apps'] && !is_array($_REQUEST['apps']))
990  {
991  $_REQUEST['apps'] = explode(';', $_REQUEST['apps']);
992  }
993 
994  $hidden = new ilHiddenInputGUI('apps');
995  $hidden->setValue(implode(';', $_REQUEST['apps']));
996  $this->form->addItem($hidden);
997 
998  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
999  $first = $_REQUEST['apps'];
1000  $first = array_shift($_REQUEST['apps']);
1001  $entry = new ilCalendarEntry($first);
1002 
1003  $this->form->getItemByPostVar('ti')->setValue($entry->getTitle());
1004  $this->form->getItemByPostVar('lo')->setValue($entry->getLocation());
1005  $this->form->getItemByPostVar('de')->setValue($entry->getDescription());
1006 
1007  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1008  $booking = new ilBookingEntry($entry->getContextId());
1009 
1010  $this->form->getItemByPostVar('bo')->setValue($booking->getNumberOfBookings());
1011 
1012  $ref_ids = array();
1013  foreach($booking->getTargetObjIds() as $obj_id)
1014  {
1015  $refs = ilObject::_getAllReferences($obj_id);
1016  $ref_ids[] = end($refs);
1017  }
1018  $this->form->getItemByPostVar('tgt')->setValue(implode(',',$ref_ids));
1019 
1020  $deadline = $booking->getDeadlineHours();
1021  $this->form->getItemByPostVar('dead')->setDays(floor($deadline/24));
1022  $this->form->getItemByPostVar('dead')->setHours($deadline%24);
1023 
1024  if($booking->getBookingGroup())
1025  {
1026  $this->form->getItemByPostVar('grp')->setValue($booking->getBookingGroup());
1027  }
1028 
1029  $this->tpl->setContent($this->form->getHTML());
1030  }
1031 
1036  protected function updateMulti()
1037  {
1038  global $ilObjDataCache;
1039 
1040  $this->initFormSequence(self::MODE_MULTI);
1041 
1042  if($this->form->checkInput())
1043  {
1044  $this->form->setValuesByPost();
1045  $apps = explode(';', $_POST['apps']);
1046 
1047  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1048  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1049 
1050  // do collision-check if max bookings were reduced
1051  // no collision check
1052  $first = $apps;
1053  $first = array_shift($first);
1055  #if($this->form->getInput('bo') < $entry->getNumberOfBookings())
1056  #{
1057  # $this->edit();
1058  # return;
1059  #}
1060 
1061  // create new context
1062  $booking = new ilBookingEntry();
1063 
1064  $booking->setObjId($this->getUserId());
1065  $booking->setNumberOfBookings($this->form->getInput('bo'));
1066 
1067  $deadline = $this->form->getInput('dead');
1068  $deadline = $deadline['dd']*24+$deadline['hh'];
1069  $booking->setDeadlineHours($deadline);
1070 
1071  $tgt = explode(',',$this->form->getInput('tgt'));
1072  $obj_ids = array();
1073  foreach((array) $tgt as $ref_id)
1074  {
1075  if(!trim($ref_id))
1076  {
1077  continue;
1078  }
1079  $obj_id = $ilObjDataCache->lookupObjId($ref_id);
1080  $type = ilObject::_lookupType($obj_id);
1081  $valid_types = array('crs','grp');
1082  if(!$obj_id or !in_array($type, $valid_types))
1083  {
1084  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
1085  $this->edit();
1086  return;
1087  }
1088  $obj_ids[] = $obj_id;
1089  }
1090  $booking->setTargetObjIds($obj_ids);
1091 
1092  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
1094  {
1095  $booking->setBookingGroup($this->form->getInput('grp'));
1096  }
1097  $booking->save();
1098 
1099 
1100  // update entries
1101  $title = $this->form->getInput('ti');
1102  $location = $this->form->getInput('lo');
1103  $description = $this->form->getInput('de');
1104 
1105  foreach($apps as $item_id)
1106  {
1107  $entry = new ilCalendarEntry($item_id);
1108  $entry->setContextId($booking->getId());
1109  $entry->setTitle($title);
1110  $entry->setLocation($location);
1111  $entry->setDescription($description);
1112  $entry->update();
1113  }
1114 
1116 
1117  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1118  $this->ctrl->redirect($this,'appointmentList');
1119  }
1120  $this->tpl->setContent($this->form->getHTML());
1121  }
1122 
1126  public function confirmDelete()
1127  {
1128  global $tpl;
1129 
1130  if(!isset($_REQUEST['apps']))
1131  {
1132  ilUtil::sendFailure($this->lng->txt('select_one'));
1133  return $this->appointmentList();
1134  }
1135 
1136  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
1137 
1138 
1139  $this->ctrl->saveParameter($this,array('seed','app_id','dt'));
1140 
1141  $confirm = new ilConfirmationGUI();
1142  $confirm->setFormAction($this->ctrl->getFormAction($this));
1143  $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
1144  $confirm->setCancel($this->lng->txt('cancel'),'cancel');
1145 
1146  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1147 
1148  $bookings_available = array();
1149  foreach((array) $_REQUEST['apps'] as $entry_id)
1150  {
1151  $entry = new ilCalendarEntry($entry_id);
1152  $confirm->addItem('apps[]', $entry_id, ilDatePresentation::formatDate($entry->getStart()).', '.$entry->getTitle());
1153 
1154  include_once './Services/Booking/classes/class.ilBookingEntry.php';
1156  {
1157  $bookings_available[] = ilDatePresentation::formatDate($entry->getStart()).', '.$entry->getTitle();
1158  }
1159  }
1160 
1161  if($bookings_available)
1162  {
1163  ilUtil::sendInfo($this->lng->txt('cal_ch_delete_app_booking_info').'<br />'.implode('<br />',$bookings_available));
1164  }
1165 
1166  $confirm->setConfirm($this->lng->txt('delete'),'delete');
1167  $confirm->setCancel($this->lng->txt('cancel'),'appointmentList');
1168 
1169  $tpl->setContent($confirm->getHTML());
1170  }
1171 
1175  public function delete()
1176  {
1177  if(!isset($_POST['apps']))
1178  {
1179  ilUtil::sendFailure($this->lng->txt('select_one'));
1180  return $this->appointmentList();
1181  }
1182 
1183  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1184  include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1185  foreach($_POST['apps'] as $entry_id)
1186  {
1187  // cancel booking for users
1189  if($booking)
1190  {
1191  foreach($booking->getCurrentBookings($entry_id) as $user_id)
1192  {
1193  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
1195  }
1196  }
1197  // remove calendar entries
1198  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
1199  $entry = new ilCalendarEntry($entry_id);
1200  $entry->delete();
1201 
1203  }
1204 
1206 
1207  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'), true);
1208  $this->ctrl->redirect($this, 'appointmentList');
1209  }
1210 
1214  public function showProfile()
1215  {
1216  global $tpl, $ilTabs, $ilCtrl;
1217 
1218  $ilTabs->clearTargets();
1219 
1220  $user_id = (int)$_GET['user'];
1221 
1222  include_once 'Services/User/classes/class.ilPublicUserProfileGUI.php';
1223  $profile = new ilPublicUserProfileGUI($user_id);
1224  $profile->setBackUrl($this->getProfileBackUrl());
1225  $tpl->setContent($ilCtrl->getHTML($profile));
1226  }
1227 
1233  protected function getProfileBackUrl()
1234  {
1235  // from repository
1236  if(isset($_REQUEST["ref_id"]))
1237  {
1238  $url = $this->ctrl->getLinkTargetByClass('ilCalendarMonthGUI');
1239  }
1240  // from panel
1241  else if(isset($_GET['panel']))
1242  {
1243  $url = $this->ctrl->getLinkTargetByClass('ilCalendarPresentationGUI');
1244  }
1245  // from appointments
1246  else
1247  {
1248  $url = $this->ctrl->getLinkTarget($this, 'appointmentList');
1249  }
1250  return $url;
1251  }
1252 
1256  public function settings()
1257  {
1258  global $tpl, $ilTabs, $ilHelp;
1259 
1260  $ilHelp->setScreenId("consultation_hours_settings");
1261  $ilTabs->activateTab('ch_settings');
1262 
1263  $form = $this->initSettingsForm();
1264  $tpl->setContent($form->getHTML());
1265  }
1266 
1271  protected function initSettingsForm()
1272  {
1273  global $ilDB, $ilUser;
1274 
1275  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
1276 
1277  $form = new ilPropertyFormGUI();
1278  $form->setFormAction($this->ctrl->getFormAction($this));
1279 
1280  $mng = new ilTextInputGUI($this->lng->txt('cal_ch_manager'), 'mng');
1281  $mng->setInfo($this->lng->txt('cal_ch_manager_info'));
1282  $form->addItem($mng);
1283 
1284  $mng->setValue(ilConsultationHourAppointments::getManager(true));
1285 
1286  $form->setTitle($this->lng->txt('settings'));
1287  $form->addCommandButton('updateSettings', $this->lng->txt('save'));
1288  // $form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
1289  return $form;
1290  }
1291 
1295  public function updateSettings()
1296  {
1297  global $ilDB, $ilCtrl, $ilUser, $tpl, $ilTabs;
1298 
1299  $form = $this->initSettingsForm();
1300  if($form->checkInput())
1301  {
1302  $mng = $form->getInput('mng');
1304  {
1305  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1306  $ilCtrl->redirect($this, 'settings');
1307  }
1308  else
1309  {
1310  $ilTabs->activateTab('ch_settings');
1311 
1312  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_user'));
1313  $field = $form->getItemByPostVar('mng');
1314  $field->setValue($mng);
1315  $tpl->setContent($form->getHTML());
1316  return;
1317  }
1318  }
1319  }
1320 }
1321 ?>