results_data_generator.class.php 6.7 KB

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