ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilConsultationHourUtils.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
27 int $ref_id,
28 int $current_user_id,
29 array $ctrl_class_structure
30 ): array {
31 global $DIC;
32
33 $ctrl = $DIC->ctrl();
34 $logger = $DIC->logger()->cal();
35 $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', '');
36 $ctrl->setParameterByClass(end($ctrl_class_structure), 'category_id', '');
37
40 $candidates = array_unique(array_merge(
41 $participants->getAdmins(),
42 $participants->getTutors()
43 ));
44 $users = \ilBookingEntry::lookupBookableUsersForObject([$obj_id], $candidates);
45 $now = new \ilDateTime(time(), IL_CAL_UNIX);
46 $links = [];
47 foreach ($users as $user_id) {
48 $next_entry = null;
50 $category_id = 0;
51 foreach ($appointments as $entry) {
52 // find next entry
53 if (ilDateTime::_before($entry->getStart(), $now, IL_CAL_DAY)) {
54 continue;
55 }
56 $category_id = ilCalendarCategoryAssignments::_lookupCategories($entry->getEntryId())[0] ?? 0;
57 $booking_entry = new ilBookingEntry($entry->getContextId());
58 if (count($booking_entry->getTargetObjIds()) > 0 && !in_array($obj_id, $booking_entry->getTargetObjIds())) {
59 continue;
60 }
61 if (!$booking_entry->isAppointmentBookableForUser($entry->getEntryId(), $current_user_id)) {
62 continue;
63 }
64 $next_entry = $entry;
65 break;
66 }
67
68 $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', $user_id);
69 if ($category_id > 0) {
70 $ctrl->setParameterByClass(end($ctrl_class_structure), 'category_id', $category_id);
71 }
72 if ($next_entry instanceof \ilCalendarEntry) {
73 $arr = explode("-", $next_entry->getStart()->get(IL_CAL_DATE));
74 $arr[2] = "01";
75 $seed = implode("-", $arr);
76 $ctrl->setParameterByClass(
77 end($ctrl_class_structure),
78 'seed',
79 $seed
80 );
81 }
82 $current_link = [
83 'link' => $ctrl->getLinkTargetByClass($ctrl_class_structure, 'selectCHCalendarOfUser'),
85 ];
86 $links[] = $current_link;
87 }
88 // Reset control structure links
89 $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', '');
90 $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', '');
91 $ctrl->setParameterByClass(end($ctrl_class_structure), 'category_id', '');
92 return $links;
93 }
94
98 public static function findCalendarAppointmentsForBooking(
99 ilBookingEntry $booking,
100 ilDateTime $start,
101 ilDateTime $end
102 ): array {
103 global $DIC;
104
105 $db = $DIC->database();
106
107 $query = 'select ce.cal_id from cal_entries ce ' .
108 'join cal_cat_assignments cca on ce.cal_id = cca.cal_id ' .
109 'join cal_categories cc on cca.cat_id = cc.cat_id ' .
110 'where context_id = ' . $db->quote($booking->getId(), 'integer') . ' ' .
111 'and starta = ' . $db->quote(
112 $start->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC),
114 ) . ' ' .
115 'and enda = ' . $db->quote(
118 ) . ' ' .
119 'and type = ' . $db->quote(\ilCalendarCategory::TYPE_CH, 'integer');
120 $res = $db->query($query);
121
122 $calendar_apppointments = [];
123 while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
124 $calendar_apppointments[] = (int) $row->cal_id;
125 }
126 return $calendar_apppointments;
127 }
128
135 public static function bookAppointment(int $a_usr_id, int $a_app_id, string $comment = ''): bool
136 {
137 global $DIC;
138
139 $lng = $DIC->language();
140
141 // Create new default consultation hour calendar
142 $cal_lang = ilLanguageFactory::_getLanguage($lng->getDefaultLanguage());
143 $cal_lang->loadLanguageModule('dateplaner');
144
147 $a_usr_id,
148 $cal_lang->txt('cal_ch_personal_ch'),
149 true
150 );
151
152 // duplicate appointment
153 $app = new ilCalendarEntry($a_app_id);
154 $personal_app = clone $app;
155 $personal_app->save();
156
157 // assign appointment to category
158 $assignment = new ilCalendarCategoryAssignments($personal_app->getEntryId());
159 $assignment->addAssignment($ch->getCategoryID());
160
161 // book appointment
162 $booking = new ilBookingEntry($app->getContextId());
163 $booking->book($app->getEntryId(), $a_usr_id);
165 $app->getEntryId(),
166 $a_usr_id,
168 );
169
170 $mail = new ilCalendarMailNotification();
171 $mail->setAppointmentId($app->getEntryId());
172 $mail->setRecipients([$a_usr_id]);
174 $mail->send();
175
176 $recipients = [$booking->getObjId()];
177 $mail = new ilCalendarMailNotification();
178 $mail->setAppointmentId($app->getEntryId());
179 $mail->setBookerID($a_usr_id);
180 $mail->setRecipients($recipients);
182 $mail->send();
183
185 false,
186 false,
187 $booking->getObjId()
188 );
189 if ($manager > 0) {
190 $recipients = [$manager];
191 $mail = new ilCalendarMailNotification();
192 $mail->setAppointmentId($app->getEntryId());
193 $mail->setBookerID($a_usr_id);
194 $mail->setRecipients($recipients);
196 $mail->send();
197 }
198 return true;
199 }
200
204 public static function cancelBooking(int $a_usr_id, int $a_app_id, bool $a_send_notification = true): bool
205 {
206 // Delete personal copy of appointment
207 $app = new ilCalendarEntry($a_app_id);
208
210 $a_usr_id,
211 $app->getContextId(),
212 $app->getStart(),
214 false
215 );
216
217 // Delete booking entries
218 // Send notification
219 $booking = new ilBookingEntry($app->getContextId());
220 if ($a_send_notification) {
221 $booking->cancelBooking($a_app_id, $a_usr_id);
222 } else {
223 $booking->deleteBooking($a_app_id, $a_usr_id);
224 }
225
226 foreach ($user_apps as $uapp_id) {
227 $uapp = new ilCalendarEntry($uapp_id);
228 $uapp->delete();
229
231
232 break;
233 }
234
235 return true;
236 }
237
242 public static function lookupManagedUsers($a_usr_id): array
243 {
244 global $DIC;
245
246 $ilDB = $DIC->database();
247 $query = 'SELECT user_id FROM cal_ch_settings ' .
248 'WHERE admin_id = ' . $ilDB->quote($a_usr_id, 'integer');
249 $res = $ilDB->query($query);
250
251 $users = array();
252 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
253 $users[] = (int) $row->user_id;
254 }
255 return $users;
256 }
257}
$comment
Definition: buildRTE.php:72
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
const IL_CAL_DAY
Booking definition.
book(int $a_entry_id, ?int $a_user_id=null)
book calendar entry for user
deleteBooking(int $a_entry_id, int $a_user_id)
Delete booking.
cancelBooking(int $a_entry_id, ?int $a_user_id=null)
cancel calendar booking for user
static writeBookingMessage(int $a_entry_id, int $a_usr_id, string $a_message)
Write booking message.
static lookupBookableUsersForObject(array $a_obj_id, array $a_user_ids)
Consultation hours are offered if 1) consultation hour owner is admin or tutor and no object assignme...
static _deleteByAppointmentId(int $a_app_id)
Delete appointment assignment.
Model for a calendar entry.
Distributes calendar mail notifications.
static initDefaultCalendarByType(int $a_type_id, int $a_usr_id, string $a_title, bool $a_create=false)
Init the default calendar for given type and user.
static getManager(bool $a_as_name=false, bool $a_full_name=false, ?int $a_user_id=null)
Get consultation hour manager for current user or specific user.
static getAppointments(int $a_user_id)
Get all appointments.
static getAppointmentIds(int $a_user_id, ?int $a_context_id=null, ?ilDateTime $a_start=null, ?int $a_type=null, bool $a_check_owner=true)
static getConsultationHourLinksForRepositoryObject(int $ref_id, int $current_user_id, array $ctrl_class_structure)
static cancelBooking(int $a_usr_id, int $a_app_id, bool $a_send_notification=true)
Cancel a booking.
static lookupManagedUsers($a_usr_id)
Lookup managed users.
static findCalendarAppointmentsForBooking(ilBookingEntry $booking, ilDateTime $start, ilDateTime $end)
static bookAppointment(int $a_usr_id, int $a_app_id, string $comment='')
Book an appointment.
@classDescription Date and time handling
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
static _before(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is before end This method does not consider tz offsets.
static _getLanguage(string $a_lang_key='')
Get language object.
static _lookupFullname(int $a_user_id)
static _lookupObjId(int $ref_id)
static getInstance(int $a_ref_id)
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26