Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15452 | Rev 15455 | 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;
7
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Mapper\CompanyMicrolearningTopicMapper;
13
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleMapper;
14
use LeadersLinked\Form\TopicCapsuleForm;
15
use LeadersLinked\Form\PushMicrolearningNotificationForm;
16
use LeadersLinked\Mapper\QueryMapper;
17
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper;
18
use LeadersLinked\Mapper\UserMapper;
19
use Laminas\Hydrator\ArraySerializableHydrator;
20
use Laminas\Db\ResultSet\HydratingResultSet;
21
use Laminas\Paginator\Adapter\DbSelect;
22
use Laminas\Paginator\Paginator;
23
use LeadersLinked\Model\CompanyMicrolearningCapsuleUser;
24
use LeadersLinked\Mapper\ApplicationMapper;
25
use LeadersLinked\Mapper\PushMapper;
26
use LeadersLinked\Mapper\PushTemplateMapper;
27
use LeadersLinked\Mapper\DeviceHistoryMapper;
28
use LeadersLinked\Model\Push;
29
use LeadersLinked\Model\Application;
30
use LeadersLinked\Form\CapsuleCustomerUploadForm;
31
use PhpOffice\PhpSpreadsheet\IOFactory;
32
use LeadersLinked\Model\User;
33
use LeadersLinked\Model\UserType;
34
use LeadersLinked\Model\UserPassword;
35
use LeadersLinked\Mapper\CompanyMicrolearningUserMapper;
36
use LeadersLinked\Mapper\UserPasswordMapper;
37
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserMapper;
38
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserCompanyMapper;
39
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserFunctionMapper;
40
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserGroupMapper;
41
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserInstitutionMapper;
42
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserProgramMapper;
43
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserPartnerMapper;
44
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserSectorMapper;
45
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserStudentTypeMapper;
46
use LeadersLinked\Model\CompanyMicrolearningExtendUser;
47
use LeadersLinked\Model\CompanyMicrolearningExtendUserCompany;
48
use LeadersLinked\Model\CompanyMicrolearningExtendUserFunction;
49
use LeadersLinked\Model\CompanyMicrolearningExtendUserGroup;
50
use LeadersLinked\Model\CompanyMicrolearningExtendUserInstitution;
51
use LeadersLinked\Model\CompanyMicrolearningExtendUserProgram;
52
use LeadersLinked\Model\CompanyMicrolearningExtendUserPartner;
53
use LeadersLinked\Model\CompanyMicrolearningExtendUserSector;
54
use LeadersLinked\Model\CompanyMicrolearningExtendUserStudentType;
55
use LeadersLinked\Model\CompanyMicrolearningUser;
13755 efrain 56
use LeadersLinked\Mapper\ApplicationVariantMapper;
15394 efrain 57
use LeadersLinked\Mapper\CompanyMicrolearningExtendUserCountryMapper;
15452 efrain 58
use LeadersLinked\Model\CompanyMicrolearningExtendUserCountry;
15453 efrain 59
use LeadersLinked\Model\Network;
60
use LeadersLinked\Mapper\CompanyMapper;
61
use LeadersLinked\Mapper\NetworkMapper;
62
use LeadersLinked\Mapper\CompanyFollowerMapper;
63
use LeadersLinked\Mapper\ConnectionMapper;
64
use LeadersLinked\Model\Connection;
65
use LeadersLinked\Model\CompanyFollower;
1 www 66
 
67
 
68
class MicrolearningAccessForStudentsController extends AbstractActionController
69
{
70
    /**
71
     *
72
     * @var AdapterInterface
73
     */
74
    private $adapter;
75
 
76
 
77
    /**
78
     *
79
     * @var AbstractAdapter
80
     */
81
    private $cache;
82
 
83
    /**
84
     *
85
     * @var  LoggerInterface
86
     */
87
    private $logger;
88
 
89
 
90
    /**
91
     *
92
     * @var array
93
     */
94
    private $config;
95
 
96
    /**
97
     *
98
     * @param AdapterInterface $adapter
99
     * @param AbstractAdapter $cache
100
     * @param LoggerInterface $logger
101
     * @param array $config
102
     */
103
    public function __construct($adapter, $cache , $logger,  $config)
104
    {
105
        $this->adapter      = $adapter;
106
        $this->cache        = $cache;
107
        $this->logger       = $logger;
108
        $this->config       = $config;
109
 
110
    }
111
 
112
    public function indexAction()
113
    {
114
        $currentUserPlugin = $this->plugin('currentUserPlugin');
115
        $currentUser = $currentUserPlugin->getUser();
116
        $currentCompany = $currentUserPlugin->getCompany();
117
 
118
        $request = $this->getRequest();
119
 
120
        if($request->isGet())
121
        {
122
 
123
            $headers  = $request->getHeaders();
124
 
125
            $isJson = false;
126
            if($headers->has('Accept')) {
127
                $accept = $headers->get('Accept');
128
 
129
                $prioritized = $accept->getPrioritized();
130
 
131
                foreach($prioritized as $key => $value) {
132
                    $raw = trim($value->getRaw());
133
 
134
                    if(!$isJson) {
135
                        $isJson = strpos($raw, 'json');
136
                    }
137
 
138
                }
139
            }
140
 
141
            if($isJson) {
142
                $topic_uuid     = filter_var($this->params()->fromQuery('topic_uuid'), FILTER_SANITIZE_STRING);
143
                $capsule_uuid   = filter_var($this->params()->fromQuery('capsule_uuid'), FILTER_SANITIZE_STRING);
144
 
145
                $data = [
146
                    'link_upload' => '',
147
                    'items' => [] ,
148
                    'total' => 0,
149
 
150
                ];
151
 
152
 
153
                if(!$topic_uuid) {
154
                    return new JsonModel([
155
                        'success' => true,
156
                        'data' => $data
157
                    ]);
158
 
159
                }
160
 
161
 
162
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
163
                $topic = $topicMapper->fetchOneByUuid($topic_uuid);
164
                if(!$topic) {
165
                    return new JsonModel([
166
                        'success' => true,
167
                        'data' => 'ERROR_TOPIC_NOT_FOUND'
168
                    ]);
169
                }
170
 
171
                if($topic->company_id != $currentCompany->id) {
172
                    return new JsonModel([
173
                        'success' => true,
174
                        'data' => 'ERROR_UNAUTHORIZED'
175
                    ]);
176
                }
177
 
178
 
179
 
180
                if(!$capsule_uuid) {
181
                    $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
182
                    $records = $capsuleMapper->fetchAllByCompanyIdAndTopicId($topic->company_id, $topic->id);
183
 
184
                    $capsules = [];
185
                    foreach($records as $record)
186
                    {
187
                        if(!$capsule_uuid) {
188
                            $capsule_uuid = $record->uuid;
189
                        }
190
 
191
                        $capsules[ $record->uuid ] = $record->name;
192
                    }
193
 
194
                    $data['capsules']  = $capsules;
195
                }
196
 
197
                if(!$capsule_uuid) {
198
                    return new JsonModel([
199
                        'success' => true,
200
                        'data' => $data
201
                    ]);
202
 
203
                }
204
 
205
                $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
206
                $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
207
 
208
 
209
 
210
                if(!$capsule) {
211
                    return new JsonModel([
212
                        'success' => true,
213
                        'data' => 'ERROR_CAPSULE_NOT_FOUND'
214
                    ]);
215
                }
216
 
217
                if($capsule->topic_id != $topic->id) {
218
                    return new JsonModel([
219
                        'success' => true,
220
                        'data' => 'ERROR_UNAUTHORIZED'
221
                    ]);
222
                }
223
 
224
 
225
                $data['link_upload'] = $this->url()->fromRoute('microlearning/access-for-students/upload',['topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid]);
226
                $data['link_notification'] = $this->url()->fromRoute('microlearning/access-for-students/notification',['topic_uuid' => $topic->uuid, 'capsule_uuid' => $capsule->uuid]);
227
 
228
                $search = $this->params()->fromQuery('search', []);
229
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
230
 
231
                $page               = intval($this->params()->fromQuery('start', 1), 10);
232
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
233
                $order =  $this->params()->fromQuery('order', []);
234
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
235
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
236
 
237
                $fields =  ['uuid', 'first_name', 'last_name', 'email'];
238
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
239
 
240
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
241
                    $order_direction = 'ASC';
242
                }
243
 
244
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
245
                $allowRevoke = $acl->isAllowed($currentUser->usertype_id, 'microlearning/access-for-students/revoke');
246
                $allowUnlimit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/access-for-students/unlimit');
247
                $allowCancel = $acl->isAllowed($currentUser->usertype_id, 'microlearning/access-for-students/cancel');
248
                $allowReactive = $acl->isAllowed($currentUser->usertype_id, 'microlearning/access-for-students/reactive');
249
 
250
                $queryMapper = QueryMapper::getInstance($this->adapter);
251
                $sql = $queryMapper->getSql();
252
                $select = $sql->select();
253
                $select->columns(['access', 'paid_from', 'paid_to', 'added_on', 'updated_on']);
254
                $select->from(['tb1' => CompanyMicrolearningCapsuleUserMapper::_TABLE] );
255
                $select->join(['tb2' => UserMapper::_TABLE], 'tb1.user_id = tb2.id', ['uuid', 'first_name', 'last_name', 'email']);
256
                $select->where->equalTo('tb1.company_id', $capsule->company_id);
257
                $select->where->equalTo('tb1.topic_id', $capsule->topic_id);
258
                $select->where->equalTo('tb1.capsule_id', $capsule->id);
259
 
260
                if($search) {
261
                    $select->where->nest()
262
                    ->like('first_name', '%' . $search . '%')
263
                    ->or->like('last_name', '%' . $search . '%')
264
                    ->or->like('email', '%' . $search . '%')
265
                    ->unnest();
266
 
267
                }
268
 
269
 
270
                $select->order($order_field . ' ' . $order_direction);
271
 
272
                $hydrator   = new ArraySerializableHydrator();
273
                $resultset  = new HydratingResultSet($hydrator);
274
 
275
                $adapter = new DbSelect($select, $sql, $resultset);
276
                $paginator = new Paginator($adapter);
277
                $paginator->setItemCountPerPage($records_x_page);
278
                $paginator->setCurrentPageNumber($page);
279
 
280
 
281
                $items = [ ];
282
                $records = $paginator->getCurrentItems();
283
                foreach($records as $record)
284
                {
285
                    $params = [
286
                        'topic_uuid' => $topic->uuid,
287
                        'capsule_uuid' => $capsule->uuid,
288
                        'user_uuid' => $record['uuid']
289
                    ];
290
 
291
                    $actions = [];
292
 
293
 
294
 
295
                    switch($record['access'])
296
                    {
297
                        case CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED :
298
                            $actions['link_revoke'] = $allowRevoke ? $this->url()->fromRoute('microlearning/access-for-students/revoke', $params) : '';
299
                            $details['access'] = 'LABEL_UNLIMIT';
300
                            break;
301
 
302
                        case CompanyMicrolearningCapsuleUser::ACCESS_REVOKE :
303
                            $actions['link_unlimit'] = $allowUnlimit ? $this->url()->fromRoute('microlearning/access-for-students/unlimit', $params) : '';
304
                            $details['access'] = 'LABEL_REVOKED';
305
                            break;
306
 
307
                        case CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD :
308
                            $actions['link_cancel'] = $allowCancel ? $this->url()->fromRoute('microlearning/access-for-students/cancel', $params) : '';
309
 
310
                            $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
311
                            $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
312
 
313
                            $details['access'] = 'LABEL_PAY_PERIOD';
314
                            $details['paid_from'] = $dt_paid_from->format('d/m/Y');
315
                            $details['paid_to'] = $dt_paid_to->format('d/m/Y');
316
                            break;
317
 
318
                        case CompanyMicrolearningCapsuleUser::ACCESS_SUPENDED :
319
                            $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
320
                            $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
321
 
322
                            $details['access'] = 'LABEL_SUSPENDED';
323
                            $details['paid_from'] = $dt_paid_from->format('d/m/Y');
324
                            $details['paid_to'] = $dt_paid_to->format('d/m/Y');
325
                            break;
326
 
327
                        case CompanyMicrolearningCapsuleUser::ACCESS_CANCELLED :
328
 
329
                            $date = date('Y-m-d');
330
                            if($allowCancel && $record['paid_from'] <= $date && $record['paid_to'] >= $date) {
331
                                $actions['link_reactive'] = $allowReactive ? $this->url()->fromRoute('microlearning/access-for-students/reactive', $params) : '';
332
                            }
333
 
334
                            $dt_paid_from = \DateTime::createFromFormat('Y-m-d', $record['paid_from']);
335
                            $dt_paid_to = \DateTime::createFromFormat('Y-m-d', $record['paid_to']);
336
 
337
                            $details['access'] = 'LABEL_CANCELLED';
338
                            $details['paid_from'] = $dt_paid_from->format('d/m/Y');
339
                            $details['paid_to'] = $dt_paid_to->format('d/m/Y');
340
                            break;
341
 
342
                    }
343
 
344
                    $dt_added_on = \DateTime::createFromFormat('Y-m-d H:i:s', $record['added_on']);
345
                    $details['added_on'] = $dt_added_on->format('d/m/Y h:i a');
346
 
347
                    $dt_updated_on = \DateTime::createFromFormat('Y-m-d H:i:s', $record['updated_on']);
348
                    $details['updated_on'] = $dt_updated_on->format('d/m/Y h:i a');
349
 
350
                    $item = [
351
                        'uuid' => $record['uuid'],
352
                        'first_name' => $record['first_name'],
353
                        'last_name' => $record['last_name'],
354
                        'email' => $record['email'],
355
                        'details' => $details,
356
                        'actions' => $actions
357
                    ];
358
 
359
 
360
                    array_push($items, $item);
361
 
362
 
363
                }
364
 
365
                $data['items'] = $items;
366
                $data['total'] = $paginator->getTotalItemCount();
367
 
368
 
369
                return new JsonModel([
370
                    'success' => true,
371
                    'data' => $data
372
                ]);
373
            } else {
374
                $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
375
                $topics = $topicMapper->fetchAllByCompanyId($currentCompany->id);
376
 
377
                if($topics) {
378
                    $topic_id = $topics[0]->id;
379
                }  else {
380
                    $topic_id = 0;
381
                }
382
 
383
 
384
                $form = new TopicCapsuleForm($this->adapter, $currentCompany->id, $topic_id);
15355 efrain 385
                $formPushNotification = new PushMicrolearningNotificationForm($this->adapter, $currentCompany->id);
1 www 386
                $formCapsuleCustomer = new CapsuleCustomerUploadForm();
387
 
388
 
389
 
390
                $this->layout()->setTemplate('layout/layout-backend');
391
                $viewModel = new ViewModel();
392
                $viewModel->setTemplate('leaders-linked/microlearning-access-for-students/index.phtml');
393
                $viewModel->setVariables([
394
                    'form' => $form,
395
                    'formPushNotification' => $formPushNotification,
396
                    'formCapsuleCustomer' => $formCapsuleCustomer
397
                ]);
398
 
399
                return $viewModel ;
400
            }
401
 
402
        } else {
403
            return new JsonModel([
404
                'success' => false,
405
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
187 efrain 406
            ]);
1 www 407
        }
408
    }
409
 
410
 
411
 
412
 
413
    public function revokeAction()
414
    {
415
        $request = $this->getRequest();
416
 
417
        $currentUserPlugin = $this->plugin('currentUserPlugin');
418
        $currentUser    = $currentUserPlugin->getUser();
419
        $currentCompany = $currentUserPlugin->getCompany();
420
 
421
        $request    = $this->getRequest();
422
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
423
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
424
        $user_uuid   = $this->params()->fromRoute('user_uuid');
425
 
426
 
427
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
428
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
429
        if(!$topic) {
430
            return new JsonModel([
431
                'success'   => false,
432
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
433
            ]);
434
        }
435
 
436
 
437
 
438
        if($topic->company_id != $currentCompany->id) {
439
            return new JsonModel([
440
                'success'   => false,
441
                'data'   => 'ERROR_UNAUTHORIZED'
442
            ]);
443
        }
444
 
445
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
446
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
447
        if(!$capsule) {
448
            return new JsonModel([
449
                'success'   => false,
450
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
451
            ]);
452
        }
453
 
454
 
455
        if($capsule->topic_id != $topic->id) {
456
            return new JsonModel([
457
                'success'   => false,
458
                'data'   => 'ERROR_UNAUTHORIZED'
459
            ]);
460
        }
461
 
462
        $userMapper = UserMapper::getInstance($this->adapter);
463
        $user = $userMapper->fetchOneByUuid($user_uuid);
464
 
465
        if(!$user) {
466
            return new JsonModel([
467
                'success'   => false,
468
                'data'   => 'ERROR_USER_NOT_FOUND'
469
            ]);
470
        }
471
 
472
        $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
473
        $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
474
        if(!$capsuleUser) {
475
            return new JsonModel([
476
                'success'   => false,
477
                'data'   => 'ERROR_UNAUTHORIZED'
478
            ]);
479
        }
480
 
481
        if($request->isPost()) {
482
 
483
 
484
            if($capsuleUser->access != CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED) {
485
                return new JsonModel([
486
                    'success'   => false,
487
                    'data'   => 'ERROR_USER_ACCESS_CANNT_BE_REVOKE'
488
                ]);
489
            }
490
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_REVOKE;
491
            if($capsuleUserMapper->update($capsuleUser)) {
492
 
493
                $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
494
                if($capsuleUser) {
495
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
496
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
497
                    if($companyMicrolearningUser) {
498
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
499
 
500
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
501
 
502
                    } else {
503
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
504
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
505
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
506
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
507
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
508
 
509
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
510
                    }
511
                }
512
 
513
                return new JsonModel([
514
                    'success' => true,
515
                    'data' => 'LABEL_USER_ACCESS_HAS_BEEN_REVOKE'
516
                ]);
517
 
518
            } else {
519
 
520
                return new JsonModel([
521
                    'success'   => false,
522
                    'data'      => $capsuleUserMapper->getError()
523
                ]);
524
            }
525
        }
526
 
527
        return new JsonModel([
528
            'success' => false,
529
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
530
        ]);
531
    }
532
 
533
    public function unlimitAction()
534
    {
535
        $request = $this->getRequest();
536
 
537
        $currentUserPlugin = $this->plugin('currentUserPlugin');
538
        $currentUser    = $currentUserPlugin->getUser();
539
        $currentCompany = $currentUserPlugin->getCompany();
540
 
541
        $request    = $this->getRequest();
542
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
543
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
544
        $user_uuid   = $this->params()->fromRoute('user_uuid');
545
 
546
 
547
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
548
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
549
        if(!$topic) {
550
            return new JsonModel([
551
                'success'   => false,
552
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
553
            ]);
554
        }
555
 
556
 
557
 
558
        if($topic->company_id != $currentCompany->id) {
559
            return new JsonModel([
560
                'success'   => false,
561
                'data'   => 'ERROR_UNAUTHORIZED'
562
            ]);
563
        }
564
 
565
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
566
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
567
        if(!$capsule) {
568
            return new JsonModel([
569
                'success'   => false,
570
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
571
            ]);
572
        }
573
 
574
 
575
        if($capsule->topic_id != $topic->id) {
576
            return new JsonModel([
577
                'success'   => false,
578
                'data'   => 'ERROR_UNAUTHORIZED'
579
            ]);
580
        }
581
 
582
        $userMapper = UserMapper::getInstance($this->adapter);
583
        $user = $userMapper->fetchOneByUuid($user_uuid);
584
 
585
        if(!$user) {
586
            return new JsonModel([
587
                'success'   => false,
588
                'data'   => 'ERROR_USER_NOT_FOUND'
589
            ]);
590
        }
591
 
592
        $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
593
        $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
594
        if(!$capsuleUser) {
595
            return new JsonModel([
596
                'success'   => false,
597
                'data'   => 'ERROR_UNAUTHORIZED'
598
            ]);
599
        }
600
 
601
        if($request->isPost()) {
602
 
603
 
604
            if($capsuleUser->access != CompanyMicrolearningCapsuleUser::ACCESS_REVOKE) {
605
                return new JsonModel([
606
                    'success'   => false,
607
                    'data'   => 'ERROR_USER_ACCESS_CANNT_BE_UNLIMIT'
608
                ]);
609
            }
610
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
611
            if($capsuleUserMapper->update($capsuleUser)) {
612
 
613
                $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
614
                if($capsuleUser) {
615
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
616
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
617
                    if($companyMicrolearningUser) {
618
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
619
 
620
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
621
 
622
                    } else {
623
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
624
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
625
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
626
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
627
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
628
 
629
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
630
                    }
631
                }
632
 
633
                return new JsonModel([
634
                    'success' => true,
635
                    'data' => 'LABEL_USER_ACCESS_HAS_BEEN_UNLIMITED'
636
                ]);
637
 
638
            } else {
639
 
640
                return new JsonModel([
641
                    'success'   => false,
642
                    'data'      => $capsuleUserMapper->getError()
643
                ]);
644
            }
645
        }
646
 
647
        return new JsonModel([
648
            'success' => false,
649
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
650
        ]);
651
    }
652
 
653
    public function uploadAction()
654
    {
655
        $request = $this->getRequest();
656
 
657
        $currentUserPlugin = $this->plugin('currentUserPlugin');
658
        $currentUser    = $currentUserPlugin->getUser();
659
        $currentCompany = $currentUserPlugin->getCompany();
660
 
15394 efrain 661
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
662
        $currentNetwork = $currentNetworkPlugin->getNetwork();
663
 
1 www 664
        $request    = $this->getRequest();
665
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
666
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
667
 
668
 
669
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
670
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
671
        if(!$topic) {
672
            return new JsonModel([
673
                'success'   => false,
674
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
675
            ]);
676
        }
677
 
678
 
679
 
680
        if($topic->company_id != $currentCompany->id) {
681
            return new JsonModel([
682
                'success'   => false,
683
                'data'   => 'ERROR_UNAUTHORIZED'
684
            ]);
685
        }
686
 
687
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
688
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
689
        if(!$capsule) {
690
            return new JsonModel([
691
                'success'   => false,
692
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
693
            ]);
694
        }
695
 
696
 
697
        if($capsule->topic_id != $topic->id) {
698
            return new JsonModel([
699
                'success'   => false,
700
                'data'   => 'ERROR_UNAUTHORIZED'
701
            ]);
702
        }
703
 
704
 
705
 
706
        if($request->isPost()) {
707
 
708
            $step = filter_var( $this->params()->fromPost('step'), FILTER_SANITIZE_STRING);
709
            if($step == 'validation') {
710
                $userMapper = UserMapper::getInstance($this->adapter);
711
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
712
 
713
                $form = new  CapsuleCustomerUploadForm();
714
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
715
 
716
                $form->setData($dataPost);
717
 
718
                if($form->isValid()) {
719
 
720
                    $file = $_FILES['file'];
721
                    $tmp_filename = $file['tmp_name'];
722
                    $final_filename =  'data/' . $file['name'];
723
 
724
                    if(!move_uploaded_file($tmp_filename, $final_filename)) {
725
                        return new JsonModel([
726
                            'success' => false,
727
                            'data' => 'ERROR_UPLOAD_FILE'
728
                        ]);
729
                    }
730
 
731
 
732
                    $users = [];
733
 
734
 
735
                    $spreadsheet = IOFactory::load($final_filename);
736
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
737
 
738
                    $emails = [];
739
 
740
                    foreach($records as $record)
741
                    {
15452 efrain 742
                        /*
743
                        A = Nombre
744
                        B = Apellido
745
                        C = Email
746
                        D = Contraseña
747
                        E = Empresa
748
                        F = Función
749
                        G = Grupo
750
                        H = Institución
751
                        I = Programa
752
                        J = Socio
753
                        K = Sector
754
                        L = Tipo de Estudiante
755
                        M = País
756
                        N = Es adulto
757
                        */
1 www 758
 
759
 
760
                        $first_name = trim(filter_var($record['A'], FILTER_SANITIZE_STRING));
761
                        $last_name = trim(filter_var($record['B'], FILTER_SANITIZE_STRING));
15394 efrain 762
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
1 www 763
                        $password = trim(filter_var($record['D'], FILTER_SANITIZE_STRING));
764
 
15452 efrain 765
                        $company =  isset($record['E']) ? trim(filter_var($record['E'], FILTER_SANITIZE_STRING)) : '';
766
                        $function = isset($record['F']) ? trim(filter_var($record['F'], FILTER_SANITIZE_STRING)) : '';
767
                        $group = isset($record['G']) ? trim(filter_var($record['G'], FILTER_SANITIZE_STRING)) : '';
768
                        $institution = isset($record['H']) ? trim(filter_var($record['H'], FILTER_SANITIZE_STRING)) : '';
769
                        $program = isset($record['I']) ? trim(filter_var($record['I'], FILTER_SANITIZE_STRING)) : '';
770
                        $partner = isset($record['J']) ? trim(filter_var($record['J'], FILTER_SANITIZE_STRING)) : '';
771
                        $sector = isset($record['K']) ? trim(filter_var($record['K'], FILTER_SANITIZE_STRING)) : '';
772
                        $studentType = isset($record['L']) ? trim(filter_var($record['L'], FILTER_SANITIZE_STRING)) : '';
773
                        $country =  isset($record['M']) ? trim(filter_var($record['M'], FILTER_SANITIZE_STRING)) : '';
774
                        $isAdult = isset($record['N']) ? trim(filter_var($record['N'], FILTER_SANITIZE_STRING)) : '';
775
 
15394 efrain 776
 
777
 
129 efrain 778
                        //||  empty($password)
779
                        if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
1 www 780
                            continue;
781
                        }
782
 
783
                        if(!in_array($email, $emails)) {
784
 
785
                            $user = $userMapper->fetchOneByEmail($email);
786
                            if($user) {
787
                                $assigned_capsules =  $capsuleUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id);
788
                            } else {
789
                                $assigned_capsules = 0;
790
                            }
791
 
792
                            array_push($emails, $email);
793
                            array_push($users, [
794
                                'first_name' => $first_name,
795
                                'last_name' => $last_name,
796
                                'password'  => $password,
797
                                'email' => $email,
798
                                'assigned_capsules' => $assigned_capsules,
15452 efrain 799
                                'company' => $company,
800
                                'function' => $function,
801
                                'group' => $group,
802
                                'institution' => $institution,
803
                                'program' => $program,
804
                                'partner' => $partner,
805
                                'sector' => $sector,
806
                                'studentType' => $studentType,
807
                                'country' => $country,
808
                                'isAdult' => $isAdult,
1 www 809
                            ]);
810
                        }
811
 
812
 
813
 
814
 
815
 
816
                    }
817
 
818
                    $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $capsule->uuid);
819
                    $this->cache->setItem($key, serialize($users));
820
 
821
                    return new JsonModel([
822
                        'success' => true,
823
                        'data' => [
824
                            'key' => $key,
825
                            'topic' => $topic->name,
826
                            'capsule' => $capsule->name,
827
                            'items' => $users,
828
                        ]
829
                    ]);
830
 
831
 
832
 
833
                } else {
834
                    $messages = [];
835
                    $form_messages = (array) $form->getMessages();
836
                    foreach($form_messages  as $fieldname => $field_messages)
837
                    {
838
 
839
                        $messages[$fieldname] = array_values($field_messages);
840
                    }
841
 
842
                    return new JsonModel([
843
                        'success'   => false,
844
                        'data'   => $messages
845
                    ]);
846
                }
847
            } else if($step == 'process') {
848
 
849
                $key = filter_var( $this->params()->fromPost('key'), FILTER_SANITIZE_STRING);
850
                if(!$key) {
851
                    return new JsonModel([
852
                        'success' => false,
853
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
854
                    ]);
855
                }
856
 
857
                $value = $this->cache->getItem($key);
858
                if(!$value) {
859
 
860
                    return new JsonModel([
861
                        'success' => false,
862
                        'data' => 'ERROR_CACHE_NOT_FOUND'
863
                    ]);
864
                }
865
 
866
                $records = unserialize($value);
867
                if(!$records) {
868
                    return new JsonModel([
869
                        'success' => false,
870
                        'data' => 'ERROR_CACHE_INVALID'
871
                    ]);
872
                }
873
 
15453 efrain 874
                $networkMapper = NetworkMapper::getInstance($this->adapter);
875
                $companyMapper = CompanyMapper::getInstance($this->adapter);
876
                $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
877
 
878
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
1 www 879
                $userMapper = UserMapper::getInstance($this->adapter);
880
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
881
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
882
 
883
                $companyMicrolearningExtendUserMapper = CompanyMicrolearningExtendUserMapper::getInstance($this->adapter);
884
                $companyMicrolearningExtendUserCompanyMapper = CompanyMicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
885
                $companyMicrolearningExtendUserFunctionMapper = CompanyMicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
886
                $companyMicrolearningExtendUserGroupMapper = CompanyMicrolearningExtendUserGroupMapper::getInstance($this->adapter);
887
                $companyMicrolearningExtendUserInstitutionMapper = CompanyMicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
888
                $companyMicrolearningExtendUserProgramMapper = CompanyMicrolearningExtendUserProgramMapper::getInstance($this->adapter);
889
                $companyMicrolearningExtendUserPartnerMapper = CompanyMicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
890
                $companyMicrolearningExtendUserSectorMapper = CompanyMicrolearningExtendUserSectorMapper::getInstance($this->adapter);
891
                $companyMicrolearningExtendUserStudentTypeMapper = CompanyMicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
15394 efrain 892
                $companyMicrolearningExtendUserCountryMapper = CompanyMicrolearningExtendUserCountryMapper::getInstance($this->adapter);
4 efrain 893
 
1 www 894
 
15453 efrain 895
                $networkDefault = $networkMapper->fetchOneByDefault();
896
 
897
                //print_r($networkDefault);
898
 
899
                $userDefaultForConnection = $userMapper->fetchOneDefaultForConnection();
900
 
901
                //print_r($userDefaultForConnection);
902
 
903
 
904
                $companyForFollower = $companyMapper->fetchOneDefaultForFollowers();
905
                //print_r($companyForFollower);
4 efrain 906
 
1 www 907
                $users_assigned = 0;
908
                $user_ids = [];
909
                foreach($records as $record)
910
                {
911
                    $first_name = $record['first_name'];
912
                    $last_name = $record['last_name'];
913
                    $password = $record['password'];
914
                    $email = $record['email'];
915
 
916
 
15452 efrain 917
                    $company = $record['company'];
918
                    $function = $record['function'];
919
                    $group = $record['group'];
920
                    $institution = $record['institution'];
921
                    $program = $record['program'];
922
                    $partner = $record['partner'];
923
                    $sector = $record['sector'];
924
                    $studentType = $record['studentType'];
925
                    $country = $record['country'];
15453 efrain 926
                    $isAdult = strtolower(trim( $record['isAdult'])) == User::IS_ADULT_YES;
927
 
928
 
1 www 929
                    $user = $userMapper->fetchOneByEmail($email);
930
                    if(!$user) {
931
 
932
 
129 efrain 933
                        if($password) {
934
                            $password_hash = password_hash($password, PASSWORD_DEFAULT);
935
 
936
                            $user = new User();
937
 
15452 efrain 938
                            $user->network_id = $currentNetwork->id;
129 efrain 939
                            $user->blocked = User::BLOCKED_NO;
940
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
941
                            $user->email = $email;
942
                            $user->first_name = $first_name;
943
                            $user->last_name = $last_name;
944
                            $user->password = $password_hash;
945
                            $user->login_attempt = 0;
946
                            $user->usertype_id = UserType::USER;
947
                            $user->status = User::STATUS_ACTIVE;
15453 efrain 948
                            $user->is_adult = $isAdult ? User::IS_ADULT_YES : User::IS_ADULT_NO;
129 efrain 949
 
950
                            $result = $userMapper->insert($user);
951
                            if($result) {
952
                                $userPassword = new UserPassword();
953
                                $userPassword->user_id = $user->id;
954
                                $userPassword->password = $password_hash;
955
                                $userPasswordMapper->insert($userPassword);
956
                            } else {
957
                                continue;
958
                            }
15453 efrain 959
 
129 efrain 960
                        }
961
                    } else {
15453 efrain 962
                        $user->is_adult = $isAdult ? User::IS_ADULT_YES : User::IS_ADULT_NO;
129 efrain 963
                        if($user->email_verified == User::EMAIL_VERIFIED_NO || $user->status != User::STATUS_ACTIVE) {
964
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
965
                            $user->status != User::STATUS_ACTIVE;
966
 
15453 efrain 967
 
129 efrain 968
                        }
15453 efrain 969
                        if(!$userMapper->update($user)) {
970
                            continue;
971
                        }
129 efrain 972
 
973
 
1 www 974
                    }
975
 
15453 efrain 976
 
977
                    $user_id_in_default_network = 0;
978
                    if($user->is_adult == User::IS_ADULT_YES) {
979
 
980
                        if($currentNetwork->default == Network::DEFAULT_YES) {
981
 
982
                            $user_id_in_default_network = $user->id;
983
 
984
 
985
 
986
                        } else {
987
 
988
                            $userInDefaultNetwork = $userMapper->fetchOneByEmailAndNetworkId($user->email, $networkDefault->id);
989
                            if($userInDefaultNetwork) {
990
                                $user_id_in_default_network = $userInDefaultNetwork->id;
991
 
992
                                if($userInDefaultNetwork->email_verified == User::EMAIL_VERIFIED_NO || $userInDefaultNetwork->status != User::STATUS_ACTIVE) {
993
                                    $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
994
                                    $userInDefaultNetwork->status != User::STATUS_ACTIVE;
995
 
996
                                    if(!$userMapper->update($userInDefaultNetwork)) {
997
                                        continue;
998
                                    }
999
                                }
1000
 
1001
 
1002
                            } else {
1003
                                $userInDefaultNetwork = new User();
1004
                                $userInDefaultNetwork->network_id = $networkDefault->id;
1005
                                $userInDefaultNetwork->blocked = User::BLOCKED_NO;
1006
                                $userInDefaultNetwork->email_verified = User::EMAIL_VERIFIED_YES;
1007
                                $userInDefaultNetwork->email = $email;
1008
                                $userInDefaultNetwork->first_name = $first_name;
1009
                                $userInDefaultNetwork->last_name = $last_name;
1010
                                $userInDefaultNetwork->password = $password_hash;
1011
                                $userInDefaultNetwork->login_attempt = 0;
1012
                                $userInDefaultNetwork->usertype_id = UserType::USER;
1013
                                $userInDefaultNetwork->status = User::STATUS_ACTIVE;
1014
                                $userInDefaultNetwork->is_adult = $isAdult == User::IS_ADULT_YES;
1015
                                $result = $userMapper->insert($userInDefaultNetwork);
1016
                                if($result) {
1017
                                    $user_id_in_default_network = $userInDefaultNetwork->id;
1018
 
1019
 
1020
                                    $userPassword = new UserPassword();
1021
                                    $userPassword->user_id = $userInDefaultNetwork->id;
1022
                                    $userPassword->password = $password_hash;
1023
                                    $userPasswordMapper->insert($userPassword);
1024
                                }
1025
 
1026
                            }
1027
 
1028
 
1029
                        }
1030
                    }
1031
 
1032
                    if($user_id_in_default_network) {
1033
 
1034
                        if($userDefaultForConnection) {
1035
 
1036
                            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($userDefaultForConnection->id, $user_id_in_default_network);
1037
                            if($connection) {
1038
                                if($connection->status != Connection::STATUS_ACCEPTED) {
1039
                                    $connection->status = Connection::STATUS_ACCEPTED;
1040
                                    $connectionMapper->update($connection);
1041
 
1042
                                }
1043
 
1044
 
1045
                            } else {
1046
                                $connection = new Connection();
1047
                                $connection->request_to = $user_id_in_default_network;
1048
                                $connection->request_from = $userDefaultForConnection->id;
1049
                                $connection->status = Connection::STATUS_ACCEPTED;
1050
                                $connectionMapper->insert($connection);
1051
                            }
1052
                        }
1053
 
1054
                        if($companyForFollower) {
1055
                            $companyFollower = $companyFollowerMapper->fetchOneByCompanyIdAndUserId($companyForFollower->id, $user_id_in_default_network);
1056
                            if(!$companyFollower) {
1057
                                $companyFollower = new CompanyFollower();
1058
                                $companyFollower->company_id = $companyForFollower->id;
1059
                                $companyFollower->follower_id = $user_id_in_default_network;
1060
 
1061
                                $companyFollowerMapper->insert($companyFollower);
1062
 
1063
 
1064
                            }
1065
                        }
1066
 
1067
 
1068
                    }
1069
 
1070
 
1 www 1071
                    if(!in_array($user->id, $user_ids)) {
1072
                        array_push($user_ids, $user->id);
1073
                    }
1074
 
4 efrain 1075
                    /*
1076
                    echo '$filterCompany = ' . $filterCompany . PHP_EOL;
1077
                    echo '$filterFunctio = ' . $filterFunction . PHP_EOL;
1078
                    echo '$filterGroup = ' . $filterGroup . PHP_EOL;
1079
                    echo '$filterInstitution = ' . $filterInstitution . PHP_EOL;
1080
                    echo '$filterPartner = ' . $filterPartner . PHP_EOL;
1081
                    echo '$filterProgram = ' . $filterProgram . PHP_EOL;
1082
                    echo '$filterSector = ' . $filterSector . PHP_EOL;
1083
                    echo '$filterStudentType = ' . $filterStudentType . PHP_EOL;
1084
                  */
1085
 
1086
 
1087
                    $extendUser = $companyMicrolearningExtendUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1088
                    if(!$extendUser) {
1089
                        $extendUser = new CompanyMicrolearningExtendUser();
1090
                        $extendUser->company_id = $currentCompany->id;
1091
                        $extendUser->user_id = $user->id;
1092
                    }
1093
 
1094
 
15452 efrain 1095
                    if($company) {
1096
                        $record = $companyMicrolearningExtendUserCompanyMapper->fetchOneByCompanyIdAndName($currentCompany->id, $company);
4 efrain 1097
 
1098
                        if(!$record) {
1099
                            $record = new CompanyMicrolearningExtendUserCompany();
1100
                            $record->company_id = $currentCompany->id;
15452 efrain 1101
                            $record->name = $company;
1 www 1102
 
4 efrain 1103
                            $companyMicrolearningExtendUserCompanyMapper->insert($record);
1104
                        }
1105
 
1106
 
1107
 
1 www 1108
 
4 efrain 1109
                        if($record->id) {
1110
                            $extendUser->extend_company_id = $record->id;
1111
                        }
1112
                    }
1 www 1113
 
15452 efrain 1114
                    if($function) {
1115
                        $record = $companyMicrolearningExtendUserFunctionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $function);
4 efrain 1116
                        if(!$record) {
1117
                            $record = new CompanyMicrolearningExtendUserFunction();
1118
                            $record->company_id = $currentCompany->id;
15452 efrain 1119
                            $record->name = $function;
1 www 1120
 
4 efrain 1121
                            $companyMicrolearningExtendUserFunctionMapper->insert($record);
1122
                        }
1 www 1123
 
4 efrain 1124
                        if($record->id) {
1125
                            $extendUser->extend_function_id = $record->id;
1126
                        }
1127
                    }
1 www 1128
 
15452 efrain 1129
                    if($group) {
1130
                        $record = $companyMicrolearningExtendUserGroupMapper->fetchOneByCompanyIdAndName($currentCompany->id, $group);
4 efrain 1131
                        if(!$record) {
1132
                            $record = new CompanyMicrolearningExtendUserGroup();
1133
                            $record->company_id = $currentCompany->id;
15452 efrain 1134
                            $record->name = $group;
1 www 1135
 
4 efrain 1136
                            $companyMicrolearningExtendUserGroupMapper->insert($record);
1137
                        }
1 www 1138
 
4 efrain 1139
                        if($record->id) {
1140
                            $extendUser->extend_group_id = $record->id;
1141
                        }
1142
                    }
1 www 1143
 
15452 efrain 1144
                    if($institution) {
1145
                        $record = $companyMicrolearningExtendUserInstitutionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $institution);
4 efrain 1146
                        if(!$record) {
1147
                            $record = new CompanyMicrolearningExtendUserInstitution();
1148
                            $record->company_id = $currentCompany->id;
15452 efrain 1149
                            $record->name = $institution;
1 www 1150
 
4 efrain 1151
                            $companyMicrolearningExtendUserInstitutionMapper->insert($record);
1152
                        }
1 www 1153
 
4 efrain 1154
                        if($record->id) {
1155
                            $extendUser->extend_institution_id = $record->id;
1156
                        }
1157
                    }
1 www 1158
 
15452 efrain 1159
                    if($program) {
1160
                        $record = $companyMicrolearningExtendUserProgramMapper->fetchOneByCompanyIdAndName($currentCompany->id, $program);
4 efrain 1161
                        if(!$record) {
1162
                            $record = new CompanyMicrolearningExtendUserProgram();
1163
                            $record->company_id = $currentCompany->id;
15452 efrain 1164
                            $record->name = $program;
1 www 1165
 
4 efrain 1166
                            $companyMicrolearningExtendUserProgramMapper->insert($record);
1167
                        }
1 www 1168
 
4 efrain 1169
                        if($record->id) {
1170
                            $extendUser->extend_program_id = $record->id;
1171
                        }
1172
                    }
1 www 1173
 
15452 efrain 1174
                    if($partner) {
4 efrain 1175
 
1176
 
15452 efrain 1177
                        $record = $companyMicrolearningExtendUserPartnerMapper->fetchOneByCompanyIdAndName($currentCompany->id, $partner);
4 efrain 1178
                        if(!$record) {
1179
                            $record = new CompanyMicrolearningExtendUserPartner();
1180
                            $record->company_id = $currentCompany->id;
15452 efrain 1181
                            $record->name = $partner;
1 www 1182
 
4 efrain 1183
                            $companyMicrolearningExtendUserPartnerMapper->insert($record);
1184
                        }
1 www 1185
 
4 efrain 1186
                        if($record->id) {
1187
                            $extendUser->extend_partner_id = $record->id;
1188
                        }
1189
                    }
1 www 1190
 
15452 efrain 1191
                    if($sector) {
1192
                        $record = $companyMicrolearningExtendUserSectorMapper->fetchOneByCompanyIdAndName($currentCompany->id, $sector);
4 efrain 1193
                        if(!$record) {
1194
                            $record = new CompanyMicrolearningExtendUserSector();
1195
                            $record->company_id = $currentCompany->id;
15452 efrain 1196
                            $record->name = $sector;
1 www 1197
 
4 efrain 1198
                            $companyMicrolearningExtendUserSectorMapper->insert($record);
1199
                        }
1 www 1200
 
4 efrain 1201
                        if($record->id) {
1202
                            $extendUser->extend_sector_id = $record->id;
1203
                        }
1204
                    }
1 www 1205
 
15452 efrain 1206
                    if($studentType) {
1207
                        $record = $companyMicrolearningExtendUserStudentTypeMapper->fetchOneByCompanyIdAndName($currentCompany->id, $studentType);
4 efrain 1208
                        if(!$record) {
1209
                            $record = new CompanyMicrolearningExtendUserStudentType();
1210
                            $record->company_id = $currentCompany->id;
15452 efrain 1211
                            $record->name = $studentType;
1 www 1212
 
4 efrain 1213
                            $companyMicrolearningExtendUserStudentTypeMapper->insert($record);
1214
                        }
1215
                        if($record->id) {
1216
                            $extendUser->extend_student_type_id = $record->id;
1217
                        }
1218
                    }
1219
 
15452 efrain 1220
                    if($country) {
1221
                        $record = $companyMicrolearningExtendUserCountryMapper->fetchOneByCompanyIdAndName($currentCompany->id, $country);
1222
                        if(!$record) {
1223
                            $record = new CompanyMicrolearningExtendUserCountry();
1224
                            $record->company_id = $currentCompany->id;
1225
                            $record->name = $country;
1226
 
1227
                            $companyMicrolearningExtendUserCountryMapper->insert($record);
1228
                        }
1229
                        if($record->id) {
1230
                            $extendUser->extend_country_id = $record->id;
1231
                        }
1232
                    }
4 efrain 1233
 
1234
 
15452 efrain 1235
 
1236
 
1 www 1237
 
4 efrain 1238
                    if($extendUser->id) {
1239
                       $result =   $companyMicrolearningExtendUserMapper->update($extendUser);
1240
                    } else {
1241
                       $result = $companyMicrolearningExtendUserMapper->insert($extendUser);
1242
                    }
1243
 
1244
 
1245
 
1 www 1246
                }
1247
 
4 efrain 1248
 
1 www 1249
                foreach($user_ids as $user_id)
1250
                {
1251
                    $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user_id, $capsule->id);
1252
                    if(!$capsuleUser) {
1253
 
1254
                        $capsuleUser = new CompanyMicrolearningCapsuleUser();
1255
                        $capsuleUser->company_id = $capsule->company_id;
1256
                        $capsuleUser->topic_id = $capsule->topic_id;
1257
                        $capsuleUser->capsule_id = $capsule->id;
1258
                        $capsuleUser->user_id = $user_id;
1259
                        $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
1260
 
1261
                        if($capsuleUserMapper->insert($capsuleUser)) {
1262
                            $users_assigned++;
1263
 
1264
 
1265
                            $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
1266
                            if($capsuleUser) {
1267
                                $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
1268
                                $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
1269
                                if($companyMicrolearningUser) {
1270
                                    $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
1271
 
1272
                                    $companyMicrolearningUserMapper->update($companyMicrolearningUser);
1273
 
1274
                                } else {
1275
                                    $companyMicrolearningUser = new CompanyMicrolearningUser();
1276
                                    $companyMicrolearningUser->company_id = $capsuleUser->company_id;
1277
                                    $companyMicrolearningUser->user_id = $capsuleUser->user_id;
1278
                                    $companyMicrolearningUser->added_on = $capsuleUser->added_on;
1279
                                    $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
1280
 
1281
                                    $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
1282
                                }
1283
                            }
1284
                        }
1285
 
1286
                    }
1287
 
1288
 
1289
 
1290
                }
1291
 
1292
                return new JsonModel([
1293
                    'success' => true,
1294
                    'data' => [
1295
                        'users_assigned' => $users_assigned
1296
                    ]
1297
                ]);
1298
 
1299
 
1300
 
1301
 
1302
            } else {
1303
                return new JsonModel([
1304
                    'success' => false,
1305
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
1306
                ]);
1307
            }
1308
 
1309
 
1310
 
1311
 
1312
        }
1313
 
1314
        return new JsonModel([
1315
            'success' => false,
1316
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1317
        ]);
1318
    }
1319
 
1320
    public function notificationAction()
1321
    {
1322
        $request = $this->getRequest();
1323
 
1324
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1325
        $currentUser    = $currentUserPlugin->getUser();
1326
        $currentCompany = $currentUserPlugin->getCompany();
1327
 
1328
        $request    = $this->getRequest();
1329
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
1330
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
1331
 
1332
 
1333
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
1334
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
1335
        if(!$topic) {
1336
            return new JsonModel([
1337
                'success'   => false,
1338
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
1339
            ]);
1340
        }
1341
 
1342
 
1343
 
1344
        if($topic->company_id != $currentCompany->id) {
1345
            return new JsonModel([
1346
                'success'   => false,
1347
                'data'   => 'ERROR_UNAUTHORIZED'
1348
            ]);
1349
        }
1350
 
1351
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
1352
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
1353
        if(!$capsule) {
1354
            return new JsonModel([
1355
                'success'   => false,
1356
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
1357
            ]);
1358
        }
1359
 
1360
 
1361
        if($capsule->topic_id != $topic->id) {
1362
            return new JsonModel([
1363
                'success'   => false,
1364
                'data'   => 'ERROR_UNAUTHORIZED'
1365
            ]);
1366
        }
1367
 
1368
 
1369
        if($request->isPost()) {
1370
            $dataPost = $request->getPost()->toArray();
15390 efrain 1371
            $form = new PushMicrolearningNotificationForm($this->adapter, $currentCompany->id);
1 www 1372
 
1373
            $form->setData($dataPost);
1374
 
1375
            if($form->isValid()) {
1376
                $customer_uuids = $this->params()->fromPost('customer_uuids');
1377
                if(!$customer_uuids) {
1378
                    return new JsonModel([
1379
                        'success' => false,
1380
                        'data' => 'ERROR_NOT_SELECTED_CUSTOMERS'
1381
                    ]);
1382
 
1383
                }
1384
 
1385
                $push_template_uuid = filter_var($form->get('push_template_id')->getValue(), FILTER_SANITIZE_STRING);
1386
                $pushMapper = PushMapper::getInstance($this->adapter);
1387
                $pushTemplateMapper = PushTemplateMapper::getInstance($this->adapter);
1388
                $pushTemplate = $pushTemplateMapper->fetchOneByUuid($push_template_uuid);
1389
 
1390
                if(!$pushTemplate) {
1391
                    return new JsonModel([
1392
                        'success' => false,
1393
                        'data' => 'ERROR_PUSH_TEMPLATE_NOT_FOUND'
1394
                    ]);
1395
                }
1396
 
1397
 
1398
 
1399
                $applicationMapper = ApplicationMapper::getInstance($this->adapter);
1400
                $application = $applicationMapper->fetchOne(Application::TWOGETSKILLS);
1401
 
1402
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
1403
 
1404
                $push_to_send = 0;
1405
 
1406
 
1407
 
1408
                $userMapper = UserMapper::getInstance($this->adapter);
1409
                $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
1410
                foreach($customer_uuids as $customer_uuid)
1411
                {
1412
 
1413
 
1414
                    $user = $userMapper->fetchOneByUuid($customer_uuid);
1415
                    if(!$user) {
1416
                        continue;
1417
                    }
1418
 
1419
                    $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
1420
                    if(!$capsuleUser) {
1421
                        continue;
1422
                    }
1423
 
1424
 
13755 efrain 1425
                    //$token = $deviceHistoryMapper->fetchLastDeviceTokenByApplicationIdAndUserId(Application::TWOGETSKILLS, $user->id);
1426
                    $device = $deviceHistoryMapper->fetchLastDeviceByApplicationIdAndUserId(Application::TWOGETSKILLS, $user->id);
1 www 1427
 
13755 efrain 1428
 
1429
 
1430
                    if($device && $device->token) {
1431
 
1432
                        $key = $application->key;
1433
                        if($device->variant_id) {
1434
 
1435
                            $applicationVariantMapper = ApplicationVariantMapper::getInstance($this->adapter);
1436
                            $applicationVariant = $applicationVariantMapper->fetchOneByApplicationIdAndVariantId($device->application_id, $device->variant_id);
1437
                            if($applicationVariant) {
1438
                                $key = $applicationVariant->key;
1439
                            } else {
1440
                                $applicationVariant = $applicationVariantMapper->fetchOneByApplicationIdAndDefault($device->application_id);
1441
                                if($applicationVariant) {
1442
                                    $key = $applicationVariant->key;
1443
                                }
1444
                            }
1445
 
1446
                        }
1447
 
1 www 1448
                        $push = new Push();
1449
                        $push->status = Push::STATUS_PENDING;
1450
                        $push->data = json_encode([
1451
                            'server' => [
13755 efrain 1452
                                'key' => $key,
1 www 1453
                            ],
1454
                            'push' => [
1455
                                'registration_ids'   => [
13755 efrain 1456
                                    $device->token,
1 www 1457
                                ],
1458
                                'notification' => [
1459
                                    'body' =>  $pushTemplate->body,
1460
                                    'title' => $pushTemplate->title,
1461
                                    'vibrate' => 1,
1462
                                    'sound' =>  1
1463
                                ],
1671 efrain 1464
                                'data' => [
10435 efrain 1465
                                    'command' => 'content-refresh',
1466
                                    'new_capsules' => '0',
1671 efrain 1467
                                ]
1 www 1468
                            ]
1469
                        ]);
1470
 
1471
                        if($pushMapper->insert($push)) {
1472
                            $push_to_send = $push_to_send + 1;
1473
                        }
1474
 
1475
                    }
1476
                }
1477
 
1478
                if(0 == $push_to_send) {
1479
                    return new JsonModel([
1480
                        'success' => false,
1481
                        'data' => 'ERROR_NO_USER_DEVICES_WERE_FOUND_TO_SEND_PUSH'
1482
                    ]);
1483
                }
1484
 
1485
 
1486
 
1487
 
1488
 
1489
                return new JsonModel([
1490
                    'success' => true,
1491
                    'data' => [
1492
                        'push_to_send' => $push_to_send,
1493
                    ]
1494
                ]);
1495
 
1496
 
1497
 
1498
 
1499
 
1500
            } else {
1501
 
1502
                $messages = [];
1503
                $form_messages = (array) $form->getMessages();
1504
                foreach($form_messages  as $fieldname => $field_messages)
1505
                {
1506
                    $messages[$fieldname] = array_values($field_messages);
1507
                }
1508
 
1509
                return new JsonModel([
1510
                    'success'   => false,
1511
                    'data'      => $messages
1512
                ]);
1513
            }
1514
        }
1515
 
1516
        return new JsonModel([
1517
            'success' => false,
1518
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1519
        ]);
1520
    }
1521
 
1522
 
1523
}