gradebook_data_generator.class.php 7.8 KB

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