block_teacher_graph.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. use CpChart\Cache as pCache;
  3. use CpChart\Data as pData;
  4. use CpChart\Image as pImage;
  5. /**
  6. * This file is part of teacher graph block plugin for dashboard,
  7. * it should be required inside dashboard controller for showing it into dashboard interface from plattform.
  8. *
  9. * @package chamilo.dashboard
  10. *
  11. * @author Christian Fasanando
  12. */
  13. /**
  14. * required files for getting data.
  15. */
  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"),
  19. * so dashboard controller will be instantiate it.
  20. *
  21. * @package chamilo.dashboard
  22. */
  23. class BlockTeacherGraph extends Block
  24. {
  25. private $user_id;
  26. private $teachers;
  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. $column = 1;
  65. $data = [];
  66. $html = $this->getBlockCard(
  67. get_lang('Teachers report chart'),
  68. $this->getContent()
  69. );
  70. $data['column'] = $column;
  71. $data['content_html'] = $html;
  72. return $data;
  73. }
  74. /**
  75. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface.
  76. *
  77. * @return string content html
  78. */
  79. public function getContent()
  80. {
  81. $teachers = $this->teachers;
  82. $graph = '';
  83. $user_ids = array_keys($teachers);
  84. $a_last_week = get_last_week();
  85. if (is_array($user_ids) && count($user_ids) > 0) {
  86. $dataSet = new pData();
  87. foreach ($user_ids as $user_id) {
  88. $teacher_info = api_get_user_info($user_id);
  89. $username = $teacher_info['username'];
  90. $time_by_days = [];
  91. foreach ($a_last_week as $day) {
  92. // day is received as y-m-d 12:00:00
  93. $start_date = api_get_utc_datetime($day);
  94. $end_date = api_get_utc_datetime($day + (3600 * 24 - 1));
  95. $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform(
  96. $user_id,
  97. 'custom',
  98. $start_date,
  99. $end_date
  100. );
  101. $hours = floor($time_on_platform_by_day / 3600);
  102. $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
  103. $time_by_days[] = $min;
  104. }
  105. $dataSet->addPoints($time_by_days, $username);
  106. }
  107. $last_week = date('Y-m-d', $a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
  108. $days_on_week = [];
  109. foreach ($a_last_week as $weekday) {
  110. $days_on_week[] = date('d/m', $weekday);
  111. }
  112. $dataSet->addPoints($days_on_week, 'Days');
  113. $dataSet->setAbscissaName($last_week);
  114. $dataSet->setAxisName(0, get_lang('Minutes'));
  115. $dataSet->setAbscissa('Days');
  116. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color', true);
  117. // Cache definition
  118. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  119. $myCache = new pCache(['CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)]);
  120. $chartHash = $myCache->getHash($dataSet);
  121. if ($myCache->isInCache($chartHash)) {
  122. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  123. $myCache->saveFromCache($chartHash, $imgPath);
  124. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  125. } else {
  126. /* Create the pChart object */
  127. $widthSize = 440;
  128. $heightSize = 350;
  129. $angle = 50;
  130. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  131. /* Turn of Antialiasing */
  132. $myPicture->Antialias = false;
  133. /* Add a border to the picture */
  134. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
  135. /* Set the default font */
  136. $myPicture->setFontProperties(['FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf', 'FontSize' => 10]);
  137. /* Do NOT Write the chart title */
  138. /* Define the chart area */
  139. $myPicture->setGraphArea(40, 40, $widthSize - 20, $heightSize - 80);
  140. /* Draw the scale */
  141. $scaleSettings = [
  142. 'GridR' => 200,
  143. 'GridG' => 200,
  144. 'GridB' => 200,
  145. 'DrawSubTicks' => true,
  146. 'CycleBackground' => true,
  147. 'Mode' => SCALE_MODE_ADDALL_START0,
  148. 'LabelRotation' => $angle,
  149. ];
  150. $myPicture->drawScale($scaleSettings);
  151. /* Turn on shadow computing */
  152. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  153. /* Draw the chart */
  154. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  155. $settings = [
  156. 'DisplayValues' => true,
  157. 'DisplayR' => 0,
  158. 'DisplayG' => 0,
  159. 'DisplayB' => 0,
  160. ];
  161. $myPicture->drawFilledSplineChart($settings);
  162. $myPicture->drawLegend(40, 20, ['Mode' => LEGEND_HORIZONTAL]);
  163. /* Write and save into cache */
  164. $myCache->writeToCache($chartHash, $myPicture);
  165. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  166. $myCache->saveFromCache($chartHash, $imgPath);
  167. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  168. }
  169. $graph = '<img src="'.$imgPath.'" >';
  170. } else {
  171. $graph = '<p>'.api_convert_encoding(get_lang('Graphic not available'), 'UTF-8').'</p>';
  172. }
  173. return $graph;
  174. }
  175. /**
  176. * Get number of teachers.
  177. *
  178. * @return int
  179. */
  180. public function get_number_of_teachers()
  181. {
  182. return count($this->teachers);
  183. }
  184. }