Proyectos de Subversion LeadersLinked - Services

Rev

Rev 628 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
628 stevensc 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Mapper;
5
 
6
use Laminas\Db\Sql\Expression;
7
use Laminas\Db\Adapter\AdapterInterface;
8
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9
use LeadersLinked\Model\MicrolearningTopicUser;
10
use LeadersLinked\Mapper\Common\MapperCommon;
11
 
12
class MicrolearningTopicUserMapper extends MapperCommon
13
{
14
    const _TABLE = 'tbl_microlearning_topic_users';
15
 
16
    /**
17
     *
18
     * @var MicrolearningTopicUserMapper
19
     */
20
    private static $_instance;
21
 
22
    /**
23
     *
24
     * @param AdapterInterface $adapter
25
     */
26
    private function __construct($adapter)
27
    {
28
        parent::__construct($adapter);
29
    }
30
 
31
    /**
32
     *
33
     * @param AdapterInterface $adapter
34
     * @return MicrolearningTopicUserMapper
35
     */
36
    public static function getInstance($adapter)
37
    {
38
        if(self::$_instance == null) {
39
            self::$_instance = new MicrolearningTopicUserMapper($adapter);
40
        }
41
        return self::$_instance;
42
    }
43
 
44
    /**
45
     *
46
     * @param int $company_id
47
     * @param int $topic_id
48
     * return int
49
     */
50
    public function fetchCountUsersByCompanyIdAndTopicId($company_id, $topic_id)
51
    {
52
 
53
        $select = $this->sql->select();
54
        $select->columns(['total' => new Expression('COUNT(*)')]);
55
        $select->from(self::_TABLE);
56
        $select->where->equalTo('company_id', $company_id);
57
        $select->where->equalTo('topic_id', $topic_id);
58
 
59
        $record = $this->executeFetchOneArray($select);
60
        return $record['total'];
61
    }
62
 
63
    /**
64
     *
65
     * @param int $company_id
66
     * @param int $user_id
67
     * return int
68
     */
69
    public function fetchCountByCompanyIdAndUserId($company_id, $user_id)
70
    {
71
 
72
        $select = $this->sql->select();
73
        $select->columns(['total' => new Expression('COUNT(*)')]);
74
        $select->from(self::_TABLE);
75
        $select->where->equalTo('company_id', $company_id);
76
        $select->where->equalTo('user_id', $user_id);
77
 
78
        $record = $this->executeFetchOneArray($select);
79
        return $record['total'];
80
    }
81
 
82
 
83
 
84
    /**
85
     *
86
     * @param int $company_id
87
     * @param int $topic_id
88
     * return int
89
     */
90
    public function fetchCountUsersActiveByCompanyIdAndTopicId($company_id, $topic_id)
91
    {
92
        $now = date('Y-m-d H:i:s');
93
 
94
        $select = $this->sql->select();
95
        $select->columns(['total' => new Expression('COUNT(*)')]);
96
        $select->from(self::_TABLE);
97
        $select->where->equalTo('company_id', $company_id);
98
        $select->where->equalTo('topic_id', $topic_id);
99
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
100
        ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
101
        ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
102
 
103
        //echo $select->getSqlString($this->adapter->platform); exit;
104
 
105
        $record = $this->executeFetchOneArray($select);
106
        return $record['total'];
107
    }
108
 
109
    /**
110
     *
111
     * @param int $company_id
112
     * @param int $topic_id
113
     * return int
114
     */
115
    public function fetchCountByCompanyIdAndTopicId($company_id,  $topic_id)
116
    {
117
        $select = $this->sql->select();
118
        $select->columns(['total' => new Expression('COUNT(*)')]);
119
        $select->from(self::_TABLE);
120
        $select->where->equalTo('company_id', $company_id);
121
        $select->where->equalTo('topic_id', $topic_id);
122
        $record = $this->executeFetchOneArray($select);
123
        return $record['total'];
124
    }
125
 
126
 
127
    /**
128
     *
129
     * @param int $company_id
130
     * @param string $date;
131
     * return []
132
     */
133
    public function fetchAllDistinctUserIdActiveByCompanyIdAndDate($company_id, $date)
134
    {
135
 
136
 
137
        $select = $this->sql->select();
138
        $select->from(self::_TABLE);
139
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)') ]);
140
        $select->where->equalTo('company_id', $company_id);
141
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
142
        ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
143
        ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
144
        ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
145
 
146
        $user_ids = [];
147
        $records = $this->executeFetchAllArray($select);
148
 
149
        foreach($records as $record)
150
        {
151
            array_push($user_ids, $record[ 'user_id' ]);
152
        }
153
 
154
        return $user_ids;
155
    }
156
 
157
 
158
    /**
159
     *
160
     * @param int $user_id
161
     * return MicrolearningTopicUser[]
162
     */
163
    public function fetchAllActiveByUserId($user_id)
164
    {
165
        $now = date('Y-m-d H:i:s');
166
 
167
        $prototype = new MicrolearningTopicUser();
168
        $select = $this->sql->select();
169
        $select->from(self::_TABLE);
170
        $select->where->equalTo('user_id', $user_id);
171
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
172
            ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
173
            ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
174
 
175
        return $this->executeFetchAllObject($select, $prototype);
176
    }
177
 
178
 
179
    /**
180
     *
181
     * @param int $user_id
182
     * return MicrolearningTopicUser[]
183
     */
184
    public function fetchAllByUserId($user_id)
185
    {
186
 
187
        $prototype = new MicrolearningTopicUser();
188
        $select = $this->sql->select();
189
        $select->from(self::_TABLE);
190
        $select->where->equalTo('user_id', $user_id);
191
 
192
        return $this->executeFetchAllObject($select, $prototype);
193
    }
194
 
195
 
196
    /**
197
     *
198
     * @param int $topic_id
199
     * @param string $date;
200
     * return MicrolearningTopicUser[]
201
     */
202
    public function fetchAllActiveByTopicIdAndDate($topic_id, $date)
203
    {
204
 
205
        $prototype = new MicrolearningTopicUser();
206
        $select = $this->sql->select();
207
        $select->from(self::_TABLE);
208
        $select->where->equalTo('topic_id', $topic_id);
209
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
210
        ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
211
        ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
212
        ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
213
 
214
        return $this->executeFetchAllObject($select, $prototype);
215
    }
216
 
217
    /**
218
     *
219
     * @param int $topic_id
220
     * @param int $user_id
221
     * @param string $date;
222
     * return MicrolearningTopicUser
223
     */
224
    public function fetchOneActiveByTopicIdAndDate($topic_id, $user_id, $date)
225
    {
226
 
227
        $prototype = new MicrolearningTopicUser();
228
        $select = $this->sql->select();
229
        $select->from(self::_TABLE);
230
        $select->where->equalTo('topic_id', $topic_id);
231
        $select->where->equalTo('user_id', $user_id);
232
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
233
        ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
234
        ->and->lessThanOrEqualTo(new Expression('DATE(paid_from)'), $date)
235
        ->and->greaterThanOrEqualTo(new Expression('DATE(paid_to)'), $date )->unnest()->unnest();
236
 
237
        return $this->executeFetchOneObject($select, $prototype);
238
    }
239
 
240
    /**
241
     *
242
     * @param int $company_id
243
     * return int[]
244
     */
245
    public function fetchAllUserIdsForTopicsActiveByCompanyId($company_id)
246
    {
247
        $now = date('Y-m-d H:i:s');
248
 
249
 
250
        $select = $this->sql->select();
251
        $select->columns(['user_id' => new Expression('DISTINCT(user_id)')]);
252
        $select->from(self::_TABLE);
253
        $select->where->equalTo('company_id', $company_id);
254
        $select->where->nest->equalTo('access', MicrolearningTopicUser::ACCESS_UNLIMITED)->or->nest()
255
        ->equalTo('access', MicrolearningTopicUser::ACCESS_PAY_PERIOD)
256
        ->and->lessThanOrEqualTo('paid_from', $now)->and->greaterThanOrEqualTo('paid_to', $now )->unnest()->unnest();
257
 
258
 
259
        $user_ids = [];
260
        $records = $this->executeFetchAllArray($select);
261
        foreach($records as $record)
262
        {
263
            array_push($user_ids, $record['user_id']);
264
        }
265
        return $user_ids;
266
    }
267
 
268
 
269
    /**
270
     *
271
     * @param int $id
272
     * return MicrolearningTopicUser
273
     */
274
    public function fetchOne($id)
275
    {
276
        $prototype = new MicrolearningTopicUser();
277
        $select = $this->sql->select();
278
        $select->from(self::_TABLE);
279
        $select->where->equalTo('id', $id);
280
 
281
        return $this->executeFetchOneObject($select, $prototype);
282
    }
283
 
284
 
285
    /**
286
     *
287
     * @param int $user_id
288
     * @param int $topic_id
289
     * return MicrolearningTopicUser
290
     */
291
    public function fetchOneByUserIdAndTopicId($user_id, $topic_id)
292
    {
293
        $prototype = new MicrolearningTopicUser();
294
        $select = $this->sql->select();
295
        $select->from(self::_TABLE);
296
        $select->where->equalTo('user_id', $user_id);
297
        $select->where->equalTo('topic_id', $topic_id);
298
 
299
        return $this->executeFetchOneObject($select, $prototype);
300
    }
301
 
632 stevensc 302
     /**
303
     * Fetch one topic-user relationship for a given user ID and company ID.
304
     *
305
     * @param int $user_id
306
     * @param int $company_id
307
     * @return int
308
     */
309
    public function fetchOneByUserIdAndCompanyId($user_id, $company_id)
310
    {
311
        $prototype = new MicrolearningTopicUser();
312
        $select = $this->sql->select();
313
        $select->from(self::_TABLE);
314
        $select->where->equalTo('user_id', $user_id);
315
        $select->where->equalTo('company_id', $company_id);
316
        return $this->executeFetchOneObject($select, $prototype);
317
    }
628 stevensc 318
 
319
 
320
    /**
321
     *
322
     * @param MicrolearningTopicUser $microlearningTopicUser
323
     * @return boolean
324
     */
325
    public function insert($microlearningTopicUser)
326
    {
327
        $hydrator = new ObjectPropertyHydrator();
328
        $values = $hydrator->extract($microlearningTopicUser);
329
        $values = $this->removeEmpty($values);
330
 
331
        $insert = $this->sql->insert(self::_TABLE);
332
        $insert->values($values);
333
 
334
        $response = $this->executeInsert($insert);
335
        if($response) {
336
            $microlearningTopicUser->id = $this->lastInsertId;
337
        }
338
 
339
        return $response;
340
    }
341
 
342
    /**
343
     *
344
     * @param MicrolearningTopicUser $microlearningTopicUser
345
     * @return boolean
346
     */
347
    public function update($microlearningTopicUser)
348
    {
349
        $hydrator = new ObjectPropertyHydrator();
350
        $values = $hydrator->extract($microlearningTopicUser);
351
        $values = $this->removeEmpty($values);
352
 
353
        $update = $this->sql->update(self::_TABLE);
354
        $update->set($values);
355
        $update->where->equalTo('id', $microlearningTopicUser->id);
356
 
357
        return $this->executeUpdate($update);
358
    }
359
 
360
}