flatview_data_generator.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. */
  8. class FlatViewDataGenerator
  9. {
  10. // Sorting types constants
  11. const FVDG_SORT_LASTNAME = 1;
  12. const FVDG_SORT_FIRSTNAME = 2;
  13. const FVDG_SORT_ASC = 4;
  14. const FVDG_SORT_DESC = 8;
  15. private $users;
  16. private $evals;
  17. private $links;
  18. private $evals_links;
  19. /**
  20. * Constructor
  21. */
  22. public function FlatViewDataGenerator ($users= array (), $evals= array (), $links= array ()) {
  23. $this->users = (isset($users) ? $users : array());
  24. $this->evals = (isset($evals) ? $evals : array());
  25. $this->links = (isset($links) ? $links : array());
  26. $this->evals_links = array_merge($this->evals, $this->links);
  27. }
  28. /**
  29. * Get total number of users (rows)
  30. */
  31. public function get_total_users_count() {
  32. return count($this->users);
  33. }
  34. /**
  35. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  36. */
  37. public function get_total_items_count() {
  38. return count($this->evals_links);
  39. }
  40. /**
  41. * Get array containing column header names (incl user columns)
  42. */
  43. public function get_header_names ($items_start = 0, $items_count = null) {
  44. $headers = array();
  45. $headers[] = get_lang('LastName');
  46. $headers[] = get_lang('FirstName');
  47. if (!isset($items_count)) {
  48. $items_count = count($this->evals_links) - $items_start;
  49. }
  50. for ($count=0;
  51. ($count < $items_count ) && ($items_start + $count < count($this->evals_links));
  52. $count++) {
  53. $item = $this->evals_links [$count + $items_start];
  54. $headers[] = $item->get_name();
  55. }
  56. $headers[] = get_lang('GradebookQualificationTotal');
  57. return $headers;
  58. }
  59. /**
  60. * Get array containing evaluation items
  61. */
  62. public function get_evaluation_items($items_start = 0, $items_count = null) {
  63. $headers = array();
  64. if (!isset($items_count)) {
  65. $items_count = count($this->evals_links) - $items_start;
  66. }
  67. for ($count=0;
  68. ($count < $items_count ) && ($items_start + $count < count($this->evals_links));
  69. $count++) {
  70. $item = $this->evals_links [$count + $items_start];
  71. $headers[] = $item->get_name();
  72. }
  73. return $headers;
  74. }
  75. /**
  76. * Get actual array data
  77. * @return array 2-dimensional array - each array contains the elements:
  78. * 0: user id
  79. * 1: user lastname
  80. * 2: user firstname
  81. * 3+: evaluation/link scores
  82. */
  83. public function get_data ($users_sorting = 0,
  84. $users_start = 0, $users_count = null,
  85. $items_start = 0, $items_count = null,
  86. $ignore_score_color = false) {
  87. // do some checks on users/items counts, redefine if invalid values
  88. if (!isset($users_count)) {
  89. $users_count = count ($this->users) - $users_start;
  90. }
  91. if ($users_count < 0) {
  92. $users_count = 0;
  93. }
  94. if (!isset($items_count)) {
  95. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  96. }
  97. if ($items_count < 0) {
  98. $items_count = 0;
  99. }
  100. // copy users to a new array that we will sort
  101. // TODO - needed ?
  102. $usertable = array ();
  103. foreach ($this->users as $user) {
  104. $usertable[] = $user;
  105. }
  106. // sort users array
  107. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  108. usort($usertable, array ('FlatViewDataGenerator','sort_by_last_name'));
  109. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  110. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  111. }
  112. if ($users_sorting & self :: FVDG_SORT_DESC) {
  113. $usertable = array_reverse($usertable);
  114. }
  115. // select the requested users
  116. $selected_users = array_slice($usertable, $users_start, $users_count);
  117. // generate actual data array
  118. $scoredisplay = ScoreDisplay :: instance();
  119. $data= array ();
  120. $displaytype = SCORE_DIV;
  121. if ($ignore_score_color) {
  122. $displaytype |= SCORE_IGNORE_SPLIT;
  123. }
  124. foreach ($selected_users as $user) {
  125. $row = array ();
  126. $row[] = $user[0]; // user id
  127. $row[] = $user[1]; // last name
  128. $row[] = $user[2]; // first name
  129. $item_value=0;
  130. $item_total=0;
  131. for ($count=0;
  132. ($count < $items_count ) && ($items_start + $count < count($this->evals_links));
  133. $count++) {
  134. $item = $this->evals_links [$count + $items_start];
  135. $score = $item->calc_score($user[0]);
  136. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  137. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  138. $item_total+=$item->get_weight();
  139. $row[] = $scoredisplay->display_score($score,SCORE_DIV_PERCENT);
  140. }
  141. $total_score=array($item_value,$item_total);
  142. $row[] = $scoredisplay->display_score($total_score,SCORE_DIV_PERCENT);
  143. unset($score);
  144. $data[] = $row;
  145. }
  146. return $data;
  147. }
  148. /**
  149. * Get actual array data evaluation/link scores
  150. */
  151. public function get_evaluation_sumary_results ($session_id = null) {
  152. $usertable = array ();
  153. foreach ($this->users as $user) { $usertable[] = $user; }
  154. $selected_users = $usertable;
  155. // generate actual data array for all selected users
  156. $data = array();
  157. foreach ($selected_users as $user) {
  158. $row = array ();
  159. $item_value=0;
  160. $item_total=0;
  161. for ($count=0;$count < count($this->evals_links); $count++) {
  162. $item = $this->evals_links [$count];
  163. $score = $item->calc_score($user[0]);
  164. $porcent_score = $score[1] ?round(($score[0]*100)/$score[1]):0;
  165. $row[$item->get_name()] = $porcent_score;
  166. }
  167. $data[$user[0]] = $row;
  168. }
  169. // get evaluations for every user by item
  170. $data_by_item = array();
  171. foreach ($data as $uid => $items) {
  172. $tmp = array();
  173. foreach ($items as $item => $value) {
  174. $tmp[] = $item;
  175. if (in_array($item,$tmp)) {
  176. $data_by_item[$item][$uid] = $value;
  177. }
  178. }
  179. }
  180. // get evaluation sumary results (maximum, minimum and average of evaluations for all students)
  181. $result = array();
  182. $maximum = $minimum = $average = 0;
  183. foreach ($data_by_item as $k => $v) {
  184. $average = round(array_sum($v)/count($v));
  185. arsort($v);
  186. $maximum = array_shift($v);
  187. $minimum = array_pop($v);
  188. $sumary_item = array('max'=>$maximum, 'min'=>$minimum, 'avg'=>$average);
  189. $result[$k] = $sumary_item;
  190. }
  191. return $result;
  192. }
  193. public function get_data_to_graph () {
  194. // do some checks on users/items counts, redefine if invalid values
  195. $usertable = array ();
  196. foreach ($this->users as $user) {
  197. $usertable[] = $user;
  198. }
  199. // sort users array
  200. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  201. // generate actual data array
  202. $scoredisplay = ScoreDisplay :: instance();
  203. $data= array ();
  204. $displaytype = SCORE_DIV;
  205. $selected_users = $usertable;
  206. foreach ($selected_users as $user) {
  207. $row = array ();
  208. $row[] = $user[0]; // user id
  209. $item_value=0;
  210. $item_total=0;
  211. for ($count=0;$count < count($this->evals_links); $count++) {
  212. $item = $this->evals_links [$count];
  213. $score = $item->calc_score($user[0]);
  214. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  215. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  216. $item_total+=$item->get_weight();
  217. $score_denom=($score[1]==0) ? 1 : $score[1];
  218. $score_final = round(($score[0] / $score_denom) * 100,2);
  219. $row[] = $score_final;
  220. }
  221. $total_score=array($item_value,$item_total);
  222. $score_final = round(($item_value / $item_total) * 100,2);
  223. $row[] = $score_final;
  224. $data[] = $row;
  225. }
  226. return $data;
  227. }
  228. public function get_data_to_graph2 () {
  229. // do some checks on users/items counts, redefine if invalid values
  230. $usertable = array ();
  231. foreach ($this->users as $user) {
  232. $usertable[] = $user;
  233. }
  234. // sort users array
  235. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  236. // generate actual data array
  237. $scoredisplay = ScoreDisplay :: instance();
  238. $data= array ();
  239. $displaytype = SCORE_DIV;
  240. $selected_users = $usertable;
  241. foreach ($selected_users as $user) {
  242. $row = array ();
  243. $row[] = $user[0]; // user id
  244. $item_value=0;
  245. $item_total=0;
  246. for ($count=0;$count < count($this->evals_links); $count++) {
  247. $item = $this->evals_links [$count];
  248. $score = $item->calc_score($user[0]);
  249. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  250. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  251. $item_total+=$item->get_weight();
  252. $score_denom=($score[1]==0) ? 1 : $score[1];
  253. $score_final = round(($score[0] / $score_denom) * 100,2);
  254. $row[] = array ($score_final, strip_tags($scoredisplay->display_score($score,SCORE_DIV_PERCENT, SCORE_ONLY_CUSTOM)));
  255. }
  256. $total_score=array($item_value,$item_total);
  257. $score_final = round(($item_value / $item_total) * 100,2);
  258. $row[] =array ($score_final, strip_tags($scoredisplay->display_score($total_score,SCORE_DIV_PERCENT, SCORE_ONLY_CUSTOM)));
  259. //$total_score=array($item_value,$item_total);
  260. //$score_final = round(($item_value / $item_total) * 100,2);
  261. //$row[] = $score_final;
  262. //var_dump($score_final);
  263. //var_dump($row);
  264. $data[] = $row;
  265. }
  266. return $data;
  267. }
  268. // Sort functions - used internally
  269. function sort_by_last_name($item1, $item2) {
  270. return api_strcmp($item1[1], $item2[1]);
  271. }
  272. function sort_by_first_name($item1, $item2) {
  273. return api_strcmp($item1[2], $item2[2]);
  274. }
  275. }