ILIAS  release_8 Revision v8.24
class.ilStartingPoint.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
27 //list view: first and last items in the table are fixed.
28 protected const ORDER_POSITION_MIN = 0;
29 protected const ORDER_POSITION_MAX = 9999;
30
31 //rule options.
32 public const FALLBACK_RULE = 1;
33 public const ROLE_BASED = 2;
34 public const USER_SELECTION_RULE = 3;
35
36 protected ?int $starting_point = null;
37 protected ?int $starting_object = null;
38 protected ?int $starting_position = null;
39 protected ?int $rule_type = null;
40 protected ?string $rule_options = null; // array serialized in db
41 protected int $id;
42 protected ?int $calendar_view = null;
43 protected ?int $calendar_period = null;
44
45 protected ilDBInterface $db;
46
47 public function __construct(int $a_id = 0)
48 {
49 global $DIC;
50
51 $this->db = $DIC->database();
52 $this->id = $a_id;
53 $this->setData();
54 }
55
56 private function setData(): void
57 {
58 $query = "SELECT * FROM usr_starting_point WHERE id = " . $this->db->quote($this->id, 'integer');
59 $res = $this->db->query($query);
60
61 while ($point = $this->db->fetchAssoc($res)) {
62 $this->setStartingPoint((int) $point['starting_point']);
63 $this->setRuleOptions((string) $point['rule_options']);
64 $this->setPosition((int) $point['position']);
65 $this->setStartingObject((int) $point['starting_object']);
66 $this->setRuleType((int) $point['rule_type']);
67 $this->setCalendarView((int) $point['calendar_view']);
68 $this->setCalendarPeriod((int) $point['calendar_period']);
69 }
70 }
71
72 public function setStartingPoint(int $a_starting_point): void
73 {
74 $this->starting_point = $a_starting_point;
75 }
76
77 public function getStartingPoint(): int
78 {
80 }
81
82 public function setStartingObject(int $a_starting_object): void
83 {
84 $this->starting_object = $a_starting_object;
85 }
86
87 public function getStartingObject(): int
88 {
90 }
91
92 public function setPosition(int $a_starting_position): void
93 {
94 $this->starting_position = $a_starting_position;
95 }
96
97 public function getPosition(): int
98 {
100 }
101
102 public function setRuleType(int $a_rule_type): void
103 {
104 $this->rule_type = $a_rule_type;
105 }
106
107 public function getRuleType(): int
108 {
109 return $this->rule_type;
110 }
111
115 public function setRuleOptions(string $a_rule_options): void
116 {
117 $this->rule_options = $a_rule_options;
118 }
119
123 public function getCalendarView(): int
124 {
126 }
127
131 public function setCalendarView(int $calendar_view): void
132 {
133 $this->calendar_view = $calendar_view;
134 }
135
136 public function getCalendarPeriod(): int
137 {
139 }
140
141 public function setCalendarPeriod(int $calendar_period): void
142 {
143 $this->calendar_period = $calendar_period;
144 }
145
146 public function getRuleOptions(): ?string
147 {
148 return $this->rule_options;
149 }
150
154 public static function getStartingPoints(): array
155 {
156 global $DIC;
157
158 $ilDB = $DIC['ilDB'];
159
160 $query = "SELECT * FROM usr_starting_point";
161 $res = $ilDB->query($query);
162 $points = array();
163 while ($point = $ilDB->fetchAssoc($res)) {
164 $points[] = array(
165 "id" => $point["id"],
166 "position" => $point["position"],
167 "starting_point" => $point['starting_point'],
168 "starting_object" => $point['starting_object'],
169 "calendar_view" => $point['calendar_view'],
170 "calendar_period" => $point['calendar_period'],
171 "rule_type" => $point['rule_type'],
172 "rule_options" => $point['rule_options']
173 );
174 }
175
176 return $points;
177 }
178
179 public static function onRoleDeleted(ilObjRole $role): void
180 {
181 foreach (self::getRolesWithStartingPoint() as $roleId => $data) {
182 if ($roleId === $role->getId()) {
183 $sp = new self((int) $data['id']);
184 $sp->delete();
185 } elseif (
186 is_null($maybeDeletedRole = ilObjectFactory::getInstanceByObjId($roleId, false)) ||
187 !($maybeDeletedRole instanceof ilObjRole)
188 ) {
189 $sp = new self((int) $data['id']);
190 $sp->delete();
191 }
192 }
193 }
194
199 public static function getRolesWithStartingPoint(): array
200 {
201 global $DIC;
202
203 $ilDB = $DIC['ilDB'];
204 $query = "SELECT * FROM usr_starting_point WHERE rule_options LIKE %s";
205 $res = $ilDB->queryF($query, ['text'], ["%role_id%"]);
206
207 $roles = [];
208 while ($sp = $ilDB->fetchAssoc($res)) {
209 $options = unserialize($sp['rule_options']);
210
211 $roles[(int) $options['role_id']] = [
212 "id" => (int) $sp['id'],
213 "starting_point" => (int) $sp['starting_point'],
214 "starting_object" => (int) $sp['starting_object'],
215 "calendar_view" => (int) $sp['calendar_view'],
216 "calendar_period" => (int) $sp['calendar_period'],
217 "position" => (int) $sp['position'],
218 "role_id" => (int) $options['role_id'],
219
220 ];
221 }
222 return $roles;
223 }
224
228 public static function getGlobalRolesWithoutStartingPoint(): array
229 {
230 global $DIC;
231
232 $rbacreview = $DIC['rbacreview'];
233 $global_roles = $rbacreview->getGlobalRoles();
234 $roles_with_starting_point = self::getRolesWithStartingPoint();
235
236 $ids_roles_with_sp = array();
237 foreach ($roles_with_starting_point as $role) {
238 $ids_roles_with_sp[] = $role['role_id'];
239 }
240
241 $ids_roles_without_sp = array_diff($global_roles, $ids_roles_with_sp);
242
243 $roles = array();
244 foreach ($ids_roles_without_sp as $roleid) {
245 if ($roleid === ANONYMOUS_ROLE_ID) {
246 continue;
247 }
248 $role_obj = new ilObjRole($roleid);
249 $roles[] = array(
250 "id" => $role_obj->getId(),
251 "title" => $role_obj->getTitle(),
252 );
253 }
254
255 return $roles;
256 }
257
261 public function save(): void
262 {
263 //get position
264 $max_position = $this->getMaxPosition();
265 $position = $max_position + 10;
266
267 $next_id = $this->db->nextId('usr_starting_point');
268 $values = array(
269 $next_id,
270 $this->getStartingPoint(),
271 $this->getStartingObject(),
272 $position,
273 $this->getRuleType(),
274 $this->getRuleOptions(),
275 $this->getCalendarView(),
276 $this->getCalendarPeriod()
277 );
278
279 $this->db->manipulateF(
280 "INSERT INTO usr_starting_point (id, starting_point, starting_object, position, rule_type, rule_options, calendar_view, calendar_period) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
289 ),
290 $values
291 );
292 }
293
297 public function update(): void
298 {
299 $this->db->manipulateF(
300 'UPDATE usr_starting_point
301 SET starting_point = %s,
302 starting_object = %s,
303 position = %s,
304 rule_type = %s,
305 rule_options = %s,
306 calendar_view = %s,
307 calendar_period = %s
308 WHERE id = %s',
317 ),
318 array($this->getStartingPoint(),
319 $this->getStartingObject(),
320 $this->getPosition(),
321 $this->getRuleType(),
322 $this->getRuleOptions(),
323 $this->getCalendarView(),
324 $this->getCalendarPeriod(),
325 $this->id
326 )
327 );
328 }
329
333 public function delete(): void
334 {
335 $query = "DELETE FROM usr_starting_point WHERE id = " . $this->db->quote($this->id, "integer");
336 $this->db->manipulate($query);
337 }
338
339 public function getMaxPosition(): int
340 {
341 //get max order number
342 $result = $this->db->query("SELECT max(position) as max_order FROM usr_starting_point");
343
344 $order_val = 0;
345 while ($row = $this->db->fetchAssoc($result)) {
346 $order_val = (int) $row['max_order'];
347 }
348 return $order_val;
349 }
350
351 public static function reArrangePositions(array $a_items): array
352 {
353 $ord_const = 0;
354 $rearranged = [];
355 foreach ($a_items as $v) {
356 $v['starting_position'] = $ord_const;
357 $rearranged[$ord_const] = $v;
358 $ord_const += 10;
359 }
360 return $rearranged;
361 }
362
366 public function saveOrder(array $a_items): void
367 {
368 asort($a_items);
369 $nr = 10;
370 foreach ($a_items as $id => $position) {
371 if ($position > self::ORDER_POSITION_MIN && $position < self::ORDER_POSITION_MAX) {
372 $this->db->manipulate(
373 "UPDATE usr_starting_point SET" .
374 " position = " . $this->db->quote($nr, 'integer') .
375 " WHERE id = " . $this->db->quote($id, 'integer')
376 );
377 $nr += 10;
378 }
379 }
380 }
381}
Class ilObjRole.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilStartingPoint.
getCalendarView()
Gets calendar view.
save()
insert starting point into database
setRuleOptions(string $a_rule_options)
serialized string
saveOrder(array $a_items)
Save all starting point positions.
static getRolesWithStartingPoint()
get array with all roles which have starting point defined.
update()
update starting point
static getStartingPoints()
Get all the starting points in database.
static reArrangePositions(array $a_items)
setRuleType(int $a_rule_type)
setStartingObject(int $a_starting_object)
setCalendarPeriod(int $calendar_period)
setStartingPoint(int $a_starting_point)
static onRoleDeleted(ilObjRole $role)
setCalendarView(int $calendar_view)
Sets calendar view.
setPosition(int $a_starting_position)
static getGlobalRolesWithoutStartingPoint()
Get id and title of the roles without starting points.
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query