gradebook_data_generator.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $status_user = api_get_status_of_user_in_course($user_id, api_get_course_int_id());
  82. // generate the data to display
  83. $data = array();
  84. foreach ($visibleitems as $item) {
  85. $row = array ();
  86. $row[] = $item;
  87. $row[] = $item->get_name();
  88. // display the 2 first line of description, and all description on mouseover (https://support.chamilo.org/issues/6588)
  89. $row[] = '<span title="'.api_remove_tags_with_space($item->get_description()).'">'.api_get_short_text_from_html($item->get_description(), 160).'</span>';
  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. * @return string
  109. */
  110. function get_certificate_link($item) {
  111. if (is_a($item, 'Category')) {
  112. if($item->is_certificate_available(api_get_user_id())) {
  113. $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>';
  114. return $link;
  115. }
  116. }
  117. return '';
  118. }
  119. // Sort functions
  120. // Make sure to only use functions as defined in the GradebookItem interface !
  121. function sort_by_name($item1, $item2) {
  122. return api_strnatcmp($item1->get_name(), $item2->get_name());
  123. }
  124. function sort_by_id($item1, $item2) {
  125. return api_strnatcmp($item1->get_id(), $item2->get_id());
  126. }
  127. function sort_by_type($item1, $item2) {
  128. if ($item1->get_item_type() == $item2->get_item_type()) {
  129. return $this->sort_by_name($item1,$item2);
  130. } else {
  131. return ($item1->get_item_type() < $item2->get_item_type() ? -1 : 1);
  132. }
  133. }
  134. function sort_by_description($item1, $item2) {
  135. $result = api_strcmp($item1->get_description(), $item2->get_description());
  136. if ($result == 0) {
  137. return $this->sort_by_name($item1,$item2);
  138. }
  139. return $result;
  140. }
  141. function sort_by_weight($item1, $item2) {
  142. if ($item1->get_weight() == $item2->get_weight()) {
  143. return $this->sort_by_name($item1,$item2);
  144. } else {
  145. return ($item1->get_weight() < $item2->get_weight() ? -1 : 1);
  146. }
  147. }
  148. function sort_by_date($item1, $item2) {
  149. if (is_int($item1->get_date())) {
  150. $timestamp1 = $item1->get_date();
  151. } else {
  152. $date = $item1->get_date();
  153. if (!empty($date)) {
  154. $timestamp1 = api_strtotime($date, 'UTC');
  155. } else {
  156. $timestamp1 = null;
  157. }
  158. }
  159. if(is_int($item2->get_date())) {
  160. $timestamp2 = $item2->get_date();
  161. } else {
  162. $timestamp2 = api_strtotime($item2->get_date(), 'UTC');
  163. }
  164. if ($timestamp1 == $timestamp2) {
  165. return $this->sort_by_name($item1,$item2);
  166. } else {
  167. return ($timestamp1 < $timestamp2 ? -1 : 1);
  168. }
  169. }
  170. // Other functions
  171. private function build_result_column($item, $ignore_score_color) {
  172. $scoredisplay = ScoreDisplay::instance();
  173. $score = $item->calc_score(api_get_user_id());
  174. if (!empty($score)) {
  175. switch ($item->get_item_type()) {
  176. // category
  177. case 'C' :
  178. if ($score != null) {
  179. $displaytype = SCORE_PERCENT;
  180. if ($ignore_score_color) {
  181. $displaytype |= SCORE_IGNORE_SPLIT;
  182. }
  183. return get_lang('Total') . ' : '. $scoredisplay->display_score($score, $displaytype);
  184. } else {
  185. return '';
  186. }
  187. // evaluation and link
  188. case 'E' :
  189. case 'L' :
  190. $displaytype = SCORE_DIV_PERCENT;
  191. if ($ignore_score_color) {
  192. $displaytype |= SCORE_IGNORE_SPLIT;
  193. }
  194. return $scoredisplay->display_score($score, SCORE_DIV_PERCENT_WITH_CUSTOM);
  195. }
  196. }
  197. return null;
  198. }
  199. private function build_date_column($item) {
  200. $date = $item->get_date();
  201. if (!isset($date) || empty($date)) {
  202. return '';
  203. } else {
  204. if(is_int($date)) {
  205. return api_convert_and_format_date($date);
  206. } else {
  207. return api_format_date($date);
  208. }
  209. }
  210. }
  211. }