results_data_generator.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 __construct(
  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(
  55. 'gradebook.gradebook_number_decimals'
  56. );
  57. if (!isset($count)) {
  58. $count = count ($this->results) - $start;
  59. }
  60. if ($count < 0) {
  61. $count = 0;
  62. }
  63. $scoredisplay = ScoreDisplay :: instance();
  64. // generate actual data array
  65. $table = array();
  66. foreach($this->results as $result) {
  67. $user = array();
  68. $info = api_get_user_info($result->get_user_id());
  69. $user['id'] = $result->get_user_id();
  70. if ($pdf){
  71. $user['username'] = $info['username'];
  72. }
  73. $user['result_id'] = $result->get_id();
  74. $user['lastname'] = $info['lastname'];
  75. $user['firstname'] = $info['firstname'];
  76. if ($pdf) {
  77. $user['score'] = $result->get_score();
  78. } else {
  79. $user['score'] = $this->get_score_display(
  80. $result->get_score(),
  81. true,
  82. $ignore_score_color
  83. );
  84. }
  85. $user['percentage_score'] = intval($scoredisplay->display_score(
  86. array($result->get_score(), $this->evaluation->get_max()),
  87. SCORE_PERCENT,
  88. SCORE_BOTH,
  89. true
  90. )
  91. );
  92. if ($pdf && $number_decimals == null){
  93. $user['scoreletter'] = $result->get_score();
  94. }
  95. if ($scoredisplay->is_custom()) {
  96. $user['display'] = $this->get_score_display(
  97. $result->get_score(),
  98. false,
  99. $ignore_score_color
  100. );
  101. }
  102. $table[] = $user;
  103. }
  104. // sort array
  105. if ($sorting & self :: RDG_SORT_LASTNAME) {
  106. usort($table, array('ResultsDataGenerator', 'sort_by_last_name'));
  107. } elseif ($sorting & self :: RDG_SORT_FIRSTNAME) {
  108. usort($table, array('ResultsDataGenerator', 'sort_by_first_name'));
  109. } elseif ($sorting & self :: RDG_SORT_SCORE) {
  110. usort($table, array('ResultsDataGenerator', 'sort_by_score'));
  111. } elseif ($sorting & self :: RDG_SORT_MASK) {
  112. usort($table, array('ResultsDataGenerator', 'sort_by_mask'));
  113. }
  114. if ($sorting & self :: RDG_SORT_DESC) {
  115. $table = array_reverse($table);
  116. }
  117. $return = array_slice($table, $start, $count);
  118. return $return;
  119. }
  120. /**
  121. * Re-formats the score to show percentage ("2/4 (50 %)") or letters ("A")
  122. * @param float Current absolute score (max score is taken from $this->evaluation->get_max()
  123. * @param bool Whether we want the real score (2/4 (50 %)) or the transformation (A, B, C, etc)
  124. * @param bool Whether we want to ignore the score color
  125. * @result string The score as we want to show it
  126. */
  127. private function get_score_display ($score, $realscore, $ignore_score_color = false)
  128. {
  129. if ($score != null) {
  130. $scoredisplay = ScoreDisplay :: instance();
  131. $type = SCORE_CUSTOM;
  132. if ($realscore === true) {
  133. $type = SCORE_DIV_PERCENT ;
  134. }
  135. return $scoredisplay->display_score(
  136. array($score, $this->evaluation->get_max()),
  137. $type,
  138. SCORE_BOTH,
  139. $ignore_score_color
  140. );
  141. }
  142. return '';
  143. }
  144. // Sort functions - used internally
  145. function sort_by_last_name($item1, $item2)
  146. {
  147. return api_strcmp($item1['lastname'], $item2['lastname']);
  148. }
  149. function sort_by_first_name($item1, $item2)
  150. {
  151. return api_strcmp($item1['firstname'], $item2['firstname']);
  152. }
  153. function sort_by_score($item1, $item2)
  154. {
  155. if ($item1['percentage_score'] == $item2['percentage_score']) {
  156. return 0;
  157. } else {
  158. return ($item1['percentage_score'] < $item2['percentage_score'] ? -1 : 1);
  159. }
  160. }
  161. function sort_by_mask ($item1, $item2)
  162. {
  163. $score1 = (isset($item1['score']) ? array($item1['score'],$this->evaluation->get_max()) : null);
  164. $score2 = (isset($item2['score']) ? array($item2['score'],$this->evaluation->get_max()) : null);
  165. return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);
  166. }
  167. }