ILIAS  Release_4_4_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 
916 
917  $entry = new ilCalendarEntry();
918  $entry->setContextId($booking->getId());
919  $entry->setTitle($this->form->getInput('ti'));
920  $entry->setSubtitle("#consultationhour#"); // dynamic, see ilCalendarEntry
921  $entry->setDescription($this->form->getInput('de'));
922  $entry->setLocation($this->form->getInput('lo'));
923  $entry->setStart($app_start);
924  $entry->setEnd($app_end);
925 
926  $entry->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
927  $entry->save();
928 
929  $cat_assign = new ilCalendarCategoryAssignments($entry->getEntryId());
930  $cat_assign->addAssignment($def_cat->getCategoryID());
931  }
932  }
933  }
934 
939  protected function setTabs()
940  {
941  global $ilTabs, $ilUser, $ilCtrl;
942 
943  $ilCtrl->setParameter($this, 'user_id', '');
944  $ilTabs->addTab('consultation_hours_'.$ilUser->getId(), $this->lng->txt('cal_ch_ch'), $this->ctrl->getLinkTarget($this,'appointmentList'));
945 
947  {
948  $ilCtrl->setParameter($this, 'user_id', $user_id);
949  $ilTabs->addTab('consultation_hours_'.$user_id, $this->lng->txt('cal_ch_ch').': '.$login, $this->ctrl->getLinkTarget($this,'appointmentList'));
950  }
951  $ilCtrl->setParameter($this, 'user_id', '');
952 
953  $ilTabs->addTab('ch_settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this,'settings'));
954 
955  $ilTabs->activateTab('consultation_hours_'.$this->getUserId());
956  }
957 
963  protected function setSubTabs()
964  {
965  global $ilTabs, $ilCtrl;
966 
967  $ilCtrl->setParameter($this,'user_id',$this->getUserId());
968  $ilTabs->addSubTab('cal_ch_app_list',$this->lng->txt('cal_ch_app_list'),$ilCtrl->getLinkTarget($this,'appointmentList'));
969  $ilTabs->addSubTab('cal_ch_app_grp',$this->lng->txt('cal_ch_app_grp'),$ilCtrl->getLinkTarget($this,'groupList'));
970  $ilTabs->addSubTab('cal_ch_app_bookings',$this->lng->txt('cal_ch_app_bookings'),$ilCtrl->getLinkTarget($this,'bookingList'));
971  }
972 
976  public function edit()
977  {
978  global $ilTabs;
979 
980  if(!isset($_REQUEST['apps']))
981  {
982  ilUtil::sendFailure($this->lng->txt('select_one'));
983  return $this->appointmentList();
984  }
985 
986  $this->initFormSequence(self::MODE_MULTI);
987 
988  if($_REQUEST['apps'] && !is_array($_REQUEST['apps']))
989  {
990  $_REQUEST['apps'] = explode(';', $_REQUEST['apps']);
991  }
992 
993  $hidden = new ilHiddenInputGUI('apps');
994  $hidden->setValue(implode(';', $_REQUEST['apps']));
995  $this->form->addItem($hidden);
996 
997  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
998  $first = $_REQUEST['apps'];
999  $first = array_shift($_REQUEST['apps']);
1000  $entry = new ilCalendarEntry($first);
1001 
1002  $this->form->getItemByPostVar('ti')->setValue($entry->getTitle());
1003  $this->form->getItemByPostVar('lo')->setValue($entry->getLocation());
1004  $this->form->getItemByPostVar('de')->setValue($entry->getDescription());
1005 
1006  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1007  $booking = new ilBookingEntry($entry->getContextId());
1008 
1009  $this->form->getItemByPostVar('bo')->setValue($booking->getNumberOfBookings());
1010 
1011  $ref_ids = array();
1012  foreach($booking->getTargetObjIds() as $obj_id)
1013  {
1014  $refs = ilObject::_getAllReferences($obj_id);
1015  $ref_ids[] = end($refs);
1016  }
1017  $this->form->getItemByPostVar('tgt')->setValue(implode(',',$ref_ids));
1018 
1019  $deadline = $booking->getDeadlineHours();
1020  $this->form->getItemByPostVar('dead')->setDays(floor($deadline/24));
1021  $this->form->getItemByPostVar('dead')->setHours($deadline%24);
1022 
1023  if($booking->getBookingGroup())
1024  {
1025  $this->form->getItemByPostVar('grp')->setValue($booking->getBookingGroup());
1026  }
1027 
1028  $this->tpl->setContent($this->form->getHTML());
1029  }
1030 
1035  protected function updateMulti()
1036  {
1037  global $ilObjDataCache;
1038 
1039  $this->initFormSequence(self::MODE_MULTI);
1040 
1041  if($this->form->checkInput())
1042  {
1043  $this->form->setValuesByPost();
1044  $apps = explode(';', $_POST['apps']);
1045 
1046  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1047  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1048 
1049  // do collision-check if max bookings were reduced
1050  // no collision check
1051  $first = $apps;
1052  $first = array_shift($first);
1054  #if($this->form->getInput('bo') < $entry->getNumberOfBookings())
1055  #{
1056  # $this->edit();
1057  # return;
1058  #}
1059 
1060  // create new context
1061  $booking = new ilBookingEntry();
1062 
1063  $booking->setObjId($this->getUserId());
1064  $booking->setNumberOfBookings($this->form->getInput('bo'));
1065 
1066  $deadline = $this->form->getInput('dead');
1067  $deadline = $deadline['dd']*24+$deadline['hh'];
1068  $booking->setDeadlineHours($deadline);
1069 
1070  $tgt = explode(',',$this->form->getInput('tgt'));
1071  $obj_ids = array();
1072  foreach((array) $tgt as $ref_id)
1073  {
1074  if(!trim($ref_id))
1075  {
1076  continue;
1077  }
1078  $obj_id = $ilObjDataCache->lookupObjId($ref_id);
1079  $type = ilObject::_lookupType($obj_id);
1080  $valid_types = array('crs','grp');
1081  if(!$obj_id or !in_array($type, $valid_types))
1082  {
1083  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
1084  $this->edit();
1085  return;
1086  }
1087  $obj_ids[] = $obj_id;
1088  }
1089  $booking->setTargetObjIds($obj_ids);
1090 
1091  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
1093  {
1094  $booking->setBookingGroup($this->form->getInput('grp'));
1095  }
1096  $booking->save();
1097 
1098 
1099  // update entries
1100  $title = $this->form->getInput('ti');
1101  $location = $this->form->getInput('lo');
1102  $description = $this->form->getInput('de');
1103 
1104  foreach($apps as $item_id)
1105  {
1106  $entry = new ilCalendarEntry($item_id);
1107  $entry->setContextId($booking->getId());
1108  $entry->setTitle($title);
1109  $entry->setLocation($location);
1110  $entry->setDescription($description);
1111  $entry->update();
1112  }
1113 
1115 
1116  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1117  $this->ctrl->redirect($this,'appointmentList');
1118  }
1119  $this->tpl->setContent($this->form->getHTML());
1120  }
1121 
1125  public function confirmDelete()
1126  {
1127  global $tpl;
1128 
1129  if(!isset($_REQUEST['apps']))
1130  {
1131  ilUtil::sendFailure($this->lng->txt('select_one'));
1132  return $this->appointmentList();
1133  }
1134 
1135  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
1136 
1137 
1138  $this->ctrl->saveParameter($this,array('seed','app_id','dt'));
1139 
1140  $confirm = new ilConfirmationGUI();
1141  $confirm->setFormAction($this->ctrl->getFormAction($this));
1142  $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
1143  $confirm->setCancel($this->lng->txt('cancel'),'cancel');
1144 
1145  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1146 
1147  $bookings_available = array();
1148  foreach((array) $_REQUEST['apps'] as $entry_id)
1149  {
1150  $entry = new ilCalendarEntry($entry_id);
1151  $confirm->addItem('apps[]', $entry_id, ilDatePresentation::formatDate($entry->getStart()).', '.$entry->getTitle());
1152 
1153  include_once './Services/Booking/classes/class.ilBookingEntry.php';
1155  {
1156  $bookings_available[] = ilDatePresentation::formatDate($entry->getStart()).', '.$entry->getTitle();
1157  }
1158  }
1159 
1160  if($bookings_available)
1161  {
1162  ilUtil::sendInfo($this->lng->txt('cal_ch_delete_app_booking_info').'<br />'.implode('<br />',$bookings_available));
1163  }
1164 
1165  $confirm->setConfirm($this->lng->txt('delete'),'delete');
1166  $confirm->setCancel($this->lng->txt('cancel'),'appointmentList');
1167 
1168  $tpl->setContent($confirm->getHTML());
1169  }
1170 
1174  public function delete()
1175  {
1176  if(!isset($_POST['apps']))
1177  {
1178  ilUtil::sendFailure($this->lng->txt('select_one'));
1179  return $this->appointmentList();
1180  }
1181 
1182  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1183  include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1184  foreach($_POST['apps'] as $entry_id)
1185  {
1186  // cancel booking for users
1188  if($booking)
1189  {
1190  foreach($booking->getCurrentBookings($entry_id) as $user_id)
1191  {
1192  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
1194  }
1195  }
1196  // remove calendar entries
1197  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
1198  $entry = new ilCalendarEntry($entry_id);
1199  $entry->delete();
1200 
1202  }
1203 
1205 
1206  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'), true);
1207  $this->ctrl->redirect($this, 'appointmentList');
1208  }
1209 
1213  public function showProfile()
1214  {
1215  global $tpl, $ilTabs, $ilCtrl;
1216 
1217  $ilTabs->clearTargets();
1218 
1219  $user_id = (int)$_GET['user'];
1220 
1221  include_once 'Services/User/classes/class.ilPublicUserProfileGUI.php';
1222  $profile = new ilPublicUserProfileGUI($user_id);
1223  $profile->setBackUrl($this->getProfileBackUrl());
1224  $tpl->setContent($ilCtrl->getHTML($profile));
1225  }
1226 
1232  protected function getProfileBackUrl()
1233  {
1234  // from repository
1235  if(isset($_REQUEST["ref_id"]))
1236  {
1237  $url = $this->ctrl->getLinkTargetByClass('ilCalendarMonthGUI');
1238  }
1239  // from panel
1240  else if(isset($_GET['panel']))
1241  {
1242  $url = $this->ctrl->getLinkTargetByClass('ilCalendarPresentationGUI');
1243  }
1244  // from appointments
1245  else
1246  {
1247  $url = $this->ctrl->getLinkTarget($this, 'appointmentList');
1248  }
1249  return $url;
1250  }
1251 
1255  public function settings()
1256  {
1257  global $tpl, $ilTabs, $ilHelp;
1258 
1259  $ilHelp->setScreenId("consultation_hours_settings");
1260  $ilTabs->activateTab('ch_settings');
1261 
1262  $form = $this->initSettingsForm();
1263  $tpl->setContent($form->getHTML());
1264  }
1265 
1270  protected function initSettingsForm()
1271  {
1272  global $ilDB, $ilUser;
1273 
1274  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
1275 
1276  $form = new ilPropertyFormGUI();
1277  $form->setFormAction($this->ctrl->getFormAction($this));
1278 
1279  $mng = new ilTextInputGUI($this->lng->txt('cal_ch_manager'), 'mng');
1280  $mng->setInfo($this->lng->txt('cal_ch_manager_info'));
1281  $form->addItem($mng);
1282 
1283  $mng->setValue(ilConsultationHourAppointments::getManager(true));
1284 
1285  $form->setTitle($this->lng->txt('settings'));
1286  $form->addCommandButton('updateSettings', $this->lng->txt('save'));
1287  // $form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
1288  return $form;
1289  }
1290 
1294  public function updateSettings()
1295  {
1296  global $ilDB, $ilCtrl, $ilUser, $tpl, $ilTabs;
1297 
1298  $form = $this->initSettingsForm();
1299  if($form->checkInput())
1300  {
1301  $mng = $form->getInput('mng');
1303  {
1304  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1305  $ilCtrl->redirect($this, 'settings');
1306  }
1307  else
1308  {
1309  $ilTabs->activateTab('ch_settings');
1310 
1311  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_user'));
1312  $field = $form->getItemByPostVar('mng');
1313  $field->setValue($mng);
1314  $tpl->setContent($form->getHTML());
1315  return;
1316  }
1317  }
1318  }
1319 }
1320 ?>