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