ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DataRetrieval.php
Go to the documentation of this file.
1 <?php
2 
20 
21 use ilLanguage;
22 use Generator;
26 use ilDateTime;
30 
34 class DataRetrieval implements I\DataRetrieval
35 {
36  private ilLanguage $lng;
37 
38  public function __construct(
39  protected bool $has_write_access,
40  ) {
41  global $DIC;
42  $this->lng = $DIC['lng'];
43  }
44 
45  public function getRows(
46  I\DataRowBuilder $row_builder,
47  array $visible_column_ids,
48  Range $range,
49  Order $order,
50  ?array $filter_data,
51  ?array $additional_parameters
52  ): Generator {
53  $records = $this->getRecords($order);
54  foreach ($records as $record) {
55  $row_id = (string) $record['id'];
56 
57  yield $row_builder->buildDataRow($row_id, $record)
58  ->withDisabledAction(Table::ACTION_DUPLICATE, !$this->has_write_access)
59  ->withDisabledAction(Table::ACTION_EDIT, !$this->has_write_access)
60  ->withDisabledAction(Table::ACTION_DELETE, !$this->has_write_access)
61  ->withDisabledAction(Table::ACTION_RESET, !$this->has_write_access);
62  }
63  }
64 
65  protected function getRecords(Order $order): array
66  {
67  $records = ilADNNotification::getArray();
68 
69  // populate with additional data
70  array_walk($records, function (array &$record): void {
71  $record['languages'] = $this->getLanguagesTextForNotification($record);
72  $record['type'] = $this->lng->txt("msg_type_" . $record['type']);
73  $record['event_start'] = $this->formatDate($record['event_start']);
74  $record['event_end'] = $this->formatDate($record['event_end']);
75  $record['display_start'] = $this->formatDate($record['display_start']);
76  $record['display_end'] = $this->formatDate($record['display_end']);
77 
78  $record['type_during_event'] = $record['permanent'] ? '' : $this->lng->txt("msg_type_" . $record['type_during_event']);
79  });
80 
81  // sort
82  [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value): array => [$key, $value]);
83  usort($records, fn($a, $b): int => $a[$order_field] <=> $b[$order_field]);
84  if ($order_direction === 'DESC') {
85  return array_reverse($records);
86  }
87 
88  return $records;
89  }
90 
91  public function getTotalRowCount(
92  ?array $filter_data,
93  ?array $additional_parameters
94  ): ?int {
95  return count(ilADNNotification::getArray());
96  }
97 
98  protected function formatDate(DateTimeImmutable $timestamp): string
99  {
100  return ilDatePresentation::formatDate(new ilDateTime($timestamp->getTimestamp(), IL_CAL_UNIX));
101  }
102 
103  protected function getLanguagesTextForNotification(array $record): string
104  {
105  $has_language_limitation = $record['has_language_limitation'];
106  $limited_to_languages = $record['limited_to_languages'];
107  // text is all by default
108  $languages_text = $this->lng->txt("all");
109  if ($has_language_limitation) {
110  // text is none in case the notification has a language limitation but no languages are specified
111  $languages_text = $this->lng->txt("none");
112  if (!empty($limited_to_languages)) {
113  $this->lng->loadLanguageModule("meta");
114  // text is comma separated list of languages if the notification has a language limitation
115  // and the languages have been specified
116  $languages_text = implode(
117  ', ',
118  array_map(
119  fn(string $lng_code): string => $this->lng->txt("meta_l_" . $lng_code),
120  $limited_to_languages
121  )
122  );
123  }
124  }
125  return $languages_text;
126  }
127 }
join($init, callable $fn)
Definition: Order.php:75
formatDate(DateTimeImmutable $timestamp)
const IL_CAL_UNIX
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
global $DIC
Definition: shib_login.php:26
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
static getArray(?string $key=null, string|array|null $values=null)
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRows(I\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
__construct(protected bool $has_write_access,)