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
24include_once './Services/Calendar/classes/class.ilCalendarRecurrence.php';
25include_once './Services/Booking/classes/class.ilBookingEntry.php';
26include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
27
28/**
29 * Consultation hours editor
30 * @author Stefan Meyer <smeyer.ilias@gmx.de>
31 *
32 * @ilCtrl_Calls: ilConsultationHoursGUI: ilPublicUserProfileGUI, ilRepositorySearchGUI
33 */
34class ilConsultationHoursGUI
35{
36    const MODE_CREATE = 1;
37    const MODE_UPDATE = 2;
38    const MODE_MULTI = 3;
39
40    const MAX_APPOINTMENTS_PER_SEQUENCE = 1000;
41
42    protected $user_id;
43    protected $ctrl;
44
45    protected $booking = null;
46
47    /**
48     * Constructor
49     */
50    public function __construct()
51    {
52        global $DIC;
53
54        $lng = $DIC['lng'];
55        $ilCtrl = $DIC['ilCtrl'];
56        $tpl = $DIC['tpl'];
57        $ilUser = $DIC['ilUser'];
58
59        $user_id = (int) $_GET['user_id'];
60        if ($user_id) {
61            if (in_array($user_id, array_keys(ilConsultationHourAppointments::getManagedUsers()))) {
62                $this->user_id = $user_id;
63            } else {
64                $user_id = false;
65            }
66        }
67        if (!$user_id) {
68            $this->user_id = $ilUser->getId();
69        }
70
71        $this->ctrl = $ilCtrl;
72        $this->lng = $lng;
73        $this->tpl = $tpl;
74    }
75
76    /**
77     * Execute command
78     * @return
79     */
80    public function executeCommand()
81    {
82        global $DIC;
83
84        $ilUser = $DIC['ilUser'];
85        $ilCtrl = $DIC['ilCtrl'];
86        $tpl = $DIC['tpl'];
87        $ilHelp = $DIC['ilHelp'];
88        $ilTabs = $DIC['ilTabs'];
89
90        $ilHelp->setScreenIdComponent("cal");
91
92        switch ($this->ctrl->getNextClass()) {
93            case "ilpublicuserprofilegui":
94                include_once('./Services/User/classes/class.ilPublicUserProfileGUI.php');
95                #22168 don't send the current user if no GET user_id
96                //$profile = new ilPublicUserProfileGUI($this->user_id);
97                $profile = new ilPublicUserProfileGUI();
98                $profile->setBackUrl($this->getProfileBackUrl());
99                $ret = $ilCtrl->forwardCommand($profile);
100                $tpl->setContent($ret);
101                break;
102
103            case 'ilrepositorysearchgui':
104
105                include_once('./Services/Search/classes/class.ilRepositorySearchGUI.php');
106                $rep_search = new ilRepositorySearchGUI();
107
108                if (isset($_REQUEST['assignM'])) {
109                    $rep_search->setCallback(
110                        $this,
111                        'assignUsersToAppointments',
112                        array()
113                    );
114                    $ilCtrl->setParameter($this, 'assignM', 1);
115                    $ilCtrl->setReturn($this, 'appointmentList');
116                    $ilTabs->activateSubTab('cal_ch_app_list');
117                } elseif (isset($_REQUEST['grp_id'])) {
118                    $rep_search->setCallback(
119                        $this,
120                        'assignUsersToGroup',
121                        array()
122                    );
123                    $ilCtrl->saveParameter($this, 'grp_id');
124                    $ilCtrl->setReturn($this, 'groupList');
125                    $ilTabs->activateSubTab('cal_ch_app_grp');
126                } elseif (isset($_REQUEST['apps'])) {
127                    $rep_search->setCallback(
128                        $this,
129                        'assignUsersToAppointment',
130                        array()
131                    );
132                    $ilCtrl->saveParameter($this, 'apps');
133                    $ilCtrl->setReturn($this, 'appointmentList');
134                    $ilTabs->activateSubTab('cal_ch_app_list');
135                }
136                $ilCtrl->forwardCommand($rep_search);
137                break;
138
139            default:
140                $tpl->setTitle($this->lng->txt("cal_ch_form_header")); // #12220
141
142                $this->setTabs();
143                if ($ilUser->getId() != $this->user_id) {
144                    $ilCtrl->setParameter($this, 'user_id', $this->user_id);
145                }
146
147                $cmd = $this->ctrl->getCmd('appointmentList');
148                $this->$cmd();
149        }
150    }
151
152    /**
153     * Get user id
154     * @return
155     */
156    public function getUserId()
157    {
158        return $this->user_id;
159    }
160
161    /**
162     * start searching for users
163     */
164    protected function searchUsersForAppointments()
165    {
166        global $DIC;
167
168        $ilCtrl = $DIC['ilCtrl'];
169        $ilTabs = $DIC['ilTabs'];
170
171        $_SESSION['ch_apps'] = $_REQUEST['apps'];
172
173        if (empty($_SESSION['ch_apps'])) {
174            ilUtil::sendFailure($this->lng->txt('select_one'), true);
175            $GLOBALS['DIC']['ilCtrl']->redirect($this, 'appointmentList');
176        }
177        $_REQUEST['assignM'] = 1;
178        $ilCtrl->setCmdClass('ilrepositorysearchgui');
179        $ilCtrl->setcmd('');
180        $this->executeCommand();
181    }
182
183    /**
184     * Send info message about unassigned users
185     * @param array $unassigned
186     */
187    protected function sendInfoAboutUnassignedUsers($unassigned)
188    {
189        if (!$unassigned) {
190            return true;
191        }
192        $users = array();
193        foreach ($unassigned as $user_id) {
194            include_once './Services/User/classes/class.ilObjUser.php';
195            $users[] = ilObjUser::_lookupFullname($user_id);
196        }
197        ilUtil::sendInfo($this->lng->txt('cal_ch_user_assignment_failed_info') . '<br />' . implode('<br />', $users), true);
198        return true;
199    }
200
201    /**
202     * Assign users to multiple appointments
203     * @param type $users
204     */
205    public function assignUsersToAppointments(array $users)
206    {
207        global $DIC;
208
209        $ilCtrl = $DIC['ilCtrl'];
210
211        $unassigned_users = array();
212        foreach ($_SESSION['ch_apps'] as $app) {
213            $unassigned_users = array_unique(array_merge($unassigned_users, $this->assignUsersToAppointment($users, $app, false)));
214        }
215
216        $this->sendInfoAboutUnassignedUsers($unassigned_users);
217        $ilCtrl->redirect($this, 'appointmentList');
218    }
219
220
221    /**
222     * Assign users to an appointment
223     * @param array $usr_ids
224     * @return array $unassigned_users
225     */
226    public function assignUsersToAppointment(array $users, $a_app = 0, $a_redirect = true)
227    {
228        global $DIC;
229
230        $ilCtrl = $DIC['ilCtrl'];
231
232        if ($a_app) {
233            $app = $a_app;
234        } else {
235            $app = $_REQUEST['apps'];
236        }
237
238        if (!count($users)) {
239            ilUtil::sendFailure($GLOBALS['DIC']->language()->txt('select_one'), true);
240            return false;
241        }
242
243
244        include_once './Services/Booking/classes/class.ilBookingEntry.php';
245        $booking = ilBookingEntry::getInstanceByCalendarEntryId($app);
246
247        $assigned_users = array();
248        foreach ($users as $user) {
249            if ($booking->getCurrentNumberOfBookings($app) >= $booking->getNumberOfBookings()) {
250                break;
251            }
252            if (!ilBookingEntry::lookupBookingsOfUser((array) $app, $user)) {
253                include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
254                ilConsultationHourUtils::bookAppointment($user, $app);
255                $assigned_users[] = $user;
256            }
257        }
258
259        $unassigned_users = array_diff($users, $assigned_users);
260
261        if ($a_redirect) {
262            $this->sendInfoAboutUnassignedUsers($unassigned_users);
263            $ilCtrl->redirect($this, 'appointmentList');
264        } else {
265            return $unassigned_users;
266        }
267    }
268
269    /**
270     *
271     * @param array $usr_ids
272     * @param type $type
273     */
274    public function assignUsersToGroup(array $usr_ids)
275    {
276        global $DIC;
277
278        $ilCtrl = $DIC['ilCtrl'];
279
280        $group_id = (int) $_REQUEST['grp_id'];
281
282        $tomorrow = new ilDateTime(time(), IL_CAL_UNIX);
283        $tomorrow->increment(IL_CAL_DAY, 1);
284
285        // Get all future consultation hours
286        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
287        include_once './Services/Booking/classes/class.ilBookingEntry.php';
288        $apps = ilConsultationHourAppointments::getAppointmentIdsByGroup(
289            $this->user_id,
290            $group_id,
291            $tomorrow
292        );
293        $users = $usr_ids;
294        $assigned_users = array();
295        foreach ($apps as $app) {
296            $booking = ilBookingEntry::getInstanceByCalendarEntryId($app);
297            foreach ($users as $user) {
298                if ($booking->getCurrentNumberOfBookings($app) >= $booking->getNumberOfBookings()) {
299                    break;
300                }
301                if (!ilBookingEntry::lookupBookingsOfUser($apps, $user)) {
302                    include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
303                    ilConsultationHourUtils::bookAppointment($user, $app);
304                    $assigned_users[] = $user;
305                }
306            }
307        }
308
309        $this->sendInfoAboutUnassignedUsers(array_diff($users, $assigned_users));
310        $ilCtrl->redirect($this, 'bookingList');
311    }
312
313
314    /**
315     * Show consultation hour group
316     * @global type $ilToolbar
317     */
318    protected function groupList()
319    {
320        global $DIC;
321
322        $ilToolbar = $DIC['ilToolbar'];
323        $ilTabs = $DIC['ilTabs'];
324        $tpl = $DIC['tpl'];
325        $ilHelp = $DIC['ilHelp'];
326
327        $ilHelp->setScreenId("consultation_hours");
328
329        $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
330        $ilToolbar->addButton($this->lng->txt('cal_ch_add_grp'), $this->ctrl->getLinkTarget($this, 'addGroup'));
331
332        $this->setSubTabs();
333        $ilTabs->activateSubTab('cal_ch_app_grp');
334
335        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroupTableGUI.php';
336        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
337        $gtbl = new ilConsultationHourGroupTableGUI($this, 'groupList', $this->getUserId());
338        $gtbl->parse(ilConsultationHourGroups::getGroupsOfUser($this->getUserId()));
339
340        $tpl->setContent($gtbl->getHTML());
341    }
342
343    /**
344     * Show add group form
345     * @global type $ilToolbar
346     * @global type $ilTabs
347     */
348    protected function addGroup(ilPropertyFormGUI $form = null)
349    {
350        global $DIC;
351
352        $ilTabs = $DIC['ilTabs'];
353        $tpl = $DIC['tpl'];
354
355        $this->setSubTabs();
356        $ilTabs->activateSubTab('cal_ch_app_grp');
357
358        if ($form == null) {
359            $form = $this->initGroupForm();
360        }
361        $tpl->setContent($form->getHTML());
362    }
363
364    /**
365     * Save new group
366     */
367    protected function saveGroup()
368    {
369        $form = $this->initGroupForm();
370        if ($form->checkInput()) {
371            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
372            $group = new ilConsultationHourGroup();
373            $group->setTitle($form->getInput('title'));
374            $group->setMaxAssignments($form->getInput('multiple'));
375            $group->setUserId($this->getUserId());
376            $group->save();
377
378            ilUtil::sendSuccess($GLOBALS['DIC']['lng']->txt('settings_saved'), true);
379            $GLOBALS['DIC']['ilCtrl']->redirect($this, 'groupList');
380        }
381
382        ilUtil::sendFailure($GLOBALS['DIC']['lng']->txt('err_check_input'), true);
383        $this->addGroup($form);
384    }
385
386    /**
387     * Edit group
388     * @global type $ilCtrl
389     * @param ilPropertyFormGUI $form
390     */
391    protected function editGroup(ilPropertyFormGUI $form = null)
392    {
393        global $DIC;
394
395        $ilCtrl = $DIC['ilCtrl'];
396        $tpl = $DIC['tpl'];
397        $ilTabs = $DIC['ilTabs'];
398
399        $ilCtrl->setParameter($this, 'grp_id', (int) $_REQUEST['grp_id']);
400        $this->setSubTabs();
401        $ilTabs->activateSubTab('cal_ch_app_grp');
402
403        if ($form == null) {
404            $form = $this->initGroupForm((int) $_REQUEST['grp_id']);
405        }
406        $tpl->setContent($form->getHTML());
407    }
408
409    /**
410     * Update group
411     * @global type $ilCtrl
412     * @global type $tpl
413     * @global type $ilTabs
414     */
415    protected function updateGroup()
416    {
417        global $DIC;
418
419        $ilCtrl = $DIC['ilCtrl'];
420        $tpl = $DIC['tpl'];
421        $ilTabs = $DIC['ilTabs'];
422
423        $ilCtrl->setParameter($this, 'grp_id', (int) $_REQUEST['grp_id']);
424
425        $form = $this->initGroupForm((int) $_REQUEST['grp_id']);
426        if ($form->checkInput()) {
427            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
428            $group = new ilConsultationHourGroup((int) $_REQUEST['grp_id']);
429            $group->setTitle($form->getInput('title'));
430            $group->setMaxAssignments($form->getInput('multiple'));
431            $group->setUserId($this->getUserId());
432            $group->update();
433
434            ilUtil::sendSuccess($GLOBALS['DIC']['lng']->txt('settings_saved'), true);
435            $GLOBALS['DIC']['ilCtrl']->redirect($this, 'groupList');
436        }
437
438        ilUtil::sendFailure($GLOBALS['DIC']['lng']->txt('err_check_input'), true);
439        $this->editGroup($form);
440    }
441
442    /**
443     * Confirm delete
444     * @global type $ilCtrl
445     * @global type $ilTabs
446     */
447    protected function confirmDeleteGroup()
448    {
449        global $DIC;
450
451        $ilCtrl = $DIC['ilCtrl'];
452        $ilTabs = $DIC['ilTabs'];
453        $tpl = $DIC['tpl'];
454
455        $ilCtrl->setParameter($this, 'grp_id', (int) $_REQUEST['grp_id']);
456        $groups = array((int) $_REQUEST['grp_id']);
457
458        $this->setSubTabs();
459        $ilTabs->activateSubTab('cal_ch_app_grp');
460
461
462        include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
463        $confirm = new ilConfirmationGUI();
464        $confirm->setFormAction($ilCtrl->getFormAction($this));
465        $confirm->setHeaderText($GLOBALS['DIC']['lng']->txt('cal_ch_grp_delete_sure'));
466        $confirm->setConfirm($GLOBALS['DIC']['lng']->txt('delete'), 'deleteGroup');
467        $confirm->setCancel($GLOBALS['DIC']['lng']->txt('cancel'), 'groupList');
468
469        foreach ($groups as $grp_id) {
470            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
471            $group = new ilConsultationHourGroup($grp_id);
472
473            $confirm->addItem('groups[]', $grp_id, $group->getTitle());
474        }
475        $tpl->setContent($confirm->getHTML());
476    }
477
478    /**
479     * Delete groups
480     */
481    protected function deleteGroup()
482    {
483        global $DIC;
484
485        $ilCtrl = $DIC['ilCtrl'];
486
487        foreach ((array) $_REQUEST['groups'] as $grp_id) {
488            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
489            $group = new ilConsultationHourGroup($grp_id);
490            $group->delete();
491        }
492        ilUtil::sendSuccess($GLOBALS['DIC']['lng']->txt('cal_ch_grp_deleted'));
493        $ilCtrl->redirect($this, 'groupList');
494    }
495
496    /**
497     * Init new/update group form
498     */
499    protected function initGroupForm($a_group_id = 0)
500    {
501        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroup.php';
502        $group = new ilConsultationHourGroup($a_group_id);
503
504        include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
505        $form = new ilPropertyFormGUI();
506        $form->setFormAction($GLOBALS['DIC']['ilCtrl']->getFormAction($this));
507
508        if ($a_group_id) {
509            $form->setTitle($GLOBALS['DIC']['lng']->txt('cal_ch_grp_update_tbl'));
510            $form->addCommandButton('updateGroup', $GLOBALS['DIC']['lng']->txt('save'));
511            $form->addCommandButton('groupList', $GLOBALS['DIC']['lng']->txt('cancel'));
512        } else {
513            $form->setTitle($GLOBALS['DIC']['lng']->txt('cal_ch_grp_add_tbl'));
514            $form->addCommandButton('saveGroup', $GLOBALS['DIC']['lng']->txt('save'));
515            $form->addCommandButton('appointmentList', $GLOBALS['DIC']['lng']->txt('cancel'));
516        }
517
518        $title = new ilTextInputGUI($GLOBALS['DIC']['lng']->txt('title'), 'title');
519        $title->setMaxLength(128);
520        $title->setSize(40);
521        $title->setRequired(true);
522        $title->setValue($group->getTitle());
523        $form->addItem($title);
524
525        $multiple = new ilNumberInputGUI($GLOBALS['DIC']['lng']->txt('cal_ch_grp_multiple'), 'multiple');
526        $multiple->setRequired(true);
527        $multiple->setMinValue(1);
528        $multiple->setSize(1);
529        $multiple->setMaxLength(2);
530        $multiple->setInfo($GLOBALS['DIC']['lng']->txt('cal_ch_grp_multiple_info'));
531        $multiple->setValue($group->getMaxAssignments());
532        $form->addItem($multiple);
533
534        return $form;
535    }
536
537    /**
538     * Show list of bookings
539     */
540    protected function bookingList()
541    {
542        global $DIC;
543
544        $ilToolbar = $DIC['ilToolbar'];
545        $ilTabs = $DIC['ilTabs'];
546        $tpl = $DIC['tpl'];
547        $ilHelp = $DIC['ilHelp'];
548
549        $ilHelp->setScreenId("consultation_hours");
550
551        $this->setSubTabs();
552        $ilTabs->activateSubTab('cal_ch_app_bookings');
553
554        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourBookingTableGUI.php';
555        $btable = new ilConsultationHourBookingTableGUI($this, 'bookingList', $this->getUserId());
556        $btable->parse(ilConsultationHourAppointments::getAppointmentIds($this->getUserId()));
557        $tpl->setContent($btable->getHTML());
558    }
559
560    /**
561     * Show delete booking confirmation
562     */
563    protected function confirmDeleteBooking()
564    {
565        $this->confirmRejectBooking(false);
566    }
567
568    /**
569     * Show delete booking confirmation
570     */
571    protected function confirmRejectBooking($a_send_notification = true)
572    {
573        global $DIC;
574
575        $ilTabs = $DIC['ilTabs'];
576        $tpl = $DIC['tpl'];
577
578        $this->setSubTabs();
579        $ilTabs->activateSubTab('cal_ch_app_bookings');
580
581        include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
582
583        $confirm = new ilConfirmationGUI();
584        $confirm->setFormAction($this->ctrl->getFormAction($this));
585
586        if ($a_send_notification) {
587            ilUtil::sendInfo($this->lng->txt('cal_ch_cancel_booking_info'));
588            $confirm->setHeaderText($this->lng->txt('cal_ch_cancel_booking_sure'));
589            $confirm->setConfirm($this->lng->txt('cal_ch_reject_booking'), 'rejectBooking');
590        } else {
591            ilUtil::sendInfo($this->lng->txt('cal_ch_delete_booking_info'));
592            $confirm->setHeaderText($this->lng->txt('cal_ch_delete_booking_sure'));
593            $confirm->setConfirm($this->lng->txt('cal_ch_delete_booking'), 'deleteBooking');
594        }
595
596        $confirm->setCancel($this->lng->txt('cancel'), 'bookingList');
597
598        include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
599        foreach ((array) $_REQUEST['bookuser'] as $bookuser) {
600            $ids = explode('_', $bookuser);
601
602            include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
603            include_once './Services/User/classes/class.ilUserUtil.php';
604            $entry = new ilCalendarEntry($ids[0]);
605            $confirm->addItem(
606                'bookuser[]',
607                $bookuser,
608                ilUserUtil::getNamePresentation(
609                    $ids[1],
610                    true,
611                    false,
612                    '',
613                    true,
614                    true
615                ) . ', ' . ilDatePresentation::formatDate($entry->getStart())
616            );
617        }
618        $tpl->setContent($confirm->getHTML());
619    }
620
621    /**
622     * Delete booking
623     */
624    protected function deleteBooking()
625    {
626        $this->rejectBooking(false);
627    }
628
629    /**
630     *
631     * @param type $a_send_notification
632     */
633    protected function rejectBooking($a_send_notification = true)
634    {
635        global $DIC;
636
637        $ilCtrl = $DIC['ilCtrl'];
638
639        foreach ((array) $_REQUEST['bookuser'] as $bookuser) {
640            $ids = explode('_', $bookuser);
641
642            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
643            ilConsultationHourUtils::cancelBooking($ids[1], $ids[0], $a_send_notification);
644        }
645        if ($a_send_notification) {
646            ilUtil::sendSuccess($this->lng->txt('cal_ch_canceled_bookings'), true);
647        } else {
648            ilUtil::sendSuccess($this->lng->txt('cal_ch_deleted_bookings'), true);
649        }
650        $ilCtrl->redirect($this, 'bookingList');
651    }
652
653    /**
654     * Show settings of consultation hours
655     * @todo add list/filter of consultation hours if user is responsible for more than one other consultation hour series.
656     * @return
657     */
658    protected function appointmentList()
659    {
660        global $DIC;
661
662        $ilToolbar = $DIC['ilToolbar'];
663        $ilHelp = $DIC['ilHelp'];
664        $ilTabs = $DIC['ilTabs'];
665
666        $ilHelp->setScreenId("consultation_hours");
667
668        $ilToolbar->setFormAction($this->ctrl->getFormAction($this));
669        $ilToolbar->addButton($this->lng->txt('cal_ch_add_sequence'), $this->ctrl->getLinkTarget($this, 'createSequence'));
670
671        $this->setSubTabs();
672        $ilTabs->activateSubTab('cal_ch_app_list');
673
674        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHoursTableGUI.php';
675        $tbl = new ilConsultationHoursTableGUI($this, 'appointmentList', $this->getUserId());
676        $tbl->parse();
677        $this->tpl->setContent($tbl->getHTML());
678    }
679
680    /**
681     * Create new sequence
682     * @return
683     */
684    protected function createSequence()
685    {
686        $this->initFormSequence(self::MODE_CREATE);
687
688        $this->booking = new ilBookingEntry();
689        $this->form->getItemByPostVar('bo')->setValue($this->booking->getNumberOfBookings());
690        $this->form->getItemByPostVar('ap')->setValue(1);
691        $this->form->getItemByPostVar('du')->setMinutes(15);
692        $this->form->getItemByPostVar('st')->setDate(
693            new ilDateTime(mktime(8, 0, 0, date('n', time()), date('d', time()), date('Y', time())), IL_CAL_UNIX)
694        );
695
696        $this->tpl->setContent($this->form->getHTML());
697    }
698
699    /**
700     * Init form
701     * @param int $a_mode
702     * @return
703     */
704    protected function initFormSequence($a_mode)
705    {
706        include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
707
708        include_once('./Services/YUI/classes/class.ilYuiUtil.php');
709        ilYuiUtil::initDomEvent();
710
711        $this->form = new ilPropertyFormGUI();
712        $this->form->setFormAction($this->ctrl->getFormAction($this));
713
714        switch ($a_mode) {
715            case self::MODE_CREATE:
716                $this->form->setTitle($this->lng->txt('cal_ch_add_sequence'));
717                $this->form->addCommandButton('saveSequence', $this->lng->txt('save'));
718                $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
719                break;
720
721            /*
722            case self::MODE_UPDATE:
723                $this->form->setTitle($this->lng->txt('cal_ch_edit_sequence'));
724                $this->form->addCommandButton('updateSequence', $this->lng->txt('save'));
725                $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
726                break;
727             */
728
729            case self::MODE_MULTI:
730                $this->form->setTitle($this->lng->txt('cal_ch_multi_edit_sequence'));
731                $this->form->addCommandButton('updateMulti', $this->lng->txt('save'));
732                $this->form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
733                break;
734        }
735
736        // in case of existing groups show a selection
737        include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
738        if (count($options = ilConsultationHourGroups::getGroupSelectOptions($this->getUserId()))) {
739            $group = new ilSelectInputGUI($this->lng->txt('cal_ch_grp_selection'), 'grp');
740            $group->setOptions($options);
741            $group->setRequired(false);
742            $this->form->addItem($group);
743        }
744
745        // Title
746        $ti = new ilTextInputGUI($this->lng->txt('title'), 'ti');
747        $ti->setSize(32);
748        $ti->setMaxLength(128);
749        $ti->setRequired(true);
750        $this->form->addItem($ti);
751
752        if ($a_mode != self::MODE_MULTI) {
753            // Start
754            include_once './Services/Form/classes/class.ilDateTimeInputGUI.php';
755            $dur = new ilDateTimeInputGUI($this->lng->txt('cal_start'), 'st');
756            $dur->setShowTime(true);
757            $dur->setRequired(true);
758            $this->form->addItem($dur);
759
760            // Duration
761            $du = new ilDurationInputGUI($this->lng->txt('cal_ch_duration'), 'du');
762            $du->setShowMinutes(true);
763            $du->setShowHours(true);
764            $this->form->addItem($du);
765
766            // Number of appointments
767            $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_appointments'), 'ap');
768            $nu->setInfo($this->lng->txt('cal_ch_num_appointments_info'));
769            $nu->setSize(2);
770            $nu->setMaxLength(2);
771            $nu->setRequired(true);
772            $nu->setMinValue(1);
773            $this->form->addItem($nu);
774
775            // Recurrence
776            include_once('./Services/Calendar/classes/Form/class.ilRecurrenceInputGUI.php');
777            $rec = new ilRecurrenceInputGUI($this->lng->txt('cal_recurrences'), 'frequence');
778            $rec->setEnabledSubForms(
779                array(
780                    IL_CAL_FREQ_DAILY,
781                    IL_CAL_FREQ_WEEKLY,
782                    IL_CAL_FREQ_MONTHLY
783                )
784            );
785            $this->form->addItem($rec);
786        }
787
788        // Number of bookings
789        $nu = new ilNumberInputGUI($this->lng->txt('cal_ch_num_bookings'), 'bo');
790        $nu->setSize(2);
791        $nu->setMaxLength(2);
792        $nu->setMinValue(1);
793        $nu->setRequired(true);
794        $this->form->addItem($nu);
795
796        // Deadline
797        $dead = new ilDurationInputGUI($this->lng->txt('cal_ch_deadline'), 'dead');
798        $dead->setInfo($this->lng->txt('cal_ch_deadline_info'));
799        $dead->setShowMinutes(false);
800        $dead->setShowHours(true);
801        $dead->setShowDays(true);
802        $this->form->addItem($dead);
803
804        // Location
805        $lo = new ilTextInputGUI($this->lng->txt('cal_where'), 'lo');
806        $lo->setSize(32);
807        $lo->setMaxLength(128);
808        $this->form->addItem($lo);
809
810        // Description
811        $de = new ilTextAreaInputGUI($this->lng->txt('description'), 'de');
812        $de->setRows(10);
813        $de->setCols(60);
814        $this->form->addItem($de);
815
816        // Target Object
817        $tgt = new ilTextInputGUI($this->lng->txt('cal_ch_target_object'), 'tgt');
818        $tgt->setInfo($this->lng->txt('cal_ch_target_object_info'));
819        $tgt->setSize(16);
820        $tgt->setMaxLength(128);
821        $this->form->addItem($tgt);
822    }
823
824    /**
825     * Save new sequence
826     * @return
827     */
828    protected function saveSequence()
829    {
830        global $DIC;
831
832        $ilObjDataCache = $DIC['ilObjDataCache'];
833
834        $this->initFormSequence(self::MODE_CREATE);
835
836        if ($this->form->checkInput()) {
837            $this->form->setValuesByPost();
838
839            $booking = new ilBookingEntry();
840            $booking->setObjId($this->getUserId());
841            $booking->setNumberOfBookings($this->form->getInput('bo'));
842
843            $deadline = $this->form->getInput('dead');
844            $deadline = $deadline['dd'] * 24 + $deadline['hh'];
845            $booking->setDeadlineHours($deadline);
846
847            // consultation hour group
848            include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourGroups.php';
849            if (ilConsultationHourGroups::getGroupsOfUser($this->getUserId())) {
850                $booking->setBookingGroup((int) $this->form->getInput('grp'));
851            }
852
853            $tgt = explode(',', $this->form->getInput('tgt'));
854            $obj_ids = array();
855            foreach ((array) $tgt as $ref_id) {
856                if (!trim($ref_id)) {
857                    continue;
858                }
859                $obj_id = $ilObjDataCache->lookupObjId($ref_id);
860                $type = ilObject::_lookupType($obj_id);
861                $valid_types = array('crs','grp');
862                if (!$obj_id or !in_array($type, $valid_types)) {
863                    ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
864                    $this->tpl->setContent($this->form->getHTML());
865                    return;
866                }
867
868                $obj_ids[] = $obj_id;
869            }
870            $booking->setTargetObjIds($obj_ids);
871
872            $booking->save();
873            $this->createAppointments($booking);
874
875            ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
876            $this->ctrl->redirect($this, 'appointmentList');
877        } else {
878            $this->form->setValuesByPost();
879            $this->tpl->setContent($this->form->getHTML());
880        }
881    }
882
883    /**
884     * Create calendar appointments
885     * @param ilBookingEntry $booking
886     * @return
887     */
888    protected function createAppointments(ilBookingEntry $booking)
889    {
890        include_once './Services/Calendar/classes/class.ilDateList.php';
891        $concurrent_dates = new ilDateList(ilDateList::TYPE_DATETIME);
892        $start = clone $this->form->getItemByPostVar('st')->getDate();
893        for ($i = 0; $i < $this->form->getItemByPostVar('ap')->getValue(); $i++) {
894            $concurrent_dates->add(clone $start);
895
896            $start->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
897            $start->increment(ilDateTime::HOUR, $this->form->getItemByPostVar('du')->getHours());
898            #$start = new ilDateTime(,IL_CAL_UNIX);
899        }
900
901        include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
902        $def_cat = ilCalendarUtil::initDefaultCalendarByType(ilCalendarCategory::TYPE_CH, $this->getUserId(), $this->lng->txt('cal_ch_personal_ch'), true);
903
904        // Add calendar appointment for each
905        include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
906        include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
907        include_once './Services/Calendar/classes/class.ilCalendarRecurrenceCalculator.php';
908        include_once './Services/Booking/classes/class.ilBookingPeriod.php';
909
910        $num_appointments = 0;
911        foreach ($concurrent_dates as $dt) {
912            if ($num_appointments >= self::MAX_APPOINTMENTS_PER_SEQUENCE) {
913                break;
914            }
915
916            $end = clone $dt;
917            $end->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
918            $end->increment(ilDateTime::HOUR, $this->form->getItemByPostVar('du')->getHours());
919
920            $calc = new ilCalendarRecurrenceCalculator(
921                new ilBookingPeriod($dt, $end),
922                $this->form->getItemByPostVar('frequence')->getRecurrence()
923            );
924
925            // Calculate with one year limit
926            $limit = clone $dt;
927            $limit->increment(ilDateTime::YEAR, 1);
928
929            $date_list = $calc->calculateDateList($dt, $limit);
930
931            $num = 0;
932            foreach ($date_list as $app_start) {
933                $app_end = clone $app_start;
934                $app_end->increment(ilDateTime::MINUTE, $this->form->getItemByPostVar('du')->getMinutes());
935                $app_end->increment(ilDateTime::HOUR, $this->form->getItemByPostVar('du')->getHours());
936
937
938                $entry = new ilCalendarEntry();
939                $entry->setContextId($booking->getId());
940                $entry->setTitle($this->form->getInput('ti'));
941                $entry->setSubtitle("#consultationhour#"); // dynamic, see ilCalendarEntry
942                $entry->setDescription($this->form->getInput('de'));
943                $entry->setLocation($this->form->getInput('lo'));
944                $entry->setStart($app_start);
945                $entry->setEnd($app_end);
946
947                $entry->setTranslationType(IL_CAL_TRANSLATION_SYSTEM);
948                $entry->save();
949
950                $cat_assign = new ilCalendarCategoryAssignments($entry->getEntryId());
951                $cat_assign->addAssignment($def_cat->getCategoryID());
952
953                $num_appointments++;
954            }
955        }
956    }
957
958    /**
959     * Set tabs
960     * @return
961     */
962    protected function setTabs()
963    {
964        global $DIC;
965
966        $ilTabs = $DIC['ilTabs'];
967        $ilUser = $DIC['ilUser'];
968        $ilCtrl = $DIC['ilCtrl'];
969
970        $ilCtrl->setParameter($this, 'user_id', '');
971        $ilTabs->addTab('consultation_hours_' . $ilUser->getId(), $this->lng->txt('cal_ch_ch'), $this->ctrl->getLinkTarget($this, 'appointmentList'));
972
973        foreach (ilConsultationHourAppointments::getManagedUsers() as $user_id => $login) {
974            $ilCtrl->setParameter($this, 'user_id', $user_id);
975            $ilTabs->addTab('consultation_hours_' . $user_id, $this->lng->txt('cal_ch_ch') . ': ' . $login, $this->ctrl->getLinkTarget($this, 'appointmentList'));
976        }
977        $ilCtrl->setParameter($this, 'user_id', '');
978
979        $ilTabs->addTab('ch_settings', $this->lng->txt('settings'), $this->ctrl->getLinkTarget($this, 'settings'));
980
981        $ilTabs->activateTab('consultation_hours_' . $this->getUserId());
982    }
983
984    /**
985     * Set sub tabs
986     * @global type $ilTabs
987     * @global type $ilCtrl
988     */
989    protected function setSubTabs()
990    {
991        global $DIC;
992
993        $ilTabs = $DIC['ilTabs'];
994        $ilCtrl = $DIC['ilCtrl'];
995
996        $ilCtrl->setParameter($this, 'user_id', $this->getUserId());
997        $ilTabs->addSubTab('cal_ch_app_list', $this->lng->txt('cal_ch_app_list'), $ilCtrl->getLinkTarget($this, 'appointmentList'));
998        $ilTabs->addSubTab('cal_ch_app_grp', $this->lng->txt('cal_ch_app_grp'), $ilCtrl->getLinkTarget($this, 'groupList'));
999        $ilTabs->addSubTab('cal_ch_app_bookings', $this->lng->txt('cal_ch_app_bookings'), $ilCtrl->getLinkTarget($this, 'bookingList'));
1000    }
1001
1002    /**
1003     * Edit multiple sequence items
1004     */
1005    public function edit()
1006    {
1007        global $DIC;
1008
1009        $ilTabs = $DIC['ilTabs'];
1010
1011        if (!isset($_REQUEST['apps'])) {
1012            ilUtil::sendFailure($this->lng->txt('select_one'));
1013            return $this->appointmentList();
1014        }
1015
1016        $this->initFormSequence(self::MODE_MULTI);
1017
1018        if ($_REQUEST['apps'] && !is_array($_REQUEST['apps'])) {
1019            $_REQUEST['apps'] = explode(';', $_REQUEST['apps']);
1020        }
1021
1022        $hidden = new ilHiddenInputGUI('apps');
1023        $hidden->setValue(implode(';', $_REQUEST['apps']));
1024        $this->form->addItem($hidden);
1025
1026        include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1027        $first = $_REQUEST['apps'];
1028        $first = array_shift($_REQUEST['apps']);
1029        $entry = new ilCalendarEntry($first);
1030
1031        $this->form->getItemByPostVar('ti')->setValue($entry->getTitle());
1032        $this->form->getItemByPostVar('lo')->setValue($entry->getLocation());
1033        $this->form->getItemByPostVar('de')->setValue($entry->getDescription());
1034
1035        include_once 'Services/Booking/classes/class.ilBookingEntry.php';
1036        $booking = new ilBookingEntry($entry->getContextId());
1037
1038        $this->form->getItemByPostVar('bo')->setValue($booking->getNumberOfBookings());
1039
1040        $ref_ids = array();
1041        foreach ($booking->getTargetObjIds() as $obj_id) {
1042            $refs = ilObject::_getAllReferences($obj_id);
1043            $ref_ids[] = end($refs);
1044        }
1045        $this->form->getItemByPostVar('tgt')->setValue(implode(',', $ref_ids));
1046
1047        $deadline = $booking->getDeadlineHours();
1048        $this->form->getItemByPostVar('dead')->setDays(floor($deadline / 24));
1049        $this->form->getItemByPostVar('dead')->setHours($deadline % 24);
1050
1051        if ($booking->getBookingGroup()) {
1052            $this->form->getItemByPostVar('grp')->setValue($booking->getBookingGroup());
1053        }
1054
1055        $this->tpl->setContent($this->form->getHTML());
1056    }
1057
1058    /**
1059     * @param ilPropertyFormGUI $validated_form
1060     * @return \ilBookingEntry | null
1061     */
1062    protected function createNewBookingEntry(\ilPropertyFormGUI $validate_form)
1063    {
1064        global $DIC;
1065
1066        $obj_cache = $DIC['ilObjDataCache'];
1067
1068        $booking = new \ilBookingEntry();
1069        $booking->setObjId($this->user_id);
1070        $booking->setNumberOfBookings((int) $this->form->getInput('bo'));
1071
1072        $deadline = $this->form->getInput('dead');
1073        $deadline = $deadline['dd'] * 24 + $deadline['hh'];
1074        $booking->setDeadlineHours($deadline);
1075
1076        $tgt = explode(',', (string) $this->form->getInput('tgt'));
1077        $obj_ids = [];
1078        foreach ((array) $tgt as $ref_id) {
1079            if (!trim($ref_id)) {
1080                continue;
1081            }
1082            $obj_id = $obj_cache->lookupObjId($ref_id);
1083            $type = ilObject::_lookupType($obj_id);
1084            $valid_types = ['crs', 'grp'];
1085            if (!$obj_id or !in_array($type, $valid_types)) {
1086                ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_repository_object'));
1087                return null;
1088            }
1089            $obj_ids[] = $obj_id;
1090        }
1091        $booking->setTargetObjIds($obj_ids);
1092
1093        if (ilConsultationHourGroups::getCountGroupsOfUser($this->getUserId())) {
1094            $booking->setBookingGroup($this->form->getInput('grp'));
1095        }
1096        $booking->save();
1097        return $booking;
1098    }
1099
1100    /**
1101     * @param ilBookingEntry $booking
1102     * @param int[] $appointments
1103     */
1104    protected function rewriteBookingIdsForAppointments(\ilBookingEntry $booking, $appointments, \ilPropertyFormGUI $form)
1105    {
1106        foreach ($appointments as $appointment_id) {
1107
1108            $booking_appointment = new \ilCalendarEntry($appointment_id);
1109            $booking_start = $booking_appointment->getStart();
1110            $booking_end = $booking_appointment->getEnd();
1111
1112            $deprecatedBooking = \ilBookingEntry::getInstanceByCalendarEntryId($appointment_id);
1113            if (!$deprecatedBooking instanceof \ilBookingEntry) {
1114                // @todo error handling
1115                continue;
1116            }
1117
1118            $relevant_appointments = \ilConsultationHourUtils::findCalendarAppointmentsForBooking(
1119                $deprecatedBooking,
1120                $booking_start,
1121                $booking_end
1122            );
1123            foreach ($relevant_appointments as $relevant_appointment_id) {
1124
1125                $entry = new \ilCalendarEntry($relevant_appointment_id);
1126                $entry->setContextId($booking->getId());
1127                $entry->setTitle($form->getInput('ti'));
1128                $entry->setLocation($form->getInput('lo'));
1129                $entry->setDescription($form->getInput('de'));
1130                $entry->update();
1131            }
1132        }
1133    }
1134
1135    /**
1136     * Update multiple sequence items
1137     * @return
1138     */
1139    protected function updateMulti()
1140    {
1141        global $DIC;
1142
1143        $ilObjDataCache = $DIC['ilObjDataCache'];
1144
1145        $this->initFormSequence(self::MODE_MULTI);
1146
1147        if ($this->form->checkInput()) {
1148
1149            $this->form->setValuesByPost();
1150            $apps = explode(';', (string) $_POST['apps']);
1151
1152            // create new booking
1153            $booking = $this->createNewBookingEntry($this->form);
1154            if (!$booking instanceof \ilBookingEntry) {
1155                $this->edit();
1156                return false;
1157            }
1158            $this->rewriteBookingIdsForAppointments($booking, $apps, $this->form);
1159            ilBookingEntry::removeObsoleteEntries();
1160
1161            ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1162            $this->ctrl->redirect($this, 'appointmentList');
1163        }
1164        $this->tpl->setContent($this->form->getHTML());
1165    }
1166
1167    /**
1168     * confirm delete for multiple entries
1169     */
1170    public function confirmDelete()
1171    {
1172        global $DIC;
1173
1174        $tpl = $DIC['tpl'];
1175
1176        if (!isset($_REQUEST['apps'])) {
1177            ilUtil::sendFailure($this->lng->txt('select_one'));
1178            return $this->appointmentList();
1179        }
1180
1181        include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
1182
1183
1184        $this->ctrl->saveParameter($this, array('seed','app_id','dt'));
1185
1186        $confirm = new ilConfirmationGUI();
1187        $confirm->setFormAction($this->ctrl->getFormAction($this));
1188        $confirm->setHeaderText($this->lng->txt('cal_delete_app_sure'));
1189        $confirm->setCancel($this->lng->txt('cancel'), 'cancel');
1190
1191        include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1192
1193        $bookings_available = array();
1194        foreach ((array) $_REQUEST['apps'] as $entry_id) {
1195            $entry = new ilCalendarEntry($entry_id);
1196            $confirm->addItem('apps[]', $entry_id, ilDatePresentation::formatDate($entry->getStart()) . ', ' . $entry->getTitle());
1197
1198            include_once './Services/Booking/classes/class.ilBookingEntry.php';
1199            if (ilBookingEntry::lookupBookingsForAppointment($entry_id)) {
1200                $bookings_available[] = ilDatePresentation::formatDate($entry->getStart()) . ', ' . $entry->getTitle();
1201            }
1202        }
1203
1204        if ($bookings_available) {
1205            ilUtil::sendInfo($this->lng->txt('cal_ch_delete_app_booking_info') . '<br />' . implode('<br />', $bookings_available));
1206        }
1207
1208        $confirm->setConfirm($this->lng->txt('delete'), 'delete');
1209        $confirm->setCancel($this->lng->txt('cancel'), 'appointmentList');
1210
1211        $tpl->setContent($confirm->getHTML());
1212    }
1213
1214    /**
1215     * delete multiple entries
1216     */
1217    public function delete()
1218    {
1219        if (!isset($_POST['apps'])) {
1220            ilUtil::sendFailure($this->lng->txt('select_one'));
1221            return $this->appointmentList();
1222        }
1223
1224        include_once 'Services/Calendar/classes/class.ilCalendarEntry.php';
1225        include_once 'Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
1226        foreach ($_POST['apps'] as $entry_id) {
1227            // cancel booking for users
1228            $booking = ilBookingEntry::getInstanceByCalendarEntryId($entry_id);
1229            if ($booking) {
1230                foreach ($booking->getCurrentBookings($entry_id) as $user_id) {
1231                    include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourUtils.php';
1232                    ilConsultationHourUtils::cancelBooking($user_id, $entry_id, false);
1233                }
1234            }
1235            // remove calendar entries
1236            include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
1237            $entry = new ilCalendarEntry($entry_id);
1238            $entry->delete();
1239
1240            ilCalendarCategoryAssignments::_deleteByAppointmentId($entry_id);
1241        }
1242
1243        ilBookingEntry::removeObsoleteEntries();
1244
1245        ilUtil::sendSuccess($this->lng->txt('cal_deleted_app'), true);
1246        $this->ctrl->redirect($this, 'appointmentList');
1247    }
1248
1249    /**
1250     * show public profile of given user
1251     */
1252    public function showProfile()
1253    {
1254        global $DIC;
1255
1256        $tpl = $DIC['tpl'];
1257        $ilTabs = $DIC['ilTabs'];
1258        $ilCtrl = $DIC['ilCtrl'];
1259
1260        $ilTabs->clearTargets();
1261
1262        $user_id = (int) $_GET['user'];
1263
1264        include_once 'Services/User/classes/class.ilPublicUserProfileGUI.php';
1265        $profile = new ilPublicUserProfileGUI($user_id);
1266        $profile->setBackUrl($this->getProfileBackUrl());
1267        $tpl->setContent($ilCtrl->getHTML($profile));
1268    }
1269
1270    /**
1271     * Build context-sensitive profile back url
1272     *
1273     * @return string
1274     */
1275    protected function getProfileBackUrl()
1276    {
1277        // from repository
1278        if (isset($_REQUEST["ref_id"])) {
1279            $url = $this->ctrl->getLinkTargetByClass('ilCalendarMonthGUI');
1280        }
1281        // from panel
1282        elseif (isset($_GET['panel'])) {
1283            $url = $this->ctrl->getLinkTargetByClass('ilCalendarPresentationGUI');
1284        }
1285        // from appointments
1286        else {
1287            $url = $this->ctrl->getLinkTarget($this, 'appointmentList');
1288        }
1289        return $url;
1290    }
1291
1292    /**
1293     * display settings gui
1294     */
1295    public function settings()
1296    {
1297        global $DIC;
1298
1299        $tpl = $DIC['tpl'];
1300        $ilTabs = $DIC['ilTabs'];
1301        $ilHelp = $DIC['ilHelp'];
1302
1303        $ilHelp->setScreenId("consultation_hours_settings");
1304        $ilTabs->activateTab('ch_settings');
1305
1306        $form = $this->initSettingsForm();
1307        $tpl->setContent($form->getHTML());
1308    }
1309
1310    /**
1311     * build settings form
1312     * @return object
1313     */
1314    protected function initSettingsForm()
1315    {
1316        global $DIC;
1317
1318        $ilDB = $DIC['ilDB'];
1319        $ilUser = $DIC['ilUser'];
1320
1321        include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
1322
1323        $form = new ilPropertyFormGUI();
1324        $form->setFormAction($this->ctrl->getFormAction($this));
1325
1326        $mng = new ilTextInputGUI($this->lng->txt('cal_ch_manager'), 'mng');
1327        $mng->setInfo($this->lng->txt('cal_ch_manager_info'));
1328        $form->addItem($mng);
1329
1330        $mng->setValue(ilConsultationHourAppointments::getManager(true));
1331
1332        $form->setTitle($this->lng->txt('settings'));
1333        $form->addCommandButton('updateSettings', $this->lng->txt('save'));
1334        // $form->addCommandButton('appointmentList', $this->lng->txt('cancel'));
1335        return $form;
1336    }
1337
1338    /**
1339     * save settings
1340     */
1341    public function updateSettings()
1342    {
1343        global $DIC;
1344
1345        $ilDB = $DIC['ilDB'];
1346        $ilCtrl = $DIC['ilCtrl'];
1347        $ilUser = $DIC['ilUser'];
1348        $tpl = $DIC['tpl'];
1349        $ilTabs = $DIC['ilTabs'];
1350
1351        $form = $this->initSettingsForm();
1352        if ($form->checkInput()) {
1353            $mng = $form->getInput('mng');
1354            if (ilConsultationHourAppointments::setManager($mng)) {
1355                ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
1356                $ilCtrl->redirect($this, 'settings');
1357            } else {
1358                $ilTabs->activateTab('ch_settings');
1359
1360                ilUtil::sendFailure($this->lng->txt('cal_ch_unknown_user'));
1361                $field = $form->getItemByPostVar('mng');
1362                $field->setValue($mng);
1363                $tpl->setContent($form->getHTML());
1364                return;
1365            }
1366        }
1367    }
1368}
1369