gradebook_data_generator.class.php 15 KB

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