flatview_data_generator.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos Latinoamerica SAC
  6. Copyright (c) 2006 Dokeos SPRL
  7. Copyright (c) 2006 Ghent University (UGent)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * Class to select, sort and transform object data into array data,
  22. * used for the teacher's flat view
  23. * @author Bert Stepp�
  24. */
  25. class FlatViewDataGenerator
  26. {
  27. // Sorting types constants
  28. const FVDG_SORT_LASTNAME = 1;
  29. const FVDG_SORT_FIRSTNAME = 2;
  30. const FVDG_SORT_ASC = 4;
  31. const FVDG_SORT_DESC = 8;
  32. private $users;
  33. private $evals;
  34. private $links;
  35. private $evals_links;
  36. /**
  37. * Constructor
  38. */
  39. public function FlatViewDataGenerator ($users= array (), $evals= array (), $links= array ()) {
  40. $this->users = (isset($users) ? $users : array());
  41. $this->evals = (isset($evals) ? $evals : array());
  42. $this->links = (isset($links) ? $links : array());
  43. $this->evals_links = array_merge($this->evals, $this->links);
  44. }
  45. /**
  46. * Get total number of users (rows)
  47. */
  48. public function get_total_users_count() {
  49. return count($this->users);
  50. }
  51. /**
  52. * Get total number of evaluations/links (columns) (the 2 users columns not included)
  53. */
  54. public function get_total_items_count() {
  55. return count($this->evals_links);
  56. }
  57. /**
  58. * Get array containing column header names (incl user columns)
  59. */
  60. public function get_header_names ($items_start = 0, $items_count = null) {
  61. $headers = array();
  62. $headers[] = get_lang('LastName');
  63. $headers[] = get_lang('FirstName');
  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. $headers[] = get_lang('GradebookQualificationTotal');
  74. return $headers;
  75. }
  76. /**
  77. * Get actual array data
  78. * @return array 2-dimensional array - each array contains the elements:
  79. * 0: user id
  80. * 1: user lastname
  81. * 2: user firstname
  82. * 3+: evaluation/link scores
  83. */
  84. public function get_data ($users_sorting = 0,
  85. $users_start = 0, $users_count = null,
  86. $items_start = 0, $items_count = null,
  87. $ignore_score_color = false) {
  88. // do some checks on users/items counts, redefine if invalid values
  89. if (!isset($users_count)) {
  90. $users_count = count ($this->users) - $users_start;
  91. }
  92. if ($users_count < 0) {
  93. $users_count = 0;
  94. }
  95. if (!isset($items_count)) {
  96. $items_count = count ($this->evals) + count ($this->links) - $items_start;
  97. }
  98. if ($items_count < 0) {
  99. $items_count = 0;
  100. }
  101. // copy users to a new array that we will sort
  102. // TODO - needed ?
  103. $usertable = array ();
  104. foreach ($this->users as $user) {
  105. $usertable[] = $user;
  106. }
  107. // sort users array
  108. if ($users_sorting & self :: FVDG_SORT_LASTNAME) {
  109. usort($usertable, array ('FlatViewDataGenerator','sort_by_last_name'));
  110. } elseif ($users_sorting & self :: FVDG_SORT_FIRSTNAME) {
  111. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  112. }
  113. if ($users_sorting & self :: FVDG_SORT_DESC) {
  114. $usertable = array_reverse($usertable);
  115. }
  116. // select the requested users
  117. $selected_users = array_slice($usertable, $users_start, $users_count);
  118. // generate actual data array
  119. $scoredisplay = ScoreDisplay :: instance();
  120. $data= array ();
  121. $displaytype = SCORE_DIV;
  122. if ($ignore_score_color) {
  123. $displaytype |= SCORE_IGNORE_SPLIT;
  124. }
  125. foreach ($selected_users as $user) {
  126. $row = array ();
  127. $row[] = $user[0]; // user id
  128. $row[] = $user[1]; // last name
  129. $row[] = $user[2]; // first name
  130. $item_value=0;
  131. $item_total=0;
  132. for ($count=0;
  133. ($count < $items_count ) && ($items_start + $count < count($this->evals_links));
  134. $count++) {
  135. $item = $this->evals_links [$count + $items_start];
  136. $score = $item->calc_score($user[0]);
  137. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  138. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  139. $item_total+=$item->get_weight();
  140. $row[] = $scoredisplay->display_score($score,SCORE_DIV_PERCENT);
  141. }
  142. $total_score=array($item_value,$item_total);
  143. $row[] = $scoredisplay->display_score($total_score,SCORE_DIV_PERCENT);
  144. unset($score);
  145. $data[] = $row;
  146. }
  147. return $data;
  148. }
  149. public function get_data_to_graph () {
  150. // do some checks on users/items counts, redefine if invalid values
  151. $usertable = array ();
  152. foreach ($this->users as $user) {
  153. $usertable[] = $user;
  154. }
  155. // sort users array
  156. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  157. // generate actual data array
  158. $scoredisplay = ScoreDisplay :: instance();
  159. $data= array ();
  160. $displaytype = SCORE_DIV;
  161. $selected_users = $usertable;
  162. foreach ($selected_users as $user) {
  163. $row = array ();
  164. $row[] = $user[0]; // user id
  165. $item_value=0;
  166. $item_total=0;
  167. for ($count=0;$count < count($this->evals_links); $count++) {
  168. $item = $this->evals_links [$count];
  169. $score = $item->calc_score($user[0]);
  170. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  171. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  172. $item_total+=$item->get_weight();
  173. $score_denom=($score[1]==0) ? 1 : $score[1];
  174. $score_final = round(($score[0] / $score_denom) * 100,2);
  175. $row[] = $score_final;
  176. }
  177. $total_score=array($item_value,$item_total);
  178. $score_final = round(($item_value / $item_total) * 100,2);
  179. $row[] = $score_final;
  180. $data[] = $row;
  181. }
  182. return $data;
  183. }
  184. public function get_data_to_graph2 () {
  185. // do some checks on users/items counts, redefine if invalid values
  186. $usertable = array ();
  187. foreach ($this->users as $user) {
  188. $usertable[] = $user;
  189. }
  190. // sort users array
  191. usort($usertable, array ('FlatViewDataGenerator','sort_by_first_name'));
  192. // generate actual data array
  193. $scoredisplay = ScoreDisplay :: instance();
  194. $data= array ();
  195. $displaytype = SCORE_DIV;
  196. $selected_users = $usertable;
  197. foreach ($selected_users as $user) {
  198. $row = array ();
  199. $row[] = $user[0]; // user id
  200. $item_value=0;
  201. $item_total=0;
  202. for ($count=0;$count < count($this->evals_links); $count++) {
  203. $item = $this->evals_links [$count];
  204. $score = $item->calc_score($user[0]);
  205. $divide=( ($score[1])==0 ) ? 1 : $score[1];
  206. $item_value+=round($score[0]/$divide*$item->get_weight(),2);
  207. $item_total+=$item->get_weight();
  208. $score_denom=($score[1]==0) ? 1 : $score[1];
  209. $score_final = round(($score[0] / $score_denom) * 100,2);
  210. $row[] = array ($score_final, strip_tags($scoredisplay->display_score($score,SCORE_DIV_PERCENT, SCORE_ONLY_CUSTOM)));
  211. }
  212. $total_score=array($item_value,$item_total);
  213. $score_final = round(($item_value / $item_total) * 100,2);
  214. $row[] =array ($score_final, strip_tags($scoredisplay->display_score($total_score,SCORE_DIV_PERCENT, SCORE_ONLY_CUSTOM)));
  215. //$total_score=array($item_value,$item_total);
  216. //$score_final = round(($item_value / $item_total) * 100,2);
  217. //$row[] = $score_final;
  218. //var_dump($score_final);
  219. //var_dump($row);
  220. $data[] = $row;
  221. }
  222. return $data;
  223. }
  224. // Sort functions - used internally
  225. function sort_by_last_name($item1, $item2) {
  226. if (api_strtolower($item1[1]) == api_strtolower($item2[1])) {
  227. return 0;
  228. } else {
  229. return (api_strtolower($item1[1]) < api_strtolower($item2[1]) ? -1 : 1);
  230. }
  231. }
  232. function sort_by_first_name($item1, $item2)
  233. {
  234. if (api_strtolower($item1[2]) == api_strtolower($item2[2])) {
  235. return $this->sort_by_last_name($item1, $item2);
  236. } else {
  237. return (api_strtolower($item1[2]) < api_strtolower($item2[2]) ? -1 : 1);
  238. }
  239. }
  240. }