block_teacher_graph.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'tracking.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  15. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  16. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  17. /**
  18. * This class is used like controller for teacher graph block plugin,
  19. * the class name must be registered inside path.info file (e.g: controller = "BlockTeacherGraph"), so dashboard controller will be instantiate it
  20. * @package chamilo.dashboard
  21. */
  22. class BlockTeacherGraph extends Block
  23. {
  24. private $user_id;
  25. private $teachers;
  26. private $path;
  27. private $permission = array(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. * @param int User id
  42. * @return bool Is block visible for user
  43. */
  44. public function is_block_visible_for_user($user_id)
  45. {
  46. $user_info = api_get_user_info($user_id);
  47. $user_status = $user_info['status'];
  48. $is_block_visible_for_user = false;
  49. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  50. $is_block_visible_for_user = true;
  51. }
  52. return $is_block_visible_for_user;
  53. }
  54. /**
  55. * This method return content html containing information about teachers and its position for showing it inside dashboard interface
  56. * it's important to use the name 'get_block' for beeing used from dashboard controller
  57. * @return array column and content html
  58. */
  59. public function get_block()
  60. {
  61. global $charset;
  62. $column = 1;
  63. $data = array();
  64. $teacher_information_graph = $this->get_teachers_information_graph();
  65. $html = '
  66. <li class="widget color-blue" id="intro">
  67. <div class="widget-head">
  68. <h3>'.get_lang('TeachersInformationsGraph').'</h3>
  69. <div class="widget-actions"><a onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)).'\')) return false;" href="index.php?action=disable_block&path='.$this->path.'">'.Display::return_icon('close.gif',get_lang('Close')).'</a></div>
  70. </div>
  71. <div class="widget-content" align="center">
  72. <div style="padding:10px;"><strong>'.get_lang('TimeSpentOnThePlatformLastWeekByDay').'</strong></div>
  73. '.$teacher_information_graph.'
  74. </div>
  75. </li>
  76. ';
  77. $data['column'] = $column;
  78. $data['content_html'] = $html;
  79. return $data;
  80. }
  81. /**
  82. * This method return a content html, it's used inside get_block method for showing it inside dashboard interface
  83. * @return string content html
  84. */
  85. public function get_teachers_information_graph()
  86. {
  87. $teachers = $this->teachers;
  88. $graph = '';
  89. $user_ids = array_keys($teachers);
  90. $a_last_week = get_last_week();
  91. if (is_array($user_ids) && count($user_ids) > 0) {
  92. $data_set = new pData;
  93. foreach ($user_ids as $user_id) {
  94. $teacher_info = api_get_user_info($user_id);
  95. $username = $teacher_info['username'];
  96. $time_by_days = array();
  97. foreach ($a_last_week as $day) {
  98. // day is received as y-m-d 12:00:00
  99. $start_date = api_get_utc_datetime($day);
  100. $end_date = api_get_utc_datetime($day+(3600*24-1));
  101. $time_on_platform_by_day = Tracking::get_time_spent_on_the_platform($user_id, 'custom', $start_date, $end_date);
  102. $hours = floor($time_on_platform_by_day / 3600);
  103. $min = floor(($time_on_platform_by_day - ($hours * 3600)) / 60);
  104. $time_by_days[] = $min;
  105. }
  106. $data_set->AddPoint($time_by_days,$username);
  107. $data_set->AddSerie($username);
  108. }
  109. $last_week = date('Y-m-d',$a_last_week[0]).' '.get_lang('To').' '.date('Y-m-d', $a_last_week[6]);
  110. $days_on_week = array();
  111. foreach ($a_last_week as $weekday) {
  112. $days_on_week[] = date('d/m',$weekday);
  113. }
  114. $data_set->AddPoint($days_on_week,"Days");
  115. $data_set->SetXAxisName($last_week);
  116. $data_set->SetYAxisName(get_lang('Minutes'));
  117. $data_set->SetAbsciseLabelSerie("Days");
  118. $graph_id = $this->user_id.'TeacherConnectionsGraph';
  119. $cache = new pCache();
  120. // the graph id
  121. $data = $data_set->GetData();
  122. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  123. //if we already created the img
  124. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  125. } else {
  126. // Initializing the graph
  127. $bg_width = 440;
  128. $bg_height = 350;
  129. $angle = -30;
  130. $test = new pChart($bg_width+10,$bg_height+20);
  131. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  132. $test = $test->fixHeightByRotation($data_set->GetData(), $data_set->GetDataDescription(), $angle);
  133. $test->setGraphArea(65,30,$bg_width-70,$bg_height-50);
  134. $test->drawFilledRoundedRectangle(7,7,$bg_width,$bg_height,5,240,240,240);
  135. $test->drawRoundedRectangle(5,5,$bg_width+2,$bg_height+2,5,230,230,230);
  136. $test->drawGraphArea(255,255,255,TRUE);
  137. $test->drawScale(
  138. $data_set->GetData(),
  139. $data_set->GetDataDescription(),
  140. SCALE_NORMAL,
  141. 150,
  142. 150,
  143. 150,
  144. TRUE,
  145. $angle,
  146. 2,
  147. TRUE
  148. );
  149. $test->drawGrid(4,TRUE,230,230,230,50);
  150. // Drawing lines
  151. //$test->drawLineGraph($data_set->GetData(),$data_set->GetDataDescription());
  152. $test->drawFilledCubicCurve($data_set->GetData(),$data_set->GetDataDescription(),.1,30);
  153. //$test->drawPlotGraph($data_set->GetData(),$data_set->GetDataDescription(),3,2,255,255,255);
  154. // Drawing Legend
  155. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  156. $test->drawLegend($bg_width-80,20,$data_set->GetDataDescription(),204,204,255);
  157. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Days"));
  158. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  159. ob_start();
  160. $test->Stroke();
  161. ob_end_clean();
  162. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  163. }
  164. if (!empty($img_file)) {
  165. $graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  166. }
  167. } else {
  168. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  169. }
  170. return $graph;
  171. }
  172. /**
  173. * Get number of teachers
  174. * @return int
  175. */
  176. function get_number_of_teachers()
  177. {
  178. return count($this->teachers);
  179. }
  180. }