user_data_generator.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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()] = $item->calc_score($this->userid);
  117. }
  118. usort($allitems, ['UserDataGenerator', 'sort_by_score']);
  119. } elseif ($sorting & self::UDG_SORT_MASK) {
  120. // if user sorts on student's masks, first calculate scores and cache them
  121. foreach ($allitems as $item) {
  122. $this->scorecache[$item->get_item_type().$item->get_id()] = $item->calc_score($this->userid);
  123. }
  124. usort($allitems, ['UserDataGenerator', 'sort_by_mask']);
  125. }
  126. if ($sorting & self::UDG_SORT_DESC) {
  127. $allitems = array_reverse($allitems);
  128. }
  129. // select the items we have to display
  130. $visibleitems = array_slice($allitems, $start, $count);
  131. // fill score cache if not done yet
  132. if (!isset($this->scorecache)) {
  133. foreach ($visibleitems as $item) {
  134. $this->scorecache[$item->get_item_type().$item->get_id()] = $item->calc_score($this->userid);
  135. }
  136. }
  137. // generate the data to display
  138. $scoredisplay = ScoreDisplay::instance();
  139. $data = [];
  140. $model = ExerciseLib::getCourseScoreModel();
  141. foreach ($visibleitems as $item) {
  142. $row = [];
  143. $row[] = $item;
  144. $row[] = $item->get_name();
  145. $row[] = $this->build_course_name($item);
  146. $row[] = $this->build_category_name($item);
  147. if (!empty($model)) {
  148. if (isset($this->avgcache)) {
  149. $avgscore = $this->avgcache[$item->get_item_type().$item->get_id()];
  150. } else {
  151. $avgscore = $item->calc_score();
  152. }
  153. $row[] = ExerciseLib::show_score($avgscore[0], $avgscore[1]);
  154. $score = $this->scorecache[$item->get_item_type().$item->get_id()];
  155. $displayScore = ExerciseLib::show_score($score[0], $score[1]);
  156. $row[] = $displayScore;
  157. if ($scoredisplay->is_custom()) {
  158. $row[] = $displayScore;
  159. }
  160. } else {
  161. $row[] = $this->build_average_column($item, $ignore_score_color);
  162. $row[] = $this->build_result_column($item, $ignore_score_color);
  163. if ($scoredisplay->is_custom()) {
  164. $row[] = $this->build_mask_column($item, $ignore_score_color);
  165. }
  166. }
  167. $data[] = $row;
  168. }
  169. return $data;
  170. }
  171. /**
  172. * @param $item1
  173. * @param $item2
  174. *
  175. * @return int
  176. */
  177. public function sort_by_type($item1, $item2)
  178. {
  179. if ($item1->get_item_type() == $item2->get_item_type()) {
  180. return $this->sort_by_name($item1, $item2);
  181. } else {
  182. return $item1->get_item_type() < $item2->get_item_type() ? -1 : 1;
  183. }
  184. }
  185. /**
  186. * @param $item1
  187. * @param $item2
  188. *
  189. * @return int
  190. */
  191. public function sort_by_course($item1, $item2)
  192. {
  193. $name1 = api_strtolower(
  194. $this->get_course_name_from_code_cached($item1->get_course_code())
  195. );
  196. $name2 = api_strtolower(
  197. $this->get_course_name_from_code_cached($item2->get_course_code())
  198. );
  199. return api_strnatcmp($name1, $name2);
  200. }
  201. /**
  202. * @param $item1
  203. * @param $item2
  204. *
  205. * @return int
  206. */
  207. public function sort_by_category($item1, $item2)
  208. {
  209. $cat1 = $this->get_category_cached($item1->get_category_id());
  210. $cat2 = $this->get_category_cached($item2->get_category_id());
  211. $name1 = api_strtolower($this->get_category_name_to_display($cat1));
  212. $name2 = api_strtolower($this->get_category_name_to_display($cat2));
  213. return api_strnatcmp($name1, $name2);
  214. }
  215. /**
  216. * @param $item1
  217. * @param $item2
  218. *
  219. * @return int
  220. */
  221. public function sort_by_name($item1, $item2)
  222. {
  223. return api_strnatcmp($item1->get_name(), $item2->get_name());
  224. }
  225. /**
  226. * @param $item1
  227. * @param $item2
  228. *
  229. * @return int
  230. */
  231. public function sort_by_average($item1, $item2)
  232. {
  233. $score1 = $this->avgcache[$item1->get_item_type().$item1->get_id()];
  234. $score2 = $this->avgcache[$item2->get_item_type().$item2->get_id()];
  235. return $this->compare_scores($score1, $score2);
  236. }
  237. /**
  238. * @param $item1
  239. * @param $item2
  240. *
  241. * @return int
  242. */
  243. public function sort_by_score($item1, $item2)
  244. {
  245. $score1 = $this->scorecache[$item1->get_item_type().$item1->get_id()];
  246. $score2 = $this->scorecache[$item2->get_item_type().$item2->get_id()];
  247. return $this->compare_scores($score1, $score2);
  248. }
  249. /**
  250. * @param $item1
  251. * @param $item2
  252. *
  253. * @return int
  254. */
  255. public function sort_by_mask($item1, $item2)
  256. {
  257. $score1 = $this->scorecache[$item1->get_item_type().$item1->get_id()];
  258. $score2 = $this->scorecache[$item2->get_item_type().$item2->get_id()];
  259. return ScoreDisplay::compare_scores_by_custom_display($score1, $score2);
  260. }
  261. /**
  262. * @param $score1
  263. * @param $score2
  264. *
  265. * @return int
  266. */
  267. public function compare_scores($score1, $score2)
  268. {
  269. if (!isset($score1)) {
  270. return isset($score2) ? 1 : 0;
  271. } elseif (!isset($score2)) {
  272. return -1;
  273. } elseif (($score1[0] / $score1[1]) == ($score2[0] / $score2[1])) {
  274. return 0;
  275. } else {
  276. return ($score1[0] / $score1[1]) < ($score2[0] / $score2[1]) ? -1 : 1;
  277. }
  278. }
  279. /**
  280. * @param $item
  281. *
  282. * @return mixed
  283. */
  284. private function build_course_name($item)
  285. {
  286. return $this->get_course_name_from_code_cached($item->get_course_code());
  287. }
  288. /**
  289. * @param $item
  290. *
  291. * @return string
  292. */
  293. private function build_category_name($item)
  294. {
  295. $cat = $this->get_category_cached($item->get_category_id());
  296. return $this->get_category_name_to_display($cat);
  297. }
  298. /**
  299. * @param $item
  300. * @param $ignore_score_color
  301. *
  302. * @return string
  303. */
  304. private function build_average_column($item, $ignore_score_color)
  305. {
  306. if (isset($this->avgcache)) {
  307. $avgscore = $this->avgcache[$item->get_item_type().$item->get_id()];
  308. } else {
  309. $avgscore = $item->calc_score();
  310. }
  311. $scoredisplay = ScoreDisplay::instance();
  312. $displaytype = SCORE_AVERAGE;
  313. /*if ($ignore_score_color)
  314. $displaytype |= SCORE_IGNORE_SPLIT;
  315. */
  316. return $scoredisplay->display_score($avgscore, $displaytype);
  317. }
  318. /**
  319. * @param $item
  320. * @param $ignore_score_color
  321. *
  322. * @return string
  323. */
  324. private function build_result_column($item, $ignore_score_color)
  325. {
  326. $studscore = $this->scorecache[$item->get_item_type().$item->get_id()];
  327. $scoredisplay = ScoreDisplay::instance();
  328. $displaytype = SCORE_DIV_PERCENT;
  329. if ($ignore_score_color) {
  330. $displaytype |= SCORE_IGNORE_SPLIT;
  331. }
  332. return $scoredisplay->display_score(
  333. $studscore,
  334. $displaytype,
  335. SCORE_ONLY_DEFAULT
  336. );
  337. }
  338. /**
  339. * @param $item
  340. * @param $ignore_score_color
  341. *
  342. * @return string
  343. */
  344. private function build_mask_column($item, $ignore_score_color)
  345. {
  346. $studscore = $this->scorecache[$item->get_item_type().$item->get_id()];
  347. $scoredisplay = ScoreDisplay::instance();
  348. $displaytype = SCORE_DIV_PERCENT;
  349. if ($ignore_score_color) {
  350. $displaytype |= SCORE_IGNORE_SPLIT;
  351. }
  352. return $scoredisplay->display_score(
  353. $studscore,
  354. $displaytype,
  355. SCORE_ONLY_CUSTOM
  356. );
  357. }
  358. /**
  359. * @param string $coursecode
  360. *
  361. * @return mixed
  362. */
  363. private function get_course_name_from_code_cached($coursecode)
  364. {
  365. if (isset($this->coursecodecache) &&
  366. isset($this->coursecodecache[$coursecode])
  367. ) {
  368. return $this->coursecodecache[$coursecode];
  369. } else {
  370. $name = CourseManager::getCourseNameFromCode($coursecode);
  371. $this->coursecodecache[$coursecode] = $name;
  372. return $name;
  373. }
  374. }
  375. /**
  376. * @param int $category_id
  377. */
  378. private function get_category_cached($category_id)
  379. {
  380. if (isset($this->categorycache) &&
  381. isset($this->categorycache[$category_id])
  382. ) {
  383. return $this->categorycache[$category_id];
  384. } else {
  385. $cat = Category::load($category_id);
  386. if (isset($cat)) {
  387. $this->categorycache[$category_id] = $cat[0];
  388. return $cat[0];
  389. } else {
  390. return null;
  391. }
  392. }
  393. }
  394. /**
  395. * @param $cat
  396. *
  397. * @return string
  398. */
  399. private function get_category_name_to_display($cat)
  400. {
  401. if (isset($cat)) {
  402. if ($cat->get_parent_id() == '0' || $cat->get_parent_id() == null) {
  403. return '';
  404. } else {
  405. return $cat->get_name();
  406. }
  407. }
  408. return '';
  409. }
  410. }