block_evaluation_graph.class.php 22 KB

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