ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilUserCertificateTableProvider.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 {
12  private $database;
13 
17  private $logger;
18 
22  private $controller;
23 
27  private $objectHelper;
28 
32  private $defaultTitle;
33 
41  public function __construct(
45  string $defaultTitle,
47  ) {
48  $this->database = $database;
49  $this->logger = $logger;
50  $this->controller = $controller;
51  $this->defaultTitle = $defaultTitle;
52  ;
53 
54  if (null === $objectHelper) {
56  }
57  $this->objectHelper = $objectHelper;
58  }
59 
66  public function fetchDataSet($userId, $params, $filter)
67  {
68  $this->logger->info(sprintf('START - Fetching all active certificates for user: "%s"', $userId));
69 
70  $sql = 'SELECT
71  il_cert_user_cert.id,
72  il_cert_user_cert.obj_type,
73  il_cert_user_cert.thumbnail_image_path,
74  il_cert_user_cert.acquired_timestamp,
75  usr_data.firstname,
76  usr_data.lastname,
77  il_cert_user_cert.obj_id,
78  (CASE WHEN (object_data.title IS NULL OR LENGTH(object_data.title) = 0)
79  THEN
80  CASE WHEN (object_data_del.title IS NULL OR LENGTH(object_data_del.title) = 0)
81  THEN ' . $this->database->quote($this->defaultTitle, 'text') . '
82  ELSE object_data_del.title
83  END
84  ELSE object_data.title
85  END
86  ) as title,
87  (CASE WHEN (object_data.description IS NULL OR LENGTH(object_data.description) = 0)
88  THEN
89  CASE WHEN (object_data_del.description IS NULL OR LENGTH(object_data_del.description) = 0)
90  THEN ""
91  ELSE object_data_del.description
92  END
93  ELSE object_data.description
94  END
95  ) as description
96 FROM il_cert_user_cert
97 LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
98 LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
99 LEFT JOIN usr_data ON usr_data.usr_id = il_cert_user_cert.user_id
100 WHERE user_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
101 
102 
103  if (array() !== $params) {
104  $sql .= $this->getOrderByPart($params, $filter);
105  }
106 
107  if (isset($params['limit'])) {
108  if (!is_numeric($params['limit'])) {
109  throw new InvalidArgumentException('Please provide a valid numerical limit.');
110  }
111 
112  if (!isset($params['offset'])) {
113  $params['offset'] = 0;
114  } else {
115  if (!is_numeric($params['offset'])) {
116  throw new InvalidArgumentException('Please provide a valid numerical offset.');
117  }
118  }
119 
120  $this->database->setLimit($params['limit'], $params['offset']);
121  }
122 
123  $query = $this->database->query($sql);
124 
125  $data = [
126  'items' => [],
127  'cnt' => 0,
128  ];
129 
130  while ($row = $this->database->fetchAssoc($query)) {
131  $title = $row['title'];
132 
133  $data['items'][] = array(
134  'id' => $row['id'],
135  'title' => $title,
136  'obj_id' => $row['obj_id'],
137  'obj_type' => $row['obj_type'],
138  'date' => $row['acquired_timestamp'],
139  'thumbnail_image_path' => $row['thumbnail_image_path'],
140  'description' => $row['description'],
141  'firstname' => $row['firstname'],
142  'lastname' => $row['lastname'],
143  );
144  }
145 
146  if (isset($params['limit'])) {
147  $cnt_sql = '
148  SELECT COUNT(*) cnt
149  FROM il_cert_user_cert
150  WHERE user_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
151 
152  $row_cnt = $this->database->fetchAssoc($this->database->query($cnt_sql));
153 
154  $data['cnt'] = $row_cnt['cnt'];
155 
156  $this->logger->info(sprintf(
157  'All active certificates for user: "%s" total: "%s"',
158  $userId,
159  $data['cnt']
160  ));
161  } else {
162  $data['cnt'] = count($data['items']);
163  }
164 
165  $this->logger->debug(sprintf('END - Actual results:', json_encode($data)));
166 
167  return $data;
168  }
169 
175  protected function getOrderByPart(array $params, array $filter)
176  {
177  if (isset($params['order_field'])) {
178  if (!is_string($params['order_field'])) {
179  throw new InvalidArgumentException('Please provide a valid order field.');
180  }
181 
182  if (!in_array($params['order_field'], array('date', 'id', 'title'))) {
183  throw new InvalidArgumentException('Please provide a valid order field.');
184  }
185 
186  if ($params['order_field'] == 'date') {
187  $params['order_field'] = 'acquired_timestamp';
188  }
189 
190  if (!isset($params['order_direction'])) {
191  $params['order_direction'] = 'ASC';
192  } elseif (!in_array(strtolower($params['order_direction']), array('asc', 'desc'))) {
193  throw new InvalidArgumentException('Please provide a valid order direction.');
194  }
195 
196  return ' ORDER BY ' . $params['order_field'] . ' ' . $params['order_direction'];
197  }
198 
199  return '';
200  }
201 }
This class provides processing control methods.
$query
$row
__construct(ilDBInterface $database, ilLogger $logger, ilCtrl $controller, string $defaultTitle, ilCertificateObjectHelper $objectHelper=null)
Component logger with individual log levels by component id.
$data
Definition: bench.php:6