ILIAS  release_8 Revision v8.23
class.ilTermsOfServiceSequentialDocumentEvaluation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
28  protected ilObjUser $user;
30  protected array $matchingDocumentsByUser = [];
32  protected array $possibleDocuments = [];
33  protected ilLogger $log;
34 
35  public function __construct(
37  ilObjUser $user,
38  ilLogger $log,
39  array $possibleDocuments
40  ) {
41  $this->evaluation = $evaluation;
42  $this->user = $user;
43  $this->log = $log;
44  $this->possibleDocuments = $possibleDocuments;
45  }
46 
48  {
49  $clone = clone $this;
50  $clone->user = $user;
51  $clone->evaluation = $clone->evaluation->withContextUser($user);
52 
53  return $clone;
54  }
55 
59  protected function getMatchingDocuments(): array
60  {
61  if (!array_key_exists($this->user->getId(), $this->matchingDocumentsByUser)) {
62  $this->matchingDocumentsByUser[$this->user->getId()] = [];
63 
64  $this->log->debug(sprintf(
65  'Evaluating document for user "%s" (id: %s) ...',
66  $this->user->getLogin(),
67  $this->user->getId()
68  ));
69 
70  foreach ($this->possibleDocuments as $document) {
71  if ($this->evaluateDocument($document)) {
72  $this->matchingDocumentsByUser[$this->user->getId()][] = $document;
73  }
74  }
75 
76  $this->log->debug(sprintf(
77  '%s matching document(s) found',
78  count($this->matchingDocumentsByUser[$this->user->getId()])
79  ));
80  }
81 
82  return $this->matchingDocumentsByUser[$this->user->getId()];
83  }
84 
85  public function evaluateDocument(ilTermsOfServiceSignableDocument $document): bool
86  {
87  return $this->evaluation->evaluate($document);
88  }
89 
91  {
92  $matchingDocuments = $this->getMatchingDocuments();
93  if (count($matchingDocuments) > 0) {
94  return $matchingDocuments[0];
95  }
96 
98  'Could not find any terms of service document for the passed user (id: %s|login: %s)',
99  $this->user->getId(),
100  $this->user->getLogin()
101  ));
102  }
103 
104  public function hasDocument(): bool
105  {
106  return count($this->getMatchingDocuments()) > 0;
107  }
108 }
__construct(ilTermsOfServiceDocumentCriteriaEvaluation $evaluation, ilObjUser $user, ilLogger $log, array $possibleDocuments)
Interface ilTermsOfServiceSequentialDocumentEvaluation.
Interface ilTermsOfServiceDocumentEvaluation.
withContextUser(ilObjUser $user)
Returns an evaluator like this with the passed context user.
Interface ilTermsOfServiceSignableDocument.
evaluateDocument(ilTermsOfServiceSignableDocument $document)
Evaluates the passed document for the context given in the concrete implementation.
document()
Determines a document based on the context of the concrete implementation.