Proyectos de Subversion LeadersLinked - Backend

Rev

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