PageRenderTime 211ms CodeModel.GetById 82ms app.highlight 87ms RepoModel.GetById 33ms app.codeStats 1ms

/php/lib/export_manager/component/survey_excel_exporter.class.php

https://bitbucket.org/chamilo/chamilo-app-survey/
PHP | 789 lines | 549 code | 177 blank | 63 comment | 37 complexity | 40c5d8a91ae70df97e22b66f94d1b91c MD5 | raw file
  1<?php
  2namespace application\survey;
  3
  4use group\GroupDataManager;
  5
  6use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
  7use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;
  8
  9use tracking\Tracker;
 10
 11use common\libraries\AndCondition;
 12use common\libraries\EqualityCondition;
 13use common\libraries\InCondition;
 14use common\libraries\Translation;
 15use common\libraries\Utilities;
 16use common\libraries\Request;
 17
 18use reporting\ReportingData;
 19
 20use PHPExcel;
 21use PHPExcel_IOFactory;
 22use PHPExcel_Style_Alignment;
 23
 24class SurveyExportManagerSurveyExcelExporterComponent extends SurveyExportManager
 25{
 26
 27    const COUNT = 'count';
 28    const TOTAL = 'total';
 29    const NOT_STARTED_PARTICIPANTS = 'not_started_participants';
 30    const STARTED_PARTICIPANTS = 'started_participants';
 31    const ALL_PARTICIPANTS = 'all_participants';
 32    const NOT_STARTED_PARTICIPANT_COUNT = 'not_started_participant_count';
 33    const STARTED_PARTICIPANT_COUNT = 'started_participant_count';
 34    const ALL_PARTICIPANT_COUNT = 'all_participant_count';
 35    const CONTEXTS = 'groups';
 36    const CONTEXT_NAME = 'group_name';
 37    const CONTEXT_DESCRIPTION = 'group_description';
 38    const INDIVIDUAL_USERS = 'individual_users';
 39    const USERS = 'users';
 40    const PARTICIPATION_GRADE = 'participation_grade';
 41    const SURVEYS = 'surveys';
 42    const SURVEY_NAME = 'survey_name';
 43    const SURVEY_DESCRIPTION = 'survey_description';
 44    const SURVEY_COUNT = 'survey_count';
 45    const REPORTING_DATA = 'reporting_data';
 46    const DATA_NAME = 'data_name';
 47    const DATA_DESCRIPTION = 'data_description';
 48    const DATA_GROUP = 'data_group';
 49
 50    private $participants;
 51    private $surveys;
 52
 53    /**
 54     * Runs this component and displays its output.
 55     */
 56
 57    function run()
 58    {
 59        $ids = Request :: get(SurveyExportManager :: PARAM_PUBLICATION_ID);
 60
 61        if (! is_array($ids))
 62        {
 63            $ids = array($ids);
 64        }
 65        $this->create_participants($ids);
 66
 67        $this->render_data();
 68
 69    }
 70
 71    public function render_data()
 72    {
 73        $excel = new PHPExcel();
 74
 75        $worksheet = $excel->getSheet(0)->setTitle('Algemeen');
 76        $this->render_summary_data($worksheet);
 77
 78        $questions = $this->get_questions();
 79        $worksheet_index = 1;
 80
 81        foreach ($questions as $question_id => $question)
 82        {
 83
 84            $title = Utilities :: truncate_string(trim(strip_tags($question->get_title())), 15, true, '');
 85            //            $title = $title . ' (id:' . $question_id . ')';
 86            //            $worksheet = $excel->createSheet($worksheet_index)->setTitle($title);
 87            $worksheet = $excel->createSheet($worksheet_index)->setTitle($title);
 88            $worksheet = $excel->getSheet($worksheet_index);
 89            $page_reporting_data = $this->create_page_reporting_data($question);
 90            $this->render_page_data($worksheet, $page_reporting_data);
 91            $worksheet_index ++;
 92        }
 93
 94        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
 95        header('Content-Disposition: attachment;filename="' . 'survey_export' . '.xlsx"');
 96        header('Cache-Control: max-age=0');
 97        $objWriter = PHPExcel_IOFactory :: createWriter($excel, 'Excel2007');
 98        return $objWriter->save('php://output');
 99    }
100
101    private function get_questions()
102    {
103
104        $page_questions = array();
105        $surveys = $this->surveys;
106
107        foreach ($surveys as $survey)
108        {
109            $pages = $survey->get_pages();
110            foreach ($pages as $page)
111            {
112                if ($page->count_questions() != 0)
113                {
114                    $questions = $page->get_questions();
115
116                    foreach ($questions as $question)
117                    {
118
119                        $page_questions[$question->get_id()] = $question;
120                    }
121                }
122            }
123        }
124
125        return $page_questions;
126
127    }
128
129    private function render_summary_data($worksheet)
130    {
131
132        $column = 1;
133        $row = 3;
134
135        $worksheet->getColumnDimensionByColumn($column)->setWidth(20);
136        $worksheet->getColumnDimensionByColumn($column + 1)->setWidth(20);
137        $worksheet->getColumnDimensionByColumn($column + 2)->setWidth(20);
138        $worksheet->getColumnDimensionByColumn($column + 3)->setWidth(20);
139        $worksheet->getColumnDimensionByColumn($column + 4)->setWidth(5);
140        $worksheet->getColumnDimensionByColumn($column + 5)->setWidth(200);
141
142        $surveys = $this->participants[self :: SURVEYS];
143
144        $worksheet->setCellValueByColumnAndRow($column, $row, Translation :: get('SurveyName'));
145        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
146        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
147        $worksheet->setCellValueByColumnAndRow($column + 1, $row, Translation :: get('SurveyDescription'));
148        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
149        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
150        $row ++;
151
152        foreach ($surveys as $survey)
153        {
154            $title = $survey[self :: SURVEY_NAME];
155            $worksheet->setCellValueByColumnAndRow($column, $row, $title);
156            $this->wrap_text($worksheet, $column, $row);
157            $description = $survey[self :: SURVEY_DESCRIPTION];
158            $worksheet->setCellValueByColumnAndRow($column + 1, $row, $description);
159            $this->wrap_text($worksheet, $column, $row);
160            $row ++;
161        }
162
163        $row = $row + 2;
164        $all_participants = $this->participants[self :: ALL_PARTICIPANT_COUNT];
165        $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
166        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
167        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
168
169        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $all_participants);
170        $row ++;
171        $started = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
172        $worksheet->setCellValueByColumnAndRow($column, $row, 'Deelgenomen');
173        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
174        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
175
176        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started);
177        $row ++;
178        $not_started = $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT];
179        $worksheet->setCellValueByColumnAndRow($column, $row, 'Niet deelgenomen');
180        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
181        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
182
183        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $not_started);
184        $row ++;
185        $participatie = $this->participants[self :: PARTICIPATION_GRADE];
186        $worksheet->setCellValueByColumnAndRow($column, $row, 'Participatigraad (%)');
187        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
188        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
189
190        $worksheet->setCellValueByColumnAndRow($column + 1, $row, $participatie);
191        $row = $row + 2;
192
193        $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
194        $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
195        $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
196        $worksheet->setCellValueByColumnAndRow($column + 1, $row, 'Deelgenomen');
197        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
198        $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
199        $worksheet->setCellValueByColumnAndRow($column + 2, $row, 'Niet deelgenomen');
200        $worksheet->getStyleByColumnAndRow($column + 2, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
201        $worksheet->getStyleByColumnAndRow($column + 2, $row)->getFont()->setBold(true);
202        $worksheet->setCellValueByColumnAndRow($column + 3, $row, 'Participatie (%)');
203        $worksheet->getStyleByColumnAndRow($column + 3, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
204        $worksheet->getStyleByColumnAndRow($column + 3, $row)->getFont()->setBold(true);
205        $worksheet->setCellValueByColumnAndRow($column + 5, $row, 'Groepen');
206        $worksheet->getStyleByColumnAndRow($column + 5, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
207        $worksheet->getStyleByColumnAndRow($column + 5, $row)->getFont()->setBold(true);
208
209        $row = $row + 2;
210
211        $groups = $this->participants[self :: GROUPS];
212
213        foreach ($groups as $group_id => $group)
214        {
215            //            $name = $this->participants[self :: GROUPS][$group_id][self :: GROUP_NAME];
216
217
218            $all_participant_count = $group[self :: ALL_PARTICIPANT_COUNT];
219            $worksheet->setCellValueByColumnAndRow($column, $row, $all_participant_count);
220
221            $started_participant_count = $group[self :: STARTED_PARTICIPANT_COUNT];
222            $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started_participant_count);
223
224            $not_started_participant_count = $group[self :: NOT_STARTED_PARTICIPANT_COUNT];
225            $worksheet->setCellValueByColumnAndRow($column + 2, $row, $not_started_participant_count);
226
227            $participatie = $group[self :: PARTICIPATION_GRADE];
228            $worksheet->setCellValueByColumnAndRow($column + 3, $row, $participatie);
229
230            $description = $group[self :: GROUP_DESCRIPTION];
231            $worksheet->setCellValueByColumnAndRow($column + 5, $row, $description);
232            $row ++;
233        }
234
235    }
236
237    private function render_page_data($worksheet, $data)
238    {
239
240        $column = 0;
241        $block_row = 0;
242
243        $worksheet->getColumnDimensionByColumn($column)->setWidth(50);
244        $column_count = 1;
245        while ($column_count < 7)
246        {
247            $worksheet->getColumnDimensionByColumn($column + $column_count)->setWidth(15);
248            $column_count ++;
249        }
250
251        if (is_array($data))
252        {
253
254            foreach ($data as $block_data)
255            {
256                $column = 0;
257                $block_row = $block_row + 2;
258
259                $participant_group = $block_data[self :: DATA_GROUP];
260                $participant_count = $block_data[self :: STARTED_PARTICIPANT_COUNT];
261                $block_title = trim(html_entity_decode(strip_tags($block_data[self :: DATA_NAME])));
262                $block_description = trim(html_entity_decode(strip_tags($block_data[self :: DATA_DESCRIPTION])));
263                $block_content_data = $block_data[self :: REPORTING_DATA];
264
265                $worksheet->setCellValueByColumnAndRow($column, $block_row, $participant_group);
266                $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
267                $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
268                $this->wrap_text($worksheet, $column, $block_row);
269
270                $block_row ++;
271                $worksheet->setCellValueByColumnAndRow($column, $block_row, 'Deelnemers');
272                $worksheet->setCellValueByColumnAndRow($column + 1, $block_row, $participant_count);
273                $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
274                $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getFont()->setBold(true);
275
276                $block_row = $block_row + 2;
277
278                $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_title);
279                $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
280                $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
281                $this->wrap_text($worksheet, $column, $block_row);
282
283                if ($block_description != '')
284                {
285                    $block_row ++;
286                    $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_description);
287                    $this->wrap_text($worksheet, $column, $block_row);
288                    $block_row ++;
289                }
290
291                $block_row ++;
292
293                foreach ($block_content_data->get_rows() as $row_id => $row_name)
294                {
295                    //	dump($row_name);
296                    $column ++;
297                    $worksheet->getColumnDimensionByColumn($column)->setAutoSize(true);
298                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
299                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
300                    $worksheet->setCellValueByColumnAndRow($column, $block_row, trim(html_entity_decode(strip_tags($row_name), ENT_QUOTES)));
301                    $this->wrap_text($worksheet, $column, $block_row);
302
303                }
304
305                $block_row ++;
306
307                $row_count = count($block_content_data->get_rows());
308                $category_count = count($block_content_data->get_categories());
309
310                $categrory_row_index = 1;
311
312                //                dump('row count: ' . $row_count);
313                //                dump('cat count: ' . $category_count);
314
315
316                foreach ($block_content_data->get_categories() as $category_id => $category_name)
317                {
318                    $column = 0;
319
320                    $worksheet->setCellValueByColumnAndRow($column, $block_row, trim(html_entity_decode(strip_tags($category_name), ENT_QUOTES)));
321                    $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
322                    $this->wrap_text($worksheet, $column, $block_row);
323
324                    //                    dump('category row index: ' . $categrory_row_index);
325                    //                    dump('cat row: ' . $block_row);
326                    //
327
328
329                    if ($categrory_row_index == $category_count && $category_count != 1)
330                    {
331                        $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
332                    }
333
334                    $row_index = 1;
335                    //                    dump('row index: ' . $row_index);
336
337
338                    foreach ($block_content_data->get_rows() as $row_id => $row_name)
339                    {
340                        $column ++;
341                        $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_content_data->get_data_category_row($category_id, $row_id));
342                        $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
343                        //                        dump('row index: ' . $row_index);
344                        //                        dump('row: ' . $block_row);
345
346
347                        if ($row_index == $row_count && $row_count != 1)
348                        {
349                            $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
350                        }
351
352                        if ($categrory_row_index == $category_count && $category_count != 1)
353                        {
354                            $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
355                        }
356
357                        $row_index ++;
358                    }
359                    $categrory_row_index ++;
360                    $block_row ++;
361                }
362
363     //                exit();
364            }
365
366        }
367
368    }
369
370    private function wrap_text($worksheet, $colum, $row)
371    {
372        $worksheet->getStyleByColumnAndRow($colum, $row)->getAlignment()->setWrapText(true);
373
374    }
375
376    private function create_page_reporting_data($question)
377    {
378        $page_reporting_data = array();
379
380        $all_participants_ids = $this->participants[self :: ALL_PARTICIPANTS];
381        $reporting_data = $this->create_reporting_data($question, $all_participants_ids);
382
383        $reporting_data_question = array();
384        $reporting_data_question[self :: DATA_GROUP] = Translation :: get(AllGroups);
385        $reporting_data_question[self :: DATA_NAME] = $question->get_title();
386        $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
387        $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
388        $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
389        $page_reporting_data[] = $reporting_data_question;
390
391        $groups = $this->participants[self :: GROUPS];
392        foreach ($groups as $group)
393        {
394            $reporting_data_question = array();
395            $reporting_data_question[self :: DATA_GROUP] = $group[self :: GROUP_DESCRIPTION];
396            $reporting_data_question[self :: DATA_NAME] = $question->get_title();
397            $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
398            $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $group[self :: STARTED_PARTICIPANT_COUNT];
399            $all_participants_ids = $group[self :: ALL_PARTICIPANTS];
400            $reporting_data = $this->create_reporting_data($question, $all_participants_ids);
401            $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
402            $page_reporting_data[] = $reporting_data_question;
403        }
404
405        return $page_reporting_data;
406    }
407
408    private function create_reporting_data($question, $participant_ids)
409    {
410
411        //retrieve the answer trackers
412        $conditions = array();
413        $conditions[] = new InCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $participant_ids);
414        $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $question->get_id());
415        $condition = new AndCondition($conditions);
416        $trackers = Tracker :: get_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);
417
418        //option and matches of question
419        $options = array();
420        $matches = array();
421
422        //matrix to store the answer count
423        $answer_count = array();
424
425        //reporting data and type of question
426        $reporting_data = new ReportingData();
427        $type = $question->get_type();
428
429        switch ($type)
430        {
431            case SurveyMatrixQuestion :: get_type_name() :
432
433                //get options and matches
434                $opts = $question->get_options();
435                foreach ($opts as $option)
436                {
437                    $options[] = $option->get_value();
438                }
439
440                $matchs = $question->get_matches();
441                foreach ($matchs as $match)
442                {
443                    $matches[] = $match;
444                }
445                $total_key = count($matches);
446                $matches[] = Translation :: get(self :: COUNT);
447
448                //create answer matrix for answer counting
449
450
451                $option_count = count($options) - 1;
452
453                while ($option_count >= 0)
454                {
455                    $match_count = count($matches) - 1;
456                    while ($match_count >= 0)
457                    {
458                        $answer_count[$option_count][$match_count] = 0;
459                        $match_count --;
460                    }
461                    //                    $answer_count[$option_count][$total_key] = 0;
462                    $option_count --;
463                }
464
465                //count answers from all answer trackers
466
467
468                while ($tracker = $trackers->next_result())
469                {
470                    $answer = $tracker->get_answer();
471                    $options_answered = array();
472                    foreach ($answer as $key => $option)
473                    {
474                        $options_answered[] = $key;
475                        $totals = array();
476                        foreach ($option as $match_key => $match)
477                        {
478                            if ($question->get_matrix_type() == SurveyMatrixQuestion :: MATRIX_TYPE_CHECKBOX)
479                            {
480                                $answer_count[$key][$match_key] ++;
481                            }
482                            else
483                            {
484                                $answer_count[$key][$match] ++;
485                            }
486                            $answer_count[$key][$total_key] ++;
487                        }
488                    }
489                }
490
491                //creating actual reporing data
492
493
494                foreach ($matches as $match)
495                {
496                    $reporting_data->add_row(strip_tags($match));
497                }
498
499                $totals = array();
500
501                foreach ($options as $option_key => $option)
502                {
503                    $reporting_data->add_category($option);
504                    //                    dump('op key: '.$option_key);
505                    //                    dump('option: '.$option);
506                    foreach ($matches as $match_key => $match)
507                    {
508                        //                        dump('match key: '.$match_key);
509                        //                    	dump('match: '.$match);
510                        //                    	dump('answer_count: '.$answer_count[$option_key][$match_key]);
511                        $totals[$match_key] = $totals[$match_key] + $answer_count[$option_key][$match_key];
512                        //                        dump('total: '.$totals[$match_key]);
513                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key][$match_key]);
514                    }
515
516                }
517
518                //                dump($totals);
519                //
520                //                dump($answer_count);
521
522
523                //                dump($totals);
524
525
526                //                exit;
527
528
529                if (count($options) > 1)
530                {
531                    $reporting_data->add_category(Translation :: get(self :: TOTAL));
532
533                    //                    foreach ($options as $option)
534                    //                    {
535                    foreach ($matches as $match_key => $match)
536                    {
537                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $totals[$match_key]);
538                    }
539
540     //                    }
541
542
543                }
544
545                break;
546            case SurveyMultipleChoiceQuestion :: get_type_name() :
547
548                //get options and matches
549                $opts = $question->get_options();
550                foreach ($opts as $option)
551                {
552                    $options[] = $option->get_value();
553                }
554                //                $options[] = self :: NO_ANSWER;
555
556
557                $matches[] = Translation :: get(self :: COUNT);
558
559                //create answer matrix for answer counting
560
561
562                $option_count = count($options) - 1;
563                while ($option_count >= 0)
564                {
565                    $answer_count[$option_count] = 0;
566                    $option_count --;
567                }
568                //                $answer_count[self :: NO_ANSWER] = 0;
569
570
571                //count answers from all answer trackers
572
573
574                while ($tracker = $trackers->next_result())
575                {
576                    $answer = $tracker->get_answer();
577                    foreach ($answer as $key => $option)
578                    {
579                        if ($question->get_answer_type() == SurveyMultipleChoiceQuestion :: ANSWER_TYPE_CHECKBOX)
580                        {
581                            $answer_count[$key] ++;
582                        }
583                        else
584                        {
585                            $answer_count[$option] ++;
586                        }
587                    }
588                }
589
590                //creating actual reporing data
591
592
593                foreach ($matches as $match)
594                {
595                    $reporting_data->add_row(strip_tags($match));
596                }
597
598                $total = 0;
599                foreach ($options as $option_key => $option)
600                {
601
602                    $reporting_data->add_category($option);
603                    foreach ($matches as $match)
604                    {
605                        $total = $total + $answer_count[$option_key];
606                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key]);
607                    }
608
609                }
610                if (count($options) > 1)
611                {
612                    $reporting_data->add_category(Translation :: get(self :: TOTAL));
613                    foreach ($matches as $match)
614                    {
615                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $total);
616                    }
617                }
618
619                break;
620            default :
621                ;
622                break;
623        }
624
625        return $reporting_data;
626    }
627
628    private function create_participants($ids)
629    {
630
631        $this->participants = array();
632        $this->surveys = array();
633
634        $surveys = array();
635        foreach ($ids as $id)
636        {
637            $sv = array();
638            $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
639            $survey = $survey_publication->get_publication_object();
640            $this->surveys[] = $survey;
641            $survey_title = $survey->get_title();
642            $survey_description = $survey->get_description();
643            $sv[self :: SURVEY_NAME] = Utilities :: truncate_string(trim(strip_tags($survey_title)), 20, true, '');
644            $sv[self :: SURVEY_DESCRIPTION] = Utilities :: truncate_string(trim(strip_tags($survey_description)), 20, true, '');
645            $surveys[$id] = $sv;
646        }
647
648        $this->participants[self :: SURVEYS] = $surveys;
649        $this->participants[self :: SURVEY_COUNT] = count($surveys);
650
651//        $condition = new InCondition(SurveyPublicationGroup :: PROPERTY_SURVEY_PUBLICATION, $ids);
652//        $publication_rel_groups = SurveyDataManager :: get_instance()->retrieve_survey_publication_groups($condition);
653//
654//        $groups = array();
655//        $group_user_ids = array();
656//        $total_user_ids = array();
657//        while ($publication_rel_group = $publication_rel_groups->next_result())
658//        {
659//            $group = GroupDataManager :: get_instance()->retrieve_group($publication_rel_group->get_group_id());
660//            $groups[] = $group;
661//            $group_user_ids[$group->get_id()] = $group->get_users(true, true);
662//            $total_user_ids = array_merge($total_user_ids, $group_user_ids[$group->get_id()]);
663//        }
664//
665//        $user_ids = array();
666//
667//        $condition = new InCondition(SurveyPublicationUser :: PROPERTY_SURVEY_PUBLICATION, $ids);
668//        $publication_rel_users = SurveyDataManager :: get_instance()->retrieve_survey_publication_users($condition);
669
670        while ($publication_rel_user = $publication_rel_users->next_result())
671        {
672            $user_ids[] = $publication_rel_user->get_user_id();
673        }
674
675        $total_user_ids = array_merge($total_user_ids, $user_ids);
676        $total_user_ids = array_unique($total_user_ids);
677
678        $conditions = array();
679        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $ids);
680        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $total_user_ids);
681        $condition = new AndCondition($conditions);
682        $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);
683
684        $all_participants = array();
685        $started_participants = array();
686        $not_started_participants = array();
687
688        $started_users = array();
689        $not_started_users = array();
690
691        while ($tracker = $trackers->next_result())
692        {
693
694            $all_participants[] = $tracker->get_id();
695
696            switch ($tracker->get_status())
697            {
698                case SurveyParticipantTracker :: STATUS_NOTSTARTED :
699                    $not_started_participants[] = $tracker->get_id();
700                    $not_started_users[] = $tracker->get_user_id();
701                    break;
702                case SurveyParticipantTracker :: STATUS_STARTED :
703                    $started_participants[] = $tracker->get_id();
704                    $started_users[] = $tracker->get_user_id();
705                    break;
706                case SurveyParticipantTracker :: STATUS_FINISHED :
707                    $started_participants[] = $tracker->get_id();
708                    $started_users[] = $tracker->get_user_id();
709                    break;
710            }
711        }
712
713        $this->participants[self :: ALL_PARTICIPANTS] = $all_participants;
714        $all_participant_count = count($all_participants);
715        $this->participants[self :: ALL_PARTICIPANT_COUNT] = $all_participant_count;
716        $this->participants[self :: NOT_STARTED_PARTICIPANTS] = $not_started_participants;
717        $not_started_particpant_count = count($not_started_participants);
718        $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT] = $not_started_particpant_count;
719        $this->participants[self :: STARTED_PARTICIPANTS] = $started_participants;
720        $started_participant_count = count($started_participants);
721        $this->participants[self :: STARTED_PARTICIPANT_COUNT] = $started_participant_count;
722
723        $participatie = $started_participant_count / $all_participant_count * 100;
724        $participatie = number_format($participatie, 2);
725        $this->participants[self :: PARTICIPATION_GRADE] = $participatie;
726
727        foreach ($groups as $group)
728        {
729
730            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_NAME] = $group->get_name();
731            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_DESCRIPTION] = $group->get_description();
732
733            $group_users = $group_user_ids[$group->get_id()];
734
735            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $group_users);
736            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);
737
738            $all_trackers = array();
739
740            while ($tracker = $trackers->next_result())
741            {
742                $all_trackers[] = $tracker->get_id();
743            }
744
745            $all_tracker_count = count($all_trackers);
746            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANT_COUNT] = $all_tracker_count;
747            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANTS] = $all_trackers;
748
749            $started = array_intersect($group_users, $started_users);
750
751            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $started);
752            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);
753
754            $started_trackers = array();
755
756            while ($tracker = $trackers->next_result())
757            {
758                $started_trackers[] = $tracker->get_id();
759            }
760
761            $started_tracker_count = count($started_trackers);
762            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANT_COUNT] = $started_tracker_count;
763            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANTS] = $started_trackers;
764
765            $not_started = array_intersect($group_users, $not_started_users);
766
767            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $not_started);
768            $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyExportManager :: APPLICATION_NAME, $condition);
769
770            $not_started_trackers = array();
771
772            while ($tracker = $trackers->next_result())
773            {
774                $not_started_trackers[] = $tracker->get_id();
775            }
776
777            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANT_COUNT] = count($not_started_trackers);
778            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANTS] = $not_started_trackers;
779
780            $participatie = $started_tracker_count / $all_tracker_count * 100;
781            $participatie = number_format($participatie, 2);
782            $this->participants[self :: GROUPS][$group->get_id()][self :: PARTICIPATION_GRADE] = $participatie;
783
784        }
785
786    }
787}
788
789?>