attendancelink.class.php 10 KB

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