attendancelink.class.php 8.6 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. /*
  80. if (empty($session_id)) {
  81. $session_condition = api_get_session_condition(0, true);
  82. } else {
  83. $session_condition = api_get_session_condition($session_id, true, true);
  84. }
  85. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  86. FROM '.$tbl_attendance.' att
  87. WHERE att.c_id = '.$this->course_id.' AND att.active = 1 '.$session_condition;*/
  88. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  89. FROM '.$tbl_attendance.' att
  90. WHERE att.c_id = '.$this->course_id.' AND att.active = 1 AND att.session_id = '.$session_id;
  91. $result = Database::query($sql);
  92. while ($data=Database::fetch_array($result)) {
  93. if (isset($data['attendance_qualify_title']) && $data['attendance_qualify_title'] != ''){
  94. $cats[] = array ($data['id'], $data['attendance_qualify_title']);
  95. } else {
  96. $cats[] = array ($data['id'], $data['name']);
  97. }
  98. }
  99. $my_cats = isset($cats)?$cats:null;
  100. return $my_cats;
  101. }
  102. /**
  103. * Has anyone done this exercise yet ?
  104. */
  105. public function has_results()
  106. {
  107. $tbl_attendance_result = Database :: get_course_table(TABLE_ATTENDANCE_RESULT);
  108. $session_id = api_get_session_id();
  109. $sql = 'SELECT count(*) AS number FROM '.$tbl_attendance_result."
  110. WHERE
  111. session_id = $session_id AND
  112. c_id = '.$this->course_id.' AND
  113. attendance_id = '".intval($this->get_ref_id())."'";
  114. $result = Database::query($sql);
  115. $number = Database::fetch_row($result);
  116. return $number[0] != 0;
  117. }
  118. /**
  119. * @param int $stud_id
  120. * @return array|null
  121. */
  122. public function calc_score($stud_id = null, $type = null)
  123. {
  124. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  125. $session_id = api_get_session_id();
  126. // get attendance qualify max
  127. $sql = 'SELECT att.attendance_qualify_max FROM '.$this->get_attendance_table().' att
  128. WHERE
  129. att.c_id = '.$this->course_id.' AND
  130. att.id = '.intval($this->get_ref_id()).' AND
  131. att.session_id='.intval($session_id).'';
  132. $query = Database::query($sql);
  133. $attendance = Database::fetch_array($query, 'ASSOC');
  134. // Get results
  135. $sql = 'SELECT * FROM '.$tbl_attendance_result.'
  136. WHERE c_id = '.$this->course_id.' AND attendance_id = '.intval($this->get_ref_id());
  137. if (isset($stud_id)) {
  138. $sql .= ' AND user_id = '.intval($stud_id);
  139. }
  140. $scores = Database::query($sql);
  141. // for 1 student
  142. if (isset($stud_id)) {
  143. if ($data = Database::fetch_array($scores, 'ASSOC')) {
  144. return array($data['score'], $attendance['attendance_qualify_max']);
  145. } else {
  146. //We sent the 0/attendance_qualify_max instead of null for correct calculations
  147. return array(0, $attendance['attendance_qualify_max']);
  148. }
  149. } else {
  150. // all students -> get average
  151. $students = array(); // user list, needed to make sure we only
  152. // take first attempts into account
  153. $rescount = 0;
  154. $sum = 0;
  155. $sumResult = 0;
  156. $bestResult = 0;
  157. while ($data = Database::fetch_array($scores)) {
  158. if (!(array_key_exists($data['user_id'], $students))) {
  159. if ($attendance['attendance_qualify_max'] != 0) {
  160. $students[$data['user_id']] = $data['score'];
  161. $rescount++;
  162. $sum += $data['score'] / $attendance['attendance_qualify_max'];
  163. $sumResult += $data['score'];
  164. if ($data['score'] > $bestResult) {
  165. $bestResult = $data['score'];
  166. }
  167. $weight = $attendance['attendance_qualify_max'];
  168. }
  169. }
  170. }
  171. if ($rescount == 0) {
  172. return null;
  173. } else {
  174. switch ($type) {
  175. case 'best':
  176. return array($bestResult, $weight);
  177. break;
  178. case 'average':
  179. return array($sumResult / $rescount, $weight);
  180. break;
  181. case 'ranking':
  182. return AbstractLink::getCurrentUserRanking($stud_id, $students);
  183. break;
  184. default:
  185. return array($sum, $rescount);
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * Lazy load function to get the database table of the student publications
  193. */
  194. private function get_attendance_table()
  195. {
  196. $this->attendance_table = Database :: get_course_table(TABLE_ATTENDANCE);
  197. return $this->attendance_table;
  198. }
  199. public function needs_name_and_description()
  200. {
  201. return false;
  202. }
  203. public function needs_max()
  204. {
  205. return false;
  206. }
  207. public function needs_results()
  208. {
  209. return false;
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function get_name()
  215. {
  216. $this->get_attendance_data();
  217. $attendance_title = isset($this->attendance_data['name']) ? $this->attendance_data['name'] : '';
  218. $attendance_qualify_title = isset($this->attendance_data['attendance_qualify_title']) ? $this->attendance_data['attendance_qualify_title'] : '';
  219. if ( isset($attendance_qualify_title) && $attendance_qualify_title != '') {
  220. return $this->attendance_data['attendance_qualify_title'];
  221. } else {
  222. return $attendance_title;
  223. }
  224. }
  225. /**
  226. * @return string
  227. */
  228. public function get_description()
  229. {
  230. return '';
  231. }
  232. /**
  233. * Check if this still links to an exercise
  234. */
  235. public function is_valid_link()
  236. {
  237. $session_id = api_get_session_id();
  238. $sql = 'SELECT count(att.id) FROM '.$this->get_attendance_table().' att
  239. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  240. $result = Database::query($sql);
  241. $number = Database::fetch_row($result);
  242. return ($number[0] != 0);
  243. }
  244. public function get_test_id()
  245. {
  246. return 'DEBUG:ID';
  247. }
  248. public function get_link()
  249. {
  250. //it was extracts the attendance id
  251. $session_id = api_get_session_id();
  252. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  253. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  254. $result = Database::query($sql);
  255. $row = Database::fetch_array($result,'ASSOC');
  256. $attendance_id = $row['id'];
  257. $url = api_get_path(WEB_PATH).'main/attendance/index.php?action=attendance_sheet_list&gradebook=view&attendance_id='.$attendance_id.'&session_id='.$session_id.'&cidReq='.$this->get_course_code();
  258. return $url;
  259. }
  260. /**
  261. * @return array|bool
  262. */
  263. private function get_attendance_data()
  264. {
  265. $tbl_name = $this->get_attendance_table();
  266. $session_id = api_get_session_id();
  267. if ($tbl_name == '') {
  268. return false;
  269. } elseif (!isset($this->attendance_data)) {
  270. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  271. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  272. $query = Database::query($sql);
  273. $this->attendance_data = Database::fetch_array($query);
  274. }
  275. return $this->attendance_data;
  276. }
  277. /**
  278. * @return string
  279. */
  280. public function get_icon_name()
  281. {
  282. return 'attendance';
  283. }
  284. }