flatview_data_generator.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 $category = array();
  25. /**
  26. * Constructor
  27. */
  28. public function FlatViewDataGenerator ($users= array (), $evals= array (), $links= array ()) {
  29. $this->users = (isset($users) ? $users : array());
  30. $this->evals = (isset($evals) ? $evals : array());
  31. $this->links = (isset($links) ? $links : array());
  32. $this->evals_links = array_merge($this->evals, $this->links);
  33. }
  34. /**
  35. * Get total number of users (rows)
  36. */
  37. public function get_total_users_count() {
  38. return count($this->users);
  39. }
  40. /**
  41. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  42. */
  43. public function get_total_items_count() {
  44. return count($this->evals_links);
  45. }
  46. /**
  47. * Get array containing column header names (incl user columns)
  48. */
  49. public function get_header_names ($items_start = 0, $items_count = null , $show_detail = false) {
  50. $headers = array();
  51. $headers[] = get_lang('LastName');
  52. $headers[] = get_lang('FirstName');
  53. if (!isset($items_count)) {
  54. $items_count = count($this->evals_links) - $items_start;
  55. }
  56. //@todo move these in a function
  57. $sum_categories_weight_array = array();
  58. if (isset($this->category) && !empty($this->category)) {
  59. $categories = Category::load(null, null, null, $this->category->get_id());
  60. if (!empty($categories)) {
  61. foreach($categories as $category) {
  62. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  63. }
  64. } else {
  65. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  66. }
  67. }
  68. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  69. $item = $this->evals_links[$count + $items_start];
  70. //$headers[] = $item->get_name().' <br /> '.get_lang('Max').' '.$this->get_max_result_by_link($count + $items_start).' ';
  71. $sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  72. $weight = round($item->get_weight()/($sub_cat_percentage) * $sub_cat_percentage/$this->category->get_weight() *100, 2);
  73. $headers[] = $item->get_name().' '.$weight.' % ';
  74. if ($show_detail) {
  75. //$headers[] = $item->get_name().' ('.get_lang('Detail').')';
  76. }
  77. }
  78. $headers[] = get_lang('GradebookQualificationTotal').' 100%';
  79. if ($show_detail) {
  80. //$headers[] = get_lang('GradebookQualificationTotal').' ('.get_lang('Detail').')';
  81. }
  82. return $headers;
  83. }
  84. function get_max_result_by_link($id) {
  85. $usertable = array ();
  86. $items_count = count ($this->evals) + count ($this->links);
  87. $item_value = 0;
  88. $item_total = 0;
  89. $max = 0;
  90. foreach ($this->users as $user) {
  91. $item = $this->evals_links [$id];
  92. $score = $item->calc_score($user[0]);
  93. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  94. //$item_value = round($score[0]/$divide*$item->get_weight(),2);
  95. if ($score[0] > $max) {
  96. $max = $score[0];
  97. }
  98. }
  99. return $max ;
  100. }
  101. /**
  102. * Get array containing evaluation items
  103. */
  104. public function get_evaluation_items($items_start = 0, $items_count = null) {
  105. $headers = array();
  106. if (!isset($items_count)) {
  107. $items_count = count($this->evals_links) - $items_start;
  108. }
  109. for ($count=0;
  110. ($count < $items_count ) && ($items_start + $count < count($this->evals_links));
  111. $count++) {
  112. $item = $this->evals_links [$count + $items_start];
  113. $headers[] = $item->get_name();
  114. }
  115. return $headers;
  116. }
  117. /**
  118. * Get actual array data
  119. * @return array 2-dimensional array - each array contains the elements:
  120. * 0: user id
  121. * 1: user lastname
  122. * 2: user firstname
  123. * 3+: evaluation/link scores
  124. */
  125. public function get_data ($users_sorting = 0, $users_start = 0, $users_count = null,
  126. $items_start = 0, $items_count = null,
  127. $ignore_score_color = false, $show_all = false) {
  128. // do some checks on users/items counts, redefine if invalid values
  129. if (!isset($users_count)) {
  130. $users_count = count ($this->users) - $users_start;
  131. }
  132. if ($users_count < 0) {
  133. $users_count = 0;
  134. }
  135. if (!isset($items_count)) {
  136. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  137. }
  138. if ($items_count < 0) {
  139. $items_count = 0;
  140. }
  141. // copy users to a new array that we will sort
  142. // TODO - needed ?
  143. $usertable = array ();
  144. foreach ($this->users as $user) {
  145. $usertable[] = $user;
  146. }
  147. // sort users array
  148. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  149. usort($usertable, array ('FlatViewDataGenerator','sort_by_last_name'));
  150. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  151. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  152. }
  153. if ($users_sorting & self :: FVDG_SORT_DESC) {
  154. $usertable = array_reverse($usertable);
  155. }
  156. // select the requested users
  157. $selected_users = array_slice($usertable, $users_start, $users_count);
  158. // generate actual data array
  159. $scoredisplay = ScoreDisplay :: instance();
  160. $data = array ();
  161. $displaytype = SCORE_DIV;
  162. if ($ignore_score_color) {
  163. $displaytype |= SCORE_IGNORE_SPLIT;
  164. }
  165. //@todo move these in a function
  166. $sum_categories_weight_array = array();
  167. if (isset($this->category) && !empty($this->category)) {
  168. $categories = Category::load(null, null, null, $this->category->get_id());
  169. if (!empty($categories)) {
  170. foreach($categories as $category) {
  171. $sum_categories_weight_array[$category->get_id()] = $category->get_weight();
  172. }
  173. } else {
  174. $sum_categories_weight_array[$this->category->get_id()] = $this->category->get_weight();
  175. }
  176. }
  177. foreach ($selected_users as $user) {
  178. $row = array ();
  179. $row[] = $user[0]; // user id
  180. $row[] = $user[2]; // last name
  181. $row[] = $user[3]; // first name
  182. $item_value = 0;
  183. $item_value_total = 0;
  184. $item_total = 0;
  185. for ($count=0; ($count < $items_count ) && ($items_start + $count < count($this->evals_links)); $count++) {
  186. $item = $this->evals_links[$count + $items_start];
  187. $score = $item->calc_score($user[0]);
  188. $divide = ( ($score[1])==0 ) ? 1 : $score[1];
  189. $sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
  190. $item_value = round($score[0]/$divide,2) * 100;
  191. $percentage = round($item->get_weight()/($sub_cat_percentage) * $sub_cat_percentage/$this->category->get_weight(), 2);
  192. $item_value = $percentage*$item_value;
  193. $item_total += $item->get_weight();
  194. $temp_score = $scoredisplay->display_score($score, SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  195. //$temp_score = $temp_score . ' '.$item_value.' / '.$this->category->get_weight();
  196. $temp_score = $temp_score;
  197. if (!$show_all) {
  198. //$row[] = $scoredisplay->display_score($score,SCORE_DIV_PERCENT);
  199. if (in_array($item->get_type() , array(LINK_EXERCISE, LINK_DROPBOX, LINK_STUDENTPUBLICATION,
  200. LINK_LEARNPATH, LINK_FORUM_THREAD, LINK_ATTENDANCE,LINK_SURVEY))) {
  201. if (!empty($score[0])) {
  202. $row[] = $temp_score.' ';
  203. } else {
  204. $row[] = '';
  205. }
  206. //$row[] = $scoredisplay->display_score($score,SCORE_DIV_PERCENT, SCORE_ONLY_SCORE);
  207. } else {
  208. //$row[] = $scoredisplay->display_score($score,SCORE_DIV_PERCENT);
  209. //$row[] = $score[0];
  210. $row[] = $temp_score.' ';
  211. }
  212. } else {
  213. //$row[] = $scoredisplay->display_score($score, SCORE_DECIMAL);
  214. $row[] = $temp_score;
  215. //$row[] = $scoredisplay->display_score($score, SCORE_DIV_PERCENT);
  216. }
  217. $item_value_total +=$item_value;
  218. }
  219. $total_score = array($item_value_total, $item_total);
  220. if (!$show_all) {
  221. $row[] = $scoredisplay->display_score($total_score);
  222. } else {
  223. $row[] = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  224. //$row[] = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
  225. }
  226. unset($score);
  227. $data[] = $row;
  228. }
  229. return $data;
  230. }
  231. /**
  232. * Get actual array data evaluation/link scores
  233. */
  234. public function get_evaluation_sumary_results ($session_id = null) {
  235. $usertable = array ();
  236. foreach ($this->users as $user) { $usertable[] = $user; }
  237. $selected_users = $usertable;
  238. // generate actual data array for all selected users
  239. $data = array();
  240. foreach ($selected_users as $user) {
  241. $row = array ();
  242. $item_value=0;
  243. $item_total=0;
  244. for ($count=0;$count < count($this->evals_links); $count++) {
  245. $item = $this->evals_links [$count];
  246. $score = $item->calc_score($user[0]);
  247. $porcent_score = isset($score[1]) && $score[1] > 0 ? round(($score[0]*100)/$score[1]):0;
  248. $row[$item->get_name()] = $porcent_score;
  249. }
  250. $data[$user[0]] = $row;
  251. }
  252. // get evaluations for every user by item
  253. $data_by_item = array();
  254. foreach ($data as $uid => $items) {
  255. $tmp = array();
  256. foreach ($items as $item => $value) {
  257. $tmp[] = $item;
  258. if (in_array($item,$tmp)) {
  259. $data_by_item[$item][$uid] = $value;
  260. }
  261. }
  262. }
  263. // get evaluation sumary results (maximum, minimum and average of evaluations for all students)
  264. $result = array();
  265. $maximum = $minimum = $average = 0;
  266. foreach ($data_by_item as $k => $v) {
  267. $average = round(array_sum($v)/count($v));
  268. arsort($v);
  269. $maximum = array_shift($v);
  270. $minimum = array_pop($v);
  271. $sumary_item = array('max'=>$maximum, 'min'=>$minimum, 'avg'=>$average);
  272. $result[$k] = $sumary_item;
  273. }
  274. return $result;
  275. }
  276. public function get_data_to_graph () {
  277. // do some checks on users/items counts, redefine if invalid values
  278. $usertable = array ();
  279. foreach ($this->users as $user) {
  280. $usertable[] = $user;
  281. }
  282. // sort users array
  283. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  284. $data = array ();
  285. $selected_users = $usertable;
  286. foreach ($selected_users as $user) {
  287. $row = array ();
  288. $row[] = $user[0]; // user id
  289. $item_value = 0;
  290. $item_total = 0;
  291. for ($count=0;$count < count($this->evals_links); $count++) {
  292. $item = $this->evals_links[$count];
  293. $score = $item->calc_score($user[0]);
  294. $divide =( ($score[1])==0 ) ? 1 : $score[1];
  295. $item_value += round($score[0]/$divide*$item->get_weight(),2);
  296. $item_total += $item->get_weight();
  297. $score_denom = ($score[1]==0) ? 1 : $score[1];
  298. $score_final = round(($score[0] / $score_denom) * 100,2);
  299. $row[] = $score_final;
  300. }
  301. $total_score = array($item_value, $item_total);
  302. $score_final = round(($item_value / $item_total) * 100,2);
  303. $row[] = $score_final;
  304. $data[] = $row;
  305. }
  306. return $data;
  307. }
  308. public function get_data_to_graph2 () {
  309. // do some checks on users/items counts, redefine if invalid values
  310. $usertable = array ();
  311. foreach ($this->users as $user) {
  312. $usertable[] = $user;
  313. }
  314. // sort users array
  315. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  316. // generate actual data array
  317. $scoredisplay = ScoreDisplay :: instance();
  318. $data= array ();
  319. $displaytype = SCORE_DIV;
  320. $selected_users = $usertable;
  321. foreach ($selected_users as $user) {
  322. $row = array ();
  323. $row[] = $user[0]; // user id
  324. $item_value=0;
  325. $item_total=0;
  326. for ($count=0;$count < count($this->evals_links); $count++) {
  327. $item = $this->evals_links [$count];
  328. $score = $item->calc_score($user[0]);
  329. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  330. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  331. $item_total+=$item->get_weight();
  332. $score_denom=($score[1]==0) ? 1 : $score[1];
  333. $score_final = round(($score[0] / $score_denom) * 100,2);
  334. $row[] = array ($score_final, trim($scoredisplay->display_score($score, SCORE_CUSTOM,null, true)));
  335. }
  336. $total_score=array($item_value,$item_total);
  337. $score_final = round(($item_value / $item_total) * 100,2);
  338. $row[] =array ($score_final, trim($scoredisplay->display_score($total_score, SCORE_CUSTOM, null, true)));
  339. $data[] = $row;
  340. }
  341. return $data;
  342. }
  343. // Sort functions - used internally
  344. function sort_by_last_name($item1, $item2) {
  345. return api_strcmp($item1[2], $item2[2]);
  346. }
  347. function sort_by_first_name($item1, $item2) {
  348. return api_strcmp($item1[3], $item2[3]);
  349. }
  350. }