exams.php 18 KB

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