Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
1320 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Db\Adapter\AdapterInterface;
16766 efrain 7
use LeadersLinked\Cache\CacheInterface;
1320 efrain 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;
56
use LeadersLinked\Mapper\CompanyMicrolearningCapsuleCommentMapper;
16766 efrain 57
use LeadersLinked\Library\Functions;
1320 efrain 58
 
59
 
60
class MicrolearningAccessForStudentsController extends AbstractActionController
61
{
62
    /**
63
     *
64
     * @var AdapterInterface
65
     */
66
    private $adapter;
67
 
68
 
69
    /**
70
     *
16766 efrain 71
     * @var CacheInterface
1320 efrain 72
     */
73
    private $cache;
74
 
75
    /**
76
     *
77
     * @var  LoggerInterface
78
     */
79
    private $logger;
80
 
81
 
82
    /**
83
     *
84
     * @var array
85
     */
86
    private $config;
87
 
88
    /**
89
     *
90
     * @param AdapterInterface $adapter
16766 efrain 91
     * @param CacheInterface $cache
1320 efrain 92
     * @param LoggerInterface $logger
93
     * @param array $config
94
     */
95
    public function __construct($adapter, $cache , $logger,  $config)
96
    {
97
        $this->adapter      = $adapter;
98
        $this->cache        = $cache;
99
        $this->logger       = $logger;
100
        $this->config       = $config;
101
 
102
    }
103
 
104
 
105
 
106
 
107
 
108
 
109
    public function revokeAction()
110
    {
111
        $request = $this->getRequest();
112
 
113
        $currentUserPlugin = $this->plugin('currentUserPlugin');
114
        $currentUser    = $currentUserPlugin->getUser();
115
        $currentCompany = $currentUserPlugin->getCompany();
116
 
117
        $request    = $this->getRequest();
118
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
119
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
120
        $user_uuid   = $this->params()->fromRoute('user_uuid');
121
 
122
 
123
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
124
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
125
        if(!$topic) {
126
            return new JsonModel([
127
                'success'   => false,
128
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
129
            ]);
130
        }
131
 
132
 
133
 
134
        if($topic->company_id != $currentCompany->id) {
135
            return new JsonModel([
136
                'success'   => false,
137
                'data'   => 'ERROR_UNAUTHORIZED'
138
            ]);
139
        }
140
 
141
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
142
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
143
        if(!$capsule) {
144
            return new JsonModel([
145
                'success'   => false,
146
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
147
            ]);
148
        }
149
 
150
 
151
        if($capsule->topic_id != $topic->id) {
152
            return new JsonModel([
153
                'success'   => false,
154
                'data'   => 'ERROR_UNAUTHORIZED'
155
            ]);
156
        }
157
 
158
        $userMapper = UserMapper::getInstance($this->adapter);
159
        $user = $userMapper->fetchOneByUuid($user_uuid);
160
 
161
        if(!$user) {
162
            return new JsonModel([
163
                'success'   => false,
164
                'data'   => 'ERROR_USER_NOT_FOUND'
165
            ]);
166
        }
167
 
168
        $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
169
        $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
170
        if(!$capsuleUser) {
171
            return new JsonModel([
172
                'success'   => false,
173
                'data'   => 'ERROR_UNAUTHORIZED'
174
            ]);
175
        }
176
 
177
        if($request->isPost()) {
178
 
179
 
180
            if($capsuleUser->access != CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED) {
181
                return new JsonModel([
182
                    'success'   => false,
183
                    'data'   => 'ERROR_USER_ACCESS_CANNT_BE_REVOKE'
184
                ]);
185
            }
186
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_REVOKE;
187
            if($capsuleUserMapper->update($capsuleUser)) {
188
 
189
                $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
190
                if($capsuleUser) {
191
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
192
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
193
                    if($companyMicrolearningUser) {
194
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
195
 
196
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
197
 
198
                    } else {
199
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
200
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
201
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
202
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
203
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
204
 
205
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
206
                    }
207
                }
208
 
209
                return new JsonModel([
210
                    'success' => true,
211
                    'data' => 'LABEL_USER_ACCESS_HAS_BEEN_REVOKE'
212
                ]);
213
 
214
            } else {
215
 
216
                return new JsonModel([
217
                    'success'   => false,
218
                    'data'      => $capsuleUserMapper->getError()
219
                ]);
220
            }
221
        }
222
 
223
        return new JsonModel([
224
            'success' => false,
225
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
226
        ]);
227
    }
228
 
229
    public function unlimitAction()
230
    {
231
        $request = $this->getRequest();
232
 
233
        $currentUserPlugin = $this->plugin('currentUserPlugin');
234
        $currentUser    = $currentUserPlugin->getUser();
235
        $currentCompany = $currentUserPlugin->getCompany();
236
 
237
        $request    = $this->getRequest();
238
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
239
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
240
        $user_uuid   = $this->params()->fromRoute('user_uuid');
241
 
242
 
243
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
244
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
245
        if(!$topic) {
246
            return new JsonModel([
247
                'success'   => false,
248
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
249
            ]);
250
        }
251
 
252
 
253
 
254
        if($topic->company_id != $currentCompany->id) {
255
            return new JsonModel([
256
                'success'   => false,
257
                'data'   => 'ERROR_UNAUTHORIZED'
258
            ]);
259
        }
260
 
261
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
262
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
263
        if(!$capsule) {
264
            return new JsonModel([
265
                'success'   => false,
266
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
267
            ]);
268
        }
269
 
270
 
271
        if($capsule->topic_id != $topic->id) {
272
            return new JsonModel([
273
                'success'   => false,
274
                'data'   => 'ERROR_UNAUTHORIZED'
275
            ]);
276
        }
277
 
278
        $userMapper = UserMapper::getInstance($this->adapter);
279
        $user = $userMapper->fetchOneByUuid($user_uuid);
280
 
281
        if(!$user) {
282
            return new JsonModel([
283
                'success'   => false,
284
                'data'   => 'ERROR_USER_NOT_FOUND'
285
            ]);
286
        }
287
 
288
        $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
289
        $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
290
        if(!$capsuleUser) {
291
            return new JsonModel([
292
                'success'   => false,
293
                'data'   => 'ERROR_UNAUTHORIZED'
294
            ]);
295
        }
296
 
297
        if($request->isPost()) {
298
 
299
 
300
            if($capsuleUser->access != CompanyMicrolearningCapsuleUser::ACCESS_REVOKE) {
301
                return new JsonModel([
302
                    'success'   => false,
303
                    'data'   => 'ERROR_USER_ACCESS_CANNT_BE_UNLIMIT'
304
                ]);
305
            }
306
            $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
307
            if($capsuleUserMapper->update($capsuleUser)) {
308
 
309
                $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
310
                if($capsuleUser) {
311
                    $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
312
                    $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
313
                    if($companyMicrolearningUser) {
314
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
315
 
316
                        $companyMicrolearningUserMapper->update($companyMicrolearningUser);
317
 
318
                    } else {
319
                        $companyMicrolearningUser = new CompanyMicrolearningUser();
320
                        $companyMicrolearningUser->company_id = $capsuleUser->company_id;
321
                        $companyMicrolearningUser->user_id = $capsuleUser->user_id;
322
                        $companyMicrolearningUser->added_on = $capsuleUser->added_on;
323
                        $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
324
 
325
                        $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
326
                    }
327
                }
328
 
329
                return new JsonModel([
330
                    'success' => true,
331
                    'data' => 'LABEL_USER_ACCESS_HAS_BEEN_UNLIMITED'
332
                ]);
333
 
334
            } else {
335
 
336
                return new JsonModel([
337
                    'success'   => false,
338
                    'data'      => $capsuleUserMapper->getError()
339
                ]);
340
            }
341
        }
342
 
343
        return new JsonModel([
344
            'success' => false,
345
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
346
        ]);
347
    }
348
 
349
    public function uploadAction()
350
    {
351
        $request = $this->getRequest();
352
 
353
        $currentUserPlugin = $this->plugin('currentUserPlugin');
354
        $currentUser    = $currentUserPlugin->getUser();
355
        $currentCompany = $currentUserPlugin->getCompany();
356
 
357
        $request    = $this->getRequest();
358
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
359
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
360
 
361
 
362
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
363
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
364
        if(!$topic) {
365
            return new JsonModel([
366
                'success'   => false,
367
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
368
            ]);
369
        }
370
 
371
 
372
 
373
        if($topic->company_id != $currentCompany->id) {
374
            return new JsonModel([
375
                'success'   => false,
376
                'data'   => 'ERROR_UNAUTHORIZED'
377
            ]);
378
        }
379
 
380
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
381
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
382
        if(!$capsule) {
383
            return new JsonModel([
384
                'success'   => false,
385
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
386
            ]);
387
        }
388
 
389
 
390
        if($capsule->topic_id != $topic->id) {
391
            return new JsonModel([
392
                'success'   => false,
393
                'data'   => 'ERROR_UNAUTHORIZED'
394
            ]);
395
        }
396
 
397
 
398
 
399
        if($request->isPost()) {
400
 
16766 efrain 401
            $step = Functions::sanitizeFilterString($this->params()->fromPost('step'));
1320 efrain 402
            if($step == 'validation') {
403
                $userMapper = UserMapper::getInstance($this->adapter);
404
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
405
 
406
                $form = new  CapsuleCustomerUploadForm();
407
                $dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
408
 
409
                $form->setData($dataPost);
410
 
411
                if($form->isValid()) {
412
 
413
                    $file = $_FILES['file'];
414
                    $tmp_filename = $file['tmp_name'];
415
                    $final_filename =  'data/' . $file['name'];
416
 
417
                    if(!move_uploaded_file($tmp_filename, $final_filename)) {
418
                        return new JsonModel([
419
                            'success' => false,
420
                            'data' => 'ERROR_UPLOAD_FILE'
421
                        ]);
422
                    }
423
 
424
 
425
                    $users = [];
426
 
427
 
428
                    $spreadsheet = IOFactory::load($final_filename);
429
                    $records = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
430
 
431
                    $emails = [];
432
 
433
                    foreach($records as $record)
434
                    {
435
                        //A = Nombre 	B = Apellidos	C = Email 	D = contraseña
436
 
437
 
438
                        $email = trim(filter_var($record['C'], FILTER_SANITIZE_EMAIL));
16766 efrain 439
                        $first_name = Functions::sanitizeFilterString($record['A']);
440
                        $last_name = Functions::sanitizeFilterString($record['B']);
441
                        $password = Functions::sanitizeFilterString($record['D']);
1320 efrain 442
 
16766 efrain 443
                        $filterCompany =  isset($record['E']) ? Functions::sanitizeFilterString($record['E']) : '';
444
                        $filterFunction = isset($record['F']) ? Functions::sanitizeFilterString($record['F']) : '';
445
                        $filterGroup = isset($record['G']) ? Functions::sanitizeFilterString($record['G']) : '';
446
                        $filterInstitution = isset($record['H']) ? Functions::sanitizeFilterString($record['H']) : '';
447
                        $filterProgram = isset($record['I']) ? Functions::sanitizeFilterString($record['I']) : '';
448
                        $filterPartner = isset($record['J']) ? Functions::sanitizeFilterString($record['J']) : '';
449
                        $filterSector = isset($record['K']) ? Functions::sanitizeFilterString($record['K']) : '';
450
                        $filterStudentType = isset($record['L']) ? Functions::sanitizeFilterString($record['L']) : '';
1320 efrain 451
 
452
 
453
                        //||  empty($password)
454
                        if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
455
                            continue;
456
                        }
457
 
458
                        if(!in_array($email, $emails)) {
459
 
460
                            $user = $userMapper->fetchOneByEmail($email);
461
                            if($user) {
462
                                $assigned_capsules =  $capsuleUserMapper->fetchCountByCompanyIdAndTopicIdAndUserId($topic->company_id, $topic->id, $user->id);
463
                            } else {
464
                                $assigned_capsules = 0;
465
                            }
466
 
467
                            array_push($emails, $email);
468
                            array_push($users, [
469
                                'first_name' => $first_name,
470
                                'last_name' => $last_name,
471
                                'password'  => $password,
472
                                'email' => $email,
473
                                'assigned_capsules' => $assigned_capsules,
474
                                'filterCompany' => $filterCompany,
475
                                'filterFunction' => $filterFunction,
476
                                'filterGroup' => $filterGroup,
477
                                'filterInstitution' => $filterInstitution,
478
                                'filterProgram' => $filterProgram,
479
                                'filterPartner' => $filterPartner,
480
                                'filterSector' => $filterSector,
481
                                'filterStudentType' => $filterStudentType
482
                            ]);
483
                        }
484
 
485
 
486
 
487
 
488
 
489
                    }
490
 
491
                    $key = md5($currentUser->id . '-' . $topic->uuid . '-' . $capsule->uuid);
492
                    $this->cache->setItem($key, serialize($users));
493
 
494
                    return new JsonModel([
495
                        'success' => true,
496
                        'data' => [
497
                            'key' => $key,
498
                            'topic' => $topic->name,
499
                            'capsule' => $capsule->name,
500
                            'items' => $users,
501
                        ]
502
                    ]);
503
 
504
 
505
 
506
                } else {
507
                    $messages = [];
508
                    $form_messages = (array) $form->getMessages();
509
                    foreach($form_messages  as $fieldname => $field_messages)
510
                    {
511
 
512
                        $messages[$fieldname] = array_values($field_messages);
513
                    }
514
 
515
                    return new JsonModel([
516
                        'success'   => false,
517
                        'data'   => $messages
518
                    ]);
519
                }
520
            } else if($step == 'process') {
521
 
16766 efrain 522
                $key = Functions::sanitizeFilterString($this->params()->fromPost('key'));
1320 efrain 523
                if(!$key) {
524
                    return new JsonModel([
525
                        'success' => false,
526
                        'data' => 'ERROR_CACHE_KEY_EMPTY'
527
                    ]);
528
                }
529
 
530
                $value = $this->cache->getItem($key);
531
                if(!$value) {
532
 
533
                    return new JsonModel([
534
                        'success' => false,
535
                        'data' => 'ERROR_CACHE_NOT_FOUND'
536
                    ]);
537
                }
538
 
539
                $records = unserialize($value);
540
                if(!$records) {
541
                    return new JsonModel([
542
                        'success' => false,
543
                        'data' => 'ERROR_CACHE_INVALID'
544
                    ]);
545
                }
546
 
547
                $userMapper = UserMapper::getInstance($this->adapter);
548
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
549
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
550
 
551
                $companyMicrolearningExtendUserMapper = CompanyMicrolearningExtendUserMapper::getInstance($this->adapter);
552
                $companyMicrolearningExtendUserCompanyMapper = CompanyMicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
553
                $companyMicrolearningExtendUserFunctionMapper = CompanyMicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
554
                $companyMicrolearningExtendUserGroupMapper = CompanyMicrolearningExtendUserGroupMapper::getInstance($this->adapter);
555
                $companyMicrolearningExtendUserInstitutionMapper = CompanyMicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
556
                $companyMicrolearningExtendUserProgramMapper = CompanyMicrolearningExtendUserProgramMapper::getInstance($this->adapter);
557
                $companyMicrolearningExtendUserPartnerMapper = CompanyMicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
558
                $companyMicrolearningExtendUserSectorMapper = CompanyMicrolearningExtendUserSectorMapper::getInstance($this->adapter);
559
                $companyMicrolearningExtendUserStudentTypeMapper = CompanyMicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
560
 
561
 
562
 
563
 
564
                $users_assigned = 0;
565
                $user_ids = [];
566
                foreach($records as $record)
567
                {
568
                    $first_name = $record['first_name'];
569
                    $last_name = $record['last_name'];
570
                    $password = $record['password'];
571
                    $email = $record['email'];
572
 
573
 
574
                    $filterCompany = $record['filterCompany'];
575
                    $filterFunction = $record['filterFunction'];
576
                    $filterGroup = $record['filterGroup'];
577
                    $filterInstitution = $record['filterInstitution'];
578
                    $filterProgram = $record['filterProgram'];
579
                    $filterPartner = $record['filterPartner'];
580
                    $filterSector = $record['filterSector'];
581
                    $filterStudentType = $record['filterStudentType'];
582
 
583
 
584
                    $user = $userMapper->fetchOneByEmail($email);
585
                    if(!$user) {
586
 
587
 
588
                        if($password) {
589
                            $password_hash = password_hash($password, PASSWORD_DEFAULT);
590
 
591
                            $user = new User();
592
 
593
                            $user->blocked = User::BLOCKED_NO;
594
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
595
                            $user->email = $email;
596
                            $user->first_name = $first_name;
597
                            $user->last_name = $last_name;
598
                            $user->password = $password_hash;
599
                            $user->login_attempt = 0;
600
                            $user->usertype_id = UserType::USER;
601
                            $user->status = User::STATUS_ACTIVE;
602
 
603
                            $result = $userMapper->insert($user);
604
                            if($result) {
605
                                $userPassword = new UserPassword();
606
                                $userPassword->user_id = $user->id;
607
                                $userPassword->password = $password_hash;
608
                                $userPasswordMapper->insert($userPassword);
609
                            } else {
610
                                continue;
611
                            }
612
                        }
613
                    } else {
614
 
615
                        if($user->email_verified == User::EMAIL_VERIFIED_NO || $user->status != User::STATUS_ACTIVE) {
616
                            $user->email_verified = User::EMAIL_VERIFIED_YES;
617
                            $user->status != User::STATUS_ACTIVE;
618
 
619
                            if(!$userMapper->update($user)) {
620
                                continue;
621
                            }
622
                        }
623
 
624
 
625
                    }
626
 
627
                    if(!in_array($user->id, $user_ids)) {
628
                        array_push($user_ids, $user->id);
629
                    }
630
 
631
                    /*
632
                    echo '$filterCompany = ' . $filterCompany . PHP_EOL;
633
                    echo '$filterFunctio = ' . $filterFunction . PHP_EOL;
634
                    echo '$filterGroup = ' . $filterGroup . PHP_EOL;
635
                    echo '$filterInstitution = ' . $filterInstitution . PHP_EOL;
636
                    echo '$filterPartner = ' . $filterPartner . PHP_EOL;
637
                    echo '$filterProgram = ' . $filterProgram . PHP_EOL;
638
                    echo '$filterSector = ' . $filterSector . PHP_EOL;
639
                    echo '$filterStudentType = ' . $filterStudentType . PHP_EOL;
640
                  */
641
 
642
 
643
                    $extendUser = $companyMicrolearningExtendUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
644
                    if(!$extendUser) {
645
                        $extendUser = new CompanyMicrolearningExtendUser();
646
                        $extendUser->company_id = $currentCompany->id;
647
                        $extendUser->user_id = $user->id;
648
                    }
649
 
650
 
651
                    if($filterCompany) {
652
                        $record = $companyMicrolearningExtendUserCompanyMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterCompany);
653
 
654
                        if(!$record) {
655
                            $record = new CompanyMicrolearningExtendUserCompany();
656
                            $record->company_id = $currentCompany->id;
657
                            $record->name = $filterCompany;
658
 
659
                            $companyMicrolearningExtendUserCompanyMapper->insert($record);
660
                        }
661
 
662
 
663
 
664
 
665
                        if($record->id) {
666
                            $extendUser->extend_company_id = $record->id;
667
                        }
668
                    }
669
 
670
                    if($filterFunction) {
671
                        $record = $companyMicrolearningExtendUserFunctionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterFunction);
672
                        if(!$record) {
673
                            $record = new CompanyMicrolearningExtendUserFunction();
674
                            $record->company_id = $currentCompany->id;
675
                            $record->name = $filterFunction;
676
 
677
                            $companyMicrolearningExtendUserFunctionMapper->insert($record);
678
                        }
679
 
680
                        if($record->id) {
681
                            $extendUser->extend_function_id = $record->id;
682
                        }
683
                    }
684
 
685
                    if($filterGroup) {
686
                        $record = $companyMicrolearningExtendUserGroupMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterGroup);
687
                        if(!$record) {
688
                            $record = new CompanyMicrolearningExtendUserGroup();
689
                            $record->company_id = $currentCompany->id;
690
                            $record->name = $filterGroup;
691
 
692
                            $companyMicrolearningExtendUserGroupMapper->insert($record);
693
                        }
694
 
695
                        if($record->id) {
696
                            $extendUser->extend_group_id = $record->id;
697
                        }
698
                    }
699
 
700
                    if($filterInstitution) {
701
                        $record = $companyMicrolearningExtendUserInstitutionMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterInstitution);
702
                        if(!$record) {
703
                            $record = new CompanyMicrolearningExtendUserInstitution();
704
                            $record->company_id = $currentCompany->id;
705
                            $record->name = $filterInstitution;
706
 
707
                            $companyMicrolearningExtendUserInstitutionMapper->insert($record);
708
                        }
709
 
710
                        if($record->id) {
711
                            $extendUser->extend_institution_id = $record->id;
712
                        }
713
                    }
714
 
715
                    if($filterProgram) {
716
                        $record = $companyMicrolearningExtendUserProgramMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterProgram);
717
                        if(!$record) {
718
                            $record = new CompanyMicrolearningExtendUserProgram();
719
                            $record->company_id = $currentCompany->id;
720
                            $record->name = $filterProgram;
721
 
722
                            $companyMicrolearningExtendUserProgramMapper->insert($record);
723
                        }
724
 
725
                        if($record->id) {
726
                            $extendUser->extend_program_id = $record->id;
727
                        }
728
                    }
729
 
730
                    if($filterPartner) {
731
 
732
 
733
                        $record = $companyMicrolearningExtendUserPartnerMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterPartner);
734
                        if(!$record) {
735
                            $record = new CompanyMicrolearningExtendUserPartner();
736
                            $record->company_id = $currentCompany->id;
737
                            $record->name = $filterPartner;
738
 
739
                            $companyMicrolearningExtendUserPartnerMapper->insert($record);
740
                        }
741
 
742
                        if($record->id) {
743
                            $extendUser->extend_partner_id = $record->id;
744
                        }
745
                    }
746
 
747
                    if($filterSector) {
748
                        $record = $companyMicrolearningExtendUserSectorMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterSector);
749
                        if(!$record) {
750
                            $record = new CompanyMicrolearningExtendUserSector();
751
                            $record->company_id = $currentCompany->id;
752
                            $record->name = $filterSector;
753
 
754
                            $companyMicrolearningExtendUserSectorMapper->insert($record);
755
                        }
756
 
757
                        if($record->id) {
758
                            $extendUser->extend_sector_id = $record->id;
759
                        }
760
                    }
761
 
762
                    if($filterStudentType) {
763
                        $record = $companyMicrolearningExtendUserStudentTypeMapper->fetchOneByCompanyIdAndName($currentCompany->id, $filterStudentType);
764
                        if(!$record) {
765
                            $record = new CompanyMicrolearningExtendUserStudentType();
766
                            $record->company_id = $currentCompany->id;
767
                            $record->name = $filterStudentType;
768
 
769
                            $companyMicrolearningExtendUserStudentTypeMapper->insert($record);
770
                        }
771
                        if($record->id) {
772
                            $extendUser->extend_student_type_id = $record->id;
773
                        }
774
                    }
775
 
776
                    //print_r($extendUser); exit;
777
 
778
 
779
 
780
                    if($extendUser->id) {
781
                       $result =   $companyMicrolearningExtendUserMapper->update($extendUser);
782
                    } else {
783
                       $result = $companyMicrolearningExtendUserMapper->insert($extendUser);
784
                    }
785
 
786
 
787
 
788
                }
789
 
790
 
791
                foreach($user_ids as $user_id)
792
                {
793
                    $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user_id, $capsule->id);
794
                    if(!$capsuleUser) {
795
 
796
                        $capsuleUser = new CompanyMicrolearningCapsuleUser();
797
                        $capsuleUser->company_id = $capsule->company_id;
798
                        $capsuleUser->topic_id = $capsule->topic_id;
799
                        $capsuleUser->capsule_id = $capsule->id;
800
                        $capsuleUser->user_id = $user_id;
801
                        $capsuleUser->access = CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED;
802
 
803
                        if($capsuleUserMapper->insert($capsuleUser)) {
804
                            $users_assigned++;
805
 
806
 
807
                            $capsuleUser = $capsuleUserMapper->fetchOne($capsuleUser->id);
808
                            if($capsuleUser) {
809
                                $companyMicrolearningUserMapper = CompanyMicrolearningUserMapper::getInstance($this->adapter);
810
                                $companyMicrolearningUser = $companyMicrolearningUserMapper->fetchOneByUserIdAndCompanyId($capsuleUser->user_id, $capsuleUser->company_id);
811
                                if($companyMicrolearningUser) {
812
                                    $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
813
 
814
                                    $companyMicrolearningUserMapper->update($companyMicrolearningUser);
815
 
816
                                } else {
817
                                    $companyMicrolearningUser = new CompanyMicrolearningUser();
818
                                    $companyMicrolearningUser->company_id = $capsuleUser->company_id;
819
                                    $companyMicrolearningUser->user_id = $capsuleUser->user_id;
820
                                    $companyMicrolearningUser->added_on = $capsuleUser->added_on;
821
                                    $companyMicrolearningUser->updated_on = $capsuleUser->updated_on;
822
 
823
                                    $companyMicrolearningUserMapper->insert($companyMicrolearningUser);
824
                                }
825
                            }
826
                        }
827
 
828
                    }
829
 
830
 
831
 
832
                }
833
 
834
                return new JsonModel([
835
                    'success' => true,
836
                    'data' => [
837
                        'users_assigned' => $users_assigned
838
                    ]
839
                ]);
840
 
841
 
842
 
843
 
844
            } else {
845
                return new JsonModel([
846
                    'success' => false,
847
                    'data' => 'ERROR_PARAMETERS_ARE_INVALID'
848
                ]);
849
            }
850
 
851
 
852
 
853
 
854
        }
855
 
856
        return new JsonModel([
857
            'success' => false,
858
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
859
        ]);
860
    }
861
 
862
    public function notificationAction()
863
    {
864
        $request = $this->getRequest();
865
 
866
        $currentUserPlugin = $this->plugin('currentUserPlugin');
867
        $currentUser    = $currentUserPlugin->getUser();
868
        $currentCompany = $currentUserPlugin->getCompany();
869
 
870
        $request    = $this->getRequest();
871
        $topic_uuid     = $this->params()->fromRoute('topic_uuid');
872
        $capsule_uuid   = $this->params()->fromRoute('capsule_uuid');
873
 
874
 
875
        $topicMapper = CompanyMicrolearningTopicMapper::getInstance($this->adapter);
876
        $topic = $topicMapper->fetchOneByUuid($topic_uuid);
877
        if(!$topic) {
878
            return new JsonModel([
879
                'success'   => false,
880
                'data'   => 'ERROR_TOPIC_NOT_FOUND'
881
            ]);
882
        }
883
 
884
 
885
 
886
        if($topic->company_id != $currentCompany->id) {
887
            return new JsonModel([
888
                'success'   => false,
889
                'data'   => 'ERROR_UNAUTHORIZED'
890
            ]);
891
        }
892
 
893
        $capsuleMapper = CompanyMicrolearningCapsuleMapper::getInstance($this->adapter);
894
        $capsule = $capsuleMapper->fetchOneByUuid($capsule_uuid);
895
        if(!$capsule) {
896
            return new JsonModel([
897
                'success'   => false,
898
                'data'   => 'ERROR_CAPSULE_NOT_FOUND'
899
            ]);
900
        }
901
 
902
 
903
        if($capsule->topic_id != $topic->id) {
904
            return new JsonModel([
905
                'success'   => false,
906
                'data'   => 'ERROR_UNAUTHORIZED'
907
            ]);
908
        }
909
 
910
 
911
        if($request->isPost()) {
912
            $dataPost = $request->getPost()->toArray();
913
            $form = new PushMicrolearningNotificationForm($this->adapter);
914
 
915
            $form->setData($dataPost);
916
 
917
            if($form->isValid()) {
918
                $customer_uuids = $this->params()->fromPost('customer_uuids');
919
                if(!$customer_uuids) {
920
                    return new JsonModel([
921
                        'success' => false,
922
                        'data' => 'ERROR_NOT_SELECTED_CUSTOMERS'
923
                    ]);
924
 
925
                }
926
 
16766 efrain 927
                $push_template_uuid = Functions::sanitizeFilterString($form->get('push_template_id')->getValue());
1320 efrain 928
                $pushMapper = PushMapper::getInstance($this->adapter);
929
                $pushTemplateMapper = PushTemplateMapper::getInstance($this->adapter);
930
                $pushTemplate = $pushTemplateMapper->fetchOneByUuid($push_template_uuid);
931
 
932
                if(!$pushTemplate) {
933
                    return new JsonModel([
934
                        'success' => false,
935
                        'data' => 'ERROR_PUSH_TEMPLATE_NOT_FOUND'
936
                    ]);
937
                }
938
 
939
 
940
 
941
                $applicationMapper = ApplicationMapper::getInstance($this->adapter);
942
                $application = $applicationMapper->fetchOne(Application::TWOGETSKILLS);
943
 
944
                $capsuleUserMapper = CompanyMicrolearningCapsuleUserMapper::getInstance($this->adapter);
945
 
946
                $push_to_send = 0;
947
 
948
 
949
 
950
                $userMapper = UserMapper::getInstance($this->adapter);
951
                $deviceHistoryMapper = DeviceHistoryMapper::getInstance($this->adapter);
952
                foreach($customer_uuids as $customer_uuid)
953
                {
954
 
955
 
956
                    $user = $userMapper->fetchOneByUuid($customer_uuid);
957
                    if(!$user) {
958
                        continue;
959
                    }
960
 
961
                    $capsuleUser = $capsuleUserMapper->fetchOneByUserIdAndCapsuleId($user->id, $capsule->id);
962
                    if(!$capsuleUser) {
963
                        continue;
964
                    }
965
 
966
 
967
                    $token = $deviceHistoryMapper->fetchLastDeviceTokenByApplicationIdAndUserId(Application::TWOGETSKILLS, $user->id);
968
 
969
                    if($token) {
970
                        $push = new Push();
971
                        $push->status = Push::STATUS_PENDING;
972
                        $push->data = json_encode([
973
                            'server' => [
974
                                'key' =>   $application->key,
975
                            ],
976
                            'push' => [
977
                                'registration_ids'   => [
978
                                    $token,
979
                                ],
980
                                'notification' => [
981
                                    'body' =>  $pushTemplate->body,
982
                                    'title' => $pushTemplate->title,
983
                                    'vibrate' => 1,
984
                                    'sound' =>  1
985
                                ],
986
                            ]
987
                        ]);
988
 
989
                        if($pushMapper->insert($push)) {
990
                            $push_to_send = $push_to_send + 1;
991
                        }
992
 
993
                    }
994
                }
995
 
996
                if(0 == $push_to_send) {
997
                    return new JsonModel([
998
                        'success' => false,
999
                        'data' => 'ERROR_NO_USER_DEVICES_WERE_FOUND_TO_SEND_PUSH'
1000
                    ]);
1001
                }
1002
 
1003
 
1004
 
1005
 
1006
 
1007
                return new JsonModel([
1008
                    'success' => true,
1009
                    'data' => [
1010
                        'push_to_send' => $push_to_send,
1011
                    ]
1012
                ]);
1013
 
1014
 
1015
 
1016
 
1017
 
1018
            } else {
1019
 
1020
                $messages = [];
1021
                $form_messages = (array) $form->getMessages();
1022
                foreach($form_messages  as $fieldname => $field_messages)
1023
                {
1024
                    $messages[$fieldname] = array_values($field_messages);
1025
                }
1026
 
1027
                return new JsonModel([
1028
                    'success'   => false,
1029
                    'data'      => $messages
1030
                ]);
1031
            }
1032
        }
1033
 
1034
        return new JsonModel([
1035
            'success' => false,
1036
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
1037
        ]);
1038
    }
1039
 
1040
 
1041
}