attendancelink.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /**
  9. * Class
  10. * @package chamilo.gradebook
  11. */
  12. class AttendanceLink extends AbstractLink
  13. {
  14. // INTERNAL VARIABLES
  15. private $attendance_table = null;
  16. private $itemprop_table = null;
  17. // CONSTRUCTORS
  18. function __construct() {
  19. parent::__construct();
  20. $this->set_type(LINK_ATTENDANCE);
  21. }
  22. public function get_type_name() {
  23. return get_lang('Attendance');
  24. }
  25. public function is_allowed_to_change_name() {
  26. return false;
  27. }
  28. // FUNCTIONS IMPLEMENTING ABSTRACTLINK
  29. /**
  30. * Generate an array of attendances that a teacher hasn't created a link for.
  31. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  32. * @todo seems to be depracated
  33. */
  34. public function get_not_created_links() {
  35. return false;
  36. if (empty($this->course_code)) {
  37. die('Error in get_not_created_links() : course code not set');
  38. }
  39. $tbl_grade_links = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  40. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  41. FROM '.$this->get_attendance_table().' att
  42. WHERE att.c_id = '.$this->course_id.' AND att.id NOT IN (SELECT ref_id FROM '.$tbl_grade_links.' WHERE type = '.LINK_ATTENDANCE.' AND course_code = "'.Database::escape_string($this->get_course_code()).'")
  43. AND att.session_id='.api_get_session_id().'';
  44. $result = Database::query($sql);
  45. $cats=array();
  46. while ($data=Database::fetch_array($result)) {
  47. if ( isset($data['attendance_qualify_title']) && $data['attendance_qualify_title'] != ''){
  48. $cats[] = array ($data['id'], $data['attendance_qualify_title']);
  49. } else {
  50. $cats[] = array ($data['id'], $data['name']);
  51. }
  52. }
  53. return $cats;
  54. }
  55. /**
  56. * Generate an array of all attendances available.
  57. * @return array 2-dimensional array - every element contains 2 subelements (id, name)
  58. */
  59. public function get_all_links() {
  60. if (empty($this->course_code)) {
  61. die('Error in get_not_created_links() : course code not set');
  62. }
  63. $tbl_attendance = $this->get_attendance_table();
  64. $session_id = api_get_session_id();
  65. /*
  66. if (empty($session_id)) {
  67. $session_condition = api_get_session_condition(0, true);
  68. } else {
  69. $session_condition = api_get_session_condition($session_id, true, true);
  70. }
  71. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  72. FROM '.$tbl_attendance.' att
  73. WHERE att.c_id = '.$this->course_id.' AND att.active = 1 '.$session_condition;*/
  74. $sql = 'SELECT att.id, att.name, att.attendance_qualify_title
  75. FROM '.$tbl_attendance.' att
  76. WHERE att.c_id = '.$this->course_id.' AND att.active = 1 AND att.session_id = '.$session_id;
  77. $result = Database::query($sql);
  78. while ($data=Database::fetch_array($result)) {
  79. if (isset($data['attendance_qualify_title']) && $data['attendance_qualify_title'] != ''){
  80. $cats[] = array ($data['id'], $data['attendance_qualify_title']);
  81. } else {
  82. $cats[] = array ($data['id'], $data['name']);
  83. }
  84. }
  85. $my_cats = isset($cats)?$cats:null;
  86. return $my_cats;
  87. }
  88. /**
  89. * Has anyone done this exercise yet ?
  90. */
  91. public function has_results() {
  92. $tbl_attendance_result = Database :: get_course_table(TABLE_ATTENDANCE_RESULT);
  93. $session_id = api_get_session_id();
  94. $sql = 'SELECT count(*) AS number FROM '.$tbl_attendance_result."
  95. WHERE session_id = $session_id AND c_id = '.$this->course_id.' AND attendance_id = '".intval($this->get_ref_id())."'";
  96. $result = Database::query($sql);
  97. $number = Database::fetch_row($result);
  98. return ($number[0] != 0);
  99. }
  100. public function calc_score($stud_id = null) {
  101. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  102. $session_id = api_get_session_id();
  103. // get attendance qualify max
  104. $sql = 'SELECT att.attendance_qualify_max FROM '.$this->get_attendance_table().' att
  105. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' AND att.session_id='.intval($session_id).'';
  106. $query = Database::query($sql);
  107. $attendance = Database::fetch_array($query);
  108. // get results
  109. $sql = 'SELECT * FROM '.$tbl_attendance_result.'
  110. WHERE c_id = '.$this->course_id.' AND attendance_id = '.intval($this->get_ref_id());
  111. if (isset($stud_id)) {
  112. $sql .= ' AND user_id = '.intval($stud_id);
  113. }
  114. $scores = Database::query($sql);
  115. // for 1 student
  116. if (isset($stud_id)) {
  117. if ($data = Database::fetch_array($scores)) {
  118. return array ($data['score'], $attendance['attendance_qualify_max']);
  119. } else {
  120. //We sent the 0/attendance_qualify_max instead of null for correct calculations
  121. return array (0, $attendance['attendance_qualify_max']);
  122. }
  123. } else {// all students -> get average
  124. $students=array(); // user list, needed to make sure we only
  125. // take first attempts into account
  126. $rescount = 0;
  127. $sum = 0;
  128. while ($data=Database::fetch_array($scores)) {
  129. if (!(array_key_exists($data['user_id'],$students))) {
  130. if ($attendance['attendance_qualify_max'] != 0) {
  131. $students[$data['user_id']] = $data['score'];
  132. $rescount++;
  133. $sum += ($data['score'] / $attendance['attendance_qualify_max']);
  134. }
  135. }
  136. }
  137. if ($rescount == 0) {
  138. return null;
  139. } else {
  140. return array ($sum , $rescount);
  141. }
  142. }
  143. }
  144. // INTERNAL FUNCTIONS
  145. /**
  146. * Lazy load function to get the database table of the student publications
  147. */
  148. private function get_attendance_table() {
  149. $this->attendance_table = Database :: get_course_table(TABLE_ATTENDANCE);
  150. return $this->attendance_table;
  151. }
  152. /**
  153. * Lazy load function to get the database table of the item properties
  154. */
  155. private function get_itemprop_table () {
  156. $this->itemprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  157. return $this->itemprop_table;
  158. }
  159. public function needs_name_and_description() {
  160. return false;
  161. }
  162. public function needs_max() {
  163. return false;
  164. }
  165. public function needs_results() {
  166. return false;
  167. }
  168. public function get_name() {
  169. $this->get_attendance_data();
  170. $attendance_title = isset($this->attendance_data['name']) ? $this->attendance_data['name'] : '';
  171. $attendance_qualify_title = isset($this->attendance_data['attendance_qualify_title']) ? $this->attendance_data['attendance_qualify_title'] : '';
  172. if ( isset($attendance_qualify_title) && $attendance_qualify_title != '') {
  173. return $this->attendance_data['attendance_qualify_title'];
  174. } else {
  175. return $attendance_title;
  176. }
  177. }
  178. public function get_description() {
  179. return '';
  180. }
  181. /**
  182. * Check if this still links to an exercise
  183. */
  184. public function is_valid_link() {
  185. $session_id = api_get_session_id();
  186. $sql = 'SELECT count(att.id) FROM '.$this->get_attendance_table().' att
  187. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  188. $result = Database::query($sql);
  189. $number = Database::fetch_row($result);
  190. return ($number[0] != 0);
  191. }
  192. public function get_test_id() {
  193. return 'DEBUG:ID';
  194. }
  195. public function get_link() {
  196. //it was extracts the attendance id
  197. $session_id = api_get_session_id();
  198. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  199. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  200. $result = Database::query($sql);
  201. $row = Database::fetch_array($result,'ASSOC');
  202. $attendance_id = $row['id'];
  203. $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();
  204. return $url;
  205. }
  206. private function get_attendance_data() {
  207. $tbl_name = $this->get_attendance_table();
  208. $session_id = api_get_session_id();
  209. if ($tbl_name == '') {
  210. return false;
  211. } elseif (!isset($this->attendance_data)) {
  212. $sql = 'SELECT * FROM '.$this->get_attendance_table().' att
  213. WHERE att.c_id = '.$this->course_id.' AND att.id = '.intval($this->get_ref_id()).' ';
  214. $query = Database::query($sql);
  215. $this->attendance_data = Database::fetch_array($query);
  216. }
  217. return $this->attendance_data;
  218. }
  219. public function get_icon_name() {
  220. return 'attendance';
  221. }
  222. }