ILIAS  release_4-3 Revision
 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 
34 {
35  const MODE_CREATE = 1;
36  const MODE_UPDATE = 2;
37  const MODE_MULTI = 3;
38 
39  protected $user_id;
40  protected $ctrl;
41 
42  protected $booking = null;
43 
47  public function __construct()
48  {
49  global $lng, $ilCtrl, $tpl, $ilUser;
50 
51  $user_id = (int)$_GET['user_id'];
52  if($user_id)
53  {
55  {
56  $this->user_id = $user_id;
57  }
58  else
59  {
60  $user_id = false;
61  }
62  }
63  if(!$user_id)
64  {
65  $this->user_id = $ilUser->getId();
66  }
67 
68  $this->ctrl = $ilCtrl;
69  $this->lng = $lng;
70  $this->tpl = $tpl;
71  }
72 
77  public function executeCommand()
78  {
79  global $ilUser, $ilCtrl, $tpl, $ilHelp;
80 
81  $ilHelp->setScreenIdComponent("cal");
82 
83  switch($this->ctrl->getNextClass())
84  {
85  case "ilpublicuserprofilegui":
86  include_once('./Services/User/classes/class.ilPublicUserProfileGUI.php');
87  $profile = new ilPublicUserProfileGUI($this->user_id);
88  $profile->setBackUrl($this->getProfileBackUrl());
89  $ret = $ilCtrl->forwardCommand($profile);
90  $tpl->setContent($ret);
91  break;
92 
93  default:
94  $tpl->setTitle($this->lng->txt("cal_ch_form_header")); // #12220
95 
96  $this->setTabs();
97 
98  if($ilUser->getId() != $this->user_id)
99  {
100  $ilCtrl->setParameter($this, 'user_id', $this->user_id);
101  }
102 
103  $cmd = $this->ctrl->getCmd('appointmentList');
104  $this->$cmd();
105  }
106  }
107 
112  public function getUserId()
113  {
114  return $this->user_id;
115  }
116 
122  protected function appointmentList()
123  {
124  global $ilToolbar, $ilHelp;
125 
126  $ilHelp->setScreenId("consultation_hours");
127 
128  $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
129  $ilToolbar->addButton($this->lng->txt('cal_ch_add_sequence'),$this->ctrl->getLinkTarget($this,'createSequence'));
130 
131  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHoursTableGUI.php';
132  $tbl = new ilConsultationHoursTableGUI($this,'appointmentList',$this->getUserId());
133  $tbl->parse();
134  $this->tpl->setContent($tbl->getHTML());
135  }
136 
141  protected function createSequence()
142  {
143  $this->initFormSequence(self::MODE_CREATE);
144 
145  $this->booking = new ilBookingEntry();
146  $this->form->getItemByPostVar('bo')->setValue($this->booking->getNumberOfBookings());
147  $this->form->getItemByPostVar('ap')->setValue(1);
148  $this->form->getItemByPostVar('du')->setMinutes(15);
149  $this->form->getItemByPostVar('st')->setDate(
150  new ilDateTime(mktime(8,0,0,date('n',time()),date('d',time()),date('Y',time())),IL_CAL_UNIX));
151 
152  $this->tpl->setContent($this->form->getHTML());
153  }
154 
160  protected function initFormSequence($a_mode)
161  {
162  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
163 
164  include_once('./Services/YUI/classes/class.ilYuiUtil.php');
166 
167  $this->form = new ilPropertyFormGUI();
168  $this->form->setFormAction($this->ctrl->getFormAction($this));
169 
170  switch($a_mode)
171  {
172  case self::MODE_CREATE:
173  $this->form->setTitle($this->lng->txt('cal_ch_add_sequence'));
174  $this->form->addCommandButton('saveSequence', $this->lng->txt('save'));
175  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
176  break;
177 
178  /*
179  case self::MODE_UPDATE:
180  $this->form->setTitle($this->lng->txt('cal_ch_edit_sequence'));
181  $this->form->addCommandButton('updateSequence', $this->lng->txt('save'));
182  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
183  break;
184  */
185 
186  case self::MODE_MULTI:
187  $this->form->setTitle($this->lng->txt('cal_ch_multi_edit_sequence'));
188  $this->form->addCommandButton('updateMulti', $this->lng->txt('save'));
189  $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
190  break;
191  }
192 
193  // Title
194  $ti = new ilTextInputGUI($this->lng->txt('title'),'ti');
195  $ti->setSize(32);
196  $ti->setMaxLength(128);
197  $ti->setRequired(true);
198  $this->form->addItem($ti);
199 
200  if($a_mode != self::MODE_MULTI)
201  {
202  // Start
203  include_once './Services/Form/classes/class.ilDateTimeInputGUI.php';
204  $dur = new ilDateTimeInputGUI($this->lng->txt('cal_start'),'st');
205  $dur->setShowTime(true);
206  $dur->setMinuteStepSize(5);
207  $this->form->addItem($dur);
208 
209  // Duration
210  $du = new ilDurationInputGUI($this->lng->txt('cal_ch_duration'),'du');
211  $du->setShowMinutes(true);
212  $du->setShowHours(false);
213  $this->form->addItem($du);
214 
215  // Number of appointments
216  $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_appointments'),'ap');
217  $nu->setInfo($this->lng->txt('cal_ch_num_appointments_info'));
218  $nu->setSize(2);
219  $nu->setMaxLength(2);
220  $nu->setRequired(true);
221  $nu->setMinValue(1);
222  $this->form->addItem($nu);
223 
224  // Recurrence
225  include_once('./Services/Calendar/classes/Form/class.ilRecurrenceInputGUI.php');
226  $rec = new ilRecurrenceInputGUI($this->lng->txt('cal_recurrences'),'frequence');
227  $rec->setEnabledSubForms(
228  array(
232  )
233  );
234  $this->form->addItem($rec);
235  }
236 
237  // Number of bookings
238  $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_bookings'),'bo');
239  $nu->setSize(2);
240  $nu->setMaxLength(2);
241  $nu->setMinValue(1);
242  $nu->setRequired(true);
243  $this->form->addItem($nu);
244 
245  // Deadline
246  $dead = new ilDurationInputGUI($this->lng->txt('cal_ch_deadline'),'dead');
247  $dead->setInfo($this->lng->txt('cal_ch_deadline_info'));
248  $dead->setShowMinutes(false);
249  $dead->setShowHours(true);
250  $dead->setShowDays(true);
251  $this->form->addItem($dead);
252 
253  // Location
254  $lo = new ilTextInputGUI($this->lng->txt('cal_where'),'lo');
255  $lo->setSize(32);
256  $lo->setMaxLength(128);
257  $this->form->addItem($lo);
258 
259  // Description
260  $de = new ilTextAreaInputGUI($this->lng->txt('description'),'de');
261  $de->setRows(10);
262  $de->setCols(60);
263  $this->form->addItem($de);
264 
265  // Target Object
266  $tgt = new ilNumberInputGUI($this->lng->txt('cal_ch_target_object'),'tgt');
267  $tgt->setInfo($this->lng->txt('cal_ch_target_object_info'));
268  $tgt->setSize(6);
269  $this->form->addItem($tgt);
270  }
271 
276  protected function saveSequence()
277  {
278  global $ilObjDataCache;
279 
280  $this->initFormSequence(self::MODE_CREATE);
281 
282  if($this->form->checkInput())
283  {
284  $this->form->setValuesByPost();
285 
286  $booking = new ilBookingEntry();
287  $booking->setObjId($this->getUserId());
288  $booking->setNumberOfBookings($this->form->getInput('bo'));
289 
290  $deadline = $this->form->getInput('dead');
291  $deadline = $deadline['dd']*24+$deadline['hh'];
292  $booking->setDeadlineHours($deadline);
293 
294  $tgt = $this->form->getInput('tgt');
295  if($tgt)
296  {
297  $obj_id = $ilObjDataCache->lookupObjId($tgt);
298  if(!$obj_id)
299  {
300  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
301  $this->tpl->setContent($this->form->getHTML());
302  return;
303  }
304 
305  $booking->setTargetObjId($obj_id);
306  }
307 
308  $booking->save();
309 
311 
312  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
313  $this->ctrl->redirect($this,'appointmentList');
314  }
315  else
316  {
317  $this->form->setValuesByPost();
318  $this->tpl->setContent($this->form->getHTML());
319  }
320  }
321 
328  {
329  include_once './Services/Calendar/classes/class.ilDateList.php';
330  $concurrent_dates = new ilDateList(ilDateList::TYPE_DATETIME);
331  $start = clone $this->form->getItemByPostVar('st')->getDate();
332  for($i = 0; $i < $this->form->getItemByPostVar('ap')->getValue(); $i++)
333  {
334  $concurrent_dates->add($start);
335  $start = new ilDateTime($start->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes()),IL_CAL_UNIX);
336  }
337 
338  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
339  $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH,$this->getUserId(),$this->lng->txt('cal_ch_personal_ch'),true);
340 
341  // Add calendar appointment for each
342  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
343  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
344  include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
345  include_once './Services/Booking/classes/class.ilBookingPeriod.php';
346  foreach($concurrent_dates as $dt)
347  {
348  $end = clone $dt;
349  $end->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes());
350 
351  $calc = new ilCalendarRecurrenceCalculator(
352  new ilBookingPeriod($dt,$end),
353  $this->form->getItemByPostVar('frequence')->getRecurrence()
354  );
355 
356  // Calculate with one year limit
357  $limit = clone $dt;
358  $limit->increment(ilDAteTime::YEAR,1);
359 
360  $date_list = $calc->calculateDateList($dt,$limit);
361 
362  foreach($date_list as $app_start)
363  {
364  $app_end = clone $app_start;
365  $app_end->increment(ilDateTime::MINUTE,$this->form->getItemByPostVar('du')->getMinutes());
366 
367 
368  $entry = new ilCalendarEntry();
369  $entry->setContextId($booking->getId());
370  $entry->setTitle($this->form->getInput('ti'));
371  $entry->setDescription($this->form->getInput('de'));
372  $entry->setLocation($this->form->getInput('lo'));
373  $entry->setStart($app_start);
374  $entry->setEnd($app_end);
375 
376  $entry->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
377  $entry->save();
378 
379  $cat_assign = new ilCalendarCategoryAssignments($entry->getEntryId());
380  $cat_assign->addAssignment($def_cat->getCategoryID());
381  }
382  }
383  }
384 
389  protected function setTabs()
390  {
391  global $ilTabs, $ilUser, $ilCtrl;
392 
393  $ilCtrl->setParameter($this, 'user_id', '');
394  $ilTabs->addTab('consultation_hours_'.$ilUser->getId(), $this->lng->txt('cal_ch_ch'), $this->ctrl->getLinkTarget($this,'appointmentList'));
395 
397  {
398  $ilCtrl->setParameter($this, 'user_id', $user_id);
399  $ilTabs->addTab('consultation_hours_'.$user_id, $this->lng->txt('cal_ch_ch').': '.$login, $this->ctrl->getLinkTarget($this,'appointmentList'));
400  }
401  $ilCtrl->setParameter($this, 'user_id', '');
402 
403  $ilTabs->addTab('settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this,'settings'));
404 
405  $ilTabs->activateTab('consultation_hours_'.$this->getUserId());
406  }
407 
411  public function edit()
412  {
413  global $ilTabs;
414 
415  if(!isset($_POST['apps']))
416  {
417  ilUtil::sendFailure($this->lng->txt('select_one'));
418  return $this->appointmentList();
419  }
420 
421  $this->initFormSequence(self::MODE_MULTI);
422 
423  if($_POST['apps'] && !is_array($_POST['apps']))
424  {
425  $_POST['apps'] = explode(';', $_POST['apps']);
426  }
427 
428  $hidden = new ilHiddenInputGUI('apps');
429  $hidden->setValue(implode(';', $_POST['apps']));
430  $this->form->addItem($hidden);
431 
432  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
433  $first = $_POST['apps'];
434  $first = array_shift($_POST['apps']);
435  $entry = new ilCalendarEntry($first);
436 
437  $this->form->getItemByPostVar('ti')->setValue($entry->getTitle());
438  $this->form->getItemByPostVar('lo')->setValue($entry->getLocation());
439  $this->form->getItemByPostVar('de')->setValue($entry->getDescription());
440 
441  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
442  $booking = new ilBookingEntry($entry->getContextId());
443 
444  $this->form->getItemByPostVar('bo')->setValue($booking->getNumberOfBookings());
445  $this->form->getItemByPostVar('tgt')->setValue($booking->getTargetObjId());
446 
447  $deadline = $booking->getDeadlineHours();
448  $this->form->getItemByPostVar('dead')->setDays(floor($deadline/24));
449  $this->form->getItemByPostVar('dead')->setHours($deadline%24);
450 
451  $this->tpl->setContent($this->form->getHTML());
452  }
453 
458  protected function updateMulti()
459  {
460  global $ilObjDataCache;
461 
462  $this->initFormSequence(self::MODE_MULTI);
463 
464  if($this->form->checkInput())
465  {
466  $this->form->setValuesByPost();
467  $apps = explode(';', $_POST['apps']);
468 
469  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
470  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
471 
472  // do collision-check if max bookings were reduced
473  $first = $apps;
474  $first = array_shift($first);
476  if($this->form->getInput('bo') < $entry->getNumberOfBookings())
477  {
478  $this->edit();
479  return;
480  }
481 
482  // create new context
483 
484  $booking = new ilBookingEntry();
485 
486  $booking->setObjId($this->getUserId());
487  $booking->setNumberOfBookings($this->form->getInput('bo'));
488 
489  $deadline = $this->form->getInput('dead');
490  $deadline = $deadline['dd']*24+$deadline['hh'];
491  $booking->setDeadlineHours($deadline);
492 
493  $tgt = $this->form->getInput('tgt');
494  if($tgt)
495  {
496  // if value was not changed, we already have an object id
497  if($tgt != $entry->getTargetObjId())
498  {
499  $obj_id = $ilObjDataCache->lookupObjId($tgt);
500  if(!$obj_id)
501  {
502  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'), true);
503  $this->edit();
504  return;
505  }
506  $booking->setTargetObjId($obj_id);
507  }
508  else
509  {
510  $booking->setTargetObjId($tgt);
511  }
512  }
513 
514  $booking->save();
515 
516 
517  // update entries
518 
519  $title = $this->form->getInput('ti');
520  $location = $this->form->getInput('lo');
521  $description = $this->form->getInput('de');
522 
523  foreach($apps as $item_id)
524  {
525  $entry = new ilCalendarEntry($item_id);
526  $entry->setContextId($booking->getId());
527  $entry->setTitle($title);
528  $entry->setLocation($location);
529  $entry->setDescription($description);
530  $entry->update();
531  }
532 
534 
535  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
536  $this->ctrl->redirect($this,'appointmentList');
537  }
538  $this->tpl->setContent($this->form->getHTML());
539  }
540 
544  public function confirmDelete()
545  {
546  global $tpl;
547 
548  if(!isset($_POST['apps']))
549  {
550  ilUtil::sendFailure($this->lng->txt('select_one'));
551  return $this->appointmentList();
552  }
553 
554  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
555 
556  $this->ctrl->saveParameter($this,array('seed','app_id','dt'));
557 
558  $confirm = new ilConfirmationGUI();
559  $confirm->setFormAction($this->ctrl->getFormAction($this));
560  $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
561  $confirm->setCancel($this->lng->txt('cancel'),'cancel');
562 
563  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
564  foreach($_POST['apps'] as $entry_id)
565  {
566  $entry = new ilCalendarEntry($entry_id);
567  $confirm->addItem('apps[]', $entry_id, ilDatePresentation::formatDate($entry->getStart()).', '.$entry->getTitle());
568  }
569 
570  $confirm->setConfirm($this->lng->txt('delete'),'delete');
571  $confirm->setCancel($this->lng->txt('cancel'),'appointmentList');
572 
573  $tpl->setContent($confirm->getHTML());
574  }
575 
579  public function delete()
580  {
581  global $tpl;
582 
583  if(!isset($_POST['apps']))
584  {
585  ilUtil::sendFailure($this->lng->txt('select_one'));
586  return $this->appointmentList();
587  }
588 
589  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
590  include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
591  foreach($_POST['apps'] as $entry_id)
592  {
593  // cancel booking for users
595  if($entry)
596  {
597  foreach($entry->getCurrentBookings($entry_id) as $user_id)
598  {
599  $entry->cancelBooking($entry_id, $user_id);
600  }
601  }
602 
603  // remove calendar entries
604  $entry = new ilCalendarEntry($entry_id);
605  $entry->delete();
606 
608  }
609 
611 
612  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'), true);
613  $this->ctrl->redirect($this, 'appointmentList');
614  }
615 
619  public function showProfile()
620  {
621  global $tpl, $ilTabs, $ilCtrl;
622 
623  $ilTabs->clearTargets();
624 
625  $user_id = (int)$_GET['user'];
626 
627  include_once 'Services/User/classes/class.ilPublicUserProfileGUI.php';
628  $profile = new ilPublicUserProfileGUI($user_id);
629  $profile->setBackUrl($this->getProfileBackUrl());
630  $tpl->setContent($ilCtrl->getHTML($profile));
631  }
632 
638  protected function getProfileBackUrl()
639  {
640  // from repository
641  if(isset($_REQUEST["ref_id"]))
642  {
643  $url = $this->ctrl->getLinkTargetByClass('ilCalendarMonthGUI');
644  }
645  // from panel
646  else if(isset($_GET['panel']))
647  {
648  $url = $this->ctrl->getLinkTargetByClass('ilCalendarPresentationGUI');
649  }
650  // from appointments
651  else
652  {
653  $url = $this->ctrl->getLinkTarget($this, 'appointmentList');
654  }
655  return $url;
656  }
657 
661  public function settings()
662  {
663  global $tpl, $ilTabs, $ilHelp;
664 
665  $ilHelp->setScreenId("consultation_hours_settings");
666  $ilTabs->activateTab('settings');
667 
668  $form = $this->initSettingsForm();
669  $tpl->setContent($form->getHTML());
670  }
671 
676  protected function initSettingsForm()
677  {
678  global $ilDB, $ilUser;
679 
680  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
681 
682  $form = new ilPropertyFormGUI();
683  $form->setFormAction($this->ctrl->getFormAction($this));
684 
685  $mng = new ilTextInputGUI($this->lng->txt('cal_ch_manager'), 'mng');
686  $mng->setInfo($this->lng->txt('cal_ch_manager_info'));
687  $form->addItem($mng);
688 
689  $mng->setValue(ilConsultationHourAppointments::getManager(true));
690 
691  $form->setTitle($this->lng->txt('settings'));
692  $form->addCommandButton('updateSettings', $this->lng->txt('save'));
693  // $form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
694  return $form;
695  }
696 
700  public function updateSettings()
701  {
702  global $ilDB, $ilCtrl, $ilUser, $tpl, $ilTabs;
703 
704  $form = $this->initSettingsForm();
705  if($form->checkInput())
706  {
707  $mng = $form->getInput('mng');
709  {
710  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
711  $ilCtrl->redirect($this, 'appointmentList');
712  }
713  else
714  {
715  $ilTabs->activateTab('settings');
716 
717  ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_user'));
718  $field = $form->getItemByPostVar('mng');
719  $field->setValue($mng);
720  $tpl->setContent($form->getHTML());
721  return;
722  }
723  }
724  }
725 }
726 ?>