lp_results_by_user.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * Exercise results from Learning paths
  6. *
  7. * @todo implement pagination
  8. * @package chamilo.tracking
  9. */
  10. /**
  11. * Code
  12. */
  13. $language_file = array ('registration', 'index', 'tracking', 'exercice','survey');
  14. require_once '../inc/global.inc.php';
  15. require_once api_get_path(LIBRARY_PATH).'pear/Spreadsheet_Excel_Writer/Writer.php';
  16. $this_section = SECTION_TRACKING;
  17. $is_allowedToTrack = api_is_course_admin() || api_is_platform_admin() || api_is_course_coach() || api_is_session_admin();
  18. if (!$is_allowedToTrack) {
  19. Display :: display_header(null);
  20. api_not_allowed();
  21. Display :: display_footer();
  22. }
  23. $export_to_csv = false;
  24. if (isset($_GET['export'])) {
  25. $export_to_csv = true;
  26. }
  27. if (api_is_platform_admin() ) {
  28. $global = true;
  29. } else {
  30. $global = false;
  31. }
  32. if ($global) {
  33. $temp_course_list = CourseManager :: get_courses_list();
  34. foreach($temp_course_list as $temp_course_item) {
  35. $course_item = CourseManager ::get_course_information($temp_course_item['code']);
  36. $course_list[]= array('db_name' =>$course_item['db_name'],'code'=>$course_item['code'], 'title'=>$course_item['title']);
  37. }
  38. } else {
  39. $current_course['db_name'] = $_course['dbName'];
  40. $current_course['code'] = $_course['id'];
  41. $course_list = array($current_course);
  42. }
  43. $new_course_select = array();
  44. foreach($course_list as $data) {
  45. $new_course_select[$data['code']] = $data['title'];
  46. }
  47. $form = new FormValidator('search_simple','POST','','',null,false);
  48. $form->addElement('select','course_code',get_lang('Course'), $new_course_select);
  49. if ($global) {
  50. $form->addElement('hidden','view','admin');
  51. } else {
  52. //Get exam lists
  53. $course_id = api_get_course_int_id();
  54. $t_quiz = Database::get_course_table(TABLE_QUIZ_TEST);
  55. $sqlExercices = " SELECT quiz.title,id FROM ".$t_quiz." AS quiz WHERE c_id = $course_id AND active='1' ORDER BY quiz.title ASC";
  56. $resultExercices = Database::query($sqlExercices);
  57. $exercise_list[0] = get_lang('All');
  58. while($a_exercices = Database::fetch_array($resultExercices)) {
  59. $exercise_list[$a_exercices['id']] = $a_exercices['title'];
  60. }
  61. $form->addElement('select', 'exercise_id', get_lang('Exercise'), $exercise_list);
  62. }
  63. //$form->addElement('submit','submit',get_lang('Filter'));
  64. $form->addElement('style_submit_button','submit', get_lang('Filter'),'class="search"' );
  65. if (!empty($_REQUEST['course_code']))
  66. $selected_course = $_REQUEST['course_code'];
  67. if (!empty($selected_course)) {
  68. $selected_course = api_get_course_info($selected_course);
  69. $course_list = array($selected_course);
  70. }
  71. if (!$export_to_csv) {
  72. Display :: display_header(get_lang('Reporting'));
  73. echo '<div class="actions" style ="font-size:10pt;">';
  74. if ($global) {
  75. echo '<div style="float:right"> <a href="'.api_get_self().'?export=1&score='.$filter_score.'&exercise_id='.$exercise_id.'"><img align="absbottom" src="../img/csv.gif">&nbsp;'.get_lang('ExportAsCSV').'</a>' .
  76. '<a href="javascript: void(0);" onclick="javascript: window.print()"><img align="absbottom" src="../img/printmgr.gif">&nbsp;'.get_lang('Print').'</a></div>';
  77. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=teacher">'.get_lang('TeacherInterface').'</a>';
  78. if (api_is_platform_admin()) {
  79. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=admin">'.get_lang('AdminInterface').'</a>';
  80. } else {
  81. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=coach">'.get_lang('AdminInterface').'</a>';
  82. }
  83. $menu_items[] = get_lang('ExamTracking');
  84. $nb_menu_items = count($menu_items);
  85. if($nb_menu_items>1) {
  86. foreach($menu_items as $key=> $item) {
  87. echo $item;
  88. if($key!=$nb_menu_items-1) {
  89. echo ' | ';
  90. }
  91. }
  92. echo '<br />';
  93. }
  94. } else {
  95. echo '<a href="courseLog.php?'.api_get_cidreq().'&studentlist=true">'.get_lang('StudentsTracking').'</a>&nbsp;|
  96. <a href="courseLog.php?'.api_get_cidreq().'&studentlist=false">'.get_lang('CourseTracking').'</a>&nbsp;|&nbsp';
  97. echo '<a href="courseLog.php?'.api_get_cidreq().'&studentlist=resources">'.get_lang('ResourcesTracking').'</a>';
  98. echo ' | '.get_lang('ExamTracking').'';
  99. 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><br /><br />';
  100. }
  101. echo '</div>';
  102. echo '<br /><br />';
  103. $form->display();
  104. //echo '<h3>'.sprintf(get_lang('FilteringWithScoreX'), $filter_score).'%</h3>';
  105. }
  106. $main_result = array();
  107. $session_id = 0;
  108. $user_list = array();
  109. //Getting course list
  110. foreach($course_list as $current_course ) {
  111. $course_info = api_get_course_info($current_course['code']);
  112. $_course = $course_info;
  113. //Getting LP list
  114. $list = new LearnpathList('', $current_course['code'], $session_id);
  115. $lp_list = $list->get_flat_list();
  116. // Looping LPs
  117. $lps = array();
  118. foreach ($lp_list as $lp_id =>$lp) {
  119. $exercise_list = ExerciseLib::get_all_exercises_from_lp($lp_id, $course_info['real_id']);
  120. $attempt_result = array();
  121. //Looping Chamilo Exercises in LP
  122. foreach ($exercise_list as $exercise) {
  123. $exercise_stats = get_all_exercise_event_from_lp($exercise['path'], $course_info['real_id'], $session_id);
  124. //Looping Exercise Attempts
  125. foreach($exercise_stats as $stats) {
  126. //$attempt_result[$exercise['id']]['users'][$stats['exe_user_id']][$stats['exe_id']] = array('exe_result' =>$stats['exe_result'],'exe_weighting' =>$stats['exe_weighting']);
  127. $attempt_result[$exercise['id']]['users'][$stats['exe_user_id']][$stats['exe_id']] = $stats;
  128. $user_list[$stats['exe_user_id']] = $stats['exe_user_id'];
  129. }
  130. $exercise_list_name[$exercise['id']] = $exercise['title'];
  131. }
  132. $lps[$lp_id] = array('lp_name' =>$lp['lp_name'], 'exercises' =>$attempt_result);
  133. $lp_list_name[$lp_id] = $lp['lp_name'];
  134. }
  135. $main_result[$current_course['code']] = $lps;
  136. }
  137. //echo '<pre>';print_r($main_result);
  138. if (!empty($user_list)) {
  139. foreach($user_list as $user_id) {
  140. $user_data = api_get_user_info($user_id);
  141. $user_list_name[$user_id] = api_get_person_name($user_data['firstname'],$user_data['lastname']);
  142. }
  143. }
  144. $export_array = array();
  145. if (!empty($main_result)) {
  146. $html_result .= '<table class="data_table">';
  147. $html_result .= '<tr><th>'.get_lang('Course').'</th>';
  148. $html_result .= '<th>'.get_lang('LearningPath').'</th>';
  149. $html_result .= '<th>'.get_lang('Exercise').'</th>';
  150. $html_result .= '<th>'.get_lang('User').'</th>';
  151. $html_result .= '<th>'.get_lang('Attempt').'</th>';
  152. $html_result .= '<th>'.get_lang('Date').'</th>';
  153. $html_result .= '<th>'.get_lang('Results').'</th>';
  154. $html_result .= '</tr>';
  155. foreach ($main_result as $course_code => $lps) {
  156. if (empty($lps)) {
  157. continue;
  158. }
  159. foreach($lps as $lp_id => $lp_data) {
  160. $exercises = $lp_data['exercises'];
  161. foreach($exercises as $exercise_id => $exercise_data) {
  162. $users = $exercise_data['users'];
  163. foreach($users as $user_id => $attempts) {
  164. $attempt = 1;
  165. foreach($attempts as $exe_id => $attempt_data) {
  166. $html_result .= '<tr colspan="">';
  167. $html_result .= Display::tag('td', $course_code);
  168. $html_result .= Display::tag('td', $lp_list_name[$lp_id]);
  169. $html_result .= Display::tag('td', $exercise_list_name[$exercise_id]);
  170. $html_result .= Display::tag('td', $user_list_name[$user_id]);
  171. $result = $attempt_data['exe_result'].' / '.$attempt_data['exe_weighting'];
  172. $html_result .= Display::tag('td', $attempt);
  173. $html_result .= Display::tag('td', api_get_local_time($attempt_data['exe_date']));
  174. $html_result .= Display::tag('td', $result);
  175. $html_result .= '</tr>';
  176. $export_array[]= array($course_code, $lp_list_name[$lp_id], $exercise_list_name[$exercise_id], $user_list_name[$user_id], $attempt, api_get_local_time($attempt_data['exe_date']), $result);
  177. $attempt++;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. $html_result .='</table>';
  184. }
  185. if (!$export_to_csv) {
  186. echo $html_result;
  187. }
  188. $filename = 'learning_path_results-'.date('Y-m-d-h:i:s').'.xls';
  189. if ($export_to_csv) {
  190. export_complete_report_csv($filename, $export_array);
  191. exit;
  192. }
  193. function export_complete_report_csv($filename, $array) {
  194. $header[] = array(get_lang('Course'),get_lang('LearningPath'), get_lang('Exercise'), get_lang('User'),get_lang('Attempt'), get_lang('Date'),get_lang('Results'));
  195. if (!empty($array)) {
  196. $array = array_merge($header, $array);
  197. Export :: export_table_csv($array, $filename);
  198. }
  199. exit;
  200. /*
  201. * Too much encoding problems while exporting to XLS, keep it simple
  202. *
  203. *
  204. global $global, $filter_score;
  205. $workbook = new Spreadsheet_Excel_Writer();
  206. $workbook ->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  207. $workbook->send($filename);
  208. $workbook->setVersion(8); // BIFF8
  209. $worksheet =& $workbook->addWorksheet('Report');
  210. $worksheet->setInputEncoding(api_get_system_encoding());
  211. $line = 0;
  212. $column = 0; //skip the first column (row titles)
  213. $worksheet->write($line,$column,get_lang('Course'));
  214. $column++;
  215. $worksheet->write($line,$column,get_lang('LearningPath'));
  216. $column++;
  217. $worksheet->write($line,$column,get_lang('Exercise'));
  218. $column++;
  219. $worksheet->write($line,$column,get_lang('User'));
  220. $column++;
  221. $worksheet->write($line,$column,get_lang('Attempt'));
  222. $column++;
  223. $worksheet->write($line,$column,get_lang('Date'));
  224. $column++;
  225. $worksheet->write($line,$column,get_lang('Results'));
  226. $column++;
  227. $line++;
  228. foreach ($array as $row) {
  229. $column = 0;
  230. foreach ($row as $item) {
  231. $worksheet->write($line,$column,html_entity_decode(strip_tags($item)));
  232. $column++;
  233. }
  234. $line++;
  235. }
  236. $line++;
  237. $workbook->close();
  238. exit;*/
  239. }
  240. Display :: display_footer();