ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 mixed $additional_viewcontrol_data,
110 mixed $filter_data,
111 mixed $additional_parameters
112 ): \Generator {
113 $records = $this->getRecords($range, $order);
114 foreach ($records as $idx => $record) {
115 $row_id = (string) $record["id"];
116
117 yield $row_builder->buildDataRow($row_id, $record);
118 }
119 }
120
121 public function getTotalRowCount(
122 mixed $additional_viewcontrol_data,
123 mixed $filter_data,
124 mixed $additional_parameters
125 ): ?int {
126 return count($this->getRecords());
127 }
128
129 protected function getRecords(?Data\Range $range = null, ?Data\Order $order = null): array
130 {
131 $usages = \ilGlossaryTerm::getUsages($this->term_id);
132
133 $agg_usages = [];
134 foreach ($usages as $usage) {
135 if (empty($agg_usages[$usage["type"] . ":" . $usage["id"]])) {
136 $usage["hist_nr"] = [$usage["hist_nr"] ?? 0];
137 $agg_usages[$usage["type"] . ":" . $usage["id"]] = $usage;
138 } else {
139 $agg_usages[$usage["type"] . ":" . $usage["id"]]["hist_nr"][] =
140 $usage["hist_nr"] ?? 0;
141 }
142 }
143
144 $records = [];
145 $i = 0;
146 foreach ($agg_usages as $k => $usage) {
147 $records[$i]["id"] = $k;
148
149 $cont_type = "";
150 if (is_int(strpos($usage["type"], ":"))) {
151 $us_arr = explode(":", $usage["type"]);
152 $usage["type"] = $us_arr[1];
153 $cont_type = $us_arr[0];
154 }
155
156 switch ($usage["type"]) {
157 case "pg":
158 $item = [];
159
160 switch ($cont_type) {
161 case "lm":
162 $page_obj = new \ilLMPage($usage["id"]);
163 $lm_obj = new \ilObjLearningModule($page_obj->getParentId(), false);
164 $item["obj_type_txt"] = $this->lng->txt("obj_" . $cont_type);
165 $item["obj_title"] = $lm_obj->getTitle();
166 $item["sub_txt"] = $this->lng->txt("pg");
167 $item["sub_title"] = \ilLMObject::_lookupTitle($page_obj->getId());
168 $ref_id = $this->getFirstWritableRefId($lm_obj->getId());
169 if ($ref_id > 0) {
170 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "lm");
171 }
172 break;
173
174 case "wpg":
175 $page_obj = new \ilWikiPage($usage["id"]);
176 $item["obj_type_txt"] = $this->lng->txt("obj_wiki");
177 $item["obj_title"] = \ilObject::_lookupTitle($page_obj->getParentId());
178 $item["sub_txt"] = $this->lng->txt("pg");
179 $item["sub_title"] = \ilWikiPage::lookupTitle($page_obj->getId());
180 $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
181 if ($ref_id > 0) {
182 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "wiki");
183 }
184 break;
185
186 case "term":
187 $page_obj = new \ilGlossaryDefPage($usage["id"]);
188 $term_id = $page_obj->getId();
190 $item["obj_type_txt"] = $this->lng->txt("obj_glo");
191 $item["obj_title"] = \ilObject::_lookupTitle($glo_id);
192 $item["sub_txt"] = $this->lng->txt("cont_term");
193 $item["sub_title"] = \ilGlossaryTerm::_lookGlossaryTerm($term_id);
194 $ref_id = $this->getFirstWritableRefId($page_obj->getParentId());
195 if ($ref_id > 0) {
196 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "glo");
197 }
198 break;
199
200 case "fold":
201 case "root":
202 case "crs":
203 case "grp":
204 case "cat":
205 case "cont":
206 $item["obj_type_txt"] = $this->lng->txt("obj_" . $cont_type);
207 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
208 $ref_id = $this->getFirstWritableRefId($usage["id"]);
209 if ($ref_id > 0) {
210 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, $cont_type);
211 }
212 break;
213
214 default:
215 $item["obj_title"] = "Page " . $cont_type . ", " . $usage["id"];
216 break;
217 }
218 break;
219
220 case "mep":
221 $item["obj_type_txt"] = $this->lng->txt("obj_mep");
222 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
223 $ref_id = $this->getFirstWritableRefId($usage["id"]);
224 if ($ref_id > 0) {
225 $item["obj_link"] = \ilLink::_getStaticLink($ref_id, "mep");
226 }
227 break;
228
229 case "map":
230 $item["obj_type_txt"] = $this->lng->txt("obj_mob");
231 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
232 $item["sub_txt"] = $this->lng->txt("cont_link_area");
233 break;
234
235 case "sqst":
236 $item["obj_type_txt"] = $this->lng->txt("cont_sqst");
237 $obj_id = \SurveyQuestion::lookupObjFi($usage["id"]);
238 $item["obj_title"] = \ilObject::_lookupTitle($obj_id);
239 $item["sub_txt"] = $this->lng->txt("question");
240 $item["sub_title"] = \SurveyQuestion::_getTitle($usage["id"]);
241 $ref_id = $this->getFirstWritableRefId($obj_id);
242 if ($ref_id > 0) {
243 $item["obj_link"] = \ilLink::_getStaticLink($ref_id);
244 }
245 break;
246
247 case "termref":
248 $item["obj_type_txt"] = $this->lng->txt("obj_glo");
249 $item["obj_title"] = \ilObject::_lookupTitle($usage["id"]);
250 $item["sub_txt"] = $this->lng->txt("glo_referenced_term");
251 $ref_id = $this->getFirstWritableRefId($usage["id"]);
252 if ($ref_id > 0) {
253 $item["obj_link"] = \ilLink::_getStaticLink($ref_id);
254 }
255 break;
256
257 default:
258 $item["obj_title"] = "Type " . $usage["type"] . ", " . $usage["id"];
259 break;
260 }
261
262 // show versions
263 if (is_array($usage["hist_nr"]) &&
264 (count($usage["hist_nr"]) > 1 || $usage["hist_nr"][0] > 0)) {
265 asort($usage["hist_nr"]);
266 $ver = $sep = "";
267 if ($usage["hist_nr"][0] == 0) {
268 array_shift($usage["hist_nr"]);
269 $usage["hist_nr"][] = 0;
270 }
271 foreach ($usage["hist_nr"] as $nr) {
272 if ($nr > 0) {
273 $ver .= $sep . $nr;
274 } else {
275 $ver .= $sep . $this->lng->txt("cont_current_version");
276 }
277 $sep = ", ";
278 }
279
280 $records[$i]["version"] = $ver;
281 }
282
283 if (($item["obj_type_txt"] ?? "") != "") {
284 $records[$i]["type"] = $item["obj_type_txt"];
285 }
286
287 if (($usage["type"] ?? "") != "clip") {
288 $records[$i]["object"] = $item["obj_title"];
289 if ($item["obj_link"] ?? "") {
290 $link = $this->ui_fac->link()->standard($this->lng->txt("cont_link"), $item["obj_link"]);
291 $records[$i]["link"] = $link;
292 }
293
294 $sub_text = "";
295 if (($item["sub_txt"] ?? "") != "") {
296 $sub_text = $item["sub_txt"];
297 if (($item["sub_title"] ?? "") != "") {
298 $sub_text .= ": ";
299 $sub_text .= $item["sub_title"];
300 }
301 $records[$i]["sub_object"] = $sub_text;
302 }
303 } else {
304 $records[$i]["object"] = $this->lng->txt("cont_users_have_mob_in_clip1") .
305 " " . $usage["cnt"] . " " . $this->lng->txt("cont_users_have_mob_in_clip2");
306 }
307
308 $i++;
309 }
310
311 if ($order) {
312 $records = $this->orderRecords($records, $order);
313 }
314
315 if ($range) {
316 $records = $this->limitRecords($records, $range);
317 }
318
319 return $records;
320 }
321
322 protected function getFirstWritableRefId(int $obj_id): int
323 {
324 $ref_ids = \ilObject::_getAllReferences($obj_id);
325 foreach ($ref_ids as $ref_id) {
326 if ($this->access->checkAccess("write", "", $ref_id)) {
327 return $ref_id;
328 }
329 }
330 return 0;
331 }
332 };
333
334 return $data_retrieval;
335 }
336}
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