PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/export_manager/component/survey_excel_percentage_exporter.class.php

https://bitbucket.org/chamilo/chamilo-app-survey-dev/
PHP | 569 lines | 407 code | 130 blank | 32 comment | 20 complexity | 3e5c928c082d8baeddc887aa9dd9afd5 MD5 | raw file
  1. <?php
  2. namespace application\survey;
  3. use repository\content_object\survey\SurveyAnalyzer;
  4. use repository\content_object\survey\SurveyTemplateUser;
  5. use repository\content_object\survey\SurveyContextRelUser;
  6. use repository\content_object\survey\SurveyContextDataManager;
  7. use repository\content_object\survey\SurveyContext;
  8. use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
  9. use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;
  10. use tracking\Tracker;
  11. use reporting\ReportingData;
  12. use group\GroupDataManager;
  13. use common\libraries\AndCondition;
  14. use common\libraries\EqualityCondition;
  15. use common\libraries\InCondition;
  16. use common\libraries\Translation;
  17. use common\libraries\Utilities;
  18. use common\libraries\Request;
  19. use common\libraries\Path;
  20. use PHPExcel;
  21. use PHPExcel_Style_Alignment;
  22. use PHPExcel_IOFactory;
  23. ini_set("memory_limit", "-1");
  24. ini_set("max_execution_time", "0");
  25. require_once Path :: get_plugin_path() . 'phpexcel/PHPExcel.php';
  26. class SurveyExportManagerSurveyExcelPercentageExporterComponent extends SurveyExportManager
  27. {
  28. const COUNT = 'count';
  29. const TOTAL = 'total';
  30. const NOT_STARTED_PARTICIPANTS = 'not_started_participants';
  31. const STARTED_PARTICIPANTS = 'started_participants';
  32. const ALL_PARTICIPANTS = 'all_participants';
  33. const NOT_STARTED_PARTICIPANT_COUNT = 'not_started_participant_count';
  34. const STARTED_PARTICIPANT_COUNT = 'started_participant_count';
  35. const ALL_PARTICIPANT_COUNT = 'all_participant_count';
  36. const CONTEXTS = 'groups';
  37. const CONTEXT_NAME = 'group_name';
  38. const CONTEXT_DESCRIPTION = 'group_description';
  39. const INDIVIDUAL_USERS = 'individual_users';
  40. const USERS = 'users';
  41. const PARTICIPATION_GRADE = 'participation_grade';
  42. const SURVEYS = 'surveys';
  43. const SURVEY_NAME = 'survey_name';
  44. const SURVEY_DESCRIPTION = 'survey_description';
  45. const SURVEY_COUNT = 'survey_count';
  46. const REPORTING_DATA = 'reporting_data';
  47. const DATA_NAME = 'data_name';
  48. const DATA_DESCRIPTION = 'data_description';
  49. const DATA_GROUP = 'data_group';
  50. private $participants;
  51. private $surveys;
  52. /**
  53. * Runs this component and displays its output.
  54. */
  55. function run()
  56. {
  57. $id = Request :: get(SurveyManager :: PARAM_PUBLICATION_ID);
  58. // if (! is_array($ids))
  59. // {
  60. // $ids = array($ids);
  61. // }
  62. $this->create_participants($id);
  63. $this->render_data();
  64. }
  65. public function render_data()
  66. {
  67. $excel = new PHPExcel();
  68. $worksheet = $excel->getSheet(0)->setTitle('Algemeen');
  69. $this->render_summary_data($worksheet);
  70. $complex_questions = $this->get_questions();
  71. $worksheet_index = 1;
  72. foreach ($complex_questions as $complex_question_id => $complex_question)
  73. {
  74. $question = $complex_question->get_ref_object();
  75. $title = Utilities :: truncate_string(trim(strip_tags($question->get_title())), 15, true, '');
  76. $worksheet = $excel->createSheet($worksheet_index)->setTitle($title);
  77. $worksheet = $excel->getSheet($worksheet_index);
  78. $page_reporting_data = $this->create_page_reporting_data($complex_question, $question);
  79. $this->render_page_data($worksheet, $page_reporting_data);
  80. $worksheet_index ++;
  81. }
  82. exit();
  83. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  84. header('Content-Disposition: attachment;filename="' . 'survey_export' . '.xlsx"');
  85. header('Cache-Control: max-age=0');
  86. $objWriter = PHPExcel_IOFactory :: createWriter($excel, 'Excel2007');
  87. return $objWriter->save('php://output');
  88. }
  89. private function get_questions()
  90. {
  91. $page_questions = array();
  92. $surveys = $this->surveys;
  93. foreach ($surveys as $survey)
  94. {
  95. $complex_questions = $survey->get_complex_questions();
  96. }
  97. return $complex_questions;
  98. }
  99. private function render_summary_data($worksheet)
  100. {
  101. $column = 1;
  102. $row = 3;
  103. $worksheet->getColumnDimensionByColumn($column)->setWidth(20);
  104. $worksheet->getColumnDimensionByColumn($column + 1)->setWidth(20);
  105. $worksheet->getColumnDimensionByColumn($column + 2)->setWidth(20);
  106. $worksheet->getColumnDimensionByColumn($column + 3)->setWidth(20);
  107. $worksheet->getColumnDimensionByColumn($column + 4)->setWidth(5);
  108. $worksheet->getColumnDimensionByColumn($column + 5)->setWidth(200);
  109. $surveys = $this->participants[self :: SURVEYS];
  110. $worksheet->setCellValueByColumnAndRow($column, $row, Translation :: get('SurveyName'));
  111. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  112. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  113. $worksheet->setCellValueByColumnAndRow($column + 1, $row, Translation :: get('SurveyDescription'));
  114. $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  115. $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
  116. $row ++;
  117. foreach ($surveys as $survey)
  118. {
  119. $title = $survey[self :: SURVEY_NAME];
  120. $worksheet->setCellValueByColumnAndRow($column, $row, $title);
  121. $this->wrap_text($worksheet, $column, $row);
  122. $description = $survey[self :: SURVEY_DESCRIPTION];
  123. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $description);
  124. $this->wrap_text($worksheet, $column, $row);
  125. $row ++;
  126. }
  127. $row = $row + 2;
  128. $all_participants = $this->participants[self :: ALL_PARTICIPANT_COUNT];
  129. $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
  130. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  131. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  132. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $all_participants);
  133. $row ++;
  134. $started = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
  135. $worksheet->setCellValueByColumnAndRow($column, $row, 'Deelgenomen');
  136. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  137. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  138. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started);
  139. $row ++;
  140. $not_started = $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT];
  141. $worksheet->setCellValueByColumnAndRow($column, $row, 'Niet deelgenomen');
  142. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  143. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  144. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $not_started);
  145. $row ++;
  146. $participatie = $this->participants[self :: PARTICIPATION_GRADE];
  147. $worksheet->setCellValueByColumnAndRow($column, $row, 'Participatigraad (%)');
  148. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  149. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  150. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $participatie);
  151. $row = $row + 2;
  152. $worksheet->setCellValueByColumnAndRow($column, $row, 'Aantal participanten');
  153. $worksheet->getStyleByColumnAndRow($column, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  154. $worksheet->getStyleByColumnAndRow($column, $row)->getFont()->setBold(true);
  155. $worksheet->setCellValueByColumnAndRow($column + 1, $row, 'Deelgenomen');
  156. $worksheet->getStyleByColumnAndRow($column + 1, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  157. $worksheet->getStyleByColumnAndRow($column + 1, $row)->getFont()->setBold(true);
  158. $worksheet->setCellValueByColumnAndRow($column + 2, $row, 'Niet deelgenomen');
  159. $worksheet->getStyleByColumnAndRow($column + 2, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  160. $worksheet->getStyleByColumnAndRow($column + 2, $row)->getFont()->setBold(true);
  161. $worksheet->setCellValueByColumnAndRow($column + 3, $row, 'Participatie (%)');
  162. $worksheet->getStyleByColumnAndRow($column + 3, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  163. $worksheet->getStyleByColumnAndRow($column + 3, $row)->getFont()->setBold(true);
  164. $worksheet->setCellValueByColumnAndRow($column + 5, $row, 'Groepen');
  165. $worksheet->getStyleByColumnAndRow($column + 5, $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  166. $worksheet->getStyleByColumnAndRow($column + 5, $row)->getFont()->setBold(true);
  167. $row = $row + 2;
  168. $groups = $this->participants[self :: GROUPS];
  169. foreach ($groups as $group_id => $group)
  170. {
  171. // $name = $this->participants[self :: GROUPS][$group_id][self :: GROUP_NAME];
  172. $all_participant_count = $group[self :: ALL_PARTICIPANT_COUNT];
  173. $worksheet->setCellValueByColumnAndRow($column, $row, $all_participant_count);
  174. $started_participant_count = $group[self :: STARTED_PARTICIPANT_COUNT];
  175. $worksheet->setCellValueByColumnAndRow($column + 1, $row, $started_participant_count);
  176. $not_started_participant_count = $group[self :: NOT_STARTED_PARTICIPANT_COUNT];
  177. $worksheet->setCellValueByColumnAndRow($column + 2, $row, $not_started_participant_count);
  178. $participatie = $group[self :: PARTICIPATION_GRADE];
  179. $worksheet->setCellValueByColumnAndRow($column + 3, $row, $participatie);
  180. $description = $group[self :: GROUP_DESCRIPTION];
  181. $worksheet->setCellValueByColumnAndRow($column + 5, $row, $description);
  182. $row ++;
  183. }
  184. }
  185. private function render_page_data($worksheet, $data)
  186. {
  187. $column = 0;
  188. $block_row = 0;
  189. $worksheet->getColumnDimensionByColumn($column)->setWidth(50);
  190. $column_count = 1;
  191. while ($column_count < 7)
  192. {
  193. $worksheet->getColumnDimensionByColumn($column + $column_count)->setWidth(15);
  194. $column_count ++;
  195. }
  196. if (is_array($data))
  197. {
  198. foreach ($data as $block_data)
  199. {
  200. $column = 0;
  201. $block_row = $block_row + 2;
  202. $participant_group = $block_data[self :: DATA_GROUP];
  203. $participant_count = $block_data[self :: STARTED_PARTICIPANT_COUNT];
  204. $block_title = trim(html_entity_decode(strip_tags($block_data[self :: DATA_NAME])));
  205. $block_description = trim(html_entity_decode(strip_tags($block_data[self :: DATA_DESCRIPTION])));
  206. $block_content_data = $block_data[self :: REPORTING_DATA];
  207. $worksheet->setCellValueByColumnAndRow($column, $block_row, $participant_group);
  208. $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  209. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  210. $this->wrap_text($worksheet, $column, $block_row);
  211. $block_row ++;
  212. $worksheet->setCellValueByColumnAndRow($column, $block_row, 'Deelnemers');
  213. $worksheet->setCellValueByColumnAndRow($column + 1, $block_row, $participant_count);
  214. $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  215. $worksheet->getStyleByColumnAndRow($column + 1, $block_row)->getFont()->setBold(true);
  216. $block_row = $block_row + 2;
  217. $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_title);
  218. $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  219. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  220. $this->wrap_text($worksheet, $column, $block_row);
  221. if ($block_description != '')
  222. {
  223. $block_row ++;
  224. $worksheet->setCellValueByColumnAndRow($column, $block_row, $block_description);
  225. $this->wrap_text($worksheet, $column, $block_row);
  226. $block_row ++;
  227. }
  228. $block_row ++;
  229. foreach ($block_content_data->get_rows() as $row_id => $row_name)
  230. {
  231. // dump($row_name);
  232. $column ++;
  233. $worksheet->getColumnDimensionByColumn($column)->setAutoSize(true);
  234. $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
  235. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  236. $worksheet->setCellValueByColumnAndRow($column, $block_row, $this->transcode_string($row_name));
  237. $this->wrap_text($worksheet, $column, $block_row);
  238. }
  239. $block_row ++;
  240. $row_count = count($block_content_data->get_rows());
  241. $category_count = count($block_content_data->get_categories());
  242. $categrory_row_index = 1;
  243. // dump('row count: ' . $row_count);
  244. // dump('cat count: ' . $category_count);
  245. foreach ($block_content_data->get_categories() as $category_id => $category_name)
  246. {
  247. $column = 0;
  248. $worksheet->setCellValueByColumnAndRow($column, $block_row, $this->transcode_string($category_name));
  249. $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_LEFT);
  250. $this->wrap_text($worksheet, $column, $block_row);
  251. // dump('category row index: ' . $categrory_row_index);
  252. // dump('cat row: ' . $block_row);
  253. //
  254. if ($categrory_row_index == $category_count && $category_count != 1)
  255. {
  256. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  257. }
  258. $row_index = 1;
  259. // dump('row index: ' . $row_index);
  260. foreach ($block_content_data->get_rows() as $row_id => $row_name)
  261. {
  262. $column ++;
  263. $value = $block_content_data->get_data_category_row($category_id, $row_id);
  264. $worksheet->setCellValueByColumnAndRow($column, $block_row, $this->transcode_string($value));
  265. $worksheet->getStyleByColumnAndRow($column, $block_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
  266. // dump('row index: ' . $row_index);
  267. // dump('row: ' . $block_row);
  268. if ($row_index == $row_count && $row_count != 1)
  269. {
  270. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  271. }
  272. if ($categrory_row_index == $category_count && $category_count != 1)
  273. {
  274. $worksheet->getStyleByColumnAndRow($column, $block_row)->getFont()->setBold(true);
  275. }
  276. $row_index ++;
  277. }
  278. $categrory_row_index ++;
  279. $block_row ++;
  280. }
  281. // exit();
  282. }
  283. }
  284. }
  285. private function wrap_text($worksheet, $colum, $row)
  286. {
  287. $worksheet->getStyleByColumnAndRow($colum, $row)->getAlignment()->setWrapText(true);
  288. }
  289. private function create_page_reporting_data($complex_question, $question)
  290. {
  291. $page_reporting_data = array();
  292. $all_participants_ids = $this->participants[self :: ALL_PARTICIPANTS];
  293. // $reporting_data = $this->create_reporting_data($complex_question, $question, $all_participants_ids);
  294. $analyzer = SurveyAnalyzer :: factory(SurveyAnalyzer :: TYPE_PERCENTAGE, $question, $all_participants_ids[$complex_question->get_id()]);
  295. $reporting_data = $analyzer->analyse();
  296. $reporting_data_question = array();
  297. $reporting_data_question[self :: DATA_GROUP] = Translation :: get('AllGroups');
  298. $reporting_data_question[self :: DATA_NAME] = $question->get_title();
  299. $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
  300. $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $this->participants[self :: STARTED_PARTICIPANT_COUNT];
  301. $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
  302. $page_reporting_data[] = $reporting_data_question;
  303. $groups = $this->participants[self :: GROUPS];
  304. foreach ($groups as $group)
  305. {
  306. $reporting_data_question = array();
  307. $reporting_data_question[self :: DATA_GROUP] = $group[self :: GROUP_DESCRIPTION];
  308. $reporting_data_question[self :: DATA_NAME] = $question->get_title();
  309. $reporting_data_question[self :: DATA_DESCRIPTION] = $question->get_description();
  310. $reporting_data_question[self :: STARTED_PARTICIPANT_COUNT] = $group[self :: STARTED_PARTICIPANT_COUNT];
  311. $all_participants_ids = $group[self :: ALL_PARTICIPANTS];
  312. // $reporting_data = $this->create_reporting_data($complex_question, $question, $all_participants_ids);
  313. $analyzer = SurveyAnalyzer :: factory(SurveyAnalyzer :: TYPE_PERCENTAGE, $question, $all_participants_ids[$complex_question->get_id()]);
  314. $reporting_data = $analyzer->analyse();
  315. $reporting_data_question[self :: REPORTING_DATA] = $reporting_data;
  316. $page_reporting_data[] = $reporting_data_question;
  317. }
  318. return $page_reporting_data;
  319. }
  320. private function create_participants($id)
  321. {
  322. $this->participants = array();
  323. $this->surveys = array();
  324. $surveys = array();
  325. $sv = array();
  326. $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($id);
  327. $survey = $survey_publication->get_publication_object();
  328. $template_type = $survey->get_context_template()->get_type();
  329. $this->surveys[] = $survey;
  330. $survey_title = $survey->get_title();
  331. $survey_description = $survey->get_description();
  332. $sv[self :: SURVEY_NAME] = Utilities :: truncate_string(trim(strip_tags($survey_title)), 20, true, '');
  333. $sv[self :: SURVEY_DESCRIPTION] = Utilities :: truncate_string(trim(strip_tags($survey_description)), 20, true, '');
  334. $surveys[$id] = $sv;
  335. $this->participants[self :: SURVEYS] = $surveys;
  336. $this->participants[self :: SURVEY_COUNT] = count($surveys);
  337. $invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_PARTICIPATE, $id, SurveyRights :: TYPE_PUBLICATION);
  338. $invitees = array_unique($invitees);
  339. $invitee_count = count($invitees);
  340. $started = array();
  341. $finished = array();
  342. $not_started = array();
  343. // $condition = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $id);
  344. //
  345. // $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  346. //
  347. $all_participants = array();
  348. $all_user_ids = array();
  349. // while ($tracker = $trackers->next_result())
  350. // {
  351. // $all_participants[] = $tracker->get_id();
  352. // $all_user_ids[]= $tracker->get_user_id();
  353. //
  354. // }
  355. $context_ids = array();
  356. $condition = new InCondition(SurveyQuestionAnswerTracker :: PROPERTY_PUBLICATION_ID, $id);
  357. $answer_trackers = Tracker :: get_data(SurveyQuestionAnswerTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  358. $context_user_ids = array();
  359. $answer_tracker_ids = array();
  360. while ($answer_tracker = $answer_trackers->next_result())
  361. {
  362. $context_id = $answer_tracker->get_context_id();
  363. $user_id = $answer_tracker->get_user_id();
  364. $context_user_ids[$context_id][] = $user_id;
  365. $context_ids[] = $context_id;
  366. $all_user_ids[] = $user_id;
  367. $all_participants[$answer_tracker->get_question_cid()][] = $answer_tracker->get_answer();
  368. $answer_tracker_ids[$context_id][$answer_tracker->get_question_cid()][] = $answer_tracker->get_answer();
  369. }
  370. $context_ids = array_unique($context_ids);
  371. $all_user_ids = array_unique($all_user_ids);
  372. $all_user_count = count($all_user_ids);
  373. $not_started = array_diff($invitees, $all_user_ids);
  374. $not_started_count = count(array_unique($not_started));
  375. $this->participants[self :: ALL_PARTICIPANTS] = $all_participants;
  376. $this->participants[self :: ALL_PARTICIPANT_COUNT] = $invitee_count;
  377. $this->participants[self :: NOT_STARTED_PARTICIPANT_COUNT] = $not_started_count;
  378. $this->participants[self :: STARTED_PARTICIPANT_COUNT] = $all_user_count;
  379. $participatie = $all_user_count / $invitee_count * 100;
  380. $participatie = number_format($participatie, 2);
  381. $this->participants[self :: PARTICIPATION_GRADE] = $participatie;
  382. $condition = new InCondition(SurveyContext :: PROPERTY_ID, $context_ids);
  383. $context = SurveyContextDataManager :: get_instance()->retrieve_survey_context_by_id($context_ids[0]);
  384. $type = $context->get_type();
  385. $groups = SurveyContextDataManager :: get_instance()->retrieve_survey_contexts($type, $condition);
  386. $all_users = array();
  387. $conditions = array();
  388. $tempate_user_alias = SurveyContextDataManager :: get_instance()->get_alias($template_type);
  389. $condition = new EqualityCondition(SurveyTemplateUser :: PROPERTY_TYPE, $template_type, SurveyTemplateUser :: get_table_name());
  390. $template_users = SurveyContextDataManager :: get_instance()->retrieve_survey_template_users($template_type, $condition);
  391. while ($template_user = $template_users->next_result())
  392. {
  393. $property_names = $template_user->get_additional_property_names();
  394. foreach ($property_names as $name)
  395. {
  396. $group_id = $template_user->get_additional_property($name);
  397. $all_users[$group_id][] = $template_user->get_user_id();
  398. }
  399. }
  400. while ($group = $groups->next_result())
  401. {
  402. $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_NAME] = $group->get_name();
  403. $this->participants[self :: GROUPS][$group->get_id()][self :: GROUP_DESCRIPTION] = $group->get_name();
  404. $all_group_users = array_unique($all_users[$group->get_id()]);
  405. $all_group_user_count = count($all_group_users);
  406. $group_users = $context_user_ids[$group->get_id()];
  407. $group_users = array_unique($group_users);
  408. $group_user_count = count($group_users);
  409. $conditions = array();
  410. $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_USER_ID, $group_users);
  411. $conditions[] = new InCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $id);
  412. $condition = new AndCondition($conditions);
  413. $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
  414. $all_trackers = array();
  415. while ($tracker = $trackers->next_result())
  416. {
  417. $all_trackers[] = $tracker->get_id();
  418. }
  419. $all_tracker_count = count($all_trackers);
  420. $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANTS] = $answer_tracker_ids[$group->get_id()];
  421. $participatie = $group_user_count / $all_group_user_count * 100;
  422. $participatie = number_format($participatie, 2);
  423. $this->participants[self :: GROUPS][$group->get_id()][self :: PARTICIPATION_GRADE] = $participatie;
  424. $this->participants[self :: GROUPS][$group->get_id()][self :: ALL_PARTICIPANT_COUNT] = $all_group_user_count;
  425. $this->participants[self :: GROUPS][$group->get_id()][self :: STARTED_PARTICIPANT_COUNT] = $group_user_count;
  426. $this->participants[self :: GROUPS][$group->get_id()][self :: NOT_STARTED_PARTICIPANT_COUNT] = $all_group_user_count - $group_user_count;
  427. }
  428. // dump($this->participants);
  429. // exit;
  430. }
  431. function transcode_string($string)
  432. {
  433. $stripped_answer = trim(strip_tags(html_entity_decode($string, ENT_QUOTES, 'UTF-8')));
  434. $stripped_answer = str_replace(html_entity_decode('&nbsp;', ENT_COMPAT, 'UTF-8'), ' ', $stripped_answer);
  435. $stripped_answer = preg_replace('/[ \n\r\t]{2,}/', ' ', $stripped_answer);
  436. return $stripped_answer;
  437. }
  438. }
  439. ?>