PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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