ILIAS  eassessment Revision 61809
 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(),false);
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  {
715  $confirm->addButton($this->lng->txt('cal_delete_single'),'deleteexclude');
716  $confirm->setConfirm($this->lng->txt('cal_delete_recurrences'),'delete');
717  }
718  else
719  {
720  $confirm->setConfirm($this->lng->txt('delete'),'delete');
721  }
722 
723  $tpl->setContent($confirm->getHTML());
724 
725  }
726 
734  protected function delete()
735  {
736  foreach($_POST['appointments'] as $app_id)
737  {
738  $app = new ilCalendarEntry($app_id);
739  $app->delete();
740 
741  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
743  }
744  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'),true);
745  $this->ctrl->returnToParent($this);
746  }
747 
755  protected function deleteExclude()
756  {
757  include_once('./Services/Calendar/classes/class.ilCalendarRecurrenceExclusion.php');
758  $excl = new ilCalendarRecurrenceExclusion();
759  $excl->setEntryId($_GET['app_id']);
760  $excl->setDate(new ilDate($_GET['dt'], IL_CAL_UNIX));
761  $excl->save();
762 
763  ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'),true);
764  $this->ctrl->returnToParent($this);
765  }
766 
774  protected function initTimeZone()
775  {
776  global $ilUser;
777 
778  $this->timezone = $ilUser->getTimeZone();
779  }
780 
785  protected function initInitialDate(ilDate $initialDate)
786  {
787  if(!isset($_GET['hour']))
788  {
789  $this->initialDate = clone $initialDate;
790  $this->default_fulltime = true;
791  }
792  else
793  {
794  if((int) $_GET['hour'] < 10)
795  {
796  $time = '0'.(int) $_GET['hour'].':00:00';
797  }
798  else
799  {
800  $time = (int) $_GET['hour'].':00:00';
801  }
802  $this->initialDate = new ilDateTime($initialDate->get(IL_CAL_DATE).' '.$time,IL_CAL_DATETIME,$this->timezone);
803  $this->default_fulltime = false;
804  }
805  }
806 
814  protected function initSeed(ilDate $seed)
815  {
816  $this->seed = clone $seed;
817  $this->default_fulltime = true;
818  }
819 
827  protected function initAppointment($a_app_id = 0)
828  {
829  include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
830  include_once('./Services/Calendar/classes/class.ilCalendarRecurrences.php');
831  $this->app = new ilCalendarEntry($a_app_id);
832 
833  if(!$a_app_id)
834  {
835  $start = clone $this->initialDate;
836  $this->app->setStart($start);
837 
838  $seed_end = clone $this->initialDate;
839  if($this->default_fulltime)
840  {
841  #$seed_end->increment(IL_CAL_DAY,1);
842  }
843  else
844  {
845  $seed_end->increment(IL_CAL_HOUR,1);
846  }
847  $this->app->setEnd($seed_end);
848  $this->app->setFullday($this->default_fulltime);
849 
850  $this->rec = new ilCalendarRecurrence();
851  }
852  else
853  {
854  $this->rec = ilCalendarRecurrences::_getFirstRecurrence($this->app->getEntryId());
855  }
856 
857  }
858 
866  protected function load($a_as_milestone = false)
867  {
868  if ($a_as_milestone)
869  {
870  $this->app->setMilestone(true);
871  $this->app->setCompletion(ilUtil::stripSlashes($_POST['completion']));
872  }
873 
874  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
875  $this->app->setLocation(ilUtil::stripSlashes($_POST['location']));
876  $this->app->setDescription(ilUtil::stripSlashes($_POST['description']));
877  $this->app->setTitle(ilUtil::stripSlashes($_POST['title']));
878  $this->app->enableNotification((int) $_POST['not']);
879  if ($a_as_milestone) // milestones are always fullday events
880  {
881  $this->app->setFullday(true);
882  }
883  else
884  {
885  $this->app->setFullday(isset($_POST['event']['fulltime']) ? true : false);
886  }
887 
888  if($this->app->isFullday())
889  {
890  $start = new ilDate($_POST['event']['start']['date']['y'].'-'.$_POST['event']['start']['date']['m'].'-'.$_POST['event']['start']['date']['d'],
891  IL_CAL_DATE);
892  $this->app->setStart($start);
893 
894  $end = new ilDate($_POST['event']['end']['date']['y'].'-'.$_POST['event']['end']['date']['m'].'-'.$_POST['event']['end']['date']['d'],
895  IL_CAL_DATE);
896 
897  if ($a_as_milestone)
898  {
899  // for milestones is end date = start date
900  $this->app->setEnd($start);
901  }
902  else
903  {
904  $this->app->setEnd($end);
905  }
906  }
907  else
908  {
909  $start_dt['year'] = (int) $_POST['event']['start']['date']['y'];
910  $start_dt['mon'] = (int) $_POST['event']['start']['date']['m'];
911  $start_dt['mday'] = (int) $_POST['event']['start']['date']['d'];
912  $start_dt['hours'] = (int) $_POST['event']['start']['time']['h'];
913  $start_dt['minutes'] = (int) $_POST['event']['start']['time']['m'];
914  $start = new ilDateTime($start_dt,IL_CAL_FKT_GETDATE,$this->timezone);
915  $this->app->setStart($start);
916 
917  $end_dt['year'] = (int) $_POST['event']['end']['date']['y'];
918  $end_dt['mon'] = (int) $_POST['event']['end']['date']['m'];
919  $end_dt['mday'] = (int) $_POST['event']['end']['date']['d'];
920  $end_dt['hours'] = (int) $_POST['event']['end']['time']['h'];
921  $end_dt['minutes'] = (int) $_POST['event']['end']['time']['m'];
922  $end = new ilDateTime($end_dt,IL_CAL_FKT_GETDATE,$this->timezone);
923  $this->app->setEnd($end);
924  }
925  $this->loadRecurrenceSettings($a_as_milestone = false);
926  }
927 
934  protected function loadRecurrenceSettings($a_as_milestone = false)
935  {
936  $this->rec->reset();
937 
938  switch($_POST['frequence'])
939  {
940  case IL_CAL_FREQ_DAILY:
941  $this->rec->setFrequenceType($_POST['frequence']);
942  $this->rec->setInterval((int) $_POST['count_DAILY']);
943  break;
944 
945  case IL_CAL_FREQ_WEEKLY:
946  $this->rec->setFrequenceType($_POST['frequence']);
947  $this->rec->setInterval((int) $_POST['count_WEEKLY']);
948  if(is_array($_POST['byday_WEEKLY']))
949  {
950  $this->rec->setBYDAY(ilUtil::stripSlashes(implode(',',$_POST['byday_WEEKLY'])));
951  }
952  break;
953 
954  case IL_CAL_FREQ_MONTHLY:
955  $this->rec->setFrequenceType($_POST['frequence']);
956  $this->rec->setInterval((int) $_POST['count_MONTHLY']);
957  switch((int) $_POST['subtype_MONTHLY'])
958  {
959  case 0:
960  // nothing to do;
961  break;
962 
963  case 1:
964  switch((int) $_POST['monthly_byday_day'])
965  {
966  case 8:
967  // Weekday
968  $this->rec->setBYSETPOS((int) $_POST['monthly_byday_num']);
969  $this->rec->setBYDAY('MO,TU,WE,TH,FR');
970  break;
971 
972  case 9:
973  // Day of month
974  $this->rec->setBYMONTHDAY((int) $_POST['monthly_byday_num']);
975  break;
976 
977  default:
978  $this->rec->setBYDAY((int) $_POST['monthly_byday_num'].$_POST['monthly_byday_day']);
979  break;
980  }
981  break;
982 
983  case 2:
984  $this->rec->setBYMONTHDAY((int) $_POST['monthly_bymonthday']);
985  break;
986  }
987  break;
988 
989  case IL_CAL_FREQ_YEARLY:
990  $this->rec->setFrequenceType($_POST['frequence']);
991  $this->rec->setInterval((int) $_POST['count_YEARLY']);
992  switch((int) $_POST['subtype_YEARLY'])
993  {
994  case 0:
995  // nothing to do;
996  break;
997 
998  case 1:
999  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_byday']);
1000  $this->rec->setBYDAY((int) $_POST['yearly_byday_num'].$_POST['yearly_byday']);
1001  break;
1002 
1003  case 2:
1004  $this->rec->setBYMONTH((int) $_POST['yearly_bymonth_by_monthday']);
1005  $this->rec->setBYMONTHDAY((int) $_POST['yearly_bymonthday']);
1006  break;
1007  }
1008  break;
1009  }
1010 
1011  // UNTIL
1012  switch((int) $_POST['until_type'])
1013  {
1014  case 1:
1015  $this->rec->setFrequenceUntilDate(null);
1016  // nothing to do
1017  break;
1018 
1019  case 2:
1020  $this->rec->setFrequenceUntilDate(null);
1021  $this->rec->setFrequenceUntilCount((int) $_POST['count']);
1022  break;
1023 
1024  case 3:
1025  $end_dt['year'] = (int) $_POST['until_end']['date']['y'];
1026  $end_dt['mon'] = (int) $_POST['until_end']['date']['m'];
1027  $end_dt['mday'] = (int) $_POST['until_end']['date']['d'];
1028 
1029  $this->rec->setFrequenceUntilCount(0);
1030  $this->rec->setFrequenceUntilDate(new ilDate($end_dt,IL_CAL_FKT_GETDATE,$this->timezone));
1031  break;
1032  }
1033 
1034  }
1035 
1043  protected function saveRecurrenceSettings()
1044  {
1045  switch($_POST['frequence'])
1046  {
1047  case 'NONE':
1048  // No recurrence => delete if there is an recurrence rule
1049  if($this->rec->getRecurrenceId())
1050  {
1051  $this->rec->delete();
1052  }
1053  break;
1054 
1055  default:
1056  if($this->rec->getRecurrenceId())
1057  {
1058  $this->rec->update();
1059  }
1060  else
1061  {
1062  $this->rec->save();
1063  }
1064  break;
1065  }
1066  }
1067 
1074  protected function createDefaultCalendar()
1075  {
1076  global $ilUser,$lng;
1077 
1078  $cat = new ilCalendarCategory();
1079  $cat->setColor(ilCalendarCategory::DEFAULT_COLOR);
1080  $cat->setType(ilCalendarCategory::TYPE_USR);
1081  $cat->setTitle($this->lng->txt('cal_default_calendar'));
1082  $cat->setObjId($ilUser->getId());
1083  return $cat->add();
1084  }
1085 
1090  protected function confirmRegister()
1091  {
1092  global $tpl;
1093 
1094  $entry = new ilCalendarEntry((int) $_GET['app_id']);
1096  new ilDateTime($_GET['dstart'],IL_CAL_UNIX),
1097  new ilDateTime($_GET['dend'],IL_CAL_UNIX)
1098  );
1099 
1100 
1101  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1102  $conf = new ilConfirmationGUI;
1103 
1104  $this->ctrl->setParameter($this,'dstart',(int) $_REQUEST['dstart']);
1105  $this->ctrl->setParameter($this,'dend',(int) $_REQUEST['dend']);
1106 
1107  $conf->setFormAction($this->ctrl->getFormAction($this));
1108  $conf->setHeaderText($this->lng->txt('cal_confirm_reg_info'));
1109  $conf->setConfirm($this->lng->txt('cal_reg_register'), 'register');
1110  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1111  $conf->addItem('app_id', $entry->getEntryId(), $entry->getTitle().' ('.$start.')');
1112 
1113  $tpl->setContent($conf->getHTML());
1114  }
1115 
1120  protected function register()
1121  {
1122  global $ilUser;
1123 
1124  include_once './Services/Calendar/classes/class.ilCalendarRegistration.php';
1125  $reg = new ilCalendarRegistration((int) $_POST['app_id']);
1126  $reg->register(
1127  $ilUser->getId(),
1128  new ilDateTime((int) $_REQUEST['dstart'],IL_CAL_UNIX),
1129  new ilDateTime((int) $_REQUEST['dend'],IL_CAL_UNIX)
1130  );
1131 
1132  ilUtil::sendSuccess($this->lng->txt('cal_reg_registered'),true);
1133  $this->ctrl->returnToParent($this);
1134  }
1135 
1139  public function confirmUnregister()
1140  {
1141  global $tpl;
1142 
1143 
1144  $entry = new ilCalendarEntry((int) $_GET['app_id']);
1146  $dstart = new ilDateTime($_GET['dstart'],IL_CAL_UNIX),
1147  $dend = new ilDateTime($_GET['dend'],IL_CAL_UNIX)
1148  );
1149 
1150 
1151  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1152  $conf = new ilConfirmationGUI;
1153 
1154  $this->ctrl->setParameter($this,'dstart',(int) $_REQUEST['dstart']);
1155  $this->ctrl->setParameter($this,'dend',(int) $_REQUEST['dend']);
1156 
1157  $conf->setFormAction($this->ctrl->getFormAction($this));
1158  $conf->setHeaderText($this->lng->txt('cal_confirm_unreg_info'));
1159  $conf->setConfirm($this->lng->txt('cal_reg_unregister'), 'unregister');
1160  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1161  $conf->addItem('app_id', $entry->getEntryId(), $entry->getTitle().' ('.$start.')');
1162 
1163  $tpl->setContent($conf->getHTML());
1164  }
1165 
1170  protected function unregister()
1171  {
1172  global $ilUser;
1173 
1174  include_once './Services/Calendar/classes/class.ilCalendarRegistration.php';
1175  $reg = new ilCalendarRegistration((int) $_POST['app_id']);
1176  $reg->unregister(
1177  $ilUser->getId(),
1178  new ilDateTime((int) $_REQUEST['dstart'],IL_CAL_UNIX),
1179  new ilDateTime((int) $_REQUEST['dend'],IL_CAL_UNIX)
1180  );
1181 
1182  ilUtil::sendSuccess($this->lng->txt('cal_reg_unregistered'),true);
1183  $this->ctrl->returnToParent($this);
1184  }
1185 
1189  public function book()
1190  {
1191  global $ilUser, $tpl;
1192 
1193  $entry = (int)$_GET['app_id'];
1194  $user = (int)$_GET['bkid'];
1195 
1196  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1197  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1198  $entry = new ilCalendarEntry($entry);
1199  $booking = new ilBookingEntry($entry->getContextId());
1200 
1201  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
1202  $timezone = $ilUser->getTimeZone();
1203  switch($user_settings->getTimeFormat())
1204  {
1206  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1207  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1208  break;
1209 
1211  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1212  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1213  break;
1214  }
1215 
1216  $title .= ' '.$entry->getTitle()." (".ilObjUser::_lookupFullname($user).')';
1217 
1218  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1219  $conf = new ilConfirmationGUI;
1220  $conf->setFormAction($this->ctrl->getFormAction($this));
1221  $conf->setHeaderText($this->lng->txt('cal_confirm_booking_info'));
1222  $conf->setConfirm($this->lng->txt('cal_confirm_booking'), 'bookconfirmed');
1223  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1224  $conf->addItem('app_id', $entry->getEntryId(), $title);
1225 
1226  $tpl->setContent($conf->getHTML());
1227  }
1228 
1232  public function bookconfirmed()
1233  {
1234  global $ilUser;
1235 
1236  $entry = (int)$_POST['app_id'];
1237  $user = (int)$_GET['bkid'];
1238 
1239  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1240  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1241  $entry = new ilCalendarEntry($entry);
1242  $booking = new ilBookingEntry($entry->getContextId());
1243  $booking->book($entry->getEntryId());
1244 
1245  // create user calendar/appointment
1246  include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
1247  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
1248  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1249  $user_entry = clone $entry;
1250  $user_entry->save();
1251  $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH,$ilUser->getId(),$this->lng->txt('cal_ch_personal_ch'),true);
1252  $assign = new ilCalendarCategoryAssignments($user_entry->getEntryId());
1253  $assign->addAssignment($def_cat->getCategoryID());
1254 
1255  ilUtil::sendSuccess($this->lng->txt('cal_booking_confirmed'),true);
1256  $this->ctrl->returnToParent($this);
1257  }
1258 
1263  public function cancelBooking()
1264  {
1265  global $ilUser, $tpl;
1266 
1267  $entry = (int)$_GET['app_id'];
1268 
1269  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1270  $entry = new ilCalendarEntry($entry);
1271 
1272  $category = $this->calendarEntryToCategory($entry);
1273  if($category->getType() == ilCalendarCategory::TYPE_CH)
1274  {
1275  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1276  $booking = new ilBookingEntry($entry->getContextId());
1277  if(!$booking->hasBooked($entry->getEntryId()))
1278  {
1279  $this->ctrl->returnToParent($this);
1280  return false;
1281  }
1282 
1283  $entry_title = ' '.$entry->getTitle()." (".ilObjUser::_lookupFullname($booking->getObjId()).')';
1284  }
1285  else if($category->getType() == ilCalendarCategory::TYPE_BOOK)
1286  {
1287  $entry_title = ' '.$entry->getTitle();
1288  }
1289  else
1290  {
1291  $this->ctrl->returnToParent($this);
1292  return false;
1293  }
1294 
1295  $user_settings = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId());
1296  $timezone = $ilUser->getTimeZone();
1297  switch($user_settings->getTimeFormat())
1298  {
1300  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1301  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'H:i',$timezone);
1302  break;
1303 
1305  $title = $entry->getStart()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1306  $title .= "-".$entry->getEnd()->get(IL_CAL_FKT_DATE,'h:ia',$timezone);
1307  break;
1308  }
1309 
1310  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
1311  $conf = new ilConfirmationGUI;
1312  $conf->setFormAction($this->ctrl->getFormAction($this));
1313  $conf->setHeaderText($this->lng->txt('cal_cancel_booking_info'));
1314  $conf->setConfirm($this->lng->txt('cal_cancel_booking'), 'cancelconfirmed');
1315  $conf->setCancel($this->lng->txt('cancel'), 'cancel');
1316  $conf->addItem('app_id', $entry->getEntryId(), $title.' - '.$entry_title);
1317 
1318  $tpl->setContent($conf->getHTML());
1319  }
1320 
1325  public function cancelConfirmed()
1326  {
1327  global $ilUser;
1328 
1329  $entry = (int)$_POST['app_id'];
1330  $user = (int)$_GET['bkid'];
1331 
1332  include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1333  $entry = new ilCalendarEntry($entry);
1334 
1335  $category = $this->calendarEntryToCategory($entry);
1336  if($category->getType() == ilCalendarCategory::TYPE_CH)
1337  {
1338  // find cloned calendar entry in user calendar
1339  include_once 'Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
1340  $apps = ilConsultationHourAppointments::getAppointmentIds($ilUser->getId(), $entry->getContextId(), $entry->getStart());
1341  if($apps)
1342  {
1343  $ref_entry = new ilCalendarEntry($apps[0]);
1344  $ref_entry->delete();
1345  }
1346 
1347  include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1348  $booking = new ilBookingEntry($entry->getContextId());
1349  $booking->cancelBooking($entry->getEntryId());
1350 
1351  // do NOT delete original entry
1352  }
1353  else if($category->getType() == ilCalendarCategory::TYPE_BOOK)
1354  {
1355  include_once 'Modules/BookingManager/classes/class.ilBookingReservation.php';
1356  $booking = new ilBookingReservation($entry->getContextId());
1358  $booking->update();
1359 
1360  $entry->delete();
1361  }
1362 
1363  ilUtil::sendSuccess($this->lng->txt('cal_cancel_booking_confirmed'),true);
1364  $this->ctrl->returnToParent($this);
1365  }
1366 
1372  protected function calendarEntryToCategory(ilCalendarEntry $entry)
1373  {
1374  include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1375  include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
1376  $assignment = new ilCalendarCategoryAssignments($entry->getEntryId());
1377  $assignment = $assignment->getFirstAssignment();
1378  return new ilCalendarCategory($assignment);
1379  }
1380 }
1381 ?>