ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
Retrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Generator;
27use ILIAS\Repository\RetrievalBase;
30use ilObject;
31use ILIAS\Data\Factory as DataFactory;
32use DateTimeImmutable;
34
36{
37 use RetrievalBase;
38
39 protected array $internal_data;
40
41 public function __construct(
42 protected SubObjectRetrieval $sub_object_retrieval,
43 protected InternalDomainService $domain,
44 protected DataFactory $data_factory
45 ) {
46 }
47
52 protected function getInternalData(array $filter): array
53 {
54 $media_object_manager = $this->domain->mediaObject();
55 $lom = $this->domain->learningObjectMetadata();
56
57 if (isset($this->internal_data)) {
59 }
60 $this->internal_data = [];
61 foreach ($this->sub_object_retrieval->getPossibleTypes() as $type) {
62 foreach ($this->sub_object_retrieval->getAllIDsForType($type) as $id) {
63 $title = $this->sub_object_retrieval->getTitleOfSubObject($type, $id);
64 $link = $this->sub_object_retrieval->getLinkToSubObject($type, $id);
65 foreach (ilObjMediaObject::_getMobsOfObject($type, $id, 0) as $mob_id) {
66 /*
67 * type and id are only needed here internally to separate
68 * internal from external usages, see addExternalData
69 */
70 $this->internal_data[$mob_id]['internal_usages'][$type . ':' . $id] = [
71 'title' => $title,
72 'link' => $link
73 ];
74 }
75 }
76 }
77 foreach ($this->internal_data as $mob_id => $value) {
78 if (!ilObjMediaObject::_exists($mob_id)) {
79 unset($this->internal_data[$mob_id]);
80 continue;
81 }
82 $title = ilObject::_lookupTitle($mob_id);
83 $last_update = $media_object_manager->getLastChangeTimestamp($mob_id);
84
85 $reader = $lom->read(0, $mob_id, 'mob', $lom->paths()->copyright());
86 $preset_copyright = $lom->copyrightHelper()->readPresetCopyright($reader);
87
88 if ($this->isDatumExcludedByFilter($title, $preset_copyright->identifier(), $last_update, $filter)) {
89 unset($this->internal_data[$mob_id]);
90 continue;
91 }
92
93 $this->internal_data[$mob_id]['id'] = $mob_id;
94 $this->internal_data[$mob_id]['title'] = $title;
95 $this->internal_data[$mob_id]['last_update'] = $last_update;
96 $this->internal_data[$mob_id]['copyright_identifier'] = $preset_copyright->identifier();
97 $this->internal_data[$mob_id]['copyright'] = $lom->copyrightHelper()->hasPresetCopyright($reader) ?
98 $preset_copyright->presentAsString() :
99 $lom->copyrightHelper()->readCustomCopyright($reader);
100 }
102 }
103
108 protected function addExternalData(array $internal_data): array
109 {
110 $access = $this->domain->access();
111 $static_url = $this->domain->staticUrl();
112
114 foreach ($data as $key => $datum) {
115 $mob_id = $datum['id'];
116 $already_collected_obj_ids = [];
117 foreach (ilObjMediaObject::lookupUsages($mob_id, false) as $usage) {
118 /*
119 * filter out already collected usages, see getInternalData
120 */
121 if (in_array(
122 $usage['type'] . ':' . $usage['id'],
123 array_keys($datum['internal_usages'])
124 )) {
125 continue;
126 }
127
128 $parent_obj_id = ilObjMediaObject::getParentObjectIdForUsage($usage);
129 if (!$parent_obj_id || in_array($parent_obj_id, $already_collected_obj_ids)) {
130 continue;
131 }
132
133 $parent_ref_id = null;
134 $show_link = false;
135 foreach (ilObject::_getAllReferences($parent_obj_id) as $ref_id) {
136 if (!$access->checkAccess('visible', '', $ref_id)) {
137 continue;
138 }
139 $parent_ref_id = $ref_id;
140 $show_link = $access->checkAccess('read', '', $parent_ref_id);
141 break;
142 }
143 if (!$parent_ref_id) {
144 continue;
145 }
146
147 $parent_title = ilObject::_lookupTitle($parent_obj_id);
148 $parent_type = ilObject::_lookupType($parent_obj_id);
149 $link_to_parent = '';
150 if ($show_link) {
151 $link_to_parent = (string) $static_url->builder()->build(
152 $parent_type,
153 $this->data_factory->refId($parent_ref_id)
154 );
155 }
156
157 $already_collected_obj_ids[] = $parent_obj_id;
158 if ($parent_type === 'mep') {
159 $data[$key]['mep_usages'][] = [
160 'title' => $parent_title,
161 'link' => $link_to_parent
162 ];
163 continue;
164 }
165 $data[$key]['external_usages'][] = [
166 'title' => $parent_title,
167 'link' => $link_to_parent
168 ];
169 }
170 }
171 return $data;
172 }
173
174 protected function isDatumExcludedByFilter(
175 string $title,
176 string $copyright_identifier,
177 int $last_update,
178 array $filter
179 ): bool {
180 if (
181 ($filter['title'] ?? '') !== '' &&
182 !str_contains(strtolower($title), strtolower($filter['title']))
183 ) {
184 return true;
185 }
186
187 if (
188 ($filter['copyright'] ?? []) !== [] &&
189 !in_array($copyright_identifier, $filter['copyright'])
190 ) {
191 return true;
192 }
193
194 $last_update = new DateTimeImmutable('@' . $last_update);
195 if (
196 (isset($filter['last_update'][0]) && $last_update < new DateTimeImmutable($filter['last_update'][0])) ||
197 (isset($filter['last_update'][1]) && $last_update > new DateTimeImmutable($filter['last_update'][1]))
198 ) {
199 return true;
200 }
201
202 return false;
203 }
204
205 public function getData(
206 array $fields,
207 ?Range $range = null,
208 ?Order $order = null,
209 array $filter = [],
210 array $parameters = []
211 ): Generator {
212 $data = $this->getInternalData($filter);
213
214 $data = $this->applyOrder($data, $order);
215 $data = $this->applyRange($data, $range);
216
217 /*
218 * read out other usages after applying order and range,
219 * to avoid unnecessary access checks
220 */
221 $data = $this->addExternalData($data);
222
223 yield from $data;
224 }
225
226 public function count(
227 array $filter,
228 array $parameters
229 ): int {
230 return count($this->getInternalData($filter));
231 }
232
233 public function isFieldNumeric(string $field): bool
234 {
235 return $field === 'last_update';
236 }
237}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
count(array $filter, array $parameters)
Definition: Retrieval.php:226
isDatumExcludedByFilter(string $title, string $copyright_identifier, int $last_update, array $filter)
Definition: Retrieval.php:174
getData(array $fields, ?Range $range=null, ?Order $order=null, array $filter=[], array $parameters=[])
Definition: Retrieval.php:205
addExternalData(array $internal_data)
add external usages to data, requiring additional access checks
Definition: Retrieval.php:108
__construct(protected SubObjectRetrieval $sub_object_retrieval, protected InternalDomainService $domain, protected DataFactory $data_factory)
Definition: Retrieval.php:41
getInternalData(array $filter)
get data only with internal usages and properties of the media objects
Definition: Retrieval.php:52
static getParentObjectIdForUsage(array $a_usage, bool $a_include_all_access_obj_ids=false)
Get's the repository object ID of a parent object, if possible see ilWebAccessChecker.
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _getMobsOfObject(string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
static lookupUsages(int $a_id, bool $a_include_history=true)
Lookup usages of media object.
Class ilObject Basic functions for all objects.
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$static_url
Definition: goto.php:29
$ref_id
Definition: ltiauth.php:66
if(!file_exists('../ilias.ini.php'))