attendancelink.class.php 8.4 KB

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