block_teacher_graph.class.php 7.7 KB

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