gradebook_data_generator.class.php 6.5 KB

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