flatviewtable.class.php 17 KB

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