results_data_generator.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos Latinoamerica SAC
  6. Copyright (c) 2006 Dokeos SPRL
  7. Copyright (c) 2006 Ghent University (UGent)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * Class to select, sort and transform object data into array data,
  22. * used for the teacher's evaluation results view
  23. * @author Bert Steppé
  24. */
  25. class ResultsDataGenerator
  26. {
  27. // Sorting types constants
  28. const RDG_SORT_LASTNAME = 1;
  29. const RDG_SORT_FIRSTNAME = 2;
  30. const RDG_SORT_SCORE = 4;
  31. const RDG_SORT_MASK = 8;
  32. const RDG_SORT_ASC = 16;
  33. const RDG_SORT_DESC = 32;
  34. private $evaluation;
  35. private $results;
  36. private $is_course_ind;
  37. private $include_edit;
  38. /**
  39. * Constructor
  40. */
  41. function ResultsDataGenerator ( $evaluation,
  42. $results = array(),
  43. $include_edit = false) {
  44. $this->evaluation = $evaluation;
  45. $this->results = (isset($results) ? $results : array());
  46. }
  47. /**
  48. * Get total number of results (rows)
  49. */
  50. public function get_total_results_count () {
  51. return count($this->results);
  52. }
  53. /**
  54. * Get actual array data
  55. * @return array 2-dimensional array - each array contains the elements:
  56. * 0 ['id'] : user id
  57. * 1 ['result_id'] : result id
  58. * 2 ['lastname'] : user lastname
  59. * 3 ['firstname'] : user firstname
  60. * 4 ['score'] : student's score
  61. * 5 ['display'] : custom score display (only if custom scoring enabled)
  62. */
  63. public function get_data ($sorting = 0, $start = 0, $count = null, $ignore_score_color = false) {
  64. // do some checks on count, redefine if invalid value
  65. if (!isset($count)) {
  66. $count = count ($this->results) - $start;
  67. }
  68. if ($count < 0) {
  69. $count = 0;
  70. }
  71. $scoredisplay = ScoreDisplay :: instance();
  72. // generate actual data array
  73. $table = array();
  74. foreach($this->results as $result) {
  75. $user = array();
  76. $info = get_user_info_from_id($result->get_user_id());
  77. $user['id'] = $result->get_user_id();
  78. $user['result_id'] = $result->get_id();
  79. $user['lastname'] = $info['lastname'];
  80. $user['firstname'] = $info['firstname'];
  81. $user['score'] = $this->get_score_display($result->get_score(),true, $ignore_score_color);
  82. if ($scoredisplay->is_custom())
  83. $user['display'] = $this->get_score_display($result->get_score(),false, $ignore_score_color);;
  84. $table[] = $user;
  85. }
  86. // sort array
  87. if ($sorting & self :: RDG_SORT_LASTNAME) {
  88. usort($table, array('ResultsDataGenerator', 'sort_by_last_name'));
  89. } elseif ($sorting & self :: RDG_SORT_FIRSTNAME) {
  90. usort($table, array('ResultsDataGenerator', 'sort_by_first_name'));
  91. } elseif ($sorting & self :: RDG_SORT_SCORE) {
  92. usort($table, array('ResultsDataGenerator', 'sort_by_score'));
  93. } elseif ($sorting & self :: RDG_SORT_MASK) {
  94. usort($table, array('ResultsDataGenerator', 'sort_by_mask'));
  95. }
  96. if ($sorting & self :: RDG_SORT_DESC) {
  97. $table = array_reverse($table);
  98. }
  99. return array_slice($table, $start, $count);
  100. }
  101. private function get_score_display ($score, $realscore, $ignore_score_color) {
  102. if ($score != null) {
  103. $display_type = SCORE_DIV_PERCENT;
  104. if ($ignore_score_color) {
  105. $display_type |= SCORE_IGNORE_SPLIT;
  106. }
  107. $scoredisplay = ScoreDisplay :: instance();
  108. return $scoredisplay->display_score
  109. (array($score,$this->evaluation->get_max()),
  110. $display_type,
  111. $realscore ? SCORE_ONLY_DEFAULT : SCORE_ONLY_CUSTOM);
  112. }
  113. else {
  114. return '';
  115. }
  116. }
  117. // Sort functions - used internally
  118. function sort_by_last_name($item1, $item2) {
  119. if (api_strtolower($item1['lastname']) == api_strtolower($item2['lastname'])) {
  120. return 0;
  121. } else {
  122. return (api_strtolower($item1['lastname']) < api_strtolower($item2['lastname']) ? -1 : 1);
  123. }
  124. }
  125. function sort_by_first_name($item1, $item2) {
  126. if (api_strtolower($item1['firstname']) == api_strtolower($item2['firstname'])) {
  127. return 0;
  128. }
  129. else {
  130. return (api_strtolower($item1['firstname']) < api_strtolower($item2['firstname']) ? -1 : 1);
  131. }
  132. }
  133. function sort_by_score($item1, $item2) {
  134. if ($item1['score'] == $item2['score']) {
  135. return 0;
  136. }else {
  137. return ($item1['score'] < $item2['score'] ? -1 : 1);
  138. }
  139. }
  140. function sort_by_mask ($item1, $item2) {
  141. $score1 = (isset($item1['score']) ? array($item1['score'],$this->evaluation->get_max()) : null);
  142. $score2 = (isset($item2['score']) ? array($item2['score'],$this->evaluation->get_max()) : null);
  143. return ScoreDisplay :: compare_scores_by_custom_display($score1, $score2);
  144. }
  145. }