Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 43 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 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\CompanyMicrolearningUserLog;
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 CompanyMicrolearningUserLogMapper extends MapperCommon
17
{
18
    const _TABLE = 'tbl_company_microlearning_user_log';
19
 
20
    /**
21
     *
22
     * @var CompanyMicrolearningUserLogMapper
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 CompanyMicrolearningUserLogMapper
39
     */
40
    public static function getInstance($adapter)
41
    {
42
        if(self::$_instance == null) {
43
            self::$_instance = new CompanyMicrolearningUserLogMapper($adapter);
44
        }
45
        return self::$_instance;
46
    }
47
 
48
    /**
49
     *
50
     * @param int $user_id
51
     * @return CompanyMicrolearningUserLog[]
52
     */
53
    public function fetchLast20ByUserId($user_id)
54
    {
55
        $prototype = new CompanyMicrolearningUserLog();
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
 
54 efrain 65
 
1 www 66
    /**
67
     *
68
     * @param int $user_id
54 efrain 69
     * @return CompanyMicrolearningUserLog[]
70
     */
71
    public function fetchAllByUserId($user_id)
72
    {
73
        $prototype = new CompanyMicrolearningUserLog();
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
16 efrain 85
     * @return CompanyMicrolearningUserLog
86
     */
87
    public function fetchLastBy($user_id)
88
    {
89
        $prototype = new CompanyMicrolearningUserLog();
90
 
91
        $select = $this->sql->select(self::_TABLE);
92
        $select->where->equalTo('user_id', $user_id);
42 efrain 93
        $select->order(['added_on desc']);
16 efrain 94
        $select->limit(1);
95
 
96
        return $this->executeFetchOneObject($select, $prototype);
97
    }
98
 
99
    /**
100
     *
101
     * @param int $user_id
1 www 102
     * @return CompanyMicrolearningUserLog[]
103
     */
104
    public function fetchBatchLastByUserId($user_id, $max_records = 20)
105
    {
106
        $prototype = new CompanyMicrolearningUserLog();
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
            CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
131
            CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
132
            CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
133
            CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
134
            CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
135
            CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
136
            CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
137
            CompanyMicrolearningUserLog::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
            CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
159
            CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
160
            CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
161
            CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
162
            CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
163
            CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
164
            CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
165
            CompanyMicrolearningUserLog::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
            CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
189
            CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
190
            CompanyMicrolearningUserLog::ACTIVITY_VIEW_SLIDE,
191
            CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
192
            CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
193
            CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
194
            CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
195
            CompanyMicrolearningUserLog::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 CompanyMicrolearningUserLog $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
 
43 efrain 221
        $values['added_on'] = $userLog->added_on;
222
 
1 www 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 CompanyMicrolearningUserLog();
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
                CompanyMicrolearningUserLog::ACTIVITY_SIGNIN,
319
                CompanyMicrolearningUserLog::ACTIVITY_SIGNOUT,
320
            ])
321
        ->unnest()->or->nest()
322
            ->equalTo('user_id', $user_id)
323
            ->equalTo('company_id', $company_id)
324
            ->in('activity', [
325
                CompanyMicrolearningUserLog::ACTIVITY_APPROVED_TEST,
326
                CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_CAPSULE,
327
                CompanyMicrolearningUserLog::ACTIVITY_COMPLETED_TOPIC,
328
                CompanyMicrolearningUserLog::ACTIVITY_RETAKE_A_TEST,
329
                CompanyMicrolearningUserLog::ACTIVITY_START_CAPSULE,
330
                CompanyMicrolearningUserLog::ACTIVITY_START_TOPIC,
331
                CompanyMicrolearningUserLog::ACTIVITY_TAKE_A_TEST,
332
                CompanyMicrolearningUserLog::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 CompanyMicrolearningUserLog();
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
}