Proyectos de Subversion LeadersLinked - Services

Rev

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