Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16817 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16817 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\Log\LoggerInterface;
11
use Laminas\View\Model\ViewModel;
12
use Laminas\View\Model\JsonModel;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\IndustryMapper;
15
use LeadersLinked\Mapper\JobDescriptionMapper;
16
use LeadersLinked\Mapper\SkillMapper;
17
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
18
use LeadersLinked\Mapper\SurveyCampaignMapper;
19
use LeadersLinked\Mapper\SurveyFormMapper;
20
use LeadersLinked\Model\SurveyCampaign;
21
use LeadersLinked\Form\Survey\SurveyCampaignForm;
22
use LeadersLinked\Mapper\LocationMapper;
23
use LeadersLinked\Model\Location;
24
use LeadersLinked\Mapper\SurveyCampaignJobDescriptionMapper;
25
use LeadersLinked\Model\SurveyCampaignJobDescription;
26
use LeadersLinked\Mapper\SurveyCampaignSkillMapper;
27
use LeadersLinked\Model\SurveyCampaignSkill;
28
use LeadersLinked\Mapper\SurveyCampaignIndustryMapper;
29
use LeadersLinked\Model\SurveyCampaignIndustry;
30
use LeadersLinked\Mapper\SurveyCampaignLanguageMapper;
31
use LeadersLinked\Model\SurveyCampaignLanguage;
32
use LeadersLinked\Mapper\CompanyUserMapper;
33
use LeadersLinked\Mapper\CalendarEventMapper;
34
use LeadersLinked\Model\CalendarEvent;
35
use LeadersLinked\Mapper\SurveyTestMapper;
36
use LeadersLinked\Model\SurveyTest;
37
use Laminas\Http\Response;
38
use LeadersLinked\Library\SurveyReport;
39
use PhpOffice\PhpSpreadsheet\Spreadsheet;
40
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
41
 
42
class OrganizationalClimateCampaignController extends AbstractActionController {
43
 
44
    /**
45
     *
46
     * @var \Laminas\Db\Adapter\AdapterInterface
47
     */
48
    private $adapter;
49
 
50
    /**
51
     *
52
     * @var \LeadersLinked\Cache\CacheInterface
53
     */
54
    private $cache;
55
 
56
 
57
    /**
58
     *
59
     * @var \Laminas\Log\LoggerInterface
60
     */
61
    private $logger;
62
 
63
    /**
64
     *
65
     * @var array
66
     */
67
    private $config;
68
 
69
 
70
    /**
71
     *
72
     * @var \Laminas\Mvc\I18n\Translator
73
     */
74
    private $translator;
75
 
76
 
77
    /**
78
     *
79
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
80
     * @param \LeadersLinked\Cache\CacheInterface $cache
81
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
82
     * @param array $config
83
     * @param \Laminas\Mvc\I18n\Translator $translator
84
     */
85
    public function __construct($adapter, $cache, $logger, $config, $translator)
86
    {
87
        $this->adapter      = $adapter;
88
        $this->cache        = $cache;
89
        $this->logger       = $logger;
90
        $this->config       = $config;
91
        $this->translator   = $translator;
92
    }
93
 
94
    public function indexAction() {
95
        $request = $this->getRequest();
96
        $currentUserPlugin = $this->plugin('currentUserPlugin');
97
        $currentCompany = $currentUserPlugin->getCompany();
98
        $currentUser = $currentUserPlugin->getUser();
99
 
100
 
101
        $request = $this->getRequest();
102
        if ($request->isGet()) {
103
            $sandbox = $this->config['leaderslinked.runmode.sandbox'];
104
            if($sandbox) {
105
                $google_map_key  = $this->config['leaderslinked.google_map.sandbox_api_key'];
106
            } else {
107
                $google_map_key  = $this->config['leaderslinked.google_map.production_api_key'];
108
            }
109
 
110
            $headers = $request->getHeaders();
111
 
112
            $isJson = false;
113
            if ($headers->has('Accept')) {
114
                $accept = $headers->get('Accept');
115
 
116
                $prioritized = $accept->getPrioritized();
117
 
118
                foreach ($prioritized as $key => $value) {
119
                    $raw = trim($value->getRaw());
120
 
121
                    if (!$isJson) {
122
                        $isJson = strpos($raw, 'json');
123
                    }
124
                }
125
            }
126
 
127
            if ($isJson) {
128
                $search = $this->params()->fromQuery('search', []);
129
                $search = empty($search['value']) ? '' :  Functions::sanitizeFilterString($search['value']);
130
 
131
                $start = intval($this->params()->fromQuery('start', 0), 10);
132
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
133
                $page =  intval($start / $records_x_page);
134
                $page++;
135
 
136
                $order = $this->params()->fromQuery('order', []);
137
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
138
                $order_direction = empty($order[0]['dir']) ? 'ASC' : Functions::sanitizeFilterString(filter_var($order[0]['dir']));
139
 
140
                $fields = ['name'];
141
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
142
 
143
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
144
                    $order_direction = 'ASC';
145
                }
146
 
147
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
148
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/delete');
149
                $allowOverview = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/overview');
150
                $allowPdf = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/pdf');
151
                $allowExcel = $acl->isAllowed($currentUser->usertype_id, 'organizational-climate/campaign/excel');
152
 
153
 
154
                $surveyForms = [];
155
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
156
 
157
                $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
158
                $paginator = $surveyCampaignMapper->fetchAllDataTableOrganizationClimateByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
159
 
160
                $items = [];
161
                $records = $paginator->getCurrentItems();
162
 
163
                foreach ($records as $record)
164
                {
165
 
166
                    if(isset($surveyForms[$record->form_id])) {
167
                        $surveyForm = $surveyForms[$record->form_id];
168
                    } else {
169
                        $surveyForm = $surveyFormMapper->fetchOne($record->form_id);
170
 
171
                        $surveyForms[$record->form_id] = $surveyForm;
172
                    }
173
 
174
 
175
                    $dtStartDate = \DateTime::createFromFormat('Y-m-d', $record->start_date);
176
                    $dtEndDate = \DateTime::createFromFormat('Y-m-d', $record->end_date);
177
 
178
                    $item = [
179
                        'id' => $record->id,
180
                        'name' => $record->name,
181
                        'form' =>  $surveyForm->name,
182
                        'details' => [
183
                            'start_date' => $dtStartDate ? $dtStartDate->format('d/m/Y') : '',
184
                            'end_date' => $dtEndDate ? $dtEndDate->format('d/m/Y') : '',
185
                            'total_test' => $record->total_test,
186
                            'completed_test' => $record->completed_test,
187
                        ],
188
 
189
                        'status' => $record->status,
190
                        'actions' => [
191
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('organizational-climate/campaign/delete', ['id' => $record->uuid]) : '',
192
                            'link_overview' => $allowOverview && $record->completed_test ? $this->url()->fromRoute('organizational-climate/campaign/overview', ['id' => $record->uuid]) : '',
193
                            'link_pdf' => $allowPdf && $record->completed_test ? $this->url()->fromRoute('organizational-climate/campaign/pdf', ['id' => $record->uuid]) : '',
194
                            'link_excel' => $allowExcel && $record->completed_test  ? $this->url()->fromRoute('organizational-climate/campaign/excel', ['id' => $record->uuid]) : '',
195
 
196
                        ]
197
                    ];
198
 
199
                    array_push($items, $item);
200
                }
201
 
202
                return new JsonModel([
203
                    'success' => true,
204
                    'data' => [
205
                        'items' => $items,
206
                        'total' => $paginator->getTotalItemCount(),
207
                    ]
208
                ]);
209
            } else {
210
 
211
                $form = new SurveyCampaignForm($this->adapter, $currentCompany->id, SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE);
212
 
213
                $this->layout()->setTemplate('layout/layout-backend');
214
                $viewModel = new ViewModel();
215
                $viewModel->setTemplate('leaders-linked/organizational-climate-campaign/index.phtml');
216
                $viewModel->setVariable('form', $form);
217
                $viewModel->setVariable('google_map_key', $google_map_key);
218
                return $viewModel;
219
            }
220
        } else {
221
            return new JsonModel([
222
                'success' => false,
223
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
224
            ]);
225
        }
226
    }
227
 
228
    public function addAction()
229
    {
230
        $request = $this->getRequest();
231
        $currentUserPlugin = $this->plugin('currentUserPlugin');
232
        $currentCompany = $currentUserPlugin->getCompany();
233
        $currentUser = $currentUserPlugin->getUser();
234
 
235
        $request = $this->getRequest();
236
 
237
        if ($request->isPost()) {
238
 
239
 
240
 
241
            $dataPost = $request->getPost()->toArray();
242
            $dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : SurveyCampaign::STATUS_INACTIVE;
243
 
244
 
245
            $start_date = empty($dataPost['start_date']) ? '' : $dataPost['start_date'];
246
            $dt = \DateTime::createFromFormat('d/m/Y', $start_date);
247
            if($dt) {
248
                $dataPost['start_date'] = $dt->format('Y-m-d');
249
            } else {
250
                $dataPost['start_date'] = '';
251
            }
252
 
253
            $end_date = empty($dataPost['end_date']) ? '' : $dataPost['end_date'];
254
            $dt = \DateTime::createFromFormat('d/m/Y', $end_date);
255
            if($dt) {
256
                $dataPost['end_date'] = $dt->format('Y-m-d');
257
            } else {
258
                $dataPost['end_date'] = '';
259
            }
260
 
261
 
262
 
263
            $form = new SurveyCampaignForm($this->adapter, $currentCompany->id, SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE);
264
            $form->setData($dataPost);
265
 
266
            if ($form->isValid()) {
267
                $dataPost = (array) $form->getData();
268
 
269
 
270
 
271
 
272
                $surveyFormMapper =  SurveyFormMapper::getInstance($this->adapter);
273
                $surveyForm = $surveyFormMapper->fetchOneByUuid($dataPost['form_id']);
274
 
275
                $content = json_encode([
276
                    'name' => $surveyForm->name,
277
                    'text' => $surveyForm->text,
278
                    'content' => json_decode($surveyForm->content)
279
 
280
                ]);
281
 
282
                $surveyCampaign = new SurveyCampaign();
283
                $surveyCampaign->type = SurveyCampaign::TYPE_ORGANIZATIONAL_CLIMATE;
284
                $surveyCampaign->company_id = $currentCompany->id;
285
                $surveyCampaign->form_id = $surveyForm->id;
286
                $surveyCampaign->name = $dataPost['name'];
287
                $surveyCampaign->start_date = $dataPost['start_date'];
288
                $surveyCampaign->end_date = $dataPost['end_date'];
289
                $surveyCampaign->status = $dataPost['status'];
290
                $surveyCampaign->identity = $dataPost['identity'];
291
                $surveyCampaign->content = $content;
292
 
293
                $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
294
                if($surveyCampaignMapper->insert($surveyCampaign)) {
295
 
296
                    $ok = true;
297
                    if(!empty($dataPost['location_search'])){
298
                        $location = new Location();
299
                        $location->formatted_address = $dataPost['formatted_address'];
300
                        $location->city1 = $dataPost['city1'];
301
                        $location->city2 = $dataPost['city2'];
302
                        $location->address1 = $dataPost['address1'];
303
                        $location->address2 = $dataPost['address2'];
304
                        $location->country = $dataPost['country'];
305
                        $location->latitude = $dataPost['latitude'];
306
                        $location->longitude = $dataPost['longitude'];
307
                        $location->postal_code = $dataPost['postal_code'];
308
                        $location->state = $dataPost['state'];
309
 
310
                        $locationMapper = LocationMapper::getInstance($this->adapter);
311
                        if($locationMapper->insert($location)) {
312
                            $surveyCampaign->location_id = $location->id;
313
                            $surveyCampaignMapper->update($surveyCampaign);
314
 
315
                        } else {
316
                            $ok = false;
317
                        }
318
                    }
319
 
320
                    $user_ids = [];
321
 
322
                    if(!empty($dataPost['job_description_id'])) {
323
                        $surveyCampaignJobDescriptionMapper = SurveyCampaignJobDescriptionMapper::getInstance($this->adapter);
324
                        $jobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
325
 
326
 
327
                        foreach($dataPost['job_description_id'] as $jobDescriptionUuid)
328
                        {
329
                            $jobDescription = $jobDescriptionMapper->fetchOneByUuid($jobDescriptionUuid);
330
 
331
                            if($jobDescription && $jobDescription->company_id = $surveyCampaign->company_id) {
332
                                $surveyCampaignJobDescription = new SurveyCampaignJobDescription();
333
                                $surveyCampaignJobDescription->job_description_id = $jobDescription->id;
334
                                $surveyCampaignJobDescription->campaign_id = $surveyCampaign->id;
335
 
336
                                $ok = $ok && $surveyCampaignJobDescriptionMapper->insert($surveyCampaignJobDescription);
337
                            }
338
                        }
339
 
340
                        $aux_ids = $surveyCampaignJobDescriptionMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
341
                        if(!empty($aux_ids)) {
342
                            $user_ids = array_merge($user_ids, $aux_ids);
343
                        }
344
                    }
345
 
346
 
347
                    if(!empty($dataPost['skill_id'])) {
348
                         $surveyCampaignSkillMapper = SurveyCampaignSkillMapper::getInstance($this->adapter);
349
                         $skillMapper = SkillMapper::getInstance($this->adapter);
350
 
351
                         foreach($dataPost['skill_id'] as $skillUuid) {
352
                             $skill = $skillMapper->fetchOneByUuid($skillUuid);
353
 
354
                             if($skill) {
355
                                $surveyCampaignSkill = new SurveyCampaignSkill();
356
                                $surveyCampaignSkill->skill_id = $skill->id;
357
                                $surveyCampaignSkill->campaign_id = $surveyCampaign->id;
358
 
359
                                $ok = $ok &&  $surveyCampaignSkillMapper->insert($surveyCampaignSkill);
360
                             }
361
                         }
362
 
363
                         $aux_ids = $surveyCampaignSkillMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
364
                         if(!empty($aux_ids)) {
365
 
366
                             $user_ids = array_merge($user_ids, $aux_ids);
367
                         }
368
                     }
369
 
370
 
371
 
372
                    if(!empty($dataPost['industry_id'])) {
373
                         $surveyCampaignIndustryMapper = SurveyCampaignIndustryMapper::getInstance($this->adapter);
374
                         $industryMapper = IndustryMapper::getInstance($this->adapter);
375
 
376
                         foreach($dataPost['industry_id'] as $industryUuid)
377
                         {
378
 
379
                            $industry = $industryMapper->fetchOneByUuid($industryUuid);
380
                            if($industry) {
381
                                $surveyCampaignIndustry = new SurveyCampaignIndustry();
382
                                $surveyCampaignIndustry->industry_id = $industry->id;
383
                                $surveyCampaignIndustry->campaign_id = $surveyCampaign->id;
384
 
385
                                $ok = $ok &&  $surveyCampaignIndustryMapper->insert($surveyCampaignIndustry);
386
 
387
                            }
388
                         }
389
 
390
                         $aux_ids = $surveyCampaignIndustryMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
391
                         if(!empty($aux_ids)) {
392
                             $user_ids = array_merge($user_ids, $aux_ids);
393
                         }
394
 
395
                    }
396
 
397
 
398
                    if(!empty($dataPost['language_id'])){
399
                        $surveyCampaignLanguageMapper = SurveyCampaignLanguageMapper::getInstance($this->adapter);
400
 
401
                        foreach($dataPost['language_id'] as $language_id)
402
                        {
403
                            $surveyCampaignLanguage = new SurveyCampaignLanguage();
404
                            $surveyCampaignLanguage->language_id = $language_id;
405
                            $surveyCampaignLanguage->campaign_id = $surveyCampaign->id;
406
 
407
                            $ok = $ok && $surveyCampaignLanguageMapper->insert($surveyCampaignLanguage);
408
                        }
409
 
410
                        $aux_ids = $surveyCampaignLanguageMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
411
                        if(!empty($aux_ids)) {
412
                            $user_ids = array_merge($user_ids, $aux_ids);
413
                        }
414
                     }
415
 
416
                     if($surveyCampaign->location_id) {
417
                         $locationMapper = LocationMapper::getInstance($this->adapter);
418
                         $location = $locationMapper->fetchOne($surveyCampaign->location_id);
419
 
420
                         if($location) {
421
                             $aux_ids = $locationMapper->fetchAllUserIdsByCompanyId($surveyCampaign->company_id, $location->latitude, $location->longitude);
422
 
423
                             if(!empty($aux_ids)) {
424
                                 $user_ids = array_merge($user_ids, $aux_ids);
425
                             }
426
                         }
427
                     }
428
 
429
 
430
                    if(!empty($user_ids)) {
431
                        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
432
 
433
                        $calendarEventMapper = CalendarEventMapper::getInstance($this->adapter);
434
 
435
                        $user_ids = array_unique($user_ids);
436
 
437
                        foreach($user_ids as $user_id)
438
                        {
439
                            $surveyTest = new SurveyTest();
440
                            $surveyTest->company_id = $surveyCampaign->company_id;
441
                            $surveyTest->campaign_id = $surveyCampaign->id;
442
                            $surveyTest->user_id = $user_id;
443
                            $surveyTest->content = $content;
444
                            $surveyTest->status = SurveyTest::STATUS_PENDING;
445
 
446
                            if($surveyTestMapper->insert($surveyTest)) {
447
 
448
                                $calendarEvent = new CalendarEvent();
16820 efrain 449
                                $calendarEvent->relational_id = $surveyTest->id;
16817 efrain 450
                                $calendarEvent->user_id = $user_id;
451
                                $calendarEvent->type = CalendarEvent::TYPE_SURVEY_ORGANIZATIONAL_CLIMATE;
452
                                $calendarEvent->start_time = $surveyCampaign->end_date;
453
                                $calendarEventMapper->insert($calendarEvent);
454
                            }
455
 
456
                        }
457
 
458
                    }
459
 
460
 
461
                    $data = [
462
                        'success' => true,
463
                        'data' => 'LABEL_RECORD_ADDED'
464
                    ];
465
 
466
                } else {
467
                    $data = [
468
                        'success'   => false,
469
                        'data'      => $surveyCampaignMapper->getError()
470
                    ];
471
                }
472
 
473
 
474
                return new JsonModel($data);
475
            } else {
476
                $messages = [];
477
                $form_messages = (array) $form->getMessages();
478
                foreach ($form_messages as $fieldname => $field_messages) {
479
 
480
                    $messages[$fieldname] = array_values($field_messages);
481
                }
482
 
483
                return new JsonModel([
484
                    'success' => false,
485
                    'data' => $messages
486
                ]);
487
            }
488
        } else {
489
            $data = [
490
                'success' => false,
491
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
492
            ];
493
 
494
            return new JsonModel($data);
495
        }
496
 
497
        return new JsonModel($data);
498
    }
499
 
500
 
501
/*
502
    public function viewAction()
503
    {
504
        $request = $this->getRequest();
505
        $currentUserPlugin = $this->plugin('currentUserPlugin');
506
        $currentCompany = $currentUserPlugin->getCompany();
507
        $currentUser = $currentUserPlugin->getUser();
508
 
509
        $request = $this->getRequest();
510
        $uuid = $this->params()->fromRoute('id');
511
 
512
 
513
        if (!$uuid) {
514
            $data = [
515
                'success' => false,
516
                'data' => 'ERROR_INVALID_PARAMETER'
517
            ];
518
 
519
            return new JsonModel($data);
520
        }
521
 
522
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
523
        $organizationalClimate = $surveyCampaignMapper->fetchOneByUuid($uuid);
524
 
525
        if (!$organizationalClimate) {
526
            $data = [
527
                'success' => false,
528
                'data' => 'ERROR_RECORD_NOT_FOUND'
529
            ];
530
 
531
            return new JsonModel($data);
532
        }
533
 
534
        if ($organizationalClimate->company_id != $currentCompany->id) {
535
            return new JsonModel([
536
                'success' => false,
537
                'data' => 'ERROR_UNAUTHORIZED'
538
            ]);
539
        }
540
 
541
 
542
      if ($request->isGet()) {
543
            $hydrator = new ObjectPropertyHydrator();
544
 
545
            $organizationalClimatelenguageMapper = OrganizationalClimateLanguageMapper::getInstance($this->adapter);
546
            $languages  = $organizationalClimatelenguageMapper->fetchAllBySurveyId($organizationalClimate->id);
547
 
548
            $lenguage = [];
549
            foreach($languages as $language)
550
            {
551
                array_push($lenguage, $language->language_id);
552
            }
553
 
554
            $organizationalClimateSkillMapper = OrganizationalClimateSkillMapper::getInstance($this->adapter);
555
            $skills  = $organizationalClimateSkillMapper->fetchAllBySurveyId($organizationalClimate->id);
556
 
557
            $skilla = [];
558
            foreach($skills as $skill)
559
            {
560
                array_push($skilla, $skill->skill_id);
561
            }
562
 
563
 
564
            $skillMaster = [];
565
            foreach($skilla as $skillb)
566
            {
567
                $skillMapper = SkillMapper::getInstance($this->adapter);
568
                $skillm = $skillMapper->fetchOne($skillb);
569
                array_push($skillMaster, $skillm->uuid);
570
            }
571
 
572
            $OrganizationalClimateJobDescriptionMapper = OrganizationalClimateJobDescriptionMapper::getInstance($this->adapter);
573
            $OrganizationalClimateJobDescriptions  = $OrganizationalClimateJobDescriptionMapper->fetchAllBySurveyId($organizationalClimate->id);
574
 
575
            $OrganizationalClimateJobDescriptionA = [];
576
            foreach($OrganizationalClimateJobDescriptions as $OrganizationalClimateJobDescription)
577
            {
578
                array_push($OrganizationalClimateJobDescriptionA, $OrganizationalClimateJobDescription->job_description_id);
579
            }
580
 
581
            $OrganizationalClimateJobDescriptionMaster = [];
582
            foreach($OrganizationalClimateJobDescriptionA as $OrganizationalClimateJobDescriptionB)
583
            {
584
                $JobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
585
                $JobDescriptionm = $JobDescriptionMapper->fetchOne($OrganizationalClimateJobDescriptionB);
586
                array_push($OrganizationalClimateJobDescriptionMaster, $JobDescriptionm->uuid);
587
            }
588
 
589
            $organizationalClimateIndustryMapper = OrganizationalClimateIndustryMapper::getInstance($this->adapter);
590
            $industrys = $organizationalClimateIndustryMapper->fetchAllBySurveyId($organizationalClimate->id);
591
 
592
            $industrya = [];
593
            foreach($industrys as $industry)
594
            {
595
                array_push($industrya, $industry->industry_id);
596
            }
597
 
598
 
599
            $industryMaster = [];
600
            foreach($industrya as $industryb)
601
            {
602
                $industryMapper = IndustryMapper::getInstance($this->adapter);
603
                $industrym = $industryMapper->fetchOne($industryb);
604
                array_push($industryMaster, $industrym->uuid);
605
            }
606
 
607
 
608
            $organizationalClimateLocationMapper = OrganizationalClimateLocationMapper::getInstance($this->adapter);
609
            $location  = $organizationalClimateLocationMapper->fetchAllBySurveyId($organizationalClimate->id);
610
 
611
            //  return new JsonModel([
612
            //      'success' => false,
613
            //      'data' => $location
614
            //  ]);
615
 
616
 
617
 
618
            $organizationalClimateFormMapper = OrganizationalClimateFormMapper::getInstance($this->adapter);
619
            $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
620
            // return new JsonModel([
621
            //     'success' => false,
622
            //     'data' => $OrganizationalClimateJobDescriptionA
623
            // ]);
624
            $data = [
625
                'success' => true,
626
                'data' => [
627
                    'name' => $organizationalClimate->name,
628
                    'form_id' => $organizationalClimateForm->uuid,
629
                    'target' => $organizationalClimate->target,
630
                    'identity' => $organizationalClimate->identity,
631
                    'since_date' => $organizationalClimate->since_date,
632
                    'last_date' => $organizationalClimate->last_date,
633
                    'status' => $organizationalClimate->status,
634
                    'lenguage_id' => $lenguage,
635
                    'skill_id' => $skillMaster,
636
                    'job_description_id' => $OrganizationalClimateJobDescriptionMaster,
637
                    'industry_id' => $industryMaster,
638
                    //'location' => $location->city1,
639
                ]
640
            ];
641
 
642
            return new JsonModel($data);
643
        } else {
644
            $data = [
645
                'success' => false,
646
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
647
            ];
648
 
649
            return new JsonModel($data);
650
        }
651
 
652
        return new JsonModel($data);
653
    }
654
*/
655
    public function deleteAction()
656
    {
657
        $request = $this->getRequest();
658
        $currentUserPlugin = $this->plugin('currentUserPlugin');
659
        $currentCompany = $currentUserPlugin->getCompany();
660
        $currentUser = $currentUserPlugin->getUser();
661
 
662
        $request = $this->getRequest();
663
        $uuid = $this->params()->fromRoute('id');
664
 
665
        if (!$uuid) {
666
            $data = [
667
                'success' => false,
668
                'data' => 'ERROR_INVALID_PARAMETER'
669
            ];
670
 
671
            return new JsonModel($data);
672
        }
673
 
674
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
675
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
676
        if (!$surveyCampaign) {
677
            $data = [
678
                'success' => false,
679
                'data' => 'ERROR_RECORD_NOT_FOUND'
680
            ];
681
 
682
            return new JsonModel($data);
683
        }
684
 
685
        if ($surveyCampaign->company_id != $currentCompany->id) {
686
            return new JsonModel([
687
                'success' => false,
688
                'data' => 'ERROR_UNAUTHORIZED'
689
            ]);
690
        }
691
 
692
        if ($request->isPost()) {
693
 
694
            $result = $surveyCampaignMapper->delete($surveyCampaign->id);
695
            if ($result) {
696
                $this->logger->info('Se borro la capaña : ' . $surveyCampaign->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
697
 
698
                $data = [
699
                    'success' => true,
700
                    'data' => 'LABEL_RECORD_DELETED'
701
                ];
702
            } else {
703
 
704
                $data = [
705
                    'success' => false,
706
                    'data' => $surveyCampaignMapper->getError()
707
                ];
708
 
709
                return new JsonModel($data);
710
            }
711
        } else {
712
            $data = [
713
                'success' => false,
714
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
715
            ];
716
 
717
            return new JsonModel($data);
718
        }
719
 
720
        return new JsonModel($data);
721
    }
722
 
723
 
724
    public function overviewAction()
725
    {
726
        $request = $this->getRequest();
727
        $currentUserPlugin = $this->plugin('currentUserPlugin');
728
        $currentCompany = $currentUserPlugin->getCompany();
729
        $currentUser = $currentUserPlugin->getUser();
730
 
731
        $request = $this->getRequest();
732
        $uuid = $this->params()->fromRoute('id');
733
 
734
        if (!$uuid) {
735
            $data = [
736
                'success' => false,
737
                'data' => 'ERROR_INVALID_PARAMETER'
738
            ];
739
 
740
            return new JsonModel($data);
741
        }
742
 
743
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
744
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
745
 
746
        if (!$surveyCampaign) {
747
            $data = [
748
                'success' => false,
749
                'data' => 'ERROR_RECORD_NOT_FOUND'
750
            ];
751
 
752
            return new JsonModel($data);
753
        }
754
 
755
        if ($surveyCampaign->company_id != $currentCompany->id) {
756
            return new JsonModel([
757
                'success' => false,
758
                'data' => 'ERROR_UNAUTHORIZED'
759
            ]);
760
        }
761
 
762
 
763
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
764
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
765
 
766
        if (!$surveyTests) {
767
            $data = [
768
                'success' => false,
769
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
770
            ];
771
 
772
            return new JsonModel($data);
773
        }
774
 
775
 
776
        if ($request->isGet()) {
777
            $allTests = array_map(
778
                function($response) {
779
                   $content = json_decode($response->content, true);
780
                   return $content['content'];
781
                },
782
                $surveyTests,
783
            );
784
 
785
            $data = [];
786
            $countTests = count($allTests);
787
 
788
 
789
            foreach($allTests as $test)
790
            {
791
                foreach($test as $section)
792
                {
793
 
794
                    if(!isset($data[ $section['slug_section'] ] )) {
795
                        $data[ $section['slug_section'] ]  = [
796
                           'name' => $section['name'],
797
                           'text' => $section['text'],
798
                           'questions' => []
799
 
800
                        ];
801
                    }
802
 
803
 
804
                    foreach($section['questions'] as $question)
805
                    {
806
                        switch ($question['type'])
807
                        {
808
                            case 'range1to5':
809
 
810
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
811
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
812
                                        'text' => $question['text'],
813
                                        'options' => [
814
                                            'option1' => [
815
                                                'text' => '1',
816
                                                'total' => 0,
817
                                                'average' => 0,
818
                                            ],
819
                                            'option2' => [
820
                                                'text' => '2',
821
                                                'total' => 0,
822
                                                'average' => 0,
823
                                            ],
824
                                            'option3' => [
825
                                                'text' => '3',
826
                                                'total' => 0,
827
                                                'average' => 0,
828
                                            ],
829
                                            'option4' => [
830
                                                'text' => '4',
831
                                                'total' => 0,
832
                                                'average' => 0,
833
                                            ],
834
                                            'option5' => [
835
                                                'text' => '5',
836
                                                'total' => 0,
837
                                                'average' => 0,
838
                                            ]
839
                                        ]
840
                                    ];
841
 
842
                                }
843
 
844
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
845
                                $total++;
846
                                $average = ($total * 100) / $countTests;
847
 
848
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
849
 
850
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
851
 
852
                                break;
853
 
854
 
855
                            case 'multiple':
856
                            case 'simple':
857
 
858
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
859
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
860
                                        'text' => $question['text'],
861
                                        'options' => []
862
                                    ];
863
 
864
                                    foreach($question['options'] as $option)
865
                                    {
866
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
867
                                            'text' => $option['text'],
868
                                            'total' => 0,
869
                                            'average' => 0,
870
                                        ];
871
                                    }
872
 
873
                                }
874
 
875
 
876
                                foreach($question['options'] as $option)
877
                                {
878
                                    if($option['checked']) {
879
 
880
 
881
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
882
                                        $total++;
883
                                        $average = ($total * 100) / $countTests;
884
 
885
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
886
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
887
                                    }
888
                                }
889
 
890
                                break;
891
                        }
892
                    }
893
                }
894
            }
895
 
896
 
897
 
898
            $this->layout()->setTemplate('layout/layout-backend.phtml');
899
            $viewModel = new ViewModel();
900
            $viewModel->setTemplate('leaders-linked/organizational-climate-campaign/overview.phtml');
901
            $viewModel->setVariables([
902
                'data' => $data,
903
                'campaign' => $surveyCampaign,
904
            ]);
905
            return $viewModel ;
906
 
907
        } else {
908
            $data = [
909
                'success' => false,
910
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
911
            ];
912
 
913
            return new JsonModel($data);
914
        }
915
 
916
    }
917
 
918
 
919
    public function pdfAction()
920
    {
921
        $request = $this->getRequest();
922
        $currentUserPlugin = $this->plugin('currentUserPlugin');
923
        $currentCompany = $currentUserPlugin->getCompany();
924
        $currentUser = $currentUserPlugin->getUser();
925
 
926
        $request = $this->getRequest();
927
        $uuid = $this->params()->fromRoute('id');
928
 
929
        if (!$uuid) {
930
            $data = [
931
                'success' => false,
932
                'data' => 'ERROR_INVALID_PARAMETER'
933
            ];
934
 
935
            return new JsonModel($data);
936
        }
937
 
938
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
939
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
940
 
941
        if (!$surveyCampaign) {
942
            $data = [
943
                'success' => false,
944
                'data' => 'ERROR_RECORD_NOT_FOUND'
945
            ];
946
 
947
            return new JsonModel($data);
948
        }
949
 
950
        if ($surveyCampaign->company_id != $currentCompany->id) {
951
            return new JsonModel([
952
                'success' => false,
953
                'data' => 'ERROR_UNAUTHORIZED'
954
            ]);
955
        }
956
 
957
 
958
 
959
 
960
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
961
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
962
 
963
        if (!$surveyTests) {
964
            $data = [
965
                'success' => false,
966
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
967
            ];
968
 
969
            return new JsonModel($data);
970
        }
971
 
972
 
973
        if ($request->isGet()) {
974
            $allTests = array_map(
975
                function($response) {
976
                    $content = json_decode($response->content, true);
977
                    return $content['content'];
978
                },
979
                $surveyTests,
980
                );
981
 
982
            $data = [];
983
            $countTests = count($allTests);
984
 
985
 
986
            foreach($allTests as $test)
987
            {
988
                foreach($test as $section)
989
                {
990
 
991
                    if(!isset($data[ $section['slug_section'] ] )) {
992
                        $data[ $section['slug_section'] ]  = [
993
                            'name' => $section['name'],
994
                            'text' => $section['text'],
995
                            'questions' => []
996
 
997
                        ];
998
                    }
999
 
1000
 
1001
                    foreach($section['questions'] as $question)
1002
                    {
1003
                        switch ($question['type'])
1004
                        {
1005
                            case 'range1to5':
1006
 
1007
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1008
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1009
                                        'text' => $question['text'],
1010
                                        'options' => [
1011
                                            'option1' => [
1012
                                                'text' => '1',
1013
                                                'total' => 0,
1014
                                                'average' => 0,
1015
                                            ],
1016
                                            'option2' => [
1017
                                                'text' => '2',
1018
                                                'total' => 0,
1019
                                                'average' => 0,
1020
                                            ],
1021
                                            'option3' => [
1022
                                                'text' => '3',
1023
                                                'total' => 0,
1024
                                                'average' => 0,
1025
                                            ],
1026
                                            'option4' => [
1027
                                                'text' => '4',
1028
                                                'total' => 0,
1029
                                                'average' => 0,
1030
                                            ],
1031
                                            'option5' => [
1032
                                                'text' => '5',
1033
                                                'total' => 0,
1034
                                                'average' => 0,
1035
                                            ]
1036
                                        ]
1037
                                    ];
1038
 
1039
                                }
1040
 
1041
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
1042
                                $total++;
1043
                                $average = ($total * 100) / $countTests;
1044
 
1045
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
1046
 
1047
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
1048
 
1049
                                break;
1050
 
1051
 
1052
                            case 'multiple':
1053
                            case 'simple':
1054
 
1055
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1056
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1057
                                        'text' => $question['text'],
1058
                                        'options' => []
1059
                                    ];
1060
 
1061
                                    foreach($question['options'] as $option)
1062
                                    {
1063
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
1064
                                            'text' => $option['text'],
1065
                                            'total' => 0,
1066
                                            'average' => 0,
1067
                                        ];
1068
                                    }
1069
 
1070
                                }
1071
 
1072
 
1073
                                foreach($question['options'] as $option)
1074
                                {
1075
                                    if($option['checked']) {
1076
 
1077
 
1078
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
1079
                                        $total++;
1080
                                        $average = ($total * 100) / $countTests;
1081
 
1082
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
1083
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
1084
                                    }
1085
                                }
1086
 
1087
                                break;
1088
                        }
1089
                    }
1090
                }
1091
            }
1092
 
1093
 
1094
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $currentCompany->uuid;
1095
 
1096
            $header = $currentCompany->header ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->header : '';
1097
            if(empty($header) || !file_exists($header)) {
1098
                $header = $this->config['leaderslinked.images_default.company_pdf_header'];
1099
            }
1100
 
1101
            $footer = $currentCompany->footer ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->footer : '';
1102
            if(empty($footer) || !file_exists($footer)) {
1103
                $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
1104
            }
1105
 
1106
            $content = json_decode($surveyCampaign->content, true);
1107
 
1108
 
1109
            //Generate New PDF
1110
            $pdf = new SurveyReport();
1111
            $pdf->header = $header;
1112
            $pdf->footer = $footer;
1113
            $pdf->campaignName = $surveyCampaign->name;
1114
            $pdf->formName = $content['name'];
1115
            $pdf->formText = $content['text'];
1116
            $pdf->SetTitle(Functions::utf8_decode($surveyCampaign->name));
1117
 
1118
            $pdf->translator = $this->translator;
1119
 
1120
 
1121
 
1122
            $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;
1123
            if(!file_exists($target_path)) {
1124
                mkdir($target_path, 0755, true);
1125
            } else {
1126
                Functions::deleteFiles($target_path);
1127
            }
1128
 
1129
 
1130
            $pdf->AliasNbPages();
1131
 
1132
 
1133
            foreach($data as $slug_section => $section)
1134
            {
1135
                $pdf->AddPage();
1136
                $pdf->addSection($section['name'], $section['text']);
1137
 
1138
 
1139
                $countQuestion = 0;
1140
                foreach ($section['questions'] as $slug_question => $question)
1141
                {
1142
                    if($countQuestion == 2) {
1143
                        $pdf->AddPage();
1144
                        $countQuestion = 0;
1145
                    }
1146
 
1147
                    $labels = [];
1148
                    $values = [];
1149
 
1150
                    $existEmptyOption = false;
1151
                    foreach($question['options'] as $slug_option => $option)
1152
                    {
1153
                        if(empty($option['average'])) {
1154
                            $existEmptyOption = true;
1155
                            continue;
1156
                        }
1157
 
1158
                        array_push($labels, $option['text']);
1159
                        array_push($values, $option['average']);
1160
                    }
1161
 
1162
                    if($existEmptyOption) {
1163
                        array_push($labels, $this->translator->translate('LABEL_SURVEY_OTHER_EMPTY_OPTIONS'));
1164
                        array_push($values, 0);
1165
                    }
1166
 
1167
                    $filename = $target_path . DIRECTORY_SEPARATOR .  $slug_question . '.png';
1168
                    $pdf->Cell(0,10, $question['text'],0,1);
1169
                    $pdf->pieChart($labels, $values, '', $filename);
1170
                    $pdf->SetFont('Arial', '', 12);
1171
                    $pdf->Image($filename, 10, $pdf->getY(), 190);
1172
                    $pdf->setY($pdf->getY() + 90);
1173
 
1174
                    $countQuestion++;
1175
 
1176
                }
1177
 
1178
 
1179
            }
1180
 
1181
 
1182
 
1183
            $content = $pdf->Output('S');
1184
 
1185
            $response = new Response();
1186
            $response->setStatusCode(200);
1187
            $response->setContent($content);
1188
 
1189
 
1190
 
1191
            $headers = $response->getHeaders();
1192
            $headers->clearHeaders();
1193
 
1194
            $headers->addHeaderLine('Content-Description: File Transfer');
1195
            $headers->addHeaderLine('Content-Type: application/pdf');
1196
            //$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);
1197
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
1198
            $headers->addHeaderLine('Expires: 0');
1199
            $headers->addHeaderLine('Cache-Control: must-revalidate');
1200
            $headers->addHeaderLine('Pragma: public');
1201
            return $response;
1202
 
1203
 
1204
        } else {
1205
            $data = [
1206
                'success' => false,
1207
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1208
            ];
1209
 
1210
            return new JsonModel($data);
1211
        }
1212
 
1213
    }
1214
 
1215
    public function excelAction()
1216
    {
1217
        $request = $this->getRequest();
1218
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1219
        $currentCompany = $currentUserPlugin->getCompany();
1220
        $currentUser = $currentUserPlugin->getUser();
1221
 
1222
        $request = $this->getRequest();
1223
        $uuid = $this->params()->fromRoute('id');
1224
 
1225
        if (!$uuid) {
1226
            $data = [
1227
                'success' => false,
1228
                'data' => 'ERROR_INVALID_PARAMETER'
1229
            ];
1230
 
1231
            return new JsonModel($data);
1232
        }
1233
 
1234
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
1235
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
1236
 
1237
        if (!$surveyCampaign) {
1238
            $data = [
1239
                'success' => false,
1240
                'data' => 'ERROR_RECORD_NOT_FOUND'
1241
            ];
1242
 
1243
            return new JsonModel($data);
1244
        }
1245
 
1246
        if ($surveyCampaign->company_id != $currentCompany->id) {
1247
            return new JsonModel([
1248
                'success' => false,
1249
                'data' => 'ERROR_UNAUTHORIZED'
1250
            ]);
1251
        }
1252
 
1253
 
1254
 
1255
 
1256
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
1257
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
1258
 
1259
        if (!$surveyTests) {
1260
            $data = [
1261
                'success' => false,
1262
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
1263
            ];
1264
 
1265
            return new JsonModel($data);
1266
        }
1267
 
1268
 
1269
        if ($request->isGet()) {
1270
            $allTests = array_map(
1271
                function($response) {
1272
                    $content = json_decode($response->content, true);
1273
                    return $content['content'];
1274
                },
1275
                $surveyTests,
1276
                );
1277
 
1278
            $data = [];
1279
            $countTests = count($allTests);
1280
 
1281
 
1282
            foreach($allTests as $test)
1283
            {
1284
                foreach($test as $section)
1285
                {
1286
 
1287
                    if(!isset($data[ $section['slug_section'] ] )) {
1288
                        $data[ $section['slug_section'] ]  = [
1289
                            'name' => $section['name'],
1290
                            'text' => $section['text'],
1291
                            'questions' => []
1292
 
1293
                        ];
1294
                    }
1295
 
1296
 
1297
                    foreach($section['questions'] as $question)
1298
                    {
1299
                        switch ($question['type'])
1300
                        {
1301
                            case 'range1to5':
1302
 
1303
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1304
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1305
                                        'text' => $question['text'],
1306
                                        'options' => [
1307
                                            'option1' => [
1308
                                                'text' => '1',
1309
                                                'total' => 0,
1310
                                                'average' => 0,
1311
                                            ],
1312
                                            'option2' => [
1313
                                                'text' => '2',
1314
                                                'total' => 0,
1315
                                                'average' => 0,
1316
                                            ],
1317
                                            'option3' => [
1318
                                                'text' => '3',
1319
                                                'total' => 0,
1320
                                                'average' => 0,
1321
                                            ],
1322
                                            'option4' => [
1323
                                                'text' => '4',
1324
                                                'total' => 0,
1325
                                                'average' => 0,
1326
                                            ],
1327
                                            'option5' => [
1328
                                                'text' => '5',
1329
                                                'total' => 0,
1330
                                                'average' => 0,
1331
                                            ]
1332
                                        ]
1333
                                    ];
1334
 
1335
                                }
1336
 
1337
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
1338
                                $total++;
1339
                                $average = ($total * 100) / $countTests;
1340
 
1341
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
1342
 
1343
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
1344
 
1345
                                break;
1346
 
1347
 
1348
                            case 'multiple':
1349
                            case 'simple':
1350
 
1351
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1352
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1353
                                        'text' => $question['text'],
1354
                                        'options' => []
1355
                                    ];
1356
 
1357
                                    foreach($question['options'] as $option)
1358
                                    {
1359
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
1360
                                            'text' => $option['text'],
1361
                                            'total' => 0,
1362
                                            'average' => 0,
1363
                                        ];
1364
                                    }
1365
 
1366
                                }
1367
 
1368
 
1369
                                foreach($question['options'] as $option)
1370
                                {
1371
                                    if($option['checked']) {
1372
 
1373
 
1374
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
1375
                                        $total++;
1376
                                        $average = ($total * 100) / $countTests;
1377
 
1378
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
1379
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
1380
                                    }
1381
                                }
1382
 
1383
                                break;
1384
                        }
1385
                    }
1386
                }
1387
            }
1388
 
1389
            $spreadsheet = new Spreadsheet();
1390
            $sheet = $spreadsheet->getActiveSheet();
1391
 
1392
 
1393
            $row = 1;
1394
            foreach($data as $slug_section => $section)
1395
            {
1396
                $sheet->setCellValue('A' . $row , $section['name']);
1397
                $row++;
1398
                $sheet->setCellValue('A' . $row , $section['text']);
1399
 
1400
                $row++;
1401
 
1402
 
1403
 
1404
                foreach ($section['questions'] as $slug_question => $question)
1405
                {
1406
                    $sheet->setCellValue('B' . $row , $question['text']);
1407
                    $row++;
1408
 
1409
                    $sheet->setCellValue('C' . $row , $this->translator->translate('LABEL_OPTION'));
1410
                    $sheet->setCellValue('D' . $row , $this->translator->translate('LABEL_TOTAL'));
1411
                    $sheet->setCellValue('E' . $row , $this->translator->translate('LABEL_AVERAGE') . ' %');
1412
                    $row++;
1413
 
1414
 
1415
 
1416
                    foreach($question['options'] as $slug_option => $option)
1417
                    {
1418
                        $sheet->setCellValue('C' . $row , $option['text']);
1419
                        $sheet->setCellValue('D' . $row , $option['total']);
1420
                        $sheet->setCellValue('E' . $row , round($option['average'], 2));
1421
                        $row++;
1422
                    }
1423
                    $row++;
1424
                }
1425
                $row++;
1426
            }
1427
 
1428
            $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;
1429
            if(!file_exists($target_path)) {
1430
                mkdir($target_path, 0755, true);
1431
            }
1432
 
1433
 
1434
            $filename = Functions::normalizeStringFilename($surveyCampaign->uuid . '.xls');
1435
            $fullFilename = $target_path  . DIRECTORY_SEPARATOR . $filename;
1436
 
1437
            $writer = new Xlsx($spreadsheet);
1438
            $writer->save($fullFilename);
1439
 
1440
            $content = file_get_contents($fullFilename);
1441
 
1442
 
1443
            $response = new Response();
1444
            $response->setStatusCode(200);
1445
            $response->setContent($content);
1446
 
1447
 
1448
 
1449
            $headers = $response->getHeaders();
1450
            $headers->clearHeaders();
1451
 
1452
            $headers->addHeaderLine('Content-Description: File Transfer');
1453
            $headers->addHeaderLine('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
1454
            $headers->addHeaderLine('Content-Disposition: attachment; filename='. $filename);
1455
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
1456
            $headers->addHeaderLine('Expires: 0');
1457
            $headers->addHeaderLine('Cache-Control: must-revalidate');
1458
            $headers->addHeaderLine('Pragma: public');
1459
            return $response;
1460
 
1461
 
1462
        } else {
1463
            $data = [
1464
                'success' => false,
1465
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1466
            ];
1467
 
1468
            return new JsonModel($data);
1469
        }
1470
 
1471
    }
1472
 
1473
}