block_teacher_graph.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * This file is part of teacher graph 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. * required files for getting data.
  12. */
  13. use CpChart\Cache as pCache;
  14. use CpChart\Data as pData;
  15. use CpChart\Image as pImage;
  16. /**
  17. * This class is used like controller for teacher graph block plugin,
  18. * the class name must be registered inside path.info file (e.g: controller = "BlockTeacherGraph"), so dashboard controller will be instantiate it.
  19. *
  20. * @package chamilo.dashboard
  21. */
  22. class BlockTeacherGraph extends Block
  23. {
  24. private $user_id;
  25. private $teachers;
  26. private $path;
  27. private $permission = [DRH];
  28. /**
  29. * Controller.
  30. */
  31. public function __construct($user_id)
  32. {
  33. $this->user_id = $user_id;
  34. $this->path = 'block_teacher_graph';
  35. if ($this->is_block_visible_for_user($user_id)) {
  36. $this->teachers = UserManager::get_users_followed_by_drh($user_id, COURSEMANAGER);
  37. }
  38. }
  39. /**
  40. * This method check if a user is allowed to see the block inside dashboard interface.
  41. *
  42. * @param int User id
  43. *
  44. * @return bool Is block visible for user
  45. */
  46. public function is_block_visible_for_user($user_id)
  47. {
  48. $user_info = api_get_user_info($user_id);
  49. $user_status = $user_info['status'];
  50. $is_block_visible_for_user = false;
  51. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  52. $is_block_visible_for_user = true;
  53. }
  54. return $is_block_visible_for_user;
  55. }
  56. /**
  57. * This method return content html containing information about teachers 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 = 1;
  66. $data = [];
  67. $teacher_information_graph = $this->get_teachers_information_graph();
  68. $html = '
  69. <div class="panel panel-default" id="intro">
  70. <div class="panel-heading">'.get_lang('TeachersInformationsGraph').'
  71. <div class="pull-right"><a class="btn btn-danger btn-xs" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).'\')) 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" align="center">
  76. <div style="padding:10px;"><strong>'.get_lang('TimeSpentOnThePlatformLastWeekByDay').'</strong></div>
  77. '.$teacher_information_graph.'
  78. </div>
  79. </div>
  80. ';
  81. $data['column'] = $column;
  82. $data['content_html'] = $html;
  83. return $data;
  84. }
  85. /**
  86. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
  87. *
  88. * @return string content html
  89. */
  90. public function get_teachers_information_graph()
  91. {
  92. $teachers = $this->teachers;
  93. $graph = '';
  94. $user_ids = array_keys($teachers);
  95. $a_last_week = get_last_week();
  96. if (is_array($user_ids) && count($user_ids) > 0) {
  97. $dataSet = new pData();
  98. foreach ($user_ids as $user_id) {
  99. $teacher_info = api_get_user_info($user_id);
  100. $username = $teacher_info['username'];
  101. $time_by_days = [];
  102. foreach ($a_last_week as $day) {
  103. // day is received as y-m-d 12:00:00
  104. $start_date = api_get_utc_datetime($day);
  105. $end_date = api_get_utc_datetime($day + (3600 * 24 - 1));
  106. $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform(
  107. $user_id,
  108. 'custom',
  109. $start_date,
  110. $end_date
  111. );
  112. $hours = floor($time_on_platform_by_day / 3600);
  113. $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
  114. $time_by_days[] = $min;
  115. }
  116. $dataSet->addPoints($time_by_days, $username);
  117. }
  118. $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
  119. $days_on_week = [];
  120. foreach ($a_last_week as $weekday) {
  121. $days_on_week[] = date('d/m', $weekday);
  122. }
  123. $dataSet->addPoints($days_on_week, 'Days');
  124. $dataSet->setAbscissaName($last_week);
  125. $dataSet->setAxisName(0, get_lang('Minutes'));
  126. $dataSet->setAbscissa('Days');
  127. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
  128. // Cache definition
  129. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  130. $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
  131. $chartHash = $myCache->getHash($dataSet);
  132. if ($myCache->isInCache($chartHash)) {
  133. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  134. $myCache->saveFromCache($chartHash, $imgPath);
  135. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  136. } else {
  137. /* Create the pChart object */
  138. $widthSize = 440;
  139. $heightSize = 350;
  140. $angle = 50;
  141. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  142. /* Turn of Antialiasing */
  143. $myPicture->Antialias = false;
  144. /* Add a border to the picture */
  145. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
  146. /* Set the default font */
  147. $myPicture->setFontProperties(['FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', 'FontSize' => 10]);
  148. /* Do NOT Write the chart title */
  149. /* Define the chart area */
  150. $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80);
  151. /* Draw the scale */
  152. $scaleSettings = [
  153. 'GridR' => 200,
  154. 'GridG' => 200,
  155. 'GridB' => 200,
  156. 'DrawSubTicks' => true,
  157. 'CycleBackground' => true,
  158. 'Mode' => SCALE_MODE_ADDALL_START0,
  159. 'LabelRotation' => $angle,
  160. ];
  161. $myPicture->drawScale($scaleSettings);
  162. /* Turn on shadow computing */
  163. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  164. /* Draw the chart */
  165. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  166. $settings = [
  167. 'DisplayValues' => true,
  168. 'DisplayR' => 0,
  169. 'DisplayG' => 0,
  170. 'DisplayB' => 0,
  171. ];
  172. $myPicture->drawFilledSplineChart($settings);
  173. $myPicture->drawLegend(40, 20, ['Mode' => LEGEND_HORIZONTAL]);
  174. /* Write and save into cache */
  175. $myCache->writeToCache($chartHash, $myPicture);
  176. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  177. $myCache->saveFromCache($chartHash, $imgPath);
  178. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  179. }
  180. $graph = '<img src="'.$imgPath.'" >';
  181. } else {
  182. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
  183. }
  184. return $graph;
  185. }
  186. /**
  187. * Get number of teachers.
  188. *
  189. * @return int
  190. */
  191. public function get_number_of_teachers()
  192. {
  193. return count($this->teachers);
  194. }
  195. }