gradebook_data_generator.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. //$status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  84. // do some checks on count, redefine if invalid value
  85. if (!isset($count)) {
  86. $count = count ($this->items) - $start;
  87. }
  88. if ($count < 0) {
  89. $count = 0;
  90. }
  91. $allitems = $this->items;
  92. /*
  93. // sort array
  94. if ($sorting & self :: GDG_SORT_TYPE) {
  95. usort($allitems, array('GradebookDataGenerator', 'sort_by_type'));
  96. } elseif ($sorting & self :: GDG_SORT_ID) {
  97. usort($allitems, array('GradebookDataGenerator', 'sort_by_id'));
  98. } elseif ($sorting & self :: GDG_SORT_NAME) {
  99. usort($allitems, array('GradebookDataGenerator', 'sort_by_name'));
  100. } elseif ($sorting & self :: GDG_SORT_DESCRIPTION) {
  101. usort($allitems, array('GradebookDataGenerator', 'sort_by_description'));
  102. } elseif ($sorting & self :: GDG_SORT_WEIGHT) {
  103. usort($allitems, array('GradebookDataGenerator', 'sort_by_weight'));
  104. } elseif ($sorting & self :: GDG_SORT_DATE) {
  105. //usort($allitems, array('GradebookDataGenerator', 'sort_by_date'));
  106. }
  107. if ($sorting & self :: GDG_SORT_DESC) {
  108. $allitems = array_reverse($allitems);
  109. }*/
  110. usort($allitems, array('GradebookDataGenerator', 'sort_by_name'));
  111. $userId = $this->userId;
  112. // Get selected items
  113. $visibleitems = array_slice($allitems, $start, $count);
  114. $course_code = api_get_course_id();
  115. $sessionId = api_get_session_id();
  116. $status_user = api_get_status_of_user_in_course(
  117. api_get_user_id(),
  118. api_get_course_int_id()
  119. );
  120. $userCount = count($studentList);
  121. // Generate the data to display
  122. $data = array();
  123. /** @var GradebookItem $item */
  124. $totalWeight = 0;
  125. foreach ($visibleitems as $item) {
  126. $row = array();
  127. $row[] = $item;
  128. $row[] = $item->get_name();
  129. // display the 2 first line of description, and all description on mouseover (https://support.chamilo.org/issues/6588)
  130. $row[] = '<span title="'.api_remove_tags_with_space($item->get_description()).'">'.
  131. api_get_short_text_from_html($item->get_description(), 160).'</span>';
  132. $totalWeight += $item->get_weight();
  133. $row[] = $item->get_weight();
  134. $item->setStudentList($studentList);
  135. //if (count($this->evals_links) > 0) {
  136. if (get_class($item) == 'Evaluation') {
  137. // Items inside a category.
  138. if (1) {
  139. $resultColumn = $this->build_result_column(
  140. $userId,
  141. $item,
  142. $ignore_score_color
  143. );
  144. $row[] = $resultColumn['display'];
  145. $row['result_score'] = $resultColumn['score'];
  146. $row['result_score_weight'] = $resultColumn['score_weight'];
  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. $ranking = $this->buildRankingColumn($item, $userId, $userCount);
  157. $row['ranking'] = $ranking['display'];
  158. $row['ranking_score'] = $ranking['score'];
  159. $row[] = $item;
  160. }
  161. } else {
  162. // Category.
  163. $result = $this->build_result_column($userId, $item, $ignore_score_color, true);
  164. $row[] = $result['display'];
  165. $row['result_score'] = $result['score'];
  166. $row['result_score_weight'] = $result['score'];
  167. // Best
  168. $best = $this->buildBestResultColumn($item);
  169. $row['best'] = $best['display'];
  170. $row['best_score'] = $best['score'];
  171. // Average
  172. $average = $this->buildAverageResultColumn($item);
  173. $row['average'] = $average['display'];
  174. $row['average_score'] = $average['score'];
  175. // Ranking
  176. $rankingStudentList = array();
  177. $invalidateResults = true;
  178. foreach ($studentList as $user) {
  179. $score = $this->build_result_column(
  180. $user['user_id'],
  181. $item,
  182. $ignore_score_color,
  183. true
  184. );
  185. if (!empty($score['score'][0])) {
  186. $invalidateResults = false;
  187. }
  188. $rankingStudentList[$user['user_id']] = $score['score'][0];
  189. }
  190. $scoreDisplay = ScoreDisplay::instance();
  191. $score = AbstractLink::getCurrentUserRanking($userId, $rankingStudentList);
  192. $row['ranking'] = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH, true);
  193. if ($invalidateResults) {
  194. $row['ranking'] = null;
  195. }
  196. }
  197. $data[] = $row;
  198. }
  199. return $data;
  200. }
  201. /**
  202. * Get best result of an item
  203. * @param GradebookItem $item
  204. * @return string
  205. */
  206. private function buildBestResultColumn(GradebookItem $item)
  207. {
  208. $score = $item->calc_score(
  209. null,
  210. 'best',
  211. api_get_course_id(),
  212. api_get_session_id()
  213. );
  214. $scoreDisplay = ScoreDisplay :: instance();
  215. $display = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH, true);
  216. $type = $item->get_item_type();
  217. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  218. $display = ExerciseLib::show_score($score[0], $score[1], false);
  219. }
  220. return array(
  221. 'display' => $display,
  222. 'score' => $score
  223. );
  224. }
  225. /**
  226. * @param GradebookItem $item
  227. *
  228. * @return string
  229. */
  230. private function buildAverageResultColumn(GradebookItem $item)
  231. {
  232. $score = $item->calc_score(null, 'average');
  233. $scoreDisplay = ScoreDisplay :: instance();
  234. $display = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH, true);
  235. $type = $item->get_item_type();
  236. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  237. $display = ExerciseLib::show_score($score[0], $score[1], false);
  238. }
  239. return array(
  240. 'display' => $display,
  241. 'score' => $score
  242. );
  243. }
  244. /**
  245. * @param GradebookItem $item
  246. * @param int $userId
  247. * @param int $userCount
  248. *
  249. * @return string
  250. */
  251. private function buildRankingColumn(GradebookItem $item, $userId = null, $userCount = 0)
  252. {
  253. $score = $item->calc_score($userId, 'ranking');
  254. $score[1] = $userCount;
  255. $scoreDisplay = null;
  256. if (isset($score[0])) {
  257. $scoreDisplay = ScoreDisplay::instance();
  258. $scoreDisplay = $scoreDisplay->display_score($score, SCORE_DIV, SCORE_BOTH, true);
  259. }
  260. return array(
  261. 'display' => $scoreDisplay,
  262. 'score' => $score
  263. );
  264. }
  265. /**
  266. * @param int $userId
  267. * @param GradebookItem $item
  268. * @param boolean $ignore_score_color
  269. * @return null|string
  270. */
  271. private function build_result_column(
  272. $userId,
  273. $item,
  274. $ignore_score_color,
  275. $forceSimpleResult = false
  276. ) {
  277. $scoredisplay = ScoreDisplay::instance();
  278. $score = $item->calc_score($userId);
  279. if (!empty($score)) {
  280. switch ($item->get_item_type()) {
  281. // category
  282. case 'C' :
  283. if ($score != null) {
  284. if ($forceSimpleResult) {
  285. return
  286. array(
  287. 'display' => $scoredisplay->display_score(
  288. $score,
  289. SCORE_DIV
  290. ),
  291. 'score' => $score,
  292. 'score_weight' => $score
  293. );
  294. }
  295. return array(
  296. 'display' => $scoredisplay->display_score($score, SCORE_DIV),
  297. 'score' => $score,
  298. 'score_weight' => $score
  299. );
  300. } else {
  301. return array(
  302. 'display' => null,
  303. 'score' => $score,
  304. 'score_weight' => $score
  305. );
  306. }
  307. break;
  308. // evaluation and link
  309. case 'E' :
  310. case 'L' :
  311. //if ($parentId == 0) {
  312. $scoreWeight = [
  313. ($score[1] > 0) ? $score[0] / $score[1] * $item->get_weight() : 0,
  314. $item->get_weight()
  315. ];
  316. //}
  317. $display = $scoredisplay->display_score($score, SCORE_DIV);
  318. $type = $item->get_item_type();
  319. if ($type == 'L' && get_class($item) == 'ExerciseLink') {
  320. $display = ExerciseLib::show_score($score[0], $score[1], false);
  321. }
  322. return array(
  323. 'display' => $display,
  324. 'score' => $score,
  325. 'score_weight' => $scoreWeight,
  326. );
  327. }
  328. }
  329. return array(
  330. 'display' => null,
  331. 'score' => null,
  332. 'score_weight' => null
  333. );
  334. }
  335. /**
  336. * @param GradebookItem $item
  337. * @return string
  338. */
  339. private function build_date_column($item)
  340. {
  341. $date = $item->get_date();
  342. if (!isset($date) || empty($date)) {
  343. return '';
  344. } else {
  345. if (is_int($date)) {
  346. return api_convert_and_format_date($date);
  347. } else {
  348. return api_format_date($date);
  349. }
  350. }
  351. }
  352. /**
  353. * Returns the link to the certificate generation, if the score is enough, otherwise
  354. * returns an empty string. This only works with categories.
  355. * @param object Item
  356. */
  357. public function get_certificate_link($item)
  358. {
  359. if (is_a($item, 'Category')) {
  360. if ($item->is_certificate_available(api_get_user_id())) {
  361. $link = '<a href="'.Security::remove_XSS($_SESSION['gradebook_dest']).'?export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.
  362. get_lang('Certificate').'</a>';
  363. return $link;
  364. }
  365. }
  366. return '';
  367. }
  368. /**
  369. * @param GradebookItem $item1
  370. * @param GradebookItem $item2
  371. * @return int
  372. */
  373. public function sort_by_name($item1, $item2)
  374. {
  375. return api_strnatcmp($item1->get_name(), $item2->get_name());
  376. }
  377. /**
  378. * @param GradebookItem $item1
  379. * @param GradebookItem $item2
  380. * @return int
  381. */
  382. public function sort_by_id($item1, $item2)
  383. {
  384. return api_strnatcmp($item1->get_id(), $item2->get_id());
  385. }
  386. /**
  387. * @param GradebookItem $item1
  388. * @param GradebookItem $item2
  389. * @return int
  390. */
  391. public function sort_by_type($item1, $item2)
  392. {
  393. if ($item1->get_item_type() == $item2->get_item_type()) {
  394. return $this->sort_by_name($item1,$item2);
  395. } else {
  396. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  397. }
  398. }
  399. /**
  400. * @param GradebookItem $item1
  401. * @param GradebookItem $item2
  402. * @return int
  403. */
  404. public function sort_by_description($item1, $item2)
  405. {
  406. $result = api_strcmp($item1->get_description(), $item2->get_description());
  407. if ($result == 0) {
  408. return $this->sort_by_name($item1,$item2);
  409. }
  410. return $result;
  411. }
  412. /**
  413. * @param GradebookItem $item1
  414. * @param GradebookItem $item2
  415. * @return int
  416. */
  417. public function sort_by_weight($item1, $item2)
  418. {
  419. if ($item1->get_weight() == $item2->get_weight()) {
  420. return $this->sort_by_name($item1,$item2);
  421. } else {
  422. return ($item1->get_weight() < $item2->get_weight() ? -1 : 1);
  423. }
  424. }
  425. /**
  426. * @param GradebookItem $item1
  427. * @param GradebookItem $item2
  428. * @return int
  429. */
  430. public function sort_by_date($item1, $item2)
  431. {
  432. if (is_int($item1->get_date())) {
  433. $timestamp1 = $item1->get_date();
  434. } else {
  435. $date = $item1->get_date();
  436. if (!empty($date)) {
  437. $timestamp1 = api_strtotime($date, 'UTC');
  438. } else {
  439. $timestamp1 = null;
  440. }
  441. }
  442. if (is_int($item2->get_date())) {
  443. $timestamp2 = $item2->get_date();
  444. } else {
  445. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  446. }
  447. if ($timestamp1 == $timestamp2) {
  448. return $this->sort_by_name($item1,$item2);
  449. } else {
  450. return ($timestamp1 < $timestamp2 ? -1 : 1);
  451. }
  452. }
  453. }