ILIAS  release_8 Revision v8.23
class.ilConsultationHourUtils.php
Go to the documentation of this file.
1 <?php
2 
19 declare(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 
38  $obj_id = \ilObject::_lookupObjId($ref_id);
39  $participants = \ilParticipants::getInstance($ref_id);
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;
49  $appointments = \ilConsultationHourAppointments::getAppointments($user_id);
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'),
84  'txt' => ilObjUser::_lookupFullname($user_id)
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(
116  $end->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC),
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): 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);
164  return true;
165  }
166 
170  public static function cancelBooking(int $a_usr_id, int $a_app_id, bool $a_send_notification = true): bool
171  {
172  // Delete personal copy of appointment
173  $app = new ilCalendarEntry($a_app_id);
174 
176  $a_usr_id,
177  $app->getContextId(),
178  $app->getStart(),
180  false
181  );
182  foreach ($user_apps as $uapp_id) {
183  $uapp = new ilCalendarEntry($uapp_id);
184  $uapp->delete();
185 
187 
188  break;
189  }
190 
191  // Delete booking entries
192  // Send notification
193  $booking = new ilBookingEntry($app->getContextId());
194  if ($a_send_notification) {
195  $booking->cancelBooking($a_app_id, $a_usr_id);
196  } else {
197  $booking->deleteBooking($a_app_id, $a_usr_id);
198  }
199  return true;
200  }
201 
206  public static function lookupManagedUsers($a_usr_id): array
207  {
208  global $DIC;
209 
210  $ilDB = $DIC->database();
211  $query = 'SELECT user_id FROM cal_ch_settings ' .
212  'WHERE admin_id = ' . $ilDB->quote($a_usr_id, 'integer');
213  $res = $ilDB->query($query);
214 
215  $users = array();
216  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
217  $users[] = (int) $row->user_id;
218  }
219  return $users;
220  }
221 }
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
$app
Definition: cli.php:39
$res
Definition: ltiservices.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_DATETIME
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.
$lng
static _lookupFullname(int $a_user_id)
static lookupManagedUsers($a_usr_id)
Lookup managed users.
static getInstance(int $a_ref_id)
static cancelBooking(int $a_usr_id, int $a_app_id, bool $a_send_notification=true)
Cancel a booking.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
static getConsultationHourLinksForRepositoryObject(int $ref_id, int $current_user_id, array $ctrl_class_structure)
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
static bookAppointment(int $a_usr_id, int $a_app_id)
Book an appointment.
const IL_CAL_DAY
static getAppointmentIds(int $a_user_id, int $a_context_id=null, ?ilDateTime $a_start=null, ?int $a_type=null, bool $a_check_owner=true)
$ref_id
Definition: ltiauth.php:67
static _deleteByAppointmentId(int $a_app_id)
Delete appointment assignment.
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.
cancelBooking(int $a_entry_id, ?int $a_user_id=null)
cancel calendar booking for user
$query
static _getLanguage(string $a_lang_key='')
Get language object.
static getAppointments(int $a_user_id)
Get all appointments.
const IL_CAL_DATE
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 findCalendarAppointmentsForBooking(ilBookingEntry $booking, ilDateTime $start, ilDateTime $end)
book(int $a_entry_id, ?int $a_user_id=null)
book calendar entry for user