user_data_generator.class.php 9.3 KB

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