block_course.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 $path;
  22. private $permission = [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. *
  37. * @param int User id
  38. *
  39. * @return bool Is block visible for user
  40. */
  41. public function is_block_visible_for_user($user_id)
  42. {
  43. $user_info = api_get_user_info($user_id);
  44. $user_status = $user_info['status'];
  45. $is_block_visible_for_user = false;
  46. if (UserManager::is_admin($user_id) || in_array(
  47. $user_status,
  48. $this->permission
  49. )
  50. ) {
  51. $is_block_visible_for_user = true;
  52. }
  53. return $is_block_visible_for_user;
  54. }
  55. /**
  56. * This method return content html containing information
  57. * about courses and its position for showing it inside dashboard interface
  58. * it's important to use the name 'get_block' for beeing used from dashboard controller.
  59. *
  60. * @return array column and content html
  61. */
  62. public function get_block()
  63. {
  64. global $charset;
  65. $column = 2;
  66. $data = [];
  67. $content = $this->get_content_html();
  68. $html = '
  69. <div class="panel panel-default" id="intro">
  70. <div class="panel-heading">'.get_lang('CoursesInformation').'
  71. <div class="pull-right"><a class="btn btn-danger btn-xs" onclick="javascript:if(!confirm(\''.addslashes(
  72. api_htmlentities(
  73. get_lang('ConfirmYourChoice'),
  74. ENT_QUOTES,
  75. $charset
  76. )
  77. ).'\')) return false;" href="index.php?action=disable_block&path='.$this->path.'">
  78. <em class="fa fa-times"></em>
  79. </a></div>
  80. </div>
  81. <div class="panel-body">
  82. '.$content.'
  83. </div>
  84. </div>
  85. ';
  86. $data['column'] = $column;
  87. $data['content_html'] = $html;
  88. return $data;
  89. }
  90. /**
  91. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
  92. *
  93. * @return string content html
  94. */
  95. public function get_content_html()
  96. {
  97. $course_data = $this->get_course_information_data();
  98. $content = '<h4>'.get_lang('YourCourseList').'</h4>';
  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?follow">'.get_lang('SeeMore').'</a></div>';
  132. }
  133. //$content .= '</div>';
  134. return $content;
  135. }
  136. /**
  137. * Get number of courses.
  138. *
  139. * @return int
  140. */
  141. public function get_number_of_courses()
  142. {
  143. return count($this->courses);
  144. }
  145. /**
  146. * Get course information data.
  147. *
  148. * @return array
  149. */
  150. public function get_course_information_data()
  151. {
  152. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  153. $course_data = [];
  154. $courses = $this->courses;
  155. $thematic = new Thematic();
  156. foreach ($courses as $row_course) {
  157. $course_code = $row_course['code'];
  158. $courseInfo = api_get_course_info($course_code);
  159. $courseId = $courseInfo['real_id'];
  160. $nb_students_in_course = $avg_progress_in_course = $avg_score_in_course = $avg_time_spent_in_course = $avg_score_in_exercise = 0;
  161. // students directly subscribed to the course
  162. $sql = "SELECT user_id FROM $tbl_course_user as course_rel_user
  163. WHERE course_rel_user.status=".STUDENT." AND course_rel_user.c_id='$courseId'";
  164. $rs = Database::query($sql);
  165. $users = [];
  166. while ($row = Database::fetch_array($rs)) {
  167. $users[] = $row['user_id'];
  168. }
  169. if (count($users) > 0) {
  170. $nb_students_in_course = count($users);
  171. $avg_time_spent_in_course = api_time_to_hms(
  172. Tracking::get_time_spent_on_the_course($users, $courseId) / $nb_students_in_course
  173. );
  174. } else {
  175. $avg_time_spent_in_course = null;
  176. }
  177. $tematic_advance = $thematic->get_total_average_of_thematic_advances(
  178. $course_code,
  179. 0
  180. );
  181. if (!empty($tematic_advance)) {
  182. $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>';
  183. } else {
  184. $tematic_advance_progress = '0%';
  185. }
  186. $table_row = [];
  187. $table_row[] = $row_course['title'];
  188. $table_row[] = $nb_students_in_course;
  189. $table_row[] = $avg_time_spent_in_course;
  190. $table_row[] = $tematic_advance_progress;
  191. $course_data[] = $table_row;
  192. }
  193. return $course_data;
  194. }
  195. }