results_data_generator.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class to select, sort and transform object data into array data,
  5. * used for the teacher's evaluation results view
  6. * @author Bert Steppé
  7. * @package chamilo.gradebook
  8. */
  9. /**
  10. * Class
  11. * @package chamilo.gradebook
  12. */
  13. class ResultsDataGenerator
  14. {
  15. // Sorting types constants
  16. const RDG_SORT_LASTNAME = 1;
  17. const RDG_SORT_FIRSTNAME = 2;
  18. const RDG_SORT_SCORE = 4;
  19. const RDG_SORT_MASK = 8;
  20. const RDG_SORT_ASC = 16;
  21. const RDG_SORT_DESC = 32;
  22. private $evaluation;
  23. private $results;
  24. private $is_course_ind;
  25. private $include_edit;
  26. /**
  27. * Constructor
  28. */
  29. function ResultsDataGenerator ( $evaluation,
  30. $results = array(),
  31. $include_edit = false) {
  32. $this->evaluation = $evaluation;
  33. $this->results = (isset($results) ? $results : array());
  34. }
  35. /**
  36. * Get total number of results (rows)
  37. */
  38. public function get_total_results_count () {
  39. return count($this->results);
  40. }
  41. /**
  42. * Get actual array data
  43. * @return array 2-dimensional array - each array contains the elements:
  44. * 0 ['id'] : user id
  45. * 1 ['result_id'] : result id
  46. * 2 ['lastname'] : user lastname
  47. * 3 ['firstname'] : user firstname
  48. * 4 ['score'] : student's score
  49. * 5 ['display'] : custom score display (only if custom scoring enabled)
  50. */
  51. public function get_data ($sorting = 0, $start = 0, $count = null, $ignore_score_color = false, $pdf=false) {
  52. // do some checks on count, redefine if invalid value
  53. $number_decimals = api_get_setting('gradebook_number_decimals');
  54. if (!isset($count)) {
  55. $count = count ($this->results) - $start;
  56. }
  57. if ($count < 0) {
  58. $count = 0;
  59. }
  60. $scoredisplay = ScoreDisplay :: instance();
  61. // generate actual data array
  62. $table = array();
  63. foreach($this->results as $result) {
  64. $user = array();
  65. $info = api_get_user_info($result->get_user_id());
  66. $user['id'] = $result->get_user_id();
  67. if ($pdf){
  68. $user['username'] = $info['username'];
  69. }
  70. $user['result_id'] = $result->get_id();
  71. $user['lastname'] = $info['lastname'];
  72. $user['firstname'] = $info['firstname'];
  73. if ($pdf) {
  74. $user['score'] = $result->get_score();
  75. } else {
  76. $user['score'] = $this->get_score_display($result->get_score(),true, $ignore_score_color);
  77. }
  78. $user['percentage_score'] = intval($scoredisplay->display_score(array($result->get_score(), $this->evaluation->get_max()), SCORE_PERCENT, SCORE_BOTH, true));
  79. if ($pdf && $number_decimals == null){
  80. $user['scoreletter'] = $result->get_score();
  81. }
  82. if ($scoredisplay->is_custom()) {
  83. $user['display'] = $this->get_score_display($result->get_score(), false, $ignore_score_color);
  84. }
  85. $table[] = $user;
  86. }
  87. // sort array
  88. if ($sorting & self :: RDG_SORT_LASTNAME) {
  89. usort($table, array('ResultsDataGenerator', 'sort_by_last_name'));
  90. } elseif ($sorting & self :: RDG_SORT_FIRSTNAME) {
  91. usort($table, array('ResultsDataGenerator', 'sort_by_first_name'));
  92. } elseif ($sorting & self :: RDG_SORT_SCORE) {
  93. usort($table, array('ResultsDataGenerator', 'sort_by_score'));
  94. } elseif ($sorting & self :: RDG_SORT_MASK) {
  95. usort($table, array('ResultsDataGenerator', 'sort_by_mask'));
  96. }
  97. if ($sorting & self :: RDG_SORT_DESC) {
  98. $table = array_reverse($table);
  99. }
  100. $return = array_slice($table, $start, $count);
  101. return $return;
  102. }
  103. /**
  104. * Re-formats the score to show percentage ("2/4 (50 %)") or letters ("A")
  105. * @param float Current absolute score (max score is taken from $this->evaluation->get_max()
  106. * @param bool Whether we want the real score (2/4 (50 %)) or the transformation (A, B, C, etc)
  107. * @param bool Whether we want to ignore the score color
  108. * @result string The score as we want to show it
  109. */
  110. private function get_score_display ($score, $realscore, $ignore_score_color = false) {
  111. if ($score != null) {
  112. $scoredisplay = ScoreDisplay :: instance();
  113. $type = SCORE_CUSTOM;
  114. if ($realscore === true) {
  115. $type = SCORE_DIV_PERCENT ;
  116. }
  117. return $scoredisplay->display_score(array($score, $this->evaluation->get_max()), $type, SCORE_BOTH, $ignore_score_color);
  118. }
  119. return '';
  120. }
  121. // Sort functions - used internally
  122. function sort_by_last_name($item1, $item2) {
  123. return api_strcmp($item1['lastname'], $item2['lastname']);
  124. }
  125. function sort_by_first_name($item1, $item2) {
  126. return api_strcmp($item1['firstname'], $item2['firstname']);
  127. }
  128. function sort_by_score($item1, $item2) {
  129. if ($item1['percentage_score'] == $item2['percentage_score']) {
  130. return 0;
  131. } else {
  132. return ($item1['percentage_score'] < $item2['percentage_score'] ? -1 : 1);
  133. }
  134. }
  135. function sort_by_mask ($item1, $item2) {
  136. $score1 = (isset($item1['score']) ? array($item1['score'],$this->evaluation->get_max()) : null);
  137. $score2 = (isset($item2['score']) ? array($item2['score'],$this->evaluation->get_max()) : null);
  138. return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);
  139. }
  140. }