Proyectos de Subversion LeadersLinked - Services

Rev

Rev 283 | Rev 626 | Ir a la última revisión | | Comparar con el anterior | 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\MicrolearningUserProgress;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Expression;
590 stevensc 11
use LeadersLinked\Mapper\MicrolearningSlideMapper;
12
use LeadersLinked\Mapper\MicrolearningTopicCapsuleMapper;
283 www 13
 
14
class MicrolearningUserProgressMapper extends MapperCommon
15
{
16
    const _TABLE = 'tbl_microlearning_user_progress';
17
 
18
    /**
19
     *
20
     * @var MicrolearningUserProgressMapper
21
     */
22
    private static $_instance;
23
 
24
    /**
25
     *
26
     * @param AdapterInterface $adapter
27
     */
28
    private function __construct($adapter)
29
    {
30
        parent::__construct($adapter);
31
    }
32
 
33
    /**
34
     *
35
     * @param AdapterInterface $adapter
36
     * @return MicrolearningUserProgressMapper
37
     */
38
    public static function getInstance($adapter)
39
    {
40
        if(self::$_instance == null) {
41
            self::$_instance = new MicrolearningUserProgressMapper($adapter);
42
        }
43
        return self::$_instance;
44
    }
45
 
46
    /**
47
     *
48
     * @param int $user_id
49
     * @return MicrolearningUserProgress[]
50
     */
51
    public function fetchAllByUserId($user_id)
52
    {
53
        $prototype = new MicrolearningUserProgress();
54
 
55
        $select = $this->sql->select(self::_TABLE);
56
        $select->where->equalTo('user_id', $user_id);
57
 
58
        return $this->executeFetchAllObject($select, $prototype);
59
    }
60
 
61
    /**
62
     *
63
     * @return int[]
64
     */
65
    public function fetchAllDistinctUserIds()
66
    {
67
        $user_ids = [];
68
 
69
        $select = $this->sql->select(self::_TABLE);
70
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
71
 
72
        $records = $this->executeFetchAllArray($select);
73
        foreach ($records as $record)
74
        {
75
            array_push($user_ids, $record['user_id']);
76
        }
77
 
78
        return $user_ids;
79
 
80
    }
81
 
82
 
83
 
84
 
85
 
86
    /**
87
     *
88
     * @return MicrolearningUserProgress[]
89
     */
90
    public function fetchAllTopics()
91
    {
92
        $prototype = new MicrolearningUserProgress();
93
 
94
        $select = $this->sql->select(self::_TABLE);
95
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
96
 
97
        //echo $select->getSqlString($this->adapter->platform); exit;
98
 
99
        return $this->executeFetchAllObject($select, $prototype);
100
    }
101
 
102
 
103
    /**
104
     *
105
     * @return MicrolearningUserProgress[]
106
     */
107
    public function fetchAllCapsules()
108
    {
109
        $prototype = new MicrolearningUserProgress();
110
 
111
        $select = $this->sql->select(self::_TABLE);
112
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
113
 
114
        return $this->executeFetchAllObject($select, $prototype);
115
    }
116
 
117
 
118
    /**
119
     *
120
     * @param int $user_id
121
     * @param int $slide_id
122
     * @return MicrolearningUserProgress
123
     */
124
    public function fetchOneByUserIdAndSlideId($user_id, $slide_id)
125
    {
126
        $prototype = new MicrolearningUserProgress();
127
 
128
        $select = $this->sql->select(self::_TABLE);
129
        $select->where->equalTo('user_id', $user_id);
130
        $select->where->equalTo('slide_id', $slide_id);
131
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
132
 
133
        $select->limit(1);
134
 
135
        return $this->executeFetchOneObject($select, $prototype);
136
    }
590 stevensc 137
 
138
    /**
139
     * Fetch all progress data by user id and slide id
140
     * @param int $userId
141
     * @param int $slideId
142
     * @return MicrolearningUserProgress[]
143
     */
144
    public function fetchAllProgressDataByUserIdAndSlideId($userId, $slideId)
145
    {
146
        $select = $this->sql->select();
147
        $select->from(self::_TABLE);
148
        $select->where->equalTo('user_id', $userId);
149
        $select->where->equalTo('slide_id', $slideId);
150
    }
283 www 151
 
152
    /**
153
     *
154
     * @param int $user_id
155
     * @param int[] $capsule_ids
156
     * @return MicrolearningUserProgress
157
     */
158
    public function fetchOneLastCapsuleInProgressByUserIdAndCapsuleIds($user_id, $capsule_ids)
159
    {
160
        $capsule_ids = empty($capsule_ids) ? [0] : $capsule_ids;
161
 
162
        $prototype = new MicrolearningUserProgress();
163
 
164
        $select = $this->sql->select(self::_TABLE);
165
        $select->where->equalTo('user_id', $user_id);
166
        $select->where->in('capsule_id', $capsule_ids);
167
        $select->where->greaterThan('progress', 0);
168
        $select->where->lessThan('progress', 100);
169
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
170
        $select->order('updated_on');
171
        $select->limit(1);
172
 
173
 
174
        //echo $select->getSqlString($this->adapter->platform);
175
 
176
        return $this->executeFetchOneObject($select, $prototype);
177
    }
178
 
179
    /**
180
     *
181
     * @param int $user_id
182
     * @param int $capsule_id
183
     * @return MicrolearningUserProgress
184
     */
185
    public function fetchOneByUseridAndCapsuleId($user_id, $capsule_id)
186
    {
187
        $prototype = new MicrolearningUserProgress();
188
 
189
        $select = $this->sql->select(self::_TABLE);
190
        $select->where->equalTo('user_id', $user_id);
191
        $select->where->equalTo('capsule_id', $capsule_id);
192
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
193
        $select->limit(1);
194
 
195
 
196
        //echo $select->getSqlString($this->adapter->platform);
197
 
198
        return $this->executeFetchOneObject($select, $prototype);
199
    }
200
 
201
    /**
202
     *
203
     * @param int $user_id
204
     * @param int $topic_id
205
     * @return MicrolearningUserProgress
206
     */
207
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
208
    {
209
        $prototype = new MicrolearningUserProgress();
210
 
211
        $select = $this->sql->select(self::_TABLE);
212
        $select->where->equalTo('user_id', $user_id);
213
        $select->where->equalTo('topic_id', $topic_id);
214
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_TOPIC);
215
        $select->limit(1);
216
 
217
        return $this->executeFetchOneObject($select, $prototype);
218
    }
219
 
220
 
221
    /**
222
     *
223
     * @param int $user_id
224
     * @param int $topic_id
225
     * @return int
226
     */
227
    public function  fetchCountCapsulesStartedByIdAndTopicId($user_id, $topic_id)
228
    {
229
        $select = $this->sql->select(self::_TABLE);
230
        $select->columns(['total' => new Expression('COUNT(*)')]);
231
        $select->where->equalTo('user_id', $user_id);
232
        $select->where->equalTo('topic_id', $topic_id);
233
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
234
        $select->where->greaterThan('progress', 0);
235
        $select->where->lessThan('progress', 100);
236
        $select->where->equalTo('completed', 0);
237
        $select->limit(1);
238
 
239
        $record = $this->executeFetchOneArray($select);
240
        return $record['total'];
241
    }
242
 
243
 
244
    /**
245
     *
246
     * @param int $user_id
247
     * @param int $topic_id
248
     * @return int
249
     */
250
    public function  fetchCountCapsulesCompletedByIdAndTopicId($user_id, $topic_id)
251
    {
252
        $select = $this->sql->select(self::_TABLE);
253
        $select->columns(['total' => new Expression('COUNT(*)')]);
254
        $select->where->equalTo('user_id', $user_id);
255
        $select->where->equalTo('topic_id', $topic_id);
256
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
257
        $select->where->equalTo('completed', 1);
258
        $select->limit(1);
259
 
260
        $record = $this->executeFetchOneArray($select);
261
        return $record['total'];
262
    }
263
 
264
 
265
 
266
 
267
    /**
268
     *
269
     * @param int $user_id
270
     * @param int $topic_id
271
     * @return int
272
     */
273
    public function fetchCountCapsulesCompletedWithReturningByIdAndTopicId($user_id, $topic_id)
274
    {
275
        $select = $this->sql->select(self::_TABLE);
276
        $select->columns(['total' => new Expression('COUNT(*)')]);
277
        $select->where->equalTo('user_id', $user_id);
278
        $select->where->equalTo('topic_id', $topic_id);
279
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
280
        $select->where->equalTo('completed', 1);
281
        $select->where->notEqualTo('returning_after_completed', 0);
282
        $select->limit(1);
283
 
284
        $record = $this->executeFetchOneArray($select);
285
        return $record['total'];
286
    }
287
 
288
    /**
289
     *
290
     * @param int $user_id
291
     * @param int $topic_id
292
     * @return int
293
     */
294
    public function fetchCountCapsulesCompletedWithoutReturningByIdAndTopicId($user_id, $topic_id)
295
    {
296
        $select = $this->sql->select(self::_TABLE);
297
        $select->columns(['total' => new Expression('COUNT(*)')]);
298
        $select->where->equalTo('user_id', $user_id);
299
        $select->where->equalTo('topic_id', $topic_id);
300
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
301
        $select->where->equalTo('completed', 1);
302
        $select->where->equalTo('returning_after_completed', 0);
303
        $select->limit(1);
304
 
305
        $record = $this->executeFetchOneArray($select);
306
        return $record['total'];
307
    }
308
 
309
 
310
    /**
311
     *
312
     * @param int $user_id
313
     * @param int $capsule_id
314
     * @return int
315
     */
316
    public function fetchCountAllSlideViewedByUserIdAndCapsuleId($user_id, $capsule_id)
317
    {
318
 
319
        $select = $this->sql->select(self::_TABLE);
320
        $select->columns(['total' => new Expression('COUNT(*)')]);
321
        $select->where->equalTo('user_id', $user_id);
322
        $select->where->equalTo('capsule_id', $capsule_id);
323
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
324
        $select->limit(1);
325
 
326
        $record = $this->executeFetchOneArray($select);
327
        return $record['total'];
328
    }
329
 
330
    /**
331
     *
332
     * @param int $user_id
333
     * @param int $topic_id
334
     * @param int $capsule_id
335
     * @return int
336
     */
337
    public function fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($user_id, $topic_id, $capsule_id)
338
    {
339
 
340
        $select = $this->sql->select(self::_TABLE);
341
        $select->columns(['total' => new Expression('COUNT(*)')]);
342
        $select->where->equalTo('user_id', $user_id);
343
        $select->where->equalTo('topic_id', $topic_id);
344
        $select->where->equalTo('capsule_id', $capsule_id);
345
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
346
        $select->where->equalTo('completed', 1);
347
        $select->limit(1);
348
 
349
        $record = $this->executeFetchOneArray($select);
350
        return $record['total'];
351
    }
352
 
353
 
354
    /**
355
     *
356
     * @param int $user_id
357
     * @param int $capsule_id
358
     * @return int
359
     */
360
    public function fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id)
361
    {
362
 
363
        $select = $this->sql->select(self::_TABLE);
364
        $select->columns(['total' => new Expression('COUNT(*)')]);
365
        $select->where->equalTo('user_id', $user_id);
366
        $select->where->equalTo('capsule_id', $capsule_id);
367
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
368
        $select->where->equalTo('completed', 1);
369
        $select->limit(1);
370
 
371
        //echo $select->getSqlString($this->adapter->platform);
372
 
373
        $record = $this->executeFetchOneArray($select);
374
        return $record['total'];
375
    }
376
 
377
    /**
378
     *
379
     * @param int $user_id
380
     * @param int $capsule_id
381
     * @return int
382
     */
383
    public function fetchCountAllSlideCompletedByUserIdAndTopicId($user_id, $topic_id)
384
    {
385
 
386
        $select = $this->sql->select(self::_TABLE);
387
        $select->columns(['total' => new Expression('COUNT(*)')]);
388
        $select->where->equalTo('user_id', $user_id);
389
        $select->where->equalTo('topic_id', $topic_id);
390
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
391
        $select->where->equalTo('completed', 1);
392
        $select->limit(1);
393
 
394
       // echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
395
 
396
        $record = $this->executeFetchOneArray($select);
397
        return $record['total'];
398
    }
399
 
400
    /**
401
     *
402
     * @param int $company_id
403
     * @param int $user_id
404
     * @return int
405
     */
406
    public function fetchCountAllSlideCompletedByCompanyIdAndUserId($company_id, $user_id)
407
    {
408
 
409
        $select = $this->sql->select(self::_TABLE);
410
        $select->columns(['total' => new Expression('COUNT(*)')]);
411
        $select->where->equalTo('company_id', $company_id);
412
        $select->where->equalTo('user_id', $user_id);
413
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
414
        $select->where->equalTo('completed', 1);
415
        $select->limit(1);
416
 
417
        $record = $this->executeFetchOneArray($select);
418
        return $record['total'];
419
    }
420
 
421
 
422
    /**
423
     *
424
     * @param int $user_id
425
     * @param int $topic_id
426
     * @return int
427
     */
428
    public function fetchCountAllSlideViewedByUserIdAndTopicId($user_id, $topic_id)
429
    {
430
 
431
        $select = $this->sql->select(self::_TABLE);
432
        $select->columns(['total' => new Expression('COUNT(*)')]);
433
        $select->where->equalTo('user_id', $user_id);
434
        $select->where->equalTo('topic_id', $topic_id);
435
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
436
        $select->limit(1);
437
 
438
        $record = $this->executeFetchOneArray($select);
439
        return $record['total'];
440
    }
441
 
442
    /**
443
     *
444
     * @param MicrolearningUserProgress $userProgress
445
     * return boolean
446
     */
447
    public function insert($userProgress)
448
    {
449
        $hydrator = new ObjectPropertyHydrator();
450
        $values = $hydrator->extract($userProgress);
451
        $values = $this->removeEmpty($values);
452
 
453
        $values['added_on'] = $userProgress->added_on;
454
        $values['updated_on'] = $userProgress->updated_on;
455
 
456
        $insert = $this->sql->insert(self::_TABLE);
457
        $insert->values($values);
458
 
459
        //echo $insert->getSqlString($this->adapter->platform); exit;
460
 
461
        $result = $this->executeInsert($insert);
462
        if($result) {
463
            $userProgress->id = $this->lastInsertId;
464
        }
465
        return $result;
466
    }
467
 
468
 
469
    /**
470
     *
471
     * @param MicrolearningUserProgress $userProgress
472
     * return boolean
473
     */
474
    public function update($userProgress)
475
    {
476
        $hydrator = new ObjectPropertyHydrator();
477
        $values = $hydrator->extract($userProgress);
478
        $values = $this->removeEmpty($values);
479
 
480
 
481
        $values['added_on'] = $userProgress->added_on;
482
        $values['updated_on'] = $userProgress->updated_on;
483
 
484
 
485
        $update = $this->sql->update(self::_TABLE);
486
        $update->set($values);
487
        $update->where->equalTo('id', $userProgress->id);
488
 
489
        //echo $update->getSqlString($this->adapter->platform) . PHP_EOL;
490
 
491
        return $this->executeUpdate($update);
492
    }
493
 
494
 
495
    /**
496
     *
497
     * @param int $userId
498
     * @return int
499
     */
500
    public function getCountCapsulesCompletedByUserId($userId)
501
    {
502
        $select = $this->sql->select(self::_TABLE);
503
        $select->columns(['total' => new Expression('COUNT(*)')] );
504
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
505
        $select->where->equalTo('user_id', $userId);
506
        $select->where->equalTo('completed', 1);
507
 
508
        $record = $this->executeFetchOneArray($select);
509
 
510
        return $record['total'];
511
 
512
    }
513
 
590 stevensc 514
    public function markSlideViewed($user_id, $topic_id, $capsule_id, $slide_id)
515
    {
516
        $data = [
517
            'user_id'    => $user_id,
518
            'topic_id'   => $topic_id,
519
            'capsule_id' => $capsule_id,
520
            'slide_id'   => $slide_id,
521
            'viewed_at'  => date('Y-m-d H:i:s'),
522
        ];
283 www 523
 
590 stevensc 524
        // Verifica si ya existe el registro
525
        $exists = $this->fetchOneByUserIdAndSlideId($user_id, $slide_id);
526
        if ($exists) {
527
            return; // ya registrado
528
        }
283 www 529
 
590 stevensc 530
        return $this->insert($data);
531
    }
532
 
533
    public function markCapsuleCompleted($user_id, $topic_id, $capsule_id)
534
    {
535
        $data = [
536
            'user_id'       => $user_id,
537
            'topic_id'      => $topic_id,
538
            'capsule_id'    => $capsule_id,
539
            'completed_at'  => date('Y-m-d H:i:s'),
540
        ];
541
 
542
        // Verifica si ya existe algún progreso de cápsula
543
        $exists = $this->fetchOneByUserIdAndCapsuleId($user_id, $capsule_id);
544
        if ($exists) {
545
            return; // ya registrado
546
        }
547
 
548
        return $this->insert($data);
549
    }
550
 
551
    public function markTopicCompleted($user_id, $topic_id)
552
    {
553
        $data = [
554
            'user_id'      => $user_id,
555
            'topic_id'     => $topic_id,
556
            'topic_closed' => 1,
557
            'closed_at'    => date('Y-m-d H:i:s'),
558
        ];
559
 
560
        // Verifica si ya existe algún progreso de topic
561
        $exists = $this->fetchOneByUserIdAndTopicId($user_id, $topic_id);
562
        if ($exists) {
563
            return; // ya registrado
564
        }
565
 
566
        return $this->insert($data);
567
    }
568
 
569
    public function hasViewedAllSlidesInCapsule($user_id, $capsule_id)
570
    {
571
        $slideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
572
        $allSlides = $slideMapper->fetchAllByCapsuleId($capsule_id);
573
 
574
        $select = $this->sql->select(self::_TABLE);
575
        $select->where->equalTo('user_id', $user_id);
576
        $select->where->equalTo('capsule_id', $capsule_id);
577
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_SLIDE);
578
        $select->where->equalTo('viewed_at', 1);
579
 
580
        $record = $this->executeFetchOneArray($select);
581
        return count($record) >= count($allSlides);
582
    }
583
 
584
    public function hasCompletedAllCapsulesInTopic($user_id, $topic_id)
585
    {
586
        $capsuleMapper = MicrolearningTopicCapsuleMapper::getInstance($this->adapter);
587
        $allCapsules = $capsuleMapper->fetchAllByTopicId($topic_id);
588
 
589
        $select = $this->sql->select(self::_TABLE);
590
        $select->where->equalTo('user_id', $user_id);
591
        $select->where->equalTo('topic_id', $topic_id);
592
        $select->where->equalTo('type', MicrolearningUserProgress::TYPE_CAPSULE);
593
        $select->where->equalTo('completed', 1);
594
 
595
        $record = $this->executeFetchOneArray($select);
596
        return count($record) >= count($allCapsules);
597
    }
598
 
283 www 599
}