flatviewtable.class.php 17 KB

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