PageRenderTime 367ms CodeModel.GetById 151ms app.highlight 101ms RepoModel.GetById 110ms app.codeStats 0ms

/php/lib/export_manager/component/survey_ods_exporter.class.php

https://bitbucket.org/chamilo/chamilo-app-survey/
PHP | 528 lines | 359 code | 119 blank | 50 comment | 21 complexity | e59a83965796c1aed02a6e1e7e4901b8 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;
 17use common\libraries\path;
 18
 19use reporting\ReportingData;
 20
 21use Ods\Ods;
 22require_once Path :: get_plugin_path() . '/odsPHP/ods.php';
 23
 24class SurveyExportManagerSurveyOdsExporterComponent 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        $ods = new Ods();
 74
 75   
 76
 77        $questions = $this->get_questions();
 78       
 79
 80        header('Content-Type: application/vnd.oasis.opendocument.spreadsheet');
 81        header('Content-Disposition: attachment;filename="' . 'survey_export' . '.ods"');
 82        header('Cache-Control: max-age=0');
 83        $objWriter = $ods->array2Ods();
 84        return $objWriter->save('php://output');
 85    }
 86
 87    private function get_questions()
 88    {
 89
 90        $page_questions = array();
 91        $surveys = $this->surveys;
 92
 93        foreach ($surveys as $survey)
 94        {
 95            $pages = $survey->get_pages();
 96            foreach ($pages as $page)
 97            {
 98                if ($page->count_questions() != 0)
 99                {
100                    $questions = $page->get_questions();
101
102                    foreach ($questions as $question)
103                    {
104
105                        $page_questions[$question->get_id()] = $question;
106                    }
107                }
108            }
109        }
110
111        return $page_questions;
112
113    }
114
115    private function create_page_reporting_data($question)
116    {
117        $page_reporting_data = array();
118
119        $all_participants_ids = $this->participants[self :: ALL_PARTICIPANTS];
120        $reporting_data = $this->create_reporting_data($question, $all_participants_ids);
121
122        $reporting_data_question = array();
123        $reporting_data_question[self :: DATA_GROUP] = Translation :: get(AllGroups);
124        $reporting_data_question[self :: DATA_NAME] = $question->get_title();
125        $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
126        $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
127        $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
128        $page_reporting_data[] = $reporting_data_question;
129
130        $groups = $this->participants[self :: GROUPS];
131        foreach ($groups as $group)
132        {
133            $reporting_data_question = array();
134            $reporting_data_question[self :: DATA_GROUP] = $group[self :: GROUP_DESCRIPTION];
135            $reporting_data_question[self :: DATA_NAME] = $question->get_title();
136            $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
137            $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $group[self :: STARTED_PARTICIPANT_COUNT];
138            $all_participants_ids = $group[self :: ALL_PARTICIPANTS];
139            $reporting_data = $this->create_reporting_data($question, $all_participants_ids);
140            $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
141            $page_reporting_data[] = $reporting_data_question;
142        }
143
144        return $page_reporting_data;
145    }
146
147    private function create_reporting_data($question, $participant_ids)
148    {
149
150        //retrieve the answer trackers
151        $conditions = array();
152        $conditions[] = new InCondition(SurveyQuestionAnswerTracker :: PROPERTY_SURVEY_PARTICIPANT_ID, $participant_ids);
153        $conditions[] = new EqualityCondition(SurveyQuestionAnswerTracker :: PROPERTY_COMPLEX_QUESTION_ID, $question->get_id());
154        $condition = new AndCondition($conditions);
155        $trackers = Tracker :: get_data('survey_question_answer_tracker', SurveyExportManager :: APPLICATION_NAME, $condition);
156
157        //option and matches of question
158        $options = array();
159        $matches = array();
160
161        //matrix to store the answer count
162        $answer_count = array();
163
164        //reporting data and type of question
165        $reporting_data = new ReportingData();
166        $type = $question->get_type();
167
168        switch ($type)
169        {
170            case SurveyMatrixQuestion :: get_type_name() :
171
172                //get options and matches
173                $opts = $question->get_options();
174                foreach ($opts as $option)
175                {
176                    $options[] = $option->get_value();
177                }
178
179                $matchs = $question->get_matches();
180                foreach ($matchs as $match)
181                {
182                    $matches[] = $match;
183                }
184                $total_key = count($matches);
185                $matches[] = Translation :: get(self :: COUNT);
186
187                //create answer matrix for answer counting
188
189
190                $option_count = count($options) - 1;
191
192                while ($option_count >= 0)
193                {
194                    $match_count = count($matches) - 1;
195                    while ($match_count >= 0)
196                    {
197                        $answer_count[$option_count][$match_count] = 0;
198                        $match_count --;
199                    }
200                    //                    $answer_count[$option_count][$total_key] = 0;
201                    $option_count --;
202                }
203
204                //count answers from all answer trackers
205
206
207                while ($tracker = $trackers->next_result())
208                {
209                    $answer = $tracker->get_answer();
210                    $options_answered = array();
211                    foreach ($answer as $key => $option)
212                    {
213                        $options_answered[] = $key;
214                        $totals = array();
215                        foreach ($option as $match_key => $match)
216                        {
217                            if ($question->get_matrix_type() == SurveyMatrixQuestion :: MATRIX_TYPE_CHECKBOX)
218                            {
219                                $answer_count[$key][$match_key] ++;
220                            }
221                            else
222                            {
223                                $answer_count[$key][$match] ++;
224                            }
225                            $answer_count[$key][$total_key] ++;
226                        }
227                    }
228                }
229
230                //creating actual reporing data
231
232
233                foreach ($matches as $match)
234                {
235                    $reporting_data->add_row(strip_tags($match));
236                }
237
238                $totals = array();
239
240                foreach ($options as $option_key => $option)
241                {
242                    $reporting_data->add_category($option);
243                    //                    dump('op key: '.$option_key);
244                    //                    dump('option: '.$option);
245                    foreach ($matches as $match_key => $match)
246                    {
247                        //                        dump('match key: '.$match_key);
248                        //                    	dump('match: '.$match);
249                        //                    	dump('answer_count: '.$answer_count[$option_key][$match_key]);
250                        $totals[$match_key] = $totals[$match_key] + $answer_count[$option_key][$match_key];
251                        //                        dump('total: '.$totals[$match_key]);
252                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key][$match_key]);
253                    }
254
255                }
256
257                //                dump($totals);
258                //
259                //                dump($answer_count);
260
261
262                //                dump($totals);
263
264
265                //                exit;
266
267
268                if (count($options) > 1)
269                {
270                    $reporting_data->add_category(Translation :: get(self :: TOTAL));
271
272                    //                    foreach ($options as $option)
273                    //                    {
274                    foreach ($matches as $match_key => $match)
275                    {
276                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $totals[$match_key]);
277                    }
278
279     //                    }
280
281
282                }
283
284                break;
285            case SurveyMultipleChoiceQuestion :: get_type_name() :
286
287                //get options and matches
288                $opts = $question->get_options();
289                foreach ($opts as $option)
290                {
291                    $options[] = $option->get_value();
292                }
293                //                $options[] = self :: NO_ANSWER;
294
295
296                $matches[] = Translation :: get(self :: COUNT);
297
298                //create answer matrix for answer counting
299
300
301                $option_count = count($options) - 1;
302                while ($option_count >= 0)
303                {
304                    $answer_count[$option_count] = 0;
305                    $option_count --;
306                }
307                //                $answer_count[self :: NO_ANSWER] = 0;
308
309
310                //count answers from all answer trackers
311
312
313                while ($tracker = $trackers->next_result())
314                {
315                    $answer = $tracker->get_answer();
316                    foreach ($answer as $key => $option)
317                    {
318                        if ($question->get_answer_type() == SurveyMultipleChoiceQuestion :: ANSWER_TYPE_CHECKBOX)
319                        {
320                            $answer_count[$key] ++;
321                        }
322                        else
323                        {
324                            $answer_count[$option] ++;
325                        }
326                    }
327                }
328
329                //creating actual reporing data
330
331
332                foreach ($matches as $match)
333                {
334                    $reporting_data->add_row(strip_tags($match));
335                }
336
337                $total = 0;
338                foreach ($options as $option_key => $option)
339                {
340
341                    $reporting_data->add_category($option);
342                    foreach ($matches as $match)
343                    {
344                        $total = $total + $answer_count[$option_key];
345                        $reporting_data->add_data_category_row($option, strip_tags($match), $answer_count[$option_key]);
346                    }
347
348                }
349                if (count($options) > 1)
350                {
351                    $reporting_data->add_category(Translation :: get(self :: TOTAL));
352                    foreach ($matches as $match)
353                    {
354                        $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $total);
355                    }
356                }
357
358                break;
359            default :
360                ;
361                break;
362        }
363
364        return $reporting_data;
365    }
366
367    private function create_participants($ids)
368    {
369
370        $this->participants = array();
371        $this->surveys = array();
372
373        $surveys = array();
374        foreach ($ids as $id)
375        {
376            $sv = array();
377            $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
378            $survey = $survey_publication->get_publication_object();
379            $this->surveys[] = $survey;
380            $survey_title = $survey->get_title();
381            $survey_description = $survey->get_description();
382            $sv[self :: SURVEY_NAME] = Utilities :: truncate_string(trim(strip_tags($survey_title)), 20, true, '');
383            $sv[self :: SURVEY_DESCRIPTION] = Utilities :: truncate_string(trim(strip_tags($survey_description)), 20, true, '');
384            $surveys[$id] = $sv;
385        }
386
387        $this->participants[self :: SURVEYS] = $surveys;
388        $this->participants[self :: SURVEY_COUNT] = count($surveys);
389
390//        $condition = new InCondition(SurveyPublicationGroup :: PROPERTY_SURVEY_PUBLICATION, $ids);
391//        $publication_rel_groups = SurveyDataManager :: get_instance()->retrieve_survey_publication_groups($condition);
392//
393//        $groups = array();
394//        $group_user_ids = array();
395//        $total_user_ids = array();
396//        while ($publication_rel_group = $publication_rel_groups->next_result())
397//        {
398//            $group = GroupDataManager :: get_instance()->retrieve_group($publication_rel_group->get_group_id());
399//            $groups[] = $group;
400//            $group_user_ids[$group->get_id()] = $group->get_users(true, true);
401//            $total_user_ids = array_merge($total_user_ids, $group_user_ids[$group->get_id()]);
402//        }
403//
404//        $user_ids = array();
405//
406//        $condition = new InCondition(SurveyPublicationUser :: PROPERTY_SURVEY_PUBLICATION, $ids);
407//        $publication_rel_users = SurveyDataManager :: get_instance()->retrieve_survey_publication_users($condition);
408
409        while ($publication_rel_user = $publication_rel_users->next_result())
410        {
411            $user_ids[] = $publication_rel_user->get_user_id();
412        }
413
414        $total_user_ids = array_merge($total_user_ids, $user_ids);
415        $total_user_ids = array_unique($total_user_ids);
416
417        $conditions = array();
418        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $ids);
419        $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $total_user_ids);
420        $condition = new AndCondition($conditions);
421        $trackers = Tracker :: get_data('survey_participant_tracker', SurveyExportManager :: APPLICATION_NAME, $condition);
422
423        $all_participants = array();
424        $started_participants = array();
425        $not_started_participants = array();
426
427        $started_users = array();
428        $not_started_users = array();
429
430        while ($tracker = $trackers->next_result())
431        {
432
433            $all_participants[] = $tracker->get_id();
434
435            switch ($tracker->get_status())
436            {
437                case SurveyParticipantTracker :: STATUS_NOTSTARTED :
438                    $not_started_participants[] = $tracker->get_id();
439                    $not_started_users[] = $tracker->get_user_id();
440                    break;
441                case SurveyParticipantTracker :: STATUS_STARTED :
442                    $started_participants[] = $tracker->get_id();
443                    $started_users[] = $tracker->get_user_id();
444                    break;
445                case SurveyParticipantTracker :: STATUS_FINISHED :
446                    $started_participants[] = $tracker->get_id();
447                    $started_users[] = $tracker->get_user_id();
448                    break;
449            }
450        }
451
452        $this->participants[self :: ALL_PARTICIPANTS] = $all_participants;
453        $all_participant_count = count($all_participants);
454        $this->participants[self :: ALL_PARTICIPANT_COUNT] = $all_participant_count;
455        $this->participants[self :: NOT_STARTED_PARTICIPANTS] = $not_started_participants;
456        $not_started_particpant_count = count($not_started_participants);
457        $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT] = $not_started_particpant_count;
458        $this->participants[self :: STARTED_PARTICIPANTS] = $started_participants;
459        $started_participant_count = count($started_participants);
460        $this->participants[self :: STARTED_PARTICIPANT_COUNT] = $started_participant_count;
461
462        $participatie = $started_participant_count / $all_participant_count * 100;
463        $participatie = number_format($participatie, 2);
464        $this->participants[self :: PARTICIPATION_GRADE] = $participatie;
465
466        foreach ($groups as $group)
467        {
468
469            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_NAME] = $group->get_name();
470            $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_DESCRIPTION] = $group->get_description();
471
472            $group_users = $group_user_ids[$group->get_id()];
473
474            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $group_users);
475            $trackers = Tracker :: get_data('survey_participant_tracker', SurveyExportManager :: APPLICATION_NAME, $condition);
476
477            $all_trackers = array();
478
479            while ($tracker = $trackers->next_result())
480            {
481                $all_trackers[] = $tracker->get_id();
482            }
483
484            $all_tracker_count = count($all_trackers);
485            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANT_COUNT] = $all_tracker_count;
486            $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANTS] = $all_trackers;
487
488            $started = array_intersect($group_users, $started_users);
489
490            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $started);
491            $trackers = Tracker :: get_data('survey_participant_tracker', SurveyExportManager :: APPLICATION_NAME, $condition);
492
493            $started_trackers = array();
494
495            while ($tracker = $trackers->next_result())
496            {
497                $started_trackers[] = $tracker->get_id();
498            }
499
500            $started_tracker_count = count($started_trackers);
501            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANT_COUNT] = $started_tracker_count;
502            $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANTS] = $started_trackers;
503
504            $not_started = array_intersect($group_users, $not_started_users);
505
506            $condition = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $not_started);
507            $trackers = Tracker :: get_data('survey_participant_tracker', SurveyExportManager :: APPLICATION_NAME, $condition);
508
509            $not_started_trackers = array();
510
511            while ($tracker = $trackers->next_result())
512            {
513                $not_started_trackers[] = $tracker->get_id();
514            }
515
516            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANT_COUNT] = count($not_started_trackers);
517            $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANTS] = $not_started_trackers;
518
519            $participatie = $started_tracker_count / $all_tracker_count * 100;
520            $participatie = number_format($participatie, 2);
521            $this->participants[self :: GROUPS][$group->get_id()][self :: PARTICIPATION_GRADE] = $participatie;
522
523        }
524
525    }
526}
527
528?>