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