flatviewtable.class.php 16 KB

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