ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
Repository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\User\Settings\ConfigurationRepository as UserSettingsConfigurationRepository;
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 UserSettingsConfigurationRepository $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'], ['allowed_classes' => false]);
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
188 public function getGlobalRolesWithoutStartingPoint(): array
189 {
190 $global_roles = $this->rbac_review->getGlobalRoles();
191 $roles_with_starting_point = $this->getRolesWithStartingPoint();
192
193 $ids_roles_with_sp = [];
194 foreach ($roles_with_starting_point as $role) {
195 $ids_roles_with_sp[] = $role['role_id'];
196 }
197
198 $ids_roles_without_sp = array_diff($global_roles, $ids_roles_with_sp);
199
200 $roles = [];
201 foreach ($ids_roles_without_sp as $roleid) {
202 if ($roleid === ANONYMOUS_ROLE_ID) {
203 continue;
204 }
205 $role_obj = new \ilObjRole($roleid);
206 $roles[] = [
207 'id' => $role_obj->getId(),
208 'title' => $role_obj->getTitle(),
209 ];
210 }
211
212 return $roles;
213 }
214
215 public function save(StartingPoint $starting_point): void
216 {
217 if ($starting_point->getId() === $this->getDefaultStartingPointID()) {
218 $this->setSystemDefaultStartingPoint($starting_point);
219 return;
220 }
221 //get position
222 $max_position = $this->getMaxPosition();
223 $position = $max_position + 10;
224
225 $next_id = $this->db->nextId('usr_starting_point');
226 $values = [
227 $next_id,
228 $starting_point->getStartingPointType(),
229 $starting_point->getStartingObject(),
230 $position,
231 $starting_point->getRuleType(),
232 $starting_point->getRuleOptions(),
233 $starting_point->getCalendarView(),
234 $starting_point->getCalendarPeriod()
235 ];
236
237 $this->db->manipulateF(
238 '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)',
239 [
248 ],
249 $values
250 );
251 }
252
256 public function update(StartingPoint $starting_point): void
257 {
258 if ($starting_point->getId() === $this->getDefaultStartingPointID()) {
259 $this->setSystemDefaultStartingPoint($starting_point);
260 return;
261 }
262
263 $this->db->manipulateF(
264 'UPDATE usr_starting_point
265 SET starting_point = %s,
266 starting_object = %s,
267 position = %s,
268 rule_type = %s,
269 rule_options = %s,
270 calendar_view = %s,
271 calendar_period = %s
272 WHERE id = %s',
273 [
282 ],
283 [
284 $starting_point->getStartingPointType(),
285 $starting_point->getStartingObject(),
286 $starting_point->getPosition(),
287 $starting_point->getRuleType(),
288 $starting_point->getRuleOptions(),
289 $starting_point->getCalendarView(),
290 $starting_point->getCalendarPeriod(),
291 $starting_point->getId()
292 ]
293 );
294 }
295
296 public function delete(int $starting_point_id): void
297 {
298 $query = 'DELETE FROM usr_starting_point WHERE id = ' . $this->db->quote($starting_point_id, 'integer');
299 $this->db->manipulate($query);
300 }
301
302 public function getMaxPosition(): int
303 {
304 //get max order number
305 $result = $this->db->query('SELECT max(position) as max_order FROM usr_starting_point');
306
307 $order_val = 0;
308 while ($row = $this->db->fetchAssoc($result)) {
309 $order_val = (int) $row['max_order'];
310 }
311 return $order_val;
312 }
313
318 public function reArrangePositions(array $a_items): array
319 {
320 $ord_const = 0;
321 $rearranged = [];
322 foreach ($a_items as $v) {
323 $v['starting_position'] = $ord_const;
324 $rearranged[$ord_const] = $v;
325 $ord_const += 10;
326 }
327 return $rearranged;
328 }
329
330 public function saveOrder(array $a_items): void
331 {
332 asort($a_items);
333 $nr = 10;
334 foreach ($a_items as $id => $position) {
335 if ($position > self::ORDER_POSITION_MIN && $position < self::ORDER_POSITION_MAX) {
336 $this->db->manipulate(
337 'UPDATE usr_starting_point SET' .
338 ' position = ' . $this->db->quote($nr, 'integer') .
339 ' WHERE id = ' . $this->db->quote($id, 'integer')
340 );
341 $nr += 10;
342 }
343 }
344 }
345
349 public function getPossibleStartingPoints(bool $force_all = false): array
350 {
351 $all = [];
352
353 $all[self::START_PD_OVERVIEW] = 'mm_dashboard';
354 $all[self::START_PD_SUBSCRIPTION] = 'my_courses_groups';
355
356 if ($this->current_user_has_access_to_my_staff) {
357 $all[self::START_PD_MYSTAFF] = 'my_staff';
358 }
359
360 if ($force_all || !$this->settings->get('disable_personal_workspace')) {
361 $all[self::START_PD_WORKSPACE] = 'mm_personal_and_shared_r';
362 }
363 $calendar_settings = \ilCalendarSettings::_getInstance();
364 if ($force_all || $calendar_settings->isEnabled()) {
365 $all[self::START_PD_CALENDAR] = 'calendar';
366 }
367
368 $all[self::START_REPOSITORY] = 'obj_root';
369 $all[self::START_REPOSITORY_OBJ] = 'adm_user_starting_point_object';
370
371 return $all;
372 }
373
375 {
376 $valid = array_keys($this->getPossibleStartingPoints());
377 $current = (int) $this->settings->get('usr_starting_point');
378 if (!$current || !in_array($current, $valid)) {
380 $this->setSystemDefaultStartingPoint($current);
381 }
382 if ($this->user->getId() === ANONYMOUS_USER_ID
383 || !$this->user->getId()) {
384 $current = self::START_REPOSITORY;
385 }
386 return $current;
387 }
388
390 {
391 if ($this->settings->get('disable_my_offers') === '0' &&
392 $this->settings->get('disable_my_memberships') === '0' &&
393 $this->settings->get('personal_items_default_view') === '1') {
395 }
396
398 }
399
401 StartingPoint $starting_point
402 ): void {
403 $starting_point_type = $starting_point->getStartingPointType();
404
405 $valid_starting_points = array_keys($this->getPossibleStartingPoints());
406 if (in_array($starting_point_type, $valid_starting_points)) {
407 $this->settings->set('usr_starting_point', (string) $starting_point_type);
408 }
409
410 if ($starting_point->getStartingPointType() === self::START_REPOSITORY_OBJ) {
411 $this->settings->set('usr_starting_point_ref_id', (string) $starting_point->getStartingObject());
412 }
413
414 if ($starting_point->getStartingPointType() === self::START_PD_CALENDAR) {
415 $this->settings->set('user_calendar_view', (string) $starting_point->getCalendarView());
416 $this->settings->set('user_calendar_period', (string) $starting_point->getCalendarPeriod());
417 }
418 }
419
421 {
422 $starting_point = $this->getApplicableStartingPointTypeInfo($this->user);
423
424 if ($starting_point['type'] === self::START_REPOSITORY_OBJ
425 && (
426 $starting_point['object'] === null
427 || !\ilObject::_exists($starting_point['object'], true)
428 || $this->tree->isDeleted($starting_point['object'])
429 || !$this->rbac_system->checkAccessOfUser(
430 $this->user->getId(),
431 'read',
432 $starting_point['object']
433 )
434 )
435 ) {
436 $this->log->user()->debug(sprintf('Permission to Starting Point Denied. Starting Point Type: %s.', $starting_point['type']));
437 $starting_point['type'] = self::START_REPOSITORY;
438 }
439
440 if ($starting_point['type'] === self::START_REPOSITORY
441 && !$this->rbac_system->checkAccessOfUser(
442 $this->user->getId(),
443 'read',
444 $this->tree->getRootId()
445 )
446 || $starting_point['type'] === self::START_PD_CALENDAR
447 && !\ilCalendarSettings::_getInstance()->isEnabled()
448 ) {
449 $this->log->user()->debug(sprintf('Permission to Starting Point Denied. Starting Point Type: %s.', $starting_point['type']));
450 $starting_point['type'] = $this->getFallbackStartingPointType();
451 }
452
453 if ($starting_point['type'] === self::START_REPOSITORY) {
454 $starting_point['object'] = $this->tree->getRootId();
455 }
456
457 return $this->getLinkUrlByStartingPointTypeInfo($starting_point);
458 }
459
461 \ilObjUser $user
462 ): array {
463 if ($this->isPersonalStartingPointEnabled()
464 && $this->getPersonalStartingPointForUser($user) !== 0) {
465 return [
466 'type' => $this->getPersonalStartingPointForUser($user),
467 'object' => $this->getPersonalStartingObjectForUser($user)
468 ];
469 }
470
471 $role = $this->getFirstRoleWithStartingPointForUserId($this->user->getId());
472 if ($role !== []) {
473 return [
474 'type' => $role['starting_point'],
475 'object' => $role['starting_object'],
476 'cal_view' => $role['calendar_view'],
477 'cal_period' => $role['calendar_period']
478 ];
479 }
480
481 return [
482 'type' => $this->getSystemDefaultStartingPointType(),
483 'object' => $this->getSystemDefaultStartingObject(),
484 'cal_view' => $this->getSystemDefaultCalendarView(),
485 'cal_period' => $this->getSystemDefaultCalendarPeriod()
486 ];
487 }
488
490 {
491 $roles = $this->getRolesWithStartingPoint();
492 $role_ids = array_keys($roles);
493 foreach ($role_ids as $role_id) {
494 if ($this->rbac_review->isAssigned($user_id, $role_id)) {
495 return $roles[$role_id];
496 }
497 }
498 return [];
499 }
500
501 private function getLinkUrlByStartingPointTypeInfo(array $starting_point): string
502 {
503 $type = $starting_point['type'];
504 if ($type === self::START_REPOSITORY
505 || $type === self::START_REPOSITORY_OBJ) {
506 return \ilLink::_getStaticLink($starting_point['object'], '', true);
507 }
508
509 $url = self::URL_LINKS_BY_TYPE[$type];
510 if ($type == self::START_PD_CALENDAR) {
511 $cal_view = $starting_point['cal_view'] ?? '';
512 $cal_period = $starting_point['cal_period'] ?? '';
513 $calendar_string = '';
514 if (!empty($cal_view) && !empty($cal_period)) {
515 $calendar_string = '&cal_view=' . $cal_view . '&cal_agenda_per=' . $cal_period;
516 }
517 $url .= $calendar_string;
518 }
519
520 return $url;
521 }
522
524 {
525 return (int) $this->settings->get('usr_starting_point_ref_id');
526 }
527
532 {
533 return (int) $this->settings->get('user_calendar_view');
534 }
535
540 {
541 return (int) $this->settings->get('user_calendar_period');
542 }
543
547 public function togglePersonalStartingPointActivation(bool $value): void
548 {
549 $this->settings->set('usr_starting_point_personal', $value ? '1' : '0');
550 }
551
552 public function isPersonalStartingPointEnabled(): bool //checked
553 {
554 return $this->user_settings_repository->getByIdentifier('starting_point')->isChangeableByUser();
555 }
556
558 \ilObjUser $user
559 ): bool {
560 return !empty($user->getPref('usr_starting_point'));
561 }
562
564 \ilObjUser $user
565 ): int {
566 $current = $user->getPref('usr_starting_point');
567 if ($current !== null
568 && in_array((int) $current, array_keys($this->getPossibleStartingPoints()))) {
569 return (int) $current;
570 }
571
572 return 0;
573 }
574
579 \ilObjUser $user,
580 int $starting_point_type,
581 ?int $ref_id = null
582 ): bool {
583 if ($starting_point_type === 0) {
584 $user->deletePref('usr_starting_point');
585 $user->deletePref('usr_starting_point_ref_id');
586 return false;
587 }
588
589 if ($starting_point_type === self::START_REPOSITORY_OBJ) {
591 !$this->tree->isDeleted($ref_id)) {
592 $user->setPref('usr_starting_point', (string) $starting_point_type);
593 $user->setPref('usr_starting_point_ref_id', (string) $ref_id);
594 return true;
595 }
596 }
597 $valid = array_keys($this->getPossibleStartingPoints());
598 if (in_array($starting_point_type, $valid)) {
599 $user->setPref('usr_starting_point', (string) $starting_point_type);
600 return true;
601 }
602 return false;
603 }
604
606 \ilObjUser $user
607 ): ?int {
608 $personal_starting_object = $user->getPref('usr_starting_point_ref_id');
609 if ($personal_starting_object !== null) {
610 return (int) $personal_starting_object;
611 }
612
613 return null;
614 }
615}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Provides fluid interface to LoggingServices.
save(StartingPoint $starting_point)
Definition: Repository.php:215
getLinkUrlByStartingPointTypeInfo(array $starting_point)
Definition: Repository.php:501
getPossibleStartingPoints(bool $force_all=false)
Definition: Repository.php:349
getSystemDefaultCalendarView()
Get specific view of calendar starting point.
Definition: Repository.php:531
update(StartingPoint $starting_point)
update starting point
Definition: Repository.php:256
isPersonalStartingPointEnabledForUser(\ilObjUser $user)
Definition: Repository.php:557
__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 UserSettingsConfigurationRepository $user_settings_repository)
Definition: Repository.php:63
setPersonalStartingPointForUser(\ilObjUser $user, int $starting_point_type, ?int $ref_id=null)
Set personal starting point setting.
Definition: Repository.php:578
getSystemDefaultCalendarPeriod()
Get time frame of calendar view.
Definition: Repository.php:539
setSystemDefaultStartingPoint(StartingPoint $starting_point)
Definition: Repository.php:400
togglePersonalStartingPointActivation(bool $value)
Toggle personal starting point setting.
Definition: Repository.php:547
Class ilObjRole.
User class.
setPref(string $a_keyword, ?string $a_value)
deletePref(string $key)
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:70