ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
28
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->debug(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
79 WHEN (trans.title IS NOT NULL AND LENGTH(trans.title) > 0) THEN trans.title
80 WHEN (object_data.title IS NOT NULL AND LENGTH(object_data.title) > 0) THEN object_data.title
81 WHEN (object_data_del.title IS NOT NULL AND LENGTH(object_data_del.title) > 0) THEN object_data_del.title
82 ELSE ' . $this->database->quote($this->defaultTitle, 'text') . '
83 END
84 ) as title,
85 (CASE
86 WHEN (trans.description IS NOT NULL AND LENGTH(trans.description) > 0) THEN trans.description
87 WHEN (object_data.description IS NOT NULL AND LENGTH(object_data.description) > 0) THEN object_data.description
88 WHEN (object_data_del.description IS NOT NULL AND LENGTH(object_data_del.description) > 0) THEN object_data_del.description
89 ELSE ""
90 END
91 ) as description
92FROM il_cert_user_cert
93LEFT JOIN object_data ON object_data.obj_id = il_cert_user_cert.obj_id
94LEFT JOIN object_translation trans ON trans.obj_id = object_data.obj_id
95AND trans.lang_code = ' . $this->database->quote($params['language'], 'text') . '
96LEFT JOIN object_data_del ON object_data_del.obj_id = il_cert_user_cert.obj_id
97LEFT JOIN usr_data ON usr_data.usr_id = il_cert_user_cert.user_id
98WHERE user_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
99
100
101 if (array() !== $params) {
102 $sql .= $this->getOrderByPart($params, $filter);
103 }
104
105 if (isset($params['limit'])) {
106 if (!is_numeric($params['limit'])) {
107 throw new InvalidArgumentException('Please provide a valid numerical limit.');
108 }
109
110 if (!isset($params['offset'])) {
111 $params['offset'] = 0;
112 } else {
113 if (!is_numeric($params['offset'])) {
114 throw new InvalidArgumentException('Please provide a valid numerical offset.');
115 }
116 }
117
118 $this->database->setLimit($params['limit'], $params['offset']);
119 }
120
121 $query = $this->database->query($sql);
122
123 $data = [
124 'items' => [],
125 'cnt' => 0,
126 ];
127
128 while ($row = $this->database->fetchAssoc($query)) {
129 $title = $row['title'];
130
131 $data['items'][] = array(
132 'id' => $row['id'],
133 'title' => $title,
134 'obj_id' => $row['obj_id'],
135 'obj_type' => $row['obj_type'],
136 'date' => $row['acquired_timestamp'],
137 'thumbnail_image_path' => $row['thumbnail_image_path'],
138 'description' => $row['description'],
139 'firstname' => $row['firstname'],
140 'lastname' => $row['lastname'],
141 );
142 }
143
144 if (isset($params['limit'])) {
145 $cnt_sql = '
146 SELECT COUNT(*) cnt
147 FROM il_cert_user_cert
148 WHERE user_id = ' . $this->database->quote($userId, 'integer') . ' AND currently_active = 1';
149
150 $row_cnt = $this->database->fetchAssoc($this->database->query($cnt_sql));
151
152 $data['cnt'] = $row_cnt['cnt'];
153
154 $this->logger->debug(sprintf(
155 'All active certificates for user: "%s" total: "%s"',
156 $userId,
157 $data['cnt']
158 ));
159 } else {
160 $data['cnt'] = count($data['items']);
161 }
162
163 $this->logger->debug(sprintf('END - Actual results: "%s"', json_encode($data)));
164
165 return $data;
166 }
167
173 protected function getOrderByPart(array $params, array $filter)
174 {
175 if (isset($params['order_field'])) {
176 if (!is_string($params['order_field'])) {
177 throw new InvalidArgumentException('Please provide a valid order field.');
178 }
179
180 if (!in_array($params['order_field'], array('date', 'id', 'title'))) {
181 throw new InvalidArgumentException('Please provide a valid order field.');
182 }
183
184 if ($params['order_field'] === 'date') {
185 $params['order_field'] = 'acquired_timestamp';
186 }
187
188 if (!isset($params['order_direction'])) {
189 $params['order_direction'] = 'ASC';
190 } elseif (!in_array(strtolower($params['order_direction']), array('asc', 'desc'))) {
191 throw new InvalidArgumentException('Please provide a valid order direction.');
192 }
193
194 return ' ORDER BY ' . $params['order_field'] . ' ' . $params['order_direction'];
195 }
196
197 return '';
198 }
199}
An exception for terminatinating execution or to throw for unit testing.
This class provides processing control methods.
Component logger with individual log levels by component id.
__construct(ilDBInterface $database, ilLogger $logger, ilCtrl $controller, string $defaultTitle, ilCertificateObjectHelper $objectHelper=null)
Interface ilDBInterface.
$query
$data
Definition: storeScorm.php:23