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