Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 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\MicrolearningUserLog;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
11
use Laminas\Db\ResultSet\HydratingResultSet;
12
use Laminas\Paginator\Adapter\DbSelect;
13
use Laminas\Paginator\Paginator;
14
 
15
 
16
class MicrolearningUserLogMapper extends MapperCommon
17
{
18
    const _TABLE = 'tbl_microlearning_user_log';
19
 
20
    /**
21
     *
22
     * @var MicrolearningUserLogMapper
23
     */
24
    private static $_instance;
25
 
26
    /**
27
     *
28
     * @param AdapterInterface $adapter
29
     */
30
    private function __construct($adapter)
31
    {
32
        parent::__construct($adapter);
33
    }
34
 
35
    /**
36
     *
37
     * @param AdapterInterface $adapter
38
     * @return MicrolearningUserLogMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new MicrolearningUserLogMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @param int $user_id
51
     * @return MicrolearningUserLog[]
52
     */
53
    public function fetchLast20ByUserId($user_id)
54
    {
55
        $prototype = new MicrolearningUserLog();
56
 
57
        $select = $this->sql->select(self::_TABLE);
58
        $select->where->equalTo('user_id', $user_id);
59
        $select->order(['id desc']);
60
        $select->limit(20);
61
 
62
        return $this->executeFetchAllObject($select, $prototype);
63
    }
64
 
65
 
66
    /**
67
     *
68
     * @param int $user_id
69
     * @return MicrolearningUserLog[]
70
     */
71
    public function fetchAllByUserId($user_id)
72
    {
73
        $prototype = new MicrolearningUserLog();
74
 
75
        $select = $this->sql->select(self::_TABLE);
76
        $select->where->equalTo('user_id', $user_id);
77
        $select->order(['id desc']);
78
 
79
        return $this->executeFetchAllObject($select, $prototype);
80
    }
81
 
82
    /**
83
     *
84
     * @param int $user_id
85
     * @return MicrolearningUserLog
86
     */
87
    public function fetchLastBy($user_id)
88
    {
89
        $prototype = new MicrolearningUserLog();
90
 
91
        $select = $this->sql->select(self::_TABLE);
92
        $select->where->equalTo('user_id', $user_id);
93
        $select->order(['added_on desc']);
94
        $select->limit(1);
95
 
96
        return $this->executeFetchOneObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $user_id
102
     * @return MicrolearningUserLog[]
103
     */
104
    public function fetchBatchLastByUserId($user_id, $max_records = 20)
105
    {
106
        $prototype = new MicrolearningUserLog();
107
 
108
        $select = $this->sql->select(self::_TABLE);
109
        $select->where->equalTo('user_id', $user_id);
110
        $select->order(['id desc']);
111
        $select->limit($max_records);
112
 
113
        return $this->executeFetchAllObject($select, $prototype);
114
    }
115
 
116
    /**
117
     *
118
     * @param int $company_id
119
     * @param int $user_id
120
     * @return string
121
     */
122
    public function fetchFirstDateByCompanyIdAndUserId($company_id, $user_id)
123
    {
124
        $select = $this->sql->select();
125
        $select->columns(['date' => new Expression('MIN(added_on)') ] );
126
        $select->from(self::_TABLE);
127
        $select->where->equalTo('company_id', $company_id);
128
        $select->where->equalTo('user_id', $user_id);
129
        $select->where->in('activity', [
130
            MicrolearningUserLog::ACTIVITY_START_TOPIC,
131
            MicrolearningUserLog::ACTIVITY_START_CAPSULE,
132
            MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
133
            MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
134
            MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
135
            MicrolearningUserLog::ACTIVITY_APPROVED_TEST,
136
            MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
137
            MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
138
        ]);
139
 
140
        $record = $this->executeFetchOneArray($select);
141
        return empty($record['date']) ? '' :   $record['date'];
142
    }
143
 
144
    /**
145
     *
146
     * @param int $company_id
147
     * @param int $user_id
148
     * @return string
149
     */
150
    public function fetchLastDateByCompanyIdAndUserId($company_id, $user_id)
151
    {
152
        $select = $this->sql->select();
153
        $select->columns(['date' => new Expression('MAX(added_on)') ] );
154
        $select->from(self::_TABLE);
155
        $select->where->equalTo('company_id', $company_id);
156
        $select->where->equalTo('user_id', $user_id);
157
        $select->where->in('activity', [
158
            MicrolearningUserLog::ACTIVITY_START_TOPIC,
159
            MicrolearningUserLog::ACTIVITY_START_CAPSULE,
160
            MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
161
            MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
162
            MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
163
            MicrolearningUserLog::ACTIVITY_APPROVED_TEST,
164
            MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
165
            MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
166
        ]);
167
 
168
        $record = $this->executeFetchOneArray($select);
169
        return empty($record['date']) ? '' :  $record['date'];
170
    }
171
 
172
    /**
173
     *
174
     * @param int $company_id
175
     * @param string $min
176
     * @param string $max
177
     * @return int[]
178
     */
179
    public function fetchAllUserIdsLastWeekByCompanyId($company_id, $min, $max)
180
    {
181
 
182
        $select = $this->sql->select();
183
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)') ]);
184
        $select->from(self::_TABLE);
185
 
186
        $select->where->equalTo('company_id', $company_id);
187
        $select->where->in('activity', [
188
            MicrolearningUserLog::ACTIVITY_START_TOPIC,
189
            MicrolearningUserLog::ACTIVITY_START_CAPSULE,
190
            MicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
191
            MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
192
            MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
193
            MicrolearningUserLog::ACTIVITY_APPROVED_TEST,
194
            MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
195
            MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
196
        ]);
197
        $select->where->between(new Expression('added_on'), $min, $max);
198
 
199
 
200
        $user_ids = [];
201
        $records = $this->executeFetchAllArray($select);
202
        foreach($records as $record)
203
        {
204
            array_push($user_ids, $record['user_id']);
205
        }
206
        return $user_ids;
207
 
208
    }
209
 
210
    /**
211
     *
212
     * @param MicrolearningUserLog $userLog
213
     * return true
214
     */
215
    public function insert($userLog)
216
    {
217
        $hydrator = new ObjectPropertyHydrator();
218
        $values = $hydrator->extract($userLog);
219
        $values = $this->removeEmpty($values);
220
 
221
        $values['added_on'] = $userLog->added_on;
222
 
223
        $insert = $this->sql->insert(self::_TABLE);
224
        $insert->values($values);
225
 
226
        //echo $insert->getSqlString($this->adapter->platform); exit;
227
 
228
        $result = $this->executeInsert($insert);
229
        if($result) {
230
            $userLog->id = $this->lastInsertId;
231
        }
232
        return $result;
233
    }
234
 
235
 
236
    /**
237
     *
238
     * @param int $user_id
239
     * @param int $topic_id
240
     * @return int
241
     */
242
    public function fetchCountTotalCountViewSlideForUserIdAndTopic($user_id, $topic_id)
243
    {
244
        $select = $this->sql->select(self::_TABLE);
245
        $select->columns(['total' => new Expression('COUNT(DISTINCT(slide_id))')]);
246
        $select->where->equalTo('user_id', $user_id);
247
        $select->where->equalTo('topic_id', $topic_id);
248
        $select->where->equalTo('activity', 'VIEW-SLIDE');
249
 
250
        $record = $this->executeFetchOneArray($select);
251
        return $record['total'];
252
    }
253
 
254
 
255
    /**
256
     *
257
     * @param int $user_id
258
     * @param int $topic_id
259
     * @param int $capsule_id
260
     * @return int
261
     */
262
    public function fetchCountTotalCountViewSlideForUserIdAndTopicAndCapsuleId($user_id, $topic_id, $capsule_id)
263
    {
264
        $select = $this->sql->select(self::_TABLE);
265
        $select->columns(['total' => new Expression('COUNT(DISTINCT(slide_id))')]);
266
        $select->where->equalTo('user_id', $user_id);
267
        $select->where->equalTo('topic_id', $topic_id);
268
        $select->where->equalTo('capsule_id', $capsule_id);
269
        $select->where->equalTo('activity', 'VIEW-SLIDE');
270
 
271
        $record = $this->executeFetchOneArray($select);
272
        return $record['total'];
273
    }
274
 
275
    /**
276
     *
277
     * @param int $user_id
278
     * @param int $page
279
     * @param int $records_per_page
280
     * @return Paginator
281
     */
282
    public function getAllMessagesPaginatorByUserId($user_id, $page = 1, $records_per_page = 10)
283
    {
284
        $select = $this->sql->select(self::_TABLE);
285
        $select->where->equalTo('user_id', $user_id);
286
        $select->order('id DESC');
287
 
288
 
289
        //echo $select->getSqlString($this->adapter->getPlatform()); exit;
290
 
291
        $prototype  = new MicrolearningUserLog();
292
        $hydrator   = new ObjectPropertyHydrator();
293
        $resultset  = new HydratingResultSet($hydrator, $prototype);
294
 
295
        $adapter    = new DbSelect($select, $this->sql, $resultset);
296
        $paginator  = new Paginator($adapter);
297
        $paginator->setCurrentPageNumber($page);
298
        $paginator->setItemCountPerPage($records_per_page);
299
 
300
        return $paginator;
301
    }
302
 
303
 
304
    /**
305
     *
306
     * @param int $user_id
307
     * @param int $company_id
308
     * @param int $page
309
     * @param int $records_per_page
310
     * @return Paginator
311
     */
312
    public function getAllMessagesPaginatorByUserIdAndCompanyId($user_id, $company_id, $page = 1, $records_per_page = 10)
313
    {
314
        $select = $this->sql->select(self::_TABLE);
315
        $select->where->nest()
316
            ->equalTo('user_id', $user_id)
317
            ->in('activity', [
318
                MicrolearningUserLog::ACTIVITY_SIGNIN,
319
                MicrolearningUserLog::ACTIVITY_SIGNOUT,
320
            ])
321
        ->unnest()->or->nest()
322
            ->equalTo('user_id', $user_id)
323
            ->equalTo('company_id', $company_id)
324
            ->in('activity', [
325
                MicrolearningUserLog::ACTIVITY_APPROVED_TEST,
326
                MicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
327
                MicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
328
                MicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
329
                MicrolearningUserLog::ACTIVITY_START_CAPSULE,
330
                MicrolearningUserLog::ACTIVITY_START_TOPIC,
331
                MicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
332
                MicrolearningUserLog::ACTIVITY_VIEW_SLIDE
333
            ])
334
        ->unnest();
335
        $select->order('id DESC');
336
 
337
 
338
        //echo $select->getSqlString($this->adapter->getPlatform()); exit;
339
 
340
        $prototype  = new MicrolearningUserLog();
341
        $hydrator   = new ObjectPropertyHydrator();
342
        $resultset  = new HydratingResultSet($hydrator, $prototype);
343
 
344
        $adapter    = new DbSelect($select, $this->sql, $resultset);
345
        $paginator  = new Paginator($adapter);
346
        $paginator->setCurrentPageNumber($page);
347
        $paginator->setItemCountPerPage($records_per_page);
348
 
349
        return $paginator;
350
    }
351
 
352
    /**
353
     *
354
     * @param  int $company_id
355
     * @return string
356
     */
357
    public function fetchOneMaxDateActivityFromCompanyId($company_id)
358
    {
359
 
360
 
361
        $select = $this->sql->select();
362
        $select->columns(['date' => new Expression('DATE(MAX(added_on))') ] );
363
        $select->from(self::_TABLE);
364
        $select->where->equalTo('company_id', $company_id);
365
 
366
 
367
        $record = $this->executeFetchOneArray($select);
368
        return empty($record['date']) ? '' :   $record['date'];
369
    }
370
 
371
    /**
372
     *
373
     * @param int $user_id
374
     * @param int $slide_id;
375
     * @return string
376
     */
377
    public function fetchOneTakeATestByUserIdAndSlideId($user_id, $slide_id,)
378
    {
379
        $prototype = new MicrolearningUserLog();
380
 
381
        $select = $this->sql->select();
382
        $select->from(self::_TABLE);
383
        $select->where->equalTo('user_id', $user_id);
384
        $select->where->equalTo('slide_id', $slide_id);
385
        $select->where->equalTo('activity', MicrolearningUserLog::ACTIVITY_TAKE_A_TEST);
386
 
387
        return $this->executeFetchOneObject($select, $prototype);
388
    }
389
 
390
 
391
 
392
    /**
393
     *
394
     * @param int $company_id
395
     * @param string $start_date
396
     * @param string $end_date
397
     * @return array
398
     */
399
    public function fetchAllCountClosedCapsulesDailyByCompanyIdAndStartDateAndEndDate($company_id, $start_date, $end_date)
400
    {
401
        $select = $this->sql->select();
402
        $select->columns(['total' => new Expression('COUNT(*)'), 'date' =>  new Expression('DATE(added_on)') ] );
403
        $select->from(self::_TABLE);
404
        $select->where->equalTo('company_id', $company_id);
405
        $select->where->between(new Expression('DATE(added_on)'), $start_date, $end_date);
406
        $select->group('date');
407
        $select->order('date desc');
408
 
409
       // echo $select->getSqlString($this->adapter->platform); exit;
410
 
411
        return $this->executeFetchAllArray($select);
412
    }
413
 
414
    /**
415
     *
416
     * @param int $company_id
417
     * @param string $start_date
418
     * @param string $end_date
419
     * @return array
420
     */
421
    public function fetchAllCountUsersWithClosedCapsulesDailyByCompanyIdAndStartDateAndEndDate($company_id, $start_date, $end_date)
422
    {
423
        $select = $this->sql->select();
424
        $select->columns(['total' => new Expression('COUNT(DISTINCT(user_id))'), 'date' =>  new Expression('DATE(added_on)') ] );
425
        $select->from(self::_TABLE);
426
        $select->where->equalTo('company_id', $company_id);
427
        $select->where->between(new Expression('DATE(added_on)'), $start_date, $end_date);
428
        $select->group('date');
429
        $select->order('date desc');
430
 
431
 
432
 
433
        return $this->executeFetchAllArray($select);
434
    }
435
 
436
    /*
437
 
438
select count(distinct(user_id)) as c, date(added_on) as d
439
from tbl_microlearning_user_log
440
where company_id  = 1 and activity  = 'completed-capsule'
441
and date(added_on) BETWEEN  '2023-06-09'  and '2023-07-09'
442
group by d
443
order by d desc;
444
 
445
        select max(date(added_on)) from
446
        tbl_microlearning_user_log
447
        where company_id  = 1;
448
 
449
        select count(*) as c, date(added_on) as d
450
        from tbl_microlearning_user_log
451
        where company_id  = 1 and activity  = 'completed-capsule'
452
        and date(added_on) BETWEEN  '2023-06-09'  and '2023-07-09'
453
        group by d
454
        order by d desc
455
 
456
        select user_id, count(*) as c
457
        from tbl_microlearning_user_log
458
        where company_id  = 1 and activity  = 'completed-capsule'
459
        and date(added_on) BETWEEN  '2023-06-09'  and '2023-07-09'
460
        group by user_id
461
        order by c desc;
462
    */
463
 
464
 
465
 
466
}