Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use LeadersLinked\Mapper\Common\MapperCommon;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Model\CompanyMicrolearningUserProgress;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
11
 
12
 
13
class CompanyMicrolearningUserProgressMapper extends MapperCommon
14
{
15
    const _TABLE = 'tbl_company_microlearning_user_progress';
16
 
17
    /**
18
     *
19
     * @var CompanyMicrolearningUserProgressMapper
20
     */
21
    private static $_instance;
22
 
23
    /**
24
     *
25
     * @param AdapterInterface $adapter
26
     */
27
    private function __construct($adapter)
28
    {
29
        parent::__construct($adapter);
30
    }
31
 
32
    /**
33
     *
34
     * @param AdapterInterface $adapter
35
     * @return CompanyMicrolearningUserProgressMapper
36
     */
37
    public static function getInstance($adapter)
38
    {
39
        if(self::$_instance == null) {
40
            self::$_instance = new CompanyMicrolearningUserProgressMapper($adapter);
41
        }
42
        return self::$_instance;
43
    }
44
 
45
    /**
46
     *
47
     * @param int $user_id
48
     * @return CompanyMicrolearningUserProgress[]
49
     */
50
    public function fetchAllByUserId($user_id)
51
    {
52
        $prototype = new CompanyMicrolearningUserProgress();
53
 
54
        $select = $this->sql->select(self::_TABLE);
55
        $select->where->equalTo('user_id', $user_id);
56
 
57
        return $this->executeFetchAllObject($select, $prototype);
58
    }
59
 
60
    /**
61
     *
62
     * @return int[]
63
     */
64
    public function fetchAllDistinctUserIds()
65
    {
66
        $user_ids = [];
67
 
68
        $select = $this->sql->select(self::_TABLE);
69
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
70
 
71
        $records = $this->executeFetchAllArray($select);
72
        foreach ($records as $record)
73
        {
74
            array_push($user_ids, $record['user_id']);
75
        }
76
 
77
        return $user_ids;
78
 
79
    }
80
 
81
 
82
 
83
 
84
 
85
    /**
86
     *
87
     * @return CompanyMicrolearningUserProgress[]
88
     */
89
    public function fetchAllTopics()
90
    {
91
        $prototype = new CompanyMicrolearningUserProgress();
92
 
93
        $select = $this->sql->select(self::_TABLE);
94
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_TOPIC);
95
 
96
        //echo $select->getSqlString($this->adapter->platform); exit;
97
 
98
        return $this->executeFetchAllObject($select, $prototype);
99
    }
100
 
101
 
102
    /**
103
     *
104
     * @return CompanyMicrolearningUserProgress[]
105
     */
106
    public function fetchAllCapsules()
107
    {
108
        $prototype = new CompanyMicrolearningUserProgress();
109
 
110
        $select = $this->sql->select(self::_TABLE);
111
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
112
 
113
        return $this->executeFetchAllObject($select, $prototype);
114
    }
115
 
116
 
117
    /**
118
     *
119
     * @param int $user_id
120
     * @param int $slide_id
121
     * @return CompanyMicrolearningUserProgress
122
     */
123
    public function fetchOneByUserIdAndSlideId($user_id, $slide_id)
124
    {
125
        $prototype = new CompanyMicrolearningUserProgress();
126
 
127
        $select = $this->sql->select(self::_TABLE);
128
        $select->where->equalTo('user_id', $user_id);
129
        $select->where->equalTo('slide_id', $slide_id);
130
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
131
 
132
        $select->limit(1);
133
 
134
        return $this->executeFetchOneObject($select, $prototype);
135
    }
136
 
137
    /**
138
     *
139
     * @param int $user_id
119 efrain 140
     * @param int[] $capsule_ids
141
     * @return CompanyMicrolearningUserProgress
142
     */
143
    public function fetchOneLastCapsuleInProgressByUserIdAndCapsuleIds($user_id, $capsule_ids)
144
    {
145
        $capsule_ids = empty($capsule_ids) ? [0] : $capsule_ids;
146
 
147
        $prototype = new CompanyMicrolearningUserProgress();
148
 
149
        $select = $this->sql->select(self::_TABLE);
150
        $select->where->equalTo('user_id', $user_id);
151
        $select->where->in('capsule_id', $capsule_ids);
152
        $select->where->greaterThan('progress', 0);
153
        $select->where->lessThan('progress', 100);
154
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
155
        $select->order('updated_on');
156
        $select->limit(1);
157
 
158
 
159
        //echo $select->getSqlString($this->adapter->platform);
160
 
161
        return $this->executeFetchOneObject($select, $prototype);
162
    }
163
 
164
    /**
165
     *
166
     * @param int $user_id
1 efrain 167
     * @param int $capsule_id
168
     * @return CompanyMicrolearningUserProgress
169
     */
170
    public function fetchOneByUseridAndCapsuleId($user_id, $capsule_id)
171
    {
172
        $prototype = new CompanyMicrolearningUserProgress();
173
 
174
        $select = $this->sql->select(self::_TABLE);
175
        $select->where->equalTo('user_id', $user_id);
176
        $select->where->equalTo('capsule_id', $capsule_id);
177
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
178
        $select->limit(1);
179
 
180
 
181
        //echo $select->getSqlString($this->adapter->platform);
182
 
183
        return $this->executeFetchOneObject($select, $prototype);
184
    }
185
 
186
    /**
187
     *
188
     * @param int $user_id
189
     * @param int $topic_id
190
     * @return CompanyMicrolearningUserProgress
191
     */
192
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
193
    {
194
        $prototype = new CompanyMicrolearningUserProgress();
195
 
196
        $select = $this->sql->select(self::_TABLE);
197
        $select->where->equalTo('user_id', $user_id);
198
        $select->where->equalTo('topic_id', $topic_id);
199
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_TOPIC);
200
        $select->limit(1);
201
 
202
        return $this->executeFetchOneObject($select, $prototype);
203
    }
204
 
205
 
206
    /**
207
     *
208
     * @param int $user_id
119 efrain 209
     * @param int $topic_id
210
     * @return int
211
     */
212
    public function  fetchCountCapsulesStartedByIdAndTopicId($user_id, $topic_id)
213
    {
214
        $select = $this->sql->select(self::_TABLE);
215
        $select->columns(['total' => new Expression('COUNT(*)')]);
216
        $select->where->equalTo('user_id', $user_id);
217
        $select->where->equalTo('topic_id', $topic_id);
218
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
219
        $select->where->greaterThan('progress', 0);
220
        $select->where->lessThan('progress', 100);
221
        $select->where->equalTo('completed', 0);
222
        $select->limit(1);
223
 
224
        $record = $this->executeFetchOneArray($select);
225
        return $record['total'];
226
    }
227
 
228
 
229
    /**
230
     *
231
     * @param int $user_id
232
     * @param int $topic_id
233
     * @return int
234
     */
235
    public function  fetchCountCapsulesCompletedByIdAndTopicId($user_id, $topic_id)
236
    {
237
        $select = $this->sql->select(self::_TABLE);
238
        $select->columns(['total' => new Expression('COUNT(*)')]);
239
        $select->where->equalTo('user_id', $user_id);
240
        $select->where->equalTo('topic_id', $topic_id);
241
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
242
        $select->where->equalTo('completed', 1);
243
        $select->limit(1);
244
 
245
        $record = $this->executeFetchOneArray($select);
246
        return $record['total'];
247
    }
248
 
249
 
250
    /**
251
     *
252
     * @param int $user_id
253
     * @param int $topic_id
254
     * @return int
255
     */
256
    public function fetchCountCapsulesCompletedWithReturningByIdAndTopicId($user_id, $topic_id)
257
    {
258
        $select = $this->sql->select(self::_TABLE);
259
        $select->columns(['total' => new Expression('COUNT(*)')]);
260
        $select->where->equalTo('user_id', $user_id);
261
        $select->where->equalTo('topic_id', $topic_id);
262
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
263
        $select->where->equalTo('completed', 1);
264
        $select->where->notEqualTo('returning_after_completed', 0);
265
        $select->limit(1);
266
 
267
        $record = $this->executeFetchOneArray($select);
268
        return $record['total'];
269
    }
270
 
271
    /**
272
     *
273
     * @param int $user_id
274
     * @param int $topic_id
275
     * @return int
276
     */
277
    public function fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($user_id, $topic_id)
278
    {
279
        $select = $this->sql->select(self::_TABLE);
280
        $select->columns(['total' => new Expression('COUNT(*)')]);
281
        $select->where->equalTo('user_id', $user_id);
282
        $select->where->equalTo('topic_id', $topic_id);
283
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
284
        $select->where->equalTo('completed', 1);
285
        $select->where->equalTo('returning_after_completed', 0);
286
        $select->limit(1);
287
 
288
        $record = $this->executeFetchOneArray($select);
289
        return $record['total'];
290
    }
291
 
292
 
293
    /**
294
     *
295
     * @param int $user_id
1 efrain 296
     * @param int $capsule_id
297
     * @return int
298
     */
299
    public function fetchCountAllSlideViewedByUserIdAndCapsuleId($user_id, $capsule_id)
300
    {
301
 
302
        $select = $this->sql->select(self::_TABLE);
303
        $select->columns(['total' => new Expression('COUNT(*)')]);
304
        $select->where->equalTo('user_id', $user_id);
305
        $select->where->equalTo('capsule_id', $capsule_id);
306
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
307
        $select->limit(1);
308
 
309
        $record = $this->executeFetchOneArray($select);
310
        return $record['total'];
311
    }
312
 
313
    /**
314
     *
315
     * @param int $user_id
316
     * @param int $topic_id
317
     * @param int $capsule_id
318
     * @return int
319
     */
320
    public function fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($user_id, $topic_id, $capsule_id)
321
    {
322
 
323
        $select = $this->sql->select(self::_TABLE);
324
        $select->columns(['total' => new Expression('COUNT(*)')]);
325
        $select->where->equalTo('user_id', $user_id);
326
        $select->where->equalTo('topic_id', $topic_id);
327
        $select->where->equalTo('capsule_id', $capsule_id);
328
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
329
        $select->where->equalTo('completed', 1);
330
        $select->limit(1);
331
 
332
        $record = $this->executeFetchOneArray($select);
333
        return $record['total'];
334
    }
335
 
336
 
337
    /**
338
     *
339
     * @param int $user_id
340
     * @param int $capsule_id
341
     * @return int
342
     */
343
    public function fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id)
344
    {
345
 
346
        $select = $this->sql->select(self::_TABLE);
347
        $select->columns(['total' => new Expression('COUNT(*)')]);
348
        $select->where->equalTo('user_id', $user_id);
349
        $select->where->equalTo('capsule_id', $capsule_id);
350
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
351
        $select->where->equalTo('completed', 1);
352
        $select->limit(1);
353
 
354
        //echo $select->getSqlString($this->adapter->platform);
355
 
356
        $record = $this->executeFetchOneArray($select);
357
        return $record['total'];
358
    }
359
 
360
    /**
361
     *
362
     * @param int $user_id
363
     * @param int $capsule_id
364
     * @return int
365
     */
366
    public function fetchCountAllSlideCompletedByUserIdAndTopicId($user_id, $topic_id)
367
    {
368
 
369
        $select = $this->sql->select(self::_TABLE);
370
        $select->columns(['total' => new Expression('COUNT(*)')]);
371
        $select->where->equalTo('user_id', $user_id);
372
        $select->where->equalTo('topic_id', $topic_id);
373
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
374
        $select->where->equalTo('completed', 1);
375
        $select->limit(1);
376
 
377
        echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
378
 
379
        $record = $this->executeFetchOneArray($select);
380
        return $record['total'];
381
    }
382
 
383
    /**
384
     *
385
     * @param int $company_id
386
     * @param int $user_id
387
     * @return int
388
     */
389
    public function fetchCountAllSlideCompletedByCompanyIdAndUserId($company_id, $user_id)
390
    {
391
 
392
        $select = $this->sql->select(self::_TABLE);
393
        $select->columns(['total' => new Expression('COUNT(*)')]);
394
        $select->where->equalTo('company_id', $company_id);
395
        $select->where->equalTo('user_id', $user_id);
396
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
397
        $select->where->equalTo('completed', 1);
398
        $select->limit(1);
399
 
400
        $record = $this->executeFetchOneArray($select);
401
        return $record['total'];
402
    }
403
 
404
 
405
    /**
406
     *
407
     * @param int $user_id
408
     * @param int $topic_id
409
     * @return int
410
     */
411
    public function fetchCountAllSlideViewedByUserIdAndTopicId($user_id, $topic_id)
412
    {
413
 
414
        $select = $this->sql->select(self::_TABLE);
415
        $select->columns(['total' => new Expression('COUNT(*)')]);
416
        $select->where->equalTo('user_id', $user_id);
417
        $select->where->equalTo('topic_id', $topic_id);
418
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
419
        $select->limit(1);
420
 
421
        $record = $this->executeFetchOneArray($select);
422
        return $record['total'];
423
    }
424
 
425
    /**
426
     *
427
     * @param CompanyMicrolearningUserProgress $userProgress
428
     * return boolean
429
     */
430
    public function insert($userProgress)
431
    {
432
        $hydrator = new ObjectPropertyHydrator();
433
        $values = $hydrator->extract($userProgress);
434
        $values = $this->removeEmpty($values);
435
 
436
        $values['added_on'] = $userProgress->added_on;
437
        $values['updated_on'] = $userProgress->updated_on;
438
 
439
        $insert = $this->sql->insert(self::_TABLE);
440
        $insert->values($values);
441
 
442
        //echo $insert->getSqlString($this->adapter->platform); exit;
443
 
444
        $result = $this->executeInsert($insert);
445
        if($result) {
446
            $userProgress->id = $this->lastInsertId;
447
        }
448
        return $result;
449
    }
450
 
451
 
452
    /**
453
     *
454
     * @param CompanyMicrolearningUserProgress $userProgress
455
     * return boolean
456
     */
457
    public function update($userProgress)
458
    {
459
        $hydrator = new ObjectPropertyHydrator();
460
        $values = $hydrator->extract($userProgress);
461
        $values = $this->removeEmpty($values);
462
 
463
 
464
        $values['added_on'] = $userProgress->added_on;
465
        $values['updated_on'] = $userProgress->updated_on;
466
 
467
 
468
        $update = $this->sql->update(self::_TABLE);
469
        $update->set($values);
470
        $update->where->equalTo('id', $userProgress->id);
471
 
472
        //echo $update->getSqlString($this->adapter->platform) . PHP_EOL;
473
 
474
        return $this->executeUpdate($update);
475
    }
476
 
477
 
478
    /**
479
     *
480
     * @param int $userId
481
     * @return int
482
     */
483
    public function getCountCapsulesCompletedByUserId($userId)
484
    {
485
        $select = $this->sql->select(self::_TABLE);
486
        $select->columns(['total' => new Expression('COUNT(*)')] );
487
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
488
        $select->where->equalTo('user_id', $userId);
489
        $select->where->equalTo('completed', 1);
490
 
491
        $record = $this->executeFetchOneArray($select);
492
 
493
        return $record['total'];
494
 
495
    }
496
 
497
 
498
 
499
}