stats.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /* See license terms in /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $this_section = SECTION_COURSES;
  5. api_protect_course_script(true, false, true);
  6. $showPage = false;
  7. if (api_is_platform_admin() || api_is_course_admin() ||
  8. api_is_course_tutor() || api_is_session_general_coach() || api_is_allowed_to_edit(null, true)
  9. ) {
  10. $showPage = true;
  11. }
  12. if (!$showPage) {
  13. api_not_allowed(true);
  14. }
  15. $exerciseId = isset($_GET['exerciseId']) && !empty($_GET['exerciseId']) ? (int) $_GET['exerciseId'] : 0;
  16. $objExercise = new Exercise();
  17. $result = $objExercise->read($exerciseId);
  18. if (!$result) {
  19. api_not_allowed(true);
  20. }
  21. $sessionId = api_get_session_id();
  22. $courseCode = api_get_course_id();
  23. if (empty($sessionId)) {
  24. $students = CourseManager:: get_student_list_from_course_code(
  25. $courseCode,
  26. false
  27. );
  28. } else {
  29. $students = CourseManager:: get_student_list_from_course_code(
  30. $courseCode,
  31. true,
  32. $sessionId
  33. );
  34. }
  35. $count_students = count($students);
  36. $question_list = $objExercise->get_validated_question_list();
  37. $data = [];
  38. // Question title # of students who tool it Lowest score Average Highest score Maximum score
  39. $headers = [
  40. get_lang('Question'),
  41. get_lang('Question type'),
  42. get_lang('Number of learners who selected it'),
  43. get_lang('Lowest score'),
  44. get_lang('Average score'),
  45. get_lang('Highest score'),
  46. get_lang('Score'),
  47. ];
  48. if (!empty($question_list)) {
  49. foreach ($question_list as $question_id) {
  50. $questionObj = Question::read($question_id);
  51. $exercise_stats = ExerciseLib::get_student_stats_by_question(
  52. $question_id,
  53. $exerciseId,
  54. $courseCode,
  55. $sessionId
  56. );
  57. $count_users = ExerciseLib::get_number_students_question_with_answer_count(
  58. $question_id,
  59. $exerciseId,
  60. $courseCode,
  61. $sessionId,
  62. $questionObj->type
  63. );
  64. $data[$question_id]['name'] = cut($questionObj->question, 100);
  65. $data[$question_id]['type'] = $questionObj->get_question_type_name();
  66. $percentage = 0;
  67. if ($count_students) {
  68. $percentage = $count_users / $count_students * 100;
  69. }
  70. $data[$question_id]['students_who_try_exercise'] = Display::bar_progress(
  71. $percentage,
  72. false,
  73. $count_users.' / '.$count_students
  74. );
  75. $data[$question_id]['lowest_score'] = round($exercise_stats['min'], 2);
  76. $data[$question_id]['average_score'] = round($exercise_stats['average'], 2);
  77. $data[$question_id]['highest_score'] = round($exercise_stats['max'], 2);
  78. $data[$question_id]['max_score'] = round($questionObj->weighting, 2);
  79. }
  80. }
  81. // Format A table
  82. $table = new HTML_Table(['class' => 'data_table']);
  83. $row = 0;
  84. $column = 0;
  85. foreach ($headers as $header) {
  86. $table->setHeaderContents($row, $column, $header);
  87. $column++;
  88. }
  89. $row++;
  90. foreach ($data as $row_table) {
  91. $column = 0;
  92. foreach ($row_table as $cell) {
  93. $table->setCellContents($row, $column, $cell);
  94. $table->updateCellAttributes($row, $column, 'align="center"');
  95. $column++;
  96. }
  97. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  98. $row++;
  99. }
  100. $content = $table->toHtml();
  101. // Format B
  102. $headers = [
  103. get_lang('Question'),
  104. get_lang('Answer'),
  105. get_lang('Correct'),
  106. get_lang('Number of learners who selected it'),
  107. ];
  108. $data = [];
  109. if (!empty($question_list)) {
  110. $id = 0;
  111. foreach ($question_list as $question_id) {
  112. $questionObj = Question::read($question_id);
  113. $exercise_stats = ExerciseLib::get_student_stats_by_question(
  114. $question_id,
  115. $exerciseId,
  116. $courseCode,
  117. $sessionId
  118. );
  119. $answer = new Answer($question_id);
  120. $answer_count = $answer->selectNbrAnswers();
  121. for ($answer_id = 1; $answer_id <= $answer_count; $answer_id++) {
  122. $answer_info = $answer->selectAnswer($answer_id);
  123. $is_correct = $answer->isCorrect($answer_id);
  124. $correct_answer = $is_correct == 1 ? get_lang('Yes') : get_lang('No');
  125. $real_answer_id = $answer->selectAutoId($answer_id);
  126. // Overwriting values depending of the question
  127. switch ($questionObj->type) {
  128. case FILL_IN_BLANKS:
  129. $answer_info_db = $answer_info;
  130. $answer_info = substr($answer_info, 0, strpos($answer_info, '::'));
  131. $correct_answer = $is_correct;
  132. $answers = $objExercise->fill_in_blank_answer_to_array($answer_info);
  133. $counter = 0;
  134. foreach ($answers as $answer_item) {
  135. if ($counter == 0) {
  136. $data[$id]['name'] = cut($questionObj->question, 100);
  137. } else {
  138. $data[$id]['name'] = '-';
  139. }
  140. $data[$id]['answer'] = $answer_item;
  141. $answer_item = api_substr($answer_item, 1);
  142. $answer_item = api_substr($answer_item, 0, api_strlen($answer_item) - 1);
  143. $data[$id]['answer'] = $answer_item;
  144. $data[$id]['correct'] = '-';
  145. $count = ExerciseLib::getNumberStudentsFillBlanksAnswerCount($question_id, $exerciseId);
  146. $count = isset($count[$counter]) ? $count[$counter] : 0;
  147. $percentage = 0;
  148. if (!empty($count_students)) {
  149. $percentage = $count / $count_students * 100;
  150. }
  151. $data[$id]['attempts'] = Display::bar_progress(
  152. $percentage,
  153. false,
  154. $count.' / '.$count_students
  155. );
  156. $id++;
  157. $counter++;
  158. }
  159. break;
  160. case MATCHING:
  161. case MATCHING_DRAGGABLE:
  162. if ($is_correct == 0) {
  163. if ($answer_id == 1) {
  164. $data[$id]['name'] = cut($questionObj->question, 100);
  165. } else {
  166. $data[$id]['name'] = '-';
  167. }
  168. $correct = '';
  169. for ($i = 1; $i <= $answer_count; $i++) {
  170. $is_correct_i = $answer->isCorrect($i);
  171. if ($is_correct_i != 0 && $is_correct_i == $answer_id) {
  172. $correct = $answer->selectAnswer($i);
  173. break;
  174. }
  175. }
  176. $data[$id]['answer'] = $correct;
  177. $data[$id]['correct'] = $answer_info;
  178. $count = ExerciseLib::get_number_students_answer_count(
  179. $answer_id,
  180. $question_id,
  181. $exerciseId,
  182. $courseCode,
  183. $sessionId,
  184. MATCHING
  185. );
  186. $percentage = 0;
  187. if (!empty($count_students)) {
  188. $percentage = $count / $count_students * 100;
  189. }
  190. $data[$id]['attempts'] = Display::bar_progress(
  191. $percentage,
  192. false,
  193. $count.' / '.$count_students
  194. );
  195. }
  196. break;
  197. case HOT_SPOT:
  198. if ($answer_id == 1) {
  199. $data[$id]['name'] = cut($questionObj->question, 100);
  200. } else {
  201. $data[$id]['name'] = '-';
  202. }
  203. $data[$id]['answer'] = $answer_info;
  204. $data[$id]['correct'] = '-';
  205. $count = ExerciseLib::get_number_students_answer_hotspot_count(
  206. $answer_id,
  207. $question_id,
  208. $exerciseId,
  209. $courseCode,
  210. $sessionId
  211. );
  212. $percentage = 0;
  213. if (!empty($count_students)) {
  214. $percentage = $count / $count_students * 100;
  215. }
  216. $data[$id]['attempts'] = Display::bar_progress(
  217. $percentage,
  218. false,
  219. $count.' / '.$count_students
  220. );
  221. break;
  222. default:
  223. if ($answer_id == 1) {
  224. $data[$id]['name'] = cut($questionObj->question, 100);
  225. } else {
  226. $data[$id]['name'] = '-';
  227. }
  228. $data[$id]['answer'] = $answer_info;
  229. $data[$id]['correct'] = $correct_answer;
  230. $count = ExerciseLib::get_number_students_answer_count(
  231. $real_answer_id,
  232. $question_id,
  233. $exerciseId,
  234. $courseCode,
  235. $sessionId
  236. );
  237. $percentage = 0;
  238. if (!empty($count_students)) {
  239. $percentage = $count / $count_students * 100;
  240. }
  241. $data[$id]['attempts'] = Display::bar_progress(
  242. $percentage,
  243. false,
  244. $count.' / '.$count_students
  245. );
  246. }
  247. $id++;
  248. }
  249. }
  250. }
  251. // Format A table
  252. $table = new HTML_Table(['class' => 'data_table']);
  253. $row = 0;
  254. $column = 0;
  255. foreach ($headers as $header) {
  256. $table->setHeaderContents($row, $column, $header);
  257. $column++;
  258. }
  259. $row++;
  260. foreach ($data as $row_table) {
  261. $column = 0;
  262. foreach ($row_table as $cell) {
  263. $table->setCellContents($row, $column, $cell);
  264. $table->updateCellAttributes($row, $column, 'align="center"');
  265. $column++;
  266. }
  267. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  268. $row++;
  269. }
  270. $content .= $table->toHtml();
  271. $interbreadcrumb[] = [
  272. "url" => "exercise.php?".api_get_cidreq(),
  273. "name" => get_lang('Tests'),
  274. ];
  275. $interbreadcrumb[] = [
  276. "url" => "admin.php?exerciseId=$exerciseId&".api_get_cidreq(),
  277. "name" => $objExercise->selectTitle(true),
  278. ];
  279. $tpl = new Template(get_lang('Report by question'));
  280. $actions = '<a href="exercise_report.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'">'.
  281. Display:: return_icon(
  282. 'back.png',
  283. get_lang('Go back to the questions list'),
  284. '',
  285. ICON_SIZE_MEDIUM
  286. )
  287. .'</a>';
  288. $actions = Display::div($actions, ['class' => 'actions']);
  289. $content = $actions.$content;
  290. $tpl->assign('content', $content);
  291. $tpl->display_one_col_template();