block_course.class.php 7.2 KB

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