ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
GroupingRetrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use Generator;
29use ILIAS\UI\Factory as UIFactory;
30use ilObject;
31use ILIAS\StaticURL\Services as StaticURL;
32use ILIAS\Data\Factory as DataFactory;
33use ilLanguage;
34use ILIAS\UI\Component\Link\Standard as StandardLink;
35use ILIAS\UI\Component\Listing\Unordered as UnorderedListing;
36
38
40{
41 public function __construct(
42 protected int $content_obj_id,
43 protected ilLanguage $lng,
44 protected UIFactory $ui_factory,
45 protected DataFactory $data_factory,
46 protected StaticURL $static_url
47 ) {
48 }
49
50 public function getRows(
51 DataRowBuilder $row_builder,
52 array $visible_column_ids,
54 Order $order,
55 mixed $additional_viewcontrol_data,
56 mixed $filter_data,
57 mixed $additional_parameters
58 ): Generator {
59 $grouping_ids = $this->getAllGroupingIDs();
60
61 $records = [];
62 foreach ($grouping_ids as $grouping_id) {
63 $grouping = new ilObjCourseGrouping($grouping_id);
64 $record = [];
65
66 $record[GroupingHandler::COL_TITLE] = $grouping->getTitle();
67 $record[GroupingHandler::COL_DESCRIPTION] = $grouping->getDescription();
68
69 $link = $this->buildLinkToObject($grouping->getContainerRefId());
70 if ($link !== null) {
71 $record[GroupingHandler::COL_SOURCE] = $link;
72 }
73
74 $record[GroupingHandler::COL_UNIQUE_FIELD] = $this->lng->txt($grouping->getUniqueField());
75
76 $condition_ref_ids = [];
77 foreach ($grouping->getAssignedItems() as $condition) {
78 $condition_ref_ids[] = $condition['target_ref_id'];
79 }
80 $record[GroupingHandler::COL_ASSIGNED_OBJS] = $this->buildLinkListing(...$condition_ref_ids);
81
82 $records[$grouping->getId()] = $record;
83 }
84
85 $records = $this->sortRecords($records, $order);
86 $records = array_slice($records, $range->getStart(), $range->getLength(), true);
87
88 foreach ($records as $id => $record) {
89 yield $row_builder->buildDataRow((string) $id, $record);
90 }
91 }
92
96 public function getAllGroupingIDs(): array
97 {
98 return ilObjCourseGrouping::_getVisibleGroupings($this->content_obj_id);
99 }
100
101 public function getTotalRowCount(
102 mixed $additional_viewcontrol_data,
103 mixed $filter_data,
104 mixed $additional_parameters
105 ): ?int {
106 return count($this->getAllGroupingIDs());
107 }
108
109 protected function buildLinkToObject(int $ref_id): ?StandardLink
110 {
111 $ref_id = $this->data_factory->refId($ref_id);
112 if (ilObject::_exists($ref_id->toInt(), true)) {
113 $type = ilObject::_lookupType($ref_id->toInt(), true);
114 $title = ilObject::_lookupTitle($ref_id->toObjectId()->toInt());
115 $link = $this->static_url->builder()->build($type, $ref_id);
116 return $this->ui_factory->link()->standard($title, (string) $link);
117 }
118 return null;
119 }
120
121 protected function buildLinkListing(int ...$ref_ids): UnorderedListing
122 {
123 $links = [];
124 foreach ($ref_ids as $ref_id) {
125 $link = $this->buildLinkToObject($ref_id);
126 if ($link !== null) {
127 $links[] = $link;
128 }
129 }
130 return $this->ui_factory->listing()->unordered($links);
131 }
132
133 protected function sortRecords(array $records, Order $order): array
134 {
135 $order_field = array_keys($order->get())[0] ?? GroupingHandler::COL_TITLE;
136 $order_direction = $order->get()[$order_field] ?? Order::ASC;
137
138 $ordering_callable_without_direction = match ($order_field) {
139 GroupingHandler::COL_TITLE, GroupingHandler::COL_DESCRIPTION, GroupingHandler::COL_UNIQUE_FIELD =>
140 fn($a, $b) => ($a[$order_field] ?? '') <=> ($b[$order_field] ?? ''),
141 GroupingHandler::COL_SOURCE =>
142 fn($a, $b) => ($a[GroupingHandler::COL_SOURCE]?->getLabel() ?? '') <=> ($b[GroupingHandler::COL_SOURCE]?->getLabel() ?? ''),
143 GroupingHandler::COL_ASSIGNED_OBJS =>
144 function ($a, $b) {
145 $a_items = ($a[GroupingHandler::COL_ASSIGNED_OBJS] ?? null)?->getItems() ?? [];
146 $b_items = ($b[GroupingHandler::COL_ASSIGNED_OBJS] ?? null)?->getItems() ?? [];
147 $a_first_item_label = ($a_items[0] ?? null)?->getLabel() ?? '';
148 $b_first_item_label = ($b_items[0] ?? null)?->getLabel() ?? '';
149 return $a_first_item_label <=> $b_first_item_label;
150 }
151 };
152 $ordering_callable = fn($a, $b) => $order_direction === Order::ASC ?
153 $ordering_callable_without_direction($a, $b) :
154 $ordering_callable_without_direction($b, $a);
155
156 uasort($records, $ordering_callable);
157 return $records;
158 }
159}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(protected int $content_obj_id, protected ilLanguage $lng, protected UIFactory $ui_factory, protected DataFactory $data_factory, protected StaticURL $static_url)
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
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
Class Services.
Definition: Services.php:38
language handling
Class ilObj<module_name>
static _getVisibleGroupings(int $a_obj_id)
Returns a list of all groupings for which the current user hast write permission on all assigned obje...
Class ilObject Basic functions for all objects.
static _lookupType(int $id, bool $reference=false)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
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
buildDataRow(string $id, array $record)
$ref_id
Definition: ltiauth.php:66
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $lng
Definition: privfeed.php:31