gradebook_data_generator.class.php 16 KB

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