ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Consumer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilDBConstants;
24 use ilDBInterface;
28 use ilObjUser;
31 use Closure;
33 
34 class Consumer implements ConsumerInterface
35 {
36  private readonly Container $container;
37  private readonly Closure $lazy_users;
38 
39  public const ID = 'tos';
40  public const GOTO_NAME = 'agreement';
41 
42  public function __construct(?Container $container = null)
43  {
44  $this->container = $container ?? $GLOBALS['DIC'];
45  }
46 
47  public function id(): string
48  {
49  return self::ID;
50  }
51 
52  public function uses(UseSlot $slot, LazyProvide $provide): UseSlot
53  {
54  $blocks = new Blocks($this->id(), $this->container, $provide);
55  $default = $blocks->defaultMappings();
56  $global_settings = new Settings($blocks->selectSettingsFrom($blocks->readOnlyStore($blocks->globalStore())));
57  $is_active = $global_settings->enabled()->value();
58  $build_user = fn(ilObjUser $user) => $blocks->user($global_settings, new UserSettings($user, $blocks->selectSettingsFrom(
59  $blocks->userStore($user)
60  ), $this->container->refinery()), $user);
61  $public_api = new PublicApi($is_active, $build_user);
62  $slot = $slot->hasDocuments($default->contentAsComponent(), $default->conditionDefinitions())
63  ->hasHistory()
64  ->hasPublicApi($public_api);
65 
66  if (!$is_active) {
67  return $slot->hasPublicPage($blocks->notAvailable(...), self::GOTO_NAME);
68  }
69 
70  $user = $build_user($this->container->user());
71  $constraint = $this->container->refinery()->custom()->constraint(...);
72 
73  return $slot->canWithdraw($blocks->slot()->withdrawProcess($user, $global_settings, $this->userHasWithdrawn(...)))
74  ->hasAgreement($blocks->slot()->agreement($user, $global_settings), self::GOTO_NAME)
75  ->showInFooter($blocks->slot()->modifyFooter($user, self::GOTO_NAME))
76  ->showOnLoginPage($blocks->slot()->showOnLoginPage())
77  ->onSelfRegistration($blocks->slot()->selfRegistration($user, $build_user))
78  ->hasOnlineStatusFilter($blocks->slot()->onlineStatusFilter($this->usersWhoDidntAgree($this->container->database())))
79  ->hasUserManagementFields($blocks->userManagementAgreeDateField($build_user, 'tos_agree_date', 'tos'))
80  ->canReadInternalMails($blocks->slot()->canReadInternalMails($build_user))
81  ->canUseSoapApi($constraint(fn($u) => !$public_api->needsToAgree($u), 'TOS not accepted.'));
82  }
83 
84  private function userHasWithdrawn(): void
85  {
86  $this->container['ilAppEventHandler']->raise(
87  'Services/TermsOfService',
88  'withdraw',
89  ['event' => $this->container->user()]
90  );
91  }
92 
93  private function usersWhoDidntAgree(ilDBInterface $database): Closure
94  {
95  return function (array $users) use ($database): array {
96  $users = $database->in('usr_id', $users, false, ilDBConstants::T_INTEGER);
97  $result = $database->query('SELECT usr_id FROM usr_data WHERE agree_date IS NULL AND ' . $users);
98 
99  return array_map(intval(...), array_column($database->fetchAll($result), 'usr_id'));
100  };
101  }
102 }
uses(UseSlot $slot, LazyProvide $provide)
A
Definition: Consumer.php:52
fetchAll(ilDBStatement $statement, int $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
canWithdraw(WithdrawProcess $withdraw_process)
$GLOBALS["DIC"]
Definition: wac.php:31
readonly Container $container
Definition: Consumer.php:36
readonly Closure $lazy_users
Definition: Consumer.php:37
usersWhoDidntAgree(ilDBInterface $database)
Definition: Consumer.php:93
query(string $query)
Run a (read-only) Query on the database.
in(string $field, array $values, bool $negate=false, string $type="")
__construct(?Container $container=null)
Definition: Consumer.php:42
hasPublicPage(callable $public_page, ?string $goto_name=null)
hasDocuments(array $content_as_component=[], ?SelectionMap $available_conditions=null)