flatviewtable.class.php 17 KB

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