lp_results_by_user.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.'">
  76. '.Display::return_icon('csv.gif', get_lang('ExportAsCSV')).' &nbsp;'.get_lang('ExportAsCSV').'</a>' .
  77. '<a href="javascript: void(0);" onclick="javascript: window.print()">
  78. '.Display::return_icon('printmgr.gif', get_lang('Print')).' </a></div>';
  79. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=teacher">'.get_lang('TeacherInterface').'</a>';
  80. if (api_is_platform_admin()) {
  81. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=admin">'.get_lang('AdminInterface').'</a>';
  82. } else {
  83. $menu_items[] = '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/?view=coach">'.get_lang('AdminInterface').'</a>';
  84. }
  85. $menu_items[] = get_lang('ExamTracking');
  86. $nb_menu_items = count($menu_items);
  87. if($nb_menu_items>1) {
  88. foreach($menu_items as $key=> $item) {
  89. echo $item;
  90. if($key!=$nb_menu_items-1) {
  91. echo ' | ';
  92. }
  93. }
  94. echo '<br />';
  95. }
  96. } else {
  97. echo '<a href="courseLog.php?'.api_get_cidreq().'&studentlist=true">'.get_lang('StudentsTracking').'</a>&nbsp;|
  98. <a href="courseLog.php?'.api_get_cidreq().'&studentlist=false">'.get_lang('CourseTracking').'</a>&nbsp;|&nbsp';
  99. echo '<a href="courseLog.php?'.api_get_cidreq().'&studentlist=resources">'.get_lang('ResourcesTracking').'</a>';
  100. echo ' | '.get_lang('ExamTracking').'';
  101. echo '<a href="'.api_get_self().'?export=1&score='.$filter_score.'&exercise_id='.$exercise_id.'">
  102. '.Display::return_icon('excel.gif', get_lang('ExportAsXLS')).' </a><br /><br />';
  103. }
  104. echo '</div>';
  105. echo '<br /><br />';
  106. $form->display();
  107. //echo '<h3>'.sprintf(get_lang('FilteringWithScoreX'), $filter_score).'%</h3>';
  108. }
  109. $main_result = array();
  110. $session_id = 0;
  111. $user_list = array();
  112. //Getting course list
  113. foreach($course_list as $current_course ) {
  114. $course_info = api_get_course_info($current_course['code']);
  115. $_course = $course_info;
  116. //Getting LP list
  117. $list = new LearnpathList('', $current_course['code'], $session_id);
  118. $lp_list = $list->get_flat_list();
  119. // Looping LPs
  120. $lps = array();
  121. foreach ($lp_list as $lp_id =>$lp) {
  122. $exercise_list = ExerciseLib::get_all_exercises_from_lp($lp_id, $course_info['real_id']);
  123. $attempt_result = array();
  124. //Looping Chamilo Exercises in LP
  125. foreach ($exercise_list as $exercise) {
  126. $exercise_stats = get_all_exercise_event_from_lp($exercise['path'], $course_info['real_id'], $session_id);
  127. //Looping Exercise Attempts
  128. foreach($exercise_stats as $stats) {
  129. //$attempt_result[$exercise['id']]['users'][$stats['exe_user_id']][$stats['exe_id']] = array('exe_result' =>$stats['exe_result'],'exe_weighting' =>$stats['exe_weighting']);
  130. $attempt_result[$exercise['id']]['users'][$stats['exe_user_id']][$stats['exe_id']] = $stats;
  131. $user_list[$stats['exe_user_id']] = $stats['exe_user_id'];
  132. }
  133. $exercise_list_name[$exercise['id']] = $exercise['title'];
  134. }
  135. $lps[$lp_id] = array('lp_name' =>$lp['lp_name'], 'exercises' =>$attempt_result);
  136. $lp_list_name[$lp_id] = $lp['lp_name'];
  137. }
  138. $main_result[$current_course['code']] = $lps;
  139. }
  140. //echo '<pre>';print_r($main_result);
  141. if (!empty($user_list)) {
  142. foreach($user_list as $user_id) {
  143. $user_data = api_get_user_info($user_id);
  144. $user_list_name[$user_id] = api_get_person_name($user_data['firstname'],$user_data['lastname']);
  145. }
  146. }
  147. $export_array = array();
  148. if (!empty($main_result)) {
  149. $html_result .= '<table class="data_table">';
  150. $html_result .= '<tr><th>'.get_lang('Course').'</th>';
  151. $html_result .= '<th>'.get_lang('LearningPath').'</th>';
  152. $html_result .= '<th>'.get_lang('Exercise').'</th>';
  153. $html_result .= '<th>'.get_lang('User').'</th>';
  154. $html_result .= '<th>'.get_lang('Attempt').'</th>';
  155. $html_result .= '<th>'.get_lang('Date').'</th>';
  156. $html_result .= '<th>'.get_lang('Results').'</th>';
  157. $html_result .= '</tr>';
  158. foreach ($main_result as $course_code => $lps) {
  159. if (empty($lps)) {
  160. continue;
  161. }
  162. foreach($lps as $lp_id => $lp_data) {
  163. $exercises = $lp_data['exercises'];
  164. foreach($exercises as $exercise_id => $exercise_data) {
  165. $users = $exercise_data['users'];
  166. foreach($users as $user_id => $attempts) {
  167. $attempt = 1;
  168. foreach($attempts as $exe_id => $attempt_data) {
  169. $html_result .= '<tr colspan="">';
  170. $html_result .= Display::tag('td', $course_code);
  171. $html_result .= Display::tag('td', $lp_list_name[$lp_id]);
  172. $html_result .= Display::tag('td', $exercise_list_name[$exercise_id]);
  173. $html_result .= Display::tag('td', $user_list_name[$user_id]);
  174. $result = $attempt_data['exe_result'].' / '.$attempt_data['exe_weighting'];
  175. $html_result .= Display::tag('td', $attempt);
  176. $html_result .= Display::tag('td', api_get_local_time($attempt_data['exe_date']));
  177. $html_result .= Display::tag('td', $result);
  178. $html_result .= '</tr>';
  179. $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);
  180. $attempt++;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. $html_result .='</table>';
  187. }
  188. if (!$export_to_csv) {
  189. echo $html_result;
  190. }
  191. $filename = 'learning_path_results-'.date('Y-m-d-h:i:s').'.xls';
  192. if ($export_to_csv) {
  193. export_complete_report_csv($filename, $export_array);
  194. exit;
  195. }
  196. function export_complete_report_csv($filename, $array) {
  197. $header[] = array(get_lang('Course'),get_lang('LearningPath'), get_lang('Exercise'), get_lang('User'),get_lang('Attempt'), get_lang('Date'),get_lang('Results'));
  198. if (!empty($array)) {
  199. $array = array_merge($header, $array);
  200. Export :: export_table_csv($array, $filename);
  201. }
  202. exit;
  203. /*
  204. * Too much encoding problems while exporting to XLS, keep it simple
  205. *
  206. *
  207. global $global, $filter_score;
  208. $workbook = new Spreadsheet_Excel_Writer();
  209. $workbook ->setTempDir(api_get_path(SYS_ARCHIVE_PATH));
  210. $workbook->send($filename);
  211. $workbook->setVersion(8); // BIFF8
  212. $worksheet =& $workbook->addWorksheet('Report');
  213. $worksheet->setInputEncoding(api_get_system_encoding());
  214. $line = 0;
  215. $column = 0; //skip the first column (row titles)
  216. $worksheet->write($line,$column,get_lang('Course'));
  217. $column++;
  218. $worksheet->write($line,$column,get_lang('LearningPath'));
  219. $column++;
  220. $worksheet->write($line,$column,get_lang('Exercise'));
  221. $column++;
  222. $worksheet->write($line,$column,get_lang('User'));
  223. $column++;
  224. $worksheet->write($line,$column,get_lang('Attempt'));
  225. $column++;
  226. $worksheet->write($line,$column,get_lang('Date'));
  227. $column++;
  228. $worksheet->write($line,$column,get_lang('Results'));
  229. $column++;
  230. $line++;
  231. foreach ($array as $row) {
  232. $column = 0;
  233. foreach ($row as $item) {
  234. $worksheet->write($line,$column,html_entity_decode(strip_tags($item)));
  235. $column++;
  236. }
  237. $line++;
  238. }
  239. $line++;
  240. $workbook->close();
  241. exit;*/
  242. }
  243. Display :: display_footer();