628 |
stevensc |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Mapper;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Sql\Expression;
|
|
|
7 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
8 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
9 |
use LeadersLinked\Model\MicrolearningTopicUser;
|
|
|
10 |
use LeadersLinked\Mapper\Common\MapperCommon;
|
|
|
11 |
|
|
|
12 |
class MicrolearningTopicUserMapper extends MapperCommon
|
|
|
13 |
{
|
|
|
14 |
const _TABLE = 'tbl_microlearning_topic_users';
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
*
|
|
|
18 |
* @var MicrolearningTopicUserMapper
|
|
|
19 |
*/
|
|
|
20 |
private static $_instance;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
*
|
|
|
24 |
* @param AdapterInterface $adapter
|
|
|
25 |
*/
|
|
|
26 |
private function __construct($adapter)
|
|
|
27 |
{
|
|
|
28 |
parent::__construct($adapter);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
*
|
|
|
33 |
* @param AdapterInterface $adapter
|
|
|
34 |
* @return MicrolearningTopicUserMapper
|
|
|
35 |
*/
|
|
|
36 |
public static function getInstance($adapter)
|
|
|
37 |
{
|
|
|
38 |
if(self::$_instance == null) {
|
|
|
39 |
self::$_instance = new MicrolearningTopicUserMapper($adapter);
|
|
|
40 |
}
|
|
|
41 |
return self::$_instance;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
*
|
|
|
46 |
* @param int $company_id
|
|
|
47 |
* @param int $topic_id
|
|
|
48 |
* return int
|
|
|
49 |
*/
|
|
|
50 |
public function fetchCountUsersByCompanyIdAndTopicId($company_id, $topic_id)
|
|
|
51 |
{
|
|
|
52 |
|
|
|
53 |
$select = $this->sql->select();
|
|
|
54 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
55 |
$select->from(self::_TABLE);
|
|
|
56 |
$select->where->equalTo('company_id', $company_id);
|
|
|
57 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
58 |
|
|
|
59 |
$record = $this->executeFetchOneArray($select);
|
|
|
60 |
return $record['total'];
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
*
|
|
|
65 |
* @param int $company_id
|
|
|
66 |
* @param int $user_id
|
|
|
67 |
* return int
|
|
|
68 |
*/
|
|
|
69 |
public function fetchCountByCompanyIdAndUserId($company_id, $user_id)
|
|
|
70 |
{
|
|
|
71 |
|
|
|
72 |
$select = $this->sql->select();
|
|
|
73 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
74 |
$select->from(self::_TABLE);
|
|
|
75 |
$select->where->equalTo('company_id', $company_id);
|
|
|
76 |
$select->where->equalTo('user_id', $user_id);
|
|
|
77 |
|
|
|
78 |
$record = $this->executeFetchOneArray($select);
|
|
|
79 |
return $record['total'];
|
|
|
80 |
}
|
646 |
stevensc |
81 |
|
|
|
82 |
/**
|
|
|
83 |
*
|
|
|
84 |
* @param int $company_id
|
|
|
85 |
* @param int $topic_id
|
|
|
86 |
* @param int $user_id
|
|
|
87 |
* @return int
|
|
|
88 |
*/
|
|
|
89 |
public function fetchCountByCompanyIdAndTopicIdAndUserId($company_id, $topic_id, $user_id)
|
|
|
90 |
{
|
|
|
91 |
$select = $this->sql->select();
|
|
|
92 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
93 |
$select->from(self::_TABLE);
|
|
|
94 |
$select->where->equalTo('company_id', $company_id);
|
|
|
95 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
96 |
$select->where->equalTo('user_id', $user_id);
|
|
|
97 |
|
|
|
98 |
$record = $this->executeFetchOneArray($select);
|
|
|
99 |
return $record['total'];
|
|
|
100 |
}
|
628 |
stevensc |
101 |
|
|
|
102 |
/**
|
|
|
103 |
*
|
|
|
104 |
* @param int $company_id
|
|
|
105 |
* @param int $topic_id
|
|
|
106 |
* return int
|
|
|
107 |
*/
|
|
|
108 |
public function fetchCountUsersActiveByCompanyIdAndTopicId($company_id, $topic_id)
|
|
|
109 |
{
|
|
|
110 |
$now = date('Y-m-d H:i:s');
|
|
|
111 |
|
|
|
112 |
$select = $this->sql->select();
|
|
|
113 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
114 |
$select->from(self::_TABLE);
|
|
|
115 |
$select->where->equalTo('company_id', $company_id);
|
|
|
116 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
117 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
118 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
119 |
->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
|
|
|
120 |
|
|
|
121 |
//echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
122 |
|
|
|
123 |
$record = $this->executeFetchOneArray($select);
|
|
|
124 |
return $record['total'];
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
*
|
|
|
129 |
* @param int $company_id
|
|
|
130 |
* @param int $topic_id
|
|
|
131 |
* return int
|
|
|
132 |
*/
|
|
|
133 |
public function fetchCountByCompanyIdAndTopicId($company_id, $topic_id)
|
|
|
134 |
{
|
|
|
135 |
$select = $this->sql->select();
|
|
|
136 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
137 |
$select->from(self::_TABLE);
|
|
|
138 |
$select->where->equalTo('company_id', $company_id);
|
|
|
139 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
140 |
$record = $this->executeFetchOneArray($select);
|
|
|
141 |
return $record['total'];
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
*
|
|
|
147 |
* @param int $company_id
|
|
|
148 |
* @param string $date;
|
|
|
149 |
* return []
|
|
|
150 |
*/
|
|
|
151 |
public function fetchAllDistinctUserIdActiveByCompanyIdAndDate($company_id, $date)
|
|
|
152 |
{
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
$select = $this->sql->select();
|
|
|
156 |
$select->from(self::_TABLE);
|
|
|
157 |
$select->columns(['user_id' => new Expression('DISTINCT(user_id)') ]);
|
|
|
158 |
$select->where->equalTo('company_id', $company_id);
|
|
|
159 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
160 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
161 |
->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
|
|
|
162 |
->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
|
|
|
163 |
|
|
|
164 |
$user_ids = [];
|
|
|
165 |
$records = $this->executeFetchAllArray($select);
|
|
|
166 |
|
|
|
167 |
foreach($records as $record)
|
|
|
168 |
{
|
|
|
169 |
array_push($user_ids, $record[ 'user_id' ]);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return $user_ids;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
*
|
|
|
178 |
* @param int $user_id
|
|
|
179 |
* return MicrolearningTopicUser[]
|
|
|
180 |
*/
|
|
|
181 |
public function fetchAllActiveByUserId($user_id)
|
|
|
182 |
{
|
|
|
183 |
$now = date('Y-m-d H:i:s');
|
|
|
184 |
|
|
|
185 |
$prototype = new MicrolearningTopicUser();
|
|
|
186 |
$select = $this->sql->select();
|
|
|
187 |
$select->from(self::_TABLE);
|
|
|
188 |
$select->where->equalTo('user_id', $user_id);
|
|
|
189 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
190 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
191 |
->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
|
|
|
192 |
|
|
|
193 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
*
|
|
|
199 |
* @param int $user_id
|
|
|
200 |
* return MicrolearningTopicUser[]
|
|
|
201 |
*/
|
|
|
202 |
public function fetchAllByUserId($user_id)
|
|
|
203 |
{
|
|
|
204 |
|
|
|
205 |
$prototype = new MicrolearningTopicUser();
|
|
|
206 |
$select = $this->sql->select();
|
|
|
207 |
$select->from(self::_TABLE);
|
|
|
208 |
$select->where->equalTo('user_id', $user_id);
|
|
|
209 |
|
|
|
210 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
*
|
|
|
216 |
* @param int $topic_id
|
|
|
217 |
* @param string $date;
|
|
|
218 |
* return MicrolearningTopicUser[]
|
|
|
219 |
*/
|
|
|
220 |
public function fetchAllActiveByTopicIdAndDate($topic_id, $date)
|
|
|
221 |
{
|
|
|
222 |
|
|
|
223 |
$prototype = new MicrolearningTopicUser();
|
|
|
224 |
$select = $this->sql->select();
|
|
|
225 |
$select->from(self::_TABLE);
|
|
|
226 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
227 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
228 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
229 |
->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
|
|
|
230 |
->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
|
|
|
231 |
|
|
|
232 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
*
|
|
|
237 |
* @param int $topic_id
|
|
|
238 |
* @param int $user_id
|
|
|
239 |
* @param string $date;
|
|
|
240 |
* return MicrolearningTopicUser
|
|
|
241 |
*/
|
|
|
242 |
public function fetchOneActiveByTopicIdAndDate($topic_id, $user_id, $date)
|
|
|
243 |
{
|
|
|
244 |
|
|
|
245 |
$prototype = new MicrolearningTopicUser();
|
|
|
246 |
$select = $this->sql->select();
|
|
|
247 |
$select->from(self::_TABLE);
|
|
|
248 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
249 |
$select->where->equalTo('user_id', $user_id);
|
|
|
250 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
251 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
252 |
->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
|
|
|
253 |
->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
|
|
|
254 |
|
|
|
255 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
*
|
|
|
260 |
* @param int $company_id
|
|
|
261 |
* return int[]
|
|
|
262 |
*/
|
|
|
263 |
public function fetchAllUserIdsForTopicsActiveByCompanyId($company_id)
|
|
|
264 |
{
|
|
|
265 |
$now = date('Y-m-d H:i:s');
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
$select = $this->sql->select();
|
|
|
269 |
$select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
|
|
|
270 |
$select->from(self::_TABLE);
|
|
|
271 |
$select->where->equalTo('company_id', $company_id);
|
|
|
272 |
$select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
|
|
|
273 |
->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
|
|
|
274 |
->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
$user_ids = [];
|
|
|
278 |
$records = $this->executeFetchAllArray($select);
|
|
|
279 |
foreach($records as $record)
|
|
|
280 |
{
|
|
|
281 |
array_push($user_ids, $record['user_id']);
|
|
|
282 |
}
|
|
|
283 |
return $user_ids;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
/**
|
|
|
288 |
*
|
|
|
289 |
* @param int $id
|
|
|
290 |
* return MicrolearningTopicUser
|
|
|
291 |
*/
|
|
|
292 |
public function fetchOne($id)
|
|
|
293 |
{
|
|
|
294 |
$prototype = new MicrolearningTopicUser();
|
|
|
295 |
$select = $this->sql->select();
|
|
|
296 |
$select->from(self::_TABLE);
|
|
|
297 |
$select->where->equalTo('id', $id);
|
|
|
298 |
|
|
|
299 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
*
|
|
|
305 |
* @param int $user_id
|
|
|
306 |
* @param int $topic_id
|
|
|
307 |
* return MicrolearningTopicUser
|
|
|
308 |
*/
|
|
|
309 |
public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
|
|
|
310 |
{
|
|
|
311 |
$prototype = new MicrolearningTopicUser();
|
|
|
312 |
$select = $this->sql->select();
|
|
|
313 |
$select->from(self::_TABLE);
|
|
|
314 |
$select->where->equalTo('user_id', $user_id);
|
|
|
315 |
$select->where->equalTo('topic_id', $topic_id);
|
|
|
316 |
|
|
|
317 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
318 |
}
|
|
|
319 |
|
632 |
stevensc |
320 |
/**
|
|
|
321 |
* Fetch one topic-user relationship for a given user ID and company ID.
|
|
|
322 |
*
|
|
|
323 |
* @param int $user_id
|
|
|
324 |
* @param int $company_id
|
|
|
325 |
* @return int
|
|
|
326 |
*/
|
|
|
327 |
public function fetchOneByUserIdAndCompanyId($user_id, $company_id)
|
|
|
328 |
{
|
|
|
329 |
$prototype = new MicrolearningTopicUser();
|
|
|
330 |
$select = $this->sql->select();
|
|
|
331 |
$select->from(self::_TABLE);
|
|
|
332 |
$select->where->equalTo('user_id', $user_id);
|
|
|
333 |
$select->where->equalTo('company_id', $company_id);
|
|
|
334 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
335 |
}
|
628 |
stevensc |
336 |
|
|
|
337 |
|
|
|
338 |
/**
|
|
|
339 |
*
|
|
|
340 |
* @param MicrolearningTopicUser $microlearningTopicUser
|
|
|
341 |
* @return boolean
|
|
|
342 |
*/
|
|
|
343 |
public function insert($microlearningTopicUser)
|
|
|
344 |
{
|
|
|
345 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
346 |
$values = $hydrator->extract($microlearningTopicUser);
|
|
|
347 |
$values = $this->removeEmpty($values);
|
|
|
348 |
|
|
|
349 |
$insert = $this->sql->insert(self::_TABLE);
|
|
|
350 |
$insert->values($values);
|
|
|
351 |
|
|
|
352 |
$response = $this->executeInsert($insert);
|
|
|
353 |
if($response) {
|
|
|
354 |
$microlearningTopicUser->id = $this->lastInsertId;
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
return $response;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
*
|
|
|
362 |
* @param MicrolearningTopicUser $microlearningTopicUser
|
|
|
363 |
* @return boolean
|
|
|
364 |
*/
|
|
|
365 |
public function update($microlearningTopicUser)
|
|
|
366 |
{
|
|
|
367 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
368 |
$values = $hydrator->extract($microlearningTopicUser);
|
|
|
369 |
$values = $this->removeEmpty($values);
|
|
|
370 |
|
|
|
371 |
$update = $this->sql->update(self::_TABLE);
|
|
|
372 |
$update->set($values);
|
|
|
373 |
$update->where->equalTo('id', $microlearningTopicUser->id);
|
|
|
374 |
|
|
|
375 |
return $this->executeUpdate($update);
|
|
|
376 |
}
|
|
|
377 |
|
|
|
378 |
}
|