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