block_student_graph.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. use CpChart\Chart\Cache as pCache;
  11. use CpChart\Chart\Data as pData;
  12. use CpChart\Chart\Image as pImage;
  13. /**
  14. * This class is used like controller for student graph block plugin,
  15. * the class name must be registered inside path.info file (e.g: controller = "BlockStudentGraph"), so dashboard controller will be instantiate it
  16. * @package chamilo.dashboard
  17. */
  18. class BlockStudentGraph extends Block
  19. {
  20. private $user_id;
  21. private $students;
  22. private $path;
  23. private $permission = array(DRH);
  24. /**
  25. * Constructor
  26. */
  27. public function __construct ($user_id)
  28. {
  29. $this->user_id = $user_id;
  30. $this->path = 'block_student_graph';
  31. if ($this->is_block_visible_for_user($user_id)) {
  32. /*if (api_is_platform_admin()) {
  33. $this->students = UserManager::get_user_list(array('status' => STUDENT));
  34. } else if (api_is_drh()) {*/
  35. $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
  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. {
  46. $user_info = api_get_user_info($user_id);
  47. $user_status = $user_info['status'];
  48. $is_block_visible_for_user = false;
  49. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  50. $is_block_visible_for_user = true;
  51. }
  52. return $is_block_visible_for_user;
  53. }
  54. /**
  55. * This method return content html containing information about students
  56. * and its position for showing it inside dashboard interface
  57. * it's important to use the name 'get_block' for being used from dashboard controller
  58. * @return array column and content html
  59. */
  60. public function get_block()
  61. {
  62. global $charset;
  63. $column = 1;
  64. $data = array();
  65. $students_attendance_graph = $this->get_students_attendance_graph();
  66. $html = '<div class="panel panel-default" id="intro">
  67. <div class="panel-heading">
  68. '.get_lang('StudentsInformationsGraph').'
  69. <div class="pull-right"><a class="btn btn-danger btn-xs" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)).'\')) return false;" href="index.php?action=disable_block&path='.$this->path.'">
  70. <em class="fa fa-times"></em>
  71. </a></div>
  72. </div>
  73. <div class="panel-body" align="center">
  74. <div style="padding:10px;"><strong>'.get_lang('AttendancesFaults').'</strong></div>
  75. '.$students_attendance_graph.'
  76. </div>
  77. </div>';
  78. $data['column'] = $column;
  79. $data['content_html'] = $html;
  80. return $data;
  81. }
  82. /**
  83. * This method return a graph containing information about students evaluation,
  84. * it's used inside get_block method for showing it inside dashboard interface
  85. * @return string img html
  86. */
  87. public function get_students_attendance_graph()
  88. {
  89. $students = $this->students;
  90. $attendance = new Attendance();
  91. // get data
  92. $attendances_faults_avg = array();
  93. if (is_array($students) && count($students) > 0) {
  94. foreach ($students as $student) {
  95. $student_id = $student['user_id'];
  96. //$student_info = api_get_user_info($student_id);
  97. // get average of faults in attendances by student
  98. $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
  99. if (!empty($results_faults_avg)) {
  100. $attendances_faults_avg[$student['lastname']] = $results_faults_avg['porcent'];
  101. } else {
  102. $attendances_faults_avg[$student['lastname']] = 0;
  103. }
  104. }
  105. }
  106. arsort($attendances_faults_avg);
  107. $usernames = array_keys($attendances_faults_avg);
  108. $faults = array();
  109. foreach ($usernames as $username) {
  110. $faults[] = $attendances_faults_avg[$username];
  111. }
  112. $graph = '';
  113. $img_file = '';
  114. if (is_array($usernames) && count($usernames) > 0) {
  115. // Defining data
  116. $dataSet = new pData();
  117. $dataSet->addPoints($faults, 'Serie1');
  118. $dataSet->addPoints($usernames, 'Labels');
  119. $dataSet->setSerieDescription('Series1', get_lang('Average'));
  120. $dataSet->setSerieDescription('Labels', get_lang('User'));
  121. $dataSet->setAbscissa('Labels');
  122. $dataSet->setAbscissaName(get_lang('User'));
  123. $dataSet->setAxisName(0, get_lang('Attendance'));
  124. $palette = array(
  125. '0' => array('R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100),
  126. '1' => array('R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100),
  127. '2' => array('R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100),
  128. '3' => array('R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100),
  129. '4' => array('R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100),
  130. '5' => array('R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100),
  131. '6' => array('R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100),
  132. '7' => array('R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100),
  133. '8' => array('R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100),
  134. );
  135. // Cache definition
  136. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  137. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  138. $chartHash = $myCache->getHash($dataSet);
  139. if ($myCache->isInCache($chartHash)) {
  140. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  141. $myCache->saveFromCache($chartHash, $imgPath);
  142. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  143. } else {
  144. $maxCounts = max(count($usernames), count($faults));
  145. if ($maxCounts < 5) {
  146. $heightSize = 200;
  147. } else {
  148. $heightSize = $maxCounts * 40;
  149. }
  150. /* Create the pChart object */
  151. $widthSize = 480;
  152. $angle = 40;
  153. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  154. /* Turn of Antialiasing */
  155. $myPicture->Antialias = false;
  156. /* Add a border to the picture */
  157. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, array('R' => 0, 'G' => 0, 'B' => 0));
  158. /* Set the default font */
  159. $myPicture->setFontProperties(
  160. array(
  161. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  162. 'FontSize' => 10
  163. )
  164. );
  165. /* Do NOT Write the chart title */
  166. /* Define the chart area */
  167. $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40);
  168. /* Draw the scale */
  169. $scaleSettings = array(
  170. 'GridR' => 200,
  171. 'GridG' => 200,
  172. 'GridB' => 200,
  173. 'DrawSubTicks' => true,
  174. 'CycleBackground' => true,
  175. 'Mode' => SCALE_MODE_ADDALL_START0,
  176. 'Pos' => SCALE_POS_TOPBOTTOM,
  177. 'DrawXLines' => false,
  178. 'LabelRotation' => $angle,
  179. );
  180. $myPicture->drawScale($scaleSettings);
  181. /* Turn on shadow computing */
  182. $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
  183. /* Draw the chart */
  184. $myPicture->setShadow(true, array('X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10));
  185. $settings = array(
  186. 'OverrideColors' => $palette,
  187. 'Gradient' => false,
  188. 'GradientMode' => GRADIENT_SIMPLE,
  189. 'DisplayPos' => LABEL_POS_TOP,
  190. 'DisplayValues' => true,
  191. 'DisplayR' => 0,
  192. 'DisplayG' => 0,
  193. 'DisplayB' => 0,
  194. 'DisplayShadow' => true,
  195. 'Surrounding' => 10,
  196. );
  197. $myPicture->drawBarChart($settings);
  198. /* Write and save into cache */
  199. $myCache->writeToCache($chartHash, $myPicture);
  200. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  201. $myCache->saveFromCache($chartHash, $imgPath);
  202. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  203. }
  204. $graph = '<img src="' . $imgPath . '" >';
  205. } else {
  206. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  207. }
  208. return $graph;
  209. }
  210. /**
  211. * Get number of students
  212. * @return int
  213. */
  214. function get_number_of_students()
  215. {
  216. return count($this->students);
  217. }
  218. }