Proyectos de Subversion LeadersLinked - Backend

Rev

Ir a la última revisión | | 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 SurveyCampaignController 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, 'survey/campaign/delete');
149
                $allowOverview = $acl->isAllowed($currentUser->usertype_id, 'survey/campaign/overview');
150
                $allowPdf = $acl->isAllowed($currentUser->usertype_id, 'survey/campaign/pdf');
151
                $allowExcel = $acl->isAllowed($currentUser->usertype_id, 'survey/campaign/excel');
152
 
153
 
154
                $surveyForms = [];
155
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
156
 
157
                $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
158
                $paginator = $surveyCampaignMapper->fetchAllDataTableNormalByCompanyId($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('survey/campaign/delete', ['id' => $record->uuid]) : '',
192
                            'link_overview' => $allowOverview && $record->completed_test ?  $this->url()->fromRoute('survey/campaign/overview', ['id' => $record->uuid]) : '',
193
                            'link_pdf' => $allowPdf && $record->completed_test ? $this->url()->fromRoute('survey/campaign/pdf', ['id' => $record->uuid]) : '',
194
                            'link_excel' => $allowExcel && $record->completed_test ? $this->url()->fromRoute('survey/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_NORMAL);
212
 
213
                $this->layout()->setTemplate('layout/layout-backend');
214
                $viewModel = new ViewModel();
215
                $viewModel->setTemplate('leaders-linked/survey-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_NORMAL);
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_NORMAL;
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
 
323
                    if(!empty($dataPost['skill_id'])) {
324
                         $surveyCampaignSkillMapper = SurveyCampaignSkillMapper::getInstance($this->adapter);
325
                         $skillMapper = SkillMapper::getInstance($this->adapter);
326
 
327
                         foreach($dataPost['skill_id'] as $skillUuid) {
328
                             $skill = $skillMapper->fetchOneByUuid($skillUuid);
329
 
330
                             if($skill) {
331
                                $surveyCampaignSkill = new SurveyCampaignSkill();
332
                                $surveyCampaignSkill->skill_id = $skill->id;
333
                                $surveyCampaignSkill->campaign_id = $surveyCampaign->id;
334
 
335
                                $ok = $ok &&  $surveyCampaignSkillMapper->insert($surveyCampaignSkill);
336
                             }
337
                         }
338
 
339
                         $aux_ids = $surveyCampaignSkillMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
340
                         if(!empty($aux_ids)) {
341
 
342
                             $user_ids = array_merge($user_ids, $aux_ids);
343
                         }
344
                     }
345
 
346
 
347
 
348
                    if(!empty($dataPost['language_id'])){
349
                        $surveyCampaignLanguageMapper = SurveyCampaignLanguageMapper::getInstance($this->adapter);
350
 
351
                        foreach($dataPost['language_id'] as $language_id)
352
                        {
353
                            $surveyCampaignLanguage = new SurveyCampaignLanguage();
354
                            $surveyCampaignLanguage->language_id = $language_id;
355
                            $surveyCampaignLanguage->campaign_id = $surveyCampaign->id;
356
 
357
                            $ok = $ok && $surveyCampaignLanguageMapper->insert($surveyCampaignLanguage);
358
                        }
359
 
360
                        $aux_ids = $surveyCampaignLanguageMapper->fetchAllUserIdsByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
361
                        if(!empty($aux_ids)) {
362
                            $user_ids = array_merge($user_ids, $aux_ids);
363
                        }
364
                     }
365
 
366
                     if($surveyCampaign->location_id) {
367
                         $locationMapper = LocationMapper::getInstance($this->adapter);
368
                         $location = $locationMapper->fetchOne($surveyCampaign->location_id);
369
 
370
                         if($location) {
371
                             $aux_ids = $locationMapper->fetchAllUserIdsByCompanyId($surveyCampaign->company_id, $location->latitude, $location->longitude);
372
 
373
                             if(!empty($aux_ids)) {
374
                                 $user_ids = array_merge($user_ids, $aux_ids);
375
                             }
376
                         }
377
                     }
378
 
379
 
380
                    if(!empty($user_ids)) {
381
                        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
382
 
383
                        $calendarEventMapper = CalendarEventMapper::getInstance($this->adapter);
384
 
385
                        $user_ids = array_unique($user_ids);
386
 
387
                        foreach($user_ids as $user_id)
388
                        {
389
                            $surveyTest = new SurveyTest();
390
                            $surveyTest->company_id = $surveyCampaign->company_id;
391
                            $surveyTest->campaign_id = $surveyCampaign->id;
392
                            $surveyTest->user_id = $user_id;
393
                            $surveyTest->content = $content;
394
                            $surveyTest->status = SurveyTest::STATUS_PENDING;
395
 
396
                            if($surveyTestMapper->insert($surveyTest)) {
397
 
398
                                $calendarEvent = new CalendarEvent();
399
                                $calendarEvent->relational_id = $surveyCampaign->id;
400
                                $calendarEvent->user_id = $user_id;
401
                                $calendarEvent->type = CalendarEvent::TYPE_SURVEY_NORMAL;
402
                                $calendarEvent->start_time = $surveyCampaign->end_date;
403
                                $calendarEventMapper->insert($calendarEvent);
404
                            }
405
 
406
                        }
407
 
408
                    }
409
 
410
 
411
                    $data = [
412
                        'success' => true,
413
                        'data' => 'LABEL_RECORD_ADDED'
414
                    ];
415
 
416
                } else {
417
                    $data = [
418
                        'success'   => false,
419
                        'data'      => $surveyCampaignMapper->getError()
420
                    ];
421
                }
422
 
423
 
424
                return new JsonModel($data);
425
            } else {
426
                $messages = [];
427
                $form_messages = (array) $form->getMessages();
428
                foreach ($form_messages as $fieldname => $field_messages) {
429
 
430
                    $messages[$fieldname] = array_values($field_messages);
431
                }
432
 
433
                return new JsonModel([
434
                    'success' => false,
435
                    'data' => $messages
436
                ]);
437
            }
438
        } else {
439
            $data = [
440
                'success' => false,
441
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
442
            ];
443
 
444
            return new JsonModel($data);
445
        }
446
 
447
        return new JsonModel($data);
448
    }
449
 
450
 
451
/*
452
    public function viewAction()
453
    {
454
        $request = $this->getRequest();
455
        $currentUserPlugin = $this->plugin('currentUserPlugin');
456
        $currentCompany = $currentUserPlugin->getCompany();
457
        $currentUser = $currentUserPlugin->getUser();
458
 
459
        $request = $this->getRequest();
460
        $uuid = $this->params()->fromRoute('id');
461
 
462
 
463
        if (!$uuid) {
464
            $data = [
465
                'success' => false,
466
                'data' => 'ERROR_INVALID_PARAMETER'
467
            ];
468
 
469
            return new JsonModel($data);
470
        }
471
 
472
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
473
        $organizationalClimate = $surveyCampaignMapper->fetchOneByUuid($uuid);
474
 
475
        if (!$organizationalClimate) {
476
            $data = [
477
                'success' => false,
478
                'data' => 'ERROR_RECORD_NOT_FOUND'
479
            ];
480
 
481
            return new JsonModel($data);
482
        }
483
 
484
        if ($organizationalClimate->company_id != $currentCompany->id) {
485
            return new JsonModel([
486
                'success' => false,
487
                'data' => 'ERROR_UNAUTHORIZED'
488
            ]);
489
        }
490
 
491
 
492
      if ($request->isGet()) {
493
            $hydrator = new ObjectPropertyHydrator();
494
 
495
            $organizationalClimatelenguageMapper = SurveyLanguageMapper::getInstance($this->adapter);
496
            $languages  = $organizationalClimatelenguageMapper->fetchAllBySurveyId($organizationalClimate->id);
497
 
498
            $lenguage = [];
499
            foreach($languages as $language)
500
            {
501
                array_push($lenguage, $language->language_id);
502
            }
503
 
504
            $organizationalClimateSkillMapper = SurveySkillMapper::getInstance($this->adapter);
505
            $skills  = $organizationalClimateSkillMapper->fetchAllBySurveyId($organizationalClimate->id);
506
 
507
            $skilla = [];
508
            foreach($skills as $skill)
509
            {
510
                array_push($skilla, $skill->skill_id);
511
            }
512
 
513
 
514
            $skillMaster = [];
515
            foreach($skilla as $skillb)
516
            {
517
                $skillMapper = SkillMapper::getInstance($this->adapter);
518
                $skillm = $skillMapper->fetchOne($skillb);
519
                array_push($skillMaster, $skillm->uuid);
520
            }
521
 
522
            $SurveyJobDescriptionMapper = SurveyJobDescriptionMapper::getInstance($this->adapter);
523
            $SurveyJobDescriptions  = $SurveyJobDescriptionMapper->fetchAllBySurveyId($organizationalClimate->id);
524
 
525
            $SurveyJobDescriptionA = [];
526
            foreach($SurveyJobDescriptions as $SurveyJobDescription)
527
            {
528
                array_push($SurveyJobDescriptionA, $SurveyJobDescription->job_description_id);
529
            }
530
 
531
            $SurveyJobDescriptionMaster = [];
532
            foreach($SurveyJobDescriptionA as $SurveyJobDescriptionB)
533
            {
534
                $JobDescriptionMapper = JobDescriptionMapper::getInstance($this->adapter);
535
                $JobDescriptionm = $JobDescriptionMapper->fetchOne($SurveyJobDescriptionB);
536
                array_push($SurveyJobDescriptionMaster, $JobDescriptionm->uuid);
537
            }
538
 
539
            $organizationalClimateIndustryMapper = SurveyIndustryMapper::getInstance($this->adapter);
540
            $industrys = $organizationalClimateIndustryMapper->fetchAllBySurveyId($organizationalClimate->id);
541
 
542
            $industrya = [];
543
            foreach($industrys as $industry)
544
            {
545
                array_push($industrya, $industry->industry_id);
546
            }
547
 
548
 
549
            $industryMaster = [];
550
            foreach($industrya as $industryb)
551
            {
552
                $industryMapper = IndustryMapper::getInstance($this->adapter);
553
                $industrym = $industryMapper->fetchOne($industryb);
554
                array_push($industryMaster, $industrym->uuid);
555
            }
556
 
557
 
558
            $organizationalClimateLocationMapper = SurveyLocationMapper::getInstance($this->adapter);
559
            $location  = $organizationalClimateLocationMapper->fetchAllBySurveyId($organizationalClimate->id);
560
 
561
            //  return new JsonModel([
562
            //      'success' => false,
563
            //      'data' => $location
564
            //  ]);
565
 
566
 
567
 
568
            $organizationalClimateFormMapper = SurveyFormMapper::getInstance($this->adapter);
569
            $organizationalClimateForm = $organizationalClimateFormMapper->fetchOne($organizationalClimate->form_id);
570
            // return new JsonModel([
571
            //     'success' => false,
572
            //     'data' => $SurveyJobDescriptionA
573
            // ]);
574
            $data = [
575
                'success' => true,
576
                'data' => [
577
                    'name' => $organizationalClimate->name,
578
                    'form_id' => $organizationalClimateForm->uuid,
579
                    'target' => $organizationalClimate->target,
580
                    'identity' => $organizationalClimate->identity,
581
                    'since_date' => $organizationalClimate->since_date,
582
                    'last_date' => $organizationalClimate->last_date,
583
                    'status' => $organizationalClimate->status,
584
                    'lenguage_id' => $lenguage,
585
                    'skill_id' => $skillMaster,
586
                    'job_description_id' => $SurveyJobDescriptionMaster,
587
                    'industry_id' => $industryMaster,
588
                    //'location' => $location->city1,
589
                ]
590
            ];
591
 
592
            return new JsonModel($data);
593
        } else {
594
            $data = [
595
                'success' => false,
596
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
597
            ];
598
 
599
            return new JsonModel($data);
600
        }
601
 
602
        return new JsonModel($data);
603
    }
604
*/
605
    public function deleteAction()
606
    {
607
        $request = $this->getRequest();
608
        $currentUserPlugin = $this->plugin('currentUserPlugin');
609
        $currentCompany = $currentUserPlugin->getCompany();
610
        $currentUser = $currentUserPlugin->getUser();
611
 
612
        $request = $this->getRequest();
613
        $uuid = $this->params()->fromRoute('id');
614
 
615
        if (!$uuid) {
616
            $data = [
617
                'success' => false,
618
                'data' => 'ERROR_INVALID_PARAMETER'
619
            ];
620
 
621
            return new JsonModel($data);
622
        }
623
 
624
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
625
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
626
        if (!$surveyCampaign) {
627
            $data = [
628
                'success' => false,
629
                'data' => 'ERROR_RECORD_NOT_FOUND'
630
            ];
631
 
632
            return new JsonModel($data);
633
        }
634
 
635
        if ($surveyCampaign->company_id != $currentCompany->id) {
636
            return new JsonModel([
637
                'success' => false,
638
                'data' => 'ERROR_UNAUTHORIZED'
639
            ]);
640
        }
641
 
642
        if ($request->isPost()) {
643
 
644
            $result = $surveyCampaignMapper->delete($surveyCampaign->id);
645
            if ($result) {
646
                $this->logger->info('Se borro la capaña : ' . $surveyCampaign->name, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
647
 
648
                $data = [
649
                    'success' => true,
650
                    'data' => 'LABEL_RECORD_DELETED'
651
                ];
652
            } else {
653
 
654
                $data = [
655
                    'success' => false,
656
                    'data' => $surveyCampaignMapper->getError()
657
                ];
658
 
659
                return new JsonModel($data);
660
            }
661
        } else {
662
            $data = [
663
                'success' => false,
664
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
665
            ];
666
 
667
            return new JsonModel($data);
668
        }
669
 
670
        return new JsonModel($data);
671
    }
672
 
673
 
674
    public function overviewAction()
675
    {
676
        $request = $this->getRequest();
677
        $currentUserPlugin = $this->plugin('currentUserPlugin');
678
        $currentCompany = $currentUserPlugin->getCompany();
679
        $currentUser = $currentUserPlugin->getUser();
680
 
681
        $request = $this->getRequest();
682
        $uuid = $this->params()->fromRoute('id');
683
 
684
        if (!$uuid) {
685
            $data = [
686
                'success' => false,
687
                'data' => 'ERROR_INVALID_PARAMETER'
688
            ];
689
 
690
            return new JsonModel($data);
691
        }
692
 
693
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
694
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
695
 
696
        if (!$surveyCampaign) {
697
            $data = [
698
                'success' => false,
699
                'data' => 'ERROR_RECORD_NOT_FOUND'
700
            ];
701
 
702
            return new JsonModel($data);
703
        }
704
 
705
        if ($surveyCampaign->company_id != $currentCompany->id) {
706
            return new JsonModel([
707
                'success' => false,
708
                'data' => 'ERROR_UNAUTHORIZED'
709
            ]);
710
        }
711
 
712
 
713
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
714
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
715
 
716
        if (!$surveyTests) {
717
            $data = [
718
                'success' => false,
719
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
720
            ];
721
 
722
            return new JsonModel($data);
723
        }
724
 
725
 
726
        if ($request->isGet()) {
727
            $allTests = array_map(
728
                function($response) {
729
                   $content = json_decode($response->content, true);
730
                   return $content['content'];
731
                },
732
                $surveyTests,
733
            );
734
 
735
            $data = [];
736
            $countTests = count($allTests);
737
 
738
 
739
            foreach($allTests as $test)
740
            {
741
                foreach($test as $section)
742
                {
743
 
744
                    if(!isset($data[ $section['slug_section'] ] )) {
745
                        $data[ $section['slug_section'] ]  = [
746
                           'name' => $section['name'],
747
                           'text' => $section['text'],
748
                           'questions' => []
749
 
750
                        ];
751
                    }
752
 
753
 
754
                    foreach($section['questions'] as $question)
755
                    {
756
                        switch ($question['type'])
757
                        {
758
                            case 'range1to5':
759
 
760
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
761
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
762
                                        'text' => $question['text'],
763
                                        'options' => [
764
                                            'option1' => [
765
                                                'text' => '1',
766
                                                'total' => 0,
767
                                                'average' => 0,
768
                                            ],
769
                                            'option2' => [
770
                                                'text' => '2',
771
                                                'total' => 0,
772
                                                'average' => 0,
773
                                            ],
774
                                            'option3' => [
775
                                                'text' => '3',
776
                                                'total' => 0,
777
                                                'average' => 0,
778
                                            ],
779
                                            'option4' => [
780
                                                'text' => '4',
781
                                                'total' => 0,
782
                                                'average' => 0,
783
                                            ],
784
                                            'option5' => [
785
                                                'text' => '5',
786
                                                'total' => 0,
787
                                                'average' => 0,
788
                                            ]
789
                                        ]
790
                                    ];
791
 
792
                                }
793
 
794
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
795
                                $total++;
796
                                $average = ($total * 100) / $countTests;
797
 
798
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
799
 
800
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
801
 
802
                                break;
803
 
804
 
805
                            case 'multiple':
806
                            case 'simple':
807
 
808
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
809
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
810
                                        'text' => $question['text'],
811
                                        'options' => []
812
                                    ];
813
 
814
                                    foreach($question['options'] as $option)
815
                                    {
816
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
817
                                            'text' => $option['text'],
818
                                            'total' => 0,
819
                                            'average' => 0,
820
                                        ];
821
                                    }
822
 
823
                                }
824
 
825
 
826
                                foreach($question['options'] as $option)
827
                                {
828
                                    if($option['checked']) {
829
 
830
 
831
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
832
                                        $total++;
833
                                        $average = ($total * 100) / $countTests;
834
 
835
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
836
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
837
                                    }
838
                                }
839
 
840
                                break;
841
                        }
842
                    }
843
                }
844
            }
845
 
846
 
847
 
848
            $this->layout()->setTemplate('layout/layout-backend.phtml');
849
            $viewModel = new ViewModel();
850
            $viewModel->setTemplate('leaders-linked/survey-campaign/overview.phtml');
851
            $viewModel->setVariables([
852
                'data' => $data,
853
                'campaign' => $surveyCampaign,
854
            ]);
855
            return $viewModel ;
856
 
857
        } else {
858
            $data = [
859
                'success' => false,
860
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
861
            ];
862
 
863
            return new JsonModel($data);
864
        }
865
 
866
    }
867
 
868
 
869
    public function pdfAction()
870
    {
871
        $request = $this->getRequest();
872
        $currentUserPlugin = $this->plugin('currentUserPlugin');
873
        $currentCompany = $currentUserPlugin->getCompany();
874
        $currentUser = $currentUserPlugin->getUser();
875
 
876
        $request = $this->getRequest();
877
        $uuid = $this->params()->fromRoute('id');
878
 
879
        if (!$uuid) {
880
            $data = [
881
                'success' => false,
882
                'data' => 'ERROR_INVALID_PARAMETER'
883
            ];
884
 
885
            return new JsonModel($data);
886
        }
887
 
888
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
889
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
890
 
891
        if (!$surveyCampaign) {
892
            $data = [
893
                'success' => false,
894
                'data' => 'ERROR_RECORD_NOT_FOUND'
895
            ];
896
 
897
            return new JsonModel($data);
898
        }
899
 
900
        if ($surveyCampaign->company_id != $currentCompany->id) {
901
            return new JsonModel([
902
                'success' => false,
903
                'data' => 'ERROR_UNAUTHORIZED'
904
            ]);
905
        }
906
 
907
 
908
 
909
 
910
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
911
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
912
 
913
        if (!$surveyTests) {
914
            $data = [
915
                'success' => false,
916
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
917
            ];
918
 
919
            return new JsonModel($data);
920
        }
921
 
922
 
923
        if ($request->isGet()) {
924
            $allTests = array_map(
925
                function($response) {
926
                    $content = json_decode($response->content, true);
927
                    return $content['content'];
928
                },
929
                $surveyTests,
930
                );
931
 
932
            $data = [];
933
            $countTests = count($allTests);
934
 
935
 
936
            foreach($allTests as $test)
937
            {
938
                foreach($test as $section)
939
                {
940
 
941
                    if(!isset($data[ $section['slug_section'] ] )) {
942
                        $data[ $section['slug_section'] ]  = [
943
                            'name' => $section['name'],
944
                            'text' => $section['text'],
945
                            'questions' => []
946
 
947
                        ];
948
                    }
949
 
950
 
951
                    foreach($section['questions'] as $question)
952
                    {
953
                        switch ($question['type'])
954
                        {
955
                            case 'range1to5':
956
 
957
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
958
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
959
                                        'text' => $question['text'],
960
                                        'options' => [
961
                                            'option1' => [
962
                                                'text' => '1',
963
                                                'total' => 0,
964
                                                'average' => 0,
965
                                            ],
966
                                            'option2' => [
967
                                                'text' => '2',
968
                                                'total' => 0,
969
                                                'average' => 0,
970
                                            ],
971
                                            'option3' => [
972
                                                'text' => '3',
973
                                                'total' => 0,
974
                                                'average' => 0,
975
                                            ],
976
                                            'option4' => [
977
                                                'text' => '4',
978
                                                'total' => 0,
979
                                                'average' => 0,
980
                                            ],
981
                                            'option5' => [
982
                                                'text' => '5',
983
                                                'total' => 0,
984
                                                'average' => 0,
985
                                            ]
986
                                        ]
987
                                    ];
988
 
989
                                }
990
 
991
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
992
                                $total++;
993
                                $average = ($total * 100) / $countTests;
994
 
995
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
996
 
997
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
998
 
999
                                break;
1000
 
1001
 
1002
                            case 'multiple':
1003
                            case 'simple':
1004
 
1005
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1006
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1007
                                        'text' => $question['text'],
1008
                                        'options' => []
1009
                                    ];
1010
 
1011
                                    foreach($question['options'] as $option)
1012
                                    {
1013
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
1014
                                            'text' => $option['text'],
1015
                                            'total' => 0,
1016
                                            'average' => 0,
1017
                                        ];
1018
                                    }
1019
 
1020
                                }
1021
 
1022
 
1023
                                foreach($question['options'] as $option)
1024
                                {
1025
                                    if($option['checked']) {
1026
 
1027
 
1028
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
1029
                                        $total++;
1030
                                        $average = ($total * 100) / $countTests;
1031
 
1032
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
1033
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
1034
                                    }
1035
                                }
1036
 
1037
                                break;
1038
                        }
1039
                    }
1040
                }
1041
            }
1042
 
1043
 
1044
            $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $currentCompany->uuid;
1045
 
1046
            $header = $currentCompany->header ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->header : '';
1047
            if(empty($header) || !file_exists($header)) {
1048
                $header = $this->config['leaderslinked.images_default.company_pdf_header'];
1049
            }
1050
 
1051
            $footer = $currentCompany->footer ? $target_path . DIRECTORY_SEPARATOR . $currentCompany->footer : '';
1052
            if(empty($footer) || !file_exists($footer)) {
1053
                $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
1054
            }
1055
 
1056
            $content = json_decode($surveyCampaign->content, true);
1057
 
1058
 
1059
            //Generate New PDF
1060
            $pdf = new SurveyReport();
1061
            $pdf->header = $header;
1062
            $pdf->footer = $footer;
1063
            $pdf->campaignName = $surveyCampaign->name;
1064
            $pdf->formName = $content['name'];
1065
            $pdf->formText = $content['text'];
1066
            $pdf->SetTitle(Functions::utf8_decode($surveyCampaign->name));
1067
 
1068
            $pdf->translator = $this->translator;
1069
 
1070
 
1071
 
1072
            $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;
1073
            if(!file_exists($target_path)) {
1074
                mkdir($target_path, 0755, true);
1075
            } else {
1076
                Functions::deleteFiles($target_path);
1077
            }
1078
 
1079
 
1080
            $pdf->AliasNbPages();
1081
 
1082
 
1083
            foreach($data as $slug_section => $section)
1084
            {
1085
                $pdf->AddPage();
1086
                $pdf->addSection($section['name'], $section['text']);
1087
 
1088
 
1089
                $countQuestion = 0;
1090
                foreach ($section['questions'] as $slug_question => $question)
1091
                {
1092
                    if($countQuestion == 2) {
1093
                        $pdf->AddPage();
1094
                        $countQuestion = 0;
1095
                    }
1096
 
1097
                    $labels = [];
1098
                    $values = [];
1099
 
1100
                    $existEmptyOption = false;
1101
                    foreach($question['options'] as $slug_option => $option)
1102
                    {
1103
                        if(empty($option['average'])) {
1104
                            $existEmptyOption = true;
1105
                            continue;
1106
                        }
1107
 
1108
                        array_push($labels, $option['text']);
1109
                        array_push($values, $option['average']);
1110
                    }
1111
 
1112
                    if($existEmptyOption) {
1113
                        array_push($labels, $this->translator->translate('LABEL_SURVEY_OTHER_EMPTY_OPTIONS'));
1114
                        array_push($values, 0);
1115
                    }
1116
 
1117
                    $filename = $target_path . DIRECTORY_SEPARATOR .  $slug_question . '.png';
1118
                    $pdf->Cell(0,10, $question['text'],0,1);
1119
                    $pdf->pieChart($labels, $values, '', $filename);
1120
                    $pdf->SetFont('Arial', '', 12);
1121
                    $pdf->Image($filename, 10, $pdf->getY(), 190);
1122
                    $pdf->setY($pdf->getY() + 90);
1123
 
1124
                    $countQuestion++;
1125
 
1126
                }
1127
 
1128
 
1129
            }
1130
 
1131
 
1132
 
1133
            $content = $pdf->Output('S');
1134
 
1135
            $response = new Response();
1136
            $response->setStatusCode(200);
1137
            $response->setContent($content);
1138
 
1139
 
1140
 
1141
            $headers = $response->getHeaders();
1142
            $headers->clearHeaders();
1143
 
1144
            $headers->addHeaderLine('Content-Description: File Transfer');
1145
            $headers->addHeaderLine('Content-Type: application/pdf');
1146
            //$headers->addHeaderLine('Content-Disposition: attachment; filename=' . $filename);
1147
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
1148
            $headers->addHeaderLine('Expires: 0');
1149
            $headers->addHeaderLine('Cache-Control: must-revalidate');
1150
            $headers->addHeaderLine('Pragma: public');
1151
            return $response;
1152
 
1153
 
1154
        } else {
1155
            $data = [
1156
                'success' => false,
1157
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1158
            ];
1159
 
1160
            return new JsonModel($data);
1161
        }
1162
 
1163
    }
1164
 
1165
    public function excelAction()
1166
    {
1167
        $request = $this->getRequest();
1168
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1169
        $currentCompany = $currentUserPlugin->getCompany();
1170
        $currentUser = $currentUserPlugin->getUser();
1171
 
1172
        $request = $this->getRequest();
1173
        $uuid = $this->params()->fromRoute('id');
1174
 
1175
        if (!$uuid) {
1176
            $data = [
1177
                'success' => false,
1178
                'data' => 'ERROR_INVALID_PARAMETER'
1179
            ];
1180
 
1181
            return new JsonModel($data);
1182
        }
1183
 
1184
        $surveyCampaignMapper = SurveyCampaignMapper::getInstance($this->adapter);
1185
        $surveyCampaign = $surveyCampaignMapper->fetchOneByUuid($uuid);
1186
 
1187
        if (!$surveyCampaign) {
1188
            $data = [
1189
                'success' => false,
1190
                'data' => 'ERROR_RECORD_NOT_FOUND'
1191
            ];
1192
 
1193
            return new JsonModel($data);
1194
        }
1195
 
1196
        if ($surveyCampaign->company_id != $currentCompany->id) {
1197
            return new JsonModel([
1198
                'success' => false,
1199
                'data' => 'ERROR_UNAUTHORIZED'
1200
            ]);
1201
        }
1202
 
1203
 
1204
 
1205
 
1206
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
1207
        $surveyTests = $surveyTestMapper->fetchAllCompletedByCompanyIdAndCampaignId($surveyCampaign->company_id, $surveyCampaign->id);
1208
 
1209
        if (!$surveyTests) {
1210
            $data = [
1211
                'success' => false,
1212
                'data' => 'ERROR_SURVEY_THERE_ARE_NOT_ANSWERS'
1213
            ];
1214
 
1215
            return new JsonModel($data);
1216
        }
1217
 
1218
 
1219
        if ($request->isGet()) {
1220
            $allTests = array_map(
1221
                function($response) {
1222
                    $content = json_decode($response->content, true);
1223
                    return $content['content'];
1224
                },
1225
                $surveyTests,
1226
                );
1227
 
1228
            $data = [];
1229
            $countTests = count($allTests);
1230
 
1231
 
1232
            foreach($allTests as $test)
1233
            {
1234
                foreach($test as $section)
1235
                {
1236
 
1237
                    if(!isset($data[ $section['slug_section'] ] )) {
1238
                        $data[ $section['slug_section'] ]  = [
1239
                            'name' => $section['name'],
1240
                            'text' => $section['text'],
1241
                            'questions' => []
1242
 
1243
                        ];
1244
                    }
1245
 
1246
 
1247
                    foreach($section['questions'] as $question)
1248
                    {
1249
                        switch ($question['type'])
1250
                        {
1251
                            case 'range1to5':
1252
 
1253
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1254
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1255
                                        'text' => $question['text'],
1256
                                        'options' => [
1257
                                            'option1' => [
1258
                                                'text' => '1',
1259
                                                'total' => 0,
1260
                                                'average' => 0,
1261
                                            ],
1262
                                            'option2' => [
1263
                                                'text' => '2',
1264
                                                'total' => 0,
1265
                                                'average' => 0,
1266
                                            ],
1267
                                            'option3' => [
1268
                                                'text' => '3',
1269
                                                'total' => 0,
1270
                                                'average' => 0,
1271
                                            ],
1272
                                            'option4' => [
1273
                                                'text' => '4',
1274
                                                'total' => 0,
1275
                                                'average' => 0,
1276
                                            ],
1277
                                            'option5' => [
1278
                                                'text' => '5',
1279
                                                'total' => 0,
1280
                                                'average' => 0,
1281
                                            ]
1282
                                        ]
1283
                                    ];
1284
 
1285
                                }
1286
 
1287
                                $total = $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'];
1288
                                $total++;
1289
                                $average = ($total * 100) / $countTests;
1290
 
1291
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['total'] = $total;
1292
 
1293
                                $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ]['options']['option' . $question['answer']]['average'] = $average;
1294
 
1295
                                break;
1296
 
1297
 
1298
                            case 'multiple':
1299
                            case 'simple':
1300
 
1301
                                if(!isset($data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] )) {
1302
                                    $data[ $question['slug_section'] ]['questions'][ $question['slug_question'] ] = [
1303
                                        'text' => $question['text'],
1304
                                        'options' => []
1305
                                    ];
1306
 
1307
                                    foreach($question['options'] as $option)
1308
                                    {
1309
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ] = [
1310
                                            'text' => $option['text'],
1311
                                            'total' => 0,
1312
                                            'average' => 0,
1313
                                        ];
1314
                                    }
1315
 
1316
                                }
1317
 
1318
 
1319
                                foreach($question['options'] as $option)
1320
                                {
1321
                                    if($option['checked']) {
1322
 
1323
 
1324
                                        $total = $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'];
1325
                                        $total++;
1326
                                        $average = ($total * 100) / $countTests;
1327
 
1328
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['total'] = $total;
1329
                                        $data[ $option['slug_section'] ]['questions'][ $option['slug_question'] ]['options'][ $option['slug_option'] ]['average'] = $average;
1330
                                    }
1331
                                }
1332
 
1333
                                break;
1334
                        }
1335
                    }
1336
                }
1337
            }
1338
 
1339
            $spreadsheet = new Spreadsheet();
1340
            $sheet = $spreadsheet->getActiveSheet();
1341
 
1342
 
1343
            $row = 1;
1344
            foreach($data as $slug_section => $section)
1345
            {
1346
                $sheet->setCellValue('A' . $row , $section['name']);
1347
                $row++;
1348
                $sheet->setCellValue('A' . $row , $section['text']);
1349
 
1350
                $row++;
1351
 
1352
 
1353
 
1354
                foreach ($section['questions'] as $slug_question => $question)
1355
                {
1356
                    $sheet->setCellValue('B' . $row , $question['text']);
1357
                    $row++;
1358
 
1359
                    $sheet->setCellValue('C' . $row , $this->translator->translate('LABEL_OPTION'));
1360
                    $sheet->setCellValue('D' . $row , $this->translator->translate('LABEL_TOTAL'));
1361
                    $sheet->setCellValue('E' . $row , $this->translator->translate('LABEL_AVERAGE') . ' %');
1362
                    $row++;
1363
 
1364
 
1365
 
1366
                    foreach($question['options'] as $slug_option => $option)
1367
                    {
1368
                        $sheet->setCellValue('C' . $row , $option['text']);
1369
                        $sheet->setCellValue('D' . $row , $option['total']);
1370
                        $sheet->setCellValue('E' . $row , round($option['average'], 2));
1371
                        $row++;
1372
                    }
1373
                    $row++;
1374
                }
1375
                $row++;
1376
            }
1377
 
1378
            $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $surveyCampaign->uuid;
1379
            if(!file_exists($target_path)) {
1380
                mkdir($target_path, 0755, true);
1381
            }
1382
 
1383
 
1384
            $filename = Functions::normalizeStringFilename($surveyCampaign->uuid . '.xls');
1385
            $fullFilename = $target_path  . DIRECTORY_SEPARATOR . $filename;
1386
 
1387
            $writer = new Xlsx($spreadsheet);
1388
            $writer->save($fullFilename);
1389
 
1390
            $content = file_get_contents($fullFilename);
1391
 
1392
 
1393
            $response = new Response();
1394
            $response->setStatusCode(200);
1395
            $response->setContent($content);
1396
 
1397
 
1398
 
1399
            $headers = $response->getHeaders();
1400
            $headers->clearHeaders();
1401
 
1402
            $headers->addHeaderLine('Content-Description: File Transfer');
1403
            $headers->addHeaderLine('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
1404
            $headers->addHeaderLine('Content-Disposition: attachment; filename='. $filename);
1405
            $headers->addHeaderLine('Content-Transfer-Encoding: binary');
1406
            $headers->addHeaderLine('Expires: 0');
1407
            $headers->addHeaderLine('Cache-Control: must-revalidate');
1408
            $headers->addHeaderLine('Pragma: public');
1409
            return $response;
1410
 
1411
 
1412
        } else {
1413
            $data = [
1414
                'success' => false,
1415
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
1416
            ];
1417
 
1418
            return new JsonModel($data);
1419
        }
1420
 
1421
    }
1422
 
1423
}