flatview_data_generator.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class to select, sort and transform object data into array data,
  5. * used for the teacher's flat view
  6. * @author Bert Steppé
  7. * @package chamilo.gradebook
  8. */
  9. /**
  10. * Class
  11. * @package chamilo.gradebook
  12. */
  13. class FlatViewDataGenerator
  14. {
  15. // Sorting types constants
  16. const FVDG_SORT_LASTNAME = 1;
  17. const FVDG_SORT_FIRSTNAME = 2;
  18. const FVDG_SORT_ASC = 4;
  19. const FVDG_SORT_DESC = 8;
  20. private $users;
  21. private $evals;
  22. private $links;
  23. private $evals_links;
  24. public $params;
  25. public $category = array();
  26. /**
  27. * Constructor
  28. */
  29. public function FlatViewDataGenerator ($users = array(), $evals = array(), $links = array(), $params = array()) {
  30. $this->users = (isset($users) ? $users : array());
  31. $this->evals = (isset($evals) ? $evals : array());
  32. $this->links = (isset($links) ? $links : array());
  33. $this->evals_links = array_merge($this->evals, $this->links);
  34. $this->params = $params;
  35. }
  36. /**
  37. * Get total number of users (rows)
  38. */
  39. public function get_total_users_count() {
  40. return count($this->users);
  41. }
  42. /**
  43. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  44. */
  45. public function get_total_items_count() {
  46. return count($this->evals_links);
  47. }
  48. /**
  49. * Get array containing column header names (incl user columns)
  50. * @param int Start item offset
  51. * @param int Number of items to get
  52. * @param bool whether to show the details or not
  53. * @return array List of headers
  54. */
  55. public function get_header_names($items_start = 0, $items_count = null , $show_detail = false) {
  56. $headers = array();
  57. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  58. $headers[] = get_lang('OfficialCode');
  59. }
  60. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  61. if (api_is_western_name_order()) {
  62. $headers[] = get_lang('FirstnameAndLastname');
  63. } else {
  64. $headers[] = get_lang('LastnameAndFirstname');
  65. }
  66. } else {
  67. if (api_is_western_name_order()) {
  68. $headers[] = get_lang('FirstName');
  69. $headers[] = get_lang('LastName');
  70. } else {
  71. $headers[] = get_lang('LastName');
  72. $headers[] = get_lang('FirstName');
  73. }
  74. }
  75. if (!isset($items_count)) {
  76. $items_count = count($this->evals_links) - $items_start;
  77. }
  78. $parent_id = $this->category->get_parent_id();
  79. if ($parent_id == 0 or $this->params['only_subcat'] == $this->category->get_id()) {
  80. $main_weight = $this->category->get_weight();
  81. $grade_model_id = $this->category->get_grade_model_id();
  82. } else {
  83. $main_cat = Category::load($parent_id, null, null);
  84. $main_weight = $main_cat[0]->get_weight();
  85. $grade_model_id = $main_cat[0]->get_grade_model_id();
  86. }
  87. $use_grade_model = true;
  88. if (empty($grade_model_id) || $grade_model_id == -1) {
  89. $use_grade_model = false;
  90. }
  91. //@todo move these in a function
  92. $sum_categories_weight_array = array();
  93. if (isset($this->category) && !empty($this->category)) {
  94. $categories = Category::load(null, null, null, $this->category->get_id());
  95. if (!empty($categories)) {
  96. foreach ($categories as $category) {
  97. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  98. }
  99. } else {
  100. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  101. }
  102. }
  103. //No category was added
  104. $course_code = api_get_course_id();
  105. $session_id = api_get_session_id();
  106. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  107. if ($parent_id == 0 && !empty($allcat)) {
  108. //Means there are any subcategory
  109. foreach ($allcat as $sub_cat) {
  110. $sub_cat_weight = round(100*$sub_cat->get_weight()/$main_weight,1);
  111. $add_weight = " $sub_cat_weight %";
  112. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  113. $add_weight = null;
  114. }
  115. $headers[] = Display::url($sub_cat->get_name(), api_get_self().'?selectcat='.$sub_cat->get_id()).$add_weight;
  116. }
  117. } else {
  118. if (!isset($this->params['only_total_category']) || (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)) {
  119. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  120. $item = $this->evals_links[$count + $items_start];
  121. $weight = round(100*$item->get_weight()/$main_weight,1);
  122. $headers[] = $item->get_name().' '.$weight.' % ';
  123. }
  124. }
  125. }
  126. $headers[] = api_strtoupper(get_lang('GradebookQualificationTotal'));
  127. return $headers;
  128. }
  129. function get_max_result_by_link($id) {
  130. $max = 0;
  131. foreach ($this->users as $user) {
  132. $item = $this->evals_links [$id];
  133. $score = $item->calc_score($user[0]);
  134. if ($score[0] > $max) {
  135. $max = $score[0];
  136. }
  137. }
  138. return $max ;
  139. }
  140. /**
  141. * Get array containing evaluation items
  142. */
  143. public function get_evaluation_items($items_start = 0, $items_count = null) {
  144. $headers = array();
  145. if (!isset($items_count)) {
  146. $items_count = count($this->evals_links) - $items_start;
  147. }
  148. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  149. $item = $this->evals_links [$count + $items_start];
  150. $headers[] = $item->get_name();
  151. }
  152. return $headers;
  153. }
  154. /**
  155. * Get actual array data
  156. * @return array 2-dimensional array - each array contains the elements:
  157. * 0: user id
  158. * 1: user lastname
  159. * 2: user firstname
  160. * 3+: evaluation/link scores
  161. */
  162. public function get_data ($users_sorting = 0, $users_start = 0, $users_count = null,
  163. $items_start = 0, $items_count = null,
  164. $ignore_score_color = false, $show_all = false) {
  165. // do some checks on users/items counts, redefine if invalid values
  166. if (!isset($users_count)) {
  167. $users_count = count ($this->users) - $users_start;
  168. }
  169. if ($users_count < 0) {
  170. $users_count = 0;
  171. }
  172. if (!isset($items_count)) {
  173. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  174. }
  175. if ($items_count < 0) {
  176. $items_count = 0;
  177. }
  178. // copy users to a new array that we will sort
  179. // TODO - needed ?
  180. $usertable = array ();
  181. foreach ($this->users as $user) {
  182. $usertable[] = $user;
  183. }
  184. // sort users array
  185. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  186. usort($usertable, array ('FlatViewDataGenerator','sort_by_last_name'));
  187. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  188. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  189. }
  190. if ($users_sorting & self :: FVDG_SORT_DESC) {
  191. $usertable = array_reverse($usertable);
  192. }
  193. // select the requested users
  194. $selected_users = array_slice($usertable, $users_start, $users_count);
  195. // generate actual data array
  196. $scoredisplay = ScoreDisplay :: instance();
  197. $data = array ();
  198. $displaytype = SCORE_DIV;
  199. if ($ignore_score_color) {
  200. $displaytype |= SCORE_IGNORE_SPLIT;
  201. }
  202. //@todo move these in a function
  203. $sum_categories_weight_array = array();
  204. if (isset($this->category) && !empty($this->category)) {
  205. $categories = Category::load(null, null, null, $this->category->get_id());
  206. if (!empty($categories)) {
  207. foreach($categories as $category) {
  208. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  209. }
  210. } else {
  211. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  212. }
  213. }
  214. $parent_id = $this->category->get_parent_id();
  215. if ($parent_id == 0 or $this->params['only_subcat'] == $this->category->get_id()) {
  216. $main_weight = $this->category->get_weight();
  217. $grade_model_id = $this->category->get_grade_model_id();
  218. } else {
  219. $main_cat = Category::load($parent_id, null, null);
  220. $main_weight = $main_cat[0]->get_weight();
  221. $grade_model_id = $main_cat[0]->get_grade_model_id();
  222. }
  223. $use_grade_model = true;
  224. if (empty($grade_model_id) || $grade_model_id == -1) {
  225. $use_grade_model = false;
  226. }
  227. $export_to_pdf = false;
  228. if (isset($this->params['export_pdf']) && $this->params['export_pdf']) {
  229. $export_to_pdf = true;
  230. }
  231. foreach ($selected_users as $user) {
  232. $row = array();
  233. if ($export_to_pdf) {
  234. $row['user_id'] = $user_id = $user[0]; //user id
  235. } else {
  236. $row[] = $user_id = $user[0]; //user id
  237. }
  238. if (isset($this->params['show_official_code']) && $this->params['show_official_code']) {
  239. if ($export_to_pdf) {
  240. $row['official_code'] = $user[4]; //official code
  241. } else {
  242. $row[] = $user[4]; //official code
  243. }
  244. }
  245. if (isset($this->params['join_firstname_lastname']) && $this->params['join_firstname_lastname']) {
  246. if ($export_to_pdf) {
  247. $row['name'] = api_get_person_name($user[3], $user[2]); //last name
  248. } else {
  249. $row[] = api_get_person_name($user[3], $user[2]); //last name
  250. }
  251. } else {
  252. if ($export_to_pdf) {
  253. if (api_is_western_name_order()) {
  254. $row['firstname'] = $user[3];
  255. $row['lastname'] = $user[2];
  256. } else {
  257. $row['lastname'] = $user[2];
  258. $row['firstname'] = $user[3];
  259. }
  260. } else {
  261. if (api_is_western_name_order()) {
  262. $row[] = $user[3]; //first name
  263. $row[] = $user[2]; //last name
  264. } else {
  265. $row[] = $user[2]; //last name
  266. $row[] = $user[3]; //first name
  267. }
  268. }
  269. }
  270. $item_value = 0;
  271. $item_value_total = 0;
  272. $item_total = 0;
  273. $convert_using_the_global_weight = true;
  274. $course_code = api_get_course_id();
  275. $session_id = api_get_session_id();
  276. $allcat = $this->category->get_subcategories(null, $course_code, $session_id, 'ORDER BY id');
  277. if ($parent_id == 0 && !empty($allcat)) {
  278. foreach ($allcat as $sub_cat) {
  279. $score = $sub_cat->calc_score($user_id);
  280. $real_score = $score;
  281. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  282. $sub_cat_percentage = $sum_categories_weight_array[$sub_cat->get_id()];
  283. $item_value = $score[0]/$divide*$main_weight;
  284. //Fixing total when using one or multiple gradebooks
  285. $percentage = $sub_cat->get_weight()/($sub_cat_percentage) * $sub_cat_percentage/$this->category->get_weight();
  286. $item_value = $percentage*$item_value;
  287. $item_total += $sub_cat->get_weight();
  288. /*
  289. if ($convert_using_the_global_weight) {
  290. $score[0] = $score[0]/$main_weight*$sub_cat->get_weight();
  291. $score[1] = $main_weight ;
  292. }
  293. */
  294. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  295. //if (true) {
  296. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  297. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  298. $temp_score = Display::tip($real_score, $temp_score);
  299. } else {
  300. $real_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  301. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_SIMPLE_WITH_CUSTOM);
  302. $temp_score = Display::tip($temp_score, $real_score);
  303. }
  304. if (!isset($this->params['only_total_category']) || (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)) {
  305. if (!$show_all) {
  306. $row[] = $temp_score.' ';
  307. } else {
  308. $row[] = $temp_score;
  309. }
  310. }
  311. $item_value_total +=$item_value;
  312. }
  313. if ($convert_using_the_global_weight) {
  314. //$item_total = $main_weight;
  315. }
  316. } else {
  317. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  318. $item = $this->evals_links[$count + $items_start];
  319. $score = $item->calc_score($user_id);
  320. $real_score = $score;
  321. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  322. //sub cat weight
  323. $sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  324. $item_value = $score[0]/$divide;
  325. //Fixing total when using one or multiple gradebooks
  326. if ($this->category->get_parent_id() == 0 ) {
  327. $item_value = $score[0]/$divide*$item->get_weight();
  328. } else {
  329. $item_value = $item_value*$item->get_weight();
  330. }
  331. $item_total += $item->get_weight();
  332. /*
  333. $complete_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  334. $temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  335. $temp_score = Display::tip($temp_score, $complete_score);*/
  336. //if (true) {
  337. if (api_get_setting('gradebook_show_percentage_in_reports') == 'false') {
  338. $real_score = $scoredisplay->display_score($real_score, SCORE_SIMPLE);
  339. $temp_score = $scoredisplay->display_score(array($item_value, null), SCORE_DIV_SIMPLE_WITH_CUSTOM);
  340. $temp_score = Display::tip($real_score, $temp_score);
  341. } else {
  342. $temp_score = $scoredisplay->display_score($real_score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  343. $temp_score = Display::tip($temp_score, $complete_score);
  344. }
  345. if (!isset($this->params['only_total_category']) || (isset($this->params['only_total_category']) && $this->params['only_total_category'] == false)) {
  346. if (!$show_all) {
  347. if (in_array($item->get_type() , array(LINK_EXERCISE, LINK_DROPBOX, LINK_STUDENTPUBLICATION,
  348. LINK_LEARNPATH, LINK_FORUM_THREAD, LINK_ATTENDANCE,LINK_SURVEY))) {
  349. if (!empty($score[0])) {
  350. $row[] = $temp_score.' ';
  351. } else {
  352. $row[] = '';
  353. }
  354. } else {
  355. $row[] = $temp_score.' ';
  356. }
  357. } else {
  358. $row[] = $temp_score;
  359. }
  360. }
  361. $item_value_total +=$item_value;
  362. }
  363. $item_total = $main_weight;
  364. }
  365. $total_score = array($item_value_total, $item_total);
  366. if (!$show_all) {
  367. if ($export_to_pdf) {
  368. $row['total'] = $scoredisplay->display_score($total_score);
  369. } else {
  370. $row[] = $scoredisplay->display_score($total_score);
  371. }
  372. } else {
  373. if ($export_to_pdf) {
  374. $row['total'] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  375. } else {
  376. $row[] = $scoredisplay->display_score($total_score, SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS);
  377. }
  378. }
  379. unset($score);
  380. //var_dump($row);exit;
  381. $data[] = $row;
  382. }
  383. return $data;
  384. }
  385. /**
  386. * Get actual array data evaluation/link scores
  387. */
  388. public function get_evaluation_sumary_results ($session_id = null) {
  389. $usertable = array ();
  390. foreach ($this->users as $user) { $usertable[] = $user; }
  391. $selected_users = $usertable;
  392. // generate actual data array for all selected users
  393. $data = array();
  394. foreach ($selected_users as $user) {
  395. $row = array ();
  396. for ($count=0;$count < count($this->evals_links); $count++) {
  397. $item = $this->evals_links [$count];
  398. $score = $item->calc_score($user[0]);
  399. $porcent_score = isset($score[1]) && $score[1] > 0 ? ($score[0]*100)/$score[1] :0;
  400. $row[$item->get_name()] = $porcent_score;
  401. }
  402. $data[$user[0]] = $row;
  403. }
  404. // get evaluations for every user by item
  405. $data_by_item = array();
  406. foreach ($data as $uid => $items) {
  407. $tmp = array();
  408. foreach ($items as $item => $value) {
  409. $tmp[] = $item;
  410. if (in_array($item,$tmp)) {
  411. $data_by_item[$item][$uid] = $value;
  412. }
  413. }
  414. }
  415. // get evaluation sumary results (maximum, minimum and average of evaluations for all students)
  416. $result = array();
  417. $maximum = $minimum = $average = 0;
  418. foreach ($data_by_item as $k => $v) {
  419. $average = round(array_sum($v)/count($v));
  420. arsort($v);
  421. $maximum = array_shift($v);
  422. $minimum = array_pop($v);
  423. $sumary_item = array('max'=>$maximum, 'min'=>$minimum, 'avg'=>$average);
  424. $result[$k] = $sumary_item;
  425. }
  426. return $result;
  427. }
  428. public function get_data_to_graph () {
  429. // do some checks on users/items counts, redefine if invalid values
  430. $usertable = array ();
  431. foreach ($this->users as $user) {
  432. $usertable[] = $user;
  433. }
  434. // sort users array
  435. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  436. $data = array ();
  437. $selected_users = $usertable;
  438. foreach ($selected_users as $user) {
  439. $row = array ();
  440. $row[] = $user[0]; // user id
  441. $item_value = 0;
  442. $item_total = 0;
  443. for ($count=0;$count < count($this->evals_links); $count++) {
  444. $item = $this->evals_links[$count];
  445. $score = $item->calc_score($user[0]);
  446. $divide =( ($score[1])==0 ) ? 1 : $score[1];
  447. $item_value += $score[0]/$divide*$item->get_weight();
  448. $item_total += $item->get_weight();
  449. $score_denom = ($score[1]==0) ? 1 : $score[1];
  450. $score_final = ($score[0] / $score_denom) * 100;
  451. $row[] = $score_final;
  452. }
  453. $total_score = array($item_value, $item_total);
  454. $score_final = ($item_value / $item_total) * 100;
  455. $row[] = $score_final;
  456. $data[] = $row;
  457. }
  458. return $data;
  459. }
  460. public function get_data_to_graph2 () {
  461. // do some checks on users/items counts, redefine if invalid values
  462. $usertable = array ();
  463. foreach ($this->users as $user) {
  464. $usertable[] = $user;
  465. }
  466. // sort users array
  467. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  468. // generate actual data array
  469. $scoredisplay = ScoreDisplay :: instance();
  470. $data= array ();
  471. $displaytype = SCORE_DIV;
  472. $selected_users = $usertable;
  473. foreach ($selected_users as $user) {
  474. $row = array ();
  475. $row[] = $user[0]; // user id
  476. $item_value=0;
  477. $item_total=0;
  478. for ($count=0;$count < count($this->evals_links); $count++) {
  479. $item = $this->evals_links [$count];
  480. $score = $item->calc_score($user[0]);
  481. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  482. $item_value+= $score[0]/$divide*$item->get_weight();
  483. $item_total+=$item->get_weight();
  484. $score_denom=($score[1]==0) ? 1 : $score[1];
  485. $score_final = ($score[0] / $score_denom) * 100;
  486. $row[] = array ($score_final, trim($scoredisplay->display_score($score, SCORE_CUSTOM,null, true)));
  487. }
  488. $total_score=array($item_value,$item_total);
  489. $score_final = ($item_value / $item_total) * 100;
  490. $row[] =array ($score_final, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  491. $data[] = $row;
  492. }
  493. return $data;
  494. }
  495. // Sort functions - used internally
  496. function sort_by_last_name($item1, $item2) {
  497. return api_strcmp($item1[2], $item2[2]);
  498. }
  499. function sort_by_first_name($item1, $item2) {
  500. return api_strcmp($item1[3], $item2[3]);
  501. }
  502. }