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. * @param integer $count
  44. * @return array 2-dimensional array - each array contains the elements:
  45. * 0 ['id'] : user id
  46. * 1 ['result_id'] : result id
  47. * 2 ['lastname'] : user lastname
  48. * 3 ['firstname'] : user firstname
  49. * 4 ['score'] : student's score
  50. * 5 ['display'] : custom score display (only if custom scoring enabled)
  51. */
  52. public function get_data($sorting = 0, $start = 0, $count = null, $ignore_score_color = false, $pdf = false)
  53. {
  54. // do some checks on count, redefine if invalid value
  55. $number_decimals = api_get_setting('gradebook_number_decimals');
  56. if (!isset($count)) {
  57. $count = count($this->results) - $start;
  58. }
  59. if ($count < 0) {
  60. $count = 0;
  61. }
  62. $scoredisplay = ScoreDisplay::instance();
  63. // generate actual data array
  64. $table = array();
  65. foreach ($this->results as $result) {
  66. $user = array();
  67. $info = api_get_user_info($result->get_user_id());
  68. $user['id'] = $result->get_user_id();
  69. if ($pdf) {
  70. $user['username'] = $info['username'];
  71. }
  72. $user['result_id'] = $result->get_id();
  73. $user['lastname'] = $info['lastname'];
  74. $user['firstname'] = $info['firstname'];
  75. if ($pdf) {
  76. $user['score'] = $result->get_score();
  77. } else {
  78. $user['score'] = $this->get_score_display(
  79. $result->get_score(),
  80. true,
  81. $ignore_score_color
  82. );
  83. }
  84. $user['percentage_score'] = intval($scoredisplay->display_score(
  85. array($result->get_score(), $this->evaluation->get_max()),
  86. SCORE_PERCENT,
  87. SCORE_BOTH,
  88. true
  89. )
  90. );
  91. if ($pdf && $number_decimals == null) {
  92. $user['scoreletter'] = $result->get_score();
  93. }
  94. if ($scoredisplay->is_custom()) {
  95. $user['display'] = $this->get_score_display(
  96. $result->get_score(),
  97. false,
  98. $ignore_score_color
  99. );
  100. }
  101. $table[] = $user;
  102. }
  103. // sort array
  104. if ($sorting & self::RDG_SORT_LASTNAME) {
  105. usort($table, array('ResultsDataGenerator', 'sort_by_last_name'));
  106. } elseif ($sorting & self::RDG_SORT_FIRSTNAME) {
  107. usort($table, array('ResultsDataGenerator', 'sort_by_first_name'));
  108. } elseif ($sorting & self::RDG_SORT_SCORE) {
  109. usort($table, array('ResultsDataGenerator', 'sort_by_score'));
  110. } elseif ($sorting & self::RDG_SORT_MASK) {
  111. usort($table, array('ResultsDataGenerator', 'sort_by_mask'));
  112. }
  113. if ($sorting & self::RDG_SORT_DESC) {
  114. $table = array_reverse($table);
  115. }
  116. $return = array_slice($table, $start, $count);
  117. return $return;
  118. }
  119. /**
  120. * Re-formats the score to show percentage ("2/4 (50 %)") or letters ("A")
  121. * @param float Current absolute score (max score is taken from $this->evaluation->get_max()
  122. * @param bool Whether we want the real score (2/4 (50 %)) or the transformation (A, B, C, etc)
  123. * @param bool Whether we want to ignore the score color
  124. * @param boolean $realscore
  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. }