block_evaluation_graph.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file is part of evaluation 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. */
  9. /**
  10. * required files for getting data
  11. */
  12. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  13. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  14. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
  15. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php';
  16. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/flatview_data_generator.class.php';
  17. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  18. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/category.class.php';
  19. /**
  20. * This class is used like controller for this evaluations graph block plugin,
  21. * the class name must be registered inside path.info file (e.g: controller = "BlockEvaluationGraph"), so dashboard controller will be instantiate it
  22. * @package chamilo.dashboard
  23. */
  24. class BlockEvaluationGraph extends Block {
  25. private $user_id;
  26. private $courses;
  27. private $sessions;
  28. private $path;
  29. private $permission = array(DRH, SESSIONADMIN);
  30. /**
  31. * Constructor
  32. */
  33. public function __construct ($user_id) {
  34. $this->path = 'block_evaluation_graph';
  35. $this->user_id = $user_id;
  36. $this->bg_width = 450;
  37. $this->bg_height = 350;
  38. if ($this->is_block_visible_for_user($user_id)) {
  39. //$this->courses = CourseManager::get_real_course_list();
  40. /*if (api_is_platform_admin()) {
  41. $this->courses = CourseManager::get_real_course_list();
  42. $this->sessions = SessionManager::get_sessions_list();
  43. } else {*/
  44. if (!api_is_session_admin()) {
  45. $this->courses = CourseManager::get_courses_followed_by_drh($user_id);
  46. }
  47. $this->sessions = SessionManager::get_sessions_followed_by_drh($user_id);
  48. //}
  49. }
  50. }
  51. /**
  52. * This method check if a user is allowed to see the block inside dashboard interface
  53. * @param int User id
  54. * @return bool Is block visible for user
  55. */
  56. public function is_block_visible_for_user($user_id) {
  57. $user_info = api_get_user_info($user_id);
  58. $user_status = $user_info['status'];
  59. $is_block_visible_for_user = false;
  60. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  61. $is_block_visible_for_user = true;
  62. }
  63. return $is_block_visible_for_user;
  64. }
  65. /**
  66. * This method return content html containing information about sessions and its position for showing it inside dashboard interface
  67. * it's important to use the name 'get_block' for beeing used from dashboard controller
  68. * @return array column and content html
  69. */
  70. public function get_block() {
  71. global $charset;
  72. $column = 1;
  73. $data = array();
  74. $evaluations_base_courses_graph = $this->get_evaluations_base_courses_graph();
  75. $evaluations_courses_in_sessions_graph = $this->get_evaluations_courses_in_sessions_graph();
  76. $html = '<li class="widget color-orange" id="intro">
  77. <div class="widget-head">
  78. <h3>'.get_lang('EvaluationsGraph').'</h3>
  79. <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>
  80. </div>
  81. <div class="widget-content" align="center">';
  82. if (empty($evaluations_base_courses_graph) && empty($evaluations_courses_in_sessions_graph)) {
  83. $html .= '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  84. } else {
  85. // display evaluations base courses graph
  86. if (!empty($evaluations_base_courses_graph)) {
  87. foreach ($evaluations_base_courses_graph as $course_code => $img_html) {
  88. $html .= '<div><strong>'.$course_code.'</strong></div>';
  89. $html .= $img_html;
  90. }
  91. }
  92. // display evaluations base courses graph
  93. if (!empty($evaluations_courses_in_sessions_graph)) {
  94. foreach ($evaluations_courses_in_sessions_graph as $session_id => $courses) {
  95. $session_name = api_get_session_name($session_id);
  96. $html .= '<div><strong>'.$session_name.':'.get_lang('Evaluations').'</strong></div>';
  97. foreach ($courses as $course_code => $img_html) {
  98. $html .= '<div><strong>'.$course_code.'</strong></div>';
  99. $html .= $img_html;
  100. }
  101. }
  102. }
  103. }
  104. $html .= '</div>
  105. </li>';
  106. $data['column'] = $column;
  107. $data['content_html'] = $html;
  108. return $data;
  109. }
  110. /**
  111. * This method return a graph containing informations about evaluations inside base courses, it's used inside get_block method for showing it inside dashboard interface
  112. * @return string img html
  113. */
  114. public function get_evaluations_base_courses_graph() {
  115. $graphs = array();
  116. if (!empty($this->courses)) {
  117. $courses_code = array_keys($this->courses);
  118. foreach ($courses_code as $course_code) {
  119. $cats = Category::load(null, null, $course_code, null, null, null, false);
  120. if (isset($cats) && isset($cats[0])) {
  121. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  122. $alllinks = $cats[0]->get_links(null, true);
  123. $users = get_all_users($alleval, $alllinks);
  124. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  125. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  126. if (!empty($evaluation_sumary)) {
  127. $items = array_keys($evaluation_sumary);
  128. $max = $min = $avg = array();
  129. foreach ($evaluation_sumary as $evaluation) {
  130. $max[] = $evaluation['max'];
  131. $min[] = $evaluation['min'];
  132. $avg[] = $evaluation['avg'];
  133. }
  134. // Dataset definition
  135. $data_set = new pData;
  136. $data_set->AddPoint($max, "Max");
  137. $data_set->AddPoint($avg, "Avg");
  138. $data_set->AddPoint($min, "Min");
  139. $data_set->AddPoint($items, "Items");
  140. $data_set->SetXAxisName(get_lang('EvaluationName'));
  141. $data_set->SetYAxisName(get_lang('Percentage'));
  142. $data_set->AddAllSeries();
  143. $data_set->RemoveSerie("Items");
  144. $data_set->SetAbsciseLabelSerie("Items");
  145. $graph_id = $this->user_id.'StudentEvaluationGraph';
  146. $cache = new pCache(api_get_path(SYS_ARCHIVE_PATH));
  147. // the graph id
  148. $data = $data_set->GetData();
  149. if ($cache->IsInCache($graph_id, $data)) {
  150. //if we already created the img
  151. $img_file = $cache->GetHash($graph_id, $data);
  152. } else {
  153. // Initialise the graph
  154. $test = new pChart($this->bg_width,$this->bg_height);
  155. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  156. $test->setGraphArea(50,30,$this->bg_width-75,$this->bg_height-75);
  157. $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$this->bg_height-20,5,240,240,240);
  158. $test->drawRoundedRectangle(5,5,$this->bg_width-18,$this->bg_height-18,5,230,230,230);
  159. $test->drawGraphArea(255,255,255,TRUE);
  160. $test->setFixedScale(0,100,5);
  161. $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);
  162. $test->setColorPalette(0,105,221,34);
  163. $test->setColorPalette(1,255,135,30);
  164. $test->setColorPalette(2,255,0,0);
  165. $test->drawGrid(4,TRUE,230,230,230,50);
  166. // Draw the 0 line
  167. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  168. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  169. // Draw the bar graph
  170. $test->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 90);
  171. // Finish the graph
  172. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  173. $test->drawLegend($this->bg_width-80,20,$data_set->GetDataDescription(),255,255,255);
  174. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  175. //$test->drawTitle(50,22,$course_code,50,50,50,185);
  176. $test->setColorPalette(0,50,50,50);
  177. $test->setColorPalette(1,50,50,50);
  178. $test->setColorPalette(2,50,50,50);
  179. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Min", "Max", "Avg"));
  180. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  181. ob_start();
  182. $test->Stroke();
  183. ob_end_clean();
  184. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  185. }
  186. if (!empty($img_file)) {
  187. $graphs[$course_code] = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  188. }
  189. }
  190. }
  191. } // end for
  192. }
  193. return $graphs;
  194. }
  195. /**
  196. * This method return a graph containing informations about evaluations inside courses in sessions, it's used inside get_block method for showing it inside dashboard interface
  197. * @return string img html
  198. */
  199. public function get_evaluations_courses_in_sessions_graph() {
  200. $graphs = array();
  201. if (!empty($this->sessions)) {
  202. $session_ids = array_keys($this->sessions);
  203. foreach ($session_ids as $session_id) {
  204. $courses_code = array_keys(Tracking::get_courses_list_from_session($session_id));
  205. $courses_graph = array();
  206. foreach ($courses_code as $courseId) {
  207. $courseInfo = api_get_course_info_by_id($courseId);
  208. $course_code = $courseInfo['code'];
  209. $cats = Category::load(null, null, $course_code, null, null, $session_id);
  210. if (isset($cats) && isset($cats[0])) {
  211. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  212. $alllinks = $cats[0]->get_links(null, true);
  213. $users = get_all_users($alleval, $alllinks);
  214. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  215. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  216. if (!empty($evaluation_sumary)) {
  217. $items = array_keys($evaluation_sumary);
  218. $max = $min = $avg = array();
  219. foreach ($evaluation_sumary as $evaluation) {
  220. $max[] = $evaluation['max'];
  221. $min[] = $evaluation['min'];
  222. $avg[] = $evaluation['avg'];
  223. }
  224. // Dataset definition
  225. $data_set = new pData;
  226. $data_set->AddPoint($max, "Max");
  227. $data_set->AddPoint($avg, "Avg");
  228. $data_set->AddPoint($min, "Min");
  229. $data_set->AddPoint($items, "Items");
  230. $data_set->SetXAxisName(get_lang('EvaluationName'));
  231. $data_set->SetYAxisName(get_lang('Percentage'));
  232. $data_set->AddAllSeries();
  233. $data_set->RemoveSerie("Items");
  234. $data_set->SetAbsciseLabelSerie("Items");
  235. $graph_id = $this->user_id.'StudentEvaluationGraph';
  236. $cache = new pCache(api_get_path(SYS_ARCHIVE_PATH));
  237. // the graph id
  238. $data = $data_set->GetData();
  239. if ($cache->IsInCache($graph_id, $data)) {
  240. //if we already created the img
  241. $img_file = $cache->GetHash($graph_id, $data);
  242. } else {
  243. // Initialise the graph
  244. $test = new pChart($this->bg_width,$this->bg_height);
  245. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  246. $test->setGraphArea(50,30,$this->bg_width-75,$this->bg_height-75);
  247. $test->drawFilledRoundedRectangle(7,7,$this->bg_width-20,$this->bg_height-20,5,240,240,240);
  248. $test->drawRoundedRectangle(5,5,$this->bg_width-18,$this->bg_height-18,5,230,230,230);
  249. $test->drawGraphArea(255,255,255,TRUE);
  250. $test->setFixedScale(0,100,5);
  251. $test->drawScale($data_set->GetData(),$data_set->GetDataDescription(),SCALE_ADDALL,150,150,150,TRUE,0,2,TRUE);
  252. $test->setColorPalette(0,105,221,34);
  253. $test->setColorPalette(1,255,135,30);
  254. $test->setColorPalette(2,255,0,0);
  255. $test->drawGrid(4,TRUE,230,230,230,50);
  256. // Draw the 0 line
  257. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  258. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  259. // Draw the bar graph
  260. $test->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  261. // Finish the graph
  262. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  263. $test->drawLegend($this->bg_width-80,20,$data_set->GetDataDescription(),255,255,255);
  264. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  265. $test->setColorPalette(0,50,50,50);
  266. $test->setColorPalette(1,50,50,50);
  267. $test->setColorPalette(2,50,50,50);
  268. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Min", "Max", "Avg"));
  269. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  270. ob_start();
  271. $test->Stroke();
  272. ob_end_clean();
  273. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  274. }
  275. if (!empty($img_file)) {
  276. $courses_graph[$course_code] = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  277. }
  278. }
  279. }
  280. }
  281. if (!empty($courses_graph)) {
  282. $graphs[$session_id] = $courses_graph;
  283. }
  284. }
  285. }
  286. return $graphs;
  287. }
  288. }