Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | | 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;
16768 efrain 7
 
1 www 8
use Laminas\Mvc\Controller\AbstractActionController;
9
use Laminas\Log\LoggerInterface;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
use LeadersLinked\Library\Functions;
17002 efrain 13
use LeadersLinked\Mapper\MicrolearningTopicMapper;
14
use LeadersLinked\Mapper\MicrolearningCapsuleMapper;
1 www 15
use LeadersLinked\Form\PushMicrolearningNotificationForm;
17002 efrain 16
use LeadersLinked\Mapper\MicrolearningCapsuleUserMapper;
1 www 17
use LeadersLinked\Mapper\UserMapper;
18
 
19
use LeadersLinked\Model\User;
20
 
17002 efrain 21
use LeadersLinked\Mapper\MicrolearningUserLogMapper;
22
use LeadersLinked\Mapper\MicrolearningSlideMapper;
1 www 23
use LeadersLinked\Form\ExtendUserMicrolearningForm;
24
use LeadersLinked\Form\ChangePasswordForm;
17002 efrain 25
use LeadersLinked\Mapper\MicrolearningUserProgressMapper;
26
use LeadersLinked\Model\MicrolearningExtendUser;
27
use LeadersLinked\Mapper\MicrolearningExtendUserMapper;
1 www 28
 
17002 efrain 29
use LeadersLinked\Mapper\MicrolearningExtendUserCompanyMapper;
30
use LeadersLinked\Mapper\MicrolearningExtendUserFunctionMapper;
31
use LeadersLinked\Mapper\MicrolearningExtendUserGroupMapper;
32
use LeadersLinked\Mapper\MicrolearningExtendUserInstitutionMapper;
33
use LeadersLinked\Mapper\MicrolearningExtendUserProgramMapper;
34
use LeadersLinked\Mapper\MicrolearningExtendUserPartnerMapper;
35
use LeadersLinked\Mapper\MicrolearningExtendUserSectorMapper;
36
use LeadersLinked\Mapper\MicrolearningExtendUserStudentTypeMapper;
1 www 37
use LeadersLinked\Mapper\CompanyUserMapper;
38
use LeadersLinked\Mapper\UserPasswordMapper;
39
 
40
 
41
class MicrolearningStudentsController extends AbstractActionController
42
{
43
    /**
44
     *
16769 efrain 45
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 46
     */
47
    private $adapter;
48
 
49
    /**
50
     *
16769 efrain 51
     * @var \LeadersLinked\Cache\CacheInterface
1 www 52
     */
16769 efrain 53
    private $cache;
54
 
55
 
56
    /**
57
     *
58
     * @var \Laminas\Log\LoggerInterface
59
     */
1 www 60
    private $logger;
61
 
62
    /**
63
     *
64
     * @var array
65
     */
66
    private $config;
67
 
16769 efrain 68
 
1 www 69
    /**
70
     *
16769 efrain 71
     * @var \Laminas\Mvc\I18n\Translator
72
     */
73
    private $translator;
74
 
75
 
76
    /**
77
     *
78
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
79
     * @param \LeadersLinked\Cache\CacheInterface $cache
80
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 81
     * @param array $config
16769 efrain 82
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 83
     */
16769 efrain 84
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 85
    {
16769 efrain 86
        $this->adapter      = $adapter;
87
        $this->cache        = $cache;
88
        $this->logger       = $logger;
89
        $this->config       = $config;
90
        $this->translator   = $translator;
1 www 91
    }
92
 
93
    public function indexAction()
94
    {
95
        $currentUserPlugin = $this->plugin('currentUserPlugin');
96
        $currentUser = $currentUserPlugin->getUser();
97
        $currentCompany = $currentUserPlugin->getCompany();
98
 
99
        $request = $this->getRequest();
100
 
101
        if($request->isGet())
102
        {
103
 
104
            $headers  = $request->getHeaders();
105
 
106
            $isJson = false;
107
            if($headers->has('Accept')) {
108
                $accept = $headers->get('Accept');
109
 
110
                $prioritized = $accept->getPrioritized();
111
 
112
                foreach($prioritized as $key => $value) {
113
                    $raw = trim($value->getRaw());
114
 
115
                    if(!$isJson) {
116
                        $isJson = strpos($raw, 'json');
117
                    }
118
 
119
                }
120
            }
121
 
122
            if($isJson) {
123
 
124
                $search = $this->params()->fromQuery('search', []);
16766 efrain 125
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
1 www 126
 
127
                $page               = intval($this->params()->fromQuery('start', 1), 10);
128
                $records_x_page     = intval($this->params()->fromQuery('length', 10), 10);
129
                $order =  $this->params()->fromQuery('order', []);
130
                $order_field        = empty($order[0]['column']) ? 99 :  intval($order[0]['column'], 10);
16766 efrain 131
                $order_direction    = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
1 www 132
 
133
                $fields =  ['first_name', 'last_name', 'email'];
134
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'first_name';
135
 
136
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
137
                    $order_direction = 'ASC';
138
                }
139
 
17002 efrain 140
                //$microlearningCapsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
1 www 141
                $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
17002 efrain 142
                $microlearningCapsulUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
143
                $microlearningUserProgressMapper = MicrolearningUserProgressMapper::getInstance($this->adapter);
1 www 144
 
145
                $userMapper = UserMapper::getInstance($this->adapter);
146
                $paginator = $userMapper->fetchAllDataTableStudensByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
147
 
148
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
149
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'microlearning/students/edit');
150
                $allowTimeline = $acl->isAllowed($currentUser->usertype_id, 'microlearning/students/timeline');
151
                $allowUnblock = $acl->isAllowed($currentUser->usertype_id, 'microlearning/students/unblock');
152
                $allowChangePassword = $acl->isAllowed($currentUser->usertype_id, 'microlearning/students/change-password');
153
 
154
 
155
 
156
                $items = [ ];
157
                $records = $paginator->getCurrentItems();
158
 
159
 
160
 
161
                foreach($records as $record)
162
                {
163
 
164
 
17002 efrain 165
                    $userCapsules = $microlearningCapsulUserMapper->fetchAllActiveByUserId($record['id']);
1 www 166
 
167
                    $total_asigned = count($userCapsules);
168
                    $total_without_starting = 0;
169
                    $total_started = 0;
170
                    $total_completed = 0;
171
 
172
                    foreach($userCapsules as $userCapsule)
173
                    {
17002 efrain 174
                        $userProgress = $microlearningUserProgressMapper->fetchOneByUseridAndCapsuleId($userCapsule->user_id, $userCapsule->capsule_id);
1 www 175
                        if($userProgress) {
176
 
177
                            if($userProgress->completed) {
178
                                $total_completed++;
179
                            }  else {
180
                                $total_started++;
181
                            }
182
 
183
                        } else {
184
                            $total_without_starting++;
185
                        }
186
 
187
                    }
188
 
189
 
190
                    $actions = [];
191
                    if($allowEdit) {
192
                        $actions['link_edit'] = $this->url()->fromRoute('microlearning/students/edit', ['id' => $record['uuid'] ]  );
193
                    }
194
                    if($allowTimeline) {
195
                        $actions['link_timeline'] = $this->url()->fromRoute('microlearning/students/timeline', ['id' => $record['uuid'] ]  );
196
                    }
197
 
198
                    $totalOtherCompanies = $companyUserMapper->fetchCountOtherCompaniesByCompanyIdAndUserId($currentCompany->id, $record['id']);
199
                    if(!$totalOtherCompanies) {
200
 
201
                        $actions['link_change_password'] = $allowChangePassword ? $this->url()->fromRoute('microlearning/students/change-password', ['id' => $record['uuid'] ]) : '';
202
                        if($record['blocked'] == User::BLOCKED_YES ) {
203
                            $actions['link_unblock'] = $allowUnblock ? $this->url()->fromRoute('microlearning/students/unblock', ['id' => $record['uuid'] ]) : '';
204
                        }
205
                    }
206
 
207
                    $details = [
208
                        'total_asigned' => $total_asigned,
209
                        'total_without_starting' => $total_without_starting,
210
                        'total_started' => $total_started,
211
                        'total_completed' => $total_completed,
212
                    ];
213
 
214
 
215
                    $item = [
216
                        'uuid' => $record['uuid'],
217
                        'first_name' => $record['first_name'],
218
                        'last_name' => $record['last_name'],
219
                        'email' => $record['email'],
220
                        'details' => $details,
221
                        'actions' => $actions
222
                    ];
223
 
224
 
225
                    array_push($items, $item);
226
 
227
 
228
                }
229
 
230
                $data['items'] = $items;
231
                $data['total'] = $paginator->getTotalItemCount();
232
 
233
 
234
                return new JsonModel([
235
                    'success' => true,
236
                    'data' => $data
237
                ]);
238
            } else {
15386 efrain 239
                $formPushNotification = new PushMicrolearningNotificationForm($this->adapter, $currentCompany->id);
1 www 240
 
241
                $formExtendUser = new ExtendUserMicrolearningForm($this->adapter, $currentCompany->id);
242
                $formChangePassword = new ChangePasswordForm();
243
 
244
                $this->layout()->setTemplate('layout/layout-backend');
245
                $viewModel = new ViewModel();
246
                $viewModel->setTemplate('leaders-linked/microlearning-students/index.phtml');
247
                $viewModel->setVariables([
248
                    'formPushNotification' => $formPushNotification,
249
                    'formExtendUser' => $formExtendUser,
250
                    'formChangePassword' => $formChangePassword,
251
                ]);
252
 
253
                return $viewModel ;
254
            }
255
 
256
        } else {
257
            return new JsonModel([
258
                'success' => false,
259
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
260
            ]);;
261
        }
262
    }
263
 
264
 
265
 
266
    public function editAction()
267
    {
268
 
269
        $currentUserPlugin = $this->plugin('currentUserPlugin');
270
        $currentUser    = $currentUserPlugin->getUser();
271
        $currentCompany = $currentUserPlugin->getCompany();
272
 
273
        $request    = $this->getRequest();
274
        $user_uuid  = $this->params()->fromRoute('id');
275
 
276
 
277
        $userMapper = UserMapper::getInstance($this->adapter);
278
        $user = $userMapper->fetchOneByUuid($user_uuid);
279
 
280
        if(!$user) {
281
            return new JsonModel([
282
                'success'   => false,
283
                'data'   => 'ERROR_USER_NOT_FOUND'
284
            ]);
285
        }
286
 
17002 efrain 287
        $microlearningCapsuleUserMapper = MicrolearningCapsuleUserMapper::getInstance($this->adapter);
288
        $total = $microlearningCapsuleUserMapper->fetchCountByCompanyIdAndUserId($currentCompany->id, $user->id);
1 www 289
 
290
        if(!$total) {
291
            return new JsonModel([
292
                'success'   => false,
293
                'data'   => 'ERROR_UNAUTHORIZED'
294
            ]);
295
        }
296
 
297
 
298
        if($request->isGet()) {
299
 
300
 
301
            $data = [
302
                'first_name' => $user->first_name,
303
                'last_name' => $user->last_name,
304
                'email' => $user->email,
305
                'company_id' => '',
306
                'function_id' => '',
307
                'group_id' => '',
308
                'institution_id' => '',
309
                'partner_id' => '',
310
                'program_id' => '',
311
                'sector_id' => '',
312
                'student_type_id' => '',
313
 
314
            ];
315
 
17002 efrain 316
            $microlearningExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
317
            $microlearningExtendUser = $microlearningExtendUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
1 www 318
 
17002 efrain 319
            if($microlearningExtendUser) {
320
                if($microlearningExtendUser->extend_company_id) {
321
                    $microlearningExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
322
                    $microlearningExtendUserCompany = $microlearningExtendUserCompanyMapper->fetchOne($microlearningExtendUser->extend_company_id);
323
                    if($microlearningExtendUserCompany) {
324
                        $data['company_id'] = $microlearningExtendUserCompany->uuid;
1 www 325
                    }
326
                }
327
 
328
            }
329
 
17002 efrain 330
            if($microlearningExtendUser) {
331
                if($microlearningExtendUser->extend_function_id) {
332
                    $microlearningExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
333
                    $microlearningExtendUserFunction = $microlearningExtendUserFunctionMapper->fetchOne($microlearningExtendUser->extend_function_id);
334
                    if($microlearningExtendUserFunction) {
335
                        $data['function_id'] = $microlearningExtendUserFunction->uuid;
1 www 336
                    }
337
                }
338
            }
339
 
17002 efrain 340
            if($microlearningExtendUser) {
341
                if($microlearningExtendUser->extend_group_id) {
342
                    $microlearningExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
343
                    $microlearningExtendUserGroup = $microlearningExtendUserGroupMapper->fetchOne($microlearningExtendUser->extend_group_id);
344
                    if($microlearningExtendUserGroup) {
345
                        $data['group_id'] = $microlearningExtendUserGroup->uuid;
1 www 346
                    }
347
                }
348
            }
349
 
17002 efrain 350
            if($microlearningExtendUser) {
351
                if($microlearningExtendUser->extend_institution_id) {
352
                    $microlearningExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
353
                    $microlearningExtendUserInstitution = $microlearningExtendUserInstitutionMapper->fetchOne($microlearningExtendUser->extend_institution_id);
354
                    if($microlearningExtendUserInstitution) {
355
                        $data['institution_id'] = $microlearningExtendUserInstitution->uuid;
1 www 356
                    }
357
                }
358
            }
359
 
17002 efrain 360
            if($microlearningExtendUser) {
361
                if($microlearningExtendUser->extend_partner_id) {
362
                    $microlearningExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
363
                    $microlearningExtendUserPartner = $microlearningExtendUserPartnerMapper->fetchOne($microlearningExtendUser->extend_partner_id);
364
                    if($microlearningExtendUserPartner) {
365
                        $data['partner_id'] = $microlearningExtendUserPartner->uuid;
1 www 366
                    }
367
                }
368
            }
369
 
17002 efrain 370
            if($microlearningExtendUser) {
371
                if($microlearningExtendUser->extend_program_id) {
372
                    $microlearningExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
373
                    $microlearningExtendUserProgram = $microlearningExtendUserProgramMapper->fetchOne($microlearningExtendUser->extend_program_id);
374
                    if($microlearningExtendUserProgram) {
375
                        $data['program_id'] = $microlearningExtendUserProgram->uuid;
1 www 376
                    }
377
                }
378
            }
379
 
17002 efrain 380
            if($microlearningExtendUser) {
381
                if($microlearningExtendUser->extend_sector_id) {
382
                    $microlearningExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
383
                    $microlearningExtendUserSector = $microlearningExtendUserSectorMapper->fetchOne($microlearningExtendUser->extend_sector_id);
384
                    if($microlearningExtendUserSector) {
385
                        $data['sector_id'] = $microlearningExtendUserSector->uuid;
1 www 386
                    }
387
                }
388
            }
389
 
17002 efrain 390
            if($microlearningExtendUser) {
391
                if($microlearningExtendUser->extend_student_type_id) {
392
                    $microlearningExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
393
                    $microlearningExtendUserStudentType = $microlearningExtendUserStudentTypeMapper->fetchOne($microlearningExtendUser->extend_student_type_id);
394
                    if($microlearningExtendUserStudentType) {
395
                        $data['student_type_id'] = $microlearningExtendUserStudentType->uuid;
1 www 396
                    }
397
                }
398
            }
399
 
400
 
401
            return new JsonModel([
402
                'success'   => true,
403
                'data'   => $data,
404
            ]);
405
 
406
        }
407
 
408
 
409
        if($request->isPost()) {
410
            $form = new  ExtendUserMicrolearningForm($this->adapter, $currentCompany->id);
411
            $dataPost = $request->getPost()->toArray();
412
 
413
            $form->setData($dataPost);
414
 
415
            if($form->isValid()) {
416
                $dataPost = (array) $form->getData();
417
 
17002 efrain 418
                $microlearningExtendUserMapper = MicrolearningExtendUserMapper::getInstance($this->adapter);
419
                $microlearningExtendUser = $microlearningExtendUserMapper->fetchOneByCompanyIdAndUserId($currentCompany->id, $user->id);
420
                if(!$microlearningExtendUser) {
421
                    $microlearningExtendUser = new MicrolearningExtendUser();
422
                    $microlearningExtendUser->company_id = $currentCompany->id;
423
                    $microlearningExtendUser->user_id = $user->id;
1 www 424
                }
425
 
426
 
16766 efrain 427
                $extend_company_uuid        = Functions::sanitizeFilterString($this->params()->fromPost('company_id'));
428
                $extend_function_uuid       = Functions::sanitizeFilterString($this->params()->fromPost('function_id'));
429
                $extend_group_uuid          = Functions::sanitizeFilterString($this->params()->fromPost('group_id'));
430
                $extend_institution_uuid    = Functions::sanitizeFilterString($this->params()->fromPost('institution_id'));
431
                $extend_program_uuid        = Functions::sanitizeFilterString($this->params()->fromPost('program_id'));
432
                $extend_partner_uuid        = Functions::sanitizeFilterString($this->params()->fromPost('partner_id'));
433
                $extend_sector_uuid         = Functions::sanitizeFilterString($this->params()->fromPost('sector_id'));
434
                $extend_student_type_uuid   = Functions::sanitizeFilterString($this->params()->fromPost('student_type_id'));
1 www 435
 
436
 
437
                if($extend_company_uuid) {
17002 efrain 438
                    $microlearningExtendUserCompanyMapper = MicrolearningExtendUserCompanyMapper::getInstance($this->adapter);
439
                    $microlearningExtendUserCompany = $microlearningExtendUserCompanyMapper->fetchOneByUuid($extend_company_uuid);
440
                    if($microlearningExtendUserCompany && $microlearningExtendUserCompany->company_id = $currentCompany->id) {
441
                        $microlearningExtendUser->extend_company_id = $microlearningExtendUserCompany->id;
1 www 442
                    } else {
17002 efrain 443
                        $microlearningExtendUser->extend_company_id = null;
1 www 444
                    }
445
 
446
                } else {
17002 efrain 447
                    $microlearningExtendUser->extend_company_id = null;
1 www 448
                }
449
 
450
 
451
                if($extend_function_uuid) {
17002 efrain 452
                    $microlearningExtendUserFunctionMapper = MicrolearningExtendUserFunctionMapper::getInstance($this->adapter);
453
                    $microlearningExtendUserFunction = $microlearningExtendUserFunctionMapper->fetchOneByUuid($extend_function_uuid);
454
                    if($microlearningExtendUserFunction && $microlearningExtendUserFunction->company_id = $currentCompany->id) {
455
                        $microlearningExtendUser->extend_function_id = $microlearningExtendUserFunction->id;
1 www 456
                    } else {
17002 efrain 457
                        $microlearningExtendUser->extend_function_id = null;
1 www 458
                    }
459
 
460
                } else {
17002 efrain 461
                    $microlearningExtendUser->extend_function_id = null;
1 www 462
                }
463
 
464
                if($extend_group_uuid) {
17002 efrain 465
                    $microlearningExtendUserGroupMapper = MicrolearningExtendUserGroupMapper::getInstance($this->adapter);
466
                    $microlearningExtendUserGroup = $microlearningExtendUserGroupMapper->fetchOneByUuid($extend_group_uuid);
467
                    if($microlearningExtendUserGroup && $microlearningExtendUserGroup->company_id = $currentCompany->id) {
468
                        $microlearningExtendUser->extend_group_id = $microlearningExtendUserGroup->id;
1 www 469
                    } else {
17002 efrain 470
                        $microlearningExtendUser->extend_group_id = null;
1 www 471
                    }
472
 
473
                } else {
17002 efrain 474
                    $microlearningExtendUser->extend_group_id = null;
1 www 475
                }
476
 
477
                if($extend_institution_uuid) {
17002 efrain 478
                    $microlearningExtendUserInstitutionMapper = MicrolearningExtendUserInstitutionMapper::getInstance($this->adapter);
479
                    $microlearningExtendUserInstitution = $microlearningExtendUserInstitutionMapper->fetchOneByUuid($extend_institution_uuid);
480
                    if($microlearningExtendUserInstitution && $microlearningExtendUserInstitution->company_id = $currentCompany->id) {
481
                        $microlearningExtendUser->extend_institution_id = $microlearningExtendUserInstitution->id;
1 www 482
                    } else {
17002 efrain 483
                        $microlearningExtendUser->extend_institution_id = null;
1 www 484
                    }
485
 
486
                } else {
17002 efrain 487
                    $microlearningExtendUser->extend_institution_id = null;
1 www 488
                }
489
 
490
                if($extend_program_uuid) {
17002 efrain 491
                    $microlearningExtendUserProgramMapper = MicrolearningExtendUserProgramMapper::getInstance($this->adapter);
492
                    $microlearningExtendUserProgram = $microlearningExtendUserProgramMapper->fetchOneByUuid($extend_program_uuid);
493
                    if($microlearningExtendUserProgram && $microlearningExtendUserProgram->company_id = $currentCompany->id) {
494
                        $microlearningExtendUser->extend_program_id = $microlearningExtendUserProgram->id;
1 www 495
                    } else {
17002 efrain 496
                        $microlearningExtendUser->extend_program_id = null;
1 www 497
                    }
498
 
499
                } else {
17002 efrain 500
                    $microlearningExtendUser->extend_program_id = null;
1 www 501
                }
502
 
503
                if($extend_partner_uuid) {
17002 efrain 504
                    $microlearningExtendUserPartnerMapper = MicrolearningExtendUserPartnerMapper::getInstance($this->adapter);
505
                    $microlearningExtendUserPartner = $microlearningExtendUserPartnerMapper->fetchOneByUuid($extend_partner_uuid);
506
                    if($microlearningExtendUserPartner && $microlearningExtendUserPartner->company_id = $currentCompany->id) {
507
                        $microlearningExtendUser->extend_partner_id = $microlearningExtendUserPartner->id;
1 www 508
                    } else {
17002 efrain 509
                        $microlearningExtendUser->extend_partner_id = null;
1 www 510
                    }
511
 
512
                } else {
17002 efrain 513
                    $microlearningExtendUser->extend_partner_id = null;
1 www 514
                }
515
 
516
                if($extend_sector_uuid) {
17002 efrain 517
                    $microlearningExtendUserSectorMapper = MicrolearningExtendUserSectorMapper::getInstance($this->adapter);
518
                    $microlearningExtendUserSector = $microlearningExtendUserSectorMapper->fetchOneByUuid($extend_sector_uuid);
519
                    if($microlearningExtendUserSector && $microlearningExtendUserSector->company_id = $currentCompany->id) {
520
                        $microlearningExtendUser->extend_sector_id = $microlearningExtendUserSector->id;
1 www 521
                    } else {
17002 efrain 522
                        $microlearningExtendUser->extend_sector_id = null;
1 www 523
                    }
524
 
525
                } else {
17002 efrain 526
                    $microlearningExtendUser->extend_sector_id = null;
1 www 527
                }
528
 
529
                if($extend_student_type_uuid) {
17002 efrain 530
                    $microlearningExtendUserStudentTypeMapper = MicrolearningExtendUserStudentTypeMapper::getInstance($this->adapter);
531
                    $microlearningExtendUserStudentType = $microlearningExtendUserStudentTypeMapper->fetchOneByUuid($extend_student_type_uuid);
532
                    if($microlearningExtendUserStudentType && $microlearningExtendUserStudentType->company_id = $currentCompany->id) {
533
                        $microlearningExtendUser->extend_student_type_id = $microlearningExtendUserStudentType->id;
1 www 534
                    } else {
17002 efrain 535
                        $microlearningExtendUser->extend_student_type_id = null;
1 www 536
                    }
537
 
538
                } else {
17002 efrain 539
                    $microlearningExtendUser->extend_student_type_id = null;
1 www 540
                }
541
 
542
 
543
 
17002 efrain 544
                if($microlearningExtendUser->id) {
545
                    $result = $microlearningExtendUserMapper->update($microlearningExtendUser);
1 www 546
                    if($result) {
547
                        $this->logger->info('Se actualizo la información extendida de micro aprendizaje  del usuario ' . $user->email, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
548
 
549
                        $data = [
550
                            'success'   => true,
551
                            'data'   => 'LABEL_RECORD_ADDED'
552
                        ];
553
                    } else {
554
                        $data = [
555
                            'success'   => false,
17002 efrain 556
                            'data'      => $microlearningExtendUserMapper->getError()
1 www 557
                        ];
558
 
559
                    }
560
                } else {
17002 efrain 561
                    $result = $microlearningExtendUserMapper->insert($microlearningExtendUser);
1 www 562
                    if($result) {
563
                        $this->logger->info('Se agrego la información extendida de micro aprendizaje  del usuario ' . $user->email, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
564
 
565
                        $data = [
566
                            'success'   => true,
567
                            'data'   => 'LABEL_RECORD_ADDED'
568
                        ];
569
                    } else {
570
                        $data = [
571
                            'success'   => false,
17002 efrain 572
                            'data'      => $microlearningExtendUserMapper->getError()
1 www 573
                        ];
574
 
575
                    }
576
                }
577
 
578
 
579
 
580
 
581
                return new JsonModel($data);
582
 
583
            } else {
584
                $messages = [];
585
                $form_messages = (array) $form->getMessages();
586
                foreach($form_messages  as $fieldname => $field_messages)
587
                {
588
 
589
                    $messages[$fieldname] = array_values($field_messages);
590
                }
591
 
592
                return new JsonModel([
593
                    'success'   => false,
594
                    'data'   => $messages
595
                ]);
596
            }
597
 
598
        } else {
599
            $data = [
600
                'success' => false,
601
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
602
            ];
603
 
604
            return new JsonModel($data);
605
        }
606
 
607
        return new JsonModel([
608
            'success' => false,
609
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
610
        ]);
611
    }
612
 
613
    public function timelineAction()
614
    {
615
 
616
        $currentUserPlugin = $this->plugin('currentUserPlugin');
617
        $currentUser    = $currentUserPlugin->getUser();
618
        $currentCompany = $currentUserPlugin->getCompany();
619
 
620
        $request    = $this->getRequest();
621
        $user_uuid  = $this->params()->fromRoute('id');
622
 
623
 
624
        $userMapper = UserMapper::getInstance($this->adapter);
625
        $user = $userMapper->fetchOneByUuid($user_uuid);
626
 
627
        if(!$user) {
628
            return new JsonModel([
629
                'success'   => false,
630
                'data'   => 'ERROR_USER_NOT_FOUND'
631
            ]);
632
        }
633
 
634
        if($request->isGet()) {
635
            $page = intval(filter_var($this->params()->fromQuery('page'), FILTER_SANITIZE_NUMBER_INT) , 10);
636
            if(!$page) {
637
                $page = 1;
638
            }
639
            $records_x_page = 10;
640
 
17002 efrain 641
            $microlearningTopicMapper = MicrolearningTopicMapper::getInstance($this->adapter);
642
            $microlearningCapsuleMapper = MicrolearningCapsuleMapper::getInstance($this->adapter);
643
            $microlearningSlideMapper = MicrolearningSlideMapper::getInstance($this->adapter);
1 www 644
 
645
            $topics = [];
646
            $capsules = [];
647
            $slides = [] ;
648
 
649
 
17002 efrain 650
            $microlearningUserLogMapper = MicrolearningUserLogMapper::getInstance($this->adapter);
1 www 651
 
17002 efrain 652
            $paginator = $microlearningUserLogMapper->getAllMessagesPaginatorByUserIdAndCompanyId($user->id, $currentCompany->id, $page, $records_x_page);
1 www 653
 
654
            $items = [] ;
655
            foreach($paginator->getCurrentItems() as $record)
656
            {
657
                $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $record->added_on);
658
 
659
                $item = [
660
                    'activity' => $record->activity,
661
                    'added_on' => $dt->format('d/m/Y h:i a')
662
                ];
663
 
664
                if($record->topic_id) {
665
 
666
                    if(isset($topics[$record->topic_id])) {
667
                        $topic = $topics[ $record->topic_id ];
668
                    }  else {
17002 efrain 669
                        $topic = $microlearningTopicMapper->fetchOne( $record->topic_id );
1 www 670
                    }
671
 
672
                    if($topic) {
673
                        $item['topic'] = $topic->name;
674
                    }
675
 
676
                    if(isset($capsules[$record->capsule_id])) {
677
                        $capsule = $capsules[ $record->capsule_id ];
678
                    }  else {
17002 efrain 679
                        $capsule = $microlearningCapsuleMapper->fetchOne( $record->capsule_id );
1 www 680
                    }
681
 
682
                    if($capsule) {
683
                        $item['capsule'] = $capsule->name;
684
                    }
685
 
686
                    if(isset($slides[$record->slide_id])) {
687
                        $slide = $slides[ $record->slide_id ];
688
                    }  else {
17002 efrain 689
                        $slide = $microlearningSlideMapper->fetchOne( $record->slide_id );
1 www 690
                    }
691
 
692
                    if($slide) {
693
                        $item['slide'] = $slide->name;
694
                    }
695
                }
696
 
697
 
698
                array_push($items, $item);
699
            }
700
 
701
            $response = [
702
                'success' => true,
703
                'data' => [
704
                    'total' => [
705
                        'count' => $paginator->getTotalItemCount(),
706
                        'pages' => $paginator->getPages()->pageCount,
707
                    ],
708
                    'current' => [
709
                        'items'    => $items,
710
                        'page'     => $paginator->getCurrentPageNumber(),
711
                        'count'    => $paginator->getCurrentItemCount(),
712
                    ]
713
                ]
714
            ];
715
 
716
            return new JsonModel($response);
717
 
718
        }
719
 
720
 
721
 
722
 
723
        return new JsonModel([
724
            'success' => false,
725
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
726
        ]);
727
    }
728
 
729
 
730
    public function unblockAction()
731
    {
732
        $currentUserPlugin = $this->plugin('currentUserPlugin');
733
        $currentUser = $currentUserPlugin->getUser();
734
        $request = $this->getRequest();
735
 
736
 
737
        if($request->isPost()) {
738
 
739
            $uuid = $this->params()->fromRoute('id');
740
            if(!$uuid) {
741
                return new JsonModel([
742
                    'success'   => false,
743
                    'data'      => 'ERROR_INVALID_PARAMETER'
744
                ]);
745
            }
746
 
747
            $userMapper = UserMapper::getInstance($this->adapter);
748
            $user = $userMapper->fetchOneByUuid($uuid);
749
 
750
            if(!$user) {
751
                return new JsonModel([
752
                    'success'   => false,
753
                    'data'      => 'ERROR_USER_NOT_FOUND'
754
                ]);
755
            }
756
 
757
            if($user->blocked == User::BLOCKED_NO) {
758
                return new JsonModel([
759
                    'success'   => false,
760
                    'data'      => 'ERROR_USER_IS_NOT_BLOCKED'
761
                ]);
762
            }
763
 
764
 
765
 
766
            $result = $userMapper->unblock($user);
767
            if($result) {
768
                $this->logger->info('El usuario : ' . $user->email . ' ha sido desbloqueado ', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
769
 
770
                return new JsonModel([
771
                    'success'   => true,
772
                    'data'      => 'LABEL_USER_HAS_BEEN_UNBLOCKED',
773
                ]);
774
            }  else {
775
 
776
                return new JsonModel([
777
                    'success'   => false,
778
                    'data'      => $userMapper->getError()
779
                ]);
780
            }
781
 
782
 
783
        }
784
 
785
 
786
 
787
        return new JsonModel([
788
            'success' => false,
789
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
790
        ]);
791
    }
792
 
793
    public function changePasswordAction()
794
    {
795
        $currentUserPlugin = $this->plugin('currentUserPlugin');
796
        $currentUser = $currentUserPlugin->getUser();
797
 
798
        $request = $this->getRequest();
799
 
800
        if($request->isGet()) {
801
            $uuid = $this->params()->fromRoute('id');
802
            if(!$uuid) {
803
                return new JsonModel([
804
                    'success'   => false,
805
                    'data'      => 'ERROR_INVALID_PARAMETER'
806
                ]);
807
            }
808
 
809
            $userMapper = UserMapper::getInstance($this->adapter);
810
            $user = $userMapper->fetchOneByUuid($uuid);
811
 
812
 
813
 
814
            if($user) {
815
                return new JsonModel([
816
                    'success'   => true,
817
                    'data'      => [
818
                        'first_name' => $user->first_name,
819
                        'last_name' => $user->last_name,
820
                        'email' => $user->email,
821
                    ]
822
                ]);
823
            } else {
824
                return new JsonModel([
825
                    'success'   => false,
826
                    'data'      => 'ERROR_USER_NOT_FOUND'
827
                ]);
828
            }
829
 
830
        }
831
 
832
        if($request->isPost()) {
833
 
834
            $uuid = $this->params()->fromRoute('id');
835
            if(!$uuid) {
836
                return new JsonModel([
837
                    'success'   => false,
838
                    'data'      => 'ERROR_INVALID_PARAMETER'
839
                ]);
840
            }
841
 
842
            $userMapper = UserMapper::getInstance($this->adapter);
843
            $user = $userMapper->fetchOneByUuid($uuid);
844
 
845
            if(!$user) {
846
                return new JsonModel([
847
                    'success'   => false,
848
                    'data'      => 'ERROR_USER_NOT_FOUND'
849
                ]);
850
            }
851
 
852
 
853
            $dataPost = $request->getPost()->toArray();
854
            $form = new ChangePasswordForm();
855
            $form->setData($dataPost);
856
 
857
            if($form->isValid()) {
858
 
859
 
860
 
861
                $data = (array) $form->getData();
862
                $password = $data['password'];
863
 
864
 
865
 
866
                $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
867
                $userPasswords = $userPasswordMapper->fetchAllByUserId($user->id);
868
 
869
                $oldPassword = false;
870
                foreach($userPasswords as $userPassword)
871
                {
872
                    if(password_verify($password, $userPassword->password) || (md5($password) == $userPassword->password))
873
                    {
874
                        $oldPassword = true;
875
                        break;
876
                    }
877
                }
878
 
879
                if($oldPassword) {
880
                    $this->logger->err('Cambio de contraseña del usuario - error contraseña ya utilizada anteriormente', ['user_id' =>  $currentUser->id, 'ip' => Functions::getUserIP()]);
881
 
882
                    return new JsonModel([
883
                        'success'   => false,
884
                        'data'      => 'ERROR_PASSWORD_HAS_ALREADY_BEEN_USED'
885
 
886
                    ]);
887
                } else {
888
                    $password_hash = password_hash($password, PASSWORD_DEFAULT);
889
 
890
 
891
                    $result = $userMapper->updatePassword($user, $password_hash);
892
                    if($result) {
893
                        $this->logger->info('Cambio de contraseña del usuario realizado', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
894
 
895
 
896
                        return new JsonModel([
897
                            'success'   => true,
898
                            'data'      => 'LABEL_YOUR_PASSWORD_HAS_BEEN_UPDATED'
899
 
900
                        ]);
901
                    } else {
902
                        $this->logger->err('Cambio de contraseña del usuario - error desconocido', ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
903
 
904
                        return new JsonModel([
905
                            'success'   => true,
906
                            'data'      => 'ERROR_THERE_WAS_AN_ERROR'
907
 
908
                        ]);
909
                    }
910
                }
911
 
912
            } else {
913
                $messages = [];
914
 
915
                $form_messages = (array) $form->getMessages();
916
                foreach($form_messages  as $fieldname => $field_messages)
917
                {
918
                    $messages[$fieldname] = array_values($field_messages);
919
                }
920
 
921
                return new JsonModel([
922
                    'success'   => false,
923
                    'data'   => $messages
924
                ]);
925
            }
926
 
927
        }
928
 
929
 
930
 
931
        return new JsonModel([
932
            'success' => false,
933
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
934
        ]);
935
    }
936
 
937
 
938
 
939
}