Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 280 | Ir a la última revisión | | 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\CompanyMicrolearningCapsuleUser;
9
use Laminas\Db\Sql\Expression;
10
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
11
 
12
 
13
 
14
class CompanyMicrolearningCapsuleUserMapper extends MapperCommon
15
{
16
    const _TABLE = 'tbl_company_microlearning_capsule_users';
17
 
18
    /**
19
     *
20
     * @var CompanyMicrolearningCapsuleUserMapper
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 CompanyMicrolearningCapsuleUserMapper
37
     */
38
    public static function getInstance($adapter)
39
    {
40
        if(self::$_instance == null) {
41
            self::$_instance = new CompanyMicrolearningCapsuleUserMapper($adapter);
42
        }
43
        return self::$_instance;
44
    }
45
 
46
    /**
47
     *
48
     * @param int $company_id
49
     * @param int $topic_id
50
     * @param int $capsule_id
51
     * return int
52
     */
53
    public function fetchCountUsersByCompanyIdAndTopicIdAndCapsuleId($company_id, $topic_id, $capsule_id)
54
    {
55
 
56
        $select = $this->sql->select();
57
        $select->columns(['total' => new Expression('COUNT(*)')]);
58
        $select->from(self::_TABLE);
59
        $select->where->equalTo('company_id', $company_id);
60
        $select->where->equalTo('topic_id', $topic_id);
61
        $select->where->equalTo('capsule_id', $capsule_id);
62
 
63
        $record = $this->executeFetchOneArray($select);
64
        return $record['total'];
65
    }
66
 
67
    /**
68
     *
69
     * @param int $company_id
70
     * @param int $topic_id
71
     * @param int $user_id
72
     * return int
73
     */
74
    public function fetchCountByCompanyIdAndTopicIdAndUserId($company_id, $topic_id, $user_id)
75
    {
76
 
77
        $select = $this->sql->select();
78
        $select->columns(['total' => new Expression('COUNT(*)')]);
79
        $select->from(self::_TABLE);
80
        $select->where->equalTo('company_id', $company_id);
81
        $select->where->equalTo('topic_id', $topic_id);
82
        $select->where->equalTo('user_id', $user_id);
83
 
84
        $record = $this->executeFetchOneArray($select);
85
        return $record['total'];
86
    }
87
 
88
    /**
89
     *
90
     * @param int $company_id
91
     * @param int $user_id
92
     * return int
93
     */
94
    public function fetchCountByCompanyIdAndUserId($company_id,  $user_id)
95
    {
96
 
97
        $select = $this->sql->select();
98
        $select->columns(['total' => new Expression('COUNT(*)')]);
99
        $select->from(self::_TABLE);
100
        $select->where->equalTo('company_id', $company_id);
101
        $select->where->equalTo('user_id', $user_id);
102
 
103
        $record = $this->executeFetchOneArray($select);
104
        return $record['total'];
105
    }
106
 
107
 
108
    /**
109
     *
110
     * @param int $company_id
111
     * @param int $topic_id
112
     * @param int $capsule_id
113
     * return int
114
     */
115
    public function fetchCountUsersActiveByCompanyIdAndTopicIdAndCapsuleId($company_id, $topic_id, $capsule_id)
116
    {
117
        $now = date('Y-m-d H:i:s');
118
 
119
        $select = $this->sql->select();
120
        $select->columns(['total' => new Expression('COUNT(*)')]);
121
        $select->from(self::_TABLE);
122
        $select->where->equalTo('company_id', $company_id);
123
        $select->where->equalTo('topic_id', $topic_id);
124
        $select->where->equalTo('capsule_id', $capsule_id);
125
        $select->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
126
        ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
127
        ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
128
 
129
        //echo $select->getSqlString($this->adapter->platform); exit;
130
 
131
        $record = $this->executeFetchOneArray($select);
132
        return $record['total'];
133
    }
134
 
135
 
136
    /**
137
     *
138
     * @param int $company_id
139
     * @param string $date;
140
     * return []
141
     */
142
    public function fetchAllDistinctUserIdActiveByCompanyIdAndDate($company_id, $date)
143
    {
144
 
145
 
146
        $select = $this->sql->select();
147
        $select->from(self::_TABLE);
148
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)') ]);
149
        $select->where->equalTo('company_id', $company_id);
150
        $select->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
151
        ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
152
        ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
153
        ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
154
 
155
 
156
        $user_ids = [];
157
        $records = $this->executeFetchAllArray($select);
158
 
159
        foreach($records as $record)
160
        {
161
            array_push($user_ids, $record[ 'user_id' ]);
162
        }
163
 
164
 
165
        return $user_ids;
166
    }
167
 
168
 
169
    /**
170
     *
171
     * @param int $user_id
172
     * return CompanyMicrolearningCapsuleUser[]
173
     */
174
    public function fetchAllActiveByUserId($user_id)
175
    {
176
        $now = date('Y-m-d H:i:s');
177
 
178
        $prototype = new CompanyMicrolearningCapsuleUser();
179
        $select = $this->sql->select();
180
        $select->from(self::_TABLE);
181
        $select->where->equalTo('user_id', $user_id);
182
        $select->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
183
            ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
184
            ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
185
 
259 efrain 186
        //echo $select->getSqlString($this->adapter->platform);
1 www 187
 
188
        return $this->executeFetchAllObject($select, $prototype);
189
    }
190
 
191
 
192
    /**
193
     *
194
     * @param int $user_id
195
     * return CompanyMicrolearningCapsuleUser[]
196
     */
197
    public function fetchAllByUserId($user_id)
198
    {
199
 
200
        $prototype = new CompanyMicrolearningCapsuleUser();
201
        $select = $this->sql->select();
202
        $select->from(self::_TABLE);
203
        $select->where->equalTo('user_id', $user_id);
204
 
205
        //echo $select->getSqlString($this->adapter->platform); exit;
206
 
207
        return $this->executeFetchAllObject($select, $prototype);
208
    }
209
 
210
 
211
    /**
212
     *
213
     * @param int $user_id
214
     * @param string $date;
215
     * return CompanyMicrolearningCapsuleUser[]
216
     */
217
    public function fetchAllActiveByCapsuleIdAndDate($capsule_id, $date)
218
    {
219
 
220
        $prototype = new CompanyMicrolearningCapsuleUser();
221
        $select = $this->sql->select();
222
        $select->from(self::_TABLE);
223
        $select->where->equalTo('capsule_id', $capsule_id);
224
        $select->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
225
        ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
226
        ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
227
        ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
228
 
229
        //echo $select->getSqlString($this->adapter->platform); exit;
230
 
231
        return $this->executeFetchAllObject($select, $prototype);
232
    }
233
 
234
    /**
235
     *
236
     * @param int $company_id
237
     * return int[]
238
     */
239
    public function fetchAllUserIdsForCapsulesActiveByCompanyId($company_id)
240
    {
241
        $now = date('Y-m-d H:i:s');
242
 
243
 
244
        $select = $this->sql->select();
245
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
246
        $select->from(self::_TABLE);
247
        $select->where->equalTo('company_id', $company_id);
248
        $select->where->nest->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED)->or->nest()
249
        ->equalTo('access', CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD)
250
        ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
251
 
252
        //echo $select->getSqlString($this->adapter->platform); exit;
253
 
254
        $user_ids = [];
255
        $records = $this->executeFetchAllArray($select);
256
        foreach($records as $record)
257
        {
258
            array_push($user_ids, $record['user_id']);
259
        }
260
        return $user_ids;
261
    }
262
 
263
 
264
    /**
265
     *
266
     * @param int $id
267
     * return CompanyMicrolearningCapsuleUser
268
     */
269
    public function fetchOne($id)
270
    {
271
        $prototype = new CompanyMicrolearningCapsuleUser();
272
        $select = $this->sql->select();
273
        $select->from(self::_TABLE);
274
        $select->where->equalTo('id', $id);
275
 
276
        return $this->executeFetchOneObject($select, $prototype);
277
    }
278
 
279
 
280
    /**
281
     *
282
     * @param int $user_id
283
     * @param int $capsule_id
284
     * return CompanyMicrolearningCapsuleUser
285
     */
286
    public function fetchOneByUserIdAndCapsuleId($user_id, $capsule_id)
287
    {
288
        $prototype = new CompanyMicrolearningCapsuleUser();
289
        $select = $this->sql->select();
290
        $select->from(self::_TABLE);
291
        $select->where->equalTo('user_id', $user_id);
292
        $select->where->equalTo('capsule_id', $capsule_id);
293
 
294
        return $this->executeFetchOneObject($select, $prototype);
295
    }
296
 
297
 
298
 
299
 
300
    /**
301
     *
302
     * @param CompanyMicrolearningCapsuleUser $companyMicrolearningCapsuleUser
303
     * @return boolean
304
     */
305
    public function insert($companyMicrolearningCapsuleUser)
306
    {
307
        $hydrator = new ObjectPropertyHydrator();
308
        $values = $hydrator->extract($companyMicrolearningCapsuleUser);
309
        $values = $this->removeEmpty($values);
310
 
311
        $insert = $this->sql->insert(self::_TABLE);
312
        $insert->values($values);
313
 
314
        $response = $this->executeInsert($insert);
315
        if($response) {
316
            $companyMicrolearningCapsuleUser->id = $this->lastInsertId;
317
        }
318
 
319
        return $response;
320
    }
321
 
322
    /**
323
     *
324
     * @param CompanyMicrolearningCapsuleUser $companyMicrolearningCapsuleUser
325
     * @return boolean
326
     */
327
    public function update($companyMicrolearningCapsuleUser)
328
    {
329
        $hydrator = new ObjectPropertyHydrator();
330
        $values = $hydrator->extract($companyMicrolearningCapsuleUser);
331
        $values = $this->removeEmpty($values);
332
 
333
        $update = $this->sql->update(self::_TABLE);
334
        $update->set($values);
335
        $update->where->equalTo('id', $companyMicrolearningCapsuleUser->id);
336
 
337
        return $this->executeUpdate($update);
338
    }
339
 
340
}