gradebook_data_generator.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class GradebookDataGenerator
  5. * Class to select, sort and transform object data into array data,
  6. * used for the general gradebook view
  7. * @author Bert Steppé
  8. * @package chamilo.gradebook
  9. */
  10. class GradebookDataGenerator
  11. {
  12. // Sorting types constants
  13. const GDG_SORT_TYPE = 1;
  14. const GDG_SORT_NAME = 2;
  15. const GDG_SORT_DESCRIPTION = 4;
  16. const GDG_SORT_WEIGHT = 8;
  17. const GDG_SORT_DATE = 16;
  18. const GDG_SORT_ASC = 32;
  19. const GDG_SORT_DESC = 64;
  20. const GDG_SORT_ID = 128;
  21. public $userId;
  22. private $items;
  23. private $evals_links;
  24. /**
  25. * @param array $cats
  26. * @param array $evals
  27. * @param array $links
  28. */
  29. public function __construct($cats = array(), $evals = array(), $links = array())
  30. {
  31. $allcats = isset($cats) ? $cats : array();
  32. $allevals = isset($evals) ? $evals : array();
  33. $alllinks = isset($links) ? $links : array();
  34. // if we are in the root category and if there are sub categories
  35. // display only links depending of the root category and not link that belongs
  36. // to a sub category https://support.chamilo.org/issues/6602
  37. $tabLinkToDisplay = $alllinks;
  38. if (count($allcats) > 0) {
  39. // get sub categories id
  40. $tabCategories = array();
  41. for ($i = 0; $i < count($allcats); $i++) {
  42. $tabCategories[] = $allcats[$i]->get_id();
  43. }
  44. // dont display links that belongs to a sub category
  45. $tabLinkToDisplay = array();
  46. for ($i = 0; $i < count($alllinks); $i++) {
  47. if (!in_array($alllinks[$i]->get_category_id(), $tabCategories)) {
  48. $tabLinkToDisplay[] = $alllinks[$i];
  49. }
  50. }
  51. }
  52. // merge categories, evaluations and links
  53. $this->items = array_merge($allcats, $allevals, $tabLinkToDisplay);
  54. $this->evals_links = array_merge($allevals, $tabLinkToDisplay);
  55. $this->userId = api_get_user_id();
  56. }
  57. /**
  58. * Get total number of items (rows)
  59. * @return int
  60. */
  61. public function get_total_items_count()
  62. {
  63. return count($this->items);
  64. }
  65. /**
  66. * Get actual array data
  67. * @param integer $count
  68. * @return array 2-dimensional array - each array contains the elements:
  69. * 0: cat/eval/link object
  70. * 1: item name
  71. * 2: description
  72. * 3: weight
  73. * 4: date
  74. * 5: student's score (if student logged in)
  75. */
  76. public function get_data(
  77. $sorting = 0,
  78. $start = 0,
  79. $count = null,
  80. $ignore_score_color = false,
  81. $studentList = array()
  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. usort($allitems, array('GradebookDataGenerator', 'sort_by_name'));
  92. $userId = $this->userId;
  93. // Get selected items
  94. $visibleItems = array_slice($allitems, $start, $count);
  95. $userCount = count($studentList);
  96. // Generate the data to display
  97. $data = array();
  98. $totalWeight = 0;
  99. /** @var GradebookItem $item */
  100. foreach ($visibleItems as $item) {
  101. $row = array();
  102. $row[] = $item;
  103. $row[] = $item->get_name();
  104. // display the 2 first line of description, and all description on mouseover (https://support.chamilo.org/issues/6588)
  105. $row[] = '<span title="'.api_remove_tags_with_space($item->get_description()).'">'.
  106. api_get_short_text_from_html($item->get_description(), 160).'</span>';
  107. $totalWeight += $item->get_weight();
  108. $row[] = $item->get_weight();
  109. $item->setStudentList($studentList);
  110. //if (count($this->evals_links) > 0) {
  111. if (get_class($item) == 'Evaluation') {
  112. // Items inside a category.
  113. if (1) {
  114. $resultColumn = $this->build_result_column(
  115. $userId,
  116. $item,
  117. $ignore_score_color
  118. );
  119. $row[] = $resultColumn['display'];
  120. $row['result_score'] = $resultColumn['score'];
  121. $row['result_score_weight'] = $resultColumn['score_weight'];
  122. // Best
  123. $best = $this->buildBestResultColumn($item);
  124. $row['best'] = $best['display'];
  125. $row['best_score'] = $best['score'];
  126. // Average
  127. $average = $this->buildAverageResultColumn($item);
  128. $row['average'] = $average['display'];
  129. $row['average_score'] = $average['score'];
  130. // Ranking
  131. $ranking = $this->buildRankingColumn($item, $userId, $userCount);
  132. $row['ranking'] = $ranking['display'];
  133. $row['ranking_score'] = $ranking['score'];
  134. $row[] = $item;
  135. }
  136. } else {
  137. // Category.
  138. $result = $this->build_result_column($userId, $item, $ignore_score_color, true);
  139. $row[] = $result['display'];
  140. $row['result_score'] = $result['score'];
  141. $row['result_score_weight'] = $result['score'];
  142. // Best
  143. $best = $this->buildBestResultColumn($item);
  144. $row['best'] = $best['display'];
  145. $row['best_score'] = $best['score'];
  146. // Average
  147. $average = $this->buildAverageResultColumn($item);
  148. $row['average'] = $average['display'];
  149. $row['average_score'] = $average['score'];
  150. // Ranking
  151. $rankingStudentList = array();
  152. $invalidateResults = true;
  153. foreach ($studentList as $user) {
  154. $score = $this->build_result_column(
  155. $user['user_id'],
  156. $item,
  157. $ignore_score_color,
  158. true
  159. );
  160. if (!empty($score['score'][0])) {
  161. $invalidateResults = false;
  162. }
  163. $rankingStudentList[$user['user_id']] = $score['score'][0];
  164. }
  165. $scoreDisplay = ScoreDisplay::instance();
  166. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  167. $row['ranking'] = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH, true);
  168. if ($invalidateResults) {
  169. $row['ranking'] = null;
  170. }
  171. }
  172. $data[] = $row;
  173. }
  174. return $data;
  175. }
  176. /**
  177. * Get best result of an item
  178. * @param GradebookItem $item
  179. * @return string
  180. */
  181. private function buildBestResultColumn(GradebookItem $item)
  182. {
  183. $score = $item->calc_score(
  184. null,
  185. 'best',
  186. api_get_course_id(),
  187. api_get_session_id()
  188. );
  189. $scoreDisplay = ScoreDisplay::instance();
  190. $display = $scoreDisplay->display_score($score, SCORE_DIV_PERCENT_WITH_CUSTOM, SCORE_BOTH, true);
  191. $type = $item->get_item_type();
  192. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  193. $display = ExerciseLib::show_score($score[0], $score[1], false);
  194. }
  195. return array(
  196. 'display' => $display,
  197. 'score' => $score
  198. );
  199. }
  200. /**
  201. * @param GradebookItem $item
  202. *
  203. * @return string
  204. */
  205. private function buildAverageResultColumn(GradebookItem $item)
  206. {
  207. $score = $item->calc_score(null, 'average');
  208. $scoreDisplay = ScoreDisplay::instance();
  209. $display = $scoreDisplay->display_score($score, SCORE_DIV_PERCENT_WITH_CUSTOM, SCORE_BOTH, true);
  210. $type = $item->get_item_type();
  211. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  212. $display = ExerciseLib::show_score($score[0], $score[1], false);
  213. }
  214. return array(
  215. 'display' => $display,
  216. 'score' => $score
  217. );
  218. }
  219. /**
  220. * @param GradebookItem $item
  221. * @param int $userId
  222. * @param int $userCount
  223. *
  224. * @return string
  225. */
  226. private function buildRankingColumn(GradebookItem $item, $userId = null, $userCount = 0)
  227. {
  228. $score = $item->calc_score($userId, 'ranking');
  229. $score[1] = $userCount;
  230. $scoreDisplay = null;
  231. if (isset($score[0])) {
  232. $scoreDisplay = ScoreDisplay::instance();
  233. $scoreDisplay = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH);
  234. }
  235. return array(
  236. 'display' => $scoreDisplay,
  237. 'score' => $score
  238. );
  239. }
  240. /**
  241. * @param int $userId
  242. * @param GradebookItem $item
  243. * @param boolean $ignore_score_color
  244. * @return null|string
  245. */
  246. private function build_result_column(
  247. $userId,
  248. $item,
  249. $ignore_score_color,
  250. $forceSimpleResult = false
  251. ) {
  252. $scoredisplay = ScoreDisplay::instance();
  253. $score = $item->calc_score($userId);
  254. if (!empty($score)) {
  255. switch ($item->get_item_type()) {
  256. // category
  257. case 'C':
  258. if ($score != null) {
  259. if ($forceSimpleResult) {
  260. return
  261. array(
  262. 'display' => $scoredisplay->display_score(
  263. $score,
  264. SCORE_DIV
  265. ),
  266. 'score' => $score,
  267. 'score_weight' => $score
  268. );
  269. }
  270. return array(
  271. 'display' => $scoredisplay->display_score($score, SCORE_DIV),
  272. 'score' => $score,
  273. 'score_weight' => $score
  274. );
  275. } else {
  276. return array(
  277. 'display' => null,
  278. 'score' => $score,
  279. 'score_weight' => $score
  280. );
  281. }
  282. break;
  283. // evaluation and link
  284. case 'E':
  285. case 'L':
  286. //if ($parentId == 0) {
  287. $scoreWeight = [
  288. ($score[1] > 0) ? $score[0] / $score[1] * $item->get_weight() : 0,
  289. $item->get_weight()
  290. ];
  291. //}
  292. $display = $scoredisplay->display_score(
  293. $score,
  294. SCORE_DIV_PERCENT_WITH_CUSTOM
  295. );
  296. $type = $item->get_item_type();
  297. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  298. $display = ExerciseLib::show_score($score[0], $score[1], false);
  299. }
  300. return array(
  301. 'display' => $display,
  302. 'score' => $score,
  303. 'score_weight' => $scoreWeight,
  304. );
  305. }
  306. }
  307. return array(
  308. 'display' => null,
  309. 'score' => null,
  310. 'score_weight' => null
  311. );
  312. }
  313. /**
  314. * @param GradebookItem $item
  315. * @return string
  316. */
  317. private function build_date_column($item)
  318. {
  319. $date = $item->get_date();
  320. if (!isset($date) || empty($date)) {
  321. return '';
  322. } else {
  323. if (is_int($date)) {
  324. return api_convert_and_format_date($date);
  325. } else {
  326. return api_format_date($date);
  327. }
  328. }
  329. }
  330. /**
  331. * Returns the link to the certificate generation, if the score is enough, otherwise
  332. * returns an empty string. This only works with categories.
  333. * @param object Item
  334. */
  335. public function get_certificate_link($item)
  336. {
  337. if (is_a($item, 'Category')) {
  338. if ($item->is_certificate_available(api_get_user_id())) {
  339. $link = '<a href="'.Security::remove_XSS($_SESSION['gradebook_dest']).'?export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.
  340. get_lang('Certificate').'</a>';
  341. return $link;
  342. }
  343. }
  344. return '';
  345. }
  346. /**
  347. * @param GradebookItem $item1
  348. * @param GradebookItem $item2
  349. * @return int
  350. */
  351. public function sort_by_name($item1, $item2)
  352. {
  353. return api_strnatcmp($item1->get_name(), $item2->get_name());
  354. }
  355. /**
  356. * @param GradebookItem $item1
  357. * @param GradebookItem $item2
  358. * @return int
  359. */
  360. public function sort_by_id($item1, $item2)
  361. {
  362. return api_strnatcmp($item1->get_id(), $item2->get_id());
  363. }
  364. /**
  365. * @param GradebookItem $item1
  366. * @param GradebookItem $item2
  367. * @return int
  368. */
  369. public function sort_by_type($item1, $item2)
  370. {
  371. if ($item1->get_item_type() == $item2->get_item_type()) {
  372. return $this->sort_by_name($item1, $item2);
  373. } else {
  374. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  375. }
  376. }
  377. /**
  378. * @param GradebookItem $item1
  379. * @param GradebookItem $item2
  380. * @return int
  381. */
  382. public function sort_by_description($item1, $item2)
  383. {
  384. $result = api_strcmp($item1->get_description(), $item2->get_description());
  385. if ($result == 0) {
  386. return $this->sort_by_name($item1, $item2);
  387. }
  388. return $result;
  389. }
  390. /**
  391. * @param GradebookItem $item1
  392. * @param GradebookItem $item2
  393. * @return int
  394. */
  395. public function sort_by_weight($item1, $item2)
  396. {
  397. if ($item1->get_weight() == $item2->get_weight()) {
  398. return $this->sort_by_name($item1, $item2);
  399. } else {
  400. return ($item1->get_weight() < $item2->get_weight() ? -1 : 1);
  401. }
  402. }
  403. /**
  404. * @param GradebookItem $item1
  405. * @param GradebookItem $item2
  406. * @return int
  407. */
  408. public function sort_by_date($item1, $item2)
  409. {
  410. if (is_int($item1->get_date())) {
  411. $timestamp1 = $item1->get_date();
  412. } else {
  413. $date = $item1->get_date();
  414. if (!empty($date)) {
  415. $timestamp1 = api_strtotime($date, 'UTC');
  416. } else {
  417. $timestamp1 = null;
  418. }
  419. }
  420. if (is_int($item2->get_date())) {
  421. $timestamp2 = $item2->get_date();
  422. } else {
  423. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  424. }
  425. if ($timestamp1 == $timestamp2) {
  426. return $this->sort_by_name($item1, $item2);
  427. } else {
  428. return ($timestamp1 < $timestamp2 ? -1 : 1);
  429. }
  430. }
  431. }