gradebook_data_generator.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script
  5. * @package chamilo.gradebook
  6. */
  7. /**
  8. * Class to select, sort and transform object data into array data,
  9. * used for the general gradebook view
  10. * @author Bert Steppé
  11. * @package chamilo.gradebook
  12. */
  13. class GradebookDataGenerator
  14. {
  15. // Sorting types constants
  16. const GDG_SORT_TYPE = 1;
  17. const GDG_SORT_NAME = 2;
  18. const GDG_SORT_DESCRIPTION = 4;
  19. const GDG_SORT_WEIGHT = 8;
  20. const GDG_SORT_DATE = 16;
  21. const GDG_SORT_ASC = 32;
  22. const GDG_SORT_DESC = 64;
  23. const GDG_SORT_ID = 128;
  24. private $items;
  25. private $evals_links;
  26. function GradebookDataGenerator($cats = array(), $evals = array(), $links = array()) {
  27. $allcats = (isset($cats) ? $cats : array());
  28. $allevals = (isset($evals) ? $evals : array());
  29. $alllinks = (isset($links) ? $links : array());
  30. // merge categories, evaluations and links
  31. $this->items = array_merge($allcats, $allevals, $alllinks);
  32. $this->evals_links = array_merge($allevals, $alllinks);
  33. }
  34. /**
  35. * Get total number of items (rows)
  36. */
  37. public function get_total_items_count() {
  38. return count($this->items);
  39. }
  40. /**
  41. * Get actual array data
  42. * @return array 2-dimensional array - each array contains the elements:
  43. * 0: cat/eval/link object
  44. * 1: item name
  45. * 2: description
  46. * 3: weight
  47. * 4: date
  48. * 5: student's score (if student logged in)
  49. */
  50. public function get_data($sorting = 0, $start = 0, $count = null, $ignore_score_color = false) {
  51. //$status = CourseManager::get_user_in_course_status(api_get_user_id(), api_get_course_id());
  52. // do some checks on count, redefine if invalid value
  53. if (!isset($count)) {
  54. $count = count ($this->items) - $start;
  55. }
  56. if ($count < 0) {
  57. $count = 0;
  58. }
  59. $allitems = $this->items;
  60. // sort array
  61. if ($sorting & self :: GDG_SORT_TYPE) {
  62. usort($allitems, array('GradebookDataGenerator', 'sort_by_type'));
  63. } elseif ($sorting & self :: GDG_SORT_ID) {
  64. usort($allitems, array('GradebookDataGenerator', 'sort_by_id'));
  65. } elseif ($sorting & self :: GDG_SORT_NAME) {
  66. usort($allitems, array('GradebookDataGenerator', 'sort_by_name'));
  67. } elseif ($sorting & self :: GDG_SORT_DESCRIPTION) {
  68. usort($allitems, array('GradebookDataGenerator', 'sort_by_description'));
  69. } elseif ($sorting & self :: GDG_SORT_WEIGHT) {
  70. usort($allitems, array('GradebookDataGenerator', 'sort_by_weight'));
  71. } elseif ($sorting & self :: GDG_SORT_DATE) {
  72. //usort($allitems, array('GradebookDataGenerator', 'sort_by_date'));
  73. }
  74. if ($sorting & self :: GDG_SORT_DESC) {
  75. $allitems = array_reverse($allitems);
  76. }
  77. // get selected items
  78. $visibleitems = array_slice($allitems, $start, $count);
  79. //status de user in course
  80. $user_id = api_get_user_id();
  81. $course_code = api_get_course_id();
  82. $status_user = api_get_status_of_user_in_course($user_id, $course_code);
  83. // generate the data to display
  84. $data = array();
  85. foreach ($visibleitems as $item) {
  86. $row = array ();
  87. $row[] = $item;
  88. $row[] = $item->get_name();
  89. $row[] = $item->get_description();
  90. $row[] = $item->get_weight();
  91. /*if (api_is_allowed_to_edit(null, true)) {
  92. $row[] = $this->build_date_column($item);
  93. }*/
  94. if (count($this->evals_links) > 0) {
  95. if (!api_is_allowed_to_edit() || $status_user != 1 ) {
  96. $row[] = $this->build_result_column($item, $ignore_score_color);
  97. $row[] = $item;
  98. }
  99. }
  100. $data[] = $row;
  101. }
  102. return $data;
  103. }
  104. /**
  105. * Returns the link to the certificate generation, if the score is enough, otherwise
  106. * returns an empty string. This only works with categories.
  107. * @param object Item
  108. */
  109. function get_certificate_link($item) {
  110. if (is_a($item, 'Category')) {
  111. if($item->is_certificate_available(api_get_user_id())) {
  112. $link = '<a href="'.Security::remove_XSS($_SESSION['gradebook_dest']).'?export_certificate=1&cat='.$item->get_id().'&user='.api_get_user_id().'">'.get_lang('Certificate').'</a>';
  113. return $link;
  114. }
  115. }
  116. return '';
  117. }
  118. // Sort functions
  119. // Make sure to only use functions as defined in the GradebookItem interface !
  120. function sort_by_name($item1, $item2) {
  121. return api_strnatcmp($item1->get_name(), $item2->get_name());
  122. }
  123. function sort_by_id($item1, $item2) {
  124. return api_strnatcmp($item1->get_id(), $item2->get_id());
  125. }
  126. function sort_by_type($item1, $item2) {
  127. if ($item1->get_item_type() == $item2->get_item_type()) {
  128. return $this->sort_by_name($item1,$item2);
  129. } else {
  130. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  131. }
  132. }
  133. function sort_by_description($item1, $item2) {
  134. $result = api_strcmp($item1->get_description(), $item2->get_description());
  135. if ($result == 0) {
  136. return $this->sort_by_name($item1,$item2);
  137. }
  138. return $result;
  139. }
  140. function sort_by_weight($item1, $item2) {
  141. if ($item1->get_weight() == $item2->get_weight()) {
  142. return $this->sort_by_name($item1,$item2);
  143. } else {
  144. return ($item1->get_weight() < $item2->get_weight() ? -1 : 1);
  145. }
  146. }
  147. function sort_by_date($item1, $item2) {
  148. if (is_int($item1->get_date())) {
  149. $timestamp1 = $item1->get_date();
  150. } else {
  151. $date = $item1->get_date();
  152. if (!empty($date)) {
  153. $timestamp1 = api_strtotime($date, 'UTC');
  154. } else {
  155. $timestamp1 = null;
  156. }
  157. }
  158. if(is_int($item2->get_date())) {
  159. $timestamp2 = $item2->get_date();
  160. } else {
  161. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  162. }
  163. if ($timestamp1 == $timestamp2) {
  164. return $this->sort_by_name($item1,$item2);
  165. } else {
  166. return ($timestamp1 < $timestamp2 ? -1 : 1);
  167. }
  168. }
  169. // Other functions
  170. private function build_result_column($item, $ignore_score_color) {
  171. $scoredisplay = ScoreDisplay::instance();
  172. $score = $item->calc_score(api_get_user_id());
  173. if (!empty($score)) {
  174. switch ($item->get_item_type()) {
  175. // category
  176. case 'C' :
  177. if ($score != null) {
  178. $displaytype = SCORE_PERCENT;
  179. if ($ignore_score_color) {
  180. $displaytype |= SCORE_IGNORE_SPLIT;
  181. }
  182. return get_lang('Total') . ' : '. $scoredisplay->display_score($score, $displaytype);
  183. } else {
  184. return '';
  185. }
  186. // evaluation and link
  187. case 'E' :
  188. case 'L' :
  189. $displaytype = SCORE_DIV_PERCENT;
  190. if ($ignore_score_color) {
  191. $displaytype |= SCORE_IGNORE_SPLIT;
  192. }
  193. return $scoredisplay->display_score($score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  194. }
  195. }
  196. return null;
  197. }
  198. private function build_date_column($item) {
  199. $date = $item->get_date();
  200. if (!isset($date) || empty($date)) {
  201. return '';
  202. } else {
  203. if(is_int($date)) {
  204. return api_convert_and_format_date($date);
  205. } else {
  206. return api_format_date($date);
  207. }
  208. }
  209. }
  210. }