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'] = intval(
  95. $scoreDisplay->display_score(
  96. [$result->get_score(), $this->evaluation->get_max()],
  97. SCORE_PERCENT,
  98. SCORE_BOTH,
  99. true
  100. )
  101. );
  102. if ($pdf && $number_decimals == null) {
  103. $user['scoreletter'] = $result->get_score();
  104. }
  105. if ($scoreDisplay->is_custom()) {
  106. $user['display'] = $this->get_score_display(
  107. $result->get_score(),
  108. false,
  109. $ignore_score_color
  110. );
  111. if (!empty($model)) {
  112. $user['display'] .= '&nbsp;'.
  113. ExerciseLib::show_score(
  114. $result->get_score(),
  115. $this->evaluation->get_max()
  116. )
  117. ;
  118. }
  119. }
  120. $table[] = $user;
  121. }
  122. // sort array
  123. if ($sorting & self::RDG_SORT_LASTNAME) {
  124. usort($table, ['ResultsDataGenerator', 'sort_by_last_name']);
  125. } elseif ($sorting & self::RDG_SORT_FIRSTNAME) {
  126. usort($table, ['ResultsDataGenerator', 'sort_by_first_name']);
  127. } elseif ($sorting & self::RDG_SORT_SCORE) {
  128. usort($table, ['ResultsDataGenerator', 'sort_by_score']);
  129. } elseif ($sorting & self::RDG_SORT_MASK) {
  130. usort($table, ['ResultsDataGenerator', 'sort_by_mask']);
  131. }
  132. if ($sorting & self::RDG_SORT_DESC) {
  133. $table = array_reverse($table);
  134. }
  135. $return = array_slice($table, $start, $count);
  136. return $return;
  137. }
  138. // Sort functions - used internally
  139. /**
  140. * @param array $item1
  141. * @param array $item2
  142. *
  143. * @return int
  144. */
  145. public function sort_by_last_name($item1, $item2)
  146. {
  147. return api_strcmp($item1['lastname'], $item2['lastname']);
  148. }
  149. /**
  150. * @param array $item1
  151. * @param array $item2
  152. *
  153. * @return int
  154. */
  155. public function sort_by_first_name($item1, $item2)
  156. {
  157. return api_strcmp($item1['firstname'], $item2['firstname']);
  158. }
  159. /**
  160. * @param array $item1
  161. * @param array $item2
  162. *
  163. * @return int
  164. */
  165. public function sort_by_score($item1, $item2)
  166. {
  167. if ($item1['percentage_score'] == $item2['percentage_score']) {
  168. return 0;
  169. } else {
  170. return $item1['percentage_score'] < $item2['percentage_score'] ? -1 : 1;
  171. }
  172. }
  173. /**
  174. * @param array $item1
  175. * @param array $item2
  176. *
  177. * @return int
  178. */
  179. public function sort_by_mask($item1, $item2)
  180. {
  181. $score1 = (isset($item1['score']) ? [$item1['score'], $this->evaluation->get_max()] : null);
  182. $score2 = (isset($item2['score']) ? [$item2['score'], $this->evaluation->get_max()] : null);
  183. return ScoreDisplay::compare_scores_by_custom_display($score1, $score2);
  184. }
  185. /**
  186. * Re-formats the score to show percentage ("2/4 (50 %)") or letters ("A").
  187. *
  188. * @param float Current absolute score (max score is taken from $this->evaluation->get_max()
  189. * @param bool Whether we want the real score (2/4 (50 %)) or the transformation (A, B, C, etc)
  190. * @param bool Whether we want to ignore the score color
  191. * @param bool $realscore
  192. *
  193. * @return string The score as we want to show it
  194. */
  195. private function get_score_display(
  196. $score,
  197. $realscore,
  198. $ignore_score_color = false
  199. ) {
  200. if ($score != null) {
  201. $scoreDisplay = ScoreDisplay::instance();
  202. $type = SCORE_CUSTOM;
  203. if ($realscore === true) {
  204. $type = SCORE_DIV_PERCENT;
  205. }
  206. return $scoreDisplay->display_score(
  207. [$score, $this->evaluation->get_max()],
  208. $type,
  209. SCORE_BOTH,
  210. $ignore_score_color
  211. );
  212. }
  213. return '';
  214. }
  215. }