course_session_report.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Report
  5. * @package chamilo.tracking
  6. */
  7. $language_file = array ('registration', 'index', 'tracking', 'exercice','survey');
  8. $cidReset = true;
  9. require_once '../inc/global.inc.php';
  10. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  11. $this_section = "session_my_space";
  12. $is_allowedToTrack = $is_courseAdmin || $is_platformAdmin || $is_courseCoach || $is_sessionAdmin;
  13. if (!$is_allowedToTrack) {
  14. api_not_allowed(true);
  15. }
  16. $export_to_xls = false;
  17. if (isset($_GET['export'])) {
  18. $export_to_xls = true;
  19. }
  20. if (api_is_platform_admin() ) {
  21. $global = true;
  22. } else {
  23. $global = false;
  24. }
  25. $global = true;
  26. $session_id = isset($_GET['session_id']) ? intval($_GET['session_id']) : null;
  27. if (empty($session_id)) {
  28. $session_id = 1;
  29. }
  30. $form = new FormValidator('search_simple','POST','','',null,false);
  31. //Get session list
  32. $session_list = SessionManager::get_sessions_list(array(), array('name'));
  33. $my_session_list = array();
  34. foreach($session_list as $sesion_item) {
  35. $my_session_list[$sesion_item['id']] = $sesion_item['name'];
  36. }
  37. if (count($session_list) == 0) {
  38. $my_session_list[0] = get_lang('None');
  39. }
  40. $form->addElement('select', 'session_id', get_lang('Sessions'), $my_session_list);
  41. $form->addElement('style_submit_button','submit',get_lang('Filter'));
  42. if (!empty($_REQUEST['score'])) $filter_score = intval($_REQUEST['score']); else $filter_score = 70;
  43. if (!empty($_REQUEST['session_id'])) $session_id = intval($_REQUEST['session_id']); else $session_id = 0;
  44. if (empty($session_id)) {
  45. $session_id = key($my_session_list);
  46. }
  47. $form->setDefaults(array('session_id'=>$session_id));
  48. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  49. if (!$export_to_xls) {
  50. Display :: display_header(get_lang("MySpace"));
  51. echo '<div class="actions">';
  52. if ($global) {
  53. echo MySpace::getTopMenu();
  54. } else {
  55. echo '<div style="float:left; clear:left">
  56. <a href="courseLog.php?'.api_get_cidreq().'&studentlist=true">'.get_lang('StudentsTracking').'</a>&nbsp;|
  57. <a href="courseLog.php?'.api_get_cidreq().'&studentlist=false">'.get_lang('CourseTracking').'</a>&nbsp;';
  58. echo '</div>';
  59. }
  60. echo '</div>';
  61. if (api_is_platform_admin()) {
  62. echo MySpace::getAdminActions();
  63. }
  64. echo '<h2>'.get_lang('LPExerciseResultsBySession').'</h2>';
  65. $form->display();
  66. Display::display_normal_message(get_lang('StudentScoreAverageIsCalculatedBaseInAllLPsAndAllAttempts'));
  67. }
  68. $users = SessionManager::get_users_by_session($session_id);
  69. $course_average = $course_average_counter = array();
  70. $counter = 0;
  71. $main_result = array();
  72. //Getting course list
  73. foreach ($course_list as $current_course ) {
  74. $course_info = api_get_course_info($current_course['code']);
  75. $_course = $course_info;
  76. $attempt_result = array();
  77. //Getting LP list
  78. $list = new LearnpathList('', $current_course['code'], $session_id);
  79. $lp_list = $list->get_flat_list();
  80. // Looping LPs
  81. foreach ($lp_list as $lp_id =>$lp) {
  82. $exercise_list = get_all_exercises_from_lp($lp_id, $course_info['real_id']);
  83. // Looping Chamilo Exercises in LP
  84. foreach ($exercise_list as $exercise) {
  85. $exercise_stats = get_all_exercise_event_from_lp($exercise['path'], $course_info['id'], $session_id);
  86. // Looping Exercise Attempts
  87. foreach ($exercise_stats as $stats) {
  88. $attempt_result[$stats['exe_user_id']]['result'] += $stats['exe_result'] / $stats['exe_weighting'];
  89. $attempt_result[$stats['exe_user_id']]['attempts']++;
  90. }
  91. }
  92. }
  93. $main_result[$current_course['code']] = $attempt_result;
  94. }
  95. //var_dump($main_result);
  96. $total_average_score = 0;
  97. $total_average_score_count = 0;
  98. if (!empty($users) && is_array($users)) {
  99. $html_result .= '<table class="data_table">';
  100. $html_result .= '<tr><th>'.get_lang('User').'</th>';
  101. foreach($course_list as $item ) {
  102. $html_result .= '<th>'.$item['title'].'<br /> '.get_lang('AverageScore').' %</th>';
  103. }
  104. $html_result .= '<th>'.get_lang('AverageScore').' %</th>';
  105. $html_result .= '<th>'.get_lang('LastConnexionDate').'</th></tr>';
  106. foreach ($users as $user) {
  107. $total_student = 0;
  108. $counter ++;
  109. $s_css_class = 'row_even';
  110. if ($counter % 2 ==0 ) {
  111. $s_css_class = 'row_odd';
  112. }
  113. $html_result .= "<tr class='$s_css_class'>
  114. <td >";
  115. $html_result .= $user['firstname'].' '.$user['lastname'];
  116. $html_result .= "</td>";
  117. // Getting course list
  118. $counter = 0;
  119. $total_result_by_user = 0;
  120. foreach ($course_list as $current_course ) {
  121. $total_course = 0;
  122. $html_result .= "<td>";
  123. $result = '-';
  124. if (isset($main_result[$current_course['code']][$user['user_id']])) {
  125. $user_info_stat = $main_result[$current_course['code']][$user['user_id']];
  126. if (!empty($user_info_stat['result']) && !empty($user_info_stat['attempts'])) {
  127. $result = round(
  128. $user_info_stat['result'] / $user_info_stat['attempts'] * 100,
  129. 2
  130. );
  131. $total_course += $result;
  132. $total_result_by_user += $result;
  133. $course_average[$current_course['code']] += $total_course;
  134. $course_average_counter[$current_course['code']]++;
  135. $result = $result . ' (' . $user_info_stat['attempts'] . ' ' . get_lang(
  136. 'Attempts'
  137. ) . ')';
  138. $counter++;
  139. }
  140. }
  141. $html_result .= $result;
  142. $html_result .= "</td>";
  143. }
  144. if (empty($counter)) {
  145. $total_student = '-';
  146. } else {
  147. $total_student = $total_result_by_user/$counter;
  148. $total_average_score+=$total_student;
  149. $total_average_score_count++;
  150. }
  151. $string_date=Tracking :: get_last_connection_date($user['user_id'],true);
  152. $html_result .="<td>$total_student</td><td>$string_date</td></tr>";
  153. }
  154. $html_result .="<tr><th>".get_lang('AverageScore')."</th>";
  155. $total_average = 0;
  156. $counter = 0;
  157. foreach($course_list as $course_item) {
  158. if (!empty($course_average_counter[$course_item['code']])) {
  159. $average_per_course = round($course_average[$course_item['code']]/($course_average_counter[$course_item['code']]*100)*100,2);
  160. } else {
  161. $average_per_course = '-';
  162. }
  163. if (!empty($average_per_course)) {
  164. $counter++;
  165. }
  166. $total_average = $total_average + $average_per_course;
  167. $html_result .="<td>$average_per_course</td>";
  168. }
  169. if (!empty($total_average_score_count)) {
  170. $total_average = round($total_average_score/($total_average_score_count*100)*100,2);
  171. } else {
  172. $total_average = '-';
  173. }
  174. $html_result .='<td>'.$total_average.'</td>';
  175. $html_result .="<td>-</td>";
  176. $html_result .="</tr>";
  177. $html_result .= '</table>';
  178. } else {
  179. Display::display_warning_message(get_lang('NoResults'));
  180. }
  181. if (!$export_to_xls) {
  182. echo $html_result;
  183. }
  184. $filename = 'exam-reporting-'.date('Y-m-d-h:i:s').'.xls';
  185. if ($export_to_xls) {
  186. echo $html_result;
  187. export_complete_report_xls($filename, $export_array);
  188. exit;
  189. }
  190. function sort_user($a, $b) {
  191. if (is_numeric($a['score']) && is_numeric($b['score'])) {
  192. echo $a['score'].' : '.$b['score'];
  193. echo '<br />';
  194. if ($a['score'] < $b['score']) {
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. function export_complete_report_xls($filename, $array) {
  202. global $charset;
  203. $workbook = new Spreadsheet_Excel_Writer();
  204. $workbook ->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  205. $workbook->send($filename);
  206. $workbook->setVersion(8); // BIFF8
  207. $worksheet =& $workbook->addWorksheet('Report');
  208. //$worksheet->setInputEncoding(api_get_system_encoding());
  209. $worksheet->setInputEncoding($charset);
  210. /*
  211. $line = 0;
  212. $column = 1; // Skip the first column (row titles)
  213. foreach ($array as $elem) {
  214. $worksheet->write($line, $column, $elem);
  215. $column++;
  216. }
  217. $workbook->close();*/
  218. exit;
  219. }
  220. Display :: display_footer();