gradebook_data_generator.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class GradebookDataGenerator
  6. * Class to select, sort and transform object data into array data,
  7. * used for the general gradebook view.
  8. *
  9. * @author Bert Steppé
  10. *
  11. * @package chamilo.gradebook
  12. */
  13. class GradebookDataGenerator
  14. {
  15. // Sorting types constants
  16. const GDG_SORT_TYPE = 1;
  17. const GDG_SORT_NAME = 2;
  18. const GDG_SORT_DESCRIPTION = 4;
  19. const GDG_SORT_WEIGHT = 8;
  20. const GDG_SORT_DATE = 16;
  21. const GDG_SORT_ASC = 32;
  22. const GDG_SORT_DESC = 64;
  23. const GDG_SORT_ID = 128;
  24. public $userId;
  25. public $hidePercentage = false;
  26. public $items;
  27. public $preLoadDataKey;
  28. private $evals_links;
  29. /**
  30. * @param array $cats
  31. * @param array $evals
  32. * @param array $links
  33. */
  34. public function __construct($cats = [], $evals = [], $links = [])
  35. {
  36. $allcats = isset($cats) ? $cats : [];
  37. $allevals = isset($evals) ? $evals : [];
  38. $alllinks = isset($links) ? $links : [];
  39. // if we are in the root category and if there are sub categories
  40. // display only links depending of the root category and not link that belongs
  41. // to a sub category https://support.chamilo.org/issues/6602
  42. $tabLinkToDisplay = $alllinks;
  43. if (count($allcats) > 0) {
  44. // get sub categories id
  45. $tabCategories = [];
  46. for ($i = 0; $i < count($allcats); $i++) {
  47. $tabCategories[] = $allcats[$i]->get_id();
  48. }
  49. // dont display links that belongs to a sub category
  50. $tabLinkToDisplay = [];
  51. for ($i = 0; $i < count($alllinks); $i++) {
  52. if (!in_array($alllinks[$i]->get_category_id(), $tabCategories)) {
  53. $tabLinkToDisplay[] = $alllinks[$i];
  54. }
  55. }
  56. }
  57. // merge categories, evaluations and links
  58. $this->items = array_merge($allcats, $allevals, $tabLinkToDisplay);
  59. $this->evals_links = array_merge($allevals, $tabLinkToDisplay);
  60. $this->userId = api_get_user_id();
  61. }
  62. /**
  63. * Get total number of items (rows).
  64. *
  65. * @return int
  66. */
  67. public function get_total_items_count()
  68. {
  69. return count($this->items);
  70. }
  71. /**
  72. * Get actual array data.
  73. *
  74. * @param int $count
  75. *
  76. * @return array 2-dimensional array - each array contains the elements:
  77. * 0: cat/eval/link object
  78. * 1: item name
  79. * 2: description
  80. * 3: weight
  81. * 4: date
  82. * 5: student's score (if student logged in)
  83. */
  84. public function get_data(
  85. $sorting = 0,
  86. $start = 0,
  87. $count = null,
  88. $ignore_score_color = false,
  89. $studentList = [],
  90. $loadStats = true
  91. ) {
  92. // do some checks on count, redefine if invalid value
  93. if (!isset($count)) {
  94. $count = count($this->items) - $start;
  95. }
  96. if ($count < 0) {
  97. $count = 0;
  98. }
  99. $allitems = $this->items;
  100. usort($allitems, ['GradebookDataGenerator', 'sort_by_name']);
  101. $userId = $this->userId;
  102. // Get selected items
  103. $visibleItems = array_slice($allitems, $start, $count);
  104. $userCount = !empty($studentList) ? count($studentList) : 0;
  105. // Generate the data to display
  106. $data = [];
  107. $allowStats = api_get_configuration_value('allow_gradebook_stats');
  108. $scoreDisplay = ScoreDisplay::instance();
  109. $defaultData = Session::read($this->preLoadDataKey);
  110. /** @var GradebookItem $item */
  111. foreach ($visibleItems as $item) {
  112. $row = [];
  113. $row[] = $item;
  114. $row[] = $item->get_name();
  115. // display the 2 first line of description and all description
  116. // on mouseover (https://support.chamilo.org/issues/6588)
  117. $row[] = '<span title="'.api_remove_tags_with_space($item->get_description()).'">'.
  118. api_get_short_text_from_html($item->get_description(), 160).'</span>';
  119. $row[] = $item->get_weight();
  120. $item->setStudentList($studentList);
  121. $itemType = get_class($item);
  122. switch ($itemType) {
  123. case 'Evaluation':
  124. // Items inside a category.
  125. $resultColumn = $this->build_result_column(
  126. $userId,
  127. $item,
  128. $ignore_score_color
  129. );
  130. $row[] = $resultColumn['display'];
  131. $row['result_score'] = $resultColumn['score'];
  132. $row['result_score_weight'] = $resultColumn['score_weight'];
  133. // Best
  134. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  135. $best = $defaultData[$item->get_id()]['best'];
  136. } else {
  137. $best = $this->buildBestResultColumn($item);
  138. }
  139. $row['best'] = $best['display'];
  140. $row['best_score'] = $best['score'];
  141. // Average
  142. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  143. $average = $defaultData[$item->get_id()]['average'];
  144. } else {
  145. $average = $this->buildBestResultColumn($item);
  146. }
  147. $row['average'] = $average['display'];
  148. $row['average_score'] = $average['score'];
  149. // Ranking
  150. $ranking = $this->buildRankingColumn($item, $userId, $userCount);
  151. $row['ranking'] = $ranking['display'];
  152. $row['ranking_score'] = $ranking['score'];
  153. $row[] = $item;
  154. break;
  155. case 'ExerciseLink':
  156. /** @var ExerciseLink $item */
  157. // Category.
  158. $result = $this->build_result_column(
  159. $userId,
  160. $item,
  161. $ignore_score_color,
  162. true
  163. );
  164. $row[] = $result['display'];
  165. $row['result_score'] = $result['score'];
  166. $row['result_score_weight'] = $result['score'];
  167. // Best
  168. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  169. $best = $defaultData[$item->get_id()]['best'];
  170. } else {
  171. $best = $this->buildBestResultColumn($item);
  172. }
  173. $row['best'] = $best['display'];
  174. $row['best_score'] = $best['score'];
  175. $rankingStudentList = [];
  176. $invalidateResults = false;
  177. $debug = $item->get_id() == 1177;
  178. // Average
  179. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  180. $average = $defaultData[$item->get_id()]['average'];
  181. } else {
  182. $average = $this->buildAverageResultColumn($item);
  183. }
  184. $row['average'] = $average['display'];
  185. $row['average_score'] = $average['score'];
  186. // Ranking
  187. if ($allowStats) {
  188. // Ranking
  189. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['ranking'])) {
  190. $rankingStudentList = $defaultData[$item->get_id()]['ranking'];
  191. $invalidateResults = $defaultData[$item->get_id()]['ranking_invalidate'];
  192. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  193. } else {
  194. if (!empty($studentList)) {
  195. foreach ($studentList as $user) {
  196. $score = $this->build_result_column(
  197. $user['user_id'],
  198. $item,
  199. $ignore_score_color,
  200. true
  201. );
  202. if (!empty($score['score'][0])) {
  203. $invalidateResults = false;
  204. }
  205. $rankingStudentList[$user['user_id']] = $score['score'][0];
  206. }
  207. $defaultData[$item->get_id()]['ranking'] = $rankingStudentList;
  208. $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateResults;
  209. Session::write($this->preLoadDataKey, $defaultData);
  210. }
  211. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  212. }
  213. } else {
  214. if (!empty($studentList)) {
  215. foreach ($studentList as $user) {
  216. $score = $this->build_result_column(
  217. $user['user_id'],
  218. $item,
  219. $ignore_score_color,
  220. true
  221. );
  222. if (!empty($score['score'][0])) {
  223. $invalidateResults = false;
  224. }
  225. $rankingStudentList[$user['user_id']] = $score['score'][0];
  226. }
  227. }
  228. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  229. }
  230. $row['ranking'] = $scoreDisplay->display_score(
  231. $score,
  232. SCORE_DIV,
  233. SCORE_BOTH,
  234. true,
  235. true
  236. );
  237. if ($invalidateResults) {
  238. $row['ranking'] = null;
  239. }
  240. break;
  241. default:
  242. // Category.
  243. $result = $this->build_result_column(
  244. $userId,
  245. $item,
  246. $ignore_score_color,
  247. true
  248. );
  249. $row[] = $result['display'];
  250. $row['result_score'] = $result['score'];
  251. $row['result_score_weight'] = $result['score'];
  252. // Best
  253. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  254. $best = $defaultData[$item->get_id()]['best'];
  255. } else {
  256. $best = $this->buildBestResultColumn($item);
  257. }
  258. $row['best'] = $best['display'];
  259. $row['best_score'] = $best['score'];
  260. $rankingStudentList = [];
  261. $invalidateResults = true;
  262. // Average
  263. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  264. $average = $defaultData[$item->get_id()]['average'];
  265. } else {
  266. $average = $this->buildAverageResultColumn($item);
  267. }
  268. $row['average'] = $average['display'];
  269. $row['average_score'] = $average['score'];
  270. // Ranking
  271. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['ranking'])) {
  272. $rankingStudentList = $defaultData[$item->get_id()]['ranking'];
  273. $invalidateResults = $defaultData[$item->get_id()]['ranking_invalidate'];
  274. $invalidateResults = false;
  275. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  276. } else {
  277. if (!empty($studentList)) {
  278. foreach ($studentList as $user) {
  279. $score = $this->build_result_column(
  280. $user['user_id'],
  281. $item,
  282. $ignore_score_color,
  283. true
  284. );
  285. if (!empty($score['score'][0])) {
  286. $invalidateResults = false;
  287. }
  288. $rankingStudentList[$user['user_id']] = $score['score'][0];
  289. }
  290. }
  291. error_log('loading not cACHE');
  292. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  293. }
  294. $row['ranking'] = $scoreDisplay->display_score(
  295. $score,
  296. SCORE_DIV,
  297. SCORE_BOTH,
  298. true,
  299. true
  300. );
  301. if ($invalidateResults) {
  302. $row['ranking'] = null;
  303. }
  304. break;
  305. }
  306. $data[] = $row;
  307. }
  308. return $data;
  309. }
  310. /**
  311. * Returns the link to the certificate generation, if the score is enough, otherwise
  312. * returns an empty string. This only works with categories.
  313. *
  314. * @param object Item
  315. *
  316. * @return string
  317. */
  318. public function get_certificate_link($item)
  319. {
  320. if (is_a($item, 'Category')) {
  321. if ($item->is_certificate_available(api_get_user_id())) {
  322. $link = '<a href="'.Category::getUrl().'export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.
  323. get_lang('Certificate').'</a>';
  324. return $link;
  325. }
  326. }
  327. return '';
  328. }
  329. /**
  330. * @param GradebookItem $item1
  331. * @param GradebookItem $item2
  332. *
  333. * @return int
  334. */
  335. public static function sort_by_name($item1, $item2)
  336. {
  337. return api_strnatcmp($item1->get_name(), $item2->get_name());
  338. }
  339. /**
  340. * @param GradebookItem $item1
  341. * @param GradebookItem $item2
  342. *
  343. * @return int
  344. */
  345. public function sort_by_id($item1, $item2)
  346. {
  347. return api_strnatcmp($item1->get_id(), $item2->get_id());
  348. }
  349. /**
  350. * @param GradebookItem $item1
  351. * @param GradebookItem $item2
  352. *
  353. * @return int
  354. */
  355. public function sort_by_type($item1, $item2)
  356. {
  357. if ($item1->get_item_type() == $item2->get_item_type()) {
  358. return $this->sort_by_name($item1, $item2);
  359. } else {
  360. return $item1->get_item_type() < $item2->get_item_type() ? -1 : 1;
  361. }
  362. }
  363. /**
  364. * @param GradebookItem $item1
  365. * @param GradebookItem $item2
  366. *
  367. * @return int
  368. */
  369. public function sort_by_description($item1, $item2)
  370. {
  371. $result = api_strcmp($item1->get_description(), $item2->get_description());
  372. if ($result == 0) {
  373. return $this->sort_by_name($item1, $item2);
  374. }
  375. return $result;
  376. }
  377. /**
  378. * @param GradebookItem $item1
  379. * @param GradebookItem $item2
  380. *
  381. * @return int
  382. */
  383. public function sort_by_weight($item1, $item2)
  384. {
  385. if ($item1->get_weight() == $item2->get_weight()) {
  386. return $this->sort_by_name($item1, $item2);
  387. } else {
  388. return $item1->get_weight() < $item2->get_weight() ? -1 : 1;
  389. }
  390. }
  391. /**
  392. * @param GradebookItem $item1
  393. * @param GradebookItem $item2
  394. *
  395. * @return int
  396. */
  397. public function sort_by_date($item1, $item2)
  398. {
  399. if (is_int($item1->get_date())) {
  400. $timestamp1 = $item1->get_date();
  401. } else {
  402. $date = $item1->get_date();
  403. if (!empty($date)) {
  404. $timestamp1 = api_strtotime($date, 'UTC');
  405. } else {
  406. $timestamp1 = null;
  407. }
  408. }
  409. if (is_int($item2->get_date())) {
  410. $timestamp2 = $item2->get_date();
  411. } else {
  412. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  413. }
  414. if ($timestamp1 == $timestamp2) {
  415. return $this->sort_by_name($item1, $item2);
  416. } else {
  417. return $timestamp1 < $timestamp2 ? -1 : 1;
  418. }
  419. }
  420. /**
  421. * Get best result of an item.
  422. *
  423. * @param GradebookItem $item
  424. *
  425. * @return array
  426. */
  427. public function buildBestResultColumn(GradebookItem $item)
  428. {
  429. $score = $item->calc_score(
  430. null,
  431. 'best',
  432. api_get_course_id(),
  433. api_get_session_id()
  434. );
  435. $scoreMode = SCORE_DIV_PERCENT_WITH_CUSTOM;
  436. if ($this->hidePercentage) {
  437. $scoreMode = SCORE_DIV;
  438. }
  439. $scoreDisplay = ScoreDisplay::instance();
  440. $display = $scoreDisplay->display_score(
  441. $score,
  442. $scoreMode,
  443. SCORE_BOTH,
  444. true
  445. );
  446. $type = $item->get_item_type();
  447. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  448. $display = ExerciseLib::show_score($score[0], $score[1], false);
  449. }
  450. return [
  451. 'display' => $display,
  452. 'score' => $score,
  453. ];
  454. }
  455. /**
  456. * @param GradebookItem $item
  457. *
  458. * @return array
  459. */
  460. public function buildAverageResultColumn(GradebookItem $item)
  461. {
  462. $score = $item->calc_score(null, 'average');
  463. $scoreDisplay = ScoreDisplay::instance();
  464. $scoreMode = SCORE_DIV_PERCENT_WITH_CUSTOM;
  465. if ($this->hidePercentage) {
  466. $scoreMode = SCORE_DIV;
  467. }
  468. $display = $scoreDisplay->display_score(
  469. $score,
  470. $scoreMode,
  471. SCORE_BOTH,
  472. true
  473. );
  474. $type = $item->get_item_type();
  475. if ($type === 'L' && get_class($item) === 'ExerciseLink') {
  476. $display = ExerciseLib::show_score($score[0], $score[1], false);
  477. $result = ExerciseLib::convertScoreToPlatformSetting($score[0], $score[1]);
  478. $score[0] = $result['score'];
  479. $score[1] = $result['weight'];
  480. }
  481. return [
  482. 'display' => $display,
  483. 'score' => $score,
  484. ];
  485. }
  486. /**
  487. * @param GradebookItem $item
  488. * @param int $userId
  489. * @param int $userCount
  490. *
  491. * @return array
  492. */
  493. public function buildRankingColumn(GradebookItem $item, $userId = null, $userCount = 0)
  494. {
  495. $score = $item->calc_score($userId, 'ranking');
  496. $score[1] = $userCount;
  497. $scoreDisplay = null;
  498. if (isset($score[0])) {
  499. $scoreDisplay = ScoreDisplay::instance();
  500. $scoreDisplay = $scoreDisplay->display_score(
  501. $score,
  502. SCORE_DIV,
  503. SCORE_BOTH,
  504. false,
  505. true
  506. );
  507. }
  508. return [
  509. 'display' => $scoreDisplay,
  510. 'score' => $score,
  511. ];
  512. }
  513. /**
  514. * @param int $userId
  515. * @param GradebookItem $item
  516. * @param bool $ignore_score_color
  517. *
  518. * @return string|null
  519. */
  520. public function build_result_column(
  521. $userId,
  522. $item,
  523. $ignore_score_color,
  524. $forceSimpleResult = false
  525. ) {
  526. $scoreDisplay = ScoreDisplay::instance();
  527. $score = $item->calc_score($userId);
  528. if (!empty($score)) {
  529. switch ($item->get_item_type()) {
  530. // category
  531. case 'C':
  532. if ($score != null) {
  533. if ($forceSimpleResult) {
  534. return [
  535. 'display' => $scoreDisplay->display_score(
  536. $score,
  537. SCORE_DIV
  538. ),
  539. 'score' => $score,
  540. 'score_weight' => $score,
  541. ];
  542. }
  543. return [
  544. 'display' => $scoreDisplay->display_score(
  545. $score,
  546. SCORE_DIV
  547. ),
  548. 'score' => $score,
  549. 'score_weight' => $score,
  550. ];
  551. } else {
  552. return [
  553. 'display' => null,
  554. 'score' => $score,
  555. 'score_weight' => $score,
  556. ];
  557. }
  558. break;
  559. case 'E':
  560. case 'L':
  561. //if ($parentId == 0) {
  562. $scoreWeight = [
  563. ($score[1] > 0) ? $score[0] / $score[1] * $item->get_weight() : 0,
  564. $item->get_weight(),
  565. ];
  566. //}
  567. $display = $scoreDisplay->display_score(
  568. $score,
  569. SCORE_DIV_PERCENT_WITH_CUSTOM
  570. );
  571. $type = $item->get_item_type();
  572. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  573. $display = ExerciseLib::show_score(
  574. $score[0],
  575. $score[1],
  576. false
  577. );
  578. }
  579. return [
  580. 'display' => $display,
  581. 'score' => $score,
  582. 'score_weight' => $scoreWeight,
  583. ];
  584. }
  585. }
  586. return [
  587. 'display' => null,
  588. 'score' => null,
  589. 'score_weight' => null,
  590. ];
  591. }
  592. /**
  593. * @param GradebookItem $item
  594. *
  595. * @return string
  596. */
  597. private function build_date_column($item)
  598. {
  599. $date = $item->get_date();
  600. if (!isset($date) || empty($date)) {
  601. return '';
  602. } else {
  603. if (is_int($date)) {
  604. return api_convert_and_format_date($date);
  605. } else {
  606. return api_format_date($date);
  607. }
  608. }
  609. }
  610. }