ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarAppointmentGUI.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 
33 include_once('./Services/Calendar/classes/class.ilTimeZone.php');
34 include_once('./Services/Calendar/classes/class.ilDateTime.php');
35 
37 {
38  protected $seed = null;
39  protected $initialDate = null;
40  protected $default_fulltime = true;
41 
42  protected $app = null;
43  protected $rec = null;
44  protected $timezone = null;
45 
46  protected $tpl;
47  protected $lng;
48  protected $ctrl;
49 
57  public function __construct(ilDate $seed,ilDate $initialDate, $a_appointment_id = 0)
58  {
59  global $ilCtrl,$lng;
60 
61  $this->lng = $lng;
62  $lng->loadLanguageModule('dateplaner');
63  $this->ctrl = $ilCtrl;
64 
65  $this->initTimeZone();
66  $this->initSeed($seed);
67  $this->initInitialDate($initialDate);
68  $this->initAppointment($a_appointment_id);
69  }
70 
78  public function executeCommand()
79  {
80  global $ilUser, $ilSetting,$tpl;
81 
82  $next_class = $this->ctrl->getNextClass($this);
83  switch($next_class)
84  {
85 
86  default:
87  $cmd = $this->ctrl->getCmd("add");
88  $this->$cmd();
89  break;
90  }
91  return true;
92  }
93 
101  protected function cancel()
102  {
103  $this->ctrl->returnToParent($this);
104  }
105 
113  protected function initForm($a_mode, $a_as_milestone = false)
114  {
115  global $ilUser,$tpl;
116 
117  include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
118  include_once('./Services/Calendar/classes/class.ilCalendarRecurrenceGUI.php');
119  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
120  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
121  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
122 
123  $this->form = new ilPropertyFormGUI();
124 
125  include_once('./Services/YUI/classes/class.ilYuiUtil.php');
127  $resp_info = false;
128  switch($a_mode)
129  {
130  case 'create':
131  $this->ctrl->saveParameter($this,array('seed','idate'));
132  $this->form->setFormAction($this->ctrl->getFormAction($this));
133  if ($a_as_milestone)
134  {
135  $this->form->setTitle($this->lng->txt('cal_new_ms'));
136  $this->form->addCommandButton('saveMilestone',$this->lng->txt('cal_add_milestone'));
137  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
138  }
139  else
140  {
141  $this->form->setTitle($this->lng->txt('cal_new_app'));
142  $this->form->addCommandButton('save',$this->lng->txt('cal_add_appointment'));
143  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
144  }
145  break;
146 
147  case 'edit':
148  if ($a_as_milestone)
149  {
150  $this->form->setTitle($this->lng->txt('cal_edit_milestone'));
151  }
152  else
153  {
154  $this->form->setTitle($this->lng->txt('cal_edit_appointment'));
155  }
156  $this->ctrl->saveParameter($this,array('seed','app_id','idate'));
157  $this->form->setFormAction($this->ctrl->getFormAction($this));
158 
159  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
160  $cat = $ass->getFirstAssignment();
161  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
162  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat);
163  $type = ilObject::_lookupType($cat_info['obj_id']);
164  if ($a_as_milestone && $cat_info['type'] == ilCalendarCategory::TYPE_OBJ
165  && ($type == "grp" || $type == "crs"))
166  {
167  $resp_info = true;
168  $this->form->addCommandButton('editResponsibleUsers',$this->lng->txt('cal_change_responsible_users'));
169  }
170  $this->form->addCommandButton('update',$this->lng->txt('save'));
171  // $this->form->addCommandButton('askDelete',$this->lng->txt('delete'));
172  $this->form->addCommandButton('cancel',$this->lng->txt('cancel'));
173  break;
174  }
175  // title
176  $title = new ilTextInputGUI($this->lng->txt('title'),'title');
177  $title->setValue($this->app->getTitle());
178  $title->setRequired(true);
179  $title->setMaxLength(128);
180  $title->setSize(32);
181  $this->form->addItem($title);
182 
183  // calendar selection
184  $calendar = new ilSelectInputGUI($this->lng->txt('cal_category_selection'),'calendar');
185  if($_POST['category'])
186  {
187  $calendar->setValue((int) $_POST['calendar']);
188  $selected_calendar = (int) $_POST['calendar'];
189  }
190  else if($_GET['category_id'])
191  {
192  $calendar->setValue((int) $_GET['category_id']);
193  $selected_calendar = (int) $_GET['category_id'];
194  }
195  elseif($a_mode == 'edit')
196  {
197  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
198  $cat = $ass->getFirstAssignment();
199  $calendar->setValue($cat);
200  $selected_calendar = $cat;
201  }
202  elseif(isset($_GET['ref_id']))
203  {
204  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
205  $obj_cal = ilObject::_lookupObjId($_GET['ref_id']);
206  $calendar->setValue(ilCalendarCategories::_lookupCategoryIdByObjId($obj_cal));
207  $selected_calendar = ilCalendarCategories::_lookupCategoryIdByObjId($obj_cal);
208  }
209  $calendar->setRequired(true);
210  $cats = ilCalendarCategories::_getInstance($ilUser->getId());
211  $calendar->setOptions($cats->prepareCategoriesOfUserForSelection());
212 
213  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
214  if(ilCalendarSettings::_getInstance()->isNotificationEnabled())
215  {
216  $notification_cals = $cats->getNotificationCalendars();
217  $notification_cals = count($notification_cals) ? implode(',',$notification_cals) : '';
218  $calendar->addCustomAttribute("onchange=\"ilToggleNotification(new Array(".$notification_cals."));\"");
219  }
220  $this->form->addItem($calendar);
221 
222  if (!$a_as_milestone)
223  {
224  include_once './Services/Form/classes/class.ilDateDurationInputGUI.php';
225  $tpl->addJavaScript('./Services/Form/js/date_duration.js');
226  $dur = new ilDateDurationInputGUI($this->lng->txt('cal_fullday'),'event');
227  $dur->setStartText($this->lng->txt('cal_start'));
228  $dur->setEndText($this->lng->txt('cal_end'));
229  $dur->enableToggleFullTime(
230  $this->lng->txt('cal_fullday_title'),
231  $this->app->isFullday() ? true : false
232  );
233  $dur->setMinuteStepSize(5);
234  $dur->setShowDate(true);
235  $dur->setShowTime(true);
236  $dur->setStart($this->app->getStart());
237  $dur->setEnd($this->app->getEnd());
238  $this->form->addItem($dur);
239 
240  // recurrence
241  include_once('./Services/Calendar/classes/Form/class.ilRecurrenceInputGUI.php');
242  $rec = new ilRecurrenceInputGUI($this->lng->txt('cal_recurrences'),'frequence');
243  $rec->setRecurrence($this->rec);
244  $this->form->addItem($rec);
245 
246  // location
247  $where = new ilTextInputGUI($this->lng->txt('cal_where'),'location');
248  $where->setValue($this->app->getLocation());
249  $where->setMaxLength(128);
250  $where->setSize(32);
251  $this->form->addItem($where);
252  }
253  else
254  {
255  $deadline = new ilDateTimeInputGUI($this->lng->txt('cal_deadline'),'event[start]');
256  $deadline->setDate($this->app->getStart());
257  $deadline->setShowTime(false);
258  $deadline->setMinuteStepSize(5);
259  $this->form->addItem($deadline);
260 
261  // completion
262  $completion_vals = array();
263  for($i = 0; $i <= 100; $i+=5)
264  {
265  $completion_vals[$i] = $i." %";
266  }
267  $compl = new ilSelectInputGUI($this->lng->txt('cal_task_completion'),
268  'completion');
269  $compl->setOptions($completion_vals);
270  $compl->setValue($this->app->getCompletion());
271  $this->form->addItem($compl);
272  }
273 
274  $desc = new ilTextAreaInputGUI($this->lng->txt('description'),'description');
275  $desc->setValue($this->app->getDescription());
276  $desc->setRows(5);
277  $this->form->addItem($desc);
278 
279  if ($a_as_milestone && $a_mode == "edit" && $resp_info)
280  {
281  // users responsible
282  $users = $this->app->readResponsibleUsers();
283  $resp = new ilNonEditableValueGUI($this->lng->txt('cal_responsible'),
284  $users);
285  $delim = "";
286  foreach($users as $r)
287  {
288  $value.= $delim.$r["lastname"].", ".$r["firstname"]." [".$r["login"]."]";
289  $delim = "<br />";
290  }
291  if (count($users) > 0)
292  {
293  $resp->setValue($value);
294  }
295  else
296  {
297  $resp->setValue("-");
298  }
299 
300  $this->form->addItem($resp);
301  }
302 
303  // Notifications
304  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
305  if(ilCalendarSettings::_getInstance()->isNotificationEnabled() and count($cats->getNotificationCalendars()))
306  {
307  $selected_cal = new ilCalendarCategory($selected_calendar);
308  $disabled = true;
309  if($selected_cal->getType() == ilCalendarCategory::TYPE_OBJ)
310  {
311  if(ilObject::_lookupType($selected_cal->getObjId()) == 'crs' or ilObject::_lookupType($selected_cal->getObjId()) == 'grp')
312  {
313  $disabled = false;
314  }
315  }
316 
317  $tpl->addJavaScript('./Services/Calendar/js/toggle_notification.js');
318  $not = new ilCheckboxInputGUI($this->lng->txt('cal_notification'),'not');
319  $not->setInfo($this->lng->txt('cal_notification_info'));
320  $not->setValue(1);
321  $not->setChecked($this->app->isNotificationEnabled());
322  $not->setDisabled($disabled);
323  $this->form->addItem($not);
324  }
325  }
326 
327 
334  protected function add()
335  {
336  global $tpl;
337 
338  $this->initForm('create');
339  $tpl->setContent($this->form->getHTML());
340  }
341 
348  protected function addMilestone()
349  {
350  global $tpl;
351 
352  $this->initForm('create', true);
353  $tpl->setContent($this->form->getHTML());
354  }
355 
361  protected function saveMilestone()
362  {
363  $this->save(true);
364  }
365 
371  protected function save($a_as_milestone = false)
372  {
373  global $ilErr;
374 
375  $this->load($a_as_milestone);
376 
377  if($this->app->validate())
378  {
379  if(!(int) $_POST['calendar'])
380  {
381  $cat_id = $this->createDefaultCalendar();
382  }
383  else
384  {
385  $cat_id = (int) $_POST['calendar'];
386  }
387 
388  $this->app->save();
389  $this->rec->setEntryId($this->app->getEntryId());
390  $this->saveRecurrenceSettings();
391 
392  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
393  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
394  $ass->addAssignment($cat_id);
395 
396  // Send notifications
397  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
398  if(ilCalendarSettings::_getInstance()->isNotificationEnabled() and (bool) $_POST['not'])
399  {
400  $this->distributeNotifications($cat_id,$this->app->getEntryId(),true);
401  }
402 
403  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
404  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
405  $type = ilObject::_lookupType($cat_info['obj_id']);
406 
407  if ($a_as_milestone && $cat_info['type'] == ilCalendarCategory::TYPE_OBJ
408  && ($type == "grp" || $type == "crs"))
409  {
410  ilUtil::sendSuccess($this->lng->txt('cal_created_milestone_resp_q'),true);
411  return $this->showResponsibleUsersList($cat_info['obj_id']);
412  }
413  elseif($a_as_milestone)
414  {
415  ilUtil::sendSuccess($this->lng->txt('cal_created_milestone'),true);
416  $this->ctrl->returnToParent($this);
417  }
418  else
419  {
420  ilUtil::sendSuccess($this->lng->txt('cal_created_appointment'),true);
421  $this->ctrl->returnToParent($this);
422  }
423  }
424  else
425  {
426  ilUtil::sendFailure($ilErr->getMessage());
427  }
428  if ($a_as_milestone)
429  {
430  $this->addMilestone();
431  }
432  else
433  {
434  $this->add();
435  }
436  }
437 
442  protected function distributeNotifications($a_cat_id, $app_id, $a_new_appointment = true)
443  {
444  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
445  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($a_cat_id);
446 
447  include_once './Services/Calendar/classes/class.ilCalendarMailNotification.php';
448  $notification = new ilCalendarMailNotification();
449  $notification->setAppointmentId($app_id);
450 
451  switch($cat_info['type'])
452  {
454 
455  switch($cat_info['obj_type'])
456  {
457  case 'crs':
458  $ref_ids = ilObject::_getAllReferences($cat_info['obj_id']);
459  $ref_id = current($ref_ids);
460  $notification->setRefId($ref_id);
461  $notification->setType(
462  $a_new_appointment ?
465  break;
466 
467  case 'grp':
468  $ref_ids = ilObject::_getAllReferences($cat_info['obj_id']);
469  $ref_id = current($ref_ids);
470  $notification->setRefId($ref_id);
471  $notification->setType(
472  $a_new_appointment ?
475  break;
476  }
477  break;
478  }
479 
480  $notification->send();
481  }
482 
487  {
488  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
489  $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
490  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
491  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
492 
493  $this->showResponsibleUsersList($cat_info['obj_id']);
494  }
495 
500  function showResponsibleUsersList($a_grp_id)
501  {
502  global $tpl;
503 
504  include_once("./Services/Calendar/classes/class.ilMilestoneResponsiblesTableGUI.php");
505  $table_gui = new ilMilestoneResponsiblesTableGUI($this, "", $a_grp_id,
506  $this->app->getEntryId());
507  $tpl->setContent($table_gui->getHTML());
508  }
509 
514  {
515  global $ilCtrl;
516 
517  $this->app->writeResponsibleUsers($_POST["user_id"]);
518  $ilCtrl->returnToParent($this);
519  }
520 
528  protected function edit()
529  {
530  global $tpl,$ilUser,$ilErr;
531 
532  include_once('./Services/Calendar/classes/class.ilCalendarCategory.php');
533  include_once('./Services/Calendar/classes/class.ilCalendarCategories.php');
534  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
535 
536  $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
537  $cats = ilCalendarCategories::_getInstance($ilUser->getId());
538 
539  if(!$cats->isVisible($cat_id))
540  {
541  $ilErr->raiseError($this->lng->txt('permission_denied'),$ilErr->WARNING);
542  return false;
543  }
544  if(!$cats->isEditable($cat_id) or $this->app->isAutoGenerated())
545  {
546  $this->showInfoScreen();
547  return true;
548  }
549 
550  $this->initForm('edit', $this->app->isMilestone());
551  $tpl->setContent($this->form->getHTML());
552  }
553 
560  protected function showInfoScreen()
561  {
562  global $tpl,$ilUser;
563 
564  include_once("./Services/InfoScreen/classes/class.ilInfoScreenGUI.php");
565  $info = new ilInfoScreenGUI($this);
566  $info->setFormAction($this->ctrl->getFormAction($this));
567 
568  if ($this->app->isMilestone())
569  {
570  $info->addSection($this->lng->txt('cal_ms_details'));
571  }
572  else
573  {
574  $info->addSection($this->lng->txt('cal_details'));
575  }
576 
577  // Appointment
578  $info->addProperty($this->lng->txt('appointment'),
580  $this->app->getStart(),
581  $this->app->getEnd()));
582  $info->addProperty($this->lng->txt('title'),$this->app->getPresentationTitle());
583 
584  // Description
585  if(strlen($desc = $this->app->getDescription()))
586  {
587  $info->addProperty($this->lng->txt('description'),$desc);
588  }
589 
590  // Location
591  if(strlen($loc = $this->app->getLocation()))
592  {
593  $info->addProperty($this->lng->txt('cal_where'),$loc);
594  }
595 
596  // completion
597  if ($this->app->isMilestone() && $this->app->getCompletion() > 0)
598  {
599  $info->addProperty($this->lng->txt('cal_task_completion'),
600  $this->app->getCompletion()." %");
601  }
602 
603  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
604  $cat_id = ilCalendarCategoryAssignments::_lookupCategory($this->app->getEntryId());
605  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
606  $type = ilObject::_lookupType($cat_info['obj_id']);
607  if ($this->app->isMilestone() && $cat_info['type'] == ilCalendarCategory::TYPE_OBJ
608  && ($type == "grp" || $type == "crs"))
609  {
610  // users responsible
611  $users = $this->app->readResponsibleUsers();
612  $delim = "";
613  foreach($users as $r)
614  {
615  $value.= $delim.$r["lastname"].", ".$r["firstname"]." [".$r["login"]."]";
616  $delim = "<br />";
617  }
618  if (count($users) > 0)
619  {
620  $info->addProperty($this->lng->txt('cal_responsible'),
621  $value);
622  }
623  }
624 
625  $category = new ilCalendarCategory($cat_id);
626 
627  if($category->getType() == ilCalendarCategory::TYPE_OBJ)
628  {
629  $info->addSection($this->lng->txt('additional_info'));
630 
631  $cat_info = ilCalendarCategories::_getInstance()->getCategoryInfo($cat_id);
632  $refs = ilObject::_getAllReferences($cat_info['obj_id']);
633 
634  include_once('classes/class.ilLink.php');
635  $href = ilLink::_getStaticLink(current($refs),ilObject::_lookupType($cat_info['obj_id']),true);
636  $info->addProperty($this->lng->txt('perma_link'),'<a class="small" href="'.$href.'" target="_top">'.$href.'</a>');
637  }
638 
639  $tpl->setContent($info->getHTML());
640  }
641 
648  protected function update()
649  {
650  global $ilErr;
651 
652  $this->load($this->app->isMilestone());
653 
654  if($this->app->validate())
655  {
656  if(!(int) $_POST['calendar'])
657  {
658  $cat_id = $this->createDefaultCalendar();
659  }
660  else
661  {
662  $cat_id = (int) $_POST['calendar'];
663  }
664 
665  $this->app->update();
666  $this->saveRecurrenceSettings();
667 
668  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
669  $ass = new ilCalendarCategoryAssignments($this->app->getEntryId());
670  $ass->deleteAssignments();
671  $ass->addAssignment($cat_id);
672 
673  // Send notifications
674  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
675  if(ilCalendarSettings::_getInstance()->isNotificationEnabled() and (bool) $_POST['not'])
676  {
677  $this->distributeNotifications($cat_id,$this->app->getEntryId(),false);
678  }
679 
680  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
681  $this->ctrl->returnToParent($this);
682  }
683  else
684  {
685  ilUtil::sendFailure($ilErr->getMessage());
686  }
687 
688  $this->edit();
689 
690  }
691 
698  protected function askDelete()
699  {
700  global $tpl;
701 
702  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
703 
704  $this->ctrl->saveParameter($this,array('seed','app_id','dt','idate'));
705 
706  $confirm = new ilConfirmationGUI();
707  $confirm->setFormAction($this->ctrl->getFormAction($this));
708  $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
709  $confirm->setCancel($this->lng->txt('cancel'),'cancel');
710  $confirm->addItem('appointments[]',$this->app->getEntryId(),$this->app->getTitle());
711 
712  include_once('./Services/Calendar/classes/class.ilCalendarRecurrences.php');
713  if(sizeof(ilCalendarRecurrences::_getRecurrences($_GET['app_id']))
714  && !$this->app->isMilestone())
715  {
716  $confirm->addButton($this->lng->txt('cal_delete_single'),'deleteexclude');
717  $confirm->setConfirm($this->lng->txt('cal_delete_recurrences'),'delete');
718  }
719  else
720  {
721  $confirm->setConfirm($this->lng->txt('delete'),'delete');
722  }
723 
724  $tpl->setContent($confirm->getHTML());
725 
726  }
727 
735  protected function delete()
736  {
737  foreach($_POST['appointments'] as $app_id)
738  {
739  $app = new ilCalendarEntry($app_id);
740  $app->delete();
741 
742  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
744  }
745  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'),true);
746  $this->ctrl->returnToParent($this);
747  }
748 
756  protected function deleteExclude()
757  {
758  include_once('./Services/Calendar/classes/class.ilCalendarRecurrenceExclusion.php');
759  $excl = new ilCalendarRecurrenceExclusion();
760  $excl->setEntryId($_GET['app_id']);
761  $excl->setDate(new ilDate($_GET['dt'], IL_CAL_UNIX));
762  $excl->save();
763 
764  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'),true);
765  $this->ctrl->returnToParent($this);
766  }
767 
775  protected function initTimeZone()
776  {
777  global $ilUser;
778 
779  $this->timezone = $ilUser->getTimeZone();
780  }
781 
786  protected function initInitialDate(ilDate $initialDate)
787  {
788  if(!isset($_GET['hour']))
789  {
790  $this->initialDate = clone $initialDate;
791  $this->default_fulltime = true;
792  }
793  else
794  {
795  if((int) $_GET['hour'] < 10)
796  {
797  $time = '0'.(int) $_GET['hour'].':00:00';
798  }
799  else
800  {
801  $time = (int) $_GET['hour'].':00:00';
802  }
803  $this->initialDate = new ilDateTime($initialDate->get(IL_CAL_DATE).' '.$time,IL_CAL_DATETIME,$this->timezone);
804  $this->default_fulltime = false;
805  }
806  }
807 
815  protected function initSeed(ilDate $seed)
816  {
817  $this->seed = clone $seed;
818  $this->default_fulltime = true;
819  }
820 
828  protected function initAppointment($a_app_id = 0)
829  {
830  include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
831  include_once('./Services/Calendar/classes/class.ilCalendarRecurrences.php');
832  $this->app = new ilCalendarEntry($a_app_id);
833 
834  if(!$a_app_id)
835  {
836  $start = clone $this->initialDate;
837  $this->app->setStart($start);
838 
839  $seed_end = clone $this->initialDate;
840  if($this->default_fulltime)
841  {
842  #$seed_end->increment(IL_CAL_DAY,1);
843  }
844  else
845  {
846  $seed_end->increment(IL_CAL_HOUR,1);
847  }
848  $this->app->setEnd($seed_end);
849  $this->app->setFullday($this->default_fulltime);
850 
851  $this->rec = new ilCalendarRecurrence();
852  }
853  else
854  {
855  $this->rec = ilCalendarRecurrences::_getFirstRecurrence($this->app->getEntryId());
856  }
857 
858  }
859 
867  protected function load($a_as_milestone = false)
868  {
869  if ($a_as_milestone)
870  {
871  $this->app->setMilestone(true);
872  $this->app->setCompletion(ilUtil::stripSlashes($_POST['completion']));
873  }
874 
875  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
876  $this->app->setLocation(ilUtil::stripSlashes($_POST['location']));
877  $this->app->setDescription(ilUtil::stripSlashes($_POST['description']));
878  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
879  $this->app->enableNotification((int) $_POST['not']);
880  if ($a_as_milestone) // milestones are always fullday events
881  {
882  $this->app->setFullday(true);
883  }
884  else
885  {
886  $this->app->setFullday(isset($_POST['event']['fulltime']) ? true : false);
887  }
888 
889  if($this->app->isFullday())
890  {
891  $start = new ilDate($_POST['event']['start']['date']['y'].'-'.$_POST['event']['start']['date']['m'].'-'.$_POST['event']['start']['date']['d'],
892  IL_CAL_DATE);
893  $this->app->setStart($start);
894 
895  $end = new ilDate($_POST['event']['end']['date']['y'].'-'.$_POST['event']['end']['date']['m'].'-'.$_POST['event']['end']['date']['d'],
896  IL_CAL_DATE);
897 
898  if ($a_as_milestone)
899  {
900  // for milestones is end date = start date
901  $this->app->setEnd($start);
902  }
903  else
904  {
905  $this->app->setEnd($end);
906  }
907  }
908  else
909  {
910  $start_dt['year'] = (int) $_POST['event']['start']['date']['y'];
911  $start_dt['mon'] = (int) $_POST['event']['start']['date']['m'];
912  $start_dt['mday'] = (int) $_POST['event']['start']['date']['d'];
913  $start_dt['hours'] = (int) $_POST['event']['start']['time']['h'];
914  $start_dt['minutes'] = (int) $_POST['event']['start']['time']['m'];
915  $start = new ilDateTime($start_dt,IL_CAL_FKT_GETDATE,$this->timezone);
916  $this->app->setStart($start);
917 
918  $end_dt['year'] = (int) $_POST['event']['end']['date']['y'];
919  $end_dt['mon'] = (int) $_POST['event']['end']['date']['m'];
920  $end_dt['mday'] = (int) $_POST['event']['end']['date']['d'];
921  $end_dt['hours'] = (int) $_POST['event']['end']['time']['h'];
922  $end_dt['minutes'] = (int) $_POST['event']['end']['time']['m'];
923  $end = new ilDateTime($end_dt,IL_CAL_FKT_GETDATE,$this->timezone);
924  $this->app->setEnd($end);
925  }
926  $this->loadRecurrenceSettings($a_as_milestone = false);
927  }
928 
935  protected function loadRecurrenceSettings($a_as_milestone = false)
936  {
937  $this->rec->reset();
938 
939  switch($_POST['frequence'])
940  {
941  case IL_CAL_FREQ_DAILY:
942  $this->rec->setFrequenceType($_POST['frequence']);
943  $this->rec->setInterval((int) $_POST['count_DAILY']);
944  break;
945 
946  case IL_CAL_FREQ_WEEKLY:
947  $this->rec->setFrequenceType($_POST['frequence']);
948  $this->rec->setInterval((int) $_POST['count_WEEKLY']);
949  if(is_array($_POST['byday_WEEKLY']))
950  {
951  $this->rec->setBYDAY(ilUtil::stripSlashes(implode(',',$_POST['byday_WEEKLY'])));
952  }
953  break;
954 
955  case IL_CAL_FREQ_MONTHLY:
956  $this->rec->setFrequenceType($_POST['frequence']);
957  $this->rec->setInterval((int) $_POST['count_MONTHLY']);
958  switch((int) $_POST['subtype_MONTHLY'])
959  {
960  case 0:
961  // nothing to do;
962  break;
963 
964  case 1:
965  switch((int) $_POST['monthly_byday_day'])
966  {
967  case 8:
968  // Weekday
969  $this->rec->setBYSETPOS((int) $_POST['monthly_byday_num']);
970  $this->rec->setBYDAY('MO,TU,WE,TH,FR');
971  break;
972 
973  case 9:
974  // Day of month
975  $this->rec->setBYMONTHDAY((int) $_POST['monthly_byday_num']);
976  break;
977 
978  default:
979  $this->rec->setBYDAY((int) $_POST['monthly_byday_num'].$_POST['monthly_byday_day']);
980  break;
981  }
982  break;
983 
984  case 2:
985  $this->rec->setBYMONTHDAY((int) $_POST['monthly_bymonthday']);
986  break;
987  }
988  break;
989 
990  case IL_CAL_FREQ_YEARLY:
991  $this->rec->setFrequenceType($_POST['frequence']);
992  $this->rec->setInterval((int) $_POST['count_YEARLY']);
993  switch((int) $_POST['subtype_YEARLY'])
994  {
995  case 0:
996  // nothing to do;
997  break;
998 
999  case 1:
1000  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
1001  $this->rec->setBYDAY((int) $_POST['yearly_byday_num'].$_POST['yearly_byday']);
1002  break;
1003 
1004  case 2:
1005  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
1006  $this->rec->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
1007  break;
1008  }
1009  break;
1010  }
1011 
1012  // UNTIL
1013  switch((int) $_POST['until_type'])
1014  {
1015  case 1:
1016  $this->rec->setFrequenceUntilDate(null);
1017  // nothing to do
1018  break;
1019 
1020  case 2:
1021  $this->rec->setFrequenceUntilDate(null);
1022  $this->rec->setFrequenceUntilCount((int) $_POST['count']);
1023  break;
1024 
1025  case 3:
1026  $end_dt['year'] = (int) $_POST['until_end']['date']['y'];
1027  $end_dt['mon'] = (int) $_POST['until_end']['date']['m'];
1028  $end_dt['mday'] = (int) $_POST['until_end']['date']['d'];
1029 
1030  $this->rec->setFrequenceUntilCount(0);
1031  $this->rec->setFrequenceUntilDate(new ilDate($end_dt,IL_CAL_FKT_GETDATE,$this->timezone));
1032  break;
1033  }
1034 
1035  }
1036 
1044  protected function saveRecurrenceSettings()
1045  {
1046  switch($_POST['frequence'])
1047  {
1048  case 'NONE':
1049  // No recurrence => delete if there is an recurrence rule
1050  if($this->rec->getRecurrenceId())
1051  {
1052  $this->rec->delete();
1053  }
1054  break;
1055 
1056  default:
1057  if($this->rec->getRecurrenceId())
1058  {
1059  $this->rec->update();
1060  }
1061  else
1062  {
1063  $this->rec->save();
1064  }
1065  break;
1066  }
1067  }
1068 
1075  protected function createDefaultCalendar()
1076  {
1077  global $ilUser,$lng;
1078 
1079  $cat = new ilCalendarCategory();
1080  $cat->setColor(ilCalendarCategory::DEFAULT_COLOR);
1081  $cat->setType(ilCalendarCategory::TYPE_USR);
1082  $cat->setTitle($this->lng->txt('cal_default_calendar'));
1083  $cat->setObjId($ilUser->getId());
1084  return $cat->add();
1085  }
1086 
1091  protected function confirmRegister()
1092  {
1093  global $tpl;
1094 
1095  $entry = new ilCalendarEntry((int) $_GET['app_id']);
1097  new ilDateTime($_GET['dstart'],IL_CAL_UNIX),
1098  new ilDateTime($_GET['dend'],IL_CAL_UNIX)
1099  );
1100 
1101 
1102  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1103  $conf = new ilConfirmationGUI;
1104 
1105  $this->ctrl->setParameter($this,'dstart',(int) $_REQUEST['dstart']);
1106  $this->ctrl->setParameter($this,'dend',(int) $_REQUEST['dend']);
1107 
1108  $conf->setFormAction($this->ctrl->getFormAction($this));
1109  $conf->setHeaderText($this->lng->txt('cal_confirm_reg_info'));
1110  $conf->setConfirm($this->lng->txt('cal_reg_register'), 'register');
1111  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1112  $conf->addItem('app_id', $entry->getEntryId(), $entry->getTitle().' ('.$start.')');
1113 
1114  $tpl->setContent($conf->getHTML());
1115  }
1116 
1121  protected function register()
1122  {
1123  global $ilUser;
1124 
1125  include_once './Services/Calendar/classes/class.ilCalendarRegistration.php';
1126  $reg = new ilCalendarRegistration((int) $_POST['app_id']);
1127  $reg->register(
1128  $ilUser->getId(),
1129  new ilDateTime((int) $_REQUEST['dstart'],IL_CAL_UNIX),
1130  new ilDateTime((int) $_REQUEST['dend'],IL_CAL_UNIX)
1131  );
1132 
1133  ilUtil::sendSuccess($this->lng->txt('cal_reg_registered'),true);
1134  $this->ctrl->returnToParent($this);
1135  }
1136 
1140  public function confirmUnregister()
1141  {
1142  global $tpl;
1143 
1144 
1145  $entry = new ilCalendarEntry((int) $_GET['app_id']);
1147  $dstart = new ilDateTime($_GET['dstart'],IL_CAL_UNIX),
1148  $dend = new ilDateTime($_GET['dend'],IL_CAL_UNIX)
1149  );
1150 
1151 
1152  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1153  $conf = new ilConfirmationGUI;
1154 
1155  $this->ctrl->setParameter($this,'dstart',(int) $_REQUEST['dstart']);
1156  $this->ctrl->setParameter($this,'dend',(int) $_REQUEST['dend']);
1157 
1158  $conf->setFormAction($this->ctrl->getFormAction($this));
1159  $conf->setHeaderText($this->lng->txt('cal_confirm_unreg_info'));
1160  $conf->setConfirm($this->lng->txt('cal_reg_unregister'), 'unregister');
1161  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1162  $conf->addItem('app_id', $entry->getEntryId(), $entry->getTitle().' ('.$start.')');
1163 
1164  $tpl->setContent($conf->getHTML());
1165  }
1166 
1171  protected function unregister()
1172  {
1173  global $ilUser;
1174 
1175  include_once './Services/Calendar/classes/class.ilCalendarRegistration.php';
1176  $reg = new ilCalendarRegistration((int) $_POST['app_id']);
1177  $reg->unregister(
1178  $ilUser->getId(),
1179  new ilDateTime((int) $_REQUEST['dstart'],IL_CAL_UNIX),
1180  new ilDateTime((int) $_REQUEST['dend'],IL_CAL_UNIX)
1181  );
1182 
1183  ilUtil::sendSuccess($this->lng->txt('cal_reg_unregistered'),true);
1184  $this->ctrl->returnToParent($this);
1185  }
1186 
1190  public function book()
1191  {
1192  global $ilUser, $tpl;
1193 
1194  $entry = (int)$_GET['app_id'];
1195  $user = (int)$_GET['bkid'];
1196 
1197  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1198  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1199  $entry = new ilCalendarEntry($entry);
1200  $booking = new ilBookingEntry($entry->getContextId());
1201 
1202  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
1203  $timezone = $ilUser->getTimeZone();
1204  switch($user_settings->getTimeFormat())
1205  {
1207  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1208  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1209  break;
1210 
1212  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1213  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1214  break;
1215  }
1216 
1217  $title .= ' '.$entry->getTitle()." (".ilObjUser::_lookupFullname($user).')';
1218 
1219  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1220  $conf = new ilConfirmationGUI;
1221  $conf->setFormAction($this->ctrl->getFormAction($this));
1222  $conf->setHeaderText($this->lng->txt('cal_confirm_booking_info'));
1223  $conf->setConfirm($this->lng->txt('cal_confirm_booking'), 'bookconfirmed');
1224  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1225  $conf->addItem('app_id', $entry->getEntryId(), $title);
1226 
1227  $tpl->setContent($conf->getHTML());
1228  }
1229 
1233  public function bookconfirmed()
1234  {
1235  global $ilUser;
1236 
1237  $entry = (int)$_POST['app_id'];
1238  $user = (int)$_GET['bkid'];
1239 
1240  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1241  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1242  $entry = new ilCalendarEntry($entry);
1243  $booking = new ilBookingEntry($entry->getContextId());
1244  $booking->book($entry->getEntryId());
1245 
1246  // create user calendar/appointment
1247  include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
1248  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
1249  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1250  $user_entry = clone $entry;
1251  $user_entry->save();
1252  $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH,$ilUser->getId(),$this->lng->txt('cal_ch_personal_ch'),true);
1253  $assign = new ilCalendarCategoryAssignments($user_entry->getEntryId());
1254  $assign->addAssignment($def_cat->getCategoryID());
1255 
1256  ilUtil::sendSuccess($this->lng->txt('cal_booking_confirmed'),true);
1257  $this->ctrl->returnToParent($this);
1258  }
1259 
1264  public function cancelBooking()
1265  {
1266  global $ilUser, $tpl;
1267 
1268  $entry = (int)$_GET['app_id'];
1269 
1270  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1271  $entry = new ilCalendarEntry($entry);
1272 
1273  $category = $this->calendarEntryToCategory($entry);
1274  if($category->getType() == ilCalendarCategory::TYPE_CH)
1275  {
1276  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1277  $booking = new ilBookingEntry($entry->getContextId());
1278  if(!$booking->hasBooked($entry->getEntryId()))
1279  {
1280  $this->ctrl->returnToParent($this);
1281  return false;
1282  }
1283 
1284  $entry_title = ' '.$entry->getTitle()." (".ilObjUser::_lookupFullname($booking->getObjId()).')';
1285  }
1286  else if($category->getType() == ilCalendarCategory::TYPE_BOOK)
1287  {
1288  $entry_title = ' '.$entry->getTitle();
1289  }
1290  else
1291  {
1292  $this->ctrl->returnToParent($this);
1293  return false;
1294  }
1295 
1296  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
1297  $timezone = $ilUser->getTimeZone();
1298  switch($user_settings->getTimeFormat())
1299  {
1301  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1302  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1303  break;
1304 
1306  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1307  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1308  break;
1309  }
1310 
1311  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1312  $conf = new ilConfirmationGUI;
1313  $conf->setFormAction($this->ctrl->getFormAction($this));
1314  $conf->setHeaderText($this->lng->txt('cal_cancel_booking_info'));
1315  $conf->setConfirm($this->lng->txt('cal_cancel_booking'), 'cancelconfirmed');
1316  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1317  $conf->addItem('app_id', $entry->getEntryId(), $title.' - '.$entry_title);
1318 
1319  $tpl->setContent($conf->getHTML());
1320  }
1321 
1326  public function cancelConfirmed()
1327  {
1328  global $ilUser;
1329 
1330  $entry = (int)$_POST['app_id'];
1331  $user = (int)$_GET['bkid'];
1332 
1333  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1334  $entry = new ilCalendarEntry($entry);
1335 
1336  $category = $this->calendarEntryToCategory($entry);
1337  if($category->getType() == ilCalendarCategory::TYPE_CH)
1338  {
1339  // find cloned calendar entry in user calendar
1340  include_once 'Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
1341  $apps = ilConsultationHourAppointments::getAppointmentIds($ilUser->getId(), $entry->getContextId(), $entry->getStart());
1342  if($apps)
1343  {
1344  $ref_entry = new ilCalendarEntry($apps[0]);
1345  $ref_entry->delete();
1346  }
1347 
1348  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1349  $booking = new ilBookingEntry($entry->getContextId());
1350  $booking->cancelBooking($entry->getEntryId());
1351 
1352  // do NOT delete original entry
1353  }
1354  else if($category->getType() == ilCalendarCategory::TYPE_BOOK)
1355  {
1356  include_once 'Modules/BookingManager/classes/class.ilBookingReservation.php';
1357  $booking = new ilBookingReservation($entry->getContextId());
1359  $booking->update();
1360 
1361  $entry->delete();
1362  }
1363 
1364  ilUtil::sendSuccess($this->lng->txt('cal_cancel_booking_confirmed'),true);
1365  $this->ctrl->returnToParent($this);
1366  }
1367 
1373  protected function calendarEntryToCategory(ilCalendarEntry $entry)
1374  {
1375  include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1376  include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
1377  $assignment = new ilCalendarCategoryAssignments($entry->getEntryId());
1378  $assignment = $assignment->getFirstAssignment();
1379  return new ilCalendarCategory($assignment);
1380  }
1381 }
1382 ?>