block_student_graph.class.php 9.8 KB

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