ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ProvideDocument.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Closure;
48 use ilObjUser;
49 
51 {
52  public const CRITERION_ALREADY_EXISTS = 'Criterion already exists.';
53  public const CRITERION_WOULD_NEVER_MATCH = 'Criterion would never match.';
54 
56  private readonly Closure $create_table_gui;
57 
63  public function __construct(
64  private readonly string $id,
65  private readonly DocumentRepository $document_repository,
66  private readonly SelectionMap $conditions,
67  private readonly array $content_as_component,
68  private readonly Container $container,
69  ?Closure $create_table_gui = null
70  ) {
71  $this->create_table_gui = $create_table_gui ?? fn($gui, $command, $t) => new Table($gui, $command, $t);
72  }
73 
74  public function table(object $gui, string $command, ?EditLinks $edit_links = null): Component
75  {
76  $t = new DocumentTable(
77  fn($criterion) => $this->toCondition($criterion)->asComponent(),
78  $this->document_repository,
79  new UI($this->id, $this->container->ui(), $this->container->language()),
80  new DocumentModal($this->container->ui(), $this->contentAsComponent(...))
81  );
82 
83  if ($edit_links !== null) {
84  $t = new EditableDocumentTable($t, $edit_links);
85  }
86 
87  $table = ($this->create_table_gui)($gui, $command, $t);
88 
89  return $this->container->ui()->factory()->legacy($table->getHTML());
90  }
91 
92  public function chooseDocumentFor(ilObjUser $user): Result
93  {
94  return $this->find(
95  fn(Document $document): bool => $this->documentMatches($document, $user),
96  $this->document_repository->all()
97  );
98  }
99 
100  public function documentMatches(Document $document, ilObjUser $user): bool
101  {
102  return $this->all(
103  fn($c) => $this->toCondition($c->content())->eval($user),
104  $document->criteria()
105  );
106  }
107 
108  public function repository(): DocumentRepository
109  {
110  return $this->document_repository;
111  }
112 
113  public function hash(): string
114  {
115  return bin2hex(openssl_random_pseudo_bytes((int) floor(255 / 2)));
116  }
117 
118  public function toCondition(CriterionContent $content): Condition
119  {
120  return $this->conditions->choices()[$content->type()]->withCriterion($content);
121  }
122 
126  public function conditionGroups(?CriterionContent $criterion = null): SelectionMap
127  {
128  $selected = $criterion ? [
129  $criterion->type() => $criterion->arguments()
130  ] : [];
131 
132  $choices = $this->conditions->choices();
133  $groups = array_combine(array_keys($choices), array_map(
134  fn(ConditionDefinition $condition, string $key) => $condition->formGroup($selected[$key] ?? []),
135  array_values($choices),
136  array_keys($choices)
137  ));
138 
139  return new SelectionMap($groups, $this->conditions->defaultSelection());
140  }
141 
145  public function validateCriteriaContent(array $criteria, CriterionContent $content): Result
146  {
147  return array_reduce(
148  $criteria,
149  $this->validateAgainst(...),
150  new Ok($content)
151  );
152  }
153 
154  public function contentAsComponent(DocumentContent $content): Component
155  {
156  return $this->content_as_component[$content->type()]($content);
157  }
158 
160  {
161  return $b->then(function (CriterionContent $b) use ($a) {
162  $a = $a->content();
163  if ($a->equals($b)) {
164  return new Error(self::CRITERION_ALREADY_EXISTS);
165  }
166  $a = $this->toCondition($a);
167  if ($a->knownToNeverMatchWith($this->toCondition($b))) {
168  return new Error(self::CRITERION_WOULD_NEVER_MATCH);
169  }
170  return new Ok($b);
171  });
172  }
173 
180  private function find(Closure $predicate, array $array): Result
181  {
182  foreach ($array as $x) {
183  if ($predicate($x)) {
184  return new Ok($x);
185  }
186  }
187 
188  return new Error('Not found.');
189  }
190 
196  private function any(Closure $predicate, array $array): bool
197  {
198  return $this->find($predicate, $array)->isOk();
199  }
200 
206  private function all(Closure $predicate, array $array): bool
207  {
208  return !$this->any(static fn($x) => !$predicate($x), $array);
209  }
210 }
then(callable $f)
Get a new result from the callable or do nothing if this is an error.
all(Closure $predicate, array $array)
A
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
documentMatches(Document $document, ilObjUser $user)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
find(Closure $predicate, array $array)
A
$container
Definition: wac.php:14
__construct(private readonly string $id, private readonly DocumentRepository $document_repository, private readonly SelectionMap $conditions, private readonly array $content_as_component, private readonly Container $container, ?Closure $create_table_gui=null)
conditionGroups(?CriterionContent $criterion=null)
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
string $key
Consumer key/client ID value.
Definition: System.php:193
validateCriteriaContent(array $criteria, CriterionContent $content)
any(Closure $predicate, array $array)
A
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
table(object $gui, string $command, ?EditLinks $edit_links=null)