Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
7
 
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
 
13
use LeadersLinked\Mapper\QueryMapper;
14
use LeadersLinked\Mapper\CompanyMapper;
15
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
16
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
17
use LeadersLinked\Model\CompanyMicrolearningCapsule;
18
use LeadersLinked\Model\CompanyMicrolearningCapsuleUser;
19
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
20
use LeadersLinked\Mapper\CompanyMicrolearningUserMapper;
21
use LeadersLinked\Model\CompanyMicrolearningUser;
22
use LeadersLinked\Mapper\EngagementRewardMapper;
23
use LeadersLinked\Model\EngagementReward;
24
use LeadersLinked\Library\Functions;
25
 
26
class MarketPlaceController extends AbstractActionController
27
{
28
    /**
29
     *
30
     * @var \Laminas\Db\Adapter\AdapterInterface
31
     */
32
    private $adapter;
33
 
34
    /**
35
     *
36
     * @var \LeadersLinked\Cache\CacheInterface
37
     */
38
    private $cache;
39
 
40
 
41
    /**
42
     *
43
     * @var \Laminas\Log\LoggerInterface
44
     */
45
    private $logger;
46
 
47
    /**
48
     *
49
     * @var array
50
     */
51
    private $config;
52
 
53
 
54
    /**
55
     *
56
     * @var \Laminas\Mvc\I18n\Translator
57
     */
58
    private $translator;
59
 
60
 
61
    /**
62
     *
63
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
64
     * @param \LeadersLinked\Cache\CacheInterface $cache
65
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
66
     * @param array $config
67
     * @param \Laminas\Mvc\I18n\Translator $translator
68
     */
69
    public function __construct($adapter, $cache, $logger, $config, $translator)
70
    {
71
        $this->adapter      = $adapter;
72
        $this->cache        = $cache;
73
        $this->logger       = $logger;
74
        $this->config       = $config;
75
        $this->translator   = $translator;
76
    }
77
 
78
    /**
79
     *
80
     * Generación del listado de perfiles
81
     * {@inheritDoc}
82
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
83
     */
84
    public function indexAction()
85
    {
86
 
87
        $request = $this->getRequest();
88
        if($request->isGet()) {
89
            $currentUserPlugin = $this->plugin('currentUserPlugin');
90
            $currentUser = $currentUserPlugin->getUser();
91
 
92
 
93
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
94
                $entity = Functions::sanitizeFilterString($this->params()->fromQuery('entity', 'capsules'));
95
 
96
 
97
                if($entity == 'rewards') {
98
 
99
                    $currentUserPlugin = $this->plugin('currentUserPlugin');
100
                    $currentUser = $currentUserPlugin->getUser();
101
 
102
                    $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
103
                    $currentNetwork = $currentNetworkPlugin->getNetwork();
104
 
105
                    $companyMapper = CompanyMapper::getInstance($this->adapter);
106
                    $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentNetwork->id);
107
 
108
 
109
 
110
                    $queryMapper = QueryMapper::getInstance($this->adapter);
111
                    $select = $queryMapper->getSql()->select( EngagementRewardMapper::_TABLE);
112
                    $select->columns(['uuid', 'name',   'image', 'points']);
113
                    $select->where->equalTo('company_id', $company->id);
114
                    $select->where->equalTo('status', EngagementReward::STATUS_ACTIVE);
115
 
116
                    if($search) {
117
                        $select->where->like('c.name', '%' . $search . '%');
118
                    }
119
 
120
                    $select->order(['name ASC']);
121
 
122
 
123
                    $records = $queryMapper->fetchAll($select);
124
 
125
                    $items = [];
126
                    foreach($records as $record)
127
                    {
128
 
129
 
130
                        $item = [
131
 
132
                            'name' => $record['name'],
60 efrain 133
                            'image' => $this->url()->fromRoute('storage', ['type' => 'engagement-reward', 'code' => $record['uuid'], 'filename' => $record['image']],['force_canonical' => true]),
1 efrain 134
                            'points' => $record['points'],
135
                            'link_claim' => $this->url()->fromRoute('marketplace/claim', ['id' => $record['uuid']])
136
                        ];
137
 
138
 
139
                        array_push($items, $item);
140
                    }
141
 
142
 
143
                } else if($entity == 'capsules') {
144
 
145
                    $companyMicrolearningCapsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
146
 
147
 
148
                    $queryMapper = QueryMapper::getInstance($this->adapter);
149
                    $select = $queryMapper->getSql()->select();
150
                    $select->columns(['id', 'uuid', 'name', 'status',  'image', 'order', 'privacy', 'type', 'marketplace']);
151
                    $select->from(['c' => CompanyMicrolearningCapsuleMapper::_TABLE]);
152
                    $select->join(['t' => CompanyMicrolearningTopicMapper::_TABLE], ' c.topic_id = t.id ', ['topic_uuid' => 'uuid']);
153
                    $select->join(['o' => CompanyMapper::_TABLE], ' c.company_id = o.id ', ['company_uuid' => 'uuid']);
154
                    $select->where->equalTo('c.privacy', CompanyMicrolearningCapsule::PRIVACY_PUBLIC);
155
                    $select->where->equalTo('c.type', CompanyMicrolearningCapsule::TYPE_FREE);
156
                    $select->where->equalTo('c.status', CompanyMicrolearningCapsule::STATUS_ACTIVE);
157
 
158
                    if($search) {
159
                        $select->where->like('c.name', '%' . $search . '%');
160
                    }
161
                    $select->order(['name ASC']);
162
 
163
                   //echo $select->getSqlString($this->adapter->platform); exit;
164
 
165
                    $records = $queryMapper->fetchAll($select);
166
 
167
                    $items = [];
168
                    foreach($records as $record)
169
                    {
170
                        $capsuleUser = $companyMicrolearningCapsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $record['id']);
171
 
172
 
173
                        $params = [
174
                            'company_id' => $record['company_uuid'],
175
                            'topic_id' => $record['topic_uuid'],
176
                            'capsule_id' => $record['uuid']
177
                        ];
178
 
179
 
180
                        $item = [
181
 
182
                            'name' => $record['name'],
60 efrain 183
                            'image' => $this->url()->fromRoute('storage', ['type' => 'microlearning-capsule', 'code' => $record['uuid'], 'filename' => $record['marketplace']],['force_canonical' => true]),
1 efrain 184
                            'status' => $capsuleUser ? 'LABEL_ENROLLED' : 'LABEL_AVAILABLE',
185
                            'link_enroll' => $capsuleUser ? '' : $this->url()->fromRoute('marketplace/enroll', $params),
186
                        ];
187
 
188
 
189
                        array_push($items, $item);
190
                    }
191
                }
192
 
193
 
194
 
195
                $response = [
196
                    'success' => true,
197
                    'data' => $items
198
                ];
199
 
200
                return new JsonModel($response);
201
 
202
 
203
        } else {
204
            return new JsonModel([
205
                'success' => false,
206
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
207
            ]);
208
        }
209
    }
210
 
211
    public function enrollAction()
212
    {
213
        $request = $this->getRequest();
214
        if($request->isPost()) {
215
            $currentUserPlugin = $this->plugin('currentUserPlugin');
216
            $currentUser = $currentUserPlugin->getUser();
217
 
218
            $company_id = $this->params()->fromRoute('company_id');
219
            $topic_id = $this->params()->fromRoute('topic_id');
220
            $capsule_id = $this->params()->fromRoute('capsule_id');
221
 
222
            $companyMapper = CompanyMapper::getInstance($this->adapter);
223
            $company = $companyMapper->fetchOneByUuid($company_id);
224
            if(!$company) {
225
 
226
                return new JsonModel([
227
                    'success'   => false,
228
                    'data'   => 'ERROR_COMPANY_NOT_FOUND'
229
                ]);
230
            }
231
 
232
            $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
233
            $topic = $topicMapper->fetchOneByUuid($topic_id);
234
            if(!$topic) {
235
                return new JsonModel([
236
                    'success'   => false,
237
                    'data'   => 'ERROR_TOPIC_NOT_FOUND'
238
                ]);
239
            }
240
 
241
            if($topic->company_id != $company->id) {
242
                return new JsonModel([
243
                    'success'   => false,
244
                    'data'   => 'ERROR_UNAUTHORIZED'
245
                ]);
246
            }
247
 
248
            $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
249
            $capsule = $capsuleMapper->fetchOneByUuid($capsule_id);
250
            if(!$capsule) {
251
                return new JsonModel([
252
                    'success'   => false,
253
                    'data'   => 'ERROR_CAPSULE_NOT_FOUND'
254
                ]);
255
            }
256
 
257
            if($capsule->topic_id != $topic->id) {
258
                return new JsonModel([
259
                    'success'   => false,
260
                    'data'   => 'ERROR_UNAUTHORIZED'
261
                ]);
262
            }
263
 
264
            if($capsule->status != CompanyMicrolearningCapsule::STATUS_ACTIVE
265
                || $capsule->privacy != CompanyMicrolearningCapsule::PRIVACY_PUBLIC
266
                || $capsule->type != CompanyMicrolearningCapsule::TYPE_FREE) {
267
 
268
                return new JsonModel([
269
                    'success'   => false,
270
                    'data'   => 'ERROR_UNAUTHORIZED'
271
                ]);
272
            }
273
 
274
 
275
            $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
276
            $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($currentUser->id, $capsule->id);
277
 
278
            if($capsuleUser) {
279
                return new JsonModel([
280
                    'success'   => false,
281
                    'data'   => 'ERROR_UNAUTHORIZED'
282
                ]);
283
            }
284
 
285
            $capsuleUser = new CompanyMicrolearningCapsuleUser();
286
            $capsuleUser->company_id = $company->id;
287
            $capsuleUser->topic_id = $topic->id;
288
            $capsuleUser->capsule_id = $capsule->id;
289
            $capsuleUser->user_id = $currentUser->id;
290
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
291
 
292
            if($capsuleUserMapper->insert($capsuleUser)) {
293
 
294
 
295
                $capsuleUser = $capsuleUserMapper->fetchOne($capsule->id);
296
                if($capsuleUser) {
297
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
298
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
299
                    if($companyMicrolearningUser) {
300
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
301
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
302
 
303
                    } else {
304
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
305
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
306
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
307
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
308
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
309
 
310
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
311
                    }
312
                }
313
 
314
 
315
 
316
 
317
 
318
 
319
 
320
 
321
 
322
 
323
 
324
                return new JsonModel([
325
                    'success'   => true,
326
                    'data'   => 'LABEL_YOU_HAVE_BEEN_SUCCESSFULLY_ENROLLED'
327
                ]);
328
            } else {
329
                return new JsonModel([
330
                    'success'   => false,
331
                    'data'   => 'ERROR_UNAUTHORIZED'
332
                ]);
333
            }
334
 
335
 
336
 
337
 
338
 
339
 
340
        } else {
341
            return new JsonModel([
342
                'success' => false,
343
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
344
            ]);
345
        }
346
 
347
    }
348
 
349
    public function claimAction()
350
    {
351
        $currentUserPlugin = $this->plugin('currentUserPlugin');
352
        $currentUser = $currentUserPlugin->getUser();
353
 
354
 
355
 
356
        return new JsonModel([
357
            'success' => true,
358
            'data' => 'Por definirse'
359
        ]);
360
    }
361
 
362
    public function getCategoriesAction()
363
    {
364
        $currentUserPlugin = $this->plugin('currentUserPlugin');
365
        $currentUser = $currentUserPlugin->getUser();
366
 
367
        $acl = $this->getEvent()->getViewModel()->getVariable('acl');
368
        $allowDailyPuse = $acl->isAllowed($currentUser->usertype_id, 'daily-pulse');
369
 
370
        $data = [
371
            'capsules' => 'LABEL_CAPSULES'
372
        ];
373
 
374
        if( $allowDailyPuse) {
375
            $data['rewards'] = 'LABEL_REWARDS';
376
        }
377
 
378
 
379
        return new JsonModel([
380
            'success' => true,
381
            'data' => $data
382
        ]);
383
    }
384
 
385
}