user_data_generator.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class UserDataGenerator
  5. * Class to select, sort and transform object data into array data,
  6. * used for a student's general view
  7. * @author Bert Steppé
  8. * @package chamilo.gradebook
  9. */
  10. class UserDataGenerator
  11. {
  12. // Sorting types constants
  13. const UDG_SORT_TYPE = 1;
  14. const UDG_SORT_NAME = 2;
  15. const UDG_SORT_COURSE = 4;
  16. const UDG_SORT_CATEGORY = 8;
  17. const UDG_SORT_AVERAGE = 16;
  18. const UDG_SORT_SCORE = 32;
  19. const UDG_SORT_MASK = 64;
  20. const UDG_SORT_ASC = 128;
  21. const UDG_SORT_DESC = 256;
  22. private $items;
  23. private $userid;
  24. private $coursecodecache;
  25. private $categorycache;
  26. private $scorecache;
  27. private $avgcache;
  28. public function __construct($userid, $evals = array(), $links = array())
  29. {
  30. $this->userid = $userid;
  31. $evals_filtered = array();
  32. $result = array();
  33. foreach ($evals as $eval) {
  34. $toadd = true;
  35. $coursecode = $eval->get_course_code();
  36. if (isset($coursecode)) {
  37. $result = Result :: load(null, $userid, $eval->get_id());
  38. if (count($result) == 0) {
  39. $toadd = false;
  40. }
  41. }
  42. if ($toadd) {
  43. $evals_filtered_copy = $evals;
  44. }
  45. }
  46. if (count($result) == 0) {
  47. $evals_filtered=$evals;
  48. } else {
  49. $evals_filtered=$evals_filtered_copy;
  50. }
  51. $this->items = array_merge($evals_filtered, $links);
  52. $this->coursecodecache = array();
  53. $this->categorycache = array();
  54. $this->scorecache = null;
  55. $this->avgcache = null;
  56. }
  57. /**
  58. * Get total number of items (rows)
  59. */
  60. public function get_total_items_count()
  61. {
  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. {
  77. // do some checks on count, redefine if invalid value
  78. if (!isset($count)) {
  79. $count = count ($this->items) - $start;
  80. }
  81. if ($count < 0) {
  82. $count = 0;
  83. }
  84. $allitems = $this->items;
  85. // sort users array
  86. if ($sorting & self :: UDG_SORT_TYPE) {
  87. usort($allitems, array('UserDataGenerator', 'sort_by_type'));
  88. }elseif ($sorting & self :: UDG_SORT_NAME) {
  89. usort($allitems, array('UserDataGenerator', 'sort_by_name'));
  90. } elseif ($sorting & self :: UDG_SORT_COURSE) {
  91. usort($allitems, array('UserDataGenerator', 'sort_by_course'));
  92. } elseif ($sorting & self :: UDG_SORT_CATEGORY) {
  93. usort($allitems, array('UserDataGenerator', 'sort_by_category'));
  94. } elseif ($sorting & self :: UDG_SORT_AVERAGE) {
  95. // if user sorts on average scores, first calculate them and cache them
  96. foreach ($allitems as $item) {
  97. $this->avgcache[$item->get_item_type() . $item->get_id()]= $item->calc_score();
  98. }
  99. usort($allitems, array('UserDataGenerator', 'sort_by_average'));
  100. } elseif ($sorting & self :: UDG_SORT_SCORE) {
  101. // if user sorts on student's scores, first calculate them and cache them
  102. foreach ($allitems as $item) {
  103. $this->scorecache[$item->get_item_type() . $item->get_id()]
  104. = $item->calc_score($this->userid);
  105. }
  106. usort($allitems, array('UserDataGenerator', 'sort_by_score'));
  107. } elseif ($sorting & self :: UDG_SORT_MASK) {
  108. // if user sorts on student's masks, first calculate scores and cache them
  109. foreach ($allitems as $item) {
  110. $this->scorecache[$item->get_item_type() . $item->get_id()]
  111. = $item->calc_score($this->userid);
  112. }
  113. usort($allitems, array('UserDataGenerator', 'sort_by_mask'));
  114. }
  115. if ($sorting & self :: UDG_SORT_DESC) {
  116. $allitems = array_reverse($allitems);
  117. }
  118. // select the items we have to display
  119. $visibleitems = array_slice($allitems, $start, $count);
  120. // fill score cache if not done yet
  121. if (!isset ($this->scorecache)) {
  122. foreach ($visibleitems as $item) {
  123. $this->scorecache[$item->get_item_type() . $item->get_id()]
  124. = $item->calc_score($this->userid);
  125. }
  126. }
  127. // generate the data to display
  128. $scoredisplay = ScoreDisplay :: instance();
  129. $data = array();
  130. foreach ($visibleitems as $item) {
  131. $row = array ();
  132. $row[] = $item;
  133. $row[] = $item->get_name();
  134. $row[] = $this->build_course_name($item);
  135. $row[] = $this->build_category_name($item);
  136. $row[] = $this->build_average_column($item, $ignore_score_color);
  137. $row[] = $this->build_result_column($item, $ignore_score_color);
  138. if ($scoredisplay->is_custom())
  139. $row[] = $this->build_mask_column($item, $ignore_score_color);
  140. $data[] = $row;
  141. }
  142. return $data;
  143. }
  144. /**
  145. * @param $item1
  146. * @param $item2
  147. * @return int
  148. */
  149. function sort_by_type($item1, $item2)
  150. {
  151. if ($item1->get_item_type() == $item2->get_item_type()) {
  152. return $this->sort_by_name($item1,$item2);
  153. } else {
  154. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  155. }
  156. }
  157. /**
  158. * @param $item1
  159. * @param $item2
  160. * @return int
  161. */
  162. function sort_by_course($item1, $item2)
  163. {
  164. $name1 = api_strtolower($this->get_course_name_from_code_cached($item1->get_course_code()));
  165. $name2 = api_strtolower($this->get_course_name_from_code_cached($item2->get_course_code()));
  166. return api_strnatcmp($name1, $name2);
  167. }
  168. /**
  169. * @param $item1
  170. * @param $item2
  171. * @return int
  172. */
  173. function sort_by_category($item1, $item2)
  174. {
  175. $cat1 = $this->get_category_cached($item1->get_category_id());
  176. $cat2 = $this->get_category_cached($item2->get_category_id());
  177. $name1 = api_strtolower($this->get_category_name_to_display($cat1));
  178. $name2 = api_strtolower($this->get_category_name_to_display($cat2));
  179. return api_strnatcmp($name1, $name2);
  180. }
  181. /**
  182. * @param $item1
  183. * @param $item2
  184. * @return int
  185. */
  186. function sort_by_name($item1, $item2)
  187. {
  188. return api_strnatcmp($item1->get_name(),$item2->get_name());
  189. }
  190. /**
  191. * @param $item1
  192. * @param $item2
  193. * @return int
  194. */
  195. function sort_by_average($item1, $item2)
  196. {
  197. $score1 = $this->avgcache[$item1->get_item_type() . $item1->get_id()];
  198. $score2 = $this->avgcache[$item2->get_item_type() . $item2->get_id()];
  199. return $this->compare_scores($score1, $score2);
  200. }
  201. /**
  202. * @param $item1
  203. * @param $item2
  204. * @return int
  205. */
  206. function sort_by_score($item1, $item2)
  207. {
  208. $score1 = $this->scorecache[$item1->get_item_type() . $item1->get_id()];
  209. $score2 = $this->scorecache[$item2->get_item_type() . $item2->get_id()];
  210. return $this->compare_scores($score1, $score2);
  211. }
  212. /**
  213. * @param $item1
  214. * @param $item2
  215. * @return int
  216. */
  217. function sort_by_mask($item1, $item2)
  218. {
  219. $score1 = $this->scorecache[$item1->get_item_type() . $item1->get_id()];
  220. $score2 = $this->scorecache[$item2->get_item_type() . $item2->get_id()];
  221. return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);
  222. }
  223. /**
  224. * @param $score1
  225. * @param $score2
  226. * @return int
  227. */
  228. function compare_scores($score1, $score2)
  229. {
  230. if (!isset($score1)) {
  231. return (isset($score2) ? 1 : 0);
  232. } elseif (!isset($score2)) {
  233. return -1;
  234. } elseif (($score1[0]/$score1[1]) == ($score2[0]/$score2[1])) {
  235. return 0;
  236. } else {
  237. return (($score1[0]/$score1[1]) < ($score2[0]/$score2[1]) ? -1 : 1);
  238. }
  239. }
  240. /**
  241. * @param $item
  242. * @return mixed
  243. */
  244. private function build_course_name($item)
  245. {
  246. return $this->get_course_name_from_code_cached($item->get_course_code());
  247. }
  248. /**
  249. * @param $item
  250. * @return string
  251. */
  252. private function build_category_name($item)
  253. {
  254. $cat = $this->get_category_cached($item->get_category_id());
  255. return $this->get_category_name_to_display($cat);
  256. }
  257. /**
  258. * @param $item
  259. * @param $ignore_score_color
  260. * @return string
  261. */
  262. private function build_average_column($item, $ignore_score_color)
  263. {
  264. if (isset($this->avgcache)) {
  265. $avgscore = $this->avgcache[$item->get_item_type() . $item->get_id()];
  266. } else {
  267. $avgscore = $item->calc_score();
  268. }
  269. $scoredisplay = ScoreDisplay :: instance();
  270. $displaytype = SCORE_AVERAGE;
  271. /*if ($ignore_score_color)
  272. $displaytype |= SCORE_IGNORE_SPLIT;
  273. */
  274. return $scoredisplay->display_score($avgscore, $displaytype);
  275. }
  276. /**
  277. * @param $item
  278. * @param $ignore_score_color
  279. * @return string
  280. */
  281. private function build_result_column($item, $ignore_score_color)
  282. {
  283. $studscore = $this->scorecache[$item->get_item_type() . $item->get_id()];
  284. $scoredisplay = ScoreDisplay :: instance();
  285. $displaytype = SCORE_DIV_PERCENT;
  286. if ($ignore_score_color) {
  287. $displaytype |= SCORE_IGNORE_SPLIT;
  288. }
  289. return $scoredisplay->display_score($studscore, $displaytype, SCORE_ONLY_DEFAULT);
  290. }
  291. /**
  292. * @param $item
  293. * @param $ignore_score_color
  294. * @return string
  295. */
  296. private function build_mask_column($item, $ignore_score_color)
  297. {
  298. $studscore = $this->scorecache[$item->get_item_type() . $item->get_id()];
  299. $scoredisplay = ScoreDisplay :: instance();
  300. $displaytype = SCORE_DIV_PERCENT;
  301. if ($ignore_score_color) {
  302. $displaytype |= SCORE_IGNORE_SPLIT;
  303. }
  304. return $scoredisplay->display_score($studscore, $displaytype, SCORE_ONLY_CUSTOM);
  305. }
  306. /**
  307. * @param $coursecode
  308. * @return mixed
  309. */
  310. private function get_course_name_from_code_cached($coursecode)
  311. {
  312. if (isset ($this->coursecodecache)
  313. && isset ($this->coursecodecache[$coursecode])) {
  314. return $this->coursecodecache[$coursecode];
  315. } else {
  316. $name = CourseManager::getCourseNameFromCode($coursecode);
  317. $this->coursecodecache[$coursecode] = $name;
  318. return $name;
  319. }
  320. }
  321. /**
  322. * @param $category_id
  323. * @return null
  324. */
  325. private function get_category_cached($category_id)
  326. {
  327. if (isset ($this->categorycache)
  328. && isset ($this->categorycache[$category_id])) {
  329. return $this->categorycache[$category_id];
  330. }else {
  331. $cat = Category::load($category_id);
  332. if (isset($cat)){
  333. $this->categorycache[$category_id] = $cat[0];
  334. return $cat[0];
  335. }else
  336. return null;
  337. }
  338. }
  339. /**
  340. * @param $cat
  341. * @return string
  342. */
  343. private function get_category_name_to_display($cat)
  344. {
  345. if (isset($cat)) {
  346. if ($cat->get_parent_id() == '0' || $cat->get_parent_id() == null){
  347. return '';
  348. } else {
  349. return $cat->get_name();
  350. }
  351. } else {
  352. return '';
  353. }
  354. }
  355. }