flatviewtable.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. * Display the graph of the total results of all students
  76. * */
  77. public function display_graph()
  78. {
  79. include_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  80. include_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  81. include_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  82. $header_name = $this->datagen->get_header_names();
  83. $total_users = $this->datagen->get_total_users_count();
  84. $img_file = '';
  85. if ($this->datagen->get_total_items_count()>0 && $total_users > 0 ) {
  86. //Removing user names and total
  87. array_shift($header_name);
  88. array_shift($header_name);
  89. array_pop($header_name);
  90. $user_results = $this->datagen->get_data_to_graph();
  91. $pre_result = $new_result = array();
  92. $DataSet = new pData();
  93. //$pre_result total score of students
  94. //filling the Dataset
  95. foreach($user_results as $result) {
  96. for($i=0; $i < count($header_name); $i++) {
  97. $pre_result[$i+3]+= $result[$i+1];
  98. }
  99. }
  100. $i = 1;
  101. $show_draw = false;
  102. if ($total_users >0 ) {
  103. foreach($pre_result as $res) {
  104. $total = $res / ($total_users);
  105. if ($total != 0) {
  106. $show_draw = true;
  107. }
  108. $DataSet->AddPoint($total, "Serie".$i);
  109. $DataSet->SetSerieName(strip_tags($header_name[$i-1]),"Serie".$i);
  110. // Dataset definition
  111. $DataSet->AddAllSeries();
  112. $DataSet->SetAbsciseLabelSerie();
  113. $i++;
  114. }
  115. }
  116. // Cache definition
  117. $Cache = new pCache();
  118. // the graph id
  119. $gradebook_id = intval($_GET['selectcat']);
  120. $graph_id = api_get_user_id().'AverageResultsVsResource'.$gradebook_id.api_get_course_id();
  121. $data = $DataSet->GetData();
  122. if ($show_draw) {
  123. if ($Cache->IsInCache($graph_id, $DataSet->GetData())) {
  124. //if (0) {
  125. //if we already created the img
  126. //echo 'in cache';
  127. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  128. } else {
  129. // if the image does not exist in the archive/ folder
  130. // Initialise the graph
  131. $Test = new pChart(760, 360);
  132. //which schema of color will be used
  133. $quant_resources = count($data[0])-1;
  134. // Adding the color schemma
  135. if ($quant_resources < 8) {
  136. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/reduced.txt");
  137. } else {
  138. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/default.txt");
  139. }
  140. // set font of the axes
  141. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  142. $Test->setGraphArea(50,30,610,300);
  143. $Test->drawFilledRoundedRectangle(7,7,780,330,5,240,240,240);
  144. //$Test->drawRoundedRectangle(5,5,790,330,5,230,230,230);
  145. //background color area & stripe or not
  146. $Test->drawGraphArea(255,255,255,TRUE);
  147. $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_START0 ,150,150,150,TRUE,0,1, FALSE);
  148. //background grid
  149. $Test->drawGrid(4,TRUE,230,230,230,50);
  150. // Draw the 0 line
  151. //$Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",6);
  152. //$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  153. // Draw the bar graph
  154. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  155. //Set legend properties: width, height and text color and font
  156. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",9);
  157. $Test->drawLegend(620, 70,$DataSet->GetDataDescription(),255,255,255);
  158. //Set title properties
  159. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",10);
  160. $Test->drawTitle(50,22,get_lang('AverageResultsVsResource'),50,50,80,620);
  161. //------------------
  162. //echo 'not in cache';
  163. $Cache->WriteToCache($graph_id,$DataSet->GetData(),$Test);
  164. ob_start();
  165. $Test->Stroke();
  166. ob_end_clean();
  167. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  168. }
  169. }
  170. }
  171. return api_get_path(WEB_ARCHIVE_PATH).$img_file;
  172. }
  173. /**
  174. *
  175. */
  176. public function display_graph_by_resource()
  177. {
  178. require_once api_get_path(LIBRARY_PATH).'pchart/pData.class.php';
  179. require_once api_get_path(LIBRARY_PATH).'pchart/pChart.class.php';
  180. require_once api_get_path(LIBRARY_PATH).'pchart/pCache.class.php';
  181. $header_name = $this->datagen->get_header_names();
  182. $total_users = $this->datagen->get_total_users_count();
  183. $img_file = '';
  184. if ($this->datagen->get_total_items_count() > 0 && $total_users > 0 ) {
  185. //Removing first name
  186. array_shift($header_name);
  187. //Removing last name
  188. array_shift($header_name);
  189. $displayscore = ScoreDisplay :: instance();
  190. $customdisplays = $displayscore->get_custom_score_display_settings();
  191. if (is_array($customdisplays) && count(($customdisplays))) {
  192. $user_results = $this->datagen->get_data_to_graph2(false);
  193. $pre_result = $new_result = array();
  194. $DataSet = new pData();
  195. //filling the Dataset
  196. foreach ($user_results as $result) {
  197. //print_r($result);
  198. for($i=0; $i< count($header_name); $i++) {
  199. $pre_result[$i+3][]= $result[$i+1];
  200. }
  201. }
  202. $i=0;
  203. $show_draw = false;
  204. $resource_list = array();
  205. $pre_result2 = array();
  206. foreach($pre_result as $key=>$res_array) {
  207. rsort($res_array);
  208. $pre_result2[] = $res_array;
  209. }
  210. //@todo when a display custom does not exist the order of the color does not match
  211. //filling all the answer that are not responded with 0
  212. rsort($customdisplays);
  213. if ($total_users > 0) {
  214. foreach($pre_result2 as $key=>$res_array) {
  215. $key_list = array();
  216. foreach($res_array as $user_result) {
  217. $resource_list[$key][$user_result[1]] += 1;
  218. $key_list[] = $user_result[1];
  219. }
  220. foreach ($customdisplays as $display) {
  221. if (!in_array($display['display'], $key_list))
  222. $resource_list[$key][$display['display']] = 0;
  223. }
  224. $i++;
  225. }
  226. }
  227. //fixing $resource_list
  228. $max = 0;
  229. $new_list = array();
  230. foreach($resource_list as $key=>$value) {
  231. $new_value = array();
  232. foreach($customdisplays as $item) {
  233. if ($value[$item['display']] > $max) {
  234. $max = $value[$item['display']];
  235. }
  236. $new_value[$item['display']] = strip_tags($value[$item['display']]);
  237. }
  238. $new_list[] = $new_value;
  239. }
  240. $resource_list = $new_list;
  241. $i = 1;
  242. $j = 0;
  243. foreach($resource_list as $key=>$resource) {
  244. $new_resource_list = $new_resource_list_name = array();
  245. $DataSet = new pData();
  246. // Reverse array, otherwise we get highest values first
  247. $resource = array_reverse($resource,true);
  248. foreach ($resource as $name=>$cant) {
  249. $DataSet->AddPoint($cant,"Serie".$j);
  250. $DataSet->SetSerieName(strip_tags($name),"Serie".$j);
  251. $j++;
  252. }
  253. //print_r($pre_result); print_r($header_name);
  254. // Dataset definition
  255. $DataSet->AddAllSeries();
  256. $DataSet->SetAbsciseLabelSerie('');
  257. $DataSet->SetXAxisName(get_lang('GradebookSkillsRanking'));
  258. $DataSet->SetYAxisName(get_lang('Students'));
  259. $show_draw = true;
  260. // Cache definition
  261. $Cache = new pCache();
  262. // the graph id
  263. $gradebook_id = intval($_GET['selectcat']);
  264. $graph_id = api_get_user_id().'ByResource'.$gradebook_id.api_get_course_id().api_get_session_id();
  265. if ($show_draw) {
  266. //if ($Cache->IsInCache($graph_id, $DataSet->GetData())) {
  267. if (0) {
  268. //if we already created the img we get the img file id
  269. //echo 'in cache';
  270. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  271. } else {
  272. // if the image does not exist in the archive/ folder
  273. // Initialise the graph
  274. $chart_size_w= 480;
  275. $chart_size_h= 250;
  276. $Test = new pChart($chart_size_w, $chart_size_h);
  277. // Adding the color schemma
  278. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/pastel.txt");
  279. // set font of the axes
  280. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  281. $area_graph_w = $chart_size_w-130;
  282. $Test->setGraphArea(50,30,$area_graph_w ,$chart_size_h-50);
  283. $Test->drawFilledRoundedRectangle(5,5,$chart_size_w-1,$chart_size_h-20,5,240,240,240);
  284. //$Test->drawRoundedRectangle(5,5,790,330,5,230,230,230);
  285. //background color area & stripe or not
  286. $Test->drawGraphArea(255,255,255,TRUE);
  287. //Setting max height by default see #3296
  288. if (!empty($max)) {
  289. if (is_int($max)) {
  290. $Test->setFixedScale(0, $max + 1, $max + 1);
  291. } else {
  292. $Test->setFixedScale(0, $max);
  293. }
  294. }
  295. $Test->drawScale($DataSet->GetData(), $DataSet->GetDataDescription(), SCALE_ADDALLSTART0, 150, 150, 150, TRUE, 0, 1, FALSE);
  296. //background grid
  297. $Test->drawGrid(4, TRUE,230,230,230,50);
  298. // Draw the 0 line
  299. //$Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",6);
  300. //$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  301. // Draw the bar graph
  302. $Test->setFontProperties(api_get_path(LIBRARY_PATH) . "pchart/fonts/tahoma.ttf", 11);
  303. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  304. //Set legend properties: width, height and text color and font
  305. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",9);
  306. $Test->drawLegend($area_graph_w+10, 50,$DataSet->GetDataDescription(),255,255,255);
  307. //Set title properties
  308. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",10);
  309. $Test->drawTitle(50,22, strip_tags($header_name[$i-1]),50,50,80,$chart_size_w-50);
  310. //------------------
  311. //echo 'not in cache';
  312. $Cache->WriteToCache($graph_id,$DataSet->GetData(),$Test);
  313. //ob_start();
  314. //$Test->Stroke();
  315. //ob_end_clean();
  316. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  317. }
  318. echo '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'" >';
  319. if ($i % 2 == 0 && $i!=0) {
  320. echo '<br />';
  321. }
  322. $i++;
  323. }
  324. } //end foreach
  325. } else {
  326. echo get_lang('ToViewGraphScoreRuleMustBeEnabled');
  327. }
  328. // Pie charts
  329. /*
  330. $show_draw = false;
  331. $resource_list = array();
  332. //print_r($pre_result_pie);
  333. if ($total_users>0) {
  334. foreach($pre_result_pie as $key=>$res_array) {
  335. //$resource_list
  336. foreach($res_array as $user_result) {
  337. $total+= $user_result / ($total_users*100);
  338. }
  339. echo $total;
  340. //echo $total = $res / ($total_users*100);
  341. echo '<br>';
  342. //$DataSet->AddPoint($total,"Serie".$i);
  343. //$DataSet->SetSerieName($header_name[$i-1],"Serie".$i);
  344. }
  345. }
  346. //here--------------
  347. foreach($resource_list as $key=>$resource) {
  348. $new_resource_list = $new_resource_list_name = array();
  349. foreach($resource as $name=>$cant) {
  350. $new_resource_list[]=$cant;
  351. $new_resource_list_name[]=$name;
  352. }
  353. //Pie chart
  354. $DataSet = new pData;
  355. $DataSet->AddPoint($new_resource_list,"Serie1");
  356. $DataSet->AddPoint($new_resource_list_name,"Serie2");
  357. $DataSet->AddAllSeries();
  358. $DataSet->SetAbsciseLabelSerie("Serie2");
  359. $Test = new pChart(400,300);
  360. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/soft_tones.txt");
  361. // background
  362. //$Test->drawFilledRoundedRectangle(7,7,293,193,5,240,240,240);
  363. // border color
  364. $Test->drawRoundedRectangle(5,5,295,195,5,230,230,230);
  365. // This will draw a shadow under the pie chart
  366. //$Test->drawFilledCircle(122,102,70,200,200,200);
  367. //Draw the pie chart
  368. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  369. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  370. $tmp_path = api_get_path(SYS_ARCHIVE_PATH);
  371. $Test->drawBasicPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,100,70,PIE_PERCENTAGE,255,255,218);
  372. $Test->drawPieLegend(230,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
  373. $user_id = api_get_user_id();
  374. $img_file_generated_name = $key.uniqid('').'gradebook.png';
  375. $Test->Render($tmp_path.$img_file_generated_name);
  376. chmod($tmp_path.$img_file_generated_name, api_get_permissions_for_new_files());
  377. if ($i % 2 == 0 && $i!= 0) {
  378. echo '<br>';
  379. }
  380. echo '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file_generated_name.'">';
  381. }
  382. */
  383. }
  384. }
  385. /**
  386. * Function used by SortableTable to get total number of items in the table
  387. */
  388. public function get_total_number_of_items()
  389. {
  390. return $this->datagen->get_total_users_count();
  391. }
  392. /**
  393. * Function used by SortableTable to generate the data to display
  394. */
  395. public function get_table_data($from = 1, $per_page = null, $column = null, $direction = null, $sort = null)
  396. {
  397. $is_western_name_order = api_is_western_name_order();
  398. // create page navigation if needed
  399. $totalitems = $this->datagen->get_total_items_count();
  400. if ($this->limit_enabled && $totalitems > LIMIT) {
  401. $selectlimit = LIMIT;
  402. } else {
  403. $selectlimit = $totalitems;
  404. }
  405. $header = null;
  406. if ($this->limit_enabled && $totalitems > LIMIT) {
  407. $header .= '<table style="width: 100%; text-align: right; margin-left: auto; margin-right: auto;" border="0" cellpadding="2">'
  408. .'<tbody>'
  409. .'<tr>';
  410. // previous X
  411. $header .= '<td style="width:100%;">';
  412. if ($this->offset >= LIMIT) {
  413. $header .= '<a href="'.api_get_self()
  414. .'?selectcat='.Security::remove_XSS($_GET['selectcat'])
  415. .'&offset='.(($this->offset)-LIMIT)
  416. .(isset($_GET['search'])?'&search='.Security::remove_XSS($_GET['search']):'').'">'
  417. .Display::return_icon('action_prev.png', get_lang('PreviousPage'), array(), 32)
  418. .'</a>';
  419. } else {
  420. $header .= Display::return_icon('action_prev_na.png', get_lang('PreviousPage'), array(), 32);
  421. }
  422. $header .= ' ';
  423. // next X
  424. $calcnext = (($this->offset+(2*LIMIT)) > $totalitems) ?
  425. ($totalitems-(LIMIT+$this->offset)) : LIMIT;
  426. if ($calcnext > 0) {
  427. $header .= '<a href="'.api_get_self()
  428. .'?selectcat='.Security::remove_XSS($_GET['selectcat'])
  429. .'&offset='.($this->offset+LIMIT)
  430. .(isset($_GET['search'])?'&search='.Security::remove_XSS($_GET['search']):'').'">'
  431. .Display::return_icon('action_next.png', get_lang('NextPage'), array(), 32)
  432. .'</a>';
  433. } else {
  434. $header .= Display::return_icon('action_next_na.png', get_lang('NextPage'), array(), 32);
  435. }
  436. $header .= '</td>';
  437. $header .= '</tbody></table>';
  438. echo $header;
  439. }
  440. // retrieve sorting type
  441. if ($is_western_name_order) {
  442. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME : FlatViewDataGenerator :: FVDG_SORT_LASTNAME);
  443. } else {
  444. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_LASTNAME : FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME);
  445. }
  446. if ($this->direction == 'DESC') {
  447. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_DESC;
  448. } else {
  449. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_ASC;
  450. }
  451. // step 1: generate columns: evaluations and links
  452. $header_names = $this->datagen->get_header_names($this->offset, $selectlimit);
  453. $column = 0;
  454. if ($is_western_name_order) {
  455. $this->set_header($column++, $header_names[1]);
  456. $this->set_header($column++, $header_names[0]);
  457. } else {
  458. $this->set_header($column++, $header_names[0]);
  459. $this->set_header($column++, $header_names[1]);
  460. }
  461. while ($column < count($header_names)) {
  462. $this->set_header($column, $header_names[$column], false);
  463. $column++;
  464. }
  465. $data_array = $this->datagen->get_data(
  466. $users_sorting,
  467. $from,
  468. $this->per_page,
  469. $this->offset,
  470. $selectlimit
  471. );
  472. $table_data = array();
  473. foreach ($data_array as $user_row) {
  474. $table_row = array ();
  475. $count = 0;
  476. $user_id = $user_row[$count++];
  477. $lastname = $user_row[$count++];
  478. $firstname = $user_row[$count++];
  479. if ($is_western_name_order) {
  480. $table_row[] = $this->build_name_link($user_id, $firstname);
  481. $table_row[] = $this->build_name_link($user_id, $lastname);
  482. } else {
  483. $table_row[] = $this->build_name_link($user_id, $lastname);
  484. $table_row[] = $this->build_name_link($user_id, $firstname);
  485. }
  486. while ($count < count($user_row)) {
  487. $table_row[] = $user_row[$count++];
  488. }
  489. $table_data[]= $table_row;
  490. }
  491. return $table_data;
  492. }
  493. // Other functions
  494. /**
  495. * @param $user_id
  496. * @param $name
  497. * @return string
  498. */
  499. private function build_name_link($user_id, $name)
  500. {
  501. return '<a href="user_stats.php?userid='.$user_id.'&selectcat='.$this->selectcat->get_id().'">'.$name.'</a>';
  502. }
  503. }