exams.php 17 KB

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