ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilUserCertificateTableProvider.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  public const TABLE_NAME = 'il_cert_user_cert';
27 
28  public function __construct(
29  private readonly ilDBInterface $database,
30  private readonly ilLogger $logger,
31  private readonly string $defaultTitle
32  ) {
33  }
34 
40  public function fetchDataSet(int $userId, array $params, array $filter): array
41  {
42  $this->logger->debug(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
43  $additional_select = '';
44  if (
45  $this->database->tableColumnExists('il_cert_user_cert', 'background_image_path') &&
46  $this->database->tableColumnExists('il_cert_user_cert', 'tile_image_path')
47  ) {
48  $additional_select =
49  self::TABLE_NAME . '.tile_image_path,
50  ' . self::TABLE_NAME . '.tile_image_ident,';
51  }
52 
53  $sql = 'SELECT
54  ' . self::TABLE_NAME . '.id,
55  ' . self::TABLE_NAME . '.obj_type,
56  ' . self::TABLE_NAME . '.acquired_timestamp,
57  ' . self::TABLE_NAME . '.tile_image_ident,
58  ' . $additional_select . ' usr_data.firstname,
59  usr_data.lastname,
60  ' . self::TABLE_NAME . '.obj_id,
61  (CASE
62  WHEN (trans.title IS NOT NULL AND LENGTH(trans.title) > 0) THEN trans.title
63  WHEN (object_data.title IS NOT NULL AND LENGTH(object_data.title) > 0) THEN object_data.title
64  WHEN (object_data_del.title IS NOT NULL AND LENGTH(object_data_del.title) > 0) THEN object_data_del.title
65  ELSE ' . $this->database->quote($this->defaultTitle, 'text') . '
66  END
67  ) as title,
68  (CASE
69  WHEN (trans.description IS NOT NULL AND LENGTH(trans.description) > 0) THEN trans.description
70  WHEN (object_data.description IS NOT NULL AND LENGTH(object_data.description) > 0) THEN object_data.description
71  WHEN (object_data_del.description IS NOT NULL AND LENGTH(object_data_del.description) > 0) THEN object_data_del.description
72  ELSE ""
73  END
74  ) as description
75 FROM ' . self::TABLE_NAME . '
76 LEFT JOIN object_data ON object_data.obj_id = ' . self::TABLE_NAME . '.obj_id
77 LEFT JOIN object_translation trans ON trans.obj_id = object_data.obj_id
78 AND trans.lang_code = ' . $this->database->quote($params['language'], 'text') . '
79 LEFT JOIN object_data_del ON object_data_del.obj_id = ' . self::TABLE_NAME . '.obj_id
80 LEFT JOIN usr_data ON usr_data.usr_id = ' . self::TABLE_NAME . '.usr_id
81 WHERE ' . self::TABLE_NAME . '.usr_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
82 
83  if ([] !== $params) {
84  $sql .= $this->getOrderByPart($params, $filter);
85  }
86 
87  if (isset($params['limit'])) {
88  if (!is_numeric($params['limit'])) {
89  throw new InvalidArgumentException('Please provide a valid numerical limit.');
90  }
91 
92  if (!isset($params['offset'])) {
93  $params['offset'] = 0;
94  } elseif (!is_numeric($params['offset'])) {
95  throw new InvalidArgumentException('Please provide a valid numerical offset.');
96  }
97 
98  $this->database->setLimit($params['limit'], $params['offset']);
99  }
100 
101  $query = $this->database->query($sql);
102 
103  $data = [
104  'items' => [],
105  ];
106 
107  while ($row = $this->database->fetchAssoc($query)) {
108  $title = $row['title'];
109 
110  $data['items'][] = [
111  'id' => (int) $row['id'],
112  'title' => $title,
113  'obj_id' => (int) $row['obj_id'],
114  'obj_type' => $row['obj_type'],
115  'date' => (int) $row['acquired_timestamp'],
116  'tile_image_path' => $row['tile_image_path'] ?? '',
117  'tile_image_ident' => $row['tile_image_ident'],
118  'description' => $row['description'],
119  'firstname' => $row['firstname'],
120  'lastname' => $row['lastname'],
121  ];
122  }
123 
124  if (isset($params['limit'])) {
125  $cnt_sql = '
126  SELECT COUNT(*) cnt
127  FROM ' . self::TABLE_NAME . '
128  WHERE usr_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
129 
130  $row_cnt = $this->database->fetchAssoc($this->database->query($cnt_sql));
131 
132  $data['cnt'] = (int) $row_cnt['cnt'];
133 
134  $this->logger->debug(
135  sprintf(
136  'All active certificates for user: "%s" total: "%s"',
137  $userId,
138  $data['cnt']
139  )
140  );
141  } else {
142  $data['cnt'] = count($data['items']);
143  }
144 
145  $this->logger->debug(sprintf('END - Actual results: "%s"', json_encode($data, JSON_THROW_ON_ERROR)));
146 
147  return $data;
148  }
149 
150  protected function getOrderByPart(array $params, array $filter): string
151  {
152  if (isset($params['order_field'])) {
153  if (!is_string($params['order_field'])) {
154  throw new InvalidArgumentException('Please provide a valid order field.');
155  }
156 
157  if (!in_array($params['order_field'], ['date', 'id', 'title'])) {
158  throw new InvalidArgumentException('Please provide a valid order field.');
159  }
160 
161  if ($params['order_field'] === 'date') {
162  $params['order_field'] = 'acquired_timestamp';
163  }
164 
165  if (!isset($params['order_direction'])) {
166  $params['order_direction'] = 'ASC';
167  } elseif (!in_array(strtolower($params['order_direction']), ['asc', 'desc'])) {
168  throw new InvalidArgumentException('Please provide a valid order direction.');
169  }
170 
171  return ' ORDER BY ' . $params['order_field'] . ' ' . $params['order_direction'];
172  }
173 
174  return '';
175  }
176 }
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
__construct(private readonly ilDBInterface $database, private readonly ilLogger $logger, private readonly string $defaultTitle)
fetchDataSet(int $userId, array $params, array $filter)