ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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  $next_entry = null;
31  $appointments = \ilConsultationHourAppointments::getAppointments($user_id);
32  $category_id = 0;
33  foreach ($appointments as $entry) {
34  // find next entry
35  if (ilDateTime::_before($entry->getStart(), $now, IL_CAL_DAY)) {
36  continue;
37  }
38  $category_id = ilCalendarCategoryAssignments::_lookupCategories($entry->getEntryId())[0] ?? 0;
39  $booking_entry = new ilBookingEntry($entry->getContextId());
40  if (!in_array($obj_id, $booking_entry->getTargetObjIds())) {
41  continue;
42  }
43  if (!$booking_entry->isAppointmentBookableForUser($entry->getEntryId(), $current_user_id)) {
44  continue;
45  }
46  $next_entry = $entry;
47  break;
48  }
49 
50  $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', $user_id);
51  if ( $category_id > 0) {
52  $ctrl->setParameterByClass(end($ctrl_class_structure), 'category_id', $category_id);
53  }
54  if ($next_entry instanceof \ilCalendarEntry) {
55  $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', $next_entry->getStart()->get(IL_CAL_DATE));
56  }
57  $current_link = [
58  'link' => $ctrl->getLinkTargetByClass($ctrl_class_structure, 'selectCHCalendarOfUser'),
59  'txt' => ilObjUser::_lookupFullname($user_id)
60  ];
61  $links[] = $current_link;
62  }
63  // Reset control structure links
64  $ctrl->setParameterByClass(end($ctrl_class_structure), 'seed', '');
65  $ctrl->setParameterByClass(end($ctrl_class_structure), 'ch_user_id', '');
66  $ctrl->setParameterByClass(end($ctrl_class_structure), 'category_id', '');
67  return $links;
68  }
69 
70 
71 
79  public static function findCalendarAppointmentsForBooking(\ilBookingEntry $booking, \ilDateTime $start, \ilDateTime $end)
80  {
81  global $DIC;
82 
83  $db = $DIC->database();
84 
85  $query = 'select ce.cal_id from cal_entries ce ' .
86  'join cal_cat_assignments cca on ce.cal_id = cca.cal_id ' .
87  'join cal_categories cc on cca.cat_id = cc.cat_id ' .
88  'where context_id = ' . $db->quote($booking->getId(), 'integer') . ' ' .
89  'and starta = ' . $db->quote($start->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC), \ilDBConstants::T_TIMESTAMP) . ' ' .
90  'and enda = ' . $db->quote($end->get(IL_CAL_DATETIME, '', \ilTimeZone::UTC), \ilDBConstants::T_TIMESTAMP) . ' ' .
91  'and type = ' . $db->quote(\ilCalendarCategory::TYPE_CH, 'integer');
92  $res = $db->query($query);
93 
94  $calendar_apppointments = [];
95  while ($row = $res->fetchRow(\ilDBConstants::FETCHMODE_OBJECT)) {
96  $calendar_apppointments[] = $row->cal_id;
97  }
98  return $calendar_apppointments;
99  }
100 
101 
108  public static function bookAppointment($a_usr_id, $a_app_id)
109  {
110  global $DIC;
111 
112  $lng = $DIC['lng'];
113 
114  // Create new default consultation hour calendar
115  include_once './Services/Language/classes/class.ilLanguageFactory.php';
116  $cal_lang = ilLanguageFactory::_getLanguage($lng->getDefaultLanguage());
117  $cal_lang->loadLanguageModule('dateplaner');
118 
119  include_once './Services/Calendar/classes/class.ilCalendarUtil.php';
120  include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
123  $a_usr_id,
124  $cal_lang->txt('cal_ch_personal_ch'),
125  true
126  );
127 
128  // duplicate appointment
129  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
130  $app = new ilCalendarEntry($a_app_id);
131  $personal_app = clone $app;
132  $personal_app->save();
133 
134  // assign appointment to category
135  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
136  $assignment = new ilCalendarCategoryAssignments($personal_app->getEntryId());
137  $assignment->addAssignment($ch->getCategoryID());
138 
139  // book appointment
140  include_once './Services/Booking/classes/class.ilBookingEntry.php';
141  $booking = new ilBookingEntry($app->getContextId());
142  $booking->book($app->getEntryId(), $a_usr_id);
143  return true;
144  }
145 
152  public static function cancelBooking($a_usr_id, $a_app_id, $a_send_notification = true)
153  {
154  // Delete personal copy of appointment
155  include_once './Services/Calendar/classes/class.ilCalendarEntry.php';
156  $app = new ilCalendarEntry($a_app_id);
157 
158  include_once './Services/Calendar/classes/ConsultationHours/class.ilConsultationHourAppointments.php';
160  $a_usr_id,
161  $app->getContextId(),
162  $app->getStart(),
164  false
165  );
166  foreach ($user_apps as $uapp_id) {
167  $uapp = new ilCalendarEntry($uapp_id);
168  $uapp->delete();
169 
170  include_once './Services/Calendar/classes/class.ilCalendarCategoryAssignments.php';
172 
173  break;
174  }
175 
176  // Delete booking entries
177  // Send notification
178  $booking = new ilBookingEntry($app->getContextId());
179  if ($a_send_notification) {
180  $booking->cancelBooking($a_app_id, $a_usr_id);
181  } else {
182  $booking->deleteBooking($a_app_id, $a_usr_id);
183  }
184  return true;
185  }
186 
191  public static function lookupManagedUsers($a_usr_id)
192  {
193  global $DIC;
194 
195  $ilDB = $DIC['ilDB'];
196 
197  $query = 'SELECT user_id FROM cal_ch_settings ' .
198  'WHERE admin_id = ' . $ilDB->quote($a_usr_id, 'integer');
199  $res = $ilDB->query($query);
200 
201  $users = array();
202  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
203  $users[] = $row->user_id;
204  }
205  return $users;
206  }
207 }
$app
Definition: cli.php:38
Model for a calendar entry.
const IL_CAL_DATETIME
static lookupManagedUsers($a_usr_id)
Lookup managed users.
static _lookupFullname($a_user_id)
Lookup Full Name.
static findCalendarAppointmentsForBooking(\ilBookingEntry $booking, \ilDateTime $start, \ilDateTime $end)
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 getAppointmentIds($a_user_id, $a_context_id=null, $a_start=null, $a_type=null, $a_check_owner=true)
Get all appointment ids.
Booking definition.
static _lookupCategories($a_cal_id)
lookup categories
const IL_CAL_UNIX
static getConsultationHourLinksForRepositoryObject(int $ref_id, int $current_user_id, array $ctrl_class_structure)
static getInstance($a_ref_id)
Get instance by ref_id.
const IL_CAL_DAY
static _getLanguage($a_lang_key='')
Get langauge object.
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...
foreach($_POST as $key=> $value) $res
$lng
static getAppointments($a_user_id)
Get all appointments.
static _lookupObjId($a_id)
global $DIC
Definition: goto.php:24
cancelBooking($a_entry_id, $a_user_id=false)
cancel calendar booking for user
$query
static _deleteByAppointmentId($a_app_id)
Delete appointment assignment.
get($a_format, $a_format_str='', $a_tz='')
get formatted date
static cancelBooking($a_usr_id, $a_app_id, $a_send_notification=true)
Cancel a booking.
const IL_CAL_DATE
book($a_entry_id, $a_user_id=false)
book calendar entry for user
global $ilDB
static initDefaultCalendarByType($a_type_id, $a_usr_id, $a_title, $a_create=false)
Init the default calendar for given type and user.
static bookAppointment($a_usr_id, $a_app_id)
Book an appointment.