block_student_graph.class.php 7.2 KB

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