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