block_teacher_graph.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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).'tracking.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  13. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  14. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  15. /**
  16. * This class is used like controller for teacher graph block plugin,
  17. * the class name must be registered inside path.info file (e.g: controller = "BlockTeacherGraph"), so dashboard controller will be instantiate it
  18. * @package chamilo.dashboard
  19. */
  20. class BlockTeacherGraph extends Block {
  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. $this->user_id = $user_id;
  30. $this->path = 'block_teacher_graph';
  31. if ($this->is_block_visible_for_user($user_id)) {
  32. /*if (api_is_platform_admin()) {
  33. $this->teachers = UserManager::get_user_list(array('status' => COURSEMANAGER));
  34. } else {*/
  35. $this->teachers = UserManager::get_users_followed_by_drh($user_id, COURSEMANAGER);
  36. //}
  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. $user_info = api_get_user_info($user_id);
  46. $user_status = $user_info['status'];
  47. $is_block_visible_for_user = false;
  48. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  49. $is_block_visible_for_user = true;
  50. }
  51. return $is_block_visible_for_user;
  52. }
  53. /**
  54. * This method return content html containing information about teachers and its position for showing it inside dashboard interface
  55. * it's important to use the name 'get_block' for beeing used from dashboard controller
  56. * @return array column and content html
  57. */
  58. public function get_block() {
  59. global $charset;
  60. $column = 1;
  61. $data = array();
  62. /*if (api_is_platform_admin()) {
  63. $teacher_content_html = $this->get_teachers_content_html_for_platform_admin();
  64. } else if (api_is_drh()) {*/
  65. $teacher_information_graph = $this->get_teachers_information_graph();
  66. //}
  67. $html = '<li class="widget color-blue" id="intro">
  68. <div class="widget-head">
  69. <h3>'.get_lang('TeachersInformationsGraph').'</h3>
  70. <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>
  71. </div>
  72. <div class="widget-content" align="center">
  73. <div style="padding:10px;"><strong>'.get_lang('TimeSpentOnThePlatformLastWeekByDay').'</strong></div>
  74. '.$teacher_information_graph.'
  75. </div>
  76. </li>';
  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. $graph = '';
  87. $user_ids = array_keys($this->teachers);
  88. $a_last_week = get_last_week();
  89. if (is_array($user_ids) && count($user_ids) > 0) {
  90. $data_set = 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. $date = new DateTime(api_get_utc_datetime($day));
  97. $start_date = $date->format('Y-m-d').' 00:00:00';
  98. $end_date = $date->format('Y-m-d').' 23:59:59';
  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. $data_set->AddPoint($time_by_days, $username);
  105. $data_set->AddSerie($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 = array();
  109. foreach ($a_last_week as $weekday) {
  110. $days_on_week[] = date('d/m',$weekday);
  111. }
  112. $data_set->AddPoint($days_on_week,"Days");
  113. $data_set->SetXAxisName($last_week);
  114. $data_set->SetYAxisName(get_lang('Minutes'));
  115. $data_set->SetAbsciseLabelSerie("Days");
  116. $graph_id = $this->user_id.'TeacherConnectionsGraph';
  117. $cache = new pCache();
  118. // the graph id
  119. $data = $data_set->GetData();
  120. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  121. //if we already created the img
  122. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  123. } else {
  124. // Initializing the graph
  125. $bg_width = 440;
  126. $bg_height = 350;
  127. $test = new pChart($bg_width+10,$bg_height+20);
  128. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  129. $test->setGraphArea(65,30,$bg_width-70,$bg_height-50);
  130. $test->drawFilledRoundedRectangle(7,7,$bg_width,$bg_height,5,240,240,240);
  131. $test->drawRoundedRectangle(5,5,$bg_width+2,$bg_height+2,5,230,230,230);
  132. $test->drawGraphArea(255,255,255,TRUE);
  133. $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
  134. $test->drawGrid(4,TRUE,230,230,230,50);
  135. // Drawing lines
  136. //$test->drawLineGraph($data_set->GetData(),$data_set->GetDataDescription());
  137. $test->drawFilledCubicCurve($data_set->GetData(),$data_set->GetDataDescription(),.1,30);
  138. //$test->drawPlotGraph($data_set->GetData(),$data_set->GetDataDescription(),3,2,255,255,255);
  139. // Drawing Legend
  140. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  141. $test->drawLegend($bg_width-80,20,$data_set->GetDataDescription(),204,204,255);
  142. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Days"));
  143. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  144. ob_start();
  145. $test->Stroke();
  146. ob_end_clean();
  147. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  148. }
  149. if (!empty($img_file)) {
  150. $graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  151. }
  152. } else {
  153. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  154. }
  155. return $graph;
  156. }
  157. /**
  158. * Get number of teachers
  159. * @return int
  160. */
  161. function get_number_of_teachers() {
  162. return count($this->teachers);
  163. }
  164. }