block_student_graph.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. *
  7. * @package chamilo.dashboard
  8. *
  9. * @author Christian Fasanando
  10. * @author Julio Montoya <gugli100@gmail.com>
  11. */
  12. use CpChart\Cache as pCache;
  13. use CpChart\Data as pData;
  14. use CpChart\Image as pImage;
  15. /**
  16. * This class is used like controller for student graph block plugin,
  17. * the class name must be registered inside path.info file
  18. * (e.g: controller = "BlockStudentGraph"), so dashboard controller will be instantiate it.
  19. *
  20. * @package chamilo.dashboard
  21. */
  22. class BlockStudentGraph extends Block
  23. {
  24. private $user_id;
  25. private $students;
  26. private $path;
  27. private $permission = [DRH];
  28. /**
  29. * Constructor.
  30. */
  31. public function __construct($user_id)
  32. {
  33. $this->user_id = $user_id;
  34. $this->path = 'block_student_graph';
  35. if ($this->is_block_visible_for_user($user_id)) {
  36. /*if (api_is_platform_admin()) {
  37. $this->students = UserManager::get_user_list(array('status' => STUDENT));
  38. } else if (api_is_drh()) {*/
  39. $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
  40. //}
  41. }
  42. }
  43. /**
  44. * This method check if a user is allowed to see the block inside dashboard interface.
  45. *
  46. * @param int User id
  47. *
  48. * @return bool Is block visible for user
  49. */
  50. public function is_block_visible_for_user($user_id)
  51. {
  52. $user_info = api_get_user_info($user_id);
  53. $user_status = $user_info['status'];
  54. $is_block_visible_for_user = false;
  55. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  56. $is_block_visible_for_user = true;
  57. }
  58. return $is_block_visible_for_user;
  59. }
  60. /**
  61. * This method return content html containing information about students
  62. * and its position for showing it inside dashboard interface
  63. * it's important to use the name 'get_block' for being used from dashboard controller.
  64. *
  65. * @return array column and content html
  66. */
  67. public function get_block()
  68. {
  69. global $charset;
  70. $column = 1;
  71. $data = [];
  72. $students_attendance_graph = $this->get_students_attendance_graph();
  73. $html = '<div class="panel panel-default" id="intro">
  74. <div class="panel-heading">
  75. '.get_lang('StudentsInformationsGraph').'
  76. <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.'">
  77. <em class="fa fa-times"></em>
  78. </a></div>
  79. </div>
  80. <div class="panel-body" align="center">
  81. <div style="padding:10px;"><strong>'.get_lang('AttendancesFaults').'</strong></div>
  82. '.$students_attendance_graph.'
  83. </div>
  84. </div>';
  85. $data['column'] = $column;
  86. $data['content_html'] = $html;
  87. return $data;
  88. }
  89. /**
  90. * This method return a graph containing information about students evaluation,
  91. * it's used inside get_block method for showing it inside dashboard interface.
  92. *
  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 = [];
  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 = [];
  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. $dataSet = new pData();
  125. $dataSet->addPoints($faults, 'Serie1');
  126. $dataSet->addPoints($usernames, 'Labels');
  127. $dataSet->setSerieDescription('Series1', get_lang('Average'));
  128. $dataSet->setSerieDescription('Labels', get_lang('User'));
  129. $dataSet->setAbscissa('Labels');
  130. $dataSet->setAbscissaName(get_lang('User'));
  131. $dataSet->setAxisName(0, get_lang('Attendance'));
  132. $palette = [
  133. '0' => ['R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100],
  134. '1' => ['R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100],
  135. '2' => ['R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100],
  136. '3' => ['R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100],
  137. '4' => ['R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100],
  138. '5' => ['R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100],
  139. '6' => ['R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100],
  140. '7' => ['R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100],
  141. '8' => ['R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100],
  142. ];
  143. // Cache definition
  144. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  145. $myCache = new pCache(
  146. [
  147. 'CacheFolder' => substr(
  148. $cachePath,
  149. 0,
  150. strlen($cachePath) - 1
  151. ),
  152. ]
  153. );
  154. $chartHash = $myCache->getHash($dataSet);
  155. if ($myCache->isInCache($chartHash)) {
  156. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  157. $myCache->saveFromCache($chartHash, $imgPath);
  158. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  159. } else {
  160. $maxCounts = max(count($usernames), count($faults));
  161. if ($maxCounts < 5) {
  162. $heightSize = 200;
  163. } else {
  164. $heightSize = $maxCounts * 40;
  165. }
  166. /* Create the pChart object */
  167. $widthSize = 480;
  168. $angle = 40;
  169. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  170. /* Turn of Antialiasing */
  171. $myPicture->Antialias = false;
  172. /* Add a border to the picture */
  173. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
  174. /* Set the default font */
  175. $myPicture->setFontProperties(
  176. [
  177. 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  178. 'FontSize' => 10,
  179. ]
  180. );
  181. /* Do NOT Write the chart title */
  182. /* Define the chart area */
  183. $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40);
  184. /* Draw the scale */
  185. $scaleSettings = [
  186. 'GridR' => 200,
  187. 'GridG' => 200,
  188. 'GridB' => 200,
  189. 'DrawSubTicks' => true,
  190. 'CycleBackground' => true,
  191. 'Mode' => SCALE_MODE_ADDALL_START0,
  192. 'Pos' => SCALE_POS_TOPBOTTOM,
  193. 'DrawXLines' => false,
  194. 'LabelRotation' => $angle,
  195. ];
  196. $myPicture->drawScale($scaleSettings);
  197. /* Turn on shadow computing */
  198. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  199. /* Draw the chart */
  200. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  201. $settings = [
  202. 'OverrideColors' => $palette,
  203. 'Gradient' => false,
  204. 'GradientMode' => GRADIENT_SIMPLE,
  205. 'DisplayPos' => LABEL_POS_TOP,
  206. 'DisplayValues' => true,
  207. 'DisplayR' => 0,
  208. 'DisplayG' => 0,
  209. 'DisplayB' => 0,
  210. 'DisplayShadow' => true,
  211. 'Surrounding' => 10,
  212. ];
  213. $myPicture->drawBarChart($settings);
  214. /* Write and save into cache */
  215. $myCache->writeToCache($chartHash, $myPicture);
  216. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  217. $myCache->saveFromCache($chartHash, $imgPath);
  218. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  219. }
  220. $graph = '<img src="'.$imgPath.'" >';
  221. } else {
  222. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'), 'UTF-8').'</p>';
  223. }
  224. return $graph;
  225. }
  226. /**
  227. * Get number of students.
  228. *
  229. * @return int
  230. */
  231. public function get_number_of_students()
  232. {
  233. return count($this->students);
  234. }
  235. }