flatview_data_generator.class.php 21 KB

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