ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilBookingPreferencesManager Class Reference

Booking preferences business logic. More...

+ Collaboration diagram for ilBookingPreferencesManager:

Public Member Functions

 __construct (ilObjBookingPool $pool, ilBookingPrefBasedBookGatewayRepository $book_repo, int $current_time=null, $bookings_per_user=self::BOOKINGS_PER_USER_DEFAULT)
 Constructor. More...
 
 isGivingPreferencesPossible ()
 Can participants hand in preferences. More...
 
 isPreferenceDeadlineReached ()
 Can participants hand in preferences. More...
 
 storeBookings ($preferences, $booking_object_ids=null)
 Calculate and store bookings. More...
 
 readBookings ()
 Read the bookings. More...
 
 calculateBookings (ilBookingPreferences $preferences, $booking_object_ids=null, $availability=null)
 Calculate bookings. More...
 
 hasRun ()
 
 resetRun ()
 

Data Fields

const BOOKINGS_PER_USER_DEFAULT = 1
 

Protected Member Functions

 addBooking (&$bookings, &$preferences, &$availability, $user_id, $book_obj_id)
 Add booking. More...
 
 selectRandomEntry ($items)
 Select a random entry of an array. More...
 
 getUsersForObject ($preferences, $sel_obj_id)
 Get users for object. More...
 
 calculatePopularity (array $booking_object_ids, array $preferences)
 Calculate popularity (number of preferences each object got from users) More...
 
 getObjectWithLowestPopularity ($popularity, $availability)
 Get an availabe object with lowest popularity > 0. More...
 
 removePreference ($user_id, $obj_id, $preferences)
 Remove a preference from the preference array. More...
 
 removeObjectFromPreferences ($obj_id, $preferences)
 Remove an object from the preference array. More...
 
 removeUserFromPreferences ($user_id, $preferences)
 Remove user from preference array. More...
 
 chooseRandomUserFromPreferences ($preferences)
 Choose random user from the preference array. More...
 
 getMinimalAssignedEntryForUser ($booking_object_ids, $bookings, $user_preferences, $availability)
 Get an available object within the preferences (if no preferences left, even outside of preferences) of a user that is currently minimal assigned. More...
 

Protected Attributes

 $pool
 
 $current_time
 
 $bookings_per_user
 
 $book_repo
 

Detailed Description

Booking preferences business logic.

Author
killi.nosp@m.ng@l.nosp@m.eifos.nosp@m..de

Definition at line 10 of file class.ilBookingPreferencesManager.php.

Constructor & Destructor Documentation

◆ __construct()

ilBookingPreferencesManager::__construct ( ilObjBookingPool  $pool,
ilBookingPrefBasedBookGatewayRepository  $book_repo,
int  $current_time = null,
  $bookings_per_user = self::BOOKINGS_PER_USER_DEFAULT 
)

Constructor.

Parameters
ilObjBookingPool$pool
int | null$current_time

Definition at line 39 of file class.ilBookingPreferencesManager.php.

References $book_repo, $bookings_per_user, $current_time, and $pool.

Member Function Documentation

◆ addBooking()

ilBookingPreferencesManager::addBooking ( $bookings,
$preferences,
$availability,
  $user_id,
  $book_obj_id 
)
protected

Add booking.

Parameters
$bookings
$preferences
$availability
$user_id
$book_obj_id

Definition at line 200 of file class.ilBookingPreferencesManager.php.

References removeObjectFromPreferences(), removePreference(), and removeUserFromPreferences().

Referenced by calculateBookings().

201  {
202  $bookings[$user_id][] = $book_obj_id;
203  $availability[$book_obj_id]--;
204  if (count($bookings[$user_id]) >= $this->bookings_per_user) {
205  $preferences = $this->removeUserFromPreferences($user_id, $preferences);
206  } else {
207  $preferences = $this->removePreference($user_id, $book_obj_id, $preferences);
208  }
209  if ($availability[$book_obj_id] <= 0) {
210  $preferences = $this->removeObjectFromPreferences($book_obj_id, $preferences);
211  }
212  }
removePreference($user_id, $obj_id, $preferences)
Remove a preference from the preference array.
removeObjectFromPreferences($obj_id, $preferences)
Remove an object from the preference array.
removeUserFromPreferences($user_id, $preferences)
Remove user from preference array.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculateBookings()

ilBookingPreferencesManager::calculateBookings ( ilBookingPreferences  $preferences,
  $booking_object_ids = null,
  $availability = null 
)

Calculate bookings.

Parameters
ilBookingPreferences$preferences
int[]$booking_object_ids
Returns
array
Exceptions
ilBookingCalculationException

Definition at line 115 of file class.ilBookingPreferencesManager.php.

References $i, addBooking(), calculatePopularity(), chooseRandomUserFromPreferences(), ilBookingObject\getList(), getMinimalAssignedEntryForUser(), ilBookingReservation\getNumAvailablesNoSchedule(), getObjectWithLowestPopularity(), ilBookingPreferences\getPreferences(), getUsersForObject(), isPreferenceDeadlineReached(), removeObjectFromPreferences(), removeUserFromPreferences(), and selectRandomEntry().

Referenced by storeBookings().

119  {
120  $preferences = $preferences->getPreferences();
121 
122  // we calculate if a) any preferences are given and b) the deadline is reached
123  /*if (!is_array($preferences) || count($preferences) == 0) {
124  throw new ilBookingCalculationException("No preferences given.");
125  }*/
126  if (!$this->isPreferenceDeadlineReached()) {
127  throw new ilBookingCalculationException("Preference deadline not reached.");
128  }
129 
130  if ($booking_object_ids == null) {
131  $booking_object_ids = array_map(function ($i) {
132  return $i["booking_object_id"];
133  }, ilBookingObject::getList($this->pool->getId()));
134  }
135 
136  if ($availability == null) {
137  $availability = [];
138  foreach ($booking_object_ids as $book_obj_id) {
139  $availability[$book_obj_id] = ilBookingReservation::getNumAvailablesNoSchedule($book_obj_id);
140  }
141  }
142 
143  // remove all objects from the preferences
144  // that are already not available anymore
145  // see bug 30204 (a tutor booked an object already before and made it unavailable)
146  foreach ($availability as $book_obj_id => $cnt) {
147  if ($cnt == 0) {
148  $preferences = $this->removeObjectFromPreferences($book_obj_id, $preferences);
149  }
150  }
151 
152  $bookings = [];
153 
154  $end_phase_one = false;
155 
156  // phase one: assign lowest popular items to random user
157  while (!$end_phase_one) {
158  $popularity = $this->calculatePopularity($booking_object_ids, $preferences);
159 
160  $low_pop_book_obj_id = $this->getObjectWithLowestPopularity($popularity, $availability);
161  if ($low_pop_book_obj_id > 0) {
162  $user_ids = $this->getUsersForObject($preferences, $low_pop_book_obj_id);
163  if (count($user_ids) > 0) {
164  $user_id = $this->selectRandomEntry($user_ids);
165  $this->addBooking($bookings, $preferences, $availability, $user_id, $low_pop_book_obj_id);
166  }
167  } else {
168  $end_phase_one = true;
169  }
170  }
171 
172  $end_phase_two = false;
173 
174  // choose random user from and assign currently rarely assigned objects
175  while (!$end_phase_two) {
176  $random_user_id = $this->chooseRandomUserFromPreferences($preferences);
177  if ($random_user_id > 0) {
178  $rare_assigned_book_obj_id = $this->getMinimalAssignedEntryForUser($booking_object_ids, $bookings, $preferences[$random_user_id], $availability);
179  if ($rare_assigned_book_obj_id > 0) {
180  $this->addBooking($bookings, $preferences, $availability, $random_user_id, $rare_assigned_book_obj_id);
181  } else {
182  $preferences = $this->removeUserFromPreferences($random_user_id, $preferences);
183  }
184  } else {
185  $end_phase_two = true;
186  }
187  }
188  return $bookings;
189  }
getObjectWithLowestPopularity($popularity, $availability)
Get an availabe object with lowest popularity > 0.
static getList($a_pool_id, $a_title=null)
Get list of booking objects for given type.
selectRandomEntry($items)
Select a random entry of an array.
chooseRandomUserFromPreferences($preferences)
Choose random user from the preference array.
calculatePopularity(array $booking_object_ids, array $preferences)
Calculate popularity (number of preferences each object got from users)
getUsersForObject($preferences, $sel_obj_id)
Get users for object.
addBooking(&$bookings, &$preferences, &$availability, $user_id, $book_obj_id)
Add booking.
removeObjectFromPreferences($obj_id, $preferences)
Remove an object from the preference array.
getMinimalAssignedEntryForUser($booking_object_ids, $bookings, $user_preferences, $availability)
Get an available object within the preferences (if no preferences left, even outside of preferences) ...
getPreferences()
Get user preferences.
removeUserFromPreferences($user_id, $preferences)
Remove user from preference array.
static getNumAvailablesNoSchedule($a_obj_id)
$i
Definition: metadata.php:24
isPreferenceDeadlineReached()
Can participants hand in preferences.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ calculatePopularity()

ilBookingPreferencesManager::calculatePopularity ( array  $booking_object_ids,
array  $preferences 
)
protected

Calculate popularity (number of preferences each object got from users)

Parameters
array$booking_object_ids
array$preferences
Returns
array

Definition at line 256 of file class.ilBookingPreferencesManager.php.

Referenced by calculateBookings().

257  {
258  $popularity = [];
259  foreach ($booking_object_ids as $book_obj_id) {
260  $popularity[$book_obj_id] = 0;
261  }
262  foreach ($preferences as $user_id => $bobj_ids) {
263  foreach ($bobj_ids as $bobj_id) {
264  $popularity[$bobj_id] += 1;
265  }
266  }
267 
268  return $popularity;
269  }
+ Here is the caller graph for this function:

◆ chooseRandomUserFromPreferences()

ilBookingPreferencesManager::chooseRandomUserFromPreferences (   $preferences)
protected

Choose random user from the preference array.

Parameters
array$preferences
Returns
int

Definition at line 347 of file class.ilBookingPreferencesManager.php.

References selectRandomEntry().

Referenced by calculateBookings().

348  {
349  $user_ids = array_keys($preferences);
350  return $this->selectRandomEntry($user_ids);
351  }
selectRandomEntry($items)
Select a random entry of an array.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMinimalAssignedEntryForUser()

ilBookingPreferencesManager::getMinimalAssignedEntryForUser (   $booking_object_ids,
  $bookings,
  $user_preferences,
  $availability 
)
protected

Get an available object within the preferences (if no preferences left, even outside of preferences) of a user that is currently minimal assigned.

Parameters
int[]$booking_object_ids
int[][]$bookings
int[]$user_preferences
Returns
int

Definition at line 362 of file class.ilBookingPreferencesManager.php.

Referenced by calculateBookings().

363  {
364  // count the assignments per object
365  $count_assignments = [];
366  foreach ($booking_object_ids as $obj_id) {
367  $count_assignments[$obj_id] = 0;
368  }
369  foreach ($bookings as $user => $obj_ids) {
370  foreach ($obj_ids as $obj_id) {
371  $count_assignments[$obj_id]++;
372  }
373  }
374 
375  // sort the objects by number of assignments, return the first one being found in the user preferences
376  asort($count_assignments, SORT_NUMERIC);
377  foreach ($count_assignments as $obj_id => $cnt) {
378  // if no preferences left for user, even assign object outside preferences
379  // otherwise choose object from preferences
380  if ((count($user_preferences) == 0 || in_array($obj_id, $user_preferences))
381  && $availability[$obj_id] > 0) {
382  return (int) $obj_id;
383  }
384  }
385  return 0;
386  }
+ Here is the caller graph for this function:

◆ getObjectWithLowestPopularity()

ilBookingPreferencesManager::getObjectWithLowestPopularity (   $popularity,
  $availability 
)
protected

Get an availabe object with lowest popularity > 0.

Parameters
array$popularity
Returns
int

Definition at line 278 of file class.ilBookingPreferencesManager.php.

Referenced by calculateBookings().

279  {
280  asort($popularity, SORT_NUMERIC);
281  foreach ($popularity as $obj_id => $pop) {
282  if ($pop > 0 && $availability[$obj_id] > 0) {
283  return (int) $obj_id;
284  }
285  }
286  return 0;
287  }
+ Here is the caller graph for this function:

◆ getUsersForObject()

ilBookingPreferencesManager::getUsersForObject (   $preferences,
  $sel_obj_id 
)
protected

Get users for object.

Parameters
$preferences
$sel_obj_id
Returns
array

Definition at line 235 of file class.ilBookingPreferencesManager.php.

Referenced by calculateBookings().

236  {
237  $user_ids = [];
238  foreach ($preferences as $user_id => $obj_ids) {
239  foreach ($obj_ids as $obj_id) {
240  if ($obj_id == $sel_obj_id) {
241  $user_ids[] = $user_id;
242  }
243  }
244  }
245  return $user_ids;
246  }
+ Here is the caller graph for this function:

◆ hasRun()

ilBookingPreferencesManager::hasRun ( )

Definition at line 388 of file class.ilBookingPreferencesManager.php.

388  : bool
389  {
390  return $this->book_repo->hasRun($this->pool->getId());
391  }

◆ isGivingPreferencesPossible()

ilBookingPreferencesManager::isGivingPreferencesPossible ( )

Can participants hand in preferences.

Returns
bool

Definition at line 58 of file class.ilBookingPreferencesManager.php.

References $current_time, and ilObjBookingPool\TYPE_NO_SCHEDULE_PREFERENCES.

59  {
60  if ($this->pool->getScheduleType() == ilObjBookingPool::TYPE_NO_SCHEDULE_PREFERENCES &&
61  $this->pool->getPreferenceDeadline() > $this->current_time) {
62  return true;
63  }
64  return false;
65  }

◆ isPreferenceDeadlineReached()

ilBookingPreferencesManager::isPreferenceDeadlineReached ( )

Can participants hand in preferences.

Returns
bool

Definition at line 72 of file class.ilBookingPreferencesManager.php.

References $current_time, and ilObjBookingPool\TYPE_NO_SCHEDULE_PREFERENCES.

Referenced by calculateBookings().

73  {
74  if ($this->pool->getScheduleType() == ilObjBookingPool::TYPE_NO_SCHEDULE_PREFERENCES &&
75  $this->pool->getPreferenceDeadline() < $this->current_time) {
76  return true;
77  }
78  return false;
79  }
+ Here is the caller graph for this function:

◆ readBookings()

ilBookingPreferencesManager::readBookings ( )

Read the bookings.

Returns
int[][]

Definition at line 99 of file class.ilBookingPreferencesManager.php.

References $i, and ilBookingObject\getList().

100  {
101  $booking_object_ids = array_map(function ($i) {
102  return $i["booking_object_id"];
103  }, ilBookingObject::getList($this->pool->getId()));
104  return $this->book_repo->getBookings($booking_object_ids);
105  }
static getList($a_pool_id, $a_title=null)
Get list of booking objects for given type.
$i
Definition: metadata.php:24
+ Here is the call graph for this function:

◆ removeObjectFromPreferences()

ilBookingPreferencesManager::removeObjectFromPreferences (   $obj_id,
  $preferences 
)
protected

Remove an object from the preference array.

Parameters
int$user_id
int$obj_id
array$preferences
Returns
array

Definition at line 315 of file class.ilBookingPreferencesManager.php.

References $i.

Referenced by addBooking(), and calculateBookings().

316  {
317  $new_preferences = [];
318  foreach ($preferences as $user_id => $obj_ids) {
319  $new_preferences[$user_id] = array_filter($preferences[$user_id], function ($i) use ($obj_id) {
320  return ($i != $obj_id);
321  });
322  }
323  return $new_preferences;
324  }
$i
Definition: metadata.php:24
+ Here is the caller graph for this function:

◆ removePreference()

ilBookingPreferencesManager::removePreference (   $user_id,
  $obj_id,
  $preferences 
)
protected

Remove a preference from the preference array.

Parameters
int$user_id
int$obj_id
array$preferences
Returns
array

Definition at line 297 of file class.ilBookingPreferencesManager.php.

References $i.

Referenced by addBooking().

298  {
299  if (is_array($preferences[$user_id])) {
300  $preferences[$user_id] = array_filter($preferences[$user_id], function ($i) use ($obj_id) {
301  return ($i != $obj_id);
302  });
303  }
304  return $preferences;
305  }
$i
Definition: metadata.php:24
+ Here is the caller graph for this function:

◆ removeUserFromPreferences()

ilBookingPreferencesManager::removeUserFromPreferences (   $user_id,
  $preferences 
)
protected

Remove user from preference array.

Parameters
int$user_id
array$preferences
Returns
array

Definition at line 333 of file class.ilBookingPreferencesManager.php.

Referenced by addBooking(), and calculateBookings().

334  {
335  if (is_array($preferences[$user_id])) {
336  unset($preferences[$user_id]);
337  }
338  return $preferences;
339  }
+ Here is the caller graph for this function:

◆ resetRun()

ilBookingPreferencesManager::resetRun ( )

Definition at line 393 of file class.ilBookingPreferencesManager.php.

393  : void
394  {
395  $this->book_repo->resetRun($this->pool->getId());
396  }

◆ selectRandomEntry()

ilBookingPreferencesManager::selectRandomEntry (   $items)
protected

Select a random entry of an array.

Parameters

Definition at line 221 of file class.ilBookingPreferencesManager.php.

Referenced by calculateBookings(), and chooseRandomUserFromPreferences().

222  {
223  $nr = rand(0, count($items) - 1);
224  return $items[$nr];
225  }
+ Here is the caller graph for this function:

◆ storeBookings()

ilBookingPreferencesManager::storeBookings (   $preferences,
  $booking_object_ids = null 
)

Calculate and store bookings.

Parameters
ilBookingPreferences$preferences
int[]$booking_object_ids
Exceptions
ilBookingCalculationException

Definition at line 88 of file class.ilBookingPreferencesManager.php.

References calculateBookings().

89  {
90  $bookings = $this->calculateBookings($preferences, $booking_object_ids);
91  $this->book_repo->storeBookings($this->pool->getId(), $bookings);
92  }
calculateBookings(ilBookingPreferences $preferences, $booking_object_ids=null, $availability=null)
Calculate bookings.
+ Here is the call graph for this function:

Field Documentation

◆ $book_repo

ilBookingPreferencesManager::$book_repo
protected

Definition at line 32 of file class.ilBookingPreferencesManager.php.

Referenced by __construct().

◆ $bookings_per_user

ilBookingPreferencesManager::$bookings_per_user
protected

Definition at line 27 of file class.ilBookingPreferencesManager.php.

Referenced by __construct().

◆ $current_time

ilBookingPreferencesManager::$current_time
protected

◆ $pool

ilBookingPreferencesManager::$pool
protected

Definition at line 17 of file class.ilBookingPreferencesManager.php.

Referenced by __construct().

◆ BOOKINGS_PER_USER_DEFAULT

const ilBookingPreferencesManager::BOOKINGS_PER_USER_DEFAULT = 1

Definition at line 12 of file class.ilBookingPreferencesManager.php.


The documentation for this class was generated from the following file: