19 declare(strict_types=1);
44 private readonly
string $id,
66 return null !== $this->database->fetchAssoc($this->database->query(
67 "SELECT 1 FROM $versions INNER JOIN $tracking ON $versions.id = $tracking.tosv_id WHERE $versions.doc_id = $doc_id AND hash = $hash AND usr_id = $user AND " 68 . $this->document_meta->exists(
'doc_id')
79 $result = $this->database->fetchAssoc($this->database->query(
80 "SELECT $versions.* from $versions inner join $tracking on $versions.id = $tracking.tosv_id where usr_id = $user_id AND $versions.provider = $provider ORDER BY $tracking.ts DESC" 83 if ($result ===
null) {
84 return new Error(
'Not found.');
87 return new Ok(
new DocumentContent($result[
'type'], $result[
'title'] ??
'', $result[
'text'] ??
''));
95 $this->database->setLimit(1);
96 $result = $this->database->fetchAssoc($this->database->queryF(
97 "SELECT doc_id FROM $tracking INNER JOIN $version ON $tracking.tosv_id = $version.id WHERE provider = %s AND usr_id = %s ORDER BY $tracking.ts DESC",
99 [$this->id, $user->
getId()]
102 return $result ? $this->document_meta->find((
int) $result[
'doc_id']) :
new Error(
'No document found.');
109 public function all(array $filter = [], array $order_by = [],
int $offset = 0, ?
int $limit =
null): array
113 $documents = $this->document_meta->documentTable();
118 $order_by = $this->
mapKeys(fn($field) => match ($field) {
119 'created' =>
"$tracking.ts",
120 'document' =>
"$version.text",
121 'login' =>
"usr_data.login",
122 'firstname' =>
"usr_data.firstname",
123 'lastname' =>
"usr_data.lastname",
126 $rows = $this->database->fetchAll($this->database->query(
127 "SELECT $documents.*, $tracking.*, $version.text as old_text, $version.title as old_title, $version.type as old_type, $tracking.ts as ts FROM $tracking INNER JOIN $version ON $tracking.tosv_id = $version.id LEFT JOIN $documents ON $documents.id = $version.doc_id $join WHERE $filter AND $version.provider = $provider " .
128 $this->orderSqlFromArray($order_by) .
129 (
null === $limit ?
'' :
' LIMIT ' . $offset .
', ' . $limit)
142 $documents = $this->document_meta->documentTable();
147 return (
int) $this->database->fetchAssoc($this->database->query(
148 "SELECT COUNT(1) as count FROM $tracking INNER JOIN $version ON $tracking.tosv_id = $version.id INNER JOIN $documents ON $version.doc_id = $documents.id AND $version.provider = $provider " .
149 "$join WHERE $filter" 159 isset($row[
'id']) ? $this->document_meta->documentFromRow($row, []) :
null,
161 array_map(fn(array $criterion) =>
new CriterionContent($criterion[
'id'], $criterion[
'value']), json_decode($row[
'criteria'],
true) ?: []),
162 new DocumentContent($row[
'old_type'], $row[
'old_title'] ??
'', $row[
'old_text'] ??
'')
168 return md5($document->
content()->value());
177 $documents = $this->document_meta->documentTable();
180 $result = $this->database->fetchAssoc($this->database->query(
"SELECT id FROM $versions WHERE doc_id = $id AND hash = $hash AND " . $this->document_meta->exists(
'doc_id')));
183 new Ok((
int) $result[
'id']) :
184 new Error(
'Version not found');
189 $this->database->insert($this->
trackingTable(), $this->deriveFieldTypes([
190 'tosv_id' => $version_id,
192 'ts' => $this->action->modifiedNow()->time(),
193 'criteria' => json_encode(
array_map(fn($x) => [
194 'id' => $x->content()->type(),
195 'value' => $x->content()->arguments(),
203 $this->database->insert($this->
versionTable(), $this->deriveFieldTypes([
205 'text' => $document->
content()->value(),
206 'hash' => md5($document->
content()->value()),
207 'ts' => $this->action->modifiedNow()->time(),
208 'doc_id' => $document->
id(),
209 'title' => $document->
content()->title(),
218 return 'ldoc_versions';
223 return 'ldoc_acceptance_track';
236 $valid_field = fn(
string $field) => (
237 preg_match(
'/^([[:alnum:]_]+\.)?[[:alnum:]_]+$/i', $field) !== 1 &&
238 $this->
error(
'Invalid field name given: ' . print_r($field,
true))
240 $valid_direction = fn(
string $direction) => (
241 !in_array(strtolower($direction), [
'asc',
'desc'],
true) &&
242 $this->
error(
'Invalid order direction given, only asc and desc allowed, given: ' . print_r($direction,
true))
246 array_map($valid_field, array_keys($order_by));
249 fn(
string $field,
string $direction) => join(
' ', [$field, $direction]),
250 array_keys($order_by),
254 return $order_by !==
'' ?
' ORDER BY ' . $order_by :
'';
268 return array_combine(
283 foreach ($filter as $key => $value) {
284 $query .=
' AND ' . $filter_to_query($key, $value);
299 $join = array_intersect([
'login',
'firstname',
'lastname'], array_keys($order_by)) !== [] || isset($filter[
'query']) ?
300 " LEFT JOIN usr_data ON $tracking.usr_id = usr_data.usr_id " :
303 $filter = $this->
filterQuery($filter, fn($name, $value) => match ($name) {
307 fn($s, $v) =>
'usr_data.' . $s .
' LIKE ' . $v,
308 [
'login',
'email',
'firstname',
'lastname'],
313 return [$filter, $join];
documentHash(Document $document)
acceptDocument(ilObjUser $user, Document $document)
mapKeys(Closure $proc, array $array)
A B C
orderSqlFromArray(array $order_by)
__construct(private readonly string $id, private readonly DocumentRepositoryMeta $document_meta, private readonly ilDBInterface $database, private readonly UserAction $action)
acceptedVersion(ilObjUser $user)
addDocumentVersion(Document $document)
alreadyAccepted(ilObjUser $user, Document $document)
trackUser(int $user, Document $document, int $version_id)
filterAndJoin(array $filter, array $order_by=[])
currentDocumentOfAcceptedVersion(ilObjUser $user)
documentVersion(Document $document)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
countAll(array $filter=[])
recordFromRow(array $row)
A result encapsulates a value or an error and simplifies the handling of those.
filterQuery(array $filter, Closure $filter_to_query)
A
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
all(array $filter=[], array $order_by=[], int $offset=0, ?int $limit=null)