user_data_generator.class.php 12 KB

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