block_teacher_graph.class.php 6.6 KB

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