block_evaluation_graph.class.php 14 KB

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