attendancelink.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Gradebook link to attendance item.
  5. *
  6. * @author Christian Fasanando (christian1827@gmail.com)
  7. *
  8. * @package chamilo.gradebook
  9. */
  10. class AttendanceLink extends AbstractLink
  11. {
  12. private $attendance_table = null;
  13. private $itemprop_table = null;
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->set_type(LINK_ATTENDANCE);
  21. }
  22. /**
  23. * @return string
  24. */
  25. public function get_type_name()
  26. {
  27. return get_lang('Attendance');
  28. }
  29. /**
  30. * @return bool
  31. */
  32. public function is_allowed_to_change_name()
  33. {
  34. return false;
  35. }
  36. /**
  37. * Generate an array of all attendances available.
  38. *
  39. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  40. */
  41. public function get_all_links()
  42. {
  43. if (empty($this->course_code)) {
  44. return [];
  45. }
  46. $tbl_attendance = $this->get_attendance_table();
  47. $session_id = api_get_session_id();
  48. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  49. FROM '.$tbl_attendance.' att
  50. WHERE
  51. att.c_id = '.$this->course_id.' AND
  52. att.active = 1 AND
  53. att.session_id = '.$session_id;
  54. $result = Database::query($sql);
  55. while ($data = Database::fetch_array($result)) {
  56. if (isset($data['attendance_qualify_title']) && $data['attendance_qualify_title'] != '') {
  57. $cats[] = [$data['id'], $data['attendance_qualify_title']];
  58. } else {
  59. $cats[] = [$data['id'], $data['name']];
  60. }
  61. }
  62. $my_cats = isset($cats) ? $cats : [];
  63. return $my_cats;
  64. }
  65. /**
  66. * Has anyone done this exercise yet ?
  67. */
  68. public function has_results()
  69. {
  70. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  71. $session_id = api_get_session_id();
  72. $sql = 'SELECT count(*) AS number FROM '.$tbl_attendance_result."
  73. WHERE
  74. session_id = $session_id AND
  75. c_id = '.$this->course_id.' AND
  76. attendance_id = '".$this->get_ref_id()."'";
  77. $result = Database::query($sql);
  78. $number = Database::fetch_row($result);
  79. return $number[0] != 0;
  80. }
  81. /**
  82. * @param int $stud_id
  83. *
  84. * @return array|null
  85. */
  86. public function calc_score($stud_id = null, $type = null)
  87. {
  88. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  89. $session_id = api_get_session_id();
  90. // get attendance qualify max
  91. $sql = 'SELECT att.attendance_qualify_max
  92. FROM '.$this->get_attendance_table().' att
  93. WHERE
  94. att.c_id = '.$this->course_id.' AND
  95. att.id = '.$this->get_ref_id().' AND
  96. att.session_id='.$session_id;
  97. $query = Database::query($sql);
  98. $attendance = Database::fetch_array($query, 'ASSOC');
  99. // Get results
  100. $sql = 'SELECT *
  101. FROM '.$tbl_attendance_result.'
  102. WHERE c_id = '.$this->course_id.' AND attendance_id = '.$this->get_ref_id();
  103. if (isset($stud_id)) {
  104. $sql .= ' AND user_id = '.intval($stud_id);
  105. }
  106. $scores = Database::query($sql);
  107. // for 1 student
  108. if (isset($stud_id)) {
  109. if ($data = Database::fetch_array($scores, 'ASSOC')) {
  110. return [
  111. $data['score'],
  112. $attendance['attendance_qualify_max'],
  113. ];
  114. } else {
  115. //We sent the 0/attendance_qualify_max instead of null for correct calculations
  116. return [0, $attendance['attendance_qualify_max']];
  117. }
  118. } else {
  119. // all students -> get average
  120. $students = []; // user list, needed to make sure we only
  121. // take first attempts into account
  122. $rescount = 0;
  123. $sum = 0;
  124. $sumResult = 0;
  125. $bestResult = 0;
  126. while ($data = Database::fetch_array($scores)) {
  127. if (!(array_key_exists($data['user_id'], $students))) {
  128. if ($attendance['attendance_qualify_max'] != 0) {
  129. $students[$data['user_id']] = $data['score'];
  130. $rescount++;
  131. $sum += $data['score'] / $attendance['attendance_qualify_max'];
  132. $sumResult += $data['score'];
  133. if ($data['score'] > $bestResult) {
  134. $bestResult = $data['score'];
  135. }
  136. $weight = $attendance['attendance_qualify_max'];
  137. }
  138. }
  139. }
  140. if ($rescount == 0) {
  141. return [null, null];
  142. } else {
  143. switch ($type) {
  144. case 'best':
  145. return [$bestResult, $weight];
  146. break;
  147. case 'average':
  148. return [$sumResult / $rescount, $weight];
  149. break;
  150. case 'ranking':
  151. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  152. break;
  153. default:
  154. return [$sum, $rescount];
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. public function needs_name_and_description()
  161. {
  162. return false;
  163. }
  164. public function needs_max()
  165. {
  166. return false;
  167. }
  168. public function needs_results()
  169. {
  170. return false;
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function get_name()
  176. {
  177. $this->get_attendance_data();
  178. $attendance_title = isset($this->attendance_data['name']) ? $this->attendance_data['name'] : '';
  179. $attendance_qualify_title = isset($this->attendance_data['attendance_qualify_title']) ? $this->attendance_data['attendance_qualify_title'] : '';
  180. if (isset($attendance_qualify_title) && $attendance_qualify_title != '') {
  181. return $this->attendance_data['attendance_qualify_title'];
  182. } else {
  183. return $attendance_title;
  184. }
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function get_description()
  190. {
  191. return '';
  192. }
  193. /**
  194. * Check if this still links to an exercise.
  195. */
  196. public function is_valid_link()
  197. {
  198. $session_id = api_get_session_id();
  199. $sql = 'SELECT count(att.id) FROM '.$this->get_attendance_table().' att
  200. WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
  201. $result = Database::query($sql);
  202. $number = Database::fetch_row($result);
  203. return $number[0] != 0;
  204. }
  205. public function get_link()
  206. {
  207. //it was extracts the attendance id
  208. $session_id = api_get_session_id();
  209. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  210. WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
  211. $result = Database::query($sql);
  212. $row = Database::fetch_array($result, 'ASSOC');
  213. $attendance_id = $row['id'];
  214. $url = api_get_path(WEB_PATH).'main/attendance/index.php?action=attendance_sheet_list&gradebook=view&attendance_id='.$attendance_id.'&'.api_get_cidreq_params($this->get_course_code(), $session_id);
  215. return $url;
  216. }
  217. /**
  218. * @return string
  219. */
  220. public function get_icon_name()
  221. {
  222. return 'attendance';
  223. }
  224. /**
  225. * Lazy load function to get the database table of the student publications.
  226. */
  227. private function get_attendance_table()
  228. {
  229. $this->attendance_table = Database::get_course_table(TABLE_ATTENDANCE);
  230. return $this->attendance_table;
  231. }
  232. /**
  233. * @return array|bool
  234. */
  235. private function get_attendance_data()
  236. {
  237. $tbl_name = $this->get_attendance_table();
  238. $session_id = api_get_session_id();
  239. if ($tbl_name == '') {
  240. return false;
  241. } elseif (!isset($this->attendance_data)) {
  242. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  243. WHERE att.c_id = '.$this->course_id.' AND att.id = '.$this->get_ref_id();
  244. $query = Database::query($sql);
  245. $this->attendance_data = Database::fetch_array($query);
  246. }
  247. return $this->attendance_data;
  248. }
  249. }