block_evaluation_graph.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use CpChart\Classes\pData as pData;
  4. use CpChart\Classes\pImage as pImage;
  5. use CpChart\Classes\pCache as pCache;
  6. /**
  7. * Class BlockEvaluationGraph
  8. * This class is used like controller for this evaluations graph block plugin,
  9. * the class name must be registered inside path.info file
  10. * (e.g: controller = "BlockEvaluationGraph"),
  11. * so dashboard controller will be instantiate it
  12. *
  13. * This file is part of evaluation graph block plugin for dashboard,
  14. * it should be required inside dashboard controller for showing it
  15. * into dashboard interface from platform
  16. *
  17. * @package chamilo.dashboard
  18. * @author Christian Fasanando
  19. */
  20. class BlockEvaluationGraph extends Block
  21. {
  22. private $user_id;
  23. private $courses;
  24. private $sessions;
  25. private $path;
  26. private $permission = array(DRH, SESSIONADMIN);
  27. /**
  28. * Constructor
  29. */
  30. public function __construct($user_id)
  31. {
  32. $this->path = 'block_evaluation_graph';
  33. $this->user_id = $user_id;
  34. $this->bg_width = 450;
  35. $this->bg_height = 350;
  36. if ($this->is_block_visible_for_user($user_id)) {
  37. if (!api_is_session_admin()) {
  38. $this->courses = CourseManager::get_courses_followed_by_drh($user_id);
  39. }
  40. $this->sessions = SessionManager::get_sessions_followed_by_drh($user_id);
  41. }
  42. }
  43. /**
  44. * This method check if a user is allowed to see the block inside dashboard interface
  45. * @param int User id
  46. * @return bool Is block visible for user
  47. */
  48. public function is_block_visible_for_user($user_id)
  49. {
  50. $user_info = api_get_user_info($user_id);
  51. $user_status = $user_info['status'];
  52. $is_block_visible_for_user = false;
  53. if (UserManager::is_admin($user_id) || in_array($user_status, $this->permission)) {
  54. $is_block_visible_for_user = true;
  55. }
  56. return $is_block_visible_for_user;
  57. }
  58. /**
  59. * This method return content html containing information about sessions and its position for showing it inside dashboard interface
  60. * it's important to use the name 'get_block' for beeing used from dashboard controller
  61. * @return array column and content html
  62. */
  63. public function get_block()
  64. {
  65. global $charset;
  66. $column = 1;
  67. $data = array();
  68. $evaluations_base_courses_graph = $this->get_evaluations_base_courses_graph();
  69. $evaluations_courses_in_sessions_graph = $this->get_evaluations_courses_in_sessions_graph();
  70. $html = '<div class="panel panel-default" id="intro">
  71. <div class="panel-heading">
  72. '.get_lang('EvaluationsGraph').'
  73. <div class="pull-right"><a class="btn btn-danger btn-xs" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)).'\')) return false;" href="index.php?action=disable_block&path='.$this->path.'">
  74. <i class="fa fa-times"></i>
  75. </a></div>
  76. </div>
  77. <div class="panel-body">';
  78. if (empty($evaluations_base_courses_graph) && empty($evaluations_courses_in_sessions_graph)) {
  79. $html .= '<p>'.api_convert_encoding(get_lang('GraphicNotAvailable'),'UTF-8').'</p>';
  80. } else {
  81. // display evaluations base courses graph
  82. if (!empty($evaluations_base_courses_graph)) {
  83. foreach ($evaluations_base_courses_graph as $course_code => $img_html) {
  84. $html .= '<div><strong>'.$course_code.'</strong></div>';
  85. $html .= $img_html;
  86. }
  87. }
  88. // display evaluations base courses graph
  89. if (!empty($evaluations_courses_in_sessions_graph)) {
  90. foreach ($evaluations_courses_in_sessions_graph as $session_id => $courses) {
  91. $session_name = api_get_session_name($session_id);
  92. $html .= '<div><strong>'.$session_name.':'.get_lang('Evaluations').'</strong></div>';
  93. foreach ($courses as $course_code => $img_html) {
  94. $html .= '<div><strong>'.$course_code.'</strong></div>';
  95. $html .= $img_html;
  96. }
  97. }
  98. }
  99. }
  100. $html .= '</div>
  101. </div>';
  102. $data['column'] = $column;
  103. $data['content_html'] = $html;
  104. return $data;
  105. }
  106. /**
  107. * This method return a graph containing informations about evaluations
  108. * inside base courses, it's used inside get_block method for showing
  109. * it inside dashboard interface
  110. * @return string img html
  111. */
  112. public function get_evaluations_base_courses_graph()
  113. {
  114. $graphs = array();
  115. if (!empty($this->courses)) {
  116. $courses_code = array_keys($this->courses);
  117. foreach ($courses_code as $course_code) {
  118. $cats = Category::load(null, null, $course_code, null, null, null, false);
  119. if (isset($cats) && isset($cats[0])) {
  120. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  121. $alllinks = $cats[0]->get_links(null, true);
  122. $users = GradebookUtils::get_all_users($alleval, $alllinks);
  123. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  124. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  125. if (!empty($evaluation_sumary)) {
  126. $items = array_keys($evaluation_sumary);
  127. $max = $min = $avg = array();
  128. foreach ($evaluation_sumary as $evaluation) {
  129. $max[] = $evaluation['max'];
  130. $min[] = !empty($evaluation['min']) ? $evaluation['min'] : 0;
  131. $avg[] = $evaluation['avg'];
  132. }
  133. // Dataset definition
  134. $dataSet = new pData();
  135. $dataSet->addPoints($min, 'Serie3');
  136. $dataSet->addPoints($avg, 'Serie2');
  137. $dataSet->addPoints($max, 'Serie1');
  138. $dataSet->addPoints($items, 'Labels');
  139. $dataSet->setSerieDescription('Serie1', get_lang('Max'));
  140. $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
  141. $dataSet->setSerieDescription('Serie3', get_lang('Min'));
  142. $dataSet->setAbscissa('Labels');
  143. $dataSet->setAbscissaName(get_lang('EvaluationName'));
  144. $dataSet->normalize(100, '%');
  145. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  146. // Cache definition
  147. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  148. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  149. $chartHash = $myCache->getHash($dataSet);
  150. if ($myCache->isInCache($chartHash)) {
  151. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  152. $myCache->saveFromCache($chartHash, $imgPath);
  153. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  154. } else {
  155. /* Create the pChart object */
  156. $widthSize = $this->bg_width;
  157. $heightSize = $this->bg_height;
  158. $fontSize = 8;
  159. $angle = 50;
  160. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  161. /* Turn of Antialiasing */
  162. $myPicture->Antialias = false;
  163. /* Add a border to the picture */
  164. $myPicture->drawRectangle(
  165. 0,
  166. 0,
  167. $widthSize - 1,
  168. $heightSize - 1,
  169. array(
  170. 'R' => 0,
  171. 'G' => 0,
  172. 'B' => 0
  173. )
  174. );
  175. /* Set the default font */
  176. $myPicture->setFontProperties(
  177. array(
  178. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  179. 'FontSize' => 10
  180. )
  181. );
  182. /* Do NOT Write the chart title */
  183. /* Define the chart area */
  184. $myPicture->setGraphArea(
  185. 50,
  186. 30,
  187. $widthSize - 20,
  188. $heightSize - 100
  189. );
  190. /* Draw the scale */
  191. $scaleSettings = array(
  192. 'GridR' => 200,
  193. 'GridG' => 200,
  194. 'GridB' => 200,
  195. 'DrawSubTicks' => true,
  196. 'CycleBackground' => true,
  197. 'Mode' => SCALE_MODE_MANUAL,
  198. 'ManualScale' => array(
  199. '0' => array(
  200. 'Min' => 0,
  201. 'Max' => 100,
  202. )
  203. ),
  204. 'LabelRotation' => $angle,
  205. );
  206. $myPicture->drawScale($scaleSettings);
  207. /* Turn on shadow computing */
  208. $myPicture->setShadow(
  209. true,
  210. array(
  211. 'X' => 1,
  212. 'Y' => 1,
  213. 'R' => 0,
  214. 'G' => 0,
  215. 'B' => 0,
  216. 'Alpha' => 10
  217. )
  218. );
  219. /* Draw the chart */
  220. $myPicture->setShadow(
  221. true,
  222. array(
  223. 'X' => 1,
  224. 'Y' => 1,
  225. 'R' => 0,
  226. 'G' => 0,
  227. 'B' => 0,
  228. 'Alpha' => 10
  229. )
  230. );
  231. $settings = array(
  232. 'DisplayValues' => true,
  233. 'DisplaySize' => $fontSize,
  234. 'DisplayR' => 0,
  235. 'DisplayG' => 0,
  236. 'DisplayB' => 0,
  237. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  238. 'Gradient' => false,
  239. 'Surrounding' => 30,
  240. 'InnerSurrounding' => 25
  241. );
  242. $myPicture->drawStackedBarChart($settings);
  243. $legendSettings = array(
  244. 'Mode' => LEGEND_HORIZONTAL,
  245. 'Style' => LEGEND_NOBORDER,
  246. );
  247. $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
  248. /* Write and save into cache */
  249. $myCache->writeToCache($chartHash, $myPicture);
  250. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  251. $myCache->saveFromCache($chartHash, $imgPath);
  252. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  253. }
  254. if (!empty($imgPath)) {
  255. $courses_graph[$course_code] = '<img src="' . $imgPath . '">';
  256. }
  257. }
  258. }
  259. } // end for
  260. }
  261. return $graphs;
  262. }
  263. /**
  264. * This method return a graph containing information about evaluations
  265. * inside courses in sessions, it's used inside get_block method for
  266. * showing it inside dashboard interface
  267. * @return string img html
  268. */
  269. public function get_evaluations_courses_in_sessions_graph()
  270. {
  271. $graphs = array();
  272. if (!empty($this->sessions)) {
  273. $session_ids = array_keys($this->sessions);
  274. foreach ($session_ids as $session_id) {
  275. $courses_code = array_keys(Tracking::get_courses_list_from_session($session_id));
  276. $courses_graph = array();
  277. foreach ($courses_code as $course_code) {
  278. $cats = Category::load(null, null, $course_code, null, null, $session_id);
  279. if (isset($cats) && isset($cats[0])) {
  280. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  281. $alllinks = $cats[0]->get_links(null, true);
  282. $users = GradebookUtils::get_all_users($alleval, $alllinks);
  283. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  284. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  285. if (!empty($evaluation_sumary)) {
  286. $items = array_keys($evaluation_sumary);
  287. $max = $min = $avg = array();
  288. foreach ($evaluation_sumary as $evaluation) {
  289. $max[] = $evaluation['max'];
  290. $min[] = $evaluation['min'];
  291. $avg[] = $evaluation['avg'];
  292. }
  293. // Dataset definition
  294. $dataSet = new pData();
  295. $dataSet->addPoints($min, 'Serie3');
  296. $dataSet->addPoints($avg, 'Serie2');
  297. $dataSet->addPoints($max, 'Serie1');
  298. $dataSet->addPoints($items, 'Labels');
  299. $dataSet->setSerieDescription('Serie1', get_lang('Max'));
  300. $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
  301. $dataSet->setSerieDescription('Serie3', get_lang('Min'));
  302. $dataSet->setAbscissa('Labels');
  303. $dataSet->setAbscissaName(get_lang('EvaluationName'));
  304. $dataSet->normalize(100, '%');
  305. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  306. // Cache definition
  307. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  308. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  309. $chartHash = $myCache->getHash($dataSet);
  310. if ($myCache->isInCache($chartHash)) {
  311. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  312. $myCache->saveFromCache($chartHash, $imgPath);
  313. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  314. } else {
  315. /* Create the pChart object */
  316. $widthSize = $this->bg_width;
  317. $heightSize = $this->bg_height;
  318. $fontSize = 8;
  319. $angle = 50;
  320. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  321. /* Turn of Antialiasing */
  322. $myPicture->Antialias = false;
  323. /* Add a border to the picture */
  324. $myPicture->drawRectangle(
  325. 0,
  326. 0,
  327. $widthSize - 1,
  328. $heightSize - 1,
  329. array(
  330. 'R' => 0,
  331. 'G' => 0,
  332. 'B' => 0
  333. )
  334. );
  335. /* Set the default font */
  336. $myPicture->setFontProperties(
  337. array(
  338. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  339. 'FontSize' => 10
  340. )
  341. );
  342. /* Do NOT Write the chart title */
  343. /* Define the chart area */
  344. $myPicture->setGraphArea(50, 30, $widthSize - 20, $heightSize - 100);
  345. /* Draw the scale */
  346. $scaleSettings = array(
  347. 'GridR' => 200,
  348. 'GridG' => 200,
  349. 'GridB' => 200,
  350. 'DrawSubTicks' => true,
  351. 'CycleBackground' => true,
  352. 'Mode' => SCALE_MODE_MANUAL,
  353. 'ManualScale' => array(
  354. '0' => array(
  355. 'Min' => 0,
  356. 'Max' => 100,
  357. )
  358. ),
  359. 'LabelRotation' => $angle,
  360. );
  361. $myPicture->drawScale($scaleSettings);
  362. /* Turn on shadow computing */
  363. $myPicture->setShadow(
  364. true,
  365. array(
  366. 'X' => 1,
  367. 'Y' => 1,
  368. 'R' => 0,
  369. 'G' => 0,
  370. 'B' => 0,
  371. 'Alpha' => 10
  372. )
  373. );
  374. /* Draw the chart */
  375. $myPicture->setShadow(
  376. true,
  377. array(
  378. 'X' => 1,
  379. 'Y' => 1,
  380. 'R' => 0,
  381. 'G' => 0,
  382. 'B' => 0,
  383. 'Alpha' => 10
  384. )
  385. );
  386. $settings = array(
  387. 'DisplayValues' => true,
  388. 'DisplaySize' => $fontSize,
  389. 'DisplayR' => 0,
  390. 'DisplayG' => 0,
  391. 'DisplayB' => 0,
  392. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  393. 'Gradient' => false,
  394. 'Surrounding' => 30,
  395. 'InnerSurrounding' => 25
  396. );
  397. $myPicture->drawStackedBarChart($settings);
  398. $legendSettings = array(
  399. 'Mode' => LEGEND_HORIZONTAL,
  400. 'Style' => LEGEND_NOBORDER,
  401. );
  402. $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
  403. /* Write and save into cache */
  404. $myCache->writeToCache($chartHash, $myPicture);
  405. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  406. $myCache->saveFromCache($chartHash, $imgPath);
  407. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  408. }
  409. if (!empty($imgPath)) {
  410. $courses_graph[$course_code] = '<img src="' . $imgPath . '">';
  411. }
  412. }
  413. }
  414. }
  415. if (!empty($courses_graph)) {
  416. $graphs[$session_id] = $courses_graph;
  417. }
  418. }
  419. }
  420. return $graphs;
  421. }
  422. }