ILIAS  release_8 Revision v8.24
class.ilTermsOfServiceHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26{
31
32 public function __construct(
37 ) {
38 global $DIC;
39
40 if (null === $dataGatewayFactory) {
43 }
44 $this->dataGatewayFactory = $dataGatewayFactory;
45
46 if (null === $termsOfServiceEvaluation) {
47 $termsOfServiceEvaluation = $DIC['tos.document.evaluator'];
48 }
49 $this->termsOfServiceEvaluation = $termsOfServiceEvaluation;
50
51 if (null === $criterionTypeFactory) {
52 $criterionTypeFactory = $DIC['tos.criteria.type.factory'];
53 }
54 $this->criterionTypeFactory = $criterionTypeFactory;
55
56 if (null === $tos) {
58 }
59 $this->tos = $tos;
60 }
61
62 public static function isEnabled(): bool
63 {
64 return (new self())->tos->getStatus();
65 }
66
67 public function isGloballyEnabled(): bool
68 {
69 return $this->tos->getStatus();
70 }
71
76 public function deleteAcceptanceHistoryByUser(int $userId): void
77 {
78 $entity = $this->getEntityFactory()->getByName('ilTermsOfServiceAcceptanceEntity');
79 $databaseGateway = $this->getDataGatewayFactory()->getByName('ilTermsOfServiceAcceptanceDatabaseGateway');
80
81 $databaseGateway->deleteAcceptanceHistoryByUser($entity->withUserId($userId));
82 }
83
90 {
91 $entity = $this->getEntityFactory()->getByName('ilTermsOfServiceAcceptanceEntity');
92 $databaseGateway = $this->getDataGatewayFactory()->getByName('ilTermsOfServiceAcceptanceDatabaseGateway');
93
94 return $databaseGateway->loadCurrentAcceptanceOfUser($entity->withUserId($user->getId()));
95 }
96
103 {
104 $entity = $this->getEntityFactory()->getByName('ilTermsOfServiceAcceptanceEntity');
105 $databaseGateway = $this->getDataGatewayFactory()->getByName('ilTermsOfServiceAcceptanceDatabaseGateway');
106
107 return $databaseGateway->loadById($entity->withId($id));
108 }
109
116 public function trackAcceptance(ilObjUser $user, ilTermsOfServiceSignableDocument $document): void
117 {
118 $entity = $this->getEntityFactory()->getByName('ilTermsOfServiceAcceptanceEntity');
119 $databaseGateway = $this->getDataGatewayFactory()->getByName('ilTermsOfServiceAcceptanceDatabaseGateway');
120
121 $entity = $entity
122 ->withUserId($user->getId())
123 ->withTimestamp(time())
124 ->withText($document->content())
125 ->withHash(md5($document->content()))
126 ->withDocumentId($document->id())
127 ->withTitle($document->title());
128
129 $criteriaBag = new ilTermsOfServiceAcceptanceHistoryCriteriaBag($document->criteria());
130 $entity = $entity->withSerializedCriteria($criteriaBag->toJson());
131
132 $databaseGateway->trackAcceptance($entity);
133
134 $user->writeAccepted();
135
137 }
138
139 public function resetAcceptance(ilObjUser $user): void
140 {
141 $user->setAgreeDate(null);
142 $user->update();
143 }
144
145 public function isIncludedUser(ilObjUser $user): bool
146 {
147 $excluded_roles = [];
148 if (defined('ANONYMOUS_USER_ID')) {
149 $excluded_roles[] = ANONYMOUS_USER_ID;
150 }
151 if (defined('SYSTEM_USER_ID')) {
152 $excluded_roles[] = SYSTEM_USER_ID;
153 }
154
155 return (
156 'root' !== $user->getLogin() &&
157 !in_array($user->getId(), $excluded_roles, true) &&
158 !$user->isAnonymous() &&
159 $user->getId() > 0
160 );
161 }
162
163 public function hasToResignAcceptance(ilObjUser $user, ilLogger $logger): bool
164 {
165 $logger->debug(sprintf(
166 'Checking reevaluation of Terms of Service for user "%s" (id: %s) ...',
167 $user->getLogin(),
168 $user->getId()
169 ));
170
171 if (!$this->isGloballyEnabled()) {
172 $logger->debug('Terms of Service disabled, resigning not required ...');
173 return false;
174 }
175
176 if (!$this->isIncludedUser($user)) {
177 $logger->debug('User is not included for Terms of Service acceptance, resigning not required ...');
178 return false;
179 }
180
181 if (!$this->tos->shouldReevaluateOnLogin()) {
182 $logger->debug('Reevaluation of documents is not enabled, resigning not required ...');
183 return false;
184 }
185
186 if (!$user->getAgreeDate()) {
187 $logger->debug('Terms of Service currently not accepted by user, resigning not required ...');
188 return false;
189 }
190
191 $evaluator = $this->termsOfServiceEvaluation->withContextUser($user);
192 if (!$evaluator->hasDocument()) {
193 $logger->debug('No signable Terms of Service document found, resigning not required ...');
194 return false;
195 }
196
197 $entity = $this->getCurrentAcceptanceForUser($user);
198 if (!($entity->getId() > 0)) {
199 $logger->debug('No signed Terms of Service document found, resigning not required ...');
200 return false;
201 }
202
203 $historizedDocument = new ilTermsOfServiceHistorizedDocument(
204 $entity,
205 new ilTermsOfServiceAcceptanceHistoryCriteriaBag($entity->getSerializedCriteria())
206 );
207
208 if ($evaluator->evaluateDocument($historizedDocument)) {
209 $logger->debug('Current user values do still match historized criteria, resigning not required ...');
210 return false;
211 }
212
213 $logger->debug('Current user values do not match historized criteria, resigning required ...');
214 return true;
215 }
216
218 {
220 }
221
223 {
225 }
226}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Component logger with individual log levels by component id.
debug(string $a_message, array $a_context=array())
Class ilObjTermsOfService.
User class.
writeAccepted()
write accept date of user agreement
hasToAcceptTermsOfServiceInSession(?bool $status=null)
setAgreeDate(?string $a_str)
Class ilTermsOfServiceEntityFactory.
Class ilTermsOfServiceHelper.
__construct(?ilTermsOfServiceDataGatewayFactory $dataGatewayFactory=null, ?ilTermsOfServiceDocumentEvaluation $termsOfServiceEvaluation=null, ?ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory=null, ?ilObjTermsOfService $tos=null)
ilTermsOfServiceDataGatewayFactory $dataGatewayFactory
hasToResignAcceptance(ilObjUser $user, ilLogger $logger)
ilTermsOfServiceDocumentEvaluation $termsOfServiceEvaluation
ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory
trackAcceptance(ilObjUser $user, ilTermsOfServiceSignableDocument $document)
const SYSTEM_USER_ID
This file contains constants for PHPStan analyis, see: https://phpstan.org/config-reference#constants...
Definition: constants.php:26
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $DIC
Definition: feed.php:28
Interface ilTermsOfServiceDocumentEvaluation.
Interface ilTermsOfServiceSignableDocument.