ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ProvideDocument.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Closure;
45 use ilObjUser;
46 
48 {
49  public const CRITERION_ALREADY_EXISTS = 'Criterion already exists.';
50  public const CRITERION_WOULD_NEVER_MATCH = 'Criterion would never match.';
51 
53  private readonly Closure $create_table_gui;
54 
59  public function __construct(
60  private readonly string $id,
61  private readonly DocumentRepository $document_repository,
62  private readonly SelectionMap $conditions,
63  private readonly array $content_as_component,
64  private readonly Container $container,
65  ?Closure $create_table_gui = null
66  ) {
67  $this->create_table_gui = $create_table_gui ?? fn($gui, $command, $t) => new Table($gui, $command, $t);
68  }
69 
70  public function table(object $gui, string $command, ?EditLinks $edit_links = null): Component
71  {
72  $t = new DocumentTable(
73  fn($criterion) => $this->toCondition($criterion)->asComponent(),
74  $this->document_repository,
75  new UI($this->id, $this->container->ui(), $this->container->language()),
76  new DocumentModal($this->container->ui(), $this->contentAsComponent(...))
77  );
78 
79  if ($edit_links !== null) {
80  $t = new EditableDocumentTable($t, $edit_links);
81  }
82 
83  $table = ($this->create_table_gui)($gui, $command, $t);
84 
85  return $this->container->ui()->factory()->legacy($table->getHTML());
86  }
87 
88  public function chooseDocumentFor(ilObjUser $user): Result
89  {
90  return $this->find(
91  fn(Document $document): bool => $this->documentMatches($document, $user),
92  $this->document_repository->all()
93  );
94  }
95 
96  public function documentMatches(Document $document, ilObjUser $user): bool
97  {
98  return $this->all(
99  fn($c) => $this->toCondition($c->content())->eval($user),
100  $document->criteria()
101  );
102  }
103 
104  public function repository(): DocumentRepository
105  {
106  return $this->document_repository;
107  }
108 
109  public function hash(): string
110  {
111  return bin2hex(openssl_random_pseudo_bytes((int) floor(255 / 2)));
112  }
113 
114  public function toCondition(CriterionContent $content): Condition
115  {
116  return $this->conditions->choices()[$content->type()]->withCriterion($content);
117  }
118 
122  public function conditionGroups(?CriterionContent $criterion = null): SelectionMap
123  {
124  $selected = $criterion ? [
125  $criterion->type() => $criterion->arguments()
126  ] : [];
127 
128  $choices = $this->conditions->choices();
129  $groups = array_combine(array_keys($choices), array_map(
130  fn(ConditionDefinition $condition, string $key) => $condition->formGroup($selected[$key] ?? []),
131  array_values($choices),
132  array_keys($choices)
133  ));
134 
135  return new SelectionMap($groups, $this->conditions->defaultSelection());
136  }
137 
141  public function validateCriteriaContent(array $criteria, CriterionContent $content): Result
142  {
143  return array_reduce(
144  $criteria,
145  $this->validateAgainst(...),
146  new Ok($content)
147  );
148  }
149 
150  public function contentAsComponent(DocumentContent $content): Component
151  {
152  return $this->content_as_component[$content->type()]($content);
153  }
154 
156  {
157  return $b->then(function (CriterionContent $b) use ($a) {
158  $a = $a->content();
159  if ($a->equals($b)) {
160  return new Error(self::CRITERION_ALREADY_EXISTS);
161  }
162  $a = $this->toCondition($a);
163  if ($a->knownToNeverMatchWith($this->toCondition($b))) {
164  return new Error(self::CRITERION_WOULD_NEVER_MATCH);
165  }
166  return new Ok($b);
167  });
168  }
169 
176  private function find(Closure $predicate, array $array): Result
177  {
178  foreach ($array as $x) {
179  if ($predicate($x)) {
180  return new Ok($x);
181  }
182  }
183 
184  return new Error('Not found.');
185  }
186 
192  private function any(Closure $predicate, array $array): bool
193  {
194  return $this->find($predicate, $array)->isOk();
195  }
196 
202  private function all(Closure $predicate, array $array): bool
203  {
204  return !$this->any(static fn($x) => !$predicate($x), $array);
205  }
206 }
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
documentMatches(Document $document, ilObjUser $user)
$c
Definition: deliver.php:9
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
find(Closure $predicate, array $array)
A
$container
Definition: wac.php:13
__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
validateCriteriaContent(array $criteria, CriterionContent $content)
any(Closure $predicate, array $array)
A
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
table(object $gui, string $command, ?EditLinks $edit_links=null)