ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Repository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\User\Settings\Repository as UserSettingsRepository;
27
29{
30 private const START_PD_OVERVIEW = 1;
31 private const START_PD_SUBSCRIPTION = 2;
32 private const START_PD_NOTES = 4;
33 private const START_PD_NEWS = 5;
34 private const START_PD_WORKSPACE = 6;
35 private const START_PD_PORTFOLIO = 7;
36 private const START_PD_SKILLS = 8;
37 private const START_PD_LP = 9;
38 public const START_PD_CALENDAR = 10;
39 private const START_PD_MAIL = 11;
40 private const START_PD_CONTACTS = 12;
41 private const START_PD_PROFILE = 13;
42 private const START_PD_SETTINGS = 14;
43 private const START_REPOSITORY = 15;
44 public const START_REPOSITORY_OBJ = 16;
45 private const START_PD_MYSTAFF = 17;
46
47 private const ORDER_POSITION_MIN = 0;
48 private const ORDER_POSITION_MAX = 9999;
49
50 private const USER_STARTING_POINT_ID = -1;
51 private const DEFAULT_STARTING_POINT_ID = 0;
52
53 private const URL_LINKS_BY_TYPE = [
54 self::START_PD_OVERVIEW => 'ilias.php?baseClass=ilDashboardGUI&cmd=jumpToSelectedItems',
55 self::START_PD_SUBSCRIPTION => 'ilias.php?baseClass=ilMembershipOverviewGUI',
56 self::START_PD_WORKSPACE => 'ilias.php?baseClass=ilDashboardGUI&cmd=jumpToWorkspace',
57 self::START_PD_CALENDAR => 'ilias.php?baseClass=ilDashboardGUI&cmd=jumpToCalendar',
58 self::START_PD_MYSTAFF => 'ilias.php?baseClass=' . \ilDashboardGUI::class . '&cmd=' . \ilDashboardGUI::CMD_JUMP_TO_MY_STAFF
59 ];
60
62
63 public function __construct(
64 private readonly \ilObjUser $user,
65 private readonly \ilDBInterface $db,
66 private readonly LoggingServices $log,
67 private readonly \ilTree $tree,
68 private readonly \ilRbacReview $rbac_review,
69 private readonly \ilRbacSystem $rbac_system,
70 private readonly \ilSetting $settings,
71 private readonly UserSettingsRepository $user_settings_repository
72 ) {
73 global $DIC;
74 $this->current_user_has_access_to_my_staff = (new ilMyStaffCachedAccessDecorator(
75 $DIC,
77 ))->hasCurrentUserAccessToMyStaff();
78 }
79
80 public function getStartingPointById(?int $id): StartingPoint
81 {
82 if ($id === null) {
83 return new StartingPoint($id);
84 }
85
86 $query = 'SELECT * FROM usr_starting_point WHERE id = ' . $this->db->quote($id, 'integer');
87 $res = $this->db->query($query);
88 $starting_point_array = $this->db->fetchAssoc($res);
89
90 if ($starting_point_array === null) {
91 $default_starting_point = new StartingPoint(self::DEFAULT_STARTING_POINT_ID);
92 $default_starting_point->setStartingPointType($this->getSystemDefaultStartingPointType());
93 $default_starting_point->setStartingObject($this->getSystemDefaultStartingObject());
94 $default_starting_point->setCalendarView($this->getSystemDefaultCalendarView());
95 $default_starting_point->setCalendarPeriod($this->getSystemDefaultCalendarPeriod());
96 return $default_starting_point;
97 }
98
99 return new StartingPoint(
100 $id,
101 $starting_point_array['starting_point'],
102 $starting_point_array['starting_object'],
103 $starting_point_array['position'],
104 $starting_point_array['rule_type'],
105 $starting_point_array['rule_options'],
106 $starting_point_array['calendar_view'],
107 $starting_point_array['calendar_period']
108 );
109 }
110
114 public function getStartingPoints(): array
115 {
116 $query = 'SELECT * FROM usr_starting_point';
117 $res = $this->db->query($query);
118 $starting_points = [];
119 while ($starting_point_array = $this->db->fetchAssoc($res)) {
120 $starting_point = new StartingPoint(
121 $starting_point_array['id'],
122 $starting_point_array['starting_point'],
123 $starting_point_array['starting_object'],
124 $starting_point_array['position'],
125 $starting_point_array['rule_type'],
126 $starting_point_array['rule_options'],
127 $starting_point_array['calendar_view'],
128 $starting_point_array['calendar_period']
129 );
130
131 $starting_points[] = $starting_point;
132 }
133
134 return $starting_points;
135 }
136
138 {
140 }
141
142 public function getUserStartingPointID(): int
143 {
145 }
146
147 public function onRoleDeleted(\ilObjRole $role): void
148 {
149 foreach ($this->getRolesWithStartingPoint() as $role_id => $data) {
150 if ($role_id === $role->getId()
151 || ($maybe_deleted_role = \ilObjectFactory::getInstanceByObjId($role_id, false)) === null
152 || !($maybe_deleted_role instanceof \ilObjRole)
153 ) {
154 $this->delete($data['id']);
155 }
156 }
157 }
158
162 private function getRolesWithStartingPoint(): array
163 {
164 $query = 'SELECT * FROM usr_starting_point WHERE rule_options LIKE %s ORDER BY position ASC';
165 $res = $this->db->queryF($query, ['text'], ['%role_id%']);
166
167 $roles = [];
168 while ($sp = $this->db->fetchAssoc($res)) {
169 $options = unserialize($sp['rule_options']);
170
171 $roles[(int) $options['role_id']] = [
172 'id' => (int) $sp['id'],
173 'starting_point' => (int) $sp['starting_point'],
174 'starting_object' => (int) $sp['starting_object'],
175 'calendar_view' => (int) $sp['calendar_view'],
176 'calendar_period' => (int) $sp['calendar_period'],
177 'position' => (int) $sp['position'],
178 'role_id' => (int) $options['role_id'],
179
180 ];
181 }
182 return $roles;
183 }
184
185 public function getGlobalRolesWithoutStartingPoint(): array
186 {
187 $global_roles = $this->rbac_review->getGlobalRoles();
188 $roles_with_starting_point = $this->getRolesWithStartingPoint();
189
190 $ids_roles_with_sp = [];
191 foreach ($roles_with_starting_point as $role) {
192 $ids_roles_with_sp[] = $role['role_id'];
193 }
194
195 $ids_roles_without_sp = array_diff($global_roles, $ids_roles_with_sp);
196
197 $roles = [];
198 foreach ($ids_roles_without_sp as $roleid) {
199 if ($roleid === ANONYMOUS_ROLE_ID) {
200 continue;
201 }
202 $role_obj = new \ilObjRole($roleid);
203 $roles[] = [
204 'id' => $role_obj->getId(),
205 'title' => $role_obj->getTitle(),
206 ];
207 }
208
209 return $roles;
210 }
211
212 public function save(StartingPoint $starting_point): void
213 {
214 if ($starting_point->getId() === $this->getDefaultStartingPointID()) {
215 $this->setSystemDefaultStartingPoint($starting_point);
216 return;
217 }
218 //get position
219 $max_position = $this->getMaxPosition();
220 $position = $max_position + 10;
221
222 $next_id = $this->db->nextId('usr_starting_point');
223 $values = [
224 $next_id,
225 $starting_point->getStartingPointType(),
226 $starting_point->getStartingObject(),
227 $position,
228 $starting_point->getRuleType(),
229 $starting_point->getRuleOptions(),
230 $starting_point->getCalendarView(),
231 $starting_point->getCalendarPeriod()
232 ];
233
234 $this->db->manipulateF(
235 '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)',
236 [
245 ],
246 $values
247 );
248 }
249
253 public function update(StartingPoint $starting_point): void
254 {
255 if ($starting_point->getId() === $this->getDefaultStartingPointID()) {
256 $this->setSystemDefaultStartingPoint($starting_point);
257 return;
258 }
259
260 $this->db->manipulateF(
261 'UPDATE usr_starting_point
262 SET starting_point = %s,
263 starting_object = %s,
264 position = %s,
265 rule_type = %s,
266 rule_options = %s,
267 calendar_view = %s,
268 calendar_period = %s
269 WHERE id = %s',
270 [
279 ],
280 [
281 $starting_point->getStartingPointType(),
282 $starting_point->getStartingObject(),
283 $starting_point->getPosition(),
284 $starting_point->getRuleType(),
285 $starting_point->getRuleOptions(),
286 $starting_point->getCalendarView(),
287 $starting_point->getCalendarPeriod(),
288 $starting_point->getId()
289 ]
290 );
291 }
292
293 public function delete(int $starting_point_id): void
294 {
295 $query = 'DELETE FROM usr_starting_point WHERE id = ' . $this->db->quote($starting_point_id, 'integer');
296 $this->db->manipulate($query);
297 }
298
299 public function getMaxPosition(): int
300 {
301 //get max order number
302 $result = $this->db->query('SELECT max(position) as max_order FROM usr_starting_point');
303
304 $order_val = 0;
305 while ($row = $this->db->fetchAssoc($result)) {
306 $order_val = (int) $row['max_order'];
307 }
308 return $order_val;
309 }
310
311 public function reArrangePositions(array $a_items): array
312 {
313 $ord_const = 0;
314 $rearranged = [];
315 foreach ($a_items as $v) {
316 $v['starting_position'] = $ord_const;
317 $rearranged[$ord_const] = $v;
318 $ord_const += 10;
319 }
320 return $rearranged;
321 }
322
323 public function saveOrder(array $a_items): void
324 {
325 asort($a_items);
326 $nr = 10;
327 foreach ($a_items as $id => $position) {
328 if ($position > self::ORDER_POSITION_MIN && $position < self::ORDER_POSITION_MAX) {
329 $this->db->manipulate(
330 'UPDATE usr_starting_point SET' .
331 ' position = ' . $this->db->quote($nr, 'integer') .
332 ' WHERE id = ' . $this->db->quote($id, 'integer')
333 );
334 $nr += 10;
335 }
336 }
337 }
338
339 public function getPossibleStartingPoints(bool $force_all = false): array //checked
340 {
341 $all = [];
342
343 $all[self::START_PD_OVERVIEW] = 'mm_dashboard';
344 $all[self::START_PD_SUBSCRIPTION] = 'my_courses_groups';
345
346 if ($this->current_user_has_access_to_my_staff) {
347 $all[self::START_PD_MYSTAFF] = 'my_staff';
348 }
349
350 if ($force_all || !$this->settings->get('disable_personal_workspace')) {
351 $all[self::START_PD_WORKSPACE] = 'mm_personal_and_shared_r';
352 }
353 $calendar_settings = \ilCalendarSettings::_getInstance();
354 if ($force_all || $calendar_settings->isEnabled()) {
355 $all[self::START_PD_CALENDAR] = 'calendar';
356 }
357
358 $all[self::START_REPOSITORY] = 'obj_root';
359 $all[self::START_REPOSITORY_OBJ] = 'adm_user_starting_point_object';
360
361 return $all;
362 }
363
365 {
366 $valid = array_keys($this->getPossibleStartingPoints());
367 $current = (int) $this->settings->get('usr_starting_point');
368 if (!$current || !in_array($current, $valid)) {
370 $this->setSystemDefaultStartingPoint($current);
371 }
372 if ($this->user->getId() === ANONYMOUS_USER_ID
373 || !$this->user->getId()) {
374 $current = self::START_REPOSITORY;
375 }
376 return $current;
377 }
378
380 {
381 if ($this->settings->get('disable_my_offers') === '0' &&
382 $this->settings->get('disable_my_memberships') === '0' &&
383 $this->settings->get('personal_items_default_view') === '1') {
385 }
386
388 }
389
391 StartingPoint $starting_point
392 ): void {
393 $starting_point_type = $starting_point->getStartingPointType();
394
395 $valid_starting_points = array_keys($this->getPossibleStartingPoints());
396 if (in_array($starting_point_type, $valid_starting_points)) {
397 $this->settings->set('usr_starting_point', (string) $starting_point_type);
398 }
399
400 if ($starting_point->getStartingPointType() === self::START_REPOSITORY_OBJ) {
401 $this->settings->set('usr_starting_point_ref_id', (string) $starting_point->getStartingObject());
402 }
403
404 if ($starting_point->getStartingPointType() === self::START_PD_CALENDAR) {
405 $this->settings->set('user_calendar_view', (string) $starting_point->getCalendarView());
406 $this->settings->set('user_calendar_period', (string) $starting_point->getCalendarPeriod());
407 }
408 }
409
411 {
412 $starting_point = $this->getApplicableStartingPointTypeInfo($this->user);
413
414 if ($starting_point['type'] === self::START_REPOSITORY_OBJ
415 && (
416 $starting_point['object'] === null
417 || !\ilObject::_exists($starting_point['object'], true)
418 || $this->tree->isDeleted($starting_point['object'])
419 || !$this->rbac_system->checkAccessOfUser(
420 $this->user->getId(),
421 'read',
422 $starting_point['object']
423 )
424 )
425 ) {
426 $this->log->user()->debug(sprintf('Permission to Starting Point Denied. Starting Point Type: %s.', $starting_point['type']));
427 $starting_point['type'] = self::START_REPOSITORY;
428 }
429
430 if ($starting_point['type'] === self::START_REPOSITORY
431 && !$this->rbac_system->checkAccessOfUser(
432 $this->user->getId(),
433 'read',
434 $this->tree->getRootId()
435 )
436 || $starting_point['type'] === self::START_PD_CALENDAR
437 && !\ilCalendarSettings::_getInstance()->isEnabled()
438 ) {
439 $this->log->user()->debug(sprintf('Permission to Starting Point Denied. Starting Point Type: %s.', $starting_point['type']));
440 $starting_point['type'] = $this->getFallbackStartingPointType();
441 }
442
443 if ($starting_point['type'] === self::START_REPOSITORY) {
444 $starting_point['object'] = $this->tree->getRootId();
445 }
446
447 return $this->getLinkUrlByStartingPointTypeInfo($starting_point);
448 }
449
451 \ilObjUser $user
452 ): array {
453 if ($this->isPersonalStartingPointEnabled()
454 && $this->getPersonalStartingPointForUser($user) !== 0) {
455 return [
456 'type' => $this->getPersonalStartingPointForUser($user),
457 'object' => $this->getPersonalStartingObjectForUser($user)
458 ];
459 }
460
461 $role = $this->getFirstRoleWithStartingPointForUserId($this->user->getId());
462 if ($role !== []) {
463 return [
464 'type' => $role['starting_point'],
465 'object' => $role['starting_object'],
466 'cal_view' => $role['calendar_view'],
467 'cal_period' => $role['calendar_period']
468 ];
469 }
470
471 return [
472 'type' => $this->getSystemDefaultStartingPointType(),
473 'object' => $this->getSystemDefaultStartingObject(),
474 'cal_view' => $this->getSystemDefaultCalendarView(),
475 'cal_period' => $this->getSystemDefaultCalendarPeriod()
476 ];
477 }
478
480 {
481 $roles = $this->getRolesWithStartingPoint();
482 $role_ids = array_keys($roles);
483 foreach ($role_ids as $role_id) {
484 if ($this->rbac_review->isAssigned($user_id, $role_id)) {
485 return $roles[$role_id];
486 }
487 }
488 return [];
489 }
490
491 private function getLinkUrlByStartingPointTypeInfo(array $starting_point): string
492 {
493 $type = $starting_point['type'];
494 if ($type === self::START_REPOSITORY
495 || $type === self::START_REPOSITORY_OBJ) {
496 return \ilLink::_getStaticLink($starting_point['object'], '', true);
497 }
498
499 $url = self::URL_LINKS_BY_TYPE[$type];
500 if ($type == self::START_PD_CALENDAR) {
501 $cal_view = $starting_point['cal_view'] ?? '';
502 $cal_period = $starting_point['cal_period'] ?? '';
503 $calendar_string = '';
504 if (!empty($cal_view) && !empty($cal_period)) {
505 $calendar_string = '&cal_view=' . $cal_view . '&cal_agenda_per=' . $cal_period;
506 }
507 $url .= $calendar_string;
508 }
509
510 return $url;
511 }
512
514 {
515 return (int) $this->settings->get('usr_starting_point_ref_id');
516 }
517
522 {
523 return (int) $this->settings->get('user_calendar_view');
524 }
525
530 {
531 return (int) $this->settings->get('user_cal_period');
532 }
533
537 public function togglePersonalStartingPointActivation(bool $value): void
538 {
539 $this->settings->set('usr_starting_point_personal', $value ? '1' : '0');
540 }
541
542 public function isPersonalStartingPointEnabled(): bool //checked
543 {
544 return $this->user_settings_repository->getByIdentifier('starting_point')->isChangeableByUser();
545 }
546
548 \ilObjUser $user
549 ): bool {
550 return !empty($user->getPref('usr_starting_point'));
551 }
552
554 \ilObjUser $user
555 ): int {
556 $current = $user->getPref('usr_starting_point');
557 if ($current !== null
558 && in_array((int) $current, array_keys($this->getPossibleStartingPoints()))) {
559 return (int) $current;
560 }
561
562 return 0;
563 }
564
569 \ilObjUser $user,
570 int $starting_point_type,
571 ?int $ref_id = null
572 ): bool {
573 if ($starting_point_type === 0) {
574 $user->deletePref('usr_starting_point');
575 $user->deletePref('usr_starting_point_ref_id');
576 return false;
577 }
578
579 if ($starting_point_type === self::START_REPOSITORY_OBJ) {
581 !$this->tree->isDeleted($ref_id)) {
582 $user->setPref('usr_starting_point', (string) $starting_point_type);
583 $user->setPref('usr_starting_point_ref_id', (string) $ref_id);
584 return true;
585 }
586 }
587 $valid = array_keys($this->getPossibleStartingPoints());
588 if (in_array($starting_point_type, $valid)) {
589 $user->setPref('usr_starting_point', (string) $starting_point_type);
590 return true;
591 }
592 return false;
593 }
594
596 \ilObjUser $user
597 ): ?int {
598 $personal_starting_object = $user->getPref('usr_starting_point_ref_id');
599 if ($personal_starting_object !== null) {
600 return (int) $personal_starting_object;
601 }
602
603 return null;
604 }
605}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Provides fluid interface to LoggingServices.
save(StartingPoint $starting_point)
Definition: Repository.php:212
getLinkUrlByStartingPointTypeInfo(array $starting_point)
Definition: Repository.php:491
getPossibleStartingPoints(bool $force_all=false)
Definition: Repository.php:339
getSystemDefaultCalendarView()
Get specific view of calendar starting point.
Definition: Repository.php:521
__construct(private readonly \ilObjUser $user, private readonly \ilDBInterface $db, private readonly LoggingServices $log, private readonly \ilTree $tree, private readonly \ilRbacReview $rbac_review, private readonly \ilRbacSystem $rbac_system, private readonly \ilSetting $settings, private readonly UserSettingsRepository $user_settings_repository)
Definition: Repository.php:63
update(StartingPoint $starting_point)
update starting point
Definition: Repository.php:253
isPersonalStartingPointEnabledForUser(\ilObjUser $user)
Definition: Repository.php:547
setPersonalStartingPointForUser(\ilObjUser $user, int $starting_point_type, ?int $ref_id=null)
Set personal starting point setting.
Definition: Repository.php:568
getSystemDefaultCalendarPeriod()
Get time frame of calendar view.
Definition: Repository.php:529
setSystemDefaultStartingPoint(StartingPoint $starting_point)
Definition: Repository.php:390
togglePersonalStartingPointActivation(bool $value)
Toggle personal starting point setting.
Definition: Repository.php:537
Class ilObjRole.
User class.
setPref(string $a_keyword, ?string $a_value)
deletePref(string $keyword)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _lookupObjId(int $ref_id)
class ilRbacReview Contains Review functions of core Rbac.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
ILIAS Setting Class.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const ANONYMOUS_ROLE_ID
Definition: constants.php:28
const ANONYMOUS_USER_ID
Definition: constants.php:27
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$valid
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$log
Definition: ltiresult.php:34
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68