ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.TermUsagesTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
26
31{
32 protected \ilLanguage $lng;
33 protected UI\Factory $ui_fac;
34 protected UI\Renderer $ui_ren;
35 protected ServerRequestInterface $request;
36 protected \ilAccessHandler $access;
37 protected int $term_id;
38
39 public function __construct(int $term_id)
40 {
41 global $DIC;
42
43 $this->lng = $DIC->language();
44 $this->ui_fac = $DIC->ui()->factory();
45 $this->ui_ren = $DIC->ui()->renderer();
46 $this->request = $DIC->http()->request();
47 $this->access = $DIC->access();
48 $this->term_id = $term_id;
49 }
50
51 public function getComponent(): UI\Component\Table\Data
52 {
53 $columns = $this->getColumns();
54 $data_retrieval = $this->getDataRetrieval();
55
56 $glo_id = \ilGlossaryTerm::_lookGlossaryID($this->term_id);
57 $table = $this->ui_fac->table()
58 ->data($data_retrieval, $this->lng->txt("cont_usage"), $columns)
59 ->withId(
60 self::class . "_" .
61 $glo_id . "_" .
62 $this->term_id
63 )
64 ->withRequest($this->request);
65
66 return $table;
67 }
68
69 protected function getColumns(): array
70 {
71 $columns = [
72 "object" => $this->ui_fac->table()->column()->text($this->lng->txt("objects")),
73 "sub_object" => $this->ui_fac->table()->column()->text($this->lng->txt("subobjects")),
74 "version" => $this->ui_fac->table()->column()->text($this->lng->txt("cont_versions"))
75 ->withIsSortable(false),
76 "type" => $this->ui_fac->table()->column()->text($this->lng->txt("type")),
77 "link" => $this->ui_fac->table()->column()->link($this->lng->txt("cont_link"))
78 ->withIsSortable(false)
79 ];
80
81 return $columns;
82 }
83
84 protected function getDataRetrieval(): UI\Component\Table\DataRetrieval
85 {
86 $data_retrieval = new class (
92 ) implements UI\Component\Table\DataRetrieval {
93 use TableRecords;
94
95 public function __construct(
96 protected \ilLanguage $lng,
97 protected \ilAccess $access,
98 protected UI\Factory $ui_fac,
99 protected UI\Renderer $ui_ren,
100 protected int $term_id
101 ) {
102 }
103
104 public function getRows(
105 UI\Component\Table\DataRowBuilder $row_builder,
106 array $visible_column_ids,
108 Data\Order $order,
109 ?array $filter_data,
110 ?array $additional_parameters
111 ): \Generator {
112 $records = $this->getRecords($range, $order);
113 foreach ($records as $idx => $record) {
114 $row_id = (string) $record["id"];
115
116 yield $row_builder->buildDataRow($row_id, $record);
117 }
118 }
119
120 public function getTotalRowCount(
121 ?array $filter_data,
122 ?array $additional_parameters
123 ): ?int {
124 return count($this->getRecords());
125 }
126
127 protected function getRecords(?Data\Range $range = null, ?Data\Order $order = null): array
128 {
129 $usages = \ilGlossaryTerm::getUsages($this->term_id);
130
131 $agg_usages = [];
132 foreach ($usages as $usage) {
133 if (empty($agg_usages[$usage["type"] . ":" . $usage["id"]])) {
134 $usage["hist_nr"] = [$usage["hist_nr"] ?? 0];
135 $agg_usages[$usage["type"] . ":" . $usage["id"]] = $usage;
136 } else {
137 $agg_usages[$usage["type"] . ":" . $usage["id"]]["hist_nr"][] =
138 $usage["hist_nr"] ?? 0;
139 }
140 }
141
142 $records = [];
143 $i = 0;
144 foreach ($agg_usages as $k => $usage) {
145 $records[$i]["id"] = $k;
146
147 $cont_type = "";
148 if (is_int(strpos($usage["type"], ":"))) {
149 $us_arr = explode(":", $usage["type"]);
150 $usage["type"] = $us_arr[1];
151 $cont_type = $us_arr[0];
152 }
153
154 switch ($usage["type"]) {
155 case "pg":
156 $item = [];
157
158 switch ($cont_type) {
159 case "lm":
160 $page_obj = new \ilLMPage($usage["id"]);
161 $lm_obj = new \ilObjLearningModule($page_obj->getParentId(), false);
162 $item["obj_type_txt"] = $this->lng->txt("obj_" . $cont_type);
163 $item["obj_title"] = $lm_obj->getTitle();
164 $item["sub_txt"] = $this->lng->txt("pg");
165 $item["sub_title"] = \ilLMObject::_lookupTitle($page_obj->getId());
166 $ref_id = $this->getFirstWritableRefId($lm_obj->getId());
167 if ($ref_id > 0) {
168 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "lm");
169 }
170 break;
171
172 case "wpg":
173 $page_obj = new \ilWikiPage($usage["id"]);
174 $item["obj_type_txt"] = $this->lng->txt("obj_wiki");
175 $item["obj_title"] = \ilObject::_lookupTitle($page_obj->getParentId());
176 $item["sub_txt"] = $this->lng->txt("pg");
177 $item["sub_title"] = \ilWikiPage::lookupTitle($page_obj->getId());
178 $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
179 if ($ref_id > 0) {
180 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "wiki");
181 }
182 break;
183
184 case "term":
185 $page_obj = new \ilGlossaryDefPage($usage["id"]);
186 $term_id = $page_obj->getId();
188 $item["obj_type_txt"] = $this->lng->txt("obj_glo");
189 $item["obj_title"] = \ilObject::_lookupTitle($glo_id);
190 $item["sub_txt"] = $this->lng->txt("cont_term");
191 $item["sub_title"] = \ilGlossaryTerm::_lookGlossaryTerm($term_id);
192 $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
193 if ($ref_id > 0) {
194 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "glo");
195 }
196 break;
197
198 case "fold":
199 case "root":
200 case "crs":
201 case "grp":
202 case "cat":
203 case "cont":
204 $item["obj_type_txt"] = $this->lng->txt("obj_" . $cont_type);
205 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
206 $ref_id = $this->getFirstWritableRefId($usage["id"]);
207 if ($ref_id > 0) {
208 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, $cont_type);
209 }
210 break;
211
212 default:
213 $item["obj_title"] = "Page " . $cont_type . ", " . $usage["id"];
214 break;
215 }
216 break;
217
218 case "mep":
219 $item["obj_type_txt"] = $this->lng->txt("obj_mep");
220 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
221 $ref_id = $this->getFirstWritableRefId($usage["id"]);
222 if ($ref_id > 0) {
223 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "mep");
224 }
225 break;
226
227 case "map":
228 $item["obj_type_txt"] = $this->lng->txt("obj_mob");
229 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
230 $item["sub_txt"] = $this->lng->txt("cont_link_area");
231 break;
232
233 case "sqst":
234 $item["obj_type_txt"] = $this->lng->txt("cont_sqst");
235 $obj_id = \SurveyQuestion::lookupObjFi($usage["id"]);
236 $item["obj_title"] = \ilObject::_lookupTitle($obj_id);
237 $item["sub_txt"] = $this->lng->txt("question");
238 $item["sub_title"] = \SurveyQuestion::_getTitle($usage["id"]);
239 $ref_id = $this->getFirstWritableRefId($obj_id);
240 if ($ref_id > 0) {
241 $item["obj_link"] = \ilLink::_getStaticLink($ref_id);
242 }
243 break;
244
245 case "termref":
246 $item["obj_type_txt"] = $this->lng->txt("obj_glo");
247 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
248 $item["sub_txt"] = $this->lng->txt("glo_referenced_term");
249 $ref_id = $this->getFirstWritableRefId($usage["id"]);
250 if ($ref_id > 0) {
251 $item["obj_link"] = \ilLink::_getStaticLink($ref_id);
252 }
253 break;
254
255 default:
256 $item["obj_title"] = "Type " . $usage["type"] . ", " . $usage["id"];
257 break;
258 }
259
260 // show versions
261 if (is_array($usage["hist_nr"]) &&
262 (count($usage["hist_nr"]) > 1 || $usage["hist_nr"][0] > 0)) {
263 asort($usage["hist_nr"]);
264 $ver = $sep = "";
265 if ($usage["hist_nr"][0] == 0) {
266 array_shift($usage["hist_nr"]);
267 $usage["hist_nr"][] = 0;
268 }
269 foreach ($usage["hist_nr"] as $nr) {
270 if ($nr > 0) {
271 $ver .= $sep . $nr;
272 } else {
273 $ver .= $sep . $this->lng->txt("cont_current_version");
274 }
275 $sep = ", ";
276 }
277
278 $records[$i]["version"] = $ver;
279 }
280
281 if (($item["obj_type_txt"] ?? "") != "") {
282 $records[$i]["type"] = $item["obj_type_txt"];
283 }
284
285 if (($usage["type"] ?? "") != "clip") {
286 $records[$i]["object"] = $item["obj_title"];
287 if ($item["obj_link"] ?? "") {
288 $link = $this->ui_fac->link()->standard($this->lng->txt("cont_link"), $item["obj_link"]);
289 $records[$i]["link"] = $link;
290 }
291
292 $sub_text = "";
293 if (($item["sub_txt"] ?? "") != "") {
294 $sub_text = $item["sub_txt"];
295 if (($item["sub_title"] ?? "") != "") {
296 $sub_text .= ": ";
297 $sub_text .= $item["sub_title"];
298 }
299 $records[$i]["sub_object"] = $sub_text;
300 }
301 } else {
302 $records[$i]["object"] = $this->lng->txt("cont_users_have_mob_in_clip1") .
303 " " . $usage["cnt"] . " " . $this->lng->txt("cont_users_have_mob_in_clip2");
304 }
305
306 $i++;
307 }
308
309 if ($order) {
310 $records = $this->orderRecords($records, $order);
311 }
312
313 if ($range) {
314 $records = $this->limitRecords($records, $range);
315 }
316
317 return $records;
318 }
319
320 protected function getFirstWritableRefId(int $obj_id): int
321 {
322 $ref_ids = \ilObject::_getAllReferences($obj_id);
323 foreach ($ref_ids as $ref_id) {
324 if ($this->access->checkAccess("write", "", $ref_id)) {
325 return $ref_id;
326 }
327 }
328 return 0;
329 }
330 };
331
332 return $data_retrieval;
333 }
334}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
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
Definition: UI.php:24
static _getTitle(int $question_id)
Returns the question title of a question with a given id.
static lookupObjFi(int $a_qid)
Class ilAccessHandler Checks access for ILIAS objects.
static getUsages(int $a_term_id)
static _lookGlossaryTerm(int $term_id)
get glossary term
static _lookGlossaryID(int $term_id)
get glossary id form term id
static _lookupTitle(int $a_obj_id)
language handling
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupTitle(int $obj_id)
static lookupTitle(int $a_page_id, string $lang="-")
An entity that renders components to a string output.
Definition: Renderer.php:31
$ref_id
Definition: ltiauth.php:66
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26