Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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