gradebook_data_generator.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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(
  139. $userId,
  140. $item,
  141. $ignore_score_color,
  142. true
  143. );
  144. $row[] = $result['display'];
  145. $row['result_score'] = $result['score'];
  146. $row['result_score_weight'] = $result['score'];
  147. // Best
  148. $best = $this->buildBestResultColumn($item);
  149. $row['best'] = $best['display'];
  150. $row['best_score'] = $best['score'];
  151. // Average
  152. $average = $this->buildAverageResultColumn($item);
  153. $row['average'] = $average['display'];
  154. $row['average_score'] = $average['score'];
  155. // Ranking
  156. $rankingStudentList = array();
  157. $invalidateResults = true;
  158. foreach ($studentList as $user) {
  159. $score = $this->build_result_column(
  160. $user['user_id'],
  161. $item,
  162. $ignore_score_color,
  163. true
  164. );
  165. if (!empty($score['score'][0])) {
  166. $invalidateResults = false;
  167. }
  168. $rankingStudentList[$user['user_id']] = $score['score'][0];
  169. }
  170. $scoreDisplay = ScoreDisplay::instance();
  171. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  172. $row['ranking'] = $scoreDisplay->display_score(
  173. $score,
  174. SCORE_DIV,
  175. SCORE_BOTH,
  176. true
  177. );
  178. if ($invalidateResults) {
  179. $row['ranking'] = null;
  180. }
  181. }
  182. $data[] = $row;
  183. }
  184. return $data;
  185. }
  186. /**
  187. * Get best result of an item
  188. * @param GradebookItem $item
  189. * @return string
  190. */
  191. private function buildBestResultColumn(GradebookItem $item)
  192. {
  193. $score = $item->calc_score(
  194. null,
  195. 'best',
  196. api_get_course_id(),
  197. api_get_session_id()
  198. );
  199. $scoreDisplay = ScoreDisplay::instance();
  200. $display = $scoreDisplay->display_score(
  201. $score,
  202. SCORE_DIV_PERCENT_WITH_CUSTOM,
  203. SCORE_BOTH,
  204. true
  205. );
  206. $type = $item->get_item_type();
  207. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  208. $display = ExerciseLib::show_score($score[0], $score[1], false);
  209. }
  210. return array(
  211. 'display' => $display,
  212. 'score' => $score
  213. );
  214. }
  215. /**
  216. * @param GradebookItem $item
  217. *
  218. * @return string
  219. */
  220. private function buildAverageResultColumn(GradebookItem $item)
  221. {
  222. $score = $item->calc_score(null, 'average');
  223. $scoreDisplay = ScoreDisplay::instance();
  224. $display = $scoreDisplay->display_score(
  225. $score,
  226. SCORE_DIV_PERCENT_WITH_CUSTOM,
  227. SCORE_BOTH,
  228. true
  229. );
  230. $type = $item->get_item_type();
  231. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  232. $display = ExerciseLib::show_score($score[0], $score[1], false);
  233. }
  234. return array(
  235. 'display' => $display,
  236. 'score' => $score
  237. );
  238. }
  239. /**
  240. * @param GradebookItem $item
  241. * @param int $userId
  242. * @param int $userCount
  243. *
  244. * @return string
  245. */
  246. private function buildRankingColumn(GradebookItem $item, $userId = null, $userCount = 0)
  247. {
  248. $score = $item->calc_score($userId, 'ranking');
  249. $score[1] = $userCount;
  250. $scoreDisplay = null;
  251. if (isset($score[0])) {
  252. $scoreDisplay = ScoreDisplay::instance();
  253. $scoreDisplay = $scoreDisplay->display_score(
  254. $score,
  255. SCORE_DIV,
  256. SCORE_BOTH
  257. );
  258. }
  259. return array(
  260. 'display' => $scoreDisplay,
  261. 'score' => $score
  262. );
  263. }
  264. /**
  265. * @param int $userId
  266. * @param GradebookItem $item
  267. * @param boolean $ignore_score_color
  268. * @return null|string
  269. */
  270. private function build_result_column(
  271. $userId,
  272. $item,
  273. $ignore_score_color,
  274. $forceSimpleResult = false
  275. ) {
  276. $scoreDisplay = ScoreDisplay::instance();
  277. $score = $item->calc_score($userId);
  278. if (!empty($score)) {
  279. switch ($item->get_item_type()) {
  280. // category
  281. case 'C':
  282. if ($score != null) {
  283. if ($forceSimpleResult) {
  284. return array(
  285. 'display' => $scoreDisplay->display_score(
  286. $score,
  287. SCORE_DIV
  288. ),
  289. 'score' => $score,
  290. 'score_weight' => $score
  291. );
  292. }
  293. return array(
  294. 'display' => $scoreDisplay->display_score(
  295. $score,
  296. SCORE_DIV
  297. ),
  298. 'score' => $score,
  299. 'score_weight' => $score
  300. );
  301. } else {
  302. return array(
  303. 'display' => null,
  304. 'score' => $score,
  305. 'score_weight' => $score
  306. );
  307. }
  308. break;
  309. // evaluation and link
  310. case 'E':
  311. case 'L':
  312. //if ($parentId == 0) {
  313. $scoreWeight = [
  314. ($score[1] > 0) ? $score[0] / $score[1] * $item->get_weight() : 0,
  315. $item->get_weight()
  316. ];
  317. //}
  318. $display = $scoreDisplay->display_score(
  319. $score,
  320. SCORE_DIV_PERCENT_WITH_CUSTOM
  321. );
  322. $type = $item->get_item_type();
  323. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  324. $display = ExerciseLib::show_score(
  325. $score[0],
  326. $score[1],
  327. false
  328. );
  329. }
  330. return array(
  331. 'display' => $display,
  332. 'score' => $score,
  333. 'score_weight' => $scoreWeight,
  334. );
  335. }
  336. }
  337. return array(
  338. 'display' => null,
  339. 'score' => null,
  340. 'score_weight' => null
  341. );
  342. }
  343. /**
  344. * @param GradebookItem $item
  345. * @return string
  346. */
  347. private function build_date_column($item)
  348. {
  349. $date = $item->get_date();
  350. if (!isset($date) || empty($date)) {
  351. return '';
  352. } else {
  353. if (is_int($date)) {
  354. return api_convert_and_format_date($date);
  355. } else {
  356. return api_format_date($date);
  357. }
  358. }
  359. }
  360. /**
  361. * Returns the link to the certificate generation, if the score is enough, otherwise
  362. * returns an empty string. This only works with categories.
  363. * @param object Item
  364. * @return string
  365. */
  366. public function get_certificate_link($item)
  367. {
  368. if (is_a($item, 'Category')) {
  369. if ($item->is_certificate_available(api_get_user_id())) {
  370. $link = '<a href="'.Category::getUrl().'export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.
  371. get_lang('Certificate').'</a>';
  372. return $link;
  373. }
  374. }
  375. return '';
  376. }
  377. /**
  378. * @param GradebookItem $item1
  379. * @param GradebookItem $item2
  380. * @return int
  381. */
  382. public function sort_by_name($item1, $item2)
  383. {
  384. return api_strnatcmp($item1->get_name(), $item2->get_name());
  385. }
  386. /**
  387. * @param GradebookItem $item1
  388. * @param GradebookItem $item2
  389. * @return int
  390. */
  391. public function sort_by_id($item1, $item2)
  392. {
  393. return api_strnatcmp($item1->get_id(), $item2->get_id());
  394. }
  395. /**
  396. * @param GradebookItem $item1
  397. * @param GradebookItem $item2
  398. * @return int
  399. */
  400. public function sort_by_type($item1, $item2)
  401. {
  402. if ($item1->get_item_type() == $item2->get_item_type()) {
  403. return $this->sort_by_name($item1, $item2);
  404. } else {
  405. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  406. }
  407. }
  408. /**
  409. * @param GradebookItem $item1
  410. * @param GradebookItem $item2
  411. * @return int
  412. */
  413. public function sort_by_description($item1, $item2)
  414. {
  415. $result = api_strcmp($item1->get_description(), $item2->get_description());
  416. if ($result == 0) {
  417. return $this->sort_by_name($item1, $item2);
  418. }
  419. return $result;
  420. }
  421. /**
  422. * @param GradebookItem $item1
  423. * @param GradebookItem $item2
  424. * @return int
  425. */
  426. public function sort_by_weight($item1, $item2)
  427. {
  428. if ($item1->get_weight() == $item2->get_weight()) {
  429. return $this->sort_by_name($item1, $item2);
  430. } else {
  431. return ($item1->get_weight() < $item2->get_weight() ? -1 : 1);
  432. }
  433. }
  434. /**
  435. * @param GradebookItem $item1
  436. * @param GradebookItem $item2
  437. * @return int
  438. */
  439. public function sort_by_date($item1, $item2)
  440. {
  441. if (is_int($item1->get_date())) {
  442. $timestamp1 = $item1->get_date();
  443. } else {
  444. $date = $item1->get_date();
  445. if (!empty($date)) {
  446. $timestamp1 = api_strtotime($date, 'UTC');
  447. } else {
  448. $timestamp1 = null;
  449. }
  450. }
  451. if (is_int($item2->get_date())) {
  452. $timestamp2 = $item2->get_date();
  453. } else {
  454. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  455. }
  456. if ($timestamp1 == $timestamp2) {
  457. return $this->sort_by_name($item1, $item2);
  458. } else {
  459. return ($timestamp1 < $timestamp2 ? -1 : 1);
  460. }
  461. }
  462. }