exams.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Exams script
  5. * @package chamilo.tracking
  6. */
  7. /**
  8. * Code
  9. */
  10. $language_file = array ('registration', 'index', 'tracking', 'exercice','survey');
  11. require_once '../inc/global.inc.php';
  12. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  13. $this_section = SECTION_TRACKING;
  14. $is_allowedToTrack = api_is_course_admin() || api_is_platform_admin() || api_is_course_coach() || api_is_session_admin();
  15. if(!$is_allowedToTrack) {
  16. api_not_allowed();
  17. }
  18. $export_to_xls = false;
  19. if (isset($_GET['export'])) {
  20. $export_to_xls = true;
  21. }
  22. $tbl_stats_exercices = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  23. if (api_is_platform_admin() && empty($_GET['cidReq'])) {
  24. $global = true;
  25. } else {
  26. $global = false;
  27. }
  28. if ($global) {
  29. $temp_course_list = CourseManager :: get_courses_list();
  30. foreach($temp_course_list as $temp_course_item) {
  31. $course_item = CourseManager ::get_course_information($temp_course_item['code']);
  32. $course_list[]= array( 'db_name' => $course_item['db_name'],
  33. 'code' => $course_item['code'],
  34. 'real_id' => $course_item['real_id'],
  35. 'title' => $course_item['title']);
  36. }
  37. } else {
  38. $current_course['db_name'] = $_course['dbName'];
  39. $current_course['real_id'] = $_course['real_id'];
  40. $current_course['code'] = $_course['id'];
  41. $course_list = array($current_course);
  42. }
  43. $form = new FormValidator('search_simple','POST','','',null,false);
  44. $form->addElement('text','score',get_lang('Percentage'));
  45. if ($global) {
  46. $form->addElement('hidden','view','admin');
  47. } else {
  48. //Get exam lists
  49. $t_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  50. $course_id = api_get_course_int_id();
  51. $sqlExercices = " SELECT quiz.title,id FROM ".$t_quiz." AS quiz WHERE c_id = $course_id AND active='1' ORDER BY quiz.title ASC";
  52. $resultExercices = Database::query($sqlExercices);
  53. $exercise_list[0] = get_lang('All');
  54. while($a_exercices = Database::fetch_array($resultExercices)) {
  55. $exercise_list[$a_exercices['id']] = $a_exercices['title'];
  56. }
  57. $form->addElement('select', 'exercise_id', get_lang('Exercise'), $exercise_list);
  58. }
  59. //$form->addElement('submit','submit',get_lang('Filter'));
  60. $form->addElement('style_submit_button','submit', get_lang('Filter'),'class="search"' );
  61. if (!empty($_REQUEST['score'])) $filter_score = intval($_REQUEST['score']); else $filter_score = 70;
  62. if (!empty($_REQUEST['exercise_id'])) $exercise_id = intval($_REQUEST['exercise_id']); else $exercise_id = 0;
  63. $form->setDefaults(array('score'=>$filter_score));
  64. if (!$export_to_xls) {
  65. Display :: display_header(get_lang('Reporting'));
  66. echo '<div class="actions">';
  67. if ($global) {
  68. echo '<a href="'.api_get_path(WEB_CODE_PATH).'auth/my_progress.php">'.
  69. Display::return_icon('stats.png', get_lang('MyStats'),'',ICON_SIZE_MEDIUM);
  70. echo '</a>';
  71. echo '<span style="float:right">';
  72. echo '<a href="'.api_get_self().'?export=1&score='.$filter_score.'&exercise_id='.$exercise_id.'">'.Display::return_icon('export_excel.png',get_lang('ExportAsXLS'),'',ICON_SIZE_MEDIUM).'</a>';
  73. echo '<a href="javascript: void(0);" onclick="javascript: window.print()">'.Display::return_icon('printer.png',get_lang('Print'),'',ICON_SIZE_MEDIUM).'</a>';
  74. echo '</span>';
  75. $menu_items[] = Display::url(Display::return_icon('teacher.png', get_lang('TeacherInterface'), array(), 32), api_get_path(WEB_CODE_PATH).'mySpace/?view=teacher');
  76. if (api_is_platform_admin()) {
  77. $menu_items[] = Display::url(Display::return_icon('star.png', get_lang('AdminInterface'), array(), 32), api_get_path(WEB_CODE_PATH).'mySpace/?view=admin');
  78. } else {
  79. $menu_items[] = Display::url(Display::return_icon('star.png', get_lang('CoachInterface'), array(), 32), api_get_path(WEB_CODE_PATH).'mySpace/?view=coach');
  80. }
  81. $menu_items[] = Display::return_icon('quiz_na.png', get_lang('ExamTracking'), array(), 32);
  82. $nb_menu_items = count($menu_items);
  83. if($nb_menu_items>1) {
  84. foreach($menu_items as $key=> $item) {
  85. echo $item;
  86. }
  87. }
  88. } else {
  89. echo Display::url(Display::return_icon('user.png', get_lang('StudentsTracking'), array(), 32), 'courseLog.php?'.api_get_cidreq().'&amp;studentlist=true');
  90. echo Display::url(Display::return_icon('course.png', get_lang('CourseTracking'), array(), 32), 'courseLog.php?'.api_get_cidreq().'&amp;studentlist=false');
  91. echo Display::url(Display::return_icon('tools.png', get_lang('ResourcesTracking'), array(), 32), 'courseLog.php?'.api_get_cidreq().'&amp;studentlist=resouces');
  92. echo Display::url(Display::return_icon('export_excel.png', get_lang('ExportAsXLS'), array(), 32), api_get_self().'?'.api_get_cidreq().'&amp;export=1&amp;score='.$filter_score.'&amp;exercise_id='.$exercise_id);
  93. }
  94. echo '</div>';
  95. $form->display();
  96. echo '<h3>'.sprintf(get_lang('FilteringWithScoreX'), $filter_score).'%</h3>';
  97. }
  98. $html_result = null;
  99. if ($global) {
  100. $html_result .= '<table class="data_table">';
  101. $html_result .= '<tr><th>'.get_lang('Courses').'</th>';
  102. $html_result .= '<th>'.get_lang('Quiz').'</th>';
  103. $html_result .= '<th>'.get_lang('ExamTaken').'</th>';
  104. $html_result .= '<th>'.get_lang('ExamNotTaken').'</th>';
  105. $html_result .= '<th>'.sprintf(get_lang('ExamPassX'), $filter_score).'%</th>';
  106. $html_result .= '<th>'.get_lang('ExamFail').'</th>';
  107. $html_result .= '<th>'.get_lang('TotalStudents').'</th>';
  108. $html_result .= '</tr>';
  109. } else {
  110. $html_result .= '<table class="data_table">';
  111. $html_result .= '<tr><th>'.get_lang('Quiz').'</th>';
  112. $html_result .= '<th>'.get_lang('User').'</th>';
  113. //$html_result .= '<th>'.sprintf(get_lang('ExamPassX'), $filter_score).'</th>';
  114. $html_result .= '<th>'.get_lang('Percentage').' %</th>';
  115. $html_result .= '<th>'.get_lang('Status').'</th>';
  116. $html_result .= '<th>'.get_lang('Attempts').'</th>';
  117. $html_result .= '</tr>';
  118. }
  119. $table = Database::get_course_table(TABLE_TOOL_LIST);
  120. $t_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  121. $export_array_global = $export_array = array();
  122. if(!empty($course_list) && is_array($course_list)) {
  123. foreach ($course_list as $current_course) {
  124. $global_row = $row_not_global = array();
  125. $course_id = $current_course['real_id'];
  126. $a_students = CourseManager :: get_student_list_from_course_code($course_id, false);
  127. $total_students = count($a_students);
  128. $sqlExercices = "SELECT count(id) as count FROM ".$t_quiz." AS quiz WHERE active='1' AND c_id = $course_id ";
  129. $resultExercices = Database::query($sqlExercices);
  130. $data_exercises = Database::store_result($resultExercices);
  131. $exercise_count = $data_exercises[0]['count'];
  132. if ($global) {
  133. if ($exercise_count == 0) {
  134. $exercise_count = 2;
  135. }
  136. $html_result .= "<tr class='$s_css_class'>
  137. <td rowspan=$exercise_count>";
  138. $html_result .= $current_course['title'];
  139. $html_result .= "</td>";
  140. }
  141. $sql = "SELECT visibility FROM $table WHERE c_id = $course_id AND name='quiz'";
  142. $resultVisibilityQuizz = Database::query($sql);
  143. if (Database::result($resultVisibilityQuizz, 0 ,'visibility') == 1) {
  144. $sqlExercices = " SELECT quiz.title,id FROM ".$t_quiz." AS quiz WHERE c_id = $course_id AND active='1' ORDER BY quiz.title ASC";
  145. //Getting the exam list
  146. if (!$global) {
  147. if (!empty($exercise_id)) {
  148. $sqlExercices = " SELECT quiz.title,id FROM ".$t_quiz." AS quiz WHERE c_id = $course_id AND active='1' AND id = $exercise_id ORDER BY quiz.title ASC";
  149. }
  150. }
  151. $resultExercices = Database::query($sqlExercices);
  152. $i = 0;
  153. if (Database::num_rows($resultExercices) > 0) {
  154. while($a_exercices = Database::fetch_array($resultExercices)) {
  155. $global_row[]= $current_course['title'];
  156. if (!$global) {
  157. $html_result .= "<tr class='$s_css_class'>";
  158. }
  159. if (!$global) {
  160. $html_result .= '<td ROWSPAN="'.$total_students.'">';
  161. } else {
  162. $html_result .= '<td>';
  163. }
  164. $html_result .= $a_exercices['title'];
  165. $html_result .= '</td>';
  166. $global_row[]=$a_exercices['title'];
  167. $row_not_global['exercise']= $a_exercices['title'];
  168. $taken = 0;
  169. $total_with_parameter = 0;
  170. $fail = 0;
  171. $not_taken = 0;
  172. $total_with_parameter_score = 0;
  173. $total_with_parameter_porcentage = 0;
  174. $student_result = array();
  175. foreach ($a_students as $student ) {
  176. $current_student_id = $student['user_id'];
  177. $sqlEssais = " SELECT COUNT(ex.exe_id) as essais
  178. FROM $tbl_stats_exercices AS ex
  179. WHERE ex.c_id = '".$course_id."'
  180. AND ex.exe_exo_id = ".$a_exercices['id']."
  181. AND exe_user_id='".$current_student_id."'";
  182. $resultEssais = Database::query($sqlEssais);
  183. $a_essais = Database::fetch_array($resultEssais);
  184. $sqlScore = "SELECT exe_id, exe_result, exe_weighting
  185. FROM $tbl_stats_exercices
  186. WHERE exe_user_id = ".$current_student_id."
  187. AND c_id = '".$course_id."'
  188. AND exe_exo_id = ".$a_exercices['id']."
  189. ORDER BY exe_result DESC LIMIT 1"; // we take the higher value
  190. //ORDER BY exe_date DESC LIMIT 1";
  191. $resultScore = Database::query($sqlScore);
  192. $score = 0;
  193. while($a_score = Database::fetch_array($resultScore)) {
  194. $score = $score + $a_score['exe_result'];
  195. $weighting = $weighting + $a_score['exe_weighting'];
  196. $exe_id = $a_score['exe_id'];
  197. }
  198. $pourcentageScore = 0;
  199. if ($weighting!=0) {
  200. $pourcentageScore = round(($score*100)/$weighting);
  201. }
  202. $weighting = 0;
  203. if($i%2==0){
  204. $s_css_class="row_odd";
  205. } else {
  206. $s_css_class="row_even";
  207. }
  208. $i++;
  209. if ($a_essais['essais'] > 0 ) {
  210. $taken++;
  211. }
  212. if ($pourcentageScore >= $filter_score) {
  213. $total_with_parameter_score++;
  214. }
  215. if (!$global) {
  216. $user_info = api_get_user_info($current_student_id);
  217. //User
  218. $user_row = '<td >';
  219. $user_row .= $user_info['firstName'].' '.$user_info['lastName'];
  220. $user_row .= '</td>';
  221. $user_info = $user_info['firstName'].' '.$user_info['lastName'];
  222. //Best result
  223. if (!empty($a_essais['essais'])) {
  224. $user_row .= '<td >';
  225. $user_row .= $pourcentageScore;
  226. $temp_array [] = $pourcentageScore;
  227. $user_row .= '</td>';
  228. if ($pourcentageScore >= $filter_score ) {
  229. $user_row .= '<td style="background-color:#DFFFA8">';
  230. $user_row .= get_lang('PassExam').'</td>';
  231. $temp_array [] = get_lang('PassExam');
  232. } else {
  233. $user_row .= '<td style="background-color:#FC9A9E" >';
  234. $user_row .= get_lang('ExamFail').'</td>';
  235. $temp_array [] = get_lang('ExamFail');
  236. }
  237. $user_row .= '<td >';
  238. $user_row .= $a_essais['essais'];
  239. $temp_array [] = $a_essais['essais'];
  240. $user_row .= '</td>';
  241. } else {
  242. $score = '-';
  243. $user_row .= '<td >';
  244. $user_row .= '-';
  245. $temp_array [] = '-';
  246. $user_row .= '</td>';
  247. $user_row .= '<td style="background-color:#FCE89A">';
  248. $user_row .= get_lang('NoAttempt');
  249. $temp_array [] = get_lang('NoAttempt');
  250. $user_row .= '</td>';
  251. $user_row .= '<td >';
  252. $user_row .= 0;
  253. $temp_array [] = 0;
  254. $user_row .= '</td>';
  255. }
  256. $user_row .= '</tr>';
  257. $student_result[$current_student_id] = array('html'=>$user_row,'score'=>$score,'array'=>$temp_array,'user'=>$user_info);
  258. $temp_array = array();
  259. }
  260. }
  261. if (!$global) {
  262. if (!empty($student_result)) {
  263. $student_result_empty = $student_result_content = array();
  264. foreach($student_result as $row) {
  265. if ($row['score'] == '-') {
  266. $student_result_empty[] = $row;
  267. } else {
  268. $student_result_content[] = $row;
  269. }
  270. }
  271. //Sort only users with content
  272. usort($student_result_content, 'sort_user');
  273. $student_result = array_merge($student_result_content, $student_result_empty );
  274. foreach($student_result as $row) {
  275. $html_result .=$row['html'];
  276. $row_not_global['results'][]= $row['array'];
  277. $row_not_global['users'][] = $row['user'];
  278. }
  279. $export_array[] = $row_not_global;
  280. $row_not_global = array();
  281. }
  282. }
  283. if ($global) {
  284. //Exam taken
  285. $html_result .= '<td >';
  286. $html_result .= $taken;
  287. $global_row[]= $taken;
  288. //echo $total.' / '.$total_students;
  289. $html_result .= '</td>';
  290. //Exam NOT taken
  291. $html_result .= '<td >';
  292. $html_result .= $not_taken = $total_students - $taken;
  293. $global_row[]= $not_taken;
  294. $html_result .= '</td>';
  295. //Examn pass
  296. if (!empty($total_with_parameter_score)) {
  297. $html_result .= '<td style="background-color:#DFFFA8" >';
  298. } else {
  299. $html_result .= '<td style="background-color:#FCE89A" >';
  300. }
  301. $html_result .= $total_with_parameter_score;
  302. $global_row[]= $total_with_parameter_score;
  303. $html_result .= '</td>';
  304. //Exam fail
  305. $html_result .= '<td >';
  306. $html_result .= $fail = $taken - $total_with_parameter_score;
  307. $global_row[]= $fail;
  308. $html_result .= '</td>';
  309. $html_result .= '<td >';
  310. $html_result .= $total_students;
  311. $global_row[]= $total_students;
  312. $global_counter++;
  313. $html_result .= '</td>';
  314. $html_result .= '</tr>';
  315. $export_array_global[] = $global_row;
  316. $global_row = array();
  317. }
  318. }
  319. } else {
  320. $html_result .= " <tr>
  321. <td colspan='6'>
  322. ".get_lang('NoExercise')."
  323. </td>
  324. </tr>
  325. ";
  326. }
  327. } else {
  328. $html_result .= " <tr>
  329. <td colspan='6'>
  330. ".get_lang('NoExercise')."
  331. </td>
  332. </tr>
  333. ";
  334. }
  335. }
  336. }
  337. $html_result .= '</table>';
  338. if (!$export_to_xls) {
  339. echo $html_result;
  340. }
  341. $filename = 'exam-reporting-'.date('Y-m-d-h:i:s').'.xls';
  342. if ($export_to_xls) {
  343. if ($global) {
  344. export_complete_report_xls($filename, $export_array_global);
  345. } else {
  346. export_complete_report_xls($filename, $export_array);
  347. }
  348. exit;
  349. }
  350. function sort_user($a, $b) {
  351. if (is_numeric($a['score']) && is_numeric($b['score'])) {
  352. //echo $a['score'].' : '.$b['score']; echo '<br />';
  353. if ($a['score'] < $b['score']) {
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. return 1;
  359. }
  360. function export_complete_report_xls($filename, $array) {
  361. global $charset, $global, $filter_score;
  362. $workbook = new Spreadsheet_Excel_Writer();
  363. $workbook ->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  364. $workbook->send($filename);
  365. $workbook->setVersion(8); // BIFF8
  366. $worksheet =& $workbook->addWorksheet('Report');
  367. //$worksheet->setInputEncoding(api_get_system_encoding());
  368. $worksheet->setInputEncoding($charset);
  369. $line = 0;
  370. $column = 0; //skip the first column (row titles)
  371. if ($global) {
  372. $worksheet->write($line,$column,get_lang('Courses'));
  373. $column++;
  374. $worksheet->write($line,$column,get_lang('Exercises'));
  375. $column++;
  376. $worksheet->write($line,$column,get_lang('ExamTaken'));
  377. $column++;
  378. $worksheet->write($line,$column,get_lang('ExamNotTaken'));
  379. $column++;
  380. $worksheet->write($line,$column,sprintf(get_lang('ExamPassX'), $filter_score).'%');
  381. $column++;
  382. $worksheet->write($line,$column,get_lang('ExamFail'));
  383. $column++;
  384. $worksheet->write($line,$column,get_lang('TotalStudents'));
  385. $column++;
  386. $line++;
  387. foreach ($array as $row) {
  388. $column = 0;
  389. foreach ($row as $item) {
  390. $worksheet->write($line,$column,html_entity_decode(strip_tags($item)));
  391. $column++;
  392. }
  393. $line++;
  394. }
  395. $line++;
  396. } else {
  397. $worksheet->write($line,$column,get_lang('Exercises'));
  398. $column++;
  399. $worksheet->write($line,$column,get_lang('User'));
  400. $column++;
  401. $worksheet->write($line,$column,get_lang('Percentage'));
  402. $column++;
  403. $worksheet->write($line,$column,get_lang('Status'));
  404. $column++;
  405. $worksheet->write($line,$column,get_lang('Attempts'));
  406. $column++;
  407. $line++;
  408. foreach ($array as $row) {
  409. $column = 0;
  410. $worksheet->write($line,$column,html_entity_decode(strip_tags($row['exercise'])));
  411. $column++;
  412. foreach ($row['users'] as $key=>$user) {
  413. $column = 1;
  414. $worksheet->write($line,$column,html_entity_decode(strip_tags($user)));
  415. $column++;
  416. foreach ($row['results'][$key] as $result_item) {
  417. $worksheet->write($line,$column,html_entity_decode(strip_tags($result_item)));
  418. $column++;
  419. }
  420. $line++;
  421. }
  422. }
  423. $line++;
  424. }
  425. $workbook->close();
  426. exit;
  427. }
  428. Display :: display_footer();