current_courses.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Report for current courses followed by the user.
  5. *
  6. * @package chamilo.reporting
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_TRACKING;
  11. $filename = 'reporting.xlsx';
  12. if (!api_is_allowed_to_create_course()) {
  13. api_not_allowed(true);
  14. }
  15. $user_id = api_get_user_id();
  16. $my_courses = CourseManager::get_course_list_of_user_as_course_admin($user_id);
  17. $array = [];
  18. $i = 0;
  19. $session_id = 0;
  20. if (!empty($my_courses)) {
  21. foreach ($my_courses as $course) {
  22. $course_code = $course['code'];
  23. $course_id = $course['real_id'];
  24. $course_info = api_get_course_info($course_code);
  25. //Only show open courses
  26. if ($course_info['visibility'] == 0) {
  27. continue;
  28. }
  29. $teachers = CourseManager::get_teacher_list_from_course_code($course_code);
  30. $teacher_list = [];
  31. if (!empty($teachers)) {
  32. foreach ($teachers as $teacher) {
  33. $teacher_list[] = $teacher['firstname'].' '.$teacher['lastname'];
  34. }
  35. }
  36. $tmp_students = CourseManager :: get_student_list_from_course_code($course_code, false);
  37. //Cleaning students only REAL students
  38. $students = [];
  39. foreach ($tmp_students as $student) {
  40. $user_info = api_get_user_info($student['user_id']);
  41. if ($user_info['status'] != STUDENT) {
  42. continue;
  43. }
  44. $students[] = $student['user_id'];
  45. }
  46. $t_lp = Database::get_course_table(TABLE_LP_MAIN);
  47. $sql_lp = "SELECT lp.name, lp.id FROM $t_lp lp
  48. WHERE c_id = $course_id AND lp.session_id = 0";
  49. $rs_lp = Database::query($sql_lp);
  50. $t_lpi = Database::get_course_table(TABLE_LP_ITEM);
  51. $t_news = Database::get_course_table(TABLE_ANNOUNCEMENT);
  52. $total_tools_list = Tracking::get_tools_most_used_by_course(
  53. $course_id,
  54. $session_id
  55. );
  56. $total_tools = 0;
  57. foreach ($total_tools_list as $tool) {
  58. $total_tools += $tool['count_access_tool'];
  59. }
  60. if (Database :: num_rows($rs_lp) > 0) {
  61. while ($learnpath = Database :: fetch_array($rs_lp)) {
  62. $lp_id = $learnpath['id'];
  63. $lp_items =
  64. $array[$i]['lp'] = '<a href="'.api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?cidReq='.$course_code.'&amp;action=view&amp;lp_id='.$lp_id.'" target="_blank">'.$learnpath['name'].'</a>';
  65. $array[$i]['teachers'] = '';
  66. if (!empty($teacher_list)) {
  67. $array[$i]['teachers'] = implode(', ', $teacher_list);
  68. }
  69. $array[$i]['course_name'] = $course['title'];
  70. $count_students_accessing = 0;
  71. $count_students_complete_all_activities = 0;
  72. $count_students_complete_all_activities_at_50 = 0;
  73. $total_time_spent = 0;
  74. $total_average_progress = 0;
  75. if (!empty($students)) {
  76. foreach ($students as $student_id) {
  77. $avg_student_progress = Tracking::get_avg_student_progress($student_id, $course_code, [$lp_id], $session_id);
  78. $myavg_temp = Tracking::get_avg_student_score($student_id, $course_code, [$lp_id], $session_id);
  79. $avg_progress_in_course = Tracking::get_avg_student_progress($student_id, $course_code, [$lp_id], $session_id);
  80. if (intval($avg_progress_in_course) == 100) {
  81. $count_students_complete_all_activities++;
  82. }
  83. if (intval($avg_progress_in_course) > 0 && intval($avg_progress_in_course) <= 50) {
  84. $count_students_complete_all_activities_at_50++;
  85. }
  86. $total_average_progress += $avg_progress_in_course;
  87. $time_spent = Tracking::get_time_spent_on_the_course($student_id, $course_id, $session_id);
  88. $total_time_spent += $time_spent;
  89. if (!empty($time_spent)) {
  90. $count_students_accessing++;
  91. }
  92. }
  93. //$total_tools += $nb_assignments + $messages + $links + $chat_last_connection + $documents;
  94. }
  95. $student_count = count($students);
  96. $array[$i]['count_students'] = $student_count;
  97. $array[$i]['count_students_accessing'] = 0;
  98. $array[$i]['count_students_accessing_percentage'] = 0;
  99. $array[$i]['count_students_complete_all_activities_at_50'] = 0;
  100. $array[$i]['count_students_complete_all_activities'] = 0;
  101. $array[$i]['average_percentage_activities_completed_per_student'] = 0;
  102. $array[$i]['total_time_spent'] = 0;
  103. $array[$i]['average_time_spent_per_student'] = 0;
  104. $array[$i]['total_time_spent'] = 0;
  105. $array[$i]['average_time_spent_per_student'] = 0;
  106. //$array[$i]['tools_used'] = 0;
  107. $array[$i]['learnpath_docs'] = 0;
  108. $array[$i]['learnpath_exercises'] = 0;
  109. $array[$i]['learnpath_links'] = 0;
  110. $array[$i]['learnpath_forums'] = 0;
  111. $array[$i]['learnpath_assignments'] = 0;
  112. //registering the number of each category of
  113. //items in learning path
  114. $sql_lpi = "SELECT lpi.item_type FROM $t_lpi lpi
  115. WHERE c_id = $course_id AND lpi.lp_id = $lp_id
  116. ORDER BY item_type";
  117. $res_lpi = Database::query($sql_lpi);
  118. while ($row_lpi = Database::fetch_array($res_lpi)) {
  119. switch ($row_lpi['item_type']) {
  120. case 'document':
  121. $array[$i]['learnpath_docs']++;
  122. break;
  123. case 'quiz':
  124. $array[$i]['learnpath_exercises']++;
  125. break;
  126. case 'link':
  127. $array[$i]['learnpath_links']++;
  128. break;
  129. case 'forum':
  130. case 'thread':
  131. $array[$i]['learnpath_forums']++;
  132. break;
  133. case 'student_publication':
  134. $array[$i]['learnpath_assignments']++;
  135. break;
  136. }
  137. }
  138. // Count announcements
  139. $array[$i]['total_announcements'] = 0;
  140. $sql_news = "SELECT count(id) FROM $t_news WHERE c_id = $course_id ";
  141. $res_news = Database::query($sql_news);
  142. while ($row_news = Database::fetch_array($res_news)) {
  143. $array[$i]['total_announcements'] = $row_news[0];
  144. }
  145. //@todo don't know what means this value
  146. $count_students_complete_all_activities_at_50 = 0;
  147. if (!empty($student_count)) {
  148. $array[$i]['count_students_accessing'] = $count_students_accessing;
  149. $array[$i]['count_students_accessing_percentage'] = round($count_students_accessing / $student_count * 100, 0);
  150. $array[$i]['count_students_complete_all_activities_at_50'] = $count_students_complete_all_activities;
  151. $array[$i]['count_students_complete_all_activities'] = round($count_students_complete_all_activities / $student_count * 100, 0);
  152. $array[$i]['average_percentage_activities_completed_per_student'] = round($count_students_complete_all_activities / $student_count * 100, 2);
  153. $array[$i]['total_time_spent'] = 0;
  154. $array[$i]['average_time_spent_per_student'] = 0;
  155. if (!empty($total_time_spent)) {
  156. $array[$i]['total_time_spent'] = api_time_to_hms($total_time_spent);
  157. $array[$i]['average_time_spent_per_student'] = api_time_to_hms($total_time_spent / $student_count);
  158. }
  159. //$array[$i]['tools_used'] = $total_tools;
  160. }
  161. $i++;
  162. }
  163. }
  164. }
  165. }
  166. $headers = [
  167. get_lang('LearningPath'),
  168. get_lang('Teachers'),
  169. get_lang('Courses'),
  170. get_lang('NumberOfStudents'),
  171. get_lang('NumberStudentsAccessingCourse'),
  172. get_lang('PercentageStudentsAccessingCourse'),
  173. get_lang('NumberStudentsCompleteAllActivities'),
  174. get_lang('PercentageStudentsCompleteAllActivities'),
  175. get_lang('AverageOfActivitiesCompletedPerStudent'),
  176. get_lang('TotalTimeSpentInTheCourse'),
  177. get_lang('AverageTimePerStudentInCourse'),
  178. get_lang('NumberOfDocumentsInLearnpath'),
  179. get_lang('NumberOfExercisesInLearnpath'),
  180. get_lang('NumberOfLinksInLearnpath'),
  181. get_lang('NumberOfForumsInLearnpath'),
  182. get_lang('NumberOfAssignmentsInLearnpath'),
  183. get_lang('NumberOfAnnouncementsInCourse'),
  184. ];
  185. if (isset($_GET['export'])) {
  186. global $charset;
  187. $spreadsheet = new PHPExcel();
  188. $spreadsheet->setActiveSheetIndex(0);
  189. $worksheet = $spreadsheet->getActiveSheet();
  190. $line = 0;
  191. $column = 0; //skip the first column (row titles)
  192. foreach ($headers as $header) {
  193. $worksheet->setCellValueByColumnAndRow($column, $line, $header);
  194. $column++;
  195. }
  196. $line++;
  197. foreach ($array as $row) {
  198. $column = 0;
  199. foreach ($row as $item) {
  200. $worksheet->setCellValueByColumnAndRow(
  201. $column,
  202. $line,
  203. html_entity_decode(strip_tags($item))
  204. );
  205. $column++;
  206. }
  207. $line++;
  208. }
  209. $line++;
  210. $file = api_get_path(SYS_ARCHIVE_PATH).api_replace_dangerous_char($filename);
  211. $writer = new PHPExcel_Writer_Excel2007($spreadsheet);
  212. $writer->save($file);
  213. DocumentManager::file_send_for_download($file, true, $filename);
  214. exit;
  215. }
  216. $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('MySpace')];
  217. Display::display_header(get_lang('CurrentCourses'));
  218. $table = new HTML_Table(['class' => 'data_table']);
  219. $row = 0;
  220. $column = 0;
  221. foreach ($headers as $header) {
  222. $table->setHeaderContents($row, $column, $header);
  223. $column++;
  224. }
  225. $row++;
  226. foreach ($array as $row_table) {
  227. $column = 0;
  228. foreach ($row_table as $cell) {
  229. $table->setCellContents($row, $column, $cell);
  230. //$table->updateCellAttributes($row, $column, 'align="center"');
  231. $column++;
  232. }
  233. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  234. $row++;
  235. }
  236. echo '<div class="actions">';
  237. echo '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace">'.Display::return_icon('back.png', get_lang('Back'), [], 32).'</a>';
  238. echo '<a href="'.api_get_path(WEB_CODE_PATH).'mySpace/current_courses.php?export=1">'.Display::return_icon('export_excel.png', get_lang('CurrentCoursesReport'), [], 32).'</a> ';
  239. echo '</div>';
  240. echo '<div style="overflow:auto;">';
  241. echo $table->toHtml();
  242. echo '</div>';
  243. Display::display_footer();