PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/export_manager/component/survey_raw_excel_exporter.class.php

https://bitbucket.org/chamilo/chamilo-app-survey-dev/
PHP | 384 lines | 304 code | 74 blank | 6 comment | 27 complexity | 301fd9385f60079154f1880bb4140371 MD5 | raw file
  1. <?php
  2. namespace application\survey;
  3. use repository\content_object\survey_rating_question\SurveyRatingQuestion;
  4. use repository\content_object\survey_select_question\SurveySelectQuestion;
  5. use repository\content_object\survey_matching_question\SurveyMatchingQuestion;
  6. use repository\content_object\survey_description\SurveyDescription;
  7. use repository\content_object\survey\SurveyAnalyzer;
  8. use repository\content_object\survey\SurveyTemplateUser;
  9. use repository\content_object\survey\SurveyContextRelUser;
  10. use repository\content_object\survey\SurveyContextDataManager;
  11. use repository\content_object\survey\SurveyContext;
  12. use repository\content_object\survey\Survey;
  13. use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
  14. use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;
  15. use repository\content_object\survey_open_question\SurveyOpenQuestion;
  16. use repository\RepositoryDataManager;
  17. use tracking\Tracker;
  18. use tracking\Event;
  19. use reporting\ReportingData;
  20. use group\GroupDataManager;
  21. use common\libraries\AndCondition;
  22. use common\libraries\EqualityCondition;
  23. use common\libraries\InCondition;
  24. use common\libraries\Translation;
  25. use common\libraries\Utilities;
  26. use common\libraries\Request;
  27. use common\libraries\Path;
  28. use PHPExcel;
  29. use PHPExcel_Style_Alignment;
  30. use PHPExcel_Style_Fill;
  31. use PHPExcel_IOFactory;
  32. use PHPExcel_Style_Font;
  33. use PHPExcel_Style_Color;
  34. ini_set("memory_limit", "-1");
  35. ini_set("max_execution_time", "0");
  36. require_once Path :: get_plugin_path() . 'phpexcel/PHPExcel.php';
  37. class SurveyExportManagerSurveyRawExcelExporterComponent extends SurveyExportManager
  38. {
  39. private $questions_cache;
  40. private $options_cache;
  41. private $matches_cache;
  42. private $context_cache;
  43. private $survey_cache;
  44. private $publication_cache;
  45. private $answer_array;
  46. /**
  47. * Runs this component and displays its output.
  48. */
  49. function run()
  50. {
  51. $ids = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
  52. if (! empty($ids))
  53. {
  54. if (! is_array($ids))
  55. {
  56. $ids = array($ids);
  57. }
  58. $this->create_answer_array($ids);
  59. // dump($this->answer_array);
  60. // exit;
  61. }
  62. $this->render_data();
  63. }
  64. public function render_data()
  65. {
  66. $excel = new PHPExcel();
  67. $worksheet_index = 0;
  68. if (isset($this->user_answers["error"]))
  69. {
  70. $worksheet = $excel->createSheet($worksheet_index)->setTitle("error");
  71. $worksheet->setCellValueByColumnAndRow(1, 1, "Different context templates used in selected surveys");
  72. }
  73. else
  74. {
  75. foreach ($this->answer_array as $level => $levels)
  76. {
  77. foreach ($levels as $publication_id => $publications)
  78. {
  79. $context_template = $this->survey_cache[$publication_id]->get_context_template_for_level($level);
  80. $context_template_name = Utilities :: truncate_string($context_template->get_context_type_name(), 30);
  81. $worksheet = null;
  82. $worksheet = $excel->getSheetByName($context_template_name);
  83. if (! isset($worksheet))
  84. {
  85. $worksheet = $excel->createSheet($worksheet_index)->setTitle($context_template_name);
  86. $worksheet_index ++;
  87. $row = 1;
  88. }
  89. foreach ($publications as $context_path => $case)
  90. {
  91. foreach ($case as $user_id => $fullpaths)
  92. {
  93. foreach ($fullpaths as $fullpath => $answers)
  94. {
  95. foreach ($answers as $question => $all_answers)
  96. {
  97. foreach ($all_answers as $answer)
  98. {
  99. $column = 0;
  100. if ($row == 1)
  101. {
  102. $worksheet->setCellValueByColumnAndRow($column ++, $row, "Case");
  103. $worksheet->setCellValueByColumnAndRow($column ++, $row, "Publication");
  104. if ($context_path != 0)
  105. {
  106. $c_ids = explode("_", $context_path);
  107. $context_level = count($c_ids);
  108. foreach ($c_ids as $c_id)
  109. {
  110. $c_template = $this->survey_cache[$publication_id]->get_context_template_for_level($context_level);
  111. $worksheet->setCellValueByColumnAndRow($column ++, $row, $c_template->get_context_type_name());
  112. $context_level --;
  113. }
  114. }
  115. $worksheet->setCellValueByColumnAndRow($column ++, $row, "Vraag");
  116. $worksheet->setCellValueByColumnAndRow($column ++, $row, "Antwoord");
  117. }
  118. else
  119. {
  120. $worksheet->setCellValueByColumnAndRow($column ++, $row, $user_id);
  121. $worksheet->setCellValueByColumnAndRow($column ++, $row, $this->publication_cache[$publication_id]);
  122. if ($context_path != 0)
  123. {
  124. $c_ids = explode("_", $context_path);
  125. foreach ($c_ids as $c_id)
  126. {
  127. $worksheet->setCellValueByColumnAndRow($column ++, $row, $this->get_context($c_id));
  128. }
  129. }
  130. $worksheet->setCellValueByColumnAndRow($column ++, $row, $question);
  131. $worksheet->setCellValueByColumnAndRow($column ++, $row, $answer);
  132. }
  133. $row ++;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // exit;
  143. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  144. header('Content-Disposition: attachment;filename="' . 'survey_export' . '.xlsx"');
  145. header('Cache-Control: max-age=0');
  146. $objWriter = PHPExcel_IOFactory :: createWriter($excel, 'Excel2007');
  147. return $objWriter->save('php://output');
  148. }
  149. function create_answer_array($ids)
  150. {
  151. $this->publication_cache = array();
  152. $context_template_ids = array();
  153. foreach ($ids as $id)
  154. {
  155. $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
  156. $survey = $survey_publication->get_publication_object();
  157. $this->survey_cache[$id] = $survey;
  158. $this->publication_cache[$id] = $survey_publication->get_title();
  159. $context_template_ids[] = $survey->get_context_template_id();
  160. }
  161. $context_template_ids = array_unique($context_template_ids);
  162. $this->answer_array = array();
  163. if (count($context_template_ids) > 1)
  164. {
  165. $this->answer_array["error"] = $context_template_ids;
  166. }
  167. else
  168. {
  169. $levels = $survey->count_levels();
  170. $condition = new InCondition(SurveyMultipleChoiceQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
  171. $trackers = Tracker :: get_data(SurveyMultipleChoiceQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  172. while ($tracker = $trackers->next_result())
  173. {
  174. $user_id = $tracker->get_user_id();
  175. $context_id = $tracker->get_context_id();
  176. $complex_question_id = $tracker->get_complex_question_id();
  177. $question = $this->transcode_string($this->get_question($complex_question_id)->get_title());
  178. $question = $complex_question_id . ' ' . $question;
  179. $publication_id = $tracker->get_publication_id();
  180. $options = $this->get_options($complex_question_id);
  181. $answer = $options[$tracker->get_option_id()];
  182. $full_path = $tracker->get_context_path();
  183. $full_path_parts = explode('|', $full_path);
  184. $path = $full_path_parts[1];
  185. $path_ids = explode("_", $path);
  186. $level_count = count($path_ids);
  187. while ($level_count > 0)
  188. {
  189. $reverted_path_ids = array_reverse($path_ids);
  190. $path = (string) implode(array_reverse($reverted_path_ids), '_');
  191. $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$question][] = $answer;
  192. array_pop($path_ids);
  193. $level_count = count($path_ids);
  194. }
  195. }
  196. $condition = new InCondition(SurveyMatrixQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
  197. $trackers = Tracker :: get_data(SurveyMatrixQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  198. while ($tracker = $trackers->next_result())
  199. {
  200. $user_id = $tracker->get_user_id();
  201. $context_id = $tracker->get_context_id();
  202. $complex_question_id = $tracker->get_complex_question_id();
  203. $publication_id = $tracker->get_publication_id();
  204. $question = $this->transcode_string($this->get_question($complex_question_id)->get_title());
  205. $options = $this->get_options($complex_question_id);
  206. $option = $options[$tracker->get_option_id()];
  207. $option = $complex_question_id . ' ' . $question . ' : ' . $option;
  208. $matches = $this->get_matches($complex_question_id);
  209. $answer = $matches[$tracker->get_match_id()];
  210. $full_path = $tracker->get_context_path();
  211. $full_path_parts = explode('|', $full_path);
  212. $path = $full_path_parts[1];
  213. $path_ids = explode("_", $path);
  214. $reverted_path_ids = array_reverse($path_ids);
  215. $level_count = count($path_ids);
  216. while ($level_count > 0)
  217. {
  218. $reverted_path_ids = array_reverse($path_ids);
  219. $path = (string) implode(array_reverse($reverted_path_ids), '_');
  220. $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$option][] = $answer;
  221. array_pop($path_ids);
  222. $level_count = count($path_ids);
  223. }
  224. }
  225. $condition = new InCondition(SurveyOpenQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $ids);
  226. $trackers = Tracker :: get_data(SurveyOpenQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  227. while ($tracker = $trackers->next_result())
  228. {
  229. $user_id = $tracker->get_user_id();
  230. $context_id = $tracker->get_context_id();
  231. $publication_id = $tracker->get_publication_id();
  232. $complex_question_id = $tracker->get_complex_question_id();
  233. $question = $this->get_question($complex_question_id);
  234. $title = $this->transcode_string($question->get_title());
  235. $title = $complex_question_id . ' ' . $title;
  236. $answer = $tracker->get_text();
  237. $full_path = $tracker->get_context_path();
  238. $full_path_parts = explode('|', $full_path);
  239. $path = $full_path_parts[1];
  240. $path_ids = explode("_", $path);
  241. $reverted_path_ids = array_reverse($path_ids);
  242. $level_count = count($path_ids);
  243. while ($level_count > 0)
  244. {
  245. $reverted_path_ids = array_reverse($path_ids);
  246. $path = (string) implode(array_reverse($reverted_path_ids), '_');
  247. $this->answer_array[$level_count][$publication_id][$path][$user_id][$full_path][$title][] = $answer;
  248. array_pop($path_ids);
  249. $level_count = count($path_ids);
  250. }
  251. }
  252. }
  253. }
  254. private function get_question($complex_id)
  255. {
  256. if (! isset($this->questions_cache) || ! isset($this->questions_cache[$complex_id]))
  257. {
  258. $complex_question = RepositoryDataManager :: get_instance()->retrieve_complex_content_object_item($complex_id);
  259. $this->questions_cache[$complex_id] = $complex_question->get_ref_object();
  260. }
  261. return $this->questions_cache[$complex_id];
  262. }
  263. private function get_options($complex_id)
  264. {
  265. if (! isset($this->options_cache) || ! isset($this->options_cache[$complex_id]))
  266. {
  267. $object = $this->get_question($complex_id);
  268. $options = $object->get_options();
  269. $opts = array();
  270. while ($option = $options->next_result())
  271. {
  272. $opts[$option->get_id()] = $this->transcode_string($option->get_value());
  273. }
  274. $this->options_cache[$complex_id] = $opts;
  275. }
  276. return $this->options_cache[$complex_id];
  277. }
  278. private function get_matches($complex_id)
  279. {
  280. if (! isset($this->matches_cache) || ! isset($this->matches_cache[$complex_id]))
  281. {
  282. $object = $this->get_question($complex_id);
  283. $matches = $object->get_matches();
  284. $mats = array();
  285. while ($match = $matches->next_result())
  286. {
  287. $mats[$match->get_id()] = $this->transcode_string($match->get_value());
  288. }
  289. $this->matches_cache[$complex_id] = $mats;
  290. }
  291. return $this->matches_cache[$complex_id];
  292. }
  293. public function get_context($context_id)
  294. {
  295. if (! isset($this->context_cach) || ! isset($this->context_cach[$context_id]))
  296. {
  297. $context = SurveyContextDataManager :: get_instance()->retrieve_survey_context_by_id($context_id);
  298. $this->context_cach[$context_id] = $context->get_name();
  299. }
  300. return $this->context_cach[$context_id];
  301. }
  302. function transcode_string($string)
  303. {
  304. $stripped_answer = trim(strip_tags(html_entity_decode($string, ENT_QUOTES, 'UTF-8')));
  305. $stripped_answer = str_replace(html_entity_decode('&nbsp;', ENT_COMPAT, 'UTF-8'), ' ', $stripped_answer);
  306. $stripped_answer = preg_replace('/[ \n\r\t]{2,}/', ' ', $stripped_answer);
  307. return $stripped_answer;
  308. }
  309. }
  310. ?>