block_student_graph.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 $permission = [DRH];
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct($user_id)
  31. {
  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. *
  45. * @param int User id
  46. *
  47. * @return bool Is block visible for user
  48. */
  49. public function is_block_visible_for_user($user_id)
  50. {
  51. $user_info = api_get_user_info($user_id);
  52. $user_status = $user_info['status'];
  53. $is_block_visible_for_user = false;
  54. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  55. $is_block_visible_for_user = true;
  56. }
  57. return $is_block_visible_for_user;
  58. }
  59. /**
  60. * This method return content html containing information about students
  61. * and its position for showing it inside dashboard interface
  62. * it's important to use the name 'get_block' for being used from dashboard controller.
  63. *
  64. * @return array column and content html
  65. */
  66. public function get_block()
  67. {
  68. $column = 1;
  69. $data = [];
  70. $html = $this->getBlockCard(
  71. get_lang('Learners report chart'),
  72. $this->getContent()
  73. );
  74. $data['column'] = $column;
  75. $data['content_html'] = $html;
  76. return $data;
  77. }
  78. /**
  79. * This method return a graph containing information about students evaluation,
  80. * it's used inside get_block method for showing it inside dashboard interface.
  81. *
  82. * @return string img html
  83. */
  84. public function getContent()
  85. {
  86. $students = $this->students;
  87. $attendance = new Attendance();
  88. // get data
  89. $attendances_faults_avg = [];
  90. if (is_array($students) && count($students) > 0) {
  91. foreach ($students as $student) {
  92. $student_id = $student['user_id'];
  93. //$student_info = api_get_user_info($student_id);
  94. // get average of faults in attendances by student
  95. $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
  96. if (!empty($results_faults_avg)) {
  97. $attendances_faults_avg[$student['lastname']] = $results_faults_avg['porcent'];
  98. } else {
  99. $attendances_faults_avg[$student['lastname']] = 0;
  100. }
  101. }
  102. }
  103. arsort($attendances_faults_avg);
  104. $usernames = array_keys($attendances_faults_avg);
  105. $faults = [];
  106. foreach ($usernames as $username) {
  107. $faults[] = $attendances_faults_avg[$username];
  108. }
  109. $graph = '';
  110. $img_file = '';
  111. if (is_array($usernames) && count($usernames) > 0) {
  112. // Defining data
  113. $dataSet = new pData();
  114. $dataSet->addPoints($faults, 'Serie1');
  115. $dataSet->addPoints($usernames, 'Labels');
  116. $dataSet->setSerieDescription('Series1', get_lang('Average'));
  117. $dataSet->setSerieDescription('Labels', get_lang('User'));
  118. $dataSet->setAbscissa('Labels');
  119. $dataSet->setAbscissaName(get_lang('User'));
  120. $dataSet->setAxisName(0, get_lang('Attendance'));
  121. $palette = [
  122. '0' => ['R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100],
  123. '1' => ['R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100],
  124. '2' => ['R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100],
  125. '3' => ['R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100],
  126. '4' => ['R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100],
  127. '5' => ['R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100],
  128. '6' => ['R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100],
  129. '7' => ['R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100],
  130. '8' => ['R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100],
  131. ];
  132. // Cache definition
  133. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  134. $myCache = new pCache(
  135. [
  136. 'CacheFolder' => substr(
  137. $cachePath,
  138. 0,
  139. strlen($cachePath) - 1
  140. ),
  141. ]
  142. );
  143. $chartHash = $myCache->getHash($dataSet);
  144. if ($myCache->isInCache($chartHash)) {
  145. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  146. $myCache->saveFromCache($chartHash, $imgPath);
  147. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  148. } else {
  149. $maxCounts = max(count($usernames), count($faults));
  150. if ($maxCounts < 5) {
  151. $heightSize = 200;
  152. } else {
  153. $heightSize = $maxCounts * 40;
  154. }
  155. /* Create the pChart object */
  156. $widthSize = 480;
  157. $angle = 40;
  158. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  159. /* Turn of Antialiasing */
  160. $myPicture->Antialias = false;
  161. /* Add a border to the picture */
  162. $myPicture->drawRectangle(0, 0, $widthSize - 1, $heightSize - 1, ['R' => 0, 'G' => 0, 'B' => 0]);
  163. /* Set the default font */
  164. $myPicture->setFontProperties(
  165. [
  166. 'FontName' => api_get_path(SYS_FONTS_PATH).'opensans/OpenSans-Regular.ttf',
  167. 'FontSize' => 10,
  168. ]
  169. );
  170. /* Do NOT Write the chart title */
  171. /* Define the chart area */
  172. $myPicture->setGraphArea(80, 40, $widthSize - 20, $heightSize - 40);
  173. /* Draw the scale */
  174. $scaleSettings = [
  175. 'GridR' => 200,
  176. 'GridG' => 200,
  177. 'GridB' => 200,
  178. 'DrawSubTicks' => true,
  179. 'CycleBackground' => true,
  180. 'Mode' => SCALE_MODE_ADDALL_START0,
  181. 'Pos' => SCALE_POS_TOPBOTTOM,
  182. 'DrawXLines' => false,
  183. 'LabelRotation' => $angle,
  184. ];
  185. $myPicture->drawScale($scaleSettings);
  186. /* Turn on shadow computing */
  187. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  188. /* Draw the chart */
  189. $myPicture->setShadow(true, ['X' => 1, 'Y' => 1, 'R' => 0, 'G' => 0, 'B' => 0, 'Alpha' => 10]);
  190. $settings = [
  191. 'OverrideColors' => $palette,
  192. 'Gradient' => false,
  193. 'GradientMode' => GRADIENT_SIMPLE,
  194. 'DisplayPos' => LABEL_POS_TOP,
  195. 'DisplayValues' => true,
  196. 'DisplayR' => 0,
  197. 'DisplayG' => 0,
  198. 'DisplayB' => 0,
  199. 'DisplayShadow' => true,
  200. 'Surrounding' => 10,
  201. ];
  202. $myPicture->drawBarChart($settings);
  203. /* Write and save into cache */
  204. $myCache->writeToCache($chartHash, $myPicture);
  205. $imgPath = api_get_path(SYS_ARCHIVE_PATH).$chartHash;
  206. $myCache->saveFromCache($chartHash, $imgPath);
  207. $imgPath = api_get_path(WEB_ARCHIVE_PATH).$chartHash;
  208. }
  209. $graph = '<img src="'.$imgPath.'" >';
  210. } else {
  211. $graph = '<p>'.api_convert_encoding(get_lang('Graphic not available'), 'UTF-8').'</p>';
  212. }
  213. return $graph;
  214. }
  215. /**
  216. * Get number of students.
  217. *
  218. * @return int
  219. */
  220. public function get_number_of_students()
  221. {
  222. return count($this->students);
  223. }
  224. }