exams.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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() && 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($current_course['code'], 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.exe_cours_id = '".$current_course['code']."'
  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 exe_cours_id = '".$current_course['code']."'
  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. /*echo " <td align='right'>
  210. ";
  211. echo $current_student_id.' ';
  212. echo " </td>";
  213. */
  214. //var_dump($pourcentageScore);
  215. /* echo " <td align='right'>
  216. ";
  217. echo $pourcentageScore.' %';
  218. echo " </td>";
  219. echo "<td align='right'>
  220. ";
  221. /*
  222. echo $a_essais['essais'];
  223. echo " </td>
  224. <td align='center'>
  225. ";*/
  226. if ($a_essais['essais'] > 0 ) {
  227. $taken++;
  228. }
  229. /*if ($pourcentageScore >= $parameter_porcentage) {
  230. $total_with_parameter_porcentage++;
  231. }*/
  232. if ($pourcentageScore >= $filter_score) {
  233. $total_with_parameter_score++;
  234. }
  235. if (!$global) {
  236. $user_info = api_get_user_info($current_student_id);
  237. //User
  238. $user_row = '<td >';
  239. $user_row .= $user_info['firstName'].' '.$user_info['lastName'];
  240. $user_row .= '</td>';
  241. $user_info = $user_info['firstName'].' '.$user_info['lastName'];
  242. //Best result
  243. if (!empty($a_essais['essais'])) {
  244. $user_row .= '<td >';
  245. $user_row .= $pourcentageScore;
  246. $temp_array [] = $pourcentageScore;
  247. $user_row .= '</td>';
  248. if ($pourcentageScore >= $filter_score ) {
  249. $user_row .= '<td style="background-color:#DFFFA8">';
  250. $user_row .= get_lang('PassExam').'</td>';
  251. $temp_array [] = get_lang('PassExam');
  252. } else {
  253. $user_row .= '<td style="background-color:#FC9A9E" >';
  254. $user_row .= get_lang('ExamFail').'</td>';
  255. $temp_array [] = get_lang('ExamFail');
  256. }
  257. $user_row .= '<td >';
  258. $user_row .= $a_essais['essais'];
  259. $temp_array [] = $a_essais['essais'];
  260. $user_row .= '</td>';
  261. } else {
  262. $score = '-';
  263. $user_row .= '<td >';
  264. $user_row .= '-';
  265. $temp_array [] = '-';
  266. $user_row .= '</td>';
  267. $user_row .= '<td style="background-color:#FCE89A">';
  268. $user_row .= get_lang('NoAttempt');
  269. $temp_array [] = get_lang('NoAttempt');
  270. $user_row .= '</td>';
  271. $user_row .= '<td >';
  272. $user_row .= 0;
  273. $temp_array [] = 0;
  274. $user_row .= '</td>';
  275. }
  276. $user_row .= '</tr>';
  277. $student_result[$current_student_id] = array('html'=>$user_row,'score'=>$score,'array'=>$temp_array,'user'=>$user_info);
  278. $temp_array = array();
  279. }
  280. }
  281. if (!$global) {
  282. if (!empty($student_result)) {
  283. $student_result_empty = $student_result_content = array();
  284. foreach($student_result as $row) {
  285. if ($row['score'] == '-') {
  286. $student_result_empty[] = $row;
  287. } else {
  288. $student_result_content[] = $row;
  289. }
  290. }
  291. //Sort only users with content
  292. usort($student_result_content, 'sort_user');
  293. $student_result = array_merge($student_result_content, $student_result_empty );
  294. foreach($student_result as $row) {
  295. $html_result .=$row['html'];
  296. $row_not_global['results'][]= $row['array'];
  297. $row_not_global['users'][] = $row['user'];
  298. }
  299. $export_array[] = $row_not_global;
  300. $row_not_global = array();
  301. }
  302. }
  303. if ($global) {
  304. //Exam taken
  305. $html_result .= '<td >';
  306. $html_result .= $taken;
  307. $global_row[]= $taken;
  308. //echo $total.' / '.$total_students;
  309. $html_result .= '</td>';
  310. //Exam NOT taken
  311. $html_result .= '<td >';
  312. $html_result .= $not_taken = $total_students - $taken;
  313. $global_row[]= $not_taken;
  314. $html_result .= '</td>';
  315. //Examn pass
  316. if (!empty($total_with_parameter_score)) {
  317. $html_result .= '<td style="background-color:#DFFFA8" >';
  318. } else {
  319. $html_result .= '<td style="background-color:#FCE89A" >';
  320. }
  321. $html_result .= $total_with_parameter_score;
  322. $global_row[]= $total_with_parameter_score;
  323. $html_result .= '</td>';
  324. //Exam fail
  325. $html_result .= '<td >';
  326. $html_result .= $fail = $taken - $total_with_parameter_score;
  327. $global_row[]= $fail;
  328. $html_result .= '</td>';
  329. $html_result .= '<td >';
  330. $html_result .= $total_students;
  331. $global_row[]= $total_students;
  332. $global_counter++;
  333. $html_result .= '</td>';
  334. $html_result .= '</tr>';
  335. $export_array_global[] = $global_row;
  336. $global_row = array();
  337. }
  338. }
  339. } else {
  340. $html_result .= " <tr>
  341. <td colspan='6'>
  342. ".get_lang('NoExercise')."
  343. </td>
  344. </tr>
  345. ";
  346. }
  347. } else {
  348. $html_result .= " <tr>
  349. <td colspan='6'>
  350. ".get_lang('NoExercise')."
  351. </td>
  352. </tr>
  353. ";
  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();