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\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
 
16 efrain 81
 
1 www 82
 
83
 
16 efrain 84
 
1 www 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
140
     * @param int $capsule_id
141
     * @return CompanyMicrolearningUserProgress
142
     */
143
    public function fetchOneByUseridAndCapsuleId($user_id, $capsule_id)
144
    {
145
        $prototype = new CompanyMicrolearningUserProgress();
146
 
147
        $select = $this->sql->select(self::_TABLE);
148
        $select->where->equalTo('user_id', $user_id);
149
        $select->where->equalTo('capsule_id', $capsule_id);
150
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
151
        $select->limit(1);
152
 
259 efrain 153
 
154
        //echo $select->getSqlString($this->adapter->platform);
155
 
1 www 156
        return $this->executeFetchOneObject($select, $prototype);
157
    }
158
 
159
    /**
160
     *
161
     * @param int $user_id
162
     * @param int $topic_id
163
     * @return CompanyMicrolearningUserProgress
164
     */
165
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
166
    {
167
        $prototype = new CompanyMicrolearningUserProgress();
168
 
169
        $select = $this->sql->select(self::_TABLE);
170
        $select->where->equalTo('user_id', $user_id);
171
        $select->where->equalTo('topic_id', $topic_id);
172
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_TOPIC);
173
        $select->limit(1);
174
 
175
        return $this->executeFetchOneObject($select, $prototype);
176
    }
177
 
178
 
179
    /**
180
     *
181
     * @param int $user_id
182
     * @param int $capsule_id
183
     * @return int
184
     */
185
    public function fetchCountAllSlideViewedByUserIdAndCapsuleId($user_id, $capsule_id)
186
    {
187
 
188
        $select = $this->sql->select(self::_TABLE);
189
        $select->columns(['total' => new Expression('COUNT(*)')]);
190
        $select->where->equalTo('user_id', $user_id);
191
        $select->where->equalTo('capsule_id', $capsule_id);
192
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
193
        $select->limit(1);
194
 
195
        $record = $this->executeFetchOneArray($select);
196
        return $record['total'];
197
    }
198
 
199
    /**
200
     *
201
     * @param int $user_id
202
     * @param int $topic_id
203
     * @param int $capsule_id
204
     * @return int
205
     */
206
    public function fetchCountAllSlideCompletedByUserIdAndTopicIdAndCapsuleId($user_id, $topic_id, $capsule_id)
207
    {
208
 
209
        $select = $this->sql->select(self::_TABLE);
210
        $select->columns(['total' => new Expression('COUNT(*)')]);
211
        $select->where->equalTo('user_id', $user_id);
212
        $select->where->equalTo('topic_id', $topic_id);
213
        $select->where->equalTo('capsule_id', $capsule_id);
214
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
215
        $select->where->equalTo('completed', 1);
216
        $select->limit(1);
217
 
218
        $record = $this->executeFetchOneArray($select);
219
        return $record['total'];
220
    }
221
 
222
 
223
    /**
224
     *
225
     * @param int $user_id
226
     * @param int $capsule_id
227
     * @return int
228
     */
229
    public function fetchCountAllSlideCompletedByUserIdAndCapsuleId($user_id, $capsule_id)
230
    {
231
 
232
        $select = $this->sql->select(self::_TABLE);
233
        $select->columns(['total' => new Expression('COUNT(*)')]);
234
        $select->where->equalTo('user_id', $user_id);
235
        $select->where->equalTo('capsule_id', $capsule_id);
236
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
237
        $select->where->equalTo('completed', 1);
238
        $select->limit(1);
239
 
240
        //echo $select->getSqlString($this->adapter->platform);
241
 
242
        $record = $this->executeFetchOneArray($select);
243
        return $record['total'];
244
    }
245
 
246
    /**
247
     *
248
     * @param int $user_id
249
     * @param int $capsule_id
250
     * @return int
251
     */
252
    public function fetchCountAllSlideCompletedByUserIdAndTopicId($user_id, $topic_id)
253
    {
254
 
255
        $select = $this->sql->select(self::_TABLE);
256
        $select->columns(['total' => new Expression('COUNT(*)')]);
257
        $select->where->equalTo('user_id', $user_id);
258
        $select->where->equalTo('topic_id', $topic_id);
259
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
260
        $select->where->equalTo('completed', 1);
261
        $select->limit(1);
262
 
263
        echo $select->getSqlString($this->adapter->platform) . PHP_EOL;
264
 
265
        $record = $this->executeFetchOneArray($select);
266
        return $record['total'];
267
    }
268
 
269
    /**
270
     *
271
     * @param int $company_id
272
     * @param int $user_id
273
     * @return int
274
     */
275
    public function fetchCountAllSlideCompletedByCompanyIdAndUserId($company_id, $user_id)
276
    {
277
 
278
        $select = $this->sql->select(self::_TABLE);
279
        $select->columns(['total' => new Expression('COUNT(*)')]);
280
        $select->where->equalTo('company_id', $company_id);
281
        $select->where->equalTo('user_id', $user_id);
282
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
283
        $select->where->equalTo('completed', 1);
284
        $select->limit(1);
285
 
286
        $record = $this->executeFetchOneArray($select);
287
        return $record['total'];
288
    }
289
 
290
 
291
    /**
292
     *
293
     * @param int $user_id
294
     * @param int $topic_id
295
     * @return int
296
     */
297
    public function fetchCountAllSlideViewedByUserIdAndTopicId($user_id, $topic_id)
298
    {
299
 
300
        $select = $this->sql->select(self::_TABLE);
301
        $select->columns(['total' => new Expression('COUNT(*)')]);
302
        $select->where->equalTo('user_id', $user_id);
303
        $select->where->equalTo('topic_id', $topic_id);
304
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_SLIDE);
305
        $select->limit(1);
306
 
307
        $record = $this->executeFetchOneArray($select);
308
        return $record['total'];
309
    }
310
 
311
    /**
312
     *
313
     * @param CompanyMicrolearningUserProgress $userProgress
314
     * return boolean
315
     */
316
    public function insert($userProgress)
317
    {
318
        $hydrator = new ObjectPropertyHydrator();
319
        $values = $hydrator->extract($userProgress);
320
        $values = $this->removeEmpty($values);
321
 
43 efrain 322
        $values['added_on'] = $userProgress->added_on;
323
        $values['updated_on'] = $userProgress->updated_on;
324
 
1 www 325
        $insert = $this->sql->insert(self::_TABLE);
326
        $insert->values($values);
327
 
328
        //echo $insert->getSqlString($this->adapter->platform); exit;
329
 
330
        $result = $this->executeInsert($insert);
331
        if($result) {
332
            $userProgress->id = $this->lastInsertId;
333
        }
334
        return $result;
335
    }
336
 
337
 
338
    /**
339
     *
340
     * @param CompanyMicrolearningUserProgress $userProgress
341
     * return boolean
342
     */
343
    public function update($userProgress)
344
    {
345
        $hydrator = new ObjectPropertyHydrator();
346
        $values = $hydrator->extract($userProgress);
347
        $values = $this->removeEmpty($values);
348
 
349
 
43 efrain 350
        $values['added_on'] = $userProgress->added_on;
351
        $values['updated_on'] = $userProgress->updated_on;
352
 
353
 
1 www 354
        $update = $this->sql->update(self::_TABLE);
355
        $update->set($values);
356
        $update->where->equalTo('id', $userProgress->id);
357
 
358
        //echo $update->getSqlString($this->adapter->platform) . PHP_EOL;
359
 
360
        return $this->executeUpdate($update);
361
    }
362
 
363
 
364
    /**
365
     *
366
     * @param int $userId
367
     * @return int
368
     */
369
    public function getCountCapsulesCompletedByUserId($userId)
370
    {
371
        $select = $this->sql->select(self::_TABLE);
372
        $select->columns(['total' => new Expression('COUNT(*)')] );
373
        $select->where->equalTo('type', CompanyMicrolearningUserProgress::TYPE_CAPSULE);
374
        $select->where->equalTo('user_id', $userId);
375
        $select->where->equalTo('completed', 1);
376
 
377
        $record = $this->executeFetchOneArray($select);
378
 
379
        return $record['total'];
380
 
381
    }
382
 
383
 
384
 
385
}