block_evaluation_graph.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  4. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  5. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  6. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  7. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  8. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
  9. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php';
  10. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/flatview_data_generator.class.php';
  11. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  12. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/category.class.php';
  13. /**
  14. * Class BlockEvaluationGraph
  15. * This class is used like controller for this evaluations graph block plugin,
  16. * the class name must be registered inside path.info file
  17. * (e.g: controller = "BlockEvaluationGraph"),
  18. * so dashboard controller will be instantiate it
  19. *
  20. * This file is part of evaluation graph block plugin for dashboard,
  21. * it should be required inside dashboard controller for showing it
  22. * into dashboard interface from platform
  23. *
  24. * @package chamilo.dashboard
  25. * @author Christian Fasanando
  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[] = !empty($evaluation['min']) ? $evaluation['min'] : 0;
  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. $angle = -30;
  159. $test = new pChart($this->bg_width,$this->bg_height);
  160. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  161. $test = $test->fixHeightByRotation(
  162. $data_set->GetData(),
  163. $data_set->GetDataDescription(),
  164. $angle
  165. );
  166. $test->setGraphArea(50, 30, $this->bg_width-75, $this->bg_height - 75);
  167. $test->drawFilledRoundedRectangle(
  168. 7,
  169. 7,
  170. $this->bg_width-20,
  171. $test->YSize - 20,
  172. 5,
  173. 240,
  174. 240,
  175. 240
  176. );
  177. $test->drawRoundedRectangle(
  178. 5,
  179. 5,
  180. $this->bg_width-18,
  181. $test->YSize - 18,
  182. 5,
  183. 230,
  184. 230,
  185. 230
  186. );
  187. $test->drawGraphArea(255,255,255,TRUE);
  188. $test->setFixedScale(0,100,5);
  189. $test->drawScale(
  190. $data_set->GetData(),
  191. $data_set->GetDataDescription(),
  192. SCALE_ADDALL,
  193. 150,
  194. 150,
  195. 150,
  196. TRUE,
  197. $angle,
  198. 2,
  199. TRUE
  200. );
  201. $test->setColorPalette(0,105,221,34);
  202. $test->setColorPalette(1,255,135,30);
  203. $test->setColorPalette(2,255,0,0);
  204. $test->drawGrid(4,TRUE,230,230,230,50);
  205. // Draw the 0 line
  206. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  207. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  208. // Draw the bar graph
  209. $test->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 90);
  210. // Finish the graph
  211. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  212. $test->drawLegend($this->bg_width-80,20,$data_set->GetDataDescription(),255,255,255);
  213. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  214. //$test->drawTitle(50,22,$course_code,50,50,50,185);
  215. $test->setColorPalette(0,50,50,50);
  216. $test->setColorPalette(1,50,50,50);
  217. $test->setColorPalette(2,50,50,50);
  218. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Min", "Max", "Avg"));
  219. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  220. ob_start();
  221. $test->Stroke();
  222. ob_end_clean();
  223. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  224. }
  225. if (!empty($img_file)) {
  226. $graphs[$course_code] = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  227. }
  228. }
  229. }
  230. } // end for
  231. }
  232. return $graphs;
  233. }
  234. /**
  235. * This method return a graph containing information about evaluations
  236. * inside courses in sessions, it's used inside get_block method for
  237. * showing it inside dashboard interface
  238. * @return string img html
  239. */
  240. public function get_evaluations_courses_in_sessions_graph()
  241. {
  242. $graphs = array();
  243. if (!empty($this->sessions)) {
  244. $session_ids = array_keys($this->sessions);
  245. foreach ($session_ids as $session_id) {
  246. $courses_code = array_keys(Tracking::get_courses_list_from_session($session_id));
  247. $courses_graph = array();
  248. foreach ($courses_code as $course_code) {
  249. $cats = Category::load(null, null, $course_code, null, null, $session_id);
  250. if (isset($cats) && isset($cats[0])) {
  251. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  252. $alllinks = $cats[0]->get_links(null, true);
  253. $users = get_all_users($alleval, $alllinks);
  254. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  255. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  256. if (!empty($evaluation_sumary)) {
  257. $items = array_keys($evaluation_sumary);
  258. $max = $min = $avg = array();
  259. foreach ($evaluation_sumary as $evaluation) {
  260. $max[] = $evaluation['max'];
  261. $min[] = $evaluation['min'];
  262. $avg[] = $evaluation['avg'];
  263. }
  264. // Dataset definition
  265. $data_set = new pData;
  266. $data_set->AddPoint($max, "Max");
  267. $data_set->AddPoint($avg, "Avg");
  268. $data_set->AddPoint($min, "Min");
  269. $data_set->AddPoint($items, "Items");
  270. $data_set->SetXAxisName(get_lang('EvaluationName'));
  271. $data_set->SetYAxisName(get_lang('Percentage'));
  272. $data_set->AddAllSeries();
  273. $data_set->RemoveSerie("Items");
  274. $data_set->SetAbsciseLabelSerie("Items");
  275. $graph_id = $this->user_id.'StudentEvaluationGraph';
  276. $cache = new pCache();
  277. // the graph id
  278. $data = $data_set->GetData();
  279. if ($cache->IsInCache($graph_id, $data)) {
  280. //if we already created the img
  281. $img_file = $cache->GetHash($graph_id, $data);
  282. } else {
  283. // Initialise the graph
  284. $angle = -30;
  285. $test = new pChart($this->bg_width, $this->bg_height);
  286. $test->setFontProperties(api_get_path(LIBRARY_PATH) . 'pchart/fonts/tahoma.ttf', 8);
  287. $test->fixHeightByRotation(
  288. $data_set->GetData(),
  289. $data_set->GetDataDescription(),
  290. $angle
  291. );
  292. $test->setGraphArea(50, 30, $this->bg_width - 75, $this->bg_height - 75);
  293. $test->drawFilledRoundedRectangle(
  294. 7,
  295. 7,
  296. $this->bg_width - 20,
  297. $test->YSize - 20,
  298. 5,
  299. 240,
  300. 240,
  301. 240
  302. );
  303. $test->drawRoundedRectangle(
  304. 5,
  305. 5,
  306. $this->bg_width - 18,
  307. $test->YSize - 18,
  308. 5,
  309. 230,
  310. 230,
  311. 230
  312. );
  313. $test->drawGraphArea(255,255,255,TRUE);
  314. $test->setFixedScale(0,100,5);
  315. $test->drawScale(
  316. $data_set->GetData(),
  317. $data_set->GetDataDescription(),
  318. SCALE_ADDALL,
  319. 150,
  320. 150,
  321. 150,
  322. TRUE,
  323. $angle,
  324. 2,
  325. TRUE
  326. );
  327. $test->setColorPalette(0,105,221,34);
  328. $test->setColorPalette(1,255,135,30);
  329. $test->setColorPalette(2,255,0,0);
  330. $test->drawGrid(4,TRUE,230,230,230,50);
  331. // Draw the 0 line
  332. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',6);
  333. $test->drawTreshold(0,143,55,72,TRUE,TRUE);
  334. // Draw the bar graph
  335. $test->drawOverlayBarGraph($data_set->GetData(),$data_set->GetDataDescription(), 100);
  336. // Finish the graph
  337. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',8);
  338. $test->drawLegend($this->bg_width-80,20,$data_set->GetDataDescription(),255,255,255);
  339. $test->setFontProperties(api_get_path(LIBRARY_PATH).'pchart/fonts/tahoma.ttf',10);
  340. $test->setColorPalette(0,50,50,50);
  341. $test->setColorPalette(1,50,50,50);
  342. $test->setColorPalette(2,50,50,50);
  343. $test->writeValues($data_set->GetData(),$data_set->GetDataDescription(),array("Min", "Max", "Avg"));
  344. $cache->WriteToCache($graph_id, $data_set->GetData(), $test);
  345. ob_start();
  346. $test->Stroke();
  347. ob_end_clean();
  348. $img_file = $cache->GetHash($graph_id, $data_set->GetData());
  349. }
  350. if (!empty($img_file)) {
  351. $courses_graph[$course_code] = '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'">';
  352. }
  353. }
  354. }
  355. }
  356. if (!empty($courses_graph)) {
  357. $graphs[$session_id] = $courses_graph;
  358. }
  359. }
  360. }
  361. return $graphs;
  362. }
  363. }