block_evaluation_graph.class.php 14 KB

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