ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilBadgePersonalTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
31 use ILIAS\Data\URI;
37 
39 {
40  private readonly Factory $factory;
41  private readonly Renderer $renderer;
43  private readonly ilLanguage $lng;
44  private readonly ilGlobalTemplateInterface $tpl;
45  private readonly ILIAS\DI\Container $dic;
46  private readonly ilObjUser $user;
47  private readonly ilAccessHandler $access;
48  private readonly Tile $tile;
61  private ?array $cached_records = null;
62 
63  public function __construct()
64  {
65  global $DIC;
66  $this->dic = $DIC;
67  $this->lng = $DIC->language();
68  $this->tpl = $DIC->ui()->mainTemplate();
69  $this->factory = $DIC->ui()->factory();
70  $this->renderer = $DIC->ui()->renderer();
71  $this->request = $DIC->http()->request();
72  $this->user = $DIC->user();
73  $this->access = $DIC->access();
74  $this->tile = new Tile($DIC);
75  }
76 
77  public function getRows(
78  DataRowBuilder $row_builder,
79  array $visible_column_ids,
80  Range $range,
81  Order $order,
82  ?array $filter_data,
83  ?array $additional_parameters
84  ): Generator {
85  $records = $this->getRecords();
86 
87  if ($order) {
88  [$order_field, $order_direction] = $order->join(
89  [],
90  fn($ret, $key, $value) => [$key, $value]
91  );
92 
93  usort($records, static function (array $left, array $right) use ($order_field): int {
94  if (in_array($order_field, ['title', 'awarded_by'], true)) {
95  if (in_array($order_field, ['title', 'awarded_by'], true)) {
96  $order_field .= '_sortable';
97  }
98 
99  return ilStr::strCmp(
100  $left[$order_field],
101  $right[$order_field]
102  );
103  }
104 
105  if ($order_field === 'active') {
106  return $right[$order_field] <=> $left[$order_field];
107  }
108 
109  return $left[$order_field] <=> $right[$order_field];
110  });
111 
112  if ($order_direction === Order::DESC) {
113  $records = array_reverse($records);
114  }
115  }
116 
117  if ($range) {
118  $records = \array_slice($records, $range->getStart(), $range->getLength());
119  }
120 
121  foreach ($records as $record) {
122  yield $row_builder->buildDataRow((string) $record['id'], $record);
123  }
124  }
125 
126  public function getTotalRowCount(
127  ?array $filter_data,
128  ?array $additional_parameters
129  ): ?int {
130  return count($this->getRecords());
131  }
132 
145  private function getRecords(): array
146  {
147  if ($this->cached_records !== null) {
148  return $this->cached_records;
149  }
150 
151  $rows = [];
152  $a_user_id = $this->user->getId();
153 
154  foreach (ilBadgeAssignment::getInstancesByUserId($a_user_id) as $ass) {
155  $badge = new ilBadge($ass->getBadgeId());
156 
157  $parent = null;
158  if ($badge->getParentId()) {
159  $parent = $badge->getParentMeta();
160  if ($parent['type'] === 'bdga') {
161  $parent = null;
162  }
163  }
164 
165  $awarded_by = '';
166  $awarded_by_sortable = '';
167  if ($parent !== null) {
168  $ref_ids = ilObject::_getAllReferences($parent['id']);
169  $ref_id = current($ref_ids);
170 
171  $awarded_by = $parent['title'];
172  $awarded_by_sortable = $parent['title'];
173  if ($ref_id && $this->access->checkAccess('read', '', $ref_id)) {
174  $awarded_by = $this->renderer->render(
175  new Standard(
176  $awarded_by,
177  (string) new URI(ilLink::_getLink($ref_id))
178  )
179  );
180  }
181 
182  $awarded_by = implode(' ', [
183  $this->renderer->render(
184  $this->factory->symbol()->icon()->standard(
185  $parent['type'],
186  $parent['title']
187  )
188  ),
189  $awarded_by
190  ]);
191  }
192 
193  $rows[] = [
194  'id' => $badge->getId(),
195  'image' => $this->renderer->render(
196  $this->tile->asImage(
197  $this->tile->modalContentWithAssignment($badge, $ass),
198  ilBadgeImage::IMAGE_SIZE_XS
199  )
200  ),
201  'title' => $this->renderer->render(
202  $this->tile->asTitle(
203  $this->tile->modalContentWithAssignment($badge, $ass)
204  )
205  ),
206  'title_sortable' => $badge->getTitle(),
207  'badge_issued_on' => (new DateTimeImmutable())
208  ->setTimestamp($ass->getTimestamp())
209  ->setTimezone(new DateTimeZone($this->user->getTimeZone())),
210  'awarded_by' => $awarded_by,
211  'awarded_by_sortable' => $awarded_by_sortable,
212  'active' => (bool) $ass->getPosition()
213  ];
214  }
215 
216  $this->cached_records = $rows;
217 
218  return $rows;
219  }
220 
224  private function getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format): array
225  {
226  return [
227  'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
228  'title' => $this->factory->table()->column()->text($this->lng->txt('title')),
229  'awarded_by' => $this->factory->table()->column()->text($this->lng->txt('awarded_by')),
230  'badge_issued_on' => $this->factory->table()->column()->date(
231  $this->lng->txt('badge_issued_on'),
232  $date_format
233  ),
234  'active' => $this->factory->table()->column()->boolean(
235  $this->lng->txt('badge_in_profile'),
236  $this->lng->txt('yes'),
237  $this->lng->txt('no')
238  )->withOrderingLabels(
239  $this->lng->txt('badge_sort_added_to_profile_first'),
240  $this->lng->txt('badge_sort_excluded_from_profile_first')
241  )
242  ];
243  }
244 
248  protected function getActions(
249  URLBuilder $url_builder,
250  URLBuilderToken $action_parameter_token,
251  URLBuilderToken $row_id_token
252  ): array {
253  return [
254  'obj_badge_activate' => $this->factory->table()->action()->multi(
255  $this->lng->txt('badge_add_to_profile'),
256  $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'),
257  $row_id_token
258  ),
259  'obj_badge_deactivate' =>
260  $this->factory->table()->action()->multi(
261  $this->lng->txt('badge_remove_from_profile'),
262  $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'),
263  $row_id_token
264  )
265  ];
266  }
267 
268  public function renderTable(): void
269  {
270  $df = new \ILIAS\Data\Factory();
271  if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
272  $date_format = $df->dateFormat()->withTime12($this->user->getDateFormat());
273  } else {
274  $date_format = $df->dateFormat()->withTime24($this->user->getDateFormat());
275  }
276 
277  $table_uri = $df->uri($this->request->getUri()->__toString());
278  $url_builder = new URLBuilder($table_uri);
279  $query_params_namespace = ['badge'];
280 
281  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
282  $query_params_namespace,
283  'table_action',
284  'id',
285  );
286 
287  $table = $this->factory
288  ->table()
289  ->data(
290  $this,
291  $this->lng->txt('badge_personal_badges'),
292  $this->getColumns($date_format),
293  )
294  ->withId(self::class)
295  ->withOrder(new Order('title', Order::ASC))
296  ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
297  ->withRequest($this->request);
298 
299  $pres = new PresentationHeader($this->dic, ilBadgeProfileGUI::class);
300  $pres->show($this->lng->txt('table_view'));
301 
302  $this->tpl->setContent($this->renderer->render($table));
303  }
304 }
join($init, callable $fn)
Definition: Order.php:75
getTotalRowCount(?array $filter_data, ?array $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, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
static getInstancesByUserId(int $a_user_id)
Interface Observer Contains several chained tasks and infos about them.
factory()
static _getAllReferences(int $id)
get all reference ids for object ID
renderer()
getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format)
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
buildDataRow(string $id, array $record)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
readonly ServerRequestInterface RequestInterface $request
This is how the factory for UI elements looks.
Definition: Factory.php:37
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
global $DIC
Definition: shib_login.php:22
static strCmp(string $a, string $b)
Definition: class.ilStr.php:90
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
readonly ILIAS DI Container $dic
URLBuilder.
Definition: URLBuilder.php:40
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
readonly ilGlobalTemplateInterface $tpl