/php/lib/export_manager/component/survey_raw_excel_exporter.class.php
PHP | 384 lines | 304 code | 74 blank | 6 comment | 27 complexity | 301fd9385f60079154f1880bb4140371 MD5 | raw file
- <?php
- namespace application\survey;
-
- use repository\content_object\survey_rating_question\SurveyRatingQuestion;
-
- use repository\content_object\survey_select_question\SurveySelectQuestion;
-
- use repository\content_object\survey_matching_question\SurveyMatchingQuestion;
-
- use repository\content_object\survey_description\SurveyDescription;
-
- use repository\content_object\survey\SurveyAnalyzer;
-
- use repository\content_object\survey\SurveyTemplateUser;
-
- use repository\content_object\survey\SurveyContextRelUser;
-
- use repository\content_object\survey\SurveyContextDataManager;
-
- use repository\content_object\survey\SurveyContext;
-
- use repository\content_object\survey\Survey;
-
- use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
- use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;
- use repository\content_object\survey_open_question\SurveyOpenQuestion;
- use repository\RepositoryDataManager;
-
- use tracking\Tracker;
- use tracking\Event;
- use reporting\ReportingData;
- use group\GroupDataManager;
-
- 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 common\libraries\Path;
-
- use PHPExcel;
- use PHPExcel_Style_Alignment;
- use PHPExcel_Style_Fill;
- use PHPExcel_IOFactory;
- use PHPExcel_Style_Font;
- use PHPExcel_Style_Color;
-
- ini_set("memory_limit", "-1");
- ini_set("max_execution_time", "0");
-
- require_once Path :: get_plugin_path() . 'phpexcel/PHPExcel.php';
-
- class SurveyExportManagerSurveyRawExcelExporterComponent extends SurveyExportManager
- {
-
- private $questions_cache;
- private $options_cache;
- private $matches_cache;
- private $context_cache;
- private $survey_cache;
- private $publication_cache;
- private $answer_array;
-
- /**
- * Runs this component and displays its output.
- */
-
- function run()
- {
- $ids = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
-
- if (! empty($ids))
- {
- if (! is_array($ids))
- {
- $ids = array($ids);
- }
-
- $this->create_answer_array($ids);
-
- // dump($this->answer_array);
- // exit;
- }
-
- $this->render_data();
-
- }
-
- public function render_data()
- {
-
- $excel = new PHPExcel();
-
- $worksheet_index = 0;
-
- if (isset($this->user_answers["error"]))
- {
- $worksheet = $excel->createSheet($worksheet_index)->setTitle("error");
- $worksheet->setCellValueByColumnAndRow(1, 1, "Different context templates used in selected surveys");
- }
- else
- {
- foreach ($this->answer_array as $level => $levels)
- {
-
- foreach ($levels as $publication_id => $publications)
- {
- $context_template = $this->survey_cache[$publication_id]->get_context_template_for_level($level);
- $context_template_name = Utilities :: truncate_string($context_template->get_context_type_name(), 30);
- $worksheet = null;
- $worksheet = $excel->getSheetByName($context_template_name);
- if (! isset($worksheet))
- {
- $worksheet = $excel->createSheet($worksheet_index)->setTitle($context_template_name);
- $worksheet_index ++;
- $row = 1;
- }
-
- foreach ($publications as $context_path => $case)
- {
-
- foreach ($case as $user_id => $fullpaths)
- {
-
- foreach ($fullpaths as $fullpath => $answers)
- {
-
- foreach ($answers as $question => $all_answers)
- {
- foreach ($all_answers as $answer)
- {
- $column = 0;
-
- if ($row == 1)
- {
- $worksheet->setCellValueByColumnAndRow($column ++, $row, "Case");
- $worksheet->setCellValueByColumnAndRow($column ++, $row, "Publication");
- if ($context_path != 0)
- {
-
- $c_ids = explode("_", $context_path);
- $context_level = count($c_ids);
- foreach ($c_ids as $c_id)
- {
- $c_template = $this->survey_cache[$publication_id]->get_context_template_for_level($context_level);
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $c_template->get_context_type_name());
- $context_level --;
- }
- }
-
- $worksheet->setCellValueByColumnAndRow($column ++, $row, "Vraag");
- $worksheet->setCellValueByColumnAndRow($column ++, $row, "Antwoord");
- }
- else
- {
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $user_id);
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $this->publication_cache[$publication_id]);
- if ($context_path != 0)
- {
-
- $c_ids = explode("_", $context_path);
- foreach ($c_ids as $c_id)
- {
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $this->get_context($c_id));
- }
- }
-
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $question);
- $worksheet->setCellValueByColumnAndRow($column ++, $row, $answer);
- }
-
- $row ++;
- }
- }
- }
- }
- }
- }
- }
- }
- // exit;
- 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');
- }
-
- function create_answer_array($ids)
- {
-
- $this->publication_cache = array();
-
- $context_template_ids = array();
-
- foreach ($ids as $id)
- {
-
- $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
- $survey = $survey_publication->get_publication_object();
- $this->survey_cache[$id] = $survey;
- $this->publication_cache[$id] = $survey_publication->get_title();
- $context_template_ids[] = $survey->get_context_template_id();
- }
-
- $context_template_ids = array_unique($context_template_ids);
-
- $this->answer_array = array();
-
- if (count($context_template_ids) > 1)
- {
- $this->answer_array["error"] = $context_template_ids;
- }
- else
- {
-
- $levels = $survey->count_levels();
-
- $condition = new InCondition(SurveyMultipleChoiceQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
- $trackers = Tracker :: get_data(SurveyMultipleChoiceQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- while ($tracker = $trackers->next_result())
- {
- $user_id = $tracker->get_user_id();
- $context_id = $tracker->get_context_id();
- $complex_question_id = $tracker->get_complex_question_id();
- $question = $this->transcode_string($this->get_question($complex_question_id)->get_title());
- $question = $complex_question_id . ' ' . $question;
- $publication_id = $tracker->get_publication_id();
-
- $options = $this->get_options($complex_question_id);
- $answer = $options[$tracker->get_option_id()];
-
- $full_path = $tracker->get_context_path();
- $full_path_parts = explode('|', $full_path);
- $path = $full_path_parts[1];
- $path_ids = explode("_", $path);
-
- $level_count = count($path_ids);
- while ($level_count > 0)
- {
- $reverted_path_ids = array_reverse($path_ids);
- $path = (string) implode(array_reverse($reverted_path_ids), '_');
- $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$question][] = $answer;
- array_pop($path_ids);
- $level_count = count($path_ids);
- }
-
- }
-
- $condition = new InCondition(SurveyMatrixQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
- $trackers = Tracker :: get_data(SurveyMatrixQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- while ($tracker = $trackers->next_result())
- {
- $user_id = $tracker->get_user_id();
- $context_id = $tracker->get_context_id();
- $complex_question_id = $tracker->get_complex_question_id();
- $publication_id = $tracker->get_publication_id();
-
- $question = $this->transcode_string($this->get_question($complex_question_id)->get_title());
-
- $options = $this->get_options($complex_question_id);
- $option = $options[$tracker->get_option_id()];
- $option = $complex_question_id . ' ' . $question . ' : ' . $option;
-
- $matches = $this->get_matches($complex_question_id);
- $answer = $matches[$tracker->get_match_id()];
-
- $full_path = $tracker->get_context_path();
- $full_path_parts = explode('|', $full_path);
- $path = $full_path_parts[1];
- $path_ids = explode("_", $path);
- $reverted_path_ids = array_reverse($path_ids);
- $level_count = count($path_ids);
- while ($level_count > 0)
- {
- $reverted_path_ids = array_reverse($path_ids);
- $path = (string) implode(array_reverse($reverted_path_ids), '_');
- $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$option][] = $answer;
- array_pop($path_ids);
- $level_count = count($path_ids);
- }
- }
-
- $condition = new InCondition(SurveyOpenQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
- $trackers = Tracker :: get_data(SurveyOpenQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- while ($tracker = $trackers->next_result())
- {
- $user_id = $tracker->get_user_id();
- $context_id = $tracker->get_context_id();
- $publication_id = $tracker->get_publication_id();
- $complex_question_id = $tracker->get_complex_question_id();
- $question = $this->get_question($complex_question_id);
- $title = $this->transcode_string($question->get_title());
- $title = $complex_question_id . ' ' . $title;
-
- $answer = $tracker->get_text();
-
- $full_path = $tracker->get_context_path();
- $full_path_parts = explode('|', $full_path);
- $path = $full_path_parts[1];
- $path_ids = explode("_", $path);
- $reverted_path_ids = array_reverse($path_ids);
- $level_count = count($path_ids);
- while ($level_count > 0)
- {
- $reverted_path_ids = array_reverse($path_ids);
- $path = (string) implode(array_reverse($reverted_path_ids), '_');
- $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$title][] = $answer;
- array_pop($path_ids);
- $level_count = count($path_ids);
- }
- }
- }
- }
-
- private function get_question($complex_id)
- {
- if (! isset($this->questions_cache) || ! isset($this->questions_cache[$complex_id]))
- {
- $complex_question = RepositoryDataManager :: get_instance()->retrieve_complex_content_object_item($complex_id);
- $this->questions_cache[$complex_id] = $complex_question->get_ref_object();
- }
- return $this->questions_cache[$complex_id];
- }
-
- private function get_options($complex_id)
- {
- if (! isset($this->options_cache) || ! isset($this->options_cache[$complex_id]))
- {
- $object = $this->get_question($complex_id);
- $options = $object->get_options();
- $opts = array();
- while ($option = $options->next_result())
- {
- $opts[$option->get_id()] = $this->transcode_string($option->get_value());
- }
- $this->options_cache[$complex_id] = $opts;
- }
-
- return $this->options_cache[$complex_id];
- }
-
- private function get_matches($complex_id)
- {
- if (! isset($this->matches_cache) || ! isset($this->matches_cache[$complex_id]))
- {
- $object = $this->get_question($complex_id);
- $matches = $object->get_matches();
- $mats = array();
- while ($match = $matches->next_result())
- {
- $mats[$match->get_id()] = $this->transcode_string($match->get_value());
- }
- $this->matches_cache[$complex_id] = $mats;
- }
-
- return $this->matches_cache[$complex_id];
- }
-
- public function get_context($context_id)
- {
- if (! isset($this->context_cach) || ! isset($this->context_cach[$context_id]))
- {
- $context = SurveyContextDataManager :: get_instance()->retrieve_survey_context_by_id($context_id);
- $this->context_cach[$context_id] = $context->get_name();
-
- }
- return $this->context_cach[$context_id];
- }
-
- function transcode_string($string)
- {
- $stripped_answer = trim(strip_tags(html_entity_decode($string, ENT_QUOTES, 'UTF-8')));
- $stripped_answer = str_replace(html_entity_decode(' ', ENT_COMPAT, 'UTF-8'), ' ', $stripped_answer);
- $stripped_answer = preg_replace('/[ \n\r\t]{2,}/', ' ', $stripped_answer);
- return $stripped_answer;
- }
-
- }
- ?>