user_data_generator.class.php 12 KB

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