block_course.class.php 7.1 KB

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