flatviewtable.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. set_time_limit(0);
  4. use CpChart\Classes\pCache as pCache;
  5. use CpChart\Classes\pData as pData;
  6. use CpChart\Classes\pImage as pImage;
  7. /**
  8. * Class FlatViewTable
  9. * Table to display flat view (all evaluations and links for all students)
  10. * @author Stijn Konings
  11. * @author Bert Steppé - (refactored, optimised)
  12. * @author Julio Montoya Armas - Gradebook Graphics
  13. *
  14. * @package chamilo.gradebook
  15. */
  16. class FlatViewTable extends SortableTable
  17. {
  18. public $datagen;
  19. private $selectcat;
  20. private $limit_enabled;
  21. private $offset;
  22. private $mainCourseCategory;
  23. /**
  24. * @param Category $selectcat
  25. * @param array $users
  26. * @param array $evals
  27. * @param array $links
  28. * @param bool $limit_enabled
  29. * @param int $offset
  30. * @param null $addparams
  31. * @param Category $mainCourseCategory
  32. */
  33. public function FlatViewTable(
  34. $selectcat,
  35. $users = array(),
  36. $evals = array(),
  37. $links = array(),
  38. $limit_enabled = false,
  39. $offset = 0,
  40. $addparams = null,
  41. $mainCourseCategory = null
  42. ) {
  43. parent :: __construct('flatviewlist', null, null, api_is_western_name_order() ? 1 : 0);
  44. $this->selectcat = $selectcat;
  45. $this->datagen = new FlatViewDataGenerator(
  46. $users,
  47. $evals,
  48. $links,
  49. array('only_subcat' => $this->selectcat->get_id()),
  50. $mainCourseCategory
  51. );
  52. $this->limit_enabled = $limit_enabled;
  53. $this->offset = $offset;
  54. if (isset($addparams)) {
  55. $this->set_additional_parameters($addparams);
  56. }
  57. // step 2: generate rows: students
  58. $this->datagen->category = $this->selectcat;
  59. $this->mainCourseCategory = $mainCourseCategory;
  60. }
  61. /**
  62. * @param bool $value
  63. */
  64. public function setLimitEnabled($value)
  65. {
  66. $this->limit_enabled = (bool) $value;
  67. }
  68. /**
  69. * @return Category
  70. */
  71. public function getMainCourseCategory()
  72. {
  73. return $this->mainCourseCategory;
  74. }
  75. /**
  76. * Display gradebook graphs
  77. */
  78. public function display_graph_by_resource()
  79. {
  80. $headerName = $this->datagen->get_header_names();
  81. $total_users = $this->datagen->get_total_users_count();
  82. $displayscore = ScoreDisplay::instance();
  83. $customdisplays = $displayscore->get_custom_score_display_settings();
  84. if (empty($customdisplays)) {
  85. echo get_lang('ToViewGraphScoreRuleMustBeEnabled');
  86. return '';
  87. }
  88. $user_results = $this->datagen->get_data_to_graph2(false);
  89. //if (empty($this->datagen->get_total_items_count()) || empty($total_users)) {
  90. if (empty($user_results) || empty($total_users)) {
  91. echo get_lang('NoResults');
  92. return '';
  93. }
  94. // Removing first name
  95. array_shift($headerName);
  96. // Removing last name
  97. array_shift($headerName);
  98. $pre_result = $new_result = array();
  99. foreach ($user_results as $result) {
  100. for ($i = 0; $i < count($headerName); $i++) {
  101. if (isset($result[$i + 1])) {
  102. $pre_result[$i + 3][] = $result[$i + 1];
  103. }
  104. }
  105. }
  106. $i = 0;
  107. $show_draw = false;
  108. $resource_list = array();
  109. $pre_result2 = array();
  110. foreach ($pre_result as $key => $res_array) {
  111. rsort($res_array);
  112. $pre_result2[] = $res_array;
  113. }
  114. //@todo when a display custom does not exist the order of the color does not match
  115. //filling all the answer that are not responded with 0
  116. rsort($customdisplays);
  117. if ($total_users > 0) {
  118. foreach ($pre_result2 as $key => $res_array) {
  119. $key_list = array();
  120. foreach ($res_array as $user_result) {
  121. $userResult = isset($user_result[1]) ? $user_result[1] : null;
  122. if (!isset($resource_list[$key][$userResult])) {
  123. $resource_list[$key][$userResult] = 0;
  124. }
  125. $resource_list[$key][$userResult] += 1;
  126. $key_list[] = $userResult;
  127. }
  128. foreach ($customdisplays as $display) {
  129. if (!in_array($display['display'], $key_list))
  130. $resource_list[$key][$display['display']] = 0;
  131. }
  132. $i++;
  133. }
  134. }
  135. //fixing $resource_list
  136. $max = 0;
  137. $new_list = array();
  138. foreach ($resource_list as $key => $value) {
  139. $new_value = array();
  140. foreach ($customdisplays as $item) {
  141. if ($value[$item['display']] > $max) {
  142. $max = $value[$item['display']];
  143. }
  144. $new_value[$item['display']] = strip_tags($value[$item['display']]);
  145. }
  146. $new_list[] = $new_value;
  147. }
  148. $resource_list = $new_list;
  149. $i = 1;
  150. foreach ($resource_list as $key => $resource) {
  151. // Reverse array, otherwise we get highest values first
  152. $resource = array_reverse($resource, true);
  153. $dataSet = new pData();
  154. $dataSet->addPoints($resource, 'Serie');
  155. $dataSet->addPoints(array_keys($resource), 'Labels');
  156. $dataSet->setSerieDescription('Labels', strip_tags($headerName[$i - 1]));
  157. $dataSet->setAbscissa('Labels');
  158. $dataSet->setAbscissaName(get_lang('GradebookSkillsRanking'));
  159. $dataSet->setAxisName(0, get_lang('Students'));
  160. $palette = array(
  161. '0' => array('R' => 186, 'G' => 206, 'B' => 151, 'Alpha' => 100),
  162. '1' => array('R' => 210, 'G' => 148, 'B' => 147, 'Alpha' => 100),
  163. '2' => array('R' => 148, 'G' => 170, 'B' => 208, 'Alpha' => 100),
  164. '3' => array('R' => 221, 'G' => 133, 'B' => 61, 'Alpha' => 100),
  165. '4' => array('R' => 65, 'G' => 153, 'B' => 176, 'Alpha' => 100),
  166. '5' => array('R' => 114, 'G' => 88, 'B' => 144, 'Alpha' => 100),
  167. '6' => array('R' => 138, 'G' => 166, 'B' => 78, 'Alpha' => 100),
  168. '7' => array('R' => 171, 'G' => 70, 'B' => 67, 'Alpha' => 100),
  169. '8' => array('R' => 69, 'G' => 115, 'B' => 168, 'Alpha' => 100),
  170. );
  171. // Cache definition
  172. $cachePath = api_get_path(SYS_ARCHIVE_PATH);
  173. $myCache = new pCache(array('CacheFolder' => substr($cachePath, 0, strlen($cachePath) - 1)));
  174. $chartHash = $myCache->getHash($dataSet);
  175. if ($myCache->isInCache($chartHash)) {
  176. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  177. $myCache->saveFromCache($chartHash, $imgPath);
  178. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  179. } else {
  180. /* Create the pChart object */
  181. $widthSize = 480;
  182. $heightSize = 250;
  183. $myPicture = new pImage($widthSize, $heightSize, $dataSet);
  184. /* Turn of Antialiasing */
  185. $myPicture->Antialias = false;
  186. /* Add a border to the picture */
  187. $myPicture->drawRectangle(
  188. 0,
  189. 0,
  190. $widthSize - 1,
  191. $heightSize - 1,
  192. array(
  193. 'R' => 0,
  194. 'G' => 0,
  195. 'B' => 0
  196. )
  197. );
  198. /* Set the default font */
  199. $myPicture->setFontProperties(
  200. array(
  201. 'FontName' => api_get_path(SYS_FONTS_PATH) . 'opensans/OpenSans-Regular.ttf',
  202. 'FontSize' => 10
  203. )
  204. );
  205. /* Write the chart title */
  206. $myPicture->drawText(
  207. 250,
  208. 30,
  209. strip_tags($headerName[$i - 1]),
  210. array(
  211. 'FontSize' => 12,
  212. 'Align' => TEXT_ALIGN_BOTTOMMIDDLE
  213. )
  214. );
  215. /* Define the chart area */
  216. $myPicture->setGraphArea(50, 40, $widthSize - 20, $heightSize - 50);
  217. /* Draw the scale */
  218. $scaleSettings = array(
  219. 'GridR' => 200,
  220. 'GridG' => 200,
  221. 'GridB' => 200,
  222. 'DrawSubTicks' => true,
  223. 'CycleBackground' => true,
  224. 'Mode' => SCALE_MODE_START0
  225. );
  226. $myPicture->drawScale($scaleSettings);
  227. /* Turn on shadow computing */
  228. $myPicture->setShadow(
  229. true,
  230. array(
  231. 'X' => 1,
  232. 'Y' => 1,
  233. 'R' => 0,
  234. 'G' => 0,
  235. 'B' => 0,
  236. 'Alpha' => 10
  237. )
  238. );
  239. /* Draw the chart */
  240. $myPicture->setShadow(
  241. true,
  242. array(
  243. 'X' => 1,
  244. 'Y' => 1,
  245. 'R' => 0,
  246. 'G' => 0,
  247. 'B' => 0,
  248. 'Alpha' => 10
  249. )
  250. );
  251. $settings = array(
  252. 'OverrideColors' => $palette,
  253. 'Gradient' => false,
  254. 'GradientMode' => GRADIENT_SIMPLE,
  255. 'DisplayPos' => LABEL_POS_TOP,
  256. 'DisplayValues' => true,
  257. 'DisplayR' => 0,
  258. 'DisplayG' => 0,
  259. 'DisplayB' => 0,
  260. 'DisplayShadow' => true,
  261. 'Surrounding' => 10,
  262. );
  263. $myPicture->drawBarChart($settings);
  264. /* Render the picture (choose the best way) */
  265. $myCache->writeToCache($chartHash, $myPicture);
  266. $imgPath = api_get_path(SYS_ARCHIVE_PATH) . $chartHash;
  267. $myCache->saveFromCache($chartHash, $imgPath);
  268. $imgPath = api_get_path(WEB_ARCHIVE_PATH) . $chartHash;
  269. }
  270. echo '<img src="' . $imgPath . '" >';
  271. if ($i % 2 == 0 && $i != 0) {
  272. echo '<br /><br />';
  273. } else {
  274. echo '&nbsp;&nbsp;&nbsp;';
  275. }
  276. $i++;
  277. }
  278. }
  279. /**
  280. * Function used by SortableTable to get total number of items in the table
  281. */
  282. public function get_total_number_of_items()
  283. {
  284. return $this->datagen->get_total_users_count();
  285. }
  286. /**
  287. * Function used by SortableTable to generate the data to display
  288. */
  289. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = null)
  290. {
  291. $is_western_name_order = api_is_western_name_order();
  292. // create page navigation if needed
  293. $totalitems = $this->datagen->get_total_items_count();
  294. if ($this->limit_enabled && $totalitems > GRADEBOOK_ITEM_LIMIT) {
  295. $selectlimit = GRADEBOOK_ITEM_LIMIT;
  296. } else {
  297. $selectlimit = $totalitems;
  298. }
  299. $header = null;
  300. if ($this->limit_enabled && $totalitems > GRADEBOOK_ITEM_LIMIT) {
  301. $header .= '<table style="width: 100%; text-align: right; margin-left: auto; margin-right: auto;" border="0" cellpadding="2">'
  302. . '<tbody>'
  303. . '<tr>';
  304. // previous X
  305. $header .= '<td style="width:100%;">';
  306. if ($this->offset >= GRADEBOOK_ITEM_LIMIT) {
  307. $header .= '<a href="' . api_get_self()
  308. . '?selectcat=' . Security::remove_XSS($_GET['selectcat'])
  309. . '&offset=' . (($this->offset) - GRADEBOOK_ITEM_LIMIT)
  310. . (isset($_GET['search']) ? '&search=' . Security::remove_XSS($_GET['search']) : '') . '">'
  311. . Display::return_icon('action_prev.png', get_lang('PreviousPage'), array(), 32)
  312. . '</a>';
  313. } else {
  314. $header .= Display::return_icon('action_prev_na.png', get_lang('PreviousPage'), array(), 32);
  315. }
  316. $header .= ' ';
  317. // next X
  318. $calcnext = (($this->offset + (2 * GRADEBOOK_ITEM_LIMIT)) > $totalitems) ?
  319. ($totalitems - (GRADEBOOK_ITEM_LIMIT + $this->offset)) : GRADEBOOK_ITEM_LIMIT;
  320. if ($calcnext > 0) {
  321. $header .= '<a href="' . api_get_self()
  322. . '?selectcat=' . Security::remove_XSS($_GET['selectcat'])
  323. . '&offset=' . ($this->offset + GRADEBOOK_ITEM_LIMIT)
  324. . (isset($_GET['search']) ? '&search=' . Security::remove_XSS($_GET['search']) : '') . '">'
  325. . Display::return_icon('action_next.png', get_lang('NextPage'), array(), 32)
  326. . '</a>';
  327. } else {
  328. $header .= Display::return_icon('action_next_na.png', get_lang('NextPage'), array(), 32);
  329. }
  330. $header .= '</td>';
  331. $header .= '</tbody></table>';
  332. echo $header;
  333. }
  334. // retrieve sorting type
  335. if ($is_western_name_order) {
  336. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME : FlatViewDataGenerator :: FVDG_SORT_LASTNAME);
  337. } else {
  338. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_LASTNAME : FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME);
  339. }
  340. if ($this->direction == 'DESC') {
  341. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_DESC;
  342. } else {
  343. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_ASC;
  344. }
  345. // step 1: generate columns: evaluations and links
  346. $header_names = $this->datagen->get_header_names($this->offset, $selectlimit);
  347. $userRowSpan = false;
  348. foreach ($header_names as $item) {
  349. if (is_array($item)) {
  350. $userRowSpan = true;
  351. break;
  352. }
  353. }
  354. $thAttributes = '';
  355. if ($userRowSpan) {
  356. $thAttributes = 'rowspan=2';
  357. }
  358. $this->set_header(0, $header_names[0], true, $thAttributes);
  359. $this->set_header(1, $header_names[1], true, $thAttributes);
  360. $column = 2;
  361. $firstHeader = [];
  362. while ($column < count($header_names)) {
  363. $headerData = $header_names[$column];
  364. if (is_array($headerData)) {
  365. $countItems = count($headerData['items']);
  366. $this->set_header(
  367. $column,
  368. $headerData['header'],
  369. false,
  370. 'colspan="'.$countItems.'"'
  371. );
  372. foreach ($headerData['items'] as $item) {
  373. $firstHeader[] = '<center>'.$item.'</center>';
  374. }
  375. } else {
  376. $this->set_header($column, $headerData, false, $thAttributes);
  377. }
  378. $column++;
  379. }
  380. $data_array = $this->datagen->get_data(
  381. $users_sorting,
  382. $from,
  383. $this->per_page,
  384. $this->offset,
  385. $selectlimit
  386. );
  387. $table_data = array();
  388. if (!empty($firstHeader)) {
  389. $table_data[] = $firstHeader;
  390. }
  391. foreach ($data_array as $user_row) {
  392. $user_id = $user_row[0];
  393. unset($user_row[0]);
  394. $userInfo = api_get_user_info($user_id);
  395. if ($is_western_name_order) {
  396. $user_row[1] = $this->build_name_link($user_id, $userInfo['firstname']);
  397. $user_row[2] = $this->build_name_link($user_id, $userInfo['lastname']);
  398. } else {
  399. $user_row[1] = $this->build_name_link($user_id, $userInfo['lastname']);
  400. $user_row[2] = $this->build_name_link($user_id, $userInfo['firstname']);
  401. }
  402. $user_row = array_values($user_row);
  403. $table_data[] = $user_row;
  404. }
  405. return $table_data;
  406. }
  407. /**
  408. * @param $user_id
  409. * @param $name
  410. *
  411. * @return string
  412. */
  413. private function build_name_link($user_id, $name)
  414. {
  415. return '<a href="user_stats.php?userid=' . $user_id . '&selectcat=' . $this->selectcat->get_id() . '&'.api_get_cidreq().'">' . $name . '</a>';
  416. }
  417. }