results_data_generator.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. *
  8. * @author Bert Steppé
  9. *
  10. * @package chamilo.gradebook
  11. */
  12. class ResultsDataGenerator
  13. {
  14. // Sorting types constants
  15. const RDG_SORT_LASTNAME = 1;
  16. const RDG_SORT_FIRSTNAME = 2;
  17. const RDG_SORT_SCORE = 4;
  18. const RDG_SORT_MASK = 8;
  19. const RDG_SORT_ASC = 16;
  20. const RDG_SORT_DESC = 32;
  21. private $evaluation;
  22. private $results;
  23. private $is_course_ind;
  24. private $include_edit;
  25. /**
  26. * Constructor.
  27. */
  28. public function __construct(
  29. $evaluation,
  30. $results = [],
  31. $include_edit = false
  32. ) {
  33. $this->evaluation = $evaluation;
  34. $this->results = isset($results) ? $results : [];
  35. }
  36. /**
  37. * Get total number of results (rows).
  38. */
  39. public function get_total_results_count()
  40. {
  41. return count($this->results);
  42. }
  43. /**
  44. * Get actual array data.
  45. *
  46. * @param int $count
  47. *
  48. * @return array 2-dimensional array - each array contains the elements:
  49. * 0 ['id'] : user id
  50. * 1 ['result_id'] : result id
  51. * 2 ['lastname'] : user lastname
  52. * 3 ['firstname'] : user firstname
  53. * 4 ['score'] : student's score
  54. * 5 ['display'] : custom score display (only if custom scoring enabled)
  55. */
  56. public function get_data(
  57. $sorting = 0,
  58. $start = 0,
  59. $count = null,
  60. $ignore_score_color = false,
  61. $pdf = false
  62. ) {
  63. // do some checks on count, redefine if invalid value
  64. $number_decimals = api_get_setting('gradebook_number_decimals');
  65. if (!isset($count)) {
  66. $count = count($this->results) - $start;
  67. }
  68. if ($count < 0) {
  69. $count = 0;
  70. }
  71. $model = ExerciseLib::getCourseScoreModel();
  72. $scoreDisplay = ScoreDisplay::instance();
  73. // generate actual data array
  74. $table = [];
  75. foreach ($this->results as $result) {
  76. $user = [];
  77. $info = api_get_user_info($result->get_user_id());
  78. $user['id'] = $result->get_user_id();
  79. if ($pdf) {
  80. $user['username'] = $info['username'];
  81. }
  82. $user['result_id'] = $result->get_id();
  83. $user['lastname'] = $info['lastname'];
  84. $user['firstname'] = $info['firstname'];
  85. if ($pdf) {
  86. $user['score'] = $result->get_score();
  87. } else {
  88. $user['score'] = $this->get_score_display(
  89. $result->get_score(),
  90. true,
  91. $ignore_score_color
  92. );
  93. }
  94. $user['percentage_score'] = (int) $scoreDisplay->display_score(
  95. [$result->get_score(), $this->evaluation->get_max()],
  96. SCORE_PERCENT,
  97. SCORE_BOTH,
  98. true
  99. );
  100. if ($pdf && $number_decimals == null) {
  101. $user['scoreletter'] = $result->get_score();
  102. }
  103. if ($scoreDisplay->is_custom()) {
  104. $user['display'] = $this->get_score_display(
  105. $result->get_score(),
  106. false,
  107. $ignore_score_color
  108. );
  109. if (!empty($model)) {
  110. $user['display'] .= '&nbsp;'.
  111. ExerciseLib::show_score(
  112. $result->get_score(),
  113. $this->evaluation->get_max()
  114. )
  115. ;
  116. }
  117. }
  118. $table[] = $user;
  119. }
  120. // sort array
  121. if ($sorting & self::RDG_SORT_LASTNAME) {
  122. usort($table, ['ResultsDataGenerator', 'sort_by_last_name']);
  123. } elseif ($sorting & self::RDG_SORT_FIRSTNAME) {
  124. usort($table, ['ResultsDataGenerator', 'sort_by_first_name']);
  125. } elseif ($sorting & self::RDG_SORT_SCORE) {
  126. usort($table, ['ResultsDataGenerator', 'sort_by_score']);
  127. } elseif ($sorting & self::RDG_SORT_MASK) {
  128. usort($table, ['ResultsDataGenerator', 'sort_by_mask']);
  129. }
  130. if ($sorting & self::RDG_SORT_DESC) {
  131. $table = array_reverse($table);
  132. }
  133. $return = array_slice($table, $start, $count);
  134. return $return;
  135. }
  136. // Sort functions - used internally
  137. /**
  138. * @param array $item1
  139. * @param array $item2
  140. *
  141. * @return int
  142. */
  143. public function sort_by_last_name($item1, $item2)
  144. {
  145. return api_strcmp($item1['lastname'], $item2['lastname']);
  146. }
  147. /**
  148. * @param array $item1
  149. * @param array $item2
  150. *
  151. * @return int
  152. */
  153. public function sort_by_first_name($item1, $item2)
  154. {
  155. return api_strcmp($item1['firstname'], $item2['firstname']);
  156. }
  157. /**
  158. * @param array $item1
  159. * @param array $item2
  160. *
  161. * @return int
  162. */
  163. public function sort_by_score($item1, $item2)
  164. {
  165. if ($item1['percentage_score'] == $item2['percentage_score']) {
  166. return 0;
  167. } else {
  168. return $item1['percentage_score'] < $item2['percentage_score'] ? -1 : 1;
  169. }
  170. }
  171. /**
  172. * @param array $item1
  173. * @param array $item2
  174. *
  175. * @return int
  176. */
  177. public function sort_by_mask($item1, $item2)
  178. {
  179. $score1 = (isset($item1['score']) ? [$item1['score'], $this->evaluation->get_max()] : null);
  180. $score2 = (isset($item2['score']) ? [$item2['score'], $this->evaluation->get_max()] : null);
  181. return ScoreDisplay::compare_scores_by_custom_display($score1, $score2);
  182. }
  183. /**
  184. * Re-formats the score to show percentage ("2/4 (50 %)") or letters ("A").
  185. *
  186. * @param float Current absolute score (max score is taken from $this->evaluation->get_max()
  187. * @param bool Whether we want the real score (2/4 (50 %)) or the transformation (A, B, C, etc)
  188. * @param bool Whether we want to ignore the score color
  189. * @param bool $realscore
  190. *
  191. * @return string The score as we want to show it
  192. */
  193. private function get_score_display(
  194. $score,
  195. $realscore,
  196. $ignore_score_color = false
  197. ) {
  198. if ($score != null) {
  199. $scoreDisplay = ScoreDisplay::instance();
  200. $type = SCORE_CUSTOM;
  201. if ($realscore === true) {
  202. $type = SCORE_DIV_PERCENT;
  203. }
  204. return $scoreDisplay->display_score(
  205. [$score, $this->evaluation->get_max()],
  206. $type,
  207. SCORE_BOTH,
  208. $ignore_score_color
  209. );
  210. }
  211. return '';
  212. }
  213. }