results_data_generator.class.php 5.6 KB

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