block_student_graph.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * This file is part of student graph block plugin for dashboard,
  4. * it should be required inside dashboard controller for showing it into dashboard interface from plattform
  5. * @package chamilo.dashboard
  6. * @author Christian Fasanando
  7. */
  8. /**
  9. * required files for getting data
  10. */
  11. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  12. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'tracking.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'attendance.lib.php';
  15. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  16. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  17. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  18. require_once api_get_path(LIBRARY_PATH).'pchart/MyHorBar.class.php';
  19. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  20. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  21. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
  22. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php';
  23. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/category.class.php';
  24. /**
  25. * This class is used like controller for student graph block plugin,
  26. * the class name must be registered inside path.info file (e.g: controller = "BlockStudentGraph"), so dashboard controller will be instantiate it
  27. * @package chamilo.dashboard
  28. */
  29. class BlockStudentGraph extends Block {
  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. $this->user_id = $user_id;
  39. $this->path = 'block_student_graph';
  40. if ($this->is_block_visible_for_user($user_id)) {
  41. /*if (api_is_platform_admin()) {
  42. $this->students = UserManager::get_user_list(array('status' => STUDENT));
  43. } else if (api_is_drh()) {*/
  44. $this->students = UserManager::get_users_followed_by_drh($user_id, STUDENT);
  45. //}
  46. }
  47. }
  48. /**
  49. * This method check if a user is allowed to see the block inside dashboard interface
  50. * @param int User id
  51. * @return bool Is block visible for user
  52. */
  53. public function is_block_visible_for_user($user_id) {
  54. $user_info = api_get_user_info($user_id);
  55. $user_status = $user_info['status'];
  56. $is_block_visible_for_user = false;
  57. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  58. $is_block_visible_for_user = true;
  59. }
  60. return $is_block_visible_for_user;
  61. }
  62. /**
  63. * This method return content html containing information about students and its position for showing it inside dashboard interface
  64. * it's important to use the name 'get_block' for beeing used from dashboard controller
  65. * @return array column and content html
  66. */
  67. public function get_block() {
  68. global $charset;
  69. $column = 1;
  70. $data = array();
  71. $students_attendance_graph = $this->get_students_attendance_graph();
  72. $html = '
  73. <li class="widget color-orange" id="intro">
  74. <div class="widget-head">
  75. <h3>'.get_lang('StudentsInformationsGraph').'</h3>
  76. <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>
  77. </div>
  78. <div class="widget-content" align="center">
  79. <div style="padding:10px;"><strong>'.get_lang('AttendancesFaults').'</strong></div>
  80. '.$students_attendance_graph.'
  81. </div>
  82. </li>
  83. ';
  84. $data['column'] = $column;
  85. $data['content_html'] = $html;
  86. return $data;
  87. }
  88. /**
  89. * This method return a graph containing informations about students evaluation, it's used inside get_block method for showing it inside dashboard interface
  90. * @return string img html
  91. */
  92. public function get_students_attendance_graph() {
  93. $students = $this->students;
  94. $attendance = new Attendance();
  95. // get data
  96. $attendances_faults_avg = array();
  97. foreach ($students as $student) {
  98. $student_id = $student['user_id'];
  99. $student_info = api_get_user_info($student_id);
  100. // get average of faults in attendances by student
  101. $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
  102. if (!empty($results_faults_avg)) {
  103. $attendances_faults_avg[$student_info['username']] = $results_faults_avg['porcent'];
  104. } else {
  105. $attendances_faults_avg[$student_info['username']] = 0;
  106. }
  107. }
  108. arsort($attendances_faults_avg);
  109. $usernames = array_keys($attendances_faults_avg);
  110. // get only until five users
  111. if (count($usernames) > 5) { array_splice($usernames,5); }
  112. $faults = array();
  113. foreach ($usernames as $username) {
  114. $faults[] = $attendances_faults_avg[$username];
  115. }
  116. $graph = '';
  117. $img_file = '';
  118. if (is_array($usernames) && count($usernames) > 0) {
  119. // Defining data
  120. $data_set = new pData;
  121. $data_set->AddPoint($faults,"Promedio");
  122. $data_set->AddPoint($usernames,"Usuario");
  123. $data_set->AddAllSeries();
  124. //$data_set->SetYAxisName(get_lang('UserName'));
  125. //$data_set->SetXAxisName(get_lang('AttendancesFaults'));
  126. $data_set->SetAbsciseLabelSerie("Usuario");
  127. // prepare cache for saving image
  128. $graph_id = $this->user_id.'StudentEvaluationGraph'; // the graph id
  129. $cache = new pCache();
  130. $data = $data_set->GetData(); // return $this->DataDescription
  131. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  132. //if we already created the img
  133. $img_file = $cache->GetHash($graph_id, $data_set->GetData()); // image file with hash
  134. } else {
  135. // Initialise the graph
  136. $test = new MyHorBar(400,280);
  137. //$Test->setFontProperties("Fonts/tahoma.ttf",8);
  138. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 8);
  139. //$test->setGraphArea(120,60,450,650);
  140. $test->setGraphArea(65,30,350,200);
  141. //$Test->setFixedScale(0,5,5,0,0,0);
  142. //$test->drawFilledRoundedRectangle(7,7,493,693,5,240,240,240);
  143. $test->drawFilledRoundedRectangle(7,7,393,253,5,240,240,240);
  144. //$test->drawRoundedRectangle(5,5,495,695,5,230,230,230);
  145. $test->drawRoundedRectangle(5,5,395,255,5,230,230,230);
  146. $test->drawGraphArea(255,255,255,TRUE);
  147. $test->setFixedScale(0,100,5);
  148. $test->drawHorScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
  149. $test->setColorPalette(0,255,0,0);
  150. $test->drawHorGrid(10,TRUE,230,230,230,50);
  151. // Draw the 0 line
  152. //$Test->setFontProperties("Fonts/tahoma.ttf",6);
  153. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf', 6);
  154. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  155. // Draw the bar graph
  156. $test->drawHorBarGraph($data_set->GetData(),$data_set->GetDataDescription(),FALSE);
  157. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  158. ob_start();
  159. $test->Stroke();
  160. ob_end_clean();
  161. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  162. }
  163. if (!empty($img_file)) {
  164. $graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  165. }
  166. } else {
  167. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  168. }
  169. /*
  170. $students = $this->students;
  171. $attendance = new Attendance();
  172. // get data
  173. $attendances_faults_avg = array();
  174. foreach ($students as $student) {
  175. $student_id = $student['user_id'];
  176. $student_info = api_get_user_info($student_id);
  177. // get average of faults in attendances by student
  178. $results_faults_avg = $attendance->get_faults_average_inside_courses($student_id);
  179. if (!empty($results_faults_avg)) {
  180. $attendances_faults_avg[$student_info['username']] = $results_faults_avg['porcent'];
  181. } else {
  182. $attendances_faults_avg[$student_info['username']] = 0;
  183. }
  184. }
  185. arsort($attendances_faults_avg);
  186. $usernames = array_keys($attendances_faults_avg);
  187. // get only until five users
  188. if (count($usernames) > 5) { array_splice($usernames,5); }
  189. $faults = array();
  190. foreach ($usernames as $username) {
  191. $faults[] = $attendances_faults_avg[$username];
  192. }
  193. $graph = '';
  194. $img_file = '';
  195. if (is_array($usernames) && count($usernames) > 0) {
  196. // Defining data
  197. $data_set = new pData;
  198. $data_set->AddPoint($faults,"Promedio"); // $this->Data = array(0=>array('Promedio'=>57,'Name'=>0), 1=>array('Promedio'=>43,'Name'=>1), 2=>array('Promedio'=>29,'Name'=>2))
  199. $data_set->AddPoint($usernames,"Usuario"); // $this->Data = array(0=>array('Usuario'=>'alumno3','Name'=>0), 1=>array('Usuario'=>'alumno1','Name'=>1), 2=>array('Usuario'=>'alumno2','Name'=>2))
  200. $data_set->AddAllSeries(); // $this->DataDescription = array('Position'=>'Name', 'Format'=>array('X'=>'number','Y'=>'number'), 'Unit'=>array('X'=>null,'Y'=null),'Values'=>array(0=>'Promedio',1=>'Usuario'))
  201. $data_set->SetXAxisName(get_lang('UserName')); // $this->DataDescription["Axis"]["X"] = 'UserName';
  202. $data_set->SetYAxisName(get_lang('AttendancesFaults')); // $this->DataDescription["Axis"]["Y"] = 'AttendancesFaults';
  203. $data_set->SetAbsciseLabelSerie("Usuario"); // $this->DataDescription["Position"] = "Usuario";
  204. // prepare cache for saving image
  205. $graph_id = $this->user_id.'StudentEvaluationGraph'; // the graph id
  206. $cache = new pCache();
  207. $data = $data_set->GetData(); // return $this->DataDescription
  208. if ($cache->IsInCache($graph_id, $data_set->GetData())) {
  209. //if we already created the img
  210. $img_file = $cache->GetHash($graph_id, $data_set->GetData()); // image file with hash
  211. } else {
  212. // Initializing the graph
  213. $test = new pChart(365,300); // Create transparent image 365x300
  214. // $this->FontName = api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf'
  215. // $this->FontSize = 8
  216. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  217. $X1 = 50;
  218. $Y1 = 30;
  219. $X2 = 345;
  220. $Y2 = 200;
  221. //$this->GArea_X1 = $X1;$this->GArea_Y1 = $Y1;$this->GArea_X2 = $X2;$this->GArea_Y2 = $Y2;
  222. $test->setGraphArea(50,30,345,200);
  223. $test->drawFilledRoundedRectangle(7,7,371,240,5,240,240,240);
  224. $test->drawRoundedRectangle(5,5,373,225,5,230,230,230);
  225. $test->drawGraphArea(255,255,255,TRUE);
  226. $test->setFixedScale(0,100,5);
  227. $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,10,TRUE);
  228. $test->drawGrid(4,TRUE,230,230,230,50);
  229. // Drawing bars
  230. //$test->drawBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  231. //$test->drawLimitsGraph($data_set->GetData(),$data_set->GetDataDescription(),240,240,240);
  232. //$test->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription());
  233. $test->drawHorizontalBarGraph($data_set->GetData(),$data_set->GetDataDescription(),TRUE);
  234. // Drawing title
  235. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  236. $test->drawTitle(50,22,get_lang('AttendancesFaults'),50,50,50,385);
  237. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),"Promedio");
  238. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  239. ob_start();
  240. $test->Stroke();
  241. ob_end_clean();
  242. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  243. }
  244. if (!empty($img_file)) {
  245. $graph = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  246. }
  247. } else {
  248. $graph = '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  249. }
  250. */
  251. return $graph;
  252. }
  253. /**
  254. * Get number of students
  255. * @return int
  256. */
  257. function get_number_of_students() {
  258. return count($this->students);
  259. }
  260. }
  261. ?>