block_student_graph.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is part of student graph block plugin for dashboard,
  5. * it should be required inside dashboard controller for showing it into dashboard interface from plattform
  6. * @package chamilo.dashboard
  7. * @author Christian Fasanando
  8. * @author Julio Montoya <gugli100@gmail.com>
  9. */
  10. /**
  11. * required files for getting data
  12. */
  13. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  14. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  15. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
  16. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php';
  17. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/category.class.php';
  18. /**
  19. * This class is used like controller for student graph block plugin,
  20. * the class name must be registered inside path.info file (e.g: controller = "BlockStudentGraph"), so dashboard controller will be instantiate it
  21. * @package chamilo.dashboard
  22. */
  23. class BlockStudentGraph extends Block {
  24. private $user_id;
  25. private $students;
  26. private $path;
  27. private $permission = array(DRH);
  28. /**
  29. * Constructor
  30. */
  31. public function __construct ($user_id) {
  32. $this->user_id = $user_id;
  33. $this->path = 'block_student_graph';
  34. if ($this->is_block_visible_for_user($user_id)) {
  35. /*if (api_is_platform_admin()) {
  36. $this->students = UserManager::get_user_list(array('status' => STUDENT));
  37. } else if (api_is_drh()) {*/
  38. $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
  39. //}
  40. }
  41. }
  42. /**
  43. * This method check if a user is allowed to see the block inside dashboard interface
  44. * @param int User id
  45. * @return bool Is block visible for user
  46. */
  47. public function is_block_visible_for_user($user_id) {
  48. $user_info = api_get_user_info($user_id);
  49. $user_status = $user_info['status'];
  50. $is_block_visible_for_user = false;
  51. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  52. $is_block_visible_for_user = true;
  53. }
  54. return $is_block_visible_for_user;
  55. }
  56. /**
  57. * This method return content html containing information about students and its position for showing it inside dashboard interface
  58. * it's important to use the name 'get_block' for beeing used from dashboard controller
  59. * @return array column and content html
  60. */
  61. public function get_block() {
  62. global $charset;
  63. $column = 1;
  64. $data = array();
  65. $students_attendance_graph = $this->get_students_attendance_graph();
  66. $html = '<li class="widget color-orange" id="intro">
  67. <div class="widget-head">
  68. <h3>'.get_lang('StudentsInformationsGraph').'</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('AttendancesFaults').'</strong></div>
  73. '.$students_attendance_graph.'
  74. </div>
  75. </li>';
  76. $data['column'] = $column;
  77. $data['content_html'] = $html;
  78. return $data;
  79. }
  80. /**
  81. * This method return a graph containing informations about students evaluation, it's used inside get_block method for showing it inside dashboard interface
  82. * @return string img html
  83. */
  84. public function get_students_attendance_graph() {
  85. $students = $this->students;
  86. $attendance = new Attendance();
  87. // get data
  88. $attendances_faults_avg = array();
  89. if (is_array($students) && count($students) > 0) {
  90. foreach ($students as $student) {
  91. $student_id = $student['user_id'];
  92. //$student_info = api_get_user_info($student_id);
  93. // get average of faults in attendances by student
  94. $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
  95. if (!empty($results_faults_avg)) {
  96. $attendances_faults_avg[$student['lastname']] = $results_faults_avg['porcent'];
  97. } else {
  98. $attendances_faults_avg[$student['lastname']] = 0;
  99. }
  100. }
  101. }
  102. arsort($attendances_faults_avg);
  103. $usernames = array_keys($attendances_faults_avg);
  104. $faults = array();
  105. foreach ($usernames as $username) {
  106. $faults[] = $attendances_faults_avg[$username];
  107. }
  108. $graph = '';
  109. $img_file = '';
  110. if (is_array($usernames) && count($usernames) > 0) {
  111. // Defining data
  112. $data_set = new pData;
  113. $data_set->AddPoint($faults,"Promedio");
  114. $data_set->AddPoint($usernames,"Usuario");
  115. $data_set->AddAllSeries();
  116. $data_set->SetAbsciseLabelSerie("Usuario");
  117. // prepare cache for saving image
  118. $graph_id = $this->user_id.'StudentEvaluationGraph'; // the graph id
  119. $cache = new pCache(api_get_path(SYS_ARCHIVE_PATH));
  120. $data = $data_set->GetData(); // return $this->DataDescription
  121. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  122. //if (0) {
  123. //if we already created the img
  124. $img_file = $cache->GetHash($graph_id, $data_set->GetData()); // image file with hash
  125. } else {
  126. if (count($usernames) < 5) {
  127. $height = 200;
  128. } else {
  129. $height = (count($usernames)*40);
  130. }
  131. // Initialise the graph
  132. $test = new MyHorBar(400,($height+30));
  133. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 8);
  134. $test->setGraphArea(100,30,370,$height);
  135. $test->drawFilledRoundedRectangle(7,7,393,$height,5,240,240,240);
  136. $test->drawRoundedRectangle(5,5,395,$height,5,230,230,230);
  137. $test->drawGraphArea(255,255,255,TRUE);
  138. //X axis
  139. $test->setFixedScale(0,100,10);
  140. //var_dump($data_set->GetDataDescription());
  141. $test->drawHorScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,0,TRUE);
  142. $test->setColorPalette(0,255,0,0);
  143. $test->drawHorGrid(10,TRUE,230,230,230,50);
  144. // Draw the 0 line
  145. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 6);
  146. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  147. // Draw the bar graph
  148. $test->drawHorBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE,50);
  149. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  150. ob_start();
  151. $test->Stroke();
  152. ob_end_clean();
  153. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  154. }
  155. if (!empty($img_file)) {
  156. $graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  157. }
  158. } else {
  159. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  160. }
  161. return $graph;
  162. }
  163. /**
  164. * Get number of students
  165. * @return int
  166. */
  167. function get_number_of_students() {
  168. return count($this->students);
  169. }
  170. }