chamilo-app-survey /php/lib/export_manager/component/survey_excel_exporter.class.php

Language PHP Lines 789
MD5 Hash 40c5d8a91ae70df97e22b66f94d1b91c Estimated Cost $14,339 (why?)
Repository https://bitbucket.org/chamilo/chamilo-app-survey/ View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<?php
namespace application\survey;

use group\GroupDataManager;

use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;

use tracking\Tracker;

use common\libraries\AndCondition;
use common\libraries\EqualityCondition;
use common\libraries\InCondition;
use common\libraries\Translation;
use common\libraries\Utilities;
use common\libraries\Request;

use reporting\ReportingData;

use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Style_Alignment;

class SurveyExportManagerSurveyExcelExporterComponent extends SurveyExportManager
{

    const COUNT = 'count';
    const TOTAL = 'total';
    const NOT_STARTED_PARTICIPANTS = 'not_started_participants';
    const STARTED_PARTICIPANTS = 'started_participants';
    const ALL_PARTICIPANTS = 'all_participants';
    const NOT_STARTED_PARTICIPANT_COUNT = 'not_started_participant_count';
    const STARTED_PARTICIPANT_COUNT = 'started_participant_count';
    const ALL_PARTICIPANT_COUNT = 'all_participant_count';
    const CONTEXTS = 'groups';
    const CONTEXT_NAME = 'group_name';
    const CONTEXT_DESCRIPTION = 'group_description';
    const INDIVIDUAL_USERS = 'individual_users';
    const USERS = 'users';
    const PARTICIPATION_GRADE = 'participation_grade';
    const SURVEYS = 'surveys';
    const SURVEY_NAME = 'survey_name';
    const SURVEY_DESCRIPTION = 'survey_description';
    const SURVEY_COUNT = 'survey_count';
    const REPORTING_DATA = 'reporting_data';
    const DATA_NAME = 'data_name';
    const DATA_DESCRIPTION = 'data_description';
    const DATA_GROUP = 'data_group';

    private $participants;
    private $surveys;

    /**
     * Runs this component and displays its output.
     */

    function run()
    {
        $ids = Request :: get(SurveyExportManager :: PARAM_PUBLICATION_ID);

        if (! is_array($ids))
        {
            $ids = array($ids);
        }
        $this->create_participants($ids);

        $this->render_data();

    }

    public function render_data()
    {
        $excel = new PHPExcel();

        $worksheet = $excel->getSheet(0)->setTitle('Algemeen');
        $this->render_summary_data($worksheet);

        $questions = $this->get_questions();
        $worksheet_index = 1;

        foreach ($questions as $question_id => $question)
        {

            $title = Utilities :: truncate_string(trim(strip_tags($question->get_title())), 15, true, '');
            //            $title = $title . ' (id:' . $question_id . ')';
            //            $worksheet = $excel->createSheet($worksheet_index)->setTitle($title);
            $worksheet = $excel->createSheet($worksheet_index)->setTitle($title);
            $worksheet = $excel->getSheet($worksheet_index);
            $page_reporting_data = $this->create_page_reporting_data($question);
            $this->render_page_data($worksheet, $page_reporting_data);
            $worksheet_index ++;
        }

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . 'survey_export' . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory :: createWriter($excel, 'Excel2007');
        return $objWriter->save('php://output');
    }

    private function get_questions()
    {

        $page_questions = array();
        $surveys = $this->surveys;

        foreach ($surveys as $survey)
        {
            $pages = $survey->get_pages();
            foreach ($pages as $page)
            {
                if ($page->count_questions() != 0)
                {
                    $questions = $page->get_questions();

                    foreach ($questions as $question)
                    {

                        $page_questions[$question->get_id()] = $question;
                    }
                }
            }
        }

        return $page_questions;

    }

    private function render_summary_data($worksheet)
    {

        $column = 1;
        $row = 3;

        $worksheet->getColumnDimensionByColumn($column)->setWidth(20);
        $worksheet->getColumnDimensionByColumn($column + 1)->setWidth(20);
        $worksheet->getColumnDimensionByColumn($column + 2)->setWidth(20);
        $worksheet->getColumnDimensionByColumn($column + 3)->setWidth(20);
        $worksheet->getColumnDimensionByColumn($column + 4)->setWidth(5);
        $worksheet->getColumnDimensionByColumn($column + 5)->setWidth(200);

        $surveys = $this->participants[self :: SURVEYS];

        $worksheet->setCellValueByColumnAndRow($column, $row, Translation :: get('SurveyName'));
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
        $worksheet->setCellValueByColumnAndRow($column + 1, $row, Translation :: get('SurveyDescription'));
        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
        $row ++;

        foreach ($surveys as $survey)
        {
            $title = $survey[self :: SURVEY_NAME];
            $worksheet->setCellValueByColumnAndRow($column, $row, $title);
            $this->wrap_text($worksheet, $column, $row);
            $description = $survey[self :: SURVEY_DESCRIPTION];
            $worksheet->setCellValueByColumnAndRow($column + 1, $row, $description);
            $this->wrap_text($worksheet, $column, $row);
            $row ++;
        }

        $row = $row + 2;
        $all_participants = $this->participants[self :: ALL_PARTICIPANT_COUNT];
        $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);

        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $all_participants);
        $row ++;
        $started = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
        $worksheet->setCellValueByColumnAndRow($column, $row, 'Deelgenomen');
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);

        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started);
        $row ++;
        $not_started = $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT];
        $worksheet->setCellValueByColumnAndRow($column, $row, 'Niet deelgenomen');
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);

        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $not_started);
        $row ++;
        $participatie = $this->participants[self :: PARTICIPATION_GRADE];
        $worksheet->setCellValueByColumnAndRow($column, $row, 'Participatigraad (%)');
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);

        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $participatie);
        $row = $row + 2;

        $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
        $worksheet->setCellValueByColumnAndRow($column + 1, $row, 'Deelgenomen');
        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
        $worksheet->setCellValueByColumnAndRow($column + 2, $row, 'Niet deelgenomen');
        $worksheet->getStyleByColumnAndRow($column + 2, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column + 2, $row)->getFont()->setBold(true);
        $worksheet->setCellValueByColumnAndRow($column + 3, $row, 'Participatie (%)');
        $worksheet->getStyleByColumnAndRow($column + 3, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column + 3, $row)->getFont()->setBold(true);
        $worksheet->setCellValueByColumnAndRow($column + 5, $row, 'Groepen');
        $worksheet->getStyleByColumnAndRow($column + 5, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
        $worksheet->getStyleByColumnAndRow($column + 5, $row)->getFont()->setBold(true);

        $row = $row + 2;

        $groups = $this->participants[self :: GROUPS];

        foreach ($groups as $group_id => $group)
        {
            //            $name = $this->participants[self :: GROUPS][$group_id][self :: GROUP_NAME];


            $all_participant_count = $group[self :: ALL_PARTICIPANT_COUNT];
            $worksheet->setCellValueByColumnAndRow($column, $row, $all_participant_count);

            $started_participant_count = $group[self :: STARTED_PARTICIPANT_COUNT];
            $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started_participant_count);

            $not_started_participant_count = $group[self :: NOT_STARTED_PARTICIPANT_COUNT];
            $worksheet->setCellValueByColumnAndRow($column + 2, $row, $not_started_participant_count);

            $participatie = $group[self :: PARTICIPATION_GRADE];
            $worksheet->setCellValueByColumnAndRow($column + 3, $row, $participatie);

            $description = $group[self :: GROUP_DESCRIPTION];
            $worksheet->setCellValueByColumnAndRow($column + 5, $row, $description);
            $row ++;
        }

    }

    private function render_page_data($worksheet, $data)
    {

        $column = 0;
        $block_row = 0;

        $worksheet->getColumnDimensionByColumn($column)->setWidth(50);
        $column_count = 1;
        while ($column_count < 7)
        {
            $worksheet->getColumnDimensionByColumn($column + $column_count)->setWidth(15);
            $column_count ++;
        }

        if (is_array($data))
        {

            foreach ($data as $block_data)
            {
                $column = 0;
                $block_row = $block_row + 2;

                $participant_group = $block_data[self :: DATA_GROUP];
                $participant_count = $block_data[self :: STARTED_PARTICIPANT_COUNT];
                $block_title = trim(html_entity_decode(strip_tags($block_data[self :: DATA_NAME])));
                $block_description = trim(html_entity_decode(strip_tags($block_data[self :: DATA_DESCRIPTION])));
                $block_content_data = $block_data[self :: REPORTING_DATA];

                $worksheet->setCellValueByColumnAndRow($column, $block_row, $participant_group);
                $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                $this->wrap_text($worksheet, $column, $block_row);

                $block_row ++;
                $worksheet->setCellValueByColumnAndRow($column, $block_row, 'Deelnemers');
                $worksheet->setCellValueByColumnAndRow($column + 1, $block_row, $participant_count);
                $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getFont()->setBold(true);

                $block_row = $block_row + 2;

                $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_title);
                $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                $this->wrap_text($worksheet, $column, $block_row);

                if ($block_description != '')
                {
                    $block_row ++;
                    $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_description);
                    $this->wrap_text($worksheet, $column, $block_row);
                    $block_row ++;
                }

                $block_row ++;

                foreach ($block_content_data->get_rows() as $row_id => $row_name)
                {
                    //	dump($row_name);
                    $column ++;
                    $worksheet->getColumnDimensionByColumn($column)->setAutoSize(true);
                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                    $worksheet->setCellValueByColumnAndRow($column, $block_row, trim(html_entity_decode(strip_tags($row_name), ENT_QUOTES)));
                    $this->wrap_text($worksheet, $column, $block_row);

                }

                $block_row ++;

                $row_count = count($block_content_data->get_rows());
                $category_count = count($block_content_data->get_categories());

                $categrory_row_index = 1;

                //                dump('row count: ' . $row_count);
                //                dump('cat count: ' . $category_count);


                foreach ($block_content_data->get_categories() as $category_id => $category_name)
                {
                    $column = 0;

                    $worksheet->setCellValueByColumnAndRow($column, $block_row, trim(html_entity_decode(strip_tags($category_name), ENT_QUOTES)));
                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
                    $this->wrap_text($worksheet, $column, $block_row);

                    //                    dump('category row index: ' . $categrory_row_index);
                    //                    dump('cat row: ' . $block_row);
                    //


                    if ($categrory_row_index == $category_count && $category_count != 1)
                    {
                        $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                    }

                    $row_index = 1;
                    //                    dump('row index: ' . $row_index);


                    foreach ($block_content_data->get_rows() as $row_id => $row_name)
                    {
                        $column ++;
                        $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_content_data->get_data_category_row($category_id, $row_id));
                        $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
                        //                        dump('row index: ' . $row_index);
                        //                        dump('row: ' . $block_row);


                        if ($row_index == $row_count && $row_count != 1)
                        {
                            $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                        }

                        if ($categrory_row_index == $category_count && $category_count != 1)
                        {
                            $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
                        }

                        $row_index ++;
                    }
                    $categrory_row_index ++;
                    $block_row ++;
                }

     //                exit();
            }

        }

    }

    private function wrap_text($worksheet, $colum, $row)
    {
        $worksheet->getStyleByColumnAndRow($colum, $row)->getAlignment()->setWrapText(true);

    }

    private function create_page_reporting_data($question)
    {
        $page_reporting_data = array();

        $all_participants_ids = $this->participants[self :: ALL_PARTICIPANTS];
        $reporting_data = $this->create_reporting_data($question, $all_participants_ids);

        $reporting_data_question = array();
        $reporting_data_question[self :: DATA_GROUP] = Translation :: get(AllGroups);
        $reporting_data_question[self :: DATA_NAME] = $question->get_title();
        $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
        $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
        $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
        $page_reporting_data[] = $reporting_data_question;

        $groups = $this->participants[self :: GROUPS];
        foreach ($groups as $group)
        {
            $reporting_data_question = array();
            $reporting_data_question[self :: DATA_GROUP] = $group[self :: GROUP_DESCRIPTION];
            $reporting_data_question[self :: DATA_NAME] = $question->get_title();
            $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
            $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $group[self :: STARTED_PARTICIPANT_COUNT];
            $all_participants_ids = $group[self :: ALL_PARTICIPANTS];
            $reporting_data = $this->create_reporting_data($question, $all_participants_ids);
            $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
            $page_reporting_data[] = $reporting_data_question;
        }

        return $page_reporting_data;
    }

    private function create_reporting_data($question, $participant_ids)
    {

        //retrieve the answer trackers
        $conditions = array();
        $conditions[] = new InCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $participant_ids);
        $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $question->get_id());
        $condition = new AndCondition($conditions);
        $trackers = Tracker :: get_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);

        //option and matches of question
        $options = array();
        $matches = array();

        //matrix to store the answer count
        $answer_count = array();

        //reporting data and type of question
        $reporting_data = new ReportingData();
        $type = $question->get_type();

        switch ($type)
        {
            case SurveyMatrixQuestion :: get_type_name() :

                //get options and matches
                $opts = $question->get_options();
                foreach ($opts as $option)
                {
                    $options[] = $option->get_value();
                }

                $matchs = $question->get_matches();
                foreach ($matchs as $match)
                {
                    $matches[] = $match;
                }
                $total_key = count($matches);
                $matches[] = Translation :: get(self :: COUNT);

                //create answer matrix for answer counting


                $option_count = count($options) - 1;

                while ($option_count >= 0)
                {
                    $match_count = count($matches) - 1;
                    while ($match_count >= 0)
                    {
                        $answer_count[$option_count][$match_count] = 0;
                        $match_count --;
                    }
                    //                    $answer_count[$option_count][$total_key] = 0;
                    $option_count --;
                }

                //count answers from all answer trackers


                while ($tracker = $trackers->next_result())
                {
                    $answer = $tracker->get_answer();
                    $options_answered = array();
                    foreach ($answer as $key => $option)
                    {
                        $options_answered[] = $key;
                        $totals = array();
                        foreach ($option as $match_key => $match)
                        {
                            if ($question->get_matrix_type() == SurveyMatrixQuestion :: MATRIX_TYPE_CHECKBOX)
                            {
                                $answer_count[$key][$match_key] ++;
                            }
                            else
                            {
                                $answer_count[$key][$match] ++;
                            }
                            $answer_count[$key][$total_key] ++;
                        }
                    }
                }

                //creating actual reporing data


                foreach ($matches as $match)
                {
                    $reporting_data->add_row(strip_tags($match));
                }

                $totals = array();

                foreach ($options as $option_key => $option)
                {
                    $reporting_data->add_category($option);
                    //                    dump('op key: '.$option_key);
                    //                    dump('option: '.$option);
                    foreach ($matches as $match_key => $match)
                    {
                        //                        dump('match key: '.$match_key);
                        //                    	dump('match: '.$match);
                        //                    	dump('answer_count: '.$answer_count[$option_key][$match_key]);
                        $totals[$match_key] = $totals[$match_key] + $answer_count[$option_key][$match_key];
                        //                        dump('total: '.$totals[$match_key]);
                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key][$match_key]);
                    }

                }

                //                dump($totals);
                //
                //                dump($answer_count);


                //                dump($totals);


                //                exit;


                if (count($options) > 1)
                {
                    $reporting_data->add_category(Translation :: get(self :: TOTAL));

                    //                    foreach ($options as $option)
                    //                    {
                    foreach ($matches as $match_key => $match)
                    {
                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $totals[$match_key]);
                    }

     //                    }


                }

                break;
            case SurveyMultipleChoiceQuestion :: get_type_name() :

                //get options and matches
                $opts = $question->get_options();
                foreach ($opts as $option)
                {
                    $options[] = $option->get_value();
                }
                //                $options[] = self :: NO_ANSWER;


                $matches[] = Translation :: get(self :: COUNT);

                //create answer matrix for answer counting


                $option_count = count($options) - 1;
                while ($option_count >= 0)
                {
                    $answer_count[$option_count] = 0;
                    $option_count --;
                }
                //                $answer_count[self :: NO_ANSWER] = 0;


                //count answers from all answer trackers


                while ($tracker = $trackers->next_result())
                {
                    $answer = $tracker->get_answer();
                    foreach ($answer as $key => $option)
                    {
                        if ($question->get_answer_type() == SurveyMultipleChoiceQuestion :: ANSWER_TYPE_CHECKBOX)
                        {
                            $answer_count[$key] ++;
                        }
                        else
                        {
                            $answer_count[$option] ++;
                        }
                    }
                }

                //creating actual reporing data


                foreach ($matches as $match)
                {
                    $reporting_data->add_row(strip_tags($match));
                }

                $total = 0;
                foreach ($options as $option_key => $option)
                {

                    $reporting_data->add_category($option);
                    foreach ($matches as $match)
                    {
                        $total = $total + $answer_count[$option_key];
                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key]);
                    }

                }
                if (count($options) > 1)
                {
                    $reporting_data->add_category(Translation :: get(self :: TOTAL));
                    foreach ($matches as $match)
                    {
                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $total);
                    }
                }

                break;
            default :
                ;
                break;
        }

        return $reporting_data;
    }

    private function create_participants($ids)
    {

        $this->participants = array();
        $this->surveys = array();

        $surveys = array();
        foreach ($ids as $id)
        {
            $sv = array();
            $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
            $survey = $survey_publication->get_publication_object();
            $this->surveys[] = $survey;
            $survey_title = $survey->get_title();
            $survey_description = $survey->get_description();
            $sv[self :: SURVEY_NAME] = Utilities :: truncate_string(trim(strip_tags($survey_title)), 20, true, '');
            $sv[self :: SURVEY_DESCRIPTION] = Utilities :: truncate_string(trim(strip_tags($survey_description)), 20, true, '');
            $surveys[$id] = $sv;
        }

        $this->participants[self :: SURVEYS] = $surveys;
        $this->participants[self :: SURVEY_COUNT] = count($surveys);

//        $condition = new InCondition(SurveyPublicationGroup :: PROPERTY_SURVEY_PUBLICATION, $ids);
//        $publication_rel_groups = SurveyDataManager :: get_instance()->retrieve_survey_publication_groups($condition);
//
//        $groups = array();
//        $group_user_ids = array();
//        $total_user_ids = array();
//        while ($publication_rel_group = $publication_rel_groups->next_result())
//        {
//            $group = GroupDataManager :: get_instance()->retrieve_group($publication_rel_group->get_group_id());
//            $groups[] = $group;
//            $group_user_ids[$group->get_id()] = $group->get_users(true, true);
//            $total_user_ids = array_merge($total_user_ids, $group_user_ids[$group->get_id()]);
//        }
//
//        $user_ids = array();
//
//        $condition = new InCondition(SurveyPublicationUser :: PROPERTY_SURVEY_PUBLICATION, $ids);
//        $publication_rel_users = SurveyDataManager :: get_instance()->retrieve_survey_publication_users($condition);

        while ($publication_rel_user = $publication_rel_users->next_result())
        {
            $user_ids[] = $publication_rel_user->get_user_id();
        }

        $total_user_ids = array_merge($total_user_ids, $user_ids);
        $total_user_ids = array_unique($total_user_ids);

        $conditions = array();
        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $ids);
        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $total_user_ids);
        $condition = new AndCondition($conditions);
        $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);

        $all_participants = array();
        $started_participants = array();
        $not_started_participants = array();

        $started_users = array();
        $not_started_users = array();

        while ($tracker = $trackers->next_result())
        {

            $all_participants[] = $tracker->get_id();

            switch ($tracker->get_status())
            {
                case SurveyParticipantTracker :: STATUS_NOTSTARTED :
                    $not_started_participants[] = $tracker->get_id();
                    $not_started_users[] = $tracker->get_user_id();
                    break;
                case SurveyParticipantTracker :: STATUS_STARTED :
                    $started_participants[] = $tracker->get_id();
                    $started_users[] = $tracker->get_user_id();
                    break;
                case SurveyParticipantTracker :: STATUS_FINISHED :
                    $started_participants[] = $tracker->get_id();
                    $started_users[] = $tracker->get_user_id();
                    break;
            }
        }

        $this->participants[self :: ALL_PARTICIPANTS] = $all_participants;
        $all_participant_count = count($all_participants);
        $this->participants[self :: ALL_PARTICIPANT_COUNT] = $all_participant_count;
        $this->participants[self :: NOT_STARTED_PARTICIPANTS] = $not_started_participants;
        $not_started_particpant_count = count($not_started_participants);
        $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT] = $not_started_particpant_count;
        $this->participants[self :: STARTED_PARTICIPANTS] = $started_participants;
        $started_participant_count = count($started_participants);
        $this->participants[self :: STARTED_PARTICIPANT_COUNT] = $started_participant_count;

        $participatie = $started_participant_count / $all_participant_count * 100;
        $participatie = number_format($participatie, 2);
        $this->participants[self :: PARTICIPATION_GRADE] = $participatie;

        foreach ($groups as $group)
        {

            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_NAME] = $group->get_name();
            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_DESCRIPTION] = $group->get_description();

            $group_users = $group_user_ids[$group->get_id()];

            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $group_users);
            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);

            $all_trackers = array();

            while ($tracker = $trackers->next_result())
            {
                $all_trackers[] = $tracker->get_id();
            }

            $all_tracker_count = count($all_trackers);
            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANT_COUNT] = $all_tracker_count;
            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANTS] = $all_trackers;

            $started = array_intersect($group_users, $started_users);

            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $started);
            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);

            $started_trackers = array();

            while ($tracker = $trackers->next_result())
            {
                $started_trackers[] = $tracker->get_id();
            }

            $started_tracker_count = count($started_trackers);
            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANT_COUNT] = $started_tracker_count;
            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANTS] = $started_trackers;

            $not_started = array_intersect($group_users, $not_started_users);

            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $not_started);
            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);

            $not_started_trackers = array();

            while ($tracker = $trackers->next_result())
            {
                $not_started_trackers[] = $tracker->get_id();
            }

            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANT_COUNT] = count($not_started_trackers);
            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANTS] = $not_started_trackers;

            $participatie = $started_tracker_count / $all_tracker_count * 100;
            $participatie = number_format($participatie, 2);
            $this->participants[self :: GROUPS][$group->get_id()][self :: PARTICIPATION_GRADE] = $participatie;

        }

    }
}

?>
Back to Top