gradebook_data_generator.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. $model = ExerciseLib::getCourseScoreModel();
  111. /** @var GradebookItem $item */
  112. foreach ($visibleItems as $item) {
  113. $row = [];
  114. $row[] = $item;
  115. $row[] = $item->get_name();
  116. // display the 2 first line of description and all description
  117. // on mouseover (https://support.chamilo.org/issues/6588)
  118. $row[] = '<span title="'.api_remove_tags_with_space($item->get_description()).'">'.
  119. api_get_short_text_from_html($item->get_description(), 160).'</span>';
  120. $row[] = $item->get_weight();
  121. $item->setStudentList($studentList);
  122. $itemType = get_class($item);
  123. switch ($itemType) {
  124. case 'Evaluation':
  125. // Items inside a category.
  126. $resultColumn = $this->build_result_column(
  127. $userId,
  128. $item,
  129. $ignore_score_color
  130. );
  131. $row[] = $resultColumn['display'];
  132. $row['result_score'] = $resultColumn['score'];
  133. $row['result_score_weight'] = $resultColumn['score_weight'];
  134. // Best
  135. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  136. $best = $defaultData[$item->get_id()]['best'];
  137. } else {
  138. $best = $this->buildBestResultColumn($item);
  139. }
  140. if (empty($model)) {
  141. $row['best'] = $best['display'];
  142. $row['best_score'] = $best['score'];
  143. // Average
  144. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  145. $average = $defaultData[$item->get_id()]['average'];
  146. } else {
  147. $average = $this->buildBestResultColumn($item);
  148. }
  149. $row['average'] = $average['display'];
  150. $row['average_score'] = $average['score'];
  151. // Ranking
  152. $ranking = $this->buildRankingColumn($item, $userId, $userCount);
  153. $row['ranking'] = $ranking['display'];
  154. $row['ranking_score'] = $ranking['score'];
  155. }
  156. $row[] = $item;
  157. break;
  158. case 'ExerciseLink':
  159. /** @var ExerciseLink $item */
  160. // Category.
  161. $result = $this->build_result_column(
  162. $userId,
  163. $item,
  164. $ignore_score_color,
  165. true
  166. );
  167. $row[] = $result['display'];
  168. $row['result_score'] = $result['score'];
  169. $row['result_score_weight'] = $result['score'];
  170. if (empty($model)) {
  171. // Best
  172. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  173. $best = $defaultData[$item->get_id()]['best'];
  174. } else {
  175. $best = $this->buildBestResultColumn($item);
  176. }
  177. $row['best'] = $best['display'];
  178. $row['best_score'] = $best['score'];
  179. $rankingStudentList = [];
  180. $invalidateResults = false;
  181. // Average
  182. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  183. $average = $defaultData[$item->get_id()]['average'];
  184. } else {
  185. $average = $this->buildAverageResultColumn($item);
  186. }
  187. $row['average'] = $average['display'];
  188. $row['average_score'] = $average['score'];
  189. // Ranking
  190. if ($allowStats) {
  191. // Ranking
  192. if (isset($defaultData[$item->get_id()]) &&
  193. isset($defaultData[$item->get_id()]['ranking'])
  194. ) {
  195. $rankingStudentList = $defaultData[$item->get_id()]['ranking'];
  196. $invalidateResults = $defaultData[$item->get_id()]['ranking_invalidate'];
  197. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  198. } else {
  199. if (!empty($studentList)) {
  200. foreach ($studentList as $user) {
  201. $score = $this->build_result_column(
  202. $user['user_id'],
  203. $item,
  204. $ignore_score_color,
  205. true
  206. );
  207. if (!empty($score['score'][0])) {
  208. $invalidateResults = false;
  209. }
  210. $rankingStudentList[$user['user_id']] = $score['score'][0];
  211. }
  212. $defaultData[$item->get_id()]['ranking'] = $rankingStudentList;
  213. $defaultData[$item->get_id()]['ranking_invalidate'] = $invalidateResults;
  214. Session::write($this->preLoadDataKey, $defaultData);
  215. }
  216. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  217. }
  218. } else {
  219. if (!empty($studentList)) {
  220. foreach ($studentList as $user) {
  221. $score = $this->build_result_column(
  222. $user['user_id'],
  223. $item,
  224. $ignore_score_color,
  225. true
  226. );
  227. if (!empty($score['score'][0])) {
  228. $invalidateResults = false;
  229. }
  230. $rankingStudentList[$user['user_id']] = $score['score'][0];
  231. }
  232. }
  233. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  234. }
  235. $row['ranking'] = $scoreDisplay->display_score(
  236. $score,
  237. SCORE_DIV,
  238. SCORE_BOTH,
  239. true,
  240. true
  241. );
  242. if ($invalidateResults) {
  243. $row['ranking'] = null;
  244. }
  245. }
  246. break;
  247. default:
  248. // Category.
  249. $result = $this->build_result_column(
  250. $userId,
  251. $item,
  252. $ignore_score_color,
  253. true
  254. );
  255. $row[] = $result['display'];
  256. $row['result_score'] = $result['score'];
  257. $row['result_score_weight'] = $result['score'];
  258. if (empty($model)) {
  259. // Best
  260. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['best'])) {
  261. $best = $defaultData[$item->get_id()]['best'];
  262. } else {
  263. $best = $this->buildBestResultColumn($item);
  264. }
  265. $row['best'] = $best['display'];
  266. $row['best_score'] = $best['score'];
  267. $rankingStudentList = [];
  268. $invalidateResults = true;
  269. // Average
  270. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['average'])) {
  271. $average = $defaultData[$item->get_id()]['average'];
  272. } else {
  273. $average = $this->buildAverageResultColumn($item);
  274. }
  275. $row['average'] = $average['display'];
  276. $row['average_score'] = $average['score'];
  277. // Ranking
  278. if (isset($defaultData[$item->get_id()]) && isset($defaultData[$item->get_id()]['ranking'])) {
  279. $rankingStudentList = $defaultData[$item->get_id()]['ranking'];
  280. $invalidateResults = $defaultData[$item->get_id()]['ranking_invalidate'];
  281. $invalidateResults = false;
  282. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  283. } else {
  284. if (!empty($studentList)) {
  285. foreach ($studentList as $user) {
  286. $score = $this->build_result_column(
  287. $user['user_id'],
  288. $item,
  289. $ignore_score_color,
  290. true
  291. );
  292. if (!empty($score['score'][0])) {
  293. $invalidateResults = false;
  294. }
  295. $rankingStudentList[$user['user_id']] = $score['score'][0];
  296. }
  297. }
  298. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  299. }
  300. $row['ranking'] = $scoreDisplay->display_score(
  301. $score,
  302. SCORE_DIV,
  303. SCORE_BOTH,
  304. true,
  305. true
  306. );
  307. if ($invalidateResults) {
  308. $row['ranking'] = null;
  309. }
  310. }
  311. break;
  312. }
  313. $data[] = $row;
  314. }
  315. return $data;
  316. }
  317. /**
  318. * Returns the link to the certificate generation, if the score is enough, otherwise
  319. * returns an empty string. This only works with categories.
  320. *
  321. * @param object Item
  322. *
  323. * @return string
  324. */
  325. public function get_certificate_link($item)
  326. {
  327. if (is_a($item, 'Category')) {
  328. if ($item->is_certificate_available(api_get_user_id())) {
  329. $link = '<a href="'.Category::getUrl().'export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.
  330. get_lang('Certificate').'</a>';
  331. return $link;
  332. }
  333. }
  334. return '';
  335. }
  336. /**
  337. * @param GradebookItem $item1
  338. * @param GradebookItem $item2
  339. *
  340. * @return int
  341. */
  342. public static function sort_by_name($item1, $item2)
  343. {
  344. return api_strnatcmp($item1->get_name(), $item2->get_name());
  345. }
  346. /**
  347. * @param GradebookItem $item1
  348. * @param GradebookItem $item2
  349. *
  350. * @return int
  351. */
  352. public function sort_by_id($item1, $item2)
  353. {
  354. return api_strnatcmp($item1->get_id(), $item2->get_id());
  355. }
  356. /**
  357. * @param GradebookItem $item1
  358. * @param GradebookItem $item2
  359. *
  360. * @return int
  361. */
  362. public function sort_by_type($item1, $item2)
  363. {
  364. if ($item1->get_item_type() == $item2->get_item_type()) {
  365. return $this->sort_by_name($item1, $item2);
  366. } else {
  367. return $item1->get_item_type() < $item2->get_item_type() ? -1 : 1;
  368. }
  369. }
  370. /**
  371. * @param GradebookItem $item1
  372. * @param GradebookItem $item2
  373. *
  374. * @return int
  375. */
  376. public function sort_by_description($item1, $item2)
  377. {
  378. $result = api_strcmp($item1->get_description(), $item2->get_description());
  379. if ($result == 0) {
  380. return $this->sort_by_name($item1, $item2);
  381. }
  382. return $result;
  383. }
  384. /**
  385. * @param GradebookItem $item1
  386. * @param GradebookItem $item2
  387. *
  388. * @return int
  389. */
  390. public function sort_by_weight($item1, $item2)
  391. {
  392. if ($item1->get_weight() == $item2->get_weight()) {
  393. return $this->sort_by_name($item1, $item2);
  394. } else {
  395. return $item1->get_weight() < $item2->get_weight() ? -1 : 1;
  396. }
  397. }
  398. /**
  399. * @param GradebookItem $item1
  400. * @param GradebookItem $item2
  401. *
  402. * @return int
  403. */
  404. public function sort_by_date($item1, $item2)
  405. {
  406. if (is_int($item1->get_date())) {
  407. $timestamp1 = $item1->get_date();
  408. } else {
  409. $date = $item1->get_date();
  410. if (!empty($date)) {
  411. $timestamp1 = api_strtotime($date, 'UTC');
  412. } else {
  413. $timestamp1 = null;
  414. }
  415. }
  416. if (is_int($item2->get_date())) {
  417. $timestamp2 = $item2->get_date();
  418. } else {
  419. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  420. }
  421. if ($timestamp1 == $timestamp2) {
  422. return $this->sort_by_name($item1, $item2);
  423. } else {
  424. return $timestamp1 < $timestamp2 ? -1 : 1;
  425. }
  426. }
  427. /**
  428. * Get best result of an item.
  429. *
  430. * @param GradebookItem $item
  431. *
  432. * @return array
  433. */
  434. public function buildBestResultColumn(GradebookItem $item)
  435. {
  436. $score = $item->calc_score(
  437. null,
  438. 'best',
  439. api_get_course_id(),
  440. api_get_session_id()
  441. );
  442. $scoreMode = SCORE_DIV_PERCENT_WITH_CUSTOM;
  443. if ($this->hidePercentage) {
  444. $scoreMode = SCORE_DIV;
  445. }
  446. $scoreDisplay = ScoreDisplay::instance();
  447. $display = $scoreDisplay->display_score(
  448. $score,
  449. $scoreMode,
  450. SCORE_BOTH,
  451. true
  452. );
  453. $type = $item->get_item_type();
  454. if ($type == 'L' && get_class($item) === 'ExerciseLink') {
  455. $display = ExerciseLib::show_score($score[0], $score[1], false);
  456. }
  457. return [
  458. 'display' => $display,
  459. 'score' => $score,
  460. ];
  461. }
  462. /**
  463. * @param GradebookItem $item
  464. *
  465. * @return array
  466. */
  467. public function buildAverageResultColumn(GradebookItem $item)
  468. {
  469. $score = $item->calc_score(null, 'average');
  470. $scoreDisplay = ScoreDisplay::instance();
  471. $scoreMode = SCORE_DIV_PERCENT_WITH_CUSTOM;
  472. if ($this->hidePercentage) {
  473. $scoreMode = SCORE_DIV;
  474. }
  475. $display = $scoreDisplay->display_score(
  476. $score,
  477. $scoreMode,
  478. SCORE_BOTH,
  479. true
  480. );
  481. $type = $item->get_item_type();
  482. if ($type === 'L' && get_class($item) === 'ExerciseLink') {
  483. $display = ExerciseLib::show_score($score[0], $score[1], false);
  484. $result = ExerciseLib::convertScoreToPlatformSetting($score[0], $score[1]);
  485. $score[0] = $result['score'];
  486. $score[1] = $result['weight'];
  487. }
  488. return [
  489. 'display' => $display,
  490. 'score' => $score,
  491. ];
  492. }
  493. /**
  494. * @param GradebookItem $item
  495. * @param int $userId
  496. * @param int $userCount
  497. *
  498. * @return array
  499. */
  500. public function buildRankingColumn(GradebookItem $item, $userId = null, $userCount = 0)
  501. {
  502. $score = $item->calc_score($userId, 'ranking');
  503. $score[1] = $userCount;
  504. $scoreDisplay = null;
  505. if (isset($score[0])) {
  506. $scoreDisplay = ScoreDisplay::instance();
  507. $scoreDisplay = $scoreDisplay->display_score(
  508. $score,
  509. SCORE_DIV,
  510. SCORE_BOTH,
  511. false,
  512. true
  513. );
  514. }
  515. return [
  516. 'display' => $scoreDisplay,
  517. 'score' => $score,
  518. ];
  519. }
  520. /**
  521. * @param int $userId
  522. * @param GradebookItem $item
  523. * @param bool $ignore_score_color
  524. *
  525. * @return string|null
  526. */
  527. public function build_result_column(
  528. $userId,
  529. $item,
  530. $ignore_score_color,
  531. $forceSimpleResult = false
  532. ) {
  533. $scoreDisplay = ScoreDisplay::instance();
  534. $score = $item->calc_score($userId);
  535. $model = ExerciseLib::getCourseScoreModel();
  536. if (!empty($score)) {
  537. switch ($item->get_item_type()) {
  538. // category
  539. case 'C':
  540. if ($score != null) {
  541. if (empty($model)) {
  542. return [
  543. 'display' => $scoreDisplay->display_score(
  544. $score,
  545. SCORE_DIV
  546. ),
  547. 'score' => $score,
  548. 'score_weight' => $score,
  549. ];
  550. } else {
  551. $display = ExerciseLib::show_score(
  552. $score[0],
  553. $score[1],
  554. false
  555. );
  556. return [
  557. 'display' => $display,
  558. 'score' => $score,
  559. 'score_weight' => $score,
  560. ];
  561. }
  562. } else {
  563. return [
  564. 'display' => null,
  565. 'score' => $score,
  566. 'score_weight' => $score,
  567. ];
  568. }
  569. break;
  570. case 'E':
  571. case 'L':
  572. $scoreWeight = [
  573. ($score[1] > 0) ? $score[0] / $score[1] * $item->get_weight() : 0,
  574. $item->get_weight(),
  575. ];
  576. if (empty($model)) {
  577. $display = $scoreDisplay->display_score(
  578. $score,
  579. SCORE_DIV_PERCENT_WITH_CUSTOM
  580. );
  581. $type = $item->get_item_type();
  582. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  583. $display = ExerciseLib::show_score(
  584. $score[0],
  585. $score[1],
  586. false
  587. );
  588. }
  589. } else {
  590. $display = ExerciseLib::show_score(
  591. $score[0],
  592. $score[1],
  593. false
  594. );
  595. }
  596. return [
  597. 'display' => $display,
  598. 'score' => $score,
  599. 'score_weight' => $scoreWeight,
  600. ];
  601. }
  602. }
  603. return [
  604. 'display' => null,
  605. 'score' => null,
  606. 'score_weight' => null,
  607. ];
  608. }
  609. /**
  610. * @param GradebookItem $item
  611. *
  612. * @return string
  613. */
  614. private function build_date_column($item)
  615. {
  616. $date = $item->get_date();
  617. if (!isset($date) || empty($date)) {
  618. return '';
  619. } else {
  620. if (is_int($date)) {
  621. return api_convert_and_format_date($date);
  622. } else {
  623. return api_format_date($date);
  624. }
  625. }
  626. }
  627. }