block_evaluation_graph.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  4. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  5. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/result.class.php';
  6. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/linkfactory.class.php';
  7. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/flatview_data_generator.class.php';
  8. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  9. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/category.class.php';
  10. use CpChart\Classes\pData as pData;
  11. use CpChart\Classes\pImage as pImage;
  12. use CpChart\Classes\pCache as pCache;
  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. $dataSet = new pData();
  140. $dataSet->addPoints($min, 'Serie3');
  141. $dataSet->addPoints($avg, 'Serie2');
  142. $dataSet->addPoints($max, 'Serie1');
  143. $dataSet->addPoints($items, 'Labels');
  144. $dataSet->setSerieDescription('Serie1', get_lang('Max'));
  145. $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
  146. $dataSet->setSerieDescription('Serie3', get_lang('Min'));
  147. $dataSet->setAbscissa('Labels');
  148. $dataSet->setAbscissaName(get_lang('EvaluationName'));
  149. $dataSet->normalize(100, '%');
  150. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  151. // Cache definition
  152. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  153. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  154. $chartHash = $myCache->getHash($dataSet);
  155. if ($myCache->isInCache($chartHash)) {
  156. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  157. $myCache->saveFromCache($chartHash, $imgPath);
  158. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  159. } else {
  160. /* Create the pChart object */
  161. $widthSize = $this->bg_width;
  162. $heightSize = $this->bg_height;
  163. $fontSize = 8;
  164. $angle = 50;
  165. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  166. /* Turn of Antialiasing */
  167. $myPicture->Antialias = false;
  168. /* Add a border to the picture */
  169. $myPicture->drawRectangle(
  170. 0,
  171. 0,
  172. $widthSize - 1,
  173. $heightSize - 1,
  174. array(
  175. 'R' => 0,
  176. 'G' => 0,
  177. 'B' => 0
  178. )
  179. );
  180. /* Set the default font */
  181. $myPicture->setFontProperties(
  182. array(
  183. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  184. 'FontSize' => 10
  185. )
  186. );
  187. /* Do NOT Write the chart title */
  188. /* Define the chart area */
  189. $myPicture->setGraphArea(
  190. 50,
  191. 30,
  192. $widthSize - 20,
  193. $heightSize - 100
  194. );
  195. /* Draw the scale */
  196. $scaleSettings = array(
  197. 'GridR' => 200,
  198. 'GridG' => 200,
  199. 'GridB' => 200,
  200. 'DrawSubTicks' => true,
  201. 'CycleBackground' => true,
  202. 'Mode' => SCALE_MODE_MANUAL,
  203. 'ManualScale' => array(
  204. '0' => array(
  205. 'Min' => 0,
  206. 'Max' => 100,
  207. )
  208. ),
  209. 'LabelRotation' => $angle,
  210. );
  211. $myPicture->drawScale($scaleSettings);
  212. /* Turn on shadow computing */
  213. $myPicture->setShadow(
  214. true,
  215. array(
  216. 'X' => 1,
  217. 'Y' => 1,
  218. 'R' => 0,
  219. 'G' => 0,
  220. 'B' => 0,
  221. 'Alpha' => 10
  222. )
  223. );
  224. /* Draw the chart */
  225. $myPicture->setShadow(
  226. true,
  227. array(
  228. 'X' => 1,
  229. 'Y' => 1,
  230. 'R' => 0,
  231. 'G' => 0,
  232. 'B' => 0,
  233. 'Alpha' => 10
  234. )
  235. );
  236. $settings = array(
  237. 'DisplayValues' => true,
  238. 'DisplaySize' => $fontSize,
  239. 'DisplayR' => 0,
  240. 'DisplayG' => 0,
  241. 'DisplayB' => 0,
  242. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  243. 'Gradient' => false,
  244. 'Surrounding' => 30,
  245. 'InnerSurrounding' => 25
  246. );
  247. $myPicture->drawStackedBarChart($settings);
  248. $legendSettings = array(
  249. 'Mode' => LEGEND_HORIZONTAL,
  250. 'Style' => LEGEND_NOBORDER,
  251. );
  252. $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
  253. /* Write and save into cache */
  254. $myCache->writeToCache($chartHash, $myPicture);
  255. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  256. $myCache->saveFromCache($chartHash, $imgPath);
  257. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  258. }
  259. if (!empty($imgPath)) {
  260. $courses_graph[$course_code] = '<img src="' . $imgPath . '">';
  261. }
  262. }
  263. }
  264. } // end for
  265. }
  266. return $graphs;
  267. }
  268. /**
  269. * This method return a graph containing information about evaluations
  270. * inside courses in sessions, it's used inside get_block method for
  271. * showing it inside dashboard interface
  272. * @return string img html
  273. */
  274. public function get_evaluations_courses_in_sessions_graph()
  275. {
  276. $graphs = array();
  277. if (!empty($this->sessions)) {
  278. $session_ids = array_keys($this->sessions);
  279. foreach ($session_ids as $session_id) {
  280. $courses_code = array_keys(Tracking::get_courses_list_from_session($session_id));
  281. $courses_graph = array();
  282. foreach ($courses_code as $course_code) {
  283. $cats = Category::load(null, null, $course_code, null, null, $session_id);
  284. if (isset($cats) && isset($cats[0])) {
  285. $alleval = $cats[0]->get_evaluations(null, true, $course_code);
  286. $alllinks = $cats[0]->get_links(null, true);
  287. $users = get_all_users($alleval, $alllinks);
  288. $datagen = new FlatViewDataGenerator ($users, $alleval, $alllinks);
  289. $evaluation_sumary = $datagen->get_evaluation_sumary_results();
  290. if (!empty($evaluation_sumary)) {
  291. $items = array_keys($evaluation_sumary);
  292. $max = $min = $avg = array();
  293. foreach ($evaluation_sumary as $evaluation) {
  294. $max[] = $evaluation['max'];
  295. $min[] = $evaluation['min'];
  296. $avg[] = $evaluation['avg'];
  297. }
  298. // Dataset definition
  299. $dataSet = new pData();
  300. $dataSet->addPoints($min, 'Serie3');
  301. $dataSet->addPoints($avg, 'Serie2');
  302. $dataSet->addPoints($max, 'Serie1');
  303. $dataSet->addPoints($items, 'Labels');
  304. $dataSet->setSerieDescription('Serie1', get_lang('Max'));
  305. $dataSet->setSerieDescription('Serie2', get_lang('Avg'));
  306. $dataSet->setSerieDescription('Serie3', get_lang('Min'));
  307. $dataSet->setAbscissa('Labels');
  308. $dataSet->setAbscissaName(get_lang('EvaluationName'));
  309. $dataSet->normalize(100, '%');
  310. $dataSet->loadPalette(api_get_path(SYS_CODE_PATH) . 'palettes/pchart/default.color', true);
  311. // Cache definition
  312. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  313. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  314. $chartHash = $myCache->getHash($dataSet);
  315. if ($myCache->isInCache($chartHash)) {
  316. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  317. $myCache->saveFromCache($chartHash, $imgPath);
  318. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  319. } else {
  320. /* Create the pChart object */
  321. $widthSize = $this->bg_width;
  322. $heightSize = $this->bg_height;
  323. $fontSize = 8;
  324. $angle = 50;
  325. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  326. /* Turn of Antialiasing */
  327. $myPicture->Antialias = false;
  328. /* Add a border to the picture */
  329. $myPicture->drawRectangle(
  330. 0,
  331. 0,
  332. $widthSize - 1,
  333. $heightSize - 1,
  334. array(
  335. 'R' => 0,
  336. 'G' => 0,
  337. 'B' => 0
  338. )
  339. );
  340. /* Set the default font */
  341. $myPicture->setFontProperties(
  342. array(
  343. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  344. 'FontSize' => 10
  345. )
  346. );
  347. /* Do NOT Write the chart title */
  348. /* Define the chart area */
  349. $myPicture->setGraphArea(50, 30, $widthSize - 20, $heightSize - 100);
  350. /* Draw the scale */
  351. $scaleSettings = array(
  352. 'GridR' => 200,
  353. 'GridG' => 200,
  354. 'GridB' => 200,
  355. 'DrawSubTicks' => true,
  356. 'CycleBackground' => true,
  357. 'Mode' => SCALE_MODE_MANUAL,
  358. 'ManualScale' => array(
  359. '0' => array(
  360. 'Min' => 0,
  361. 'Max' => 100,
  362. )
  363. ),
  364. 'LabelRotation' => $angle,
  365. );
  366. $myPicture->drawScale($scaleSettings);
  367. /* Turn on shadow computing */
  368. $myPicture->setShadow(
  369. true,
  370. array(
  371. 'X' => 1,
  372. 'Y' => 1,
  373. 'R' => 0,
  374. 'G' => 0,
  375. 'B' => 0,
  376. 'Alpha' => 10
  377. )
  378. );
  379. /* Draw the chart */
  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. $settings = array(
  392. 'DisplayValues' => true,
  393. 'DisplaySize' => $fontSize,
  394. 'DisplayR' => 0,
  395. 'DisplayG' => 0,
  396. 'DisplayB' => 0,
  397. 'DisplayOrientation' => ORIENTATION_HORIZONTAL,
  398. 'Gradient' => false,
  399. 'Surrounding' => 30,
  400. 'InnerSurrounding' => 25
  401. );
  402. $myPicture->drawStackedBarChart($settings);
  403. $legendSettings = array(
  404. 'Mode' => LEGEND_HORIZONTAL,
  405. 'Style' => LEGEND_NOBORDER,
  406. );
  407. $myPicture->drawLegend($widthSize / 2, 15, $legendSettings);
  408. /* Write and save into cache */
  409. $myCache->writeToCache($chartHash, $myPicture);
  410. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  411. $myCache->saveFromCache($chartHash, $imgPath);
  412. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  413. }
  414. if (!empty($imgPath)) {
  415. $courses_graph[$course_code] = '<img src="' . $imgPath . '">';
  416. }
  417. }
  418. }
  419. }
  420. if (!empty($courses_graph)) {
  421. $graphs[$session_id] = $courses_graph;
  422. }
  423. }
  424. }
  425. return $graphs;
  426. }
  427. }