block_teacher_graph.class.php 7.8 KB

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