flatviewtable.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. // Reverse array, otherwise we get highest values first
  209. $resource = array_reverse($resource,true);
  210. foreach ($resource as $name=>$cant) {
  211. $DataSet->AddPoint($cant,"Serie".$j);
  212. $DataSet->SetSerieName(strip_tags($name),"Serie".$j);
  213. $j++;
  214. }
  215. //print_r($pre_result); print_r($header_name);
  216. // Dataset definition
  217. $DataSet->AddAllSeries();
  218. $DataSet->SetAbsciseLabelSerie('');
  219. $DataSet->SetXAxisName(get_lang('GradebookSkillsRanking'));
  220. $DataSet->SetYAxisName(get_lang('Students'));
  221. $show_draw = true;
  222. // Cache definition
  223. $Cache = new pCache();
  224. // the graph id
  225. $gradebook_id = intval($_GET['selectcat']);
  226. $graph_id = api_get_user_id().'ByResource'.$gradebook_id.api_get_course_id().api_get_session_id();
  227. if ($show_draw) {
  228. //if ($Cache->IsInCache($graph_id, $DataSet->GetData())) {
  229. if (0) {
  230. //if we already created the img we get the img file id
  231. //echo 'in cache';
  232. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  233. } else {
  234. // if the image does not exist in the archive/ folder
  235. // Initialise the graph
  236. $chart_size_w= 480;
  237. $chart_size_h= 250;
  238. $Test = new pChart($chart_size_w, $chart_size_h);
  239. // Adding the color schemma
  240. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/pastel.txt");
  241. // set font of the axes
  242. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  243. $area_graph_w = $chart_size_w-130;
  244. $Test->setGraphArea(50,30,$area_graph_w ,$chart_size_h-50);
  245. $Test->drawFilledRoundedRectangle(5,5,$chart_size_w-1,$chart_size_h-20,5,240,240,240);
  246. //$Test->drawRoundedRectangle(5,5,790,330,5,230,230,230);
  247. //background color area & stripe or not
  248. $Test->drawGraphArea(255,255,255,TRUE);
  249. //Setting max height by default see #3296
  250. if (!empty($max)) {
  251. $Test->setFixedScale(0, $max);
  252. }
  253. $Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(), SCALE_ADDALLSTART0, 150,150,150, TRUE, 0, 0, FALSE);
  254. //background grid
  255. $Test->drawGrid(4, TRUE,230,230,230,50);
  256. // Draw the 0 line
  257. //$Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",6);
  258. //$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
  259. // Draw the bar graph
  260. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  261. //Set legend properties: width, height and text color and font
  262. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",9);
  263. $Test->drawLegend($area_graph_w+10, 50,$DataSet->GetDataDescription(),255,255,255);
  264. //Set title properties
  265. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",10);
  266. $Test->drawTitle(50,22, strip_tags($header_name[$i-1]),50,50,80,$chart_size_w-50);
  267. //------------------
  268. //echo 'not in cache';
  269. $Cache->WriteToCache($graph_id,$DataSet->GetData(),$Test);
  270. //ob_start();
  271. //$Test->Stroke();
  272. //ob_end_clean();
  273. $img_file = $Cache->GetHash($graph_id,$DataSet->GetData());
  274. }
  275. echo '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file.'" >';
  276. if ($i % 2 == 0 && $i!=0) {
  277. echo '<br />';
  278. }
  279. $i++;
  280. }
  281. } //end foreach
  282. } else {
  283. echo get_lang('ToViewGraphScoreRuleMustBeEnabled');
  284. }
  285. // Pie charts
  286. /*
  287. $show_draw = false;
  288. $resource_list = array();
  289. //print_r($pre_result_pie);
  290. if ($total_users>0) {
  291. foreach($pre_result_pie as $key=>$res_array) {
  292. //$resource_list
  293. foreach($res_array as $user_result) {
  294. $total+= $user_result / ($total_users*100);
  295. }
  296. echo $total;
  297. //echo $total = $res / ($total_users*100);
  298. echo '<br>';
  299. //$DataSet->AddPoint($total,"Serie".$i);
  300. //$DataSet->SetSerieName($header_name[$i-1],"Serie".$i);
  301. }
  302. }
  303. //here--------------
  304. foreach($resource_list as $key=>$resource) {
  305. $new_resource_list = $new_resource_list_name = array();
  306. foreach($resource as $name=>$cant) {
  307. $new_resource_list[]=$cant;
  308. $new_resource_list_name[]=$name;
  309. }
  310. //Pie chart
  311. $DataSet = new pData;
  312. $DataSet->AddPoint($new_resource_list,"Serie1");
  313. $DataSet->AddPoint($new_resource_list_name,"Serie2");
  314. $DataSet->AddAllSeries();
  315. $DataSet->SetAbsciseLabelSerie("Serie2");
  316. $Test = new pChart(400,300);
  317. $Test->loadColorPalette(api_get_path(LIBRARY_PATH)."pchart/palette/soft_tones.txt");
  318. // background
  319. //$Test->drawFilledRoundedRectangle(7,7,293,193,5,240,240,240);
  320. // border color
  321. $Test->drawRoundedRectangle(5,5,295,195,5,230,230,230);
  322. // This will draw a shadow under the pie chart
  323. //$Test->drawFilledCircle(122,102,70,200,200,200);
  324. //Draw the pie chart
  325. $Test->setFontProperties(api_get_path(LIBRARY_PATH)."pchart/fonts/tahoma.ttf",8);
  326. $Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription(),TRUE);
  327. $tmp_path = api_get_path(SYS_ARCHIVE_PATH);
  328. $Test->drawBasicPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),120,100,70,PIE_PERCENTAGE,255,255,218);
  329. $Test->drawPieLegend(230,15,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250);
  330. $user_id = api_get_user_id();
  331. $img_file_generated_name = $key.uniqid('').'gradebook.png';
  332. $Test->Render($tmp_path.$img_file_generated_name);
  333. chmod($tmp_path.$img_file_generated_name, api_get_permissions_for_new_files());
  334. if ($i % 2 == 0 && $i!= 0) {
  335. echo '<br>';
  336. }
  337. echo '<img src="'.api_get_path(WEB_ARCHIVE_PATH).$img_file_generated_name.'">';
  338. }
  339. */
  340. }
  341. }
  342. /**
  343. * Function used by SortableTable to get total number of items in the table
  344. */
  345. function get_total_number_of_items() {
  346. return $this->datagen->get_total_users_count();
  347. }
  348. /**
  349. * Function used by SortableTable to generate the data to display
  350. */
  351. function get_table_data ($from = 1, $per_page = null, $column = null, $direction = null, $sort = null) {
  352. $is_western_name_order = api_is_western_name_order();
  353. // create page navigation if needed
  354. $totalitems = $this->datagen->get_total_items_count();
  355. if ($this->limit_enabled && $totalitems > LIMIT) {
  356. $selectlimit = LIMIT;
  357. } else {
  358. $selectlimit = $totalitems;
  359. }
  360. if ($this->limit_enabled && $totalitems > LIMIT) {
  361. $calcprevious = LIMIT;
  362. $header .= '<table style="width: 100%; text-align: right; margin-left: auto; margin-right: auto;" border="0" cellpadding="2">'
  363. .'<tbody>'
  364. .'<tr>';
  365. // previous X
  366. $header .= '<td style="width:100%;">';
  367. if ($this->offset >= LIMIT) {
  368. $header .= '<a href="'.api_get_self()
  369. .'?selectcat='.Security::remove_XSS($_GET['selectcat'])
  370. .'&offset='.(($this->offset)-LIMIT)
  371. .(isset($_GET['search'])?'&search='.Security::remove_XSS($_GET['search']):'').'">'
  372. .Display::return_icon('action_prev.png', get_lang('PreviousPage'), array(), 32)
  373. .'</a>';
  374. } else {
  375. $header .= Display::return_icon('action_prev_na.png', get_lang('PreviousPage'), array(), 32);
  376. }
  377. $header .= ' ';
  378. // next X
  379. $calcnext = (($this->offset+(2*LIMIT)) > $totalitems) ?
  380. ($totalitems-(LIMIT+$this->offset)) : LIMIT;
  381. if ($calcnext > 0) {
  382. $header .= '<a href="'.api_get_self()
  383. .'?selectcat='.Security::remove_XSS($_GET['selectcat'])
  384. .'&offset='.($this->offset+LIMIT)
  385. .(isset($_GET['search'])?'&search='.Security::remove_XSS($_GET['search']):'').'">'
  386. .Display::return_icon('action_next.png', get_lang('NextPage'), array(), 32)
  387. .'</a>';
  388. } else {
  389. $header .= Display::return_icon('action_next_na.png', get_lang('NextPage'), array(), 32);
  390. }
  391. $header .= '</td>';
  392. $header .= '</tbody></table>';
  393. echo $header;
  394. }
  395. // retrieve sorting type
  396. if ($is_western_name_order) {
  397. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME : FlatViewDataGenerator :: FVDG_SORT_LASTNAME);
  398. } else {
  399. $users_sorting = ($this->column == 0 ? FlatViewDataGenerator :: FVDG_SORT_LASTNAME : FlatViewDataGenerator :: FVDG_SORT_FIRSTNAME);
  400. }
  401. if ($this->direction == 'DESC') {
  402. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_DESC;
  403. } else {
  404. $users_sorting |= FlatViewDataGenerator :: FVDG_SORT_ASC;
  405. }
  406. // step 1: generate columns: evaluations and links
  407. $header_names = $this->datagen->get_header_names($this->offset, $selectlimit);
  408. $column = 0;
  409. if ($is_western_name_order) {
  410. $this->set_header($column++, $header_names[1]);
  411. $this->set_header($column++, $header_names[0]);
  412. } else {
  413. $this->set_header($column++, $header_names[0]);
  414. $this->set_header($column++, $header_names[1]);
  415. }
  416. while ($column < count($header_names)) {
  417. $this->set_header($column, $header_names[$column], false);
  418. $column++;
  419. }
  420. $data_array = $this->datagen->get_data($users_sorting, $from, $this->per_page, $this->offset, $selectlimit);
  421. $table_data = array();
  422. foreach ($data_array as $user_row) {
  423. $table_row = array ();
  424. $count = 0;
  425. $user_id = $user_row[$count++];
  426. $lastname = $user_row[$count++];
  427. $firstname = $user_row[$count++];
  428. if ($is_western_name_order) {
  429. $table_row[] = $this->build_name_link($user_id, $firstname);
  430. $table_row[] = $this->build_name_link($user_id, $lastname);
  431. } else {
  432. $table_row[] = $this->build_name_link($user_id, $lastname);
  433. $table_row[] = $this->build_name_link($user_id, $firstname);
  434. }
  435. while ($count < count($user_row)) {
  436. $table_row[] = $user_row[$count++];
  437. }
  438. $table_data[]= $table_row;
  439. }
  440. return $table_data;
  441. }
  442. // Other functions
  443. private function build_name_link ($user_id, $name) {
  444. return '<a href="user_stats.php?userid='.$user_id.'&selectcat='.$this->selectcat->get_id().'">'.$name.'</a>';
  445. }
  446. }