block_course.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * This file is part of course block plugin for dashboard,
  4. * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
  5. *
  6. * @package chamilo.dashboard
  7. *
  8. * @author Christian Fasanando
  9. */
  10. /**
  11. * This class is used like controller for this course block plugin,
  12. * the class name must be registered inside path.info file
  13. * (e.g: controller = "BlockCourse"), so dashboard controller will be instantiate it.
  14. *
  15. * @package chamilo.dashboard
  16. */
  17. class BlockCourse extends Block
  18. {
  19. private $user_id;
  20. private $courses;
  21. private $permission = [DRH];
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct($user_id)
  26. {
  27. $this->user_id = $user_id;
  28. $this->path = 'block_course';
  29. if ($this->is_block_visible_for_user($user_id)) {
  30. $this->courses = CourseManager::get_courses_followed_by_drh($user_id);
  31. }
  32. }
  33. /**
  34. * This method check if a user is allowed to see the block inside dashboard interface.
  35. *
  36. * @param int User id
  37. *
  38. * @return bool Is block visible for user
  39. */
  40. public function is_block_visible_for_user($user_id)
  41. {
  42. $user_info = api_get_user_info($user_id);
  43. $user_status = $user_info['status'];
  44. $is_block_visible_for_user = false;
  45. if (UserManager::is_admin($user_id) || in_array(
  46. $user_status,
  47. $this->permission
  48. )
  49. ) {
  50. $is_block_visible_for_user = true;
  51. }
  52. return $is_block_visible_for_user;
  53. }
  54. /**
  55. * This method return content html containing information
  56. * about courses and its position for showing it inside dashboard interface
  57. * it's important to use the name 'get_block' for beeing used from dashboard controller.
  58. *
  59. * @return array column and content html
  60. */
  61. public function get_block()
  62. {
  63. $column = 2;
  64. $data = [];
  65. $html = $this->getBlockCard(
  66. get_lang('Your courses'),
  67. $this->getContent()
  68. );
  69. $data['column'] = $column;
  70. $data['content_html'] = $html;
  71. return $data;
  72. }
  73. /**
  74. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
  75. *
  76. * @return string content html
  77. */
  78. public function getContent()
  79. {
  80. $course_data = $this->get_course_information_data();
  81. $content = '';
  82. $data_table = null;
  83. if (!empty($course_data)) {
  84. $data_table .= '<table class="data_table" width:"95%">';
  85. $data_table .= '<tr>
  86. <th>'.get_lang('Course title').'</th>
  87. <th width="20%">'.get_lang('Learners').'</th>
  88. <th width="20%">'.get_lang('Time').'</th>
  89. <th width="20%">'.get_lang('Thematic advance').'</th>
  90. </tr>';
  91. $i = 1;
  92. foreach ($course_data as $course) {
  93. if ($i % 2 == 0) {
  94. $class_tr = 'row_odd';
  95. } else {
  96. $class_tr = 'row_even';
  97. }
  98. $data_table .= '<tr class="'.$class_tr.'">';
  99. if (!isset($course[2])) {
  100. $course[2] = '0:00:00';
  101. }
  102. foreach ($course as $cell) {
  103. $data_table .= '<td align="right">'.$cell.'</td>';
  104. }
  105. $data_table .= '</tr>';
  106. $i++;
  107. }
  108. $data_table .= '</table>';
  109. } else {
  110. $data_table .= get_lang('There is no available information about your courses');
  111. }
  112. $content .= $data_table;
  113. if (!empty($course_data)) {
  114. $content .= '<div style="text-align:right;margin-top:10px;">
  115. <a href="'.api_get_path(WEB_CODE_PATH).'mySpace/course.php?follow">'.get_lang('See more').'</a></div>';
  116. }
  117. return $content;
  118. }
  119. /**
  120. * Get number of courses.
  121. *
  122. * @return int
  123. */
  124. public function get_number_of_courses()
  125. {
  126. return count($this->courses);
  127. }
  128. /**
  129. * Get course information data.
  130. *
  131. * @return array
  132. */
  133. public function get_course_information_data()
  134. {
  135. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  136. $course_data = [];
  137. $courses = $this->courses;
  138. $thematic = new Thematic();
  139. foreach ($courses as $row_course) {
  140. $course_code = $row_course['code'];
  141. $courseInfo = api_get_course_info($course_code);
  142. $courseId = $courseInfo['real_id'];
  143. $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
  144. // students directly subscribed to the course
  145. $sql = "SELECT user_id FROM $tbl_course_user as course_rel_user
  146. WHERE course_rel_user.status=".STUDENT." AND course_rel_user.c_id='$courseId'";
  147. $rs = Database::query($sql);
  148. $users = [];
  149. while ($row = Database::fetch_array($rs)) {
  150. $users[] = $row['user_id'];
  151. }
  152. if (count($users) > 0) {
  153. $nb_students_in_course = count($users);
  154. $avg_time_spent_in_course = api_time_to_hms(
  155. Tracking::get_time_spent_on_the_course($users, $courseId) / $nb_students_in_course
  156. );
  157. } else {
  158. $avg_time_spent_in_course = null;
  159. }
  160. $tematic_advance = $thematic->get_total_average_of_thematic_advances(
  161. $course_code,
  162. 0
  163. );
  164. if (!empty($tematic_advance)) {
  165. $tematic_advance_progress = '<a title="'.get_lang('GoToThematic advance').'" href="'.api_get_path(WEB_CODE_PATH).'course_progress/index.php?cidReq='.$course_code.'&action=thematic_details">'.$tematic_advance.'%</a>';
  166. } else {
  167. $tematic_advance_progress = '0%';
  168. }
  169. $table_row = [];
  170. $table_row[] = $row_course['title'];
  171. $table_row[] = $nb_students_in_course;
  172. $table_row[] = $avg_time_spent_in_course;
  173. $table_row[] = $tematic_advance_progress;
  174. $course_data[] = $table_row;
  175. }
  176. return $course_data;
  177. }
  178. }