ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBookingPreferencesDBRepository.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $db;
16
20 protected $factory;
21
26 {
27 global $DIC;
28
29 $this->db = ($db)
30 ? $db
31 : $DIC->database();
32 $this->factory = $factory;
33 }
34
41 public function getPreferences(int $a_pool_id)
42 {
43 $db = $this->db;
44
45 $set = $db->queryF(
46 "SELECT * FROM booking_preferences " .
47 " WHERE book_pool_id = %s ",
48 array("integer"),
49 array($a_pool_id)
50 );
51 $preferences = [];
52 while ($rec = $db->fetchAssoc($set)) {
53 if (!is_array($preferences[$rec["user_id"]]) || !in_array($rec["book_obj_id"], $preferences[$rec["user_id"]])) {
54 $preferences[$rec["user_id"]][] = $rec["book_obj_id"];
55 }
56 }
57 return $this->factory->preferences($preferences);
58 }
59
67 public function getPreferencesOfUser(int $a_pool_id, int $a_user_id)
68 {
69 $db = $this->db;
70
71 $set = $db->queryF(
72 "SELECT * FROM booking_preferences " .
73 " WHERE book_pool_id = %s " .
74 " AND user_id = %s ",
75 array("integer", "integer"),
76 array($a_pool_id, $a_user_id)
77 );
78 $preferences = [];
79 while ($rec = $db->fetchAssoc($set)) {
80 if (!is_array($preferences[$rec["user_id"]]) || !in_array($rec["book_obj_id"], $preferences[$rec["user_id"]])) {
81 $preferences[$rec["user_id"]][] = $rec["book_obj_id"];
82 }
83 }
84 return $this->factory->preferences($preferences);
85 }
86
93 public function savePreferences(int $a_pool_id, ilBookingPreferences $preferences)
94 {
95 $db = $this->db;
96
97 $db->manipulateF(
98 "DELETE FROM booking_preferences WHERE " .
99 " book_pool_id = %s",
100 array("integer"),
101 array($a_pool_id)
102 );
103
104 foreach ($preferences as $user_id => $obj_ids) {
105 if (is_array($obj_ids) && $user_id > 0) {
106 foreach ($obj_ids as $obj_id) {
107 $db->insert("booking_preferences", array(
108 "book_pool_id" => array("integer", $a_pool_id),
109 "user_id" => array("integer", $user_id),
110 "book_obj_id" => array("integer", $obj_id),
111 ));
112 }
113 }
114 }
115 }
116
124 public function savePreferencesOfUser(int $a_pool_id, int $a_user_id, ilBookingPreferences $preferences)
125 {
126 $db = $this->db;
127
128 $db->manipulateF(
129 "DELETE FROM booking_preferences WHERE " .
130 " book_pool_id = %s" .
131 " AND user_id = %s",
132 array("integer","integer"),
133 array($a_pool_id, $a_user_id)
134 );
135
136 foreach ($preferences->getPreferences() as $user_id => $obj_ids) {
137 if (is_array($obj_ids) && $user_id == $a_user_id) {
138 foreach ($obj_ids as $obj_id) {
139 $db->insert("booking_preferences", array(
140 "book_pool_id" => array("integer", $a_pool_id),
141 "user_id" => array("integer", $user_id),
142 "book_obj_id" => array("integer", $obj_id),
143 ));
144 }
145 }
146 }
147 }
148}
An exception for terminatinating execution or to throw for unit testing.
savePreferencesOfUser(int $a_pool_id, int $a_user_id, ilBookingPreferences $preferences)
Save all preferences of a user for a pool.
savePreferences(int $a_pool_id, ilBookingPreferences $preferences)
Save all preferences of a pool.
getPreferences(int $a_pool_id)
Get booking preferences for a pool id.
__construct(ilBookingPreferencesFactory $factory, ilDBInterface $db=null)
Constructor.
getPreferencesOfUser(int $a_pool_id, int $a_user_id)
Get booking preferences for a pool id.
Factory for booking preference data objects.
getPreferences()
Get user preferences.
Interface ilDBInterface.
$DIC
Definition: xapitoken.php:46