user_data_generator.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * Class to select, sort and transform object data into array data,
  4. * used for a student's general view
  5. * @author Bert Stepp�
  6. */
  7. class UserDataGenerator
  8. {
  9. // Sorting types constants
  10. const UDG_SORT_TYPE = 1;
  11. const UDG_SORT_NAME = 2;
  12. const UDG_SORT_COURSE = 4;
  13. const UDG_SORT_CATEGORY = 8;
  14. const UDG_SORT_AVERAGE = 16;
  15. const UDG_SORT_SCORE = 32;
  16. const UDG_SORT_MASK = 64;
  17. const UDG_SORT_ASC = 128;
  18. const UDG_SORT_DESC = 256;
  19. private $items;
  20. private $userid;
  21. private $coursecodecache;
  22. private $categorycache;
  23. private $scorecache;
  24. private $avgcache;
  25. function UserDataGenerator($userid, $evals = array(), $links = array()) {
  26. $this->userid = $userid;
  27. $evals_filtered = array();
  28. foreach ($evals as $eval) {
  29. $toadd = true;
  30. $coursecode = $eval->get_course_code();
  31. if (isset($coursecode)) {
  32. $result = Result :: load (null, $userid, $eval->get_id());
  33. if (count($result) == 0) {
  34. $toadd = false;
  35. }
  36. }
  37. if ($toadd) {
  38. $evals_filtered_copy = $evals;
  39. }
  40. }//isset($coursecode) && strcmp($coursecode,api_get_course_id())===0
  41. if (count($result) == 0) {
  42. $evals_filtered=$evals;
  43. } else {
  44. $evals_filtered=$evals_filtered_copy;
  45. }
  46. $this->items = array_merge ($evals_filtered, $links);
  47. $this->coursecodecache = array();
  48. $this->categorycache = array();
  49. $this->scorecache = null;
  50. $this->avgcache = null;
  51. }
  52. /**
  53. * Get total number of items (rows)
  54. */
  55. public function get_total_items_count() {
  56. return count($this->items);
  57. }
  58. /**
  59. * Get actual array data
  60. * @return array 2-dimensional array - each array contains the elements:
  61. * 0: eval/link object
  62. * 1: item name
  63. * 2: course name
  64. * 3: category name
  65. * 4: average score
  66. * 5: student's score
  67. * 6: student's score as custom display (only if custom scoring enabled)
  68. */
  69. public function get_data ($sorting = 0, $start = 0, $count = null, $ignore_score_color = false) {
  70. // do some checks on count, redefine if invalid value
  71. if (!isset($count)) {
  72. $count = count ($this->items) - $start;
  73. }
  74. if ($count < 0) {
  75. $count = 0;
  76. }
  77. $allitems = $this->items;
  78. // sort users array
  79. if ($sorting & self :: UDG_SORT_TYPE) {
  80. usort($allitems, array('UserDataGenerator', 'sort_by_type'));
  81. }elseif ($sorting & self :: UDG_SORT_NAME) {
  82. usort($allitems, array('UserDataGenerator', 'sort_by_name'));
  83. } elseif ($sorting & self :: UDG_SORT_COURSE) {
  84. usort($allitems, array('UserDataGenerator', 'sort_by_course'));
  85. } elseif ($sorting & self :: UDG_SORT_CATEGORY) {
  86. usort($allitems, array('UserDataGenerator', 'sort_by_category'));
  87. } elseif ($sorting & self :: UDG_SORT_AVERAGE) {
  88. // if user sorts on average scores, first calculate them and cache them
  89. foreach ($allitems as $item) {
  90. $this->avgcache[$item->get_item_type() . $item->get_id()]
  91. = $item->calc_score();
  92. }
  93. usort($allitems, array('UserDataGenerator', 'sort_by_average'));
  94. } elseif ($sorting & self :: UDG_SORT_SCORE) {
  95. // if user sorts on student's scores, first calculate them and cache them
  96. foreach ($allitems as $item) {
  97. $this->scorecache[$item->get_item_type() . $item->get_id()]
  98. = $item->calc_score($this->userid);
  99. }
  100. usort($allitems, array('UserDataGenerator', 'sort_by_score'));
  101. } elseif ($sorting & self :: UDG_SORT_MASK) {
  102. // if user sorts on student's masks, first calculate scores and cache them
  103. foreach ($allitems as $item) {
  104. $this->scorecache[$item->get_item_type() . $item->get_id()]
  105. = $item->calc_score($this->userid);
  106. }
  107. usort($allitems, array('UserDataGenerator', 'sort_by_mask'));
  108. }
  109. if ($sorting & self :: UDG_SORT_DESC) {
  110. $allitems = array_reverse($allitems);
  111. }
  112. // select the items we have to display
  113. $visibleitems = array_slice($allitems, $start, $count);
  114. // fill score cache if not done yet
  115. if (!isset ($this->scorecache)) {
  116. foreach ($visibleitems as $item) {
  117. $this->scorecache[$item->get_item_type() . $item->get_id()]
  118. = $item->calc_score($this->userid);
  119. }
  120. }
  121. // generate the data to display
  122. $scoredisplay = ScoreDisplay :: instance();
  123. $data = array();
  124. foreach ($visibleitems as $item) {
  125. $row = array ();
  126. $row[] = $item;
  127. $row[] = $item->get_name();
  128. $row[] = $this->build_course_name ($item);
  129. $row[] = $this->build_category_name ($item);
  130. $row[] = $this->build_average_column ($item, $ignore_score_color);
  131. $row[] = $this->build_result_column ($item, $ignore_score_color);
  132. if ($scoredisplay->is_custom())
  133. $row[] = $this->build_mask_column ($item, $ignore_score_color);
  134. $data[] = $row;
  135. }
  136. return $data;
  137. }
  138. // Sort functions
  139. // Make sure to only use functions as defined in the GradebookItem interface !
  140. function sort_by_type($item1, $item2) {
  141. if ($item1->get_item_type() == $item2->get_item_type()) {
  142. return $this->sort_by_name($item1,$item2);
  143. } else {
  144. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  145. }
  146. }
  147. function sort_by_course($item1, $item2) {
  148. $name1 = api_strtolower($this->get_course_name_from_code_cached($item1->get_course_code()));
  149. $name2 = api_strtolower($this->get_course_name_from_code_cached($item2->get_course_code()));
  150. return api_strnatcmp($name1, $name2);
  151. }
  152. function sort_by_category($item1, $item2) {
  153. $cat1 = $this->get_category_cached($item1->get_category_id());
  154. $cat2 = $this->get_category_cached($item2->get_category_id());
  155. $name1 = api_strtolower($this->get_category_name_to_display($cat1));
  156. $name2 = api_strtolower($this->get_category_name_to_display($cat2));
  157. return api_strnatcmp($name1, $name2);
  158. }
  159. function sort_by_name($item1, $item2) {
  160. return api_strnatcmp($item1->get_name(),$item2->get_name());
  161. }
  162. function sort_by_average($item1, $item2) {
  163. $score1 = $this->avgcache[$item1->get_item_type() . $item1->get_id()];
  164. $score2 = $this->avgcache[$item2->get_item_type() . $item2->get_id()];
  165. return $this->compare_scores($score1, $score2);
  166. }
  167. function sort_by_score($item1, $item2) {
  168. $score1 = $this->scorecache[$item1->get_item_type() . $item1->get_id()];
  169. $score2 = $this->scorecache[$item2->get_item_type() . $item2->get_id()];
  170. return $this->compare_scores($score1, $score2);
  171. }
  172. function sort_by_mask($item1, $item2) {
  173. $score1 = $this->scorecache[$item1->get_item_type() . $item1->get_id()];
  174. $score2 = $this->scorecache[$item2->get_item_type() . $item2->get_id()];
  175. return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);
  176. }
  177. function compare_scores ($score1, $score2) {
  178. if (!isset($score1)) {
  179. return (isset($score2) ? 1 : 0);
  180. } elseif (!isset($score2)) {
  181. return -1;
  182. } elseif (($score1[0]/$score1[1]) == ($score2[0]/$score2[1])) {
  183. return 0;
  184. } else {
  185. return (($score1[0]/$score1[1]) < ($score2[0]/$score2[1]) ? -1 : 1);
  186. }
  187. }
  188. // Other functions
  189. private function build_course_name ($item) {
  190. return $this->get_course_name_from_code_cached($item->get_course_code());
  191. }
  192. private function build_category_name ($item) {
  193. $cat = $this->get_category_cached($item->get_category_id());
  194. return $this->get_category_name_to_display($cat);
  195. }
  196. private function build_average_column ($item, $ignore_score_color) {
  197. if (isset($this->avgcache)) {
  198. $avgscore = $this->avgcache[$item->get_item_type() . $item->get_id()];
  199. } else {
  200. $avgscore = $item->calc_score();
  201. }
  202. $scoredisplay = ScoreDisplay :: instance();
  203. $displaytype = SCORE_AVERAGE;
  204. if ($ignore_score_color)
  205. $displaytype |= SCORE_IGNORE_SPLIT;
  206. return $scoredisplay->display_score($avgscore, $displaytype);
  207. }
  208. private function build_result_column ($item, $ignore_score_color) {
  209. $studscore = $this->scorecache[$item->get_item_type() . $item->get_id()];
  210. $scoredisplay = ScoreDisplay :: instance();
  211. $displaytype = SCORE_DIV_PERCENT;
  212. if ($ignore_score_color) {
  213. $displaytype |= SCORE_IGNORE_SPLIT;
  214. }
  215. return $scoredisplay->display_score($studscore, $displaytype, SCORE_ONLY_DEFAULT);
  216. }
  217. private function build_mask_column ($item, $ignore_score_color) {
  218. $studscore = $this->scorecache[$item->get_item_type() . $item->get_id()];
  219. $scoredisplay = ScoreDisplay :: instance();
  220. $displaytype = SCORE_DIV_PERCENT;
  221. if ($ignore_score_color) {
  222. $displaytype |= SCORE_IGNORE_SPLIT;
  223. }
  224. return $scoredisplay->display_score($studscore, $displaytype, SCORE_ONLY_CUSTOM);
  225. }
  226. private function get_course_name_from_code_cached ($coursecode) {
  227. if (isset ($this->coursecodecache)
  228. && isset ($this->coursecodecache[$coursecode])) {
  229. return $this->coursecodecache[$coursecode];
  230. } else {
  231. $name = get_course_name_from_code($coursecode);
  232. $this->coursecodecache[$coursecode] = $name;
  233. return $name;
  234. }
  235. }
  236. private function get_category_cached ($category_id) {
  237. if (isset ($this->categorycache)
  238. && isset ($this->categorycache[$category_id])) {
  239. return $this->categorycache[$category_id];
  240. }else {
  241. $cat = Category::load($category_id);
  242. if (isset($cat)){
  243. $this->categorycache[$category_id] = $cat[0];
  244. return $cat[0];
  245. }else
  246. return null;
  247. }
  248. }
  249. private function get_category_name_to_display ($cat) {
  250. if (isset($cat)){
  251. if ($cat->get_parent_id() == '0' || $cat->get_parent_id() == null){
  252. return '';
  253. } else {
  254. return $cat->get_name();
  255. }
  256. } else {
  257. return '';
  258. }
  259. }
  260. }