ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilTermsOfServiceHelper.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
17 protected $tos;
18
26 public function __construct(
31 ) {
32 global $DIC;
33
34 if (null === $dataGatewayFactory) {
36 $dataGatewayFactory->setDatabaseAdapter($DIC->database());
37 }
38 $this->dataGatewayFactory = $dataGatewayFactory;
39
40 if (null === $termsOfServiceEvaluation) {
41 $termsOfServiceEvaluation = $DIC['tos.document.evaluator'];
42 }
43 $this->termsOfServiceEvaluation = $termsOfServiceEvaluation;
44
45 if (null === $criterionTypeFactory) {
46 $criterionTypeFactory = $DIC['tos.criteria.type.factory'];
47 }
48 $this->criterionTypeFactory = $criterionTypeFactory;
49
50 if (null === $tos) {
52 }
53 $this->tos = $tos;
54 }
55
59 public static function isEnabled() : bool
60 {
61 return (new static())->tos->getStatus();
62 }
63
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((int) $user->getId()));
95 }
96
102 public function getById(int $id) : ilTermsOfServiceAcceptanceEntity
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((int) $user->getId())
123 ->withTimestamp(time())
124 ->withText((string) $document->content())
125 ->withHash(md5($document->content()))
126 ->withDocumentId((int) $document->id())
127 ->withTitle((string) $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
142 public function resetAcceptance(ilObjUser $user) : void
143 {
144 $user->setAgreeDate(null);
145 $user->update();
146 }
147
152 public function isIncludedUser(ilObjUser $user) : bool
153 {
154 return (
155 'root' !== $user->getLogin() &&
156 !in_array($user->getId(), [ANONYMOUS_USER_ID, SYSTEM_USER_ID]) &&
157 !$user->isAnonymous() &&
158 (int) $user->getId() > 0
159 );
160 }
161
167 public function hasToResignAcceptance(ilObjUser $user, ilLogger $logger) : bool
168 {
169 $logger->debug(sprintf(
170 'Checking reevaluation of Terms of Service for user "%s" (id: %s) ...',
171 $user->getLogin(),
172 $user->getId()
173 ));
174
175 if (!$this->isGloballyEnabled()) {
176 $logger->debug(sprintf(
177 'Terms of Service disabled, resigning not required ...'
178 ));
179 return false;
180 }
181
182 if (!$this->isIncludedUser($user)) {
183 $logger->debug(sprintf(
184 'User is not included for Terms of Service acceptance, resigning not required ...'
185 ));
186 return false;
187 }
188
189 if (!$this->tos->shouldReevaluateOnLogin()) {
190 $logger->debug(sprintf(
191 'Reevaluation of documents is not enabled, resigning not required ...'
192 ));
193 return false;
194 }
195
196 if (!$user->getAgreeDate()) {
197 $logger->debug(sprintf(
198 'Terms of Service currently not accepted by user, resigning not required ...'
199 ));
200 return false;
201 }
202
203 $evaluator = $this->termsOfServiceEvaluation->withContextUser($user);
204 if (!$evaluator->hasDocument()) {
205 $logger->debug(sprintf(
206 'No signable Terms of Service document found, resigning not required ...'
207 ));
208 return false;
209 }
210
211 $entity = $this->getCurrentAcceptanceForUser($user);
212 if (!($entity->getId() > 0)) {
213 $logger->debug(sprintf(
214 'No signed Terms of Service document found, resigning not required ...'
215 ));
216 return false;
217 }
218
219 $historizedDocument = new ilTermsOfServiceHistorizedDocument(
220 $entity,
221 new ilTermsOfServiceAcceptanceHistoryCriteriaBag($entity->getSerializedCriteria()),
222 $this->criterionTypeFactory
223 );
224
225 if ($evaluator->evaluateDocument($historizedDocument)) {
226 $logger->debug(sprintf(
227 'Current user values do still match historized criteria, resigning not required ...'
228 ));
229 return false;
230 }
231
232 $logger->debug(sprintf(
233 'Current user values do not match historized criteria, resigning required ...'
234 ));
235 return true;
236 }
237
242 {
244 }
245
250 {
252 }
253}
An exception for terminatinating execution or to throw for unit testing.
Component logger with individual log levels by component id.
debug($a_message, $a_context=array())
Class ilObjTermsOfService.
getAgreeDate()
get the date when the user accepted the user agreement @access public
setAgreeDate($a_str)
set date the user account was accepted by the user nullindicates that the user has not accepted his a...
writeAccepted()
write accept date of user agreement to db
getLogin()
get login / username @access public
hasToAcceptTermsOfServiceInSession($status=null)
update()
update object in db
getId()
get object id @access public
Class ilTermsOfServiceAcceptanceEntity.
Class ilTermsOfServiceEntityFactory.
Class ilTermsOfServiceHelper.
__construct(ilTermsOfServiceDataGatewayFactory $dataGatewayFactory=null, ilTermsOfServiceDocumentEvaluation $termsOfServiceEvaluation=null, ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory=null, ilObjTermsOfService $tos=null)
ilTermsOfServiceHelper constructor.
hasToResignAcceptance(ilObjUser $user, ilLogger $logger)
trackAcceptance(ilObjUser $user, ilTermsOfServiceSignableDocument $document)
const SYSTEM_USER_ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: constants.php:24
const ANONYMOUS_USER_ID
Definition: constants.php:25
global $DIC
Definition: goto.php:24
Interface ilTermsOfServiceDocumentEvaluation.
Interface ilTermsOfServiceSignableDocument.