PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/php/analyzer/percentage/percentage_analyzer.class.php

https://bitbucket.org/chamilo/chamilo-survey/
PHP | 388 lines | 284 code | 80 blank | 24 comment | 24 complexity | 3e45d9a93afeb2012f98c6de8dee9a6c MD5 | raw file
  1. <?php
  2. namespace repository\content_object\survey;
  3. use reporting\ReportingData;
  4. use repository\content_object\survey_matrix_question\SurveyMatrixQuestion;
  5. use repository\content_object\survey_multiple_choice_question\SurveyMultipleChoiceQuestion;
  6. use repository\content_object\survey_open_question\SurveyOpenQuestion;
  7. use common\libraries\Translation;
  8. use repository\content_object\survey_select_question\SurveySelectQuestion;
  9. class SurveyPercentageAnalyzer extends SurveyAnalyzer
  10. {
  11. const NO_ANSWER = 'noAnswer';
  12. const COUNT = 'percentage';
  13. const TOTAL = 'total';
  14. function analyse()
  15. {
  16. $question = $this->get_question();
  17. $type = $question->get_type();
  18. $answers = $this->get_answers();
  19. $reporting_data = new ReportingData();
  20. //option and matches of question
  21. $options = array();
  22. $matches = array();
  23. //matrix to store the answer count
  24. $answer_count = array();
  25. //reporting data and type of question
  26. $reporting_data = new ReportingData();
  27. $type = $question->get_type();
  28. switch ($type)
  29. {
  30. case SurveyMatrixQuestion :: get_type_name() :
  31. //get options and matches
  32. $opts = $question->get_options();
  33. while ($option = $opts->next_result())
  34. {
  35. $options[] = $option->get_value();
  36. }
  37. $matchs = $question->get_matches();
  38. while ($match = $matchs->next_result())
  39. {
  40. $matches[] = $match->get_value();
  41. }
  42. $total_key = count($matches);
  43. $matches[] = Translation :: get(self :: COUNT);
  44. //create answer matrix for answer counting
  45. $option_count = count($options) - 1;
  46. while ($option_count >= 0)
  47. {
  48. $match_count = count($matches) - 1;
  49. while ($match_count >= 0)
  50. {
  51. $answer_count[$option_count][$match_count] = 0;
  52. $match_count --;
  53. }
  54. // $answer_count[$option_count][$total_key] = 0;
  55. $option_count --;
  56. }
  57. //count answers from all answer trackers
  58. foreach ($answers as $answer)
  59. {
  60. $options_answered = array();
  61. foreach ($answer as $key => $option)
  62. {
  63. $options_answered[] = $key;
  64. $totals = array();
  65. foreach ($option as $match_key => $match)
  66. {
  67. if ($question->get_matrix_type() == SurveyMatrixQuestion :: MATRIX_TYPE_CHECKBOX)
  68. {
  69. $answer_count[$key][$match_key] ++;
  70. }
  71. else
  72. {
  73. $answer_count[$key][$match] ++;
  74. }
  75. $answer_count[$key][$total_key] ++;
  76. }
  77. }
  78. }
  79. //creating actual reporing data
  80. foreach ($matches as $match)
  81. {
  82. $reporting_data->add_row(strip_tags($match));
  83. }
  84. $totals = array();
  85. //percentage figures
  86. //total count
  87. foreach ($options as $option_key => $option)
  88. {
  89. foreach ($matches as $match_key => $match)
  90. {
  91. $totals[$match_key] = $totals[$match_key] + $answer_count[$option_key][$match_key];
  92. }
  93. }
  94. $total_colums = count($totals);
  95. $total_count = $totals[$total_colums - 1];
  96. $summary_totals = array();
  97. foreach ($totals as $index => $value)
  98. {
  99. if ($total_count == 0)
  100. {
  101. $summary_totals[$index] = 0;
  102. }
  103. else
  104. {
  105. $percentage = number_format($value / $total_count * 100, 2);
  106. $summary_totals[$index] = $percentage;
  107. }
  108. }
  109. $match_count = count($matches);
  110. $total_index = $match_count - 1;
  111. foreach ($options as $option_key => $option)
  112. {
  113. $reporting_data->add_category($option);
  114. // dump('op key: '.$option_key);
  115. // dump('option: '.$option);
  116. foreach ($matches as $match_key => $match)
  117. {
  118. if ($match_key == $total_index)
  119. {
  120. $value = $answer_count[$option_key][$match_key] / $total_count;
  121. $percentage = number_format($value * 100, 2);
  122. $reporting_data->add_data_category_row($option, strip_tags($match), $percentage);
  123. }
  124. else
  125. {
  126. $value = $answer_count[$option_key][$match_key] / $answer_count[$option_key][$total_index];
  127. $percentage = number_format($value * 100, 2);
  128. $reporting_data->add_data_category_row($option, strip_tags($match), $percentage);
  129. }
  130. }
  131. }
  132. if (count($options) > 1)
  133. {
  134. $reporting_data->add_category(Translation :: get(self :: TOTAL));
  135. foreach ($matches as $match_key => $match)
  136. {
  137. $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), $summary_totals[$match_key]);
  138. }
  139. }
  140. break;
  141. case SurveyMultipleChoiceQuestion :: get_type_name() :
  142. //get options and matches
  143. $opts = $question->get_options();
  144. while ($option = $opts->next_result())
  145. {
  146. $options[] = $option->get_value();
  147. }
  148. $matches[] = Translation :: get(self :: COUNT);
  149. //create answer matrix for answer counting
  150. $option_count = count($options) - 1;
  151. while ($option_count >= 0)
  152. {
  153. $answer_count[$option_count] = 0;
  154. $option_count --;
  155. }
  156. //count answers from all answer trackers
  157. foreach ($answers as $answer)
  158. {
  159. foreach ($answer as $key => $option)
  160. {
  161. if ($question->get_answer_type() == SurveyMultipleChoiceQuestion :: ANSWER_TYPE_CHECKBOX)
  162. {
  163. $option_id = array_pop(explode('_', $key));
  164. $answer_count[$option_id] ++;
  165. }
  166. else
  167. {
  168. $answer_count[$option] ++;
  169. }
  170. }
  171. }
  172. //totalcount
  173. $total_count = 0;
  174. foreach ($options as $option_key => $option)
  175. {
  176. foreach ($matches as $match)
  177. {
  178. $total_count = $total_count + $answer_count[$option_key];
  179. }
  180. }
  181. //creating actual reporing data
  182. foreach ($matches as $match)
  183. {
  184. $reporting_data->add_row(strip_tags($match));
  185. }
  186. foreach ($options as $option_key => $option)
  187. {
  188. $reporting_data->add_category($option);
  189. foreach ($matches as $match)
  190. {
  191. $value = $answer_count[$option_key] / $total_count;
  192. $percentage = number_format($value * 100, 2);
  193. $reporting_data->add_data_category_row($option, strip_tags($match), $percentage);
  194. }
  195. }
  196. if (count($options) > 1)
  197. {
  198. $reporting_data->add_category(Translation :: get(self :: TOTAL));
  199. foreach ($matches as $match)
  200. {
  201. $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), 100);
  202. }
  203. }
  204. break;
  205. case SurveyOpenQuestion :: get_type_name() :
  206. $reporting_data->add_category('answer');
  207. $stripped_answers = array();
  208. foreach ($answers as $answer)
  209. {
  210. // dump('hi');
  211. // dump($answer);
  212. $answer = array_values($answer);
  213. // dump($answer);
  214. if (strlen(strip_tags($answer[0], '<img>')) > 0)
  215. {
  216. $stripped_answers[] = $answer[0];
  217. }
  218. }
  219. $answer_count = count($stripped_answers);
  220. $categories = array();
  221. $nr = 0;
  222. while ($answer_count > 0)
  223. {
  224. $nr ++;
  225. $categories[] = $nr;
  226. $answer_count --;
  227. }
  228. $answer_row = Translation :: get('Answer');
  229. $rows = array($answer_row);
  230. $reporting_data->set_categories($categories);
  231. $reporting_data->set_rows($rows);
  232. $nr = 0;
  233. foreach ($stripped_answers as $answer)
  234. {
  235. $nr ++;
  236. $reporting_data->add_data_category_row($nr, $answer_row, $answer);
  237. }
  238. break;
  239. case SurveySelectQuestion :: get_type_name() :
  240. $opts = $question->get_options();
  241. while ($option = $opts->next_result())
  242. {
  243. $options[] = $option->get_value();
  244. }
  245. $matches[] = Translation :: get(self :: COUNT);
  246. //create answer matrix for answer counting
  247. $option_count = count($options) - 1;
  248. while ($option_count >= 0)
  249. {
  250. $answer_count[$option_count] = 0;
  251. $option_count --;
  252. }
  253. //count answers from all answer trackers
  254. foreach ($answers as $answer)
  255. {
  256. foreach ($answer as $key => $option)
  257. {
  258. if ($question->get_answer_type() == SurveyMultipleChoiceQuestion :: ANSWER_TYPE_CHECKBOX)
  259. {
  260. $answer_count[$key] ++;
  261. }
  262. else
  263. {
  264. $answer_count[$option] ++;
  265. }
  266. }
  267. }
  268. //totalcount
  269. $total_count = 0;
  270. foreach ($options as $option_key => $option)
  271. {
  272. foreach ($matches as $match)
  273. {
  274. $total_count = $total_count + $answer_count[$option_key];
  275. }
  276. }
  277. //creating actual reporing data
  278. foreach ($matches as $match)
  279. {
  280. $reporting_data->add_row(strip_tags($match));
  281. }
  282. foreach ($options as $option_key => $option)
  283. {
  284. $reporting_data->add_category($option);
  285. foreach ($matches as $match)
  286. {
  287. $value = $answer_count[$option_key] / $total_count;
  288. $percentage = number_format($value * 100, 2);
  289. $reporting_data->add_data_category_row($option, strip_tags($match), $percentage);
  290. }
  291. }
  292. if (count($options) > 1)
  293. {
  294. $reporting_data->add_category(Translation :: get(self :: TOTAL));
  295. foreach ($matches as $match)
  296. {
  297. $reporting_data->add_data_category_row(Translation :: get(self :: TOTAL), strip_tags($match), 100);
  298. }
  299. }
  300. break;
  301. default :
  302. ;
  303. break;
  304. }
  305. return $reporting_data;
  306. }
  307. }
  308. ?>