19 declare(strict_types=1);
45 private readonly
string $id,
53 $creation = $this->action->modifiedNow();
56 'creation_ts' => $creation->time(),
57 'owner_usr_id' => $creation->user(),
58 'text' => $content->
value(),
59 'type' => $content->
type(),
67 $creation = $this->action->modifiedNow();
69 'doc_id' => $document->
id(),
70 'assigned_ts' => $creation->time(),
71 'owner_usr_id' => $creation->user(),
72 ...$this->criterionFields($content),
103 $modification = $this->action->modifiedNow();
105 $this->database->update($this->
criterionTable(), $this->deriveFieldTypes([
106 'modification_ts' => $modification->time(),
107 'last_modified_usr_id' => $modification->user(),
108 ...$this->criterionFields($content),
109 ]), $this->deriveFieldTypes([
110 'id' => $criterion_id,
119 match (get_class($document_id)) {
120 HashId::class => $this->
lazyDocFields($fields_and_values, $document_id->hash(), $silent),
121 NumberId::class => $this->
setDocFields($fields_and_values, $document_id->number(), $silent),
127 return (
int) current($this->queryF(
'SELECT COUNT(1) as c FROM ' . $this->
documentTable() .
' WHERE provider = %s', [$this->
id]))[
'c'];
133 public function all(
int $offset = 0, ?
int $limit = null): array
135 return $this->
queryDocuments(
'1', $limit === null ?
'' :
' LIMIT ' . $offset .
', ' . $limit);
142 public function select(array $ids): array
157 'Document with ID ' . $id .
' not found.' 163 return match (get_class($document_id)) {
164 HashId::class => $this->
findHash($document_id->hash()),
165 NumberId::class => $this->
find($document_id->number()),
185 (
int) $row[
'sorting'],
188 ),
new DocumentContent($row[
'type'], $row[
'title'] ??
'', $row[
'text'] ??
''), $criteria);
193 return 'ldoc_documents';
196 public function exists(
string $doc_id_name): string
200 $table =
't' . random_int(0, 100);
201 return "EXISTS (SELECT 1 FROM $documents AS $table WHERE $table.id = $doc_id_name AND $table.provider = $provider)";
207 private function setDocFields(array $fields_and_values,
int $doc_id,
bool $silent): void
209 $modification = $this->action->modifiedNow();
210 $this->database->update($this->
documentTable(), $this->deriveFieldTypes([
211 ...$fields_and_values,
213 'modification_ts' => $modification->time(),
214 'last_modified_usr_id' => $modification->user(),
216 ]), $this->deriveFieldTypes([
218 'provider' => $this->
id,
225 private function lazyDocFields(array $fields_and_values,
string $hash,
bool $silent): void
227 $modification = $this->action->modifiedNow();
228 $affected_rows = $this->database->update($this->
documentTable(), $this->deriveFieldTypes([
229 ...$fields_and_values,
231 'modification_ts' => $modification->time(),
232 'last_modified_usr_id' => $modification->user(),
234 ]), $this->deriveFieldTypes([
236 'provider' => $this->
id,
239 if (0 === $affected_rows) {
240 $this->database->insert($this->
documentTable(), $this->deriveFieldTypes([
241 'id' => $this->database->nextId($this->documentTable()),
242 'creation_ts' => $modification->time(),
243 'owner_usr_id' => $modification->user(),
246 'title' =>
'Unnamed document',
248 ...$fields_and_values,
256 'criterion_id' => $content->
type(),
257 'criterion_value' => json_encode($content->
arguments()),
265 $documents = $this->query(
'SELECT * FROM ' . $doc_table .
' WHERE ' . $where .
' AND provider = ' .
$provider .
' ORDER BY sorting ' . $limit);
266 $doc_ids = array_map(fn($doc) => (
int) $doc[
'id'], $documents);
267 $array = $this->query(join(
' ', [
277 foreach ($array as $row) {
278 $document_id = (
int) $row[
'doc_id'];
279 $assignments[$document_id] ??= [];
285 fn($doc) => $this->
documentFromRow($doc, $assignments[(
int) $doc[
'id']] ?? []),
295 $row[
'criterion_id'],
296 json_decode($row[
'criterion_value'],
true)
305 return 'ldoc_criteria';
311 private function insert(
string $table, array $fields_and_values): void
313 $id = $this->database->nextId($table);
315 $this->database->insert($table, $this->deriveFieldTypes([...$fields_and_values,
'id' =>
$id]));
321 private function update(
int $id,
string $table, array $fields_and_values): void
323 $this->database->update($table, $this->deriveFieldTypes($fields_and_values), $this->deriveFieldTypes([
328 private function deleteEntry(
string $table,
int $id,
string $doc_field,
bool $cleanup =
false): void
331 $this->database->manipulate(
"DELETE FROM $table WHERE id = $id AND " . $this->
exists($table .
'.' . $doc_field));
342 $this->database->manipulate(
"DELETE FROM $criteria WHERE doc_id NOT IN (SELECT id FROM $documents)");
348 $sorting = (
int) ($this->database->fetchAssoc($this->database->query(
349 "SELECT MAX(sorting) as s FROM $documents WHERE " . $this->exists($documents .
'.id')
352 return $sorting + 10;
359 'Document with hash . ' . json_encode($hash) .
' not found.' 365 $document = current($array) ?: null;
366 return $document ?
new Ok($document) :
new Error($message);
updateDocumentContent(DocumentId $document_id, DocumentContent $content)
update(int $id, string $table, array $fields_and_values)
__construct(private readonly string $id, private readonly ilDBInterface $database, private readonly UserAction $action,)
updateDocument(DocumentId $document_id, array $fields_and_values, bool $silent=false)
exists(string $doc_id_name)
criterionFields(CriterionContent $content)
setDocFields(array $fields_and_values, int $doc_id, bool $silent)
A result encapsulates a value or an error and simplifies the handling of those.
createCriterion(Document $document, CriterionContent $content)
lazyDocFields(array $fields_and_values, string $hash, bool $silent)
first(array $array, string $message)
all(int $offset=0, ?int $limit=null)
insert(string $table, array $fields_and_values)
criterionFromRow(array $row)
updateDocumentOrder(DocumentId $document_id, int $order)
updateDocumentTitle(DocumentId $document_id, string $title)
updateCriterionContent(int $criterion_id, CriterionContent $content)
A result encapsulates a value or an error and simplifies the handling of those.
deleteCriterion(int $criterion_id)
documentFromRow(array $row, array $criteria)
deleteDocument(Document $document)
deleteEntry(string $table, int $id, string $doc_field, bool $cleanup=false)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
findId(DocumentId $document_id)
createDocument(string $title, DocumentContent $content)
queryDocuments(string $where='1', string $limit='')