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();
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 ($document_id::class) {
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 ($document_id::class) {
164 HashId::class => $this->
findHash($document_id->hash()),
165 NumberId::class => $this->
find($document_id->number()),
186 (
int) $row[
'sorting'],
189 ),
new DocumentContent($row[
'type'], $row[
'title'] ??
'', $row[
'text'] ??
''), $criteria);
194 return 'ldoc_documents';
197 public function exists(
string $doc_id_name): string
201 $table =
't' . random_int(0, 100);
202 return "EXISTS (SELECT 1 FROM $documents AS $table WHERE $table.id = $doc_id_name AND $table.provider = $provider)";
208 private function setDocFields(array $fields_and_values,
int $doc_id,
bool $silent): void
210 $modification = $this->action->modifiedNow();
212 ...$fields_and_values,
214 'modification_ts' => $modification->time(),
215 'last_modified_usr_id' => $modification->user(),
217 ]), $this->deriveFieldTypes([
219 'provider' => $this->
id,
226 private function lazyDocFields(array $fields_and_values,
string $hash,
bool $silent): void
228 $modification = $this->action->modifiedNow();
230 ...$fields_and_values,
232 'modification_ts' => $modification->time(),
233 'last_modified_usr_id' => $modification->user(),
235 ]), $this->deriveFieldTypes([
237 'provider' => $this->
id,
240 if (0 === $affected_rows) {
242 'id' => $this->
database->nextId($this->documentTable()),
243 'creation_ts' => $modification->time(),
244 'owner_usr_id' => $modification->user(),
247 'title' =>
'Unnamed document',
249 ...$fields_and_values,
260 'criterion_id' => $content->
type(),
261 'criterion_value' => json_encode($content->
arguments()),
272 $documents = $this->query(
'SELECT * FROM ' . $doc_table .
' WHERE ' . $where .
' AND provider = ' .
$provider .
' ORDER BY sorting ' . $limit);
273 $doc_ids =
array_map(fn($doc) => (
int) $doc[
'id'], $documents);
274 $array = $this->query(join(
' ', [
284 foreach ($array as $row) {
285 $document_id = (
int) $row[
'doc_id'];
286 $assignments[$document_id] ??= [];
292 fn($doc) => $this->
documentFromRow($doc, $assignments[(
int) $doc[
'id']] ?? []),
302 $row[
'criterion_id'],
303 json_decode($row[
'criterion_value'],
true)
312 return 'ldoc_criteria';
318 private function insert(
string $table, array $fields_and_values): void
322 $this->
database->insert($table, $this->deriveFieldTypes([...$fields_and_values,
'id' =>
$id]));
328 private function update(
int $id,
string $table, array $fields_and_values): void
330 $this->
database->update($table, $this->deriveFieldTypes($fields_and_values), $this->deriveFieldTypes([
335 private function deleteEntry(
string $table,
int $id,
string $doc_field,
bool $cleanup =
false): void
338 $this->
database->manipulate(
"DELETE FROM $table WHERE id = $id AND " . $this->
exists($table .
'.' . $doc_field));
349 $this->
database->manipulate(
"DELETE FROM $criteria WHERE doc_id NOT IN (SELECT id FROM $documents)");
355 $sorting = (
int) ($this->
database->fetchAssoc($this->database->query(
356 "SELECT MAX(sorting) as s FROM $documents WHERE " . $this->exists($documents .
'.id')
359 return $sorting + 10;
366 'Document with hash . ' . json_encode($hash) .
' not found.' 372 $document = current($array) ?:
null;
373 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)
createCriterion(Document $document, CriterionContent $content)
lazyDocFields(array $fields_and_values, string $hash, bool $silent)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
deleteEntry(string $table, int $id, string $doc_field, bool $cleanup=false)
findId(DocumentId $document_id)
createDocument(string $title, DocumentContent $content)
queryDocuments(string $where='1', string $limit='')