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