ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilConsultationHourUtils.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
12 public static function getConsultationHourLinksForRepositoryObject(int $ref_id, int $current_user_id, array $ctrl_class_structure)
13 {
14 global $DIC;
15
16 $ctrl = $DIC->ctrl();
17 $lng = $DIC->language();
18 $logger = $DIC->logger()->cal();
19
20 $obj_id = \ilObject::_lookupObjId($ref_id);
21 $participants = \ilParticipants::getInstance($ref_id);
22 $candidates = array_unique(array_merge(
23 $participants->getAdmins(),
24 $participants->getTutors()
25 ));
26 $users = \ilBookingEntry::lookupBookableUsersForObject($obj_id, $candidates);
27 $now = new \ilDateTime(time(), IL_CAL_UNIX);
28 $links = [];
29 foreach ($users as $user_id) {
30
31 $next_entry = null;
33 foreach ($appointments as $entry) {
34 // find next entry
35 if (ilDateTime::_before($entry->getStart(), $now, IL_CAL_DAY)) {
36 continue;
37 }
38 $booking_entry = new ilBookingEntry($entry->getContextId());
39 if (!in_array($obj_id, $booking_entry->getTargetObjIds())) {
40 continue;
41 }
42 if (!$booking_entry->isAppointmentBookableForUser($entry->getEntryId(), $current_user_id)) {
43 continue;
44 }
45 $next_entry = $entry;
46 break;
47 }
48
49 $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', $user_id);
50 if ($next_entry instanceof \ilCalendarEntry) {
51 $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', $next_entry->getStart()->get(IL_CAL_DATE));
52 }
53 $current_link = [
54 'link' => $ctrl->getLinkTargetByClass($ctrl_class_structure, 'selectCHCalendarOfUser'),
55 'txt' => str_replace("%1", ilObjUser::_lookupFullname($user_id), $lng->txt("cal_consultation_hours_for_user"))
56 ];
57 $links[] = $current_link;
58 }
59 // Reset control structure links
60 $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', '');
61 $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', '');
62 return $links;
63 }
64
65
66
74 public static function findCalendarAppointmentsForBooking(\ilBookingEntry $booking, \ilDateTime $start, \ilDateTime $end)
75 {
76 global $DIC;
77
78 $db = $DIC->database();
79
80 $query = 'select ce.cal_id from cal_entries ce ' .
81 'join cal_cat_assignments cca on ce.cal_id = cca.cal_id ' .
82 'join cal_categories cc on cca.cat_id = cc.cat_id '.
83 'where context_id = ' . $db->quote($booking->getId(), 'integer') . ' ' .
84 'and starta = ' . $db->quote($start->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC), \ilDBConstants::T_TIMESTAMP) . ' ' .
85 'and enda = ' . $db->quote($end->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC), \ilDBConstants::T_TIMESTAMP) . ' ' .
86 'and type = ' . $db->quote(\ilCalendarCategory::TYPE_CH, 'integer');
87 $res = $db->query($query);
88
89 $calendar_apppointments = [];
90 while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
91 $calendar_apppointments[] = $row->cal_id;
92 }
93 return $calendar_apppointments;
94 }
95
96
103 public static function bookAppointment($a_usr_id, $a_app_id)
104 {
105 global $DIC;
106
107 $lng = $DIC['lng'];
108
109 // Create new default consultation hour calendar
110 include_once './Services/Language/classes/class.ilLanguageFactory.php';
111 $cal_lang = ilLanguageFactory::_getLanguage($lng->getDefaultLanguage());
112 $cal_lang->loadLanguageModule('dateplaner');
113
114 include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
115 include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
118 $a_usr_id,
119 $cal_lang->txt('cal_ch_personal_ch'),
120 true
121 );
122
123 // duplicate appointment
124 include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
125 $app = new ilCalendarEntry($a_app_id);
126 $personal_app = clone $app;
127 $personal_app->save();
128
129 // assign appointment to category
130 include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
131 $assignment = new ilCalendarCategoryAssignments($personal_app->getEntryId());
132 $assignment->addAssignment($ch->getCategoryID());
133
134 // book appointment
135 include_once './Services/Booking/classes/class.ilBookingEntry.php';
136 $booking = new ilBookingEntry($app->getContextId());
137 $booking->book($app->getEntryId(), $a_usr_id);
138 return true;
139 }
140
147 public static function cancelBooking($a_usr_id, $a_app_id, $a_send_notification = true)
148 {
149 // Delete personal copy of appointment
150 include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
151 $app = new ilCalendarEntry($a_app_id);
152
153 include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
155 $a_usr_id,
156 $app->getContextId(),
157 $app->getStart(),
159 false
160 );
161 foreach ($user_apps as $uapp_id) {
162 $uapp = new ilCalendarEntry($uapp_id);
163 $uapp->delete();
164
165 include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
167
168 break;
169 }
170
171 // Delete booking entries
172 // Send notification
173 $booking = new ilBookingEntry($app->getContextId());
174 if ($a_send_notification) {
175 $booking->cancelBooking($a_app_id, $a_usr_id);
176 } else {
177 $booking->deleteBooking($a_app_id, $a_usr_id);
178 }
179 return true;
180 }
181
186 public static function lookupManagedUsers($a_usr_id)
187 {
188 global $DIC;
189
190 $ilDB = $DIC['ilDB'];
191
192 $query = 'SELECT user_id FROM cal_ch_settings ' .
193 'WHERE admin_id = ' . $ilDB->quote($a_usr_id, 'integer');
194 $res = $ilDB->query($query);
195
196 $users = array();
197 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
198 $users[] = $row->user_id;
199 }
200 return $users;
201 }
202}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
const IL_CAL_DAY
Booking definition.
static lookupBookableUsersForObject($a_obj_id, $a_user_ids)
Consultation hours are offered if 1) consultation hour owner is admin or tutor and no object assignme...
static _deleteByAppointmentId($a_app_id)
Delete appointment assignment.
Model for a calendar entry.
static initDefaultCalendarByType($a_type_id, $a_usr_id, $a_title, $a_create=false)
Init the default calendar for given type and user.
static getAppointmentIds($a_user_id, $a_context_id=null, $a_start=null, $a_type=null, $a_check_owner=true)
Get all appointment ids.
static getAppointments($a_user_id)
Get all appointments.
static getConsultationHourLinksForRepositoryObject(int $ref_id, int $current_user_id, array $ctrl_class_structure)
static lookupManagedUsers($a_usr_id)
Lookup managed users.
static findCalendarAppointmentsForBooking(\ilBookingEntry $booking, \ilDateTime $start, \ilDateTime $end)
static cancelBooking($a_usr_id, $a_app_id, $a_send_notification=true)
Cancel a booking.
static bookAppointment($a_usr_id, $a_app_id)
Book an appointment.
@classDescription Date and time handling
get($a_format, $a_format_str='', $a_tz='')
get formatted date
static _before(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
static _getLanguage($a_lang_key='')
Get langauge object.
static _lookupFullname($a_user_id)
Lookup Full Name.
static _lookupObjId($a_id)
static getInstance($a_ref_id)
Get instance by ref_id.
$app
Definition: cli.php:38
$query
$lng
foreach($_POST as $key=> $value) $res
global $ilDB
$DIC
Definition: xapitoken.php:46