Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5866 eleazar 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
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\Mapper\QueryMapper;
14
use Laminas\Db\Sql\Select;
15
use LeadersLinked\Library\Functions;
16
use LeadersLinked\Mapper\SurveyTestMapper;
17
use LeadersLinked\Mapper\SurveyMapper;
18
use LeadersLinked\Mapper\SurveyFormMapper;
19
use LeadersLinked\Form\SurveyTestForm;
20
use LeadersLinked\Model\SurveyTest;
21
use LeadersLinked\Mapper\UserMapper;
22
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
23
use LeadersLinked\Library\SurveyReport;
7080 eleazar 24
use PhpOffice\PhpSpreadsheet\Spreadsheet;
25
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
26
use PhpOffice\PhpSpreadsheet\IOFactory;
5866 eleazar 27
 
28
class SurveyReportController extends AbstractActionController {
29
 
30
    /**
31
     *
32
     * @var AdapterInterface
33
     */
34
    private $adapter;
35
 
36
    /**
37
     *
38
     * @var AbstractAdapter
39
     */
40
    private $cache;
41
 
42
    /**
43
     *
44
     * @var  LoggerInterface
45
     */
46
    private $logger;
47
 
48
    /**
49
     *
50
     * @var array
51
     */
52
    private $config;
53
 
54
    /**
55
     *
56
     * @param AdapterInterface $adapter
57
     * @param AbstractAdapter $cache
58
     * @param LoggerInterface $logger
59
     * @param array $config
60
     */
61
    public function __construct($adapter, $cache, $logger, $config) {
62
        $this->adapter = $adapter;
63
        $this->cache = $cache;
64
        $this->logger = $logger;
65
        $this->config = $config;
66
    }
67
 
68
    public function indexAction() {
5911 eleazar 69
 
5866 eleazar 70
        $request = $this->getRequest();
71
        $currentUserPlugin = $this->plugin('currentUserPlugin');
72
        $currentCompany = $currentUserPlugin->getCompany();
73
        $currentUser = $currentUserPlugin->getUser();
74
 
6424 eleazar 75
        try{
5866 eleazar 76
        $request = $this->getRequest();
77
        if ($request->isGet()) {
78
 
79
            $headers = $request->getHeaders();
80
 
81
            $isJson = false;
82
            if ($headers->has('Accept')) {
83
                $accept = $headers->get('Accept');
84
 
85
                $prioritized = $accept->getPrioritized();
86
 
87
                foreach ($prioritized as $key => $value) {
88
                    $raw = trim($value->getRaw());
89
 
90
                    if (!$isJson) {
91
                        $isJson = strpos($raw, 'json');
92
                    }
93
                }
94
            }
95
 
96
            if ($isJson) {
5911 eleazar 97
 
5866 eleazar 98
                $search = $this->params()->fromQuery('search', []);
99
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
100
 
101
                $page = intval($this->params()->fromQuery('start', 1), 10);
102
                $records_x_page = intval($this->params()->fromQuery('length', 10), 10);
103
                $order = $this->params()->fromQuery('order', []);
104
                $order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
105
                $order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(filter_var($order[0]['dir'], FILTER_SANITIZE_STRING));
106
 
6411 eleazar 107
                $fields = ['name'];
6416 eleazar 108
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'name';
5866 eleazar 109
 
110
                if (!in_array($order_direction, ['ASC', 'DESC'])) {
111
                    $order_direction = 'ASC';
112
                }
113
 
6411 eleazar 114
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
6412 eleazar 115
                $paginator = $surveyMapper->fetchAllDataTableByCompanyId($currentCompany->id, $search, $page, $records_x_page, $order_field, $order_direction);
6423 eleazar 116
 
117
                $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
118
 
5866 eleazar 119
                $items = [];
120
                $records = $paginator->getCurrentItems();
6866 eleazar 121
 
5866 eleazar 122
                foreach ($records as $record) {
6423 eleazar 123
                    $surveyForm = $surveyFormMapper->fetchOne($record->form_id);
6451 eleazar 124
                    $params = [
125
 
126
                        'id' => $record->uuid,
127
                    ];
5866 eleazar 128
                    $item = [
14215 kerby 129
                        'id' => $record->id,
6426 eleazar 130
                        'name' => $record->name,
6211 eleazar 131
                        'form' => $surveyForm->name,
6427 eleazar 132
                        'status' => $record->status,
5866 eleazar 133
                        'actions' => [
6496 eleazar 134
                            'link_report_all' => $this->url()->fromRoute('survey/report/all', ['survey_id' => $record->uuid]),
15150 efrain 135
                            'link_report_excel' => $this->url()->fromRoute('survey/report/excel', ['survey_id' => $record->uuid]),
6496 eleazar 136
                            'link_overview' => $this->url()->fromRoute('survey/report/overview', ['survey_id' => $record->uuid])
5866 eleazar 137
                        ]
138
                    ];
139
 
140
                    array_push($items, $item);
141
                }
142
 
143
                return new JsonModel([
144
                    'success' => true,
145
                    'data' => [
146
                        'items' => $items,
147
                        'total' => $paginator->getTotalItemCount(),
148
                    ]
149
                ]);
5911 eleazar 150
            } else {
151
                $surveyMapper = SurveyMapper::getInstance($this->adapter);
152
                $survies = $surveyMapper->fetchAllByCompanyId($currentCompany->id);
153
 
5866 eleazar 154
                $form = new SurveyTestForm($this->adapter, $currentCompany->id);
155
 
156
                $this->layout()->setTemplate('layout/layout-backend');
157
                $viewModel = new ViewModel();
5933 eleazar 158
                $viewModel->setTemplate('leaders-linked/survey-report/index.phtml');
5866 eleazar 159
                $viewModel->setVariables([
5911 eleazar 160
                    'form'      => $form,
161
                    'survies' => $survies
5866 eleazar 162
                ]);
163
                return $viewModel;
164
            }
165
        } else {
166
            return new JsonModel([
167
                'success' => false,
168
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
169
            ]);
170
 
6424 eleazar 171
        }
172
        } catch (\Throwable $e) {
173
            $e->getMessage();
174
            return new JsonModel([
175
                'success' => false,
176
                'data' => $e
177
            ]);
5866 eleazar 178
        }
6212 eleazar 179
 
5866 eleazar 180
    }
5911 eleazar 181
 
6496 eleazar 182
    public function overviewAction(){
183
        $request = $this->getRequest();
184
        $currentUserPlugin = $this->plugin('currentUserPlugin');
185
        $currentCompany = $currentUserPlugin->getCompany();
186
        $currentUser = $currentUserPlugin->getUser();
187
 
188
        $request = $this->getRequest();
189
        $uuid = $this->params()->fromRoute('survey_id');
190
 
191
        if (!$uuid) {
192
            $data = [
193
                'success' => false,
194
                'data' => 'ERROR_INVALID_PARAMETER'
195
            ];
196
 
197
            return new JsonModel($data);
198
        }
199
 
200
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
201
        $survey = $surveyMapper->fetchOneByUuid($uuid);
202
 
203
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
204
        $survey = $surveyMapper->fetchOneByUuid($uuid);
205
 
206
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
207
        $surveyTests = $surveyTestMapper->fetchAllBySurveyId($survey->id);
208
 
8355 eleazar 209
        if (!$surveyTests) {
210
            $data = [
211
                'success' => false,
212
                'data' => 'No hay respuestas en esta encuesta'
213
            ];
214
 
215
            return new JsonModel($data);
216
        }
217
 
6496 eleazar 218
        $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
219
        $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
220
 
221
        $sections = json_decode($surveyForm->content, true);
222
 
223
        $allTests = array_map(
224
            function($response) {
225
                return json_decode($response->content, true);
226
            },
227
            $surveyTests,
228
        );
229
 
230
        $averages = [];
231
 
232
        foreach($sections as $section) {
233
            foreach($section['questions'] as $question) {
234
                switch ($question['type']) {
235
                    case 'multiple':
236
                        $totals = [];
237
 
238
                        foreach($question['options'] as $option) {
239
                            $totals[$option['slug_option']] = 0;
240
                        }
241
 
242
                        foreach($question['options'] as $option) {
243
                            $totals[$option['slug_option']] = count(array_filter(
244
                                $allTests,
245
                                function ($test) use($option, $question) {
246
                                    return in_array($option['slug_option'], $test[$question['slug_question']]);
247
                                }
248
                            ));
249
                        }
250
 
251
                        $averages[$question['slug_question']] = $totals;
252
 
253
                        break;
254
 
255
                    case 'simple':
256
                        $totals = [];
257
 
258
                        foreach($question['options'] as $option) {
259
                            $totals[$option['slug_option']] = 0;
260
                        }
261
 
262
                        foreach ($allTests as $test) {
263
                            $totals[$test[$question['slug_question']]]++;
264
                        }
265
 
266
                        foreach($totals as $slug => $amount) {
267
                            $totals[$slug] = ($amount / count($allTests)) * 100;
268
                        }
269
 
270
                        $averages[$question['slug_question']] = $totals;
271
                    break;
272
 
273
                }
274
            }
275
        }
276
 
277
        $this->layout()->setTemplate('layout/layout-backend.phtml');
278
        $viewModel = new ViewModel();
279
        $viewModel->setTemplate('leaders-linked/survey-report/overview.phtml');
280
        $viewModel->setVariables([
6506 eleazar 281
            'sections' => $sections,
6524 eleazar 282
            'averages' => $averages,
6496 eleazar 283
        ]);
284
        return $viewModel ;
285
 
286
    }
287
 
6447 eleazar 288
    public function allAction() {
5930 eleazar 289
        $request = $this->getRequest();
290
        $currentUserPlugin = $this->plugin('currentUserPlugin');
291
        $currentCompany = $currentUserPlugin->getCompany();
292
        $currentUser = $currentUserPlugin->getUser();
293
 
294
        $request = $this->getRequest();
6043 eleazar 295
        $uuid = $this->params()->fromRoute('survey_id');
5961 eleazar 296
 
5930 eleazar 297
 
298
 
299
        if (!$uuid) {
300
            $data = [
301
                'success' => false,
302
                'data' => 'ERROR_INVALID_PARAMETER'
303
            ];
304
 
305
            return new JsonModel($data);
306
        }
307
 
6039 eleazar 308
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
309
        $survey = $surveyMapper->fetchOneByUuid($uuid);
6044 eleazar 310
 
5965 eleazar 311
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
6052 eleazar 312
        $surveyTests = $surveyTestMapper->fetchAllBySurveyId($survey->id);
5966 eleazar 313
 
6052 eleazar 314
        if (!$surveyTests) {
5930 eleazar 315
            $data = [
316
                'success' => false,
5961 eleazar 317
                'data' => 'ERROR_RECORD_NOT_FOUND'
5930 eleazar 318
            ];
319
 
320
            return new JsonModel($data);
321
        }
322
 
5965 eleazar 323
 
5930 eleazar 324
        if ($request->isGet()) {
15150 efrain 325
 
5930 eleazar 326
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
5965 eleazar 327
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
5930 eleazar 328
 
329
            if ($surveyForm) {
330
 
15150 efrain 331
                return $this->renderPDF($currentCompany, $surveyForm, $surveyTests, $survey);
5930 eleazar 332
            } else {
333
 
334
                $data = [
335
                    'success' => false,
336
                    'data' => 'ERROR_METHOD_NOT_ALLOWED'
337
                ];
338
 
339
                return new JsonModel($data);
340
            }
341
        } else {
342
            $data = [
343
                'success' => false,
344
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
345
            ];
346
 
347
            return new JsonModel($data);
348
        }
349
 
350
        return new JsonModel($data);
351
    }
352
 
353
 
15150 efrain 354
    public function renderPDF($company, $surveyForm, $surveyTests, $survey) {
5930 eleazar 355
 
15150 efrain 356
 
357
 
5930 eleazar 358
 
359
 
15150 efrain 360
        $target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
361
 
362
        $header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
363
        if(empty($header) || !file_exists($header)) {
364
            $header = $this->config['leaderslinked.images_default.company_pdf_header'];
365
        }
366
 
367
        $footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
368
        if(empty($footer) || !file_exists($footer)) {
369
            $footer = $this->config['leaderslinked.images_default.company_pdf_footer'];
370
        }
371
 
372
 
373
        //Generate New PDF
374
        $pdf = new SurveyReport();
375
 
376
 
377
        $pdf->header = $header;
378
        $pdf->footer = $footer;
379
 
380
 
381
 
382
        $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $survey->uuid;
383
        if(!file_exists($target_path)) {
384
            mkdir($target_path, 0755, true);
385
        } else {
5930 eleazar 386
            Functions::deleteFiles($target_path);
387
        }
388
        // Set Data
15150 efrain 389
        $headerFormName = $this->cleanStringToPdf($surveyForm->name);
390
        $headerSurveyName = $this->cleanStringToPdf('Informe de Encuesta: ' . trim($survey->name) . ' al ' . date("m-d-Y H:i:s", strtotime($survey->added_on)));
6052 eleazar 391
        $sections = json_decode($surveyForm->content, true);
392
 
6053 eleazar 393
        $allTests = array_map(
6052 eleazar 394
            function($response) {
395
                return json_decode($response->content, true);
396
            },
397
            $surveyTests,
398
        );
15150 efrain 399
 
400
 
6052 eleazar 401
 
15150 efrain 402
 
6052 eleazar 403
        $averages = [];
404
 
405
        foreach($sections as $section) {
406
            foreach($section['questions'] as $question) {
407
                switch ($question['type']) {
6054 eleazar 408
                    case 'multiple':
6056 eleazar 409
                        $totals = [];
410
 
411
                        foreach($question['options'] as $option) {
412
                            $totals[$option['slug_option']] = 0;
413
                        }
414
 
415
                        foreach($question['options'] as $option) {
6057 eleazar 416
                            $totals[$option['slug_option']] = count(array_filter(
6056 eleazar 417
                                $allTests,
6452 eleazar 418
                                function ($test) use($option, $question) {
6450 eleazar 419
                                    return in_array($option['slug_option'], $test[$question['slug_question']]);
6056 eleazar 420
                                }
421
                            ));
422
                        }
6084 eleazar 423
 
6056 eleazar 424
                        $averages[$question['slug_question']] = $totals;
425
 
426
                        break;
6449 eleazar 427
 
6052 eleazar 428
                    case 'simple':
6053 eleazar 429
                        $totals = [];
6052 eleazar 430
 
431
                        foreach($question['options'] as $option) {
6053 eleazar 432
                            $totals[$option['slug_option']] = 0;
6052 eleazar 433
                        }
434
 
6053 eleazar 435
                        foreach ($allTests as $test) {
436
                            $totals[$test[$question['slug_question']]]++;
6052 eleazar 437
                        }
438
 
6053 eleazar 439
                        foreach($totals as $slug => $amount) {
440
                            $totals[$slug] = ($amount / count($allTests)) * 100;
6052 eleazar 441
                        }
442
 
6084 eleazar 443
                        $averages[$question['slug_question']] = $totals;
6052 eleazar 444
                    break;
6071 eleazar 445
 
6052 eleazar 446
                }
447
            }
448
        }
449
 
5930 eleazar 450
 
451
        $pdf->AliasNbPages();
452
        $pdf->AddPage();
453
 
454
        // Set header secundary
6071 eleazar 455
        $pdf->customHeader($headerFormName, $headerSurveyName);
5930 eleazar 456
 
6609 eleazar 457
        $countSection = 0;
458
 
15150 efrain 459
        for ($i = 0, $max = count($sections);  $i < $max ; $i++) {
460
            $headerUserName = '';
461
 
6615 eleazar 462
            if ($countSection > 1) {
463
                $countSection = 0;
464
                $pdf->AddPage();
465
                $pdf->customHeader($headerFormName, $headerUserName);
466
            }
467
            $section = $sections[$i];
6084 eleazar 468
 
469
            foreach ($section['questions'] as $question) {
6615 eleazar 470
 
6614 eleazar 471
                    switch ($question['type']) {
472
                        case 'simple':
473
                            $labels = [];
474
                            $values = [];
6039 eleazar 475
 
6614 eleazar 476
                            foreach($question['options'] as $option) {
15150 efrain 477
                                $label = $this->cleanStringToPdf($option['text']) . "\n(%.1f%%)";
478
                                $value = $averages[$question['slug_question']][$option['slug_option']];
479
                                if(empty($value)) {
480
                                    continue;
481
                                }
482
 
483
                                array_push($labels, $label);
484
 
485
                                array_push($values, $value);
6614 eleazar 486
                            }
487
 
488
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
15150 efrain 489
                            $pdf->Cell(0,10, $this->cleanStringToPdf($question['text'], false),0,1);
6914 eleazar 490
                            $pdf->pieChart($labels, $values, '', $filename);
6614 eleazar 491
                            $pdf->SetFont('Arial', '', 12);
15150 efrain 492
                            $pdf->Image($filename, 10, $pdf->getY(), 190);
6614 eleazar 493
                            $pdf->setY($pdf->getY() + 90);
6912 eleazar 494
 
6614 eleazar 495
 
496
                            break;
497
 
498
                        case 'multiple':
499
                            $labels = [];
500
                            $values = [];
501
 
15150 efrain 502
                            foreach($question['options'] as $option)
503
                            {
504
                                $label = $this->cleanStringToPdf($option['text']) . "\n(%.1f%%)";
505
                                $value = $averages[$question['slug_question']][$option['slug_option']];
506
                                if(empty($value)) {
507
                                    continue;
508
                                }
509
 
510
                                array_push($labels, $label);
6614 eleazar 511
 
15150 efrain 512
                                array_push($values, $value);
6614 eleazar 513
                            }
514
 
515
                            $filename = $target_path . DIRECTORY_SEPARATOR .  $question['slug_question'] . '.png';
15150 efrain 516
                            $pdf->Cell(0,10,$this->cleanStringToPdf($question['text'], false),0,1);
517
                            $pdf->pieChart($labels, $values, '', $filename);
6614 eleazar 518
                            $pdf->SetFont('Arial', '', 12);
15150 efrain 519
                            $pdf->Image($filename, 10, $pdf->getY(), 190);
6614 eleazar 520
                            $pdf->setY($pdf->getY() + (count($values) * 13) + 10);
521
 
522
                            break;
523
                    }
6615 eleazar 524
                $countSection++;
6084 eleazar 525
            }
6608 eleazar 526
 
6615 eleazar 527
 
5930 eleazar 528
        }
529
 
7021 eleazar 530
        return $pdf->Output();
531
    }
532
 
15150 efrain 533
    public function excelAction() {
7021 eleazar 534
        $request = $this->getRequest();
535
        $currentUserPlugin = $this->plugin('currentUserPlugin');
536
        $currentCompany = $currentUserPlugin->getCompany();
537
        $currentUser = $currentUserPlugin->getUser();
538
 
539
        $request = $this->getRequest();
540
        $uuid = $this->params()->fromRoute('survey_id');
541
 
542
 
543
 
544
        if (!$uuid) {
545
            $data = [
546
                'success' => false,
547
                'data' => 'ERROR_INVALID_PARAMETER'
548
            ];
549
 
550
            return new JsonModel($data);
551
        }
552
 
553
        $surveyMapper = SurveyMapper::getInstance($this->adapter);
554
        $survey = $surveyMapper->fetchOneByUuid($uuid);
555
 
556
        $surveyTestMapper = SurveyTestMapper::getInstance($this->adapter);
557
        $surveyTests = $surveyTestMapper->fetchAllBySurveyId($survey->id);
558
 
559
        if (!$surveyTests) {
560
            $data = [
561
                'success' => false,
562
                'data' => 'ERROR_RECORD_NOT_FOUND'
563
            ];
564
 
565
            return new JsonModel($data);
566
        }
567
 
568
 
569
        if ($request->isGet()) {
15150 efrain 570
 
7021 eleazar 571
            $surveyFormMapper = SurveyFormMapper::getInstance($this->adapter);
572
            $surveyForm = $surveyFormMapper->fetchOne($survey->form_id);
573
 
574
            if ($surveyForm) {
575
 
15150 efrain 576
                $filename = $this->excelRender($surveyForm, $surveyTests, $survey);
577
                $content = file_get_contents($filename);
578
 
579
 
580
                $response = $this->getResponse();
581
                $headers = $response->getHeaders();
582
                $headers->clearHeaders();
583
 
584
 
585
                $headers->addHeaderLine('Content-Description: File Transfer');
586
                $headers->addHeaderLine('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
587
                $headers->addHeaderLine('Content-Disposition: attachment; filename='.$survey->uuid.'.xls');
588
                $headers->addHeaderLine('Content-Transfer-Encoding: binary');
589
                $headers->addHeaderLine('Expires: 0');
590
                $headers->addHeaderLine('Cache-Control: must-revalidate');
591
                $headers->addHeaderLine('Pragma: public');
592
 
593
 
594
                $response->setStatusCode(200);
595
                $response->setContent($content);
596
 
597
 
598
                return $response;
7021 eleazar 599
            } else {
600
 
601
                $data = [
602
                    'success' => false,
15150 efrain 603
                    'data' => 'ERROR_SURVEY_NOT_FOUND'
7021 eleazar 604
                ];
605
 
606
                return new JsonModel($data);
607
            }
608
        } else {
609
            $data = [
610
                'success' => false,
611
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
612
            ];
613
 
614
            return new JsonModel($data);
615
        }
616
 
617
        return new JsonModel($data);
618
    }
619
 
620
 
15150 efrain 621
    public function excelRender($surveyForm, $surveyTests, $survey)
7021 eleazar 622
    {
7026 eleazar 623
        $sections = json_decode($surveyForm->content, true);
7020 eleazar 624
 
7088 eleazar 625
        $spreadsheet = new Spreadsheet();
626
        $sheet = $spreadsheet->getActiveSheet();
627
 
7025 eleazar 628
        $allTests = array_map(
629
            function($response) {
630
                return json_decode($response->content, true);
631
            },
632
            $surveyTests,
633
        );
634
 
635
        $averages = [];
636
 
637
        foreach($sections as $section) {
638
            foreach($section['questions'] as $question) {
639
                switch ($question['type']) {
640
                    case 'multiple':
641
                        $totals = [];
642
 
643
                        foreach($question['options'] as $option) {
644
                            $totals[$option['slug_option']] = 0;
645
                        }
646
 
647
                        foreach($question['options'] as $option) {
648
                            $totals[$option['slug_option']] = count(array_filter(
649
                                $allTests,
650
                                function ($test) use($option, $question) {
651
                                    return in_array($option['slug_option'], $test[$question['slug_question']]);
652
                                }
653
                            ));
654
                        }
655
 
656
                        $averages[$question['slug_question']] = $totals;
657
 
658
                        break;
659
 
660
                    case 'simple':
661
                        $totals = [];
662
 
663
                        foreach($question['options'] as $option) {
664
                            $totals[$option['slug_option']] = 0;
665
                        }
666
 
667
                        foreach ($allTests as $test) {
668
                            $totals[$test[$question['slug_question']]]++;
669
                        }
670
 
671
                        foreach($totals as $slug => $amount) {
672
                            $totals[$slug] = ($amount / count($allTests)) * 100;
673
                        }
674
 
675
                        $averages[$question['slug_question']] = $totals;
676
                    break;
677
 
678
                }
679
            }
680
        }
681
 
7101 eleazar 682
        $row = 1;
683
 
7025 eleazar 684
        for ($i = 0; $i < count($sections); $i++) {
685
            $section = $sections[$i];
686
 
7096 eleazar 687
           for($j = 0; $j < count($section['questions']); $j++) {
7099 eleazar 688
                $question = $section['questions'][$j];
7095 eleazar 689
 
7101 eleazar 690
                if (!in_array($question['type'], ['simple', 'multiple'])){
691
                    continue;
692
                }
15150 efrain 693
 
694
                $row++;
695
                $sheet->setCellValue('A' . $row , $this->cleanStringToExcel($question['text']));
7101 eleazar 696
 
7028 eleazar 697
                switch ($question['type']) {
7032 eleazar 698
                    case 'simple':
7102 eleazar 699
                        for($k = 0; $k < count($question['options']); $k++) {
15150 efrain 700
                            $row++;
701
 
7102 eleazar 702
                            $option = $question['options'][$k];
15150 efrain 703
 
704
 
705
                            $sheet->setCellValue('B' . $row , $this->cleanStringToExcel($option['text']));
706
                            $sheet->setCellValue('C' . $row , round($averages[$question['slug_question']][$option['slug_option']], 2) . '%');
707
 
7032 eleazar 708
                        }
7025 eleazar 709
 
7032 eleazar 710
                        break;
7025 eleazar 711
 
7032 eleazar 712
                    case 'multiple':
7102 eleazar 713
                        for($k = 0; $k < count($question['options']); $k++) {
15150 efrain 714
 
715
                            $row++;
7102 eleazar 716
                            $option = $question['options'][$k];
15150 efrain 717
                            $sheet->setCellValue('B' . $row , $this->cleanStringToExcel($option['text']));
718
                            $sheet->setCellValue('C' . $row , round($averages[$question['slug_question']][$option['slug_option']]));
719
 
7032 eleazar 720
                        }
7025 eleazar 721
 
7032 eleazar 722
                        break;
723
                }
7025 eleazar 724
            }
725
 
726
 
727
        }
728
 
15150 efrain 729
 
730
 
731
        $target_path = $this->config[ 'leaderslinked.fullpath.survey'] . DIRECTORY_SEPARATOR . $survey->uuid;
732
        if(!file_exists($target_path)) {
733
            mkdir($target_path, 0755, true);
734
        }
735
 
736
        $filename = $target_path  . DIRECTORY_SEPARATOR . $survey->uuid . '.xls';
737
 
7088 eleazar 738
        $writer = new Xlsx($spreadsheet);
15150 efrain 739
        $writer->save($filename);
740
 
741
        return $filename;
742
 
743
    }
744
 
7088 eleazar 745
 
15150 efrain 746
 
747
    private function cleanStringToPdf($s, $mbConvert = true)
748
    {
749
 
750
        $s = html_entity_decode($s);
751
 
752
 
753
        if($mbConvert) {
754
            $detect = mb_detect_encoding($s);
755
            if(strtoupper($detect) != 'UTF8') {
756
 
757
                $s = mb_convert_encoding($s, 'UTF8', $detect);
758
 
759
            }
760
        } else {
761
            $s = utf8_decode($s);
762
        }
763
 
764
 
765
        return strip_tags($s);
5930 eleazar 766
    }
15150 efrain 767
 
768
    private function cleanStringToExcel($s)
769
    {
770
 
771
        $s = html_entity_decode($s);
772
        return strip_tags($s);
773
    }
5866 eleazar 774
}