attendance.lib.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like library, provides functions for attendance tool. It's also used like model to attendance_controller (MVC pattern)
  5. * @author Christian Fasanando <christian1827@gmail.com>
  6. * @author Julio Montoya <gugli100@gmail.com> improvements
  7. * @package chamilo.attendance
  8. */
  9. /**
  10. * Attendance can be used to instanciate objects or as a library to manage attendances
  11. * @package chamilo.attendance
  12. */
  13. class Attendance
  14. {
  15. private $session_id;
  16. private $course_id;
  17. private $date_time;
  18. private $name;
  19. private $description;
  20. private $attendance_qualify_title;
  21. private $attendance_weight;
  22. private $course_int_id;
  23. public $category_id;
  24. // constants
  25. const DONE_ATTENDANCE_LOG_TYPE = 'done_attendance_sheet';
  26. const UPDATED_ATTENDANCE_LOG_TYPE = 'updated_attendance_sheet';
  27. const LOCKED_ATTENDANCE_LOG_TYPE = 'locked_attendance_sheet';
  28. public function __construct() {
  29. //$this->course_int_id = api_get_course_int_id();
  30. }
  31. /**
  32. * Get the total number of attendance inside current course and current session
  33. * @see SortableTable#get_total_number_of_items()
  34. */
  35. static function get_number_of_attendances($active = -1) {
  36. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  37. $session_id = api_get_session_id();
  38. $condition_session = api_get_session_condition($session_id);
  39. $course_id = api_get_course_int_id();
  40. $sql = "SELECT COUNT(att.id) AS total_number_of_items FROM $tbl_attendance att
  41. WHERE c_id = $course_id $condition_session ";
  42. if ($active == 1 || $active == 0) {
  43. $sql .= "AND att.active = $active";
  44. }
  45. $res = Database::query($sql);
  46. $obj = Database::fetch_object($res);
  47. return $obj->total_number_of_items;
  48. }
  49. /**
  50. * Get attendance list only the id, name and attendance_qualify_max fields
  51. * @param string course db name (optional)
  52. * @param int session id (optional)
  53. * @return array attendances list
  54. */
  55. function get_attendances_list($course_id = '', $session_id = null) {
  56. // Initializing database table and variables
  57. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  58. $data = array();
  59. if (empty($course_id)) {
  60. $course_id = api_get_course_int_id();
  61. } else {
  62. $course_id = intval($course_id);
  63. }
  64. $session_id = isset($session_id)?intval($session_id):api_get_session_id();
  65. $condition_session = api_get_session_condition($session_id);
  66. // Get attendance data
  67. $sql = "SELECT id, name, attendance_qualify_max
  68. FROM $tbl_attendance
  69. WHERE c_id = $course_id AND active = 1 $condition_session ";
  70. $rs = Database::query($sql);
  71. if (Database::num_rows($rs) > 0) {
  72. while ($row = Database::fetch_array($rs,'ASSOC')) {
  73. $data[$row['id']] = $row;
  74. }
  75. }
  76. return $data;
  77. }
  78. /**
  79. * Get the attendaces to display on the current page (fill the sortable-table)
  80. * @param int offset of first user to recover
  81. * @param int Number of users to get
  82. * @param int Column to sort on
  83. * @param string Order (ASC,DESC)
  84. * @see SortableTable#get_table_data($from)
  85. */
  86. static function get_attendance_data($from = 0, $number_of_items = 20, $column = 1, $direction = 'desc') {
  87. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  88. $course_id = api_get_course_int_id();
  89. $session_id = api_get_session_id();
  90. $condition_session = api_get_session_condition($session_id);
  91. $column = intval($column);
  92. $from = intval($from);
  93. $number_of_items = intval($number_of_items);
  94. if (!in_array($direction, array('ASC','DESC'))) {
  95. $direction = 'ASC';
  96. }
  97. $att = new Attendance();
  98. $att->set_session_id($session_id);
  99. $att->set_course_int_id($course_id);
  100. $active_plus = 'att.active = 1';
  101. if (api_is_platform_admin()) {
  102. $active_plus = ' 1 = 1 ';
  103. }
  104. $sql = "SELECT
  105. att.id,
  106. att.name,
  107. att.description,
  108. att.attendance_qualify_max,
  109. att.locked,
  110. att.active,
  111. att.session_id
  112. FROM $tbl_attendance att
  113. WHERE c_id = $course_id AND $active_plus $condition_session
  114. ORDER BY $column $direction
  115. LIMIT $from, $number_of_items ";
  116. $res = Database::query($sql);
  117. $attendances = array();
  118. $param_gradebook = '';
  119. if (isset($_SESSION['gradebook'])) {
  120. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  121. }
  122. $user_info = api_get_user_info();
  123. while ($attendance = Database::fetch_array($res,'ASSOC')) {
  124. $id = $attendance['id'];
  125. $student_param = '';
  126. if (api_is_drh() && ($_GET['student_id'])) {
  127. $student_param = '&student_id='.Security::remove_XSS($_GET['student_id']);
  128. }
  129. $session_star = '';
  130. if (api_get_session_id() == $attendance['session_id']) {
  131. $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
  132. }
  133. if ($attendance['active'] == 0) {
  134. $attendance['name'] = "<del>".$attendance['name']."</del>";
  135. }
  136. if ($attendance['locked'] == 1) {
  137. if (api_is_allowed_to_edit(null, true)) {
  138. //Link to edit
  139. $attendance['name'] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$id.$param_gradebook.$student_param.'">'.$attendance['name'].'</a>'.$session_star;
  140. } else {
  141. //Link to view
  142. $attendance['name'] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list_no_edit&attendance_id='.$id.$param_gradebook.$student_param.'">'.$attendance['name'].'</a>'.$session_star;
  143. }
  144. } else {
  145. $attendance['name'] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$id.$param_gradebook.$student_param.'">'.$attendance['name'].'</a>'.$session_star;
  146. }
  147. //description
  148. $attendance['description'] = '<center>'.$attendance['description'].'</center>';
  149. if (api_is_allowed_to_edit(null, true)) {
  150. $actions = '';
  151. $actions .= '<center>';
  152. if (api_is_platform_admin()) {
  153. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$id.$param_gradebook.'">'.
  154. Display::return_icon('edit.png',get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  155. if ($attendance['locked'] == 0) {
  156. // $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=attendance_delete&attendance_id='.$id.$param_gradebook.'">'.Display::return_icon('delete.png',get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  157. }
  158. if ($attendance['active'] == 1) {
  159. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=attendance_delete&attendance_id='.$id.$param_gradebook.'">'.Display::return_icon('delete.png',get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  160. } else {
  161. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToRestore').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=attendance_restore&attendance_id='.$id.$param_gradebook.'">'.Display::return_icon('invisible.png',get_lang('Restore'), array(), ICON_SIZE_SMALL).'</a>';
  162. }
  163. } else {
  164. $is_locked_attendance = self::is_locked_attendance($attendance['id']);
  165. if ($is_locked_attendance) {
  166. $actions .= Display::return_icon('edit_na.png',get_lang('Edit')).'&nbsp;';
  167. $actions .= Display::return_icon('delete_na.png',get_lang('Delete'));
  168. } else {
  169. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$id.$param_gradebook.'">'.Display::return_icon('edit.png',get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  170. $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=attendance_delete&attendance_id='.$id.$param_gradebook.'">'.Display::return_icon('delete.png',get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  171. }
  172. }
  173. // display lock/unlock icon
  174. $is_done_all_calendar = $att->is_all_attendance_calendar_done($id);
  175. if ($is_done_all_calendar) {
  176. $locked = $attendance['attendance_qualify_max'];
  177. if ($locked == 0) {
  178. if (api_is_platform_admin()) {
  179. $message_alert = get_lang('AreYouSureToLockTheAttendance');
  180. } else {
  181. $message_alert = get_lang('UnlockMessageInformation');
  182. }
  183. $actions .= '&nbsp;<a onclick="javascript:if(!confirm(\''.$message_alert.'\')) return false;" href="index.php?'.api_get_cidreq().'&action=lock_attendance&attendance_id='.$id.$param_gradebook.'">'.
  184. Display::return_icon('unlock.png', get_lang('LockAttendance')).'</a>';
  185. } else {
  186. if (api_is_platform_admin()) {
  187. $actions .= '&nbsp;<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToUnlockTheAttendance').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=unlock_attendance&attendance_id='.$id.$param_gradebook.'">'.
  188. Display::return_icon('lock.png', get_lang('UnlockAttendance')).'</a>';
  189. } else {
  190. $actions .= '&nbsp;'.Display::return_icon('locked_na.png',get_lang('LockedAttendance'));
  191. }
  192. }
  193. }
  194. $actions .= '</center>';
  195. $attendance['actions'] = $actions;
  196. $attendances[] = $attendance;
  197. } else {
  198. $attendances[] = $attendance;
  199. }
  200. }
  201. return $attendances;
  202. }
  203. /**
  204. * Get the attendaces by id to display on the current page
  205. * @param int attendance id
  206. * @return array attendance data
  207. */
  208. public function get_attendance_by_id($attendance_id) {
  209. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  210. $attendance_id = intval($attendance_id);
  211. $course_id = $this->get_course_int_id();
  212. $attendance_data = array();
  213. $sql = "SELECT * FROM $tbl_attendance WHERE c_id = $course_id AND id = '$attendance_id'";
  214. $res = Database::query($sql);
  215. if (Database::num_rows($res) > 0) {
  216. while ($row = Database::fetch_array($res)) {
  217. $attendance_data = $row;
  218. }
  219. }
  220. return $attendance_data;
  221. }
  222. /**
  223. * Add attendaces sheet inside table. This is the *list of* dates, not
  224. * a specific date in itself.
  225. * @param bool true for adding link in gradebook or false otherwise (optional)
  226. * @return int last attendance id
  227. */
  228. public function attendance_add($link_to_gradebook = false, $user_id = null) {
  229. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  230. $table_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  231. if (empty($user_id)) {
  232. $user_id = api_get_user_id();
  233. } else {
  234. $user_id = intval($user_id);
  235. }
  236. $course_code = $this->get_course_id();
  237. $session_id = $this->get_session_id();
  238. $course_info = api_get_course_info($course_code);
  239. $course_id = $course_info['real_id'];
  240. $title_gradebook = Database::escape_string($this->attendance_qualify_title);
  241. $weight_calification = floatval($this->attendance_weight);
  242. $value_calification = 0;
  243. $aid = $this->get_last_attendance_id($course_id)+1;
  244. $sql = "INSERT INTO $tbl_attendance SET
  245. c_id = $course_id,
  246. id = $aid,
  247. name ='".Database::escape_string($this->name)."',
  248. description = '".Database::escape_string($this->description)."',
  249. attendance_qualify_title = '$title_gradebook',
  250. attendance_weight = '$weight_calification',
  251. session_id = '$session_id'";
  252. $result = Database::query($sql);
  253. $affected_rows = Database::affected_rows($result);
  254. $last_id = 0;
  255. if (!empty($affected_rows)) {
  256. // save inside item property table
  257. $last_id = $aid;
  258. api_item_property_update($course_info, TOOL_ATTENDANCE, $last_id, "AttendanceAdded", $user_id);
  259. }
  260. // add link to gradebook
  261. if ($link_to_gradebook && !empty($this->category_id)) {
  262. $description = '';
  263. $link_info = is_resource_in_course_gradebook($course_code, 7, $last_id, $session_id);
  264. $link_id = $link_info['id'];
  265. if (!$link_info) {
  266. add_resource_to_course_gradebook($this->category_id, $course_code, 7, $last_id, $title_gradebook,$weight_calification,$value_calification,$description,1,$session_id);
  267. } else {
  268. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_id.'');
  269. }
  270. }
  271. return $last_id;
  272. }
  273. /**
  274. * edit attendaces inside table
  275. * @param int attendance id
  276. * @param bool true for adding link in gradebook or false otherwise (optional)
  277. * @return int last id
  278. */
  279. public function attendance_edit($attendance_id, $link_to_gradebook = false) {
  280. $_course = api_get_course_info();
  281. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  282. $table_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  283. $session_id = api_get_session_id();
  284. $user_id = api_get_user_id();
  285. $attendance_id = intval($attendance_id);
  286. $course_code = $this->get_course_id();
  287. $course_id = $this->get_course_int_id();
  288. $title_gradebook = Database::escape_string($this->attendance_qualify_title);
  289. $value_calification = 0;
  290. $weight_calification= floatval($this->attendance_weight);
  291. if (!empty($attendance_id)) {
  292. $sql = "UPDATE $tbl_attendance
  293. SET name ='".Database::escape_string($this->name)."',
  294. description = '".Database::escape_string($this->description)."',
  295. attendance_qualify_title = '".$title_gradebook."',
  296. attendance_weight = '".$weight_calification."'
  297. WHERE c_id = $course_id AND id = '$attendance_id'";
  298. Database::query($sql);
  299. api_item_property_update($_course, TOOL_ATTENDANCE, $attendance_id,"AttendanceUpdated", $user_id);
  300. // add link to gradebook
  301. if ($link_to_gradebook && !empty($this->category_id)) {
  302. $description = '';
  303. $link_id = is_resource_in_course_gradebook($course_code, 7, $attendance_id, $session_id);
  304. if (!$link_id) {
  305. add_resource_to_course_gradebook($this->category_id, $course_code, 7, $attendance_id, $title_gradebook,$weight_calification,$value_calification,$description,1,$session_id);
  306. } else {
  307. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_id.'');
  308. }
  309. }
  310. return $attendance_id;
  311. }
  312. return null;
  313. }
  314. /**
  315. * Restore attendance
  316. * @param int|array one or many attendances id
  317. * @return int affected rows
  318. */
  319. public function attendance_restore($attendance_id) {
  320. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  321. $user_id = api_get_user_id();
  322. $course_id = $this->get_course_int_id();
  323. $course_info = api_get_course_info_by_id($course_id);
  324. if (is_array($attendance_id)) {
  325. foreach ($attendance_id as $id) {
  326. $id = intval($id);
  327. $sql = "UPDATE $tbl_attendance SET active = 1 WHERE c_id = $course_id AND id = '$id'";
  328. $result = Database::query($sql);
  329. $affected_rows = Database::affected_rows($result);
  330. if (!empty($affected_rows)) {
  331. // update row item property table
  332. api_item_property_update($course_info, TOOL_ATTENDANCE, $id,"restore", $user_id);
  333. }
  334. }
  335. } else {
  336. $attendance_id = intval($attendance_id);
  337. $sql = "UPDATE $tbl_attendance SET active = 1 WHERE c_id = $course_id AND id = '$attendance_id'";
  338. $result = Database::query($sql);
  339. $affected_rows = Database::affected_rows($result);
  340. if (!empty($affected_rows)) {
  341. // update row item property table
  342. api_item_property_update($course_info, TOOL_ATTENDANCE, $attendance_id,"restore", $user_id);
  343. }
  344. }
  345. return $affected_rows;
  346. }
  347. /**
  348. * delete attendaces
  349. * @param int|array one or many attendances id
  350. * @return int affected rows
  351. */
  352. public function attendance_delete($attendance_id) {
  353. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  354. $user_id = api_get_user_id();
  355. $course_id = $this->get_course_int_id();
  356. $course_info = api_get_course_info_by_id($course_id);
  357. if (is_array($attendance_id)) {
  358. foreach ($attendance_id as $id) {
  359. $id = intval($id);
  360. $sql = "UPDATE $tbl_attendance SET active = 0 WHERE c_id = $course_id AND id = '$id'";
  361. $result = Database::query($sql);
  362. $affected_rows = Database::affected_rows($result);
  363. if (!empty($affected_rows)) {
  364. // update row item property table
  365. api_item_property_update($course_info, TOOL_ATTENDANCE, $id,"delete", $user_id);
  366. }
  367. }
  368. } else {
  369. $attendance_id = intval($attendance_id);
  370. $sql = "UPDATE $tbl_attendance SET active = 0 WHERE c_id = $course_id AND id = '$attendance_id'";
  371. $result = Database::query($sql);
  372. $affected_rows = Database::affected_rows($result);
  373. if (!empty($affected_rows)) {
  374. // update row item property table
  375. api_item_property_update($course_info, TOOL_ATTENDANCE, $attendance_id,"delete", $user_id);
  376. }
  377. }
  378. return $affected_rows;
  379. }
  380. /**
  381. * Lock or unlock an attendance
  382. * @param int attendance id
  383. * @param bool True to lock or false otherwise
  384. */
  385. public function lock_attendance($attendance_id, $lock = true) {
  386. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  387. $course_id = $this->get_course_int_id();
  388. $attendance_id = intval($attendance_id);
  389. $locked = ($lock)?1:0;
  390. $upd = "UPDATE $tbl_attendance SET locked = $locked WHERE c_id = $course_id AND id = $attendance_id";
  391. $result = Database::query($upd);
  392. $affected_rows = Database::affected_rows($result);
  393. if ($affected_rows && $lock) {
  394. //save attendance sheet log
  395. $lastedit_type = self::LOCKED_ATTENDANCE_LOG_TYPE;
  396. $lastedit_user_id = api_get_user_id();
  397. $this->save_attendance_sheet_log($attendance_id, api_get_utc_datetime(), $lastedit_type, $lastedit_user_id);
  398. }
  399. return $affected_rows;
  400. }
  401. /**
  402. * Get registered users inside current course
  403. * @param int attendance id for showing attendance result field (optional)
  404. * @return array users data
  405. */
  406. public function get_users_rel_course($attendance_id = 0) {
  407. $current_course_id = $this->get_course_id();
  408. $courseInfo = api_get_course_info($current_course_id);
  409. $courseId = $courseInfo['real_id'];
  410. $current_session_id = $this->get_session_id();
  411. if (!empty($current_session_id)) {
  412. $a_course_users = CourseManager :: get_user_list_from_course_code($current_course_id, $current_session_id,'', 'lastname');
  413. } else {
  414. $a_course_users = CourseManager :: get_user_list_from_course_code($current_course_id, 0, '','lastname');
  415. }
  416. // get registered users inside current course
  417. $a_users = array();
  418. foreach ($a_course_users as $key =>$user_data) {
  419. $value = array();
  420. $uid = $user_data['user_id'];
  421. $status = $user_data['status'];
  422. $user_status_in_session = null;
  423. $user_status_in_course = null;
  424. if ($current_session_id) {
  425. $user_status_in_session = SessionManager::get_user_status_in_course_session($uid, $courseId, $current_session_id);
  426. } else {
  427. $user_status_in_course = CourseManager::get_user_in_course_status($uid, $courseId);
  428. }
  429. //Not taking into account DRH or COURSEMANAGER
  430. if ($uid <= 1 || $status == DRH || $user_status_in_course == COURSEMANAGER || $user_status_in_session == 2) continue;
  431. if (!empty($attendance_id)) {
  432. $user_faults = $this->get_faults_of_user($uid, $attendance_id);
  433. $value['attendance_result'] = $user_faults['faults'].'/'.$user_faults['total'].' ('.$user_faults['faults_porcent'].'%)';
  434. $value['result_color_bar'] = $user_faults['color_bar'];
  435. }
  436. // user's picture
  437. $image_path = UserManager::get_user_picture_path_by_id($uid, 'web', false);
  438. $user_profile = UserManager::get_picture_user($uid, $image_path['file'], 22, USER_IMAGE_SIZE_SMALL, ' width="22" height="22" ');
  439. if (!empty($image_path['file'])) {
  440. $photo = '<center><a class="thickbox" href="'.$image_path['dir'].$image_path['file'].'" ><img src="'.$user_profile['file'].'" '.$user_profile['style'].' alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" title="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" /></a></center>';
  441. } else {
  442. $photo = '<center><img src="'.$user_profile['file'].'" '.$user_profile['style'].' alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" title="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" /></center>';
  443. }
  444. $value['photo'] = $photo;
  445. $value['firstname'] = $user_data['firstname'];
  446. $value['lastname'] = $user_data['lastname'];
  447. $value['username'] = $user_data['username'];
  448. $value['user_id'] = $uid;
  449. //Sending only 5 items in the array instead of 60
  450. $a_users[$key] = $value;
  451. }
  452. return $a_users;
  453. }
  454. public function attendance_sheet_disable($calendar_id, $user_id) {
  455. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  456. $course_id = $this->get_course_int_id();
  457. $user_id = intval($user_id);
  458. $calendar_id = intval($calendar_id);
  459. $sql = "UPDATE $tbl_attendance_sheet SET presence = ''
  460. WHERE c_id = $course_id AND user_id ='$user_id' AND attendance_calendar_id = '$calendar_id'";
  461. Database::query($sql);
  462. return true;
  463. }
  464. public function attendance_sheet_get_info($calendar_id, $user_id) {
  465. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  466. $course_id = $this->get_course_int_id();
  467. $user_id = intval($user_id);
  468. $calendar_id = intval($calendar_id);
  469. $sql = "SELECT * FROM $tbl_attendance_sheet
  470. WHERE c_id = $course_id AND user_id = '$user_id' AND attendance_calendar_id = '$calendar_id'";
  471. $result = Database::query($sql);
  472. if (Database::num_rows($result)) {
  473. return Database::store_result($result, 'ASSOC');
  474. }
  475. return false;
  476. }
  477. /**
  478. * Add attendaces sheet inside table
  479. * @param int attendance calendar id
  480. * @param array present users during current class (array with user_ids)
  481. * @param int attendance id
  482. * @return int affected rows
  483. */
  484. public function attendance_sheet_add($calendar_id, $user_results, $attendance_id, $save_user_absents = true, $done_attendance = true) {
  485. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  486. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  487. $calendar_id = intval($calendar_id);
  488. $attendance_id = intval($attendance_id);
  489. $users = $this->get_users_rel_course();
  490. $course_id = $this->get_course_int_id();
  491. $user_ids = array_keys($users);
  492. $users_absent = array_diff($user_ids, $user_results);
  493. $affected_rows = 0;
  494. // get last edit type
  495. $calendar_data = $this->get_attendance_calendar_by_id($calendar_id);
  496. $lastedit_type = self::DONE_ATTENDANCE_LOG_TYPE;
  497. if (isset($calendar_data['done_attendance']) && $calendar_data['done_attendance']) {
  498. $lastedit_type = self::UPDATED_ATTENDANCE_LOG_TYPE;
  499. }
  500. if (!empty($user_results)) {
  501. // Save users present in class
  502. foreach ($user_results as $uid => $presence) {
  503. $presence = intval($presence);
  504. // check if user already was registered with the $calendar_id
  505. $sql = "SELECT user_id FROM $tbl_attendance_sheet WHERE c_id = $course_id AND user_id = '$uid' AND attendance_calendar_id = '$calendar_id'";
  506. $rs = Database::query($sql);
  507. if (Database::num_rows($rs) == 0) {
  508. $sql = "INSERT INTO $tbl_attendance_sheet SET
  509. c_id = $course_id,
  510. user_id = '$uid',
  511. attendance_calendar_id = '$calendar_id',
  512. presence = $presence";
  513. $result = Database::query($sql);
  514. $affected_rows += Database::affected_rows($result);
  515. } else {
  516. $sql = "UPDATE $tbl_attendance_sheet SET presence = $presence
  517. WHERE c_id = $course_id AND user_id ='$uid' AND attendance_calendar_id = '$calendar_id'";
  518. $result = Database::query($sql);
  519. $affected_rows += Database::affected_rows($result);
  520. }
  521. }
  522. }
  523. /*
  524. // save users absent in class
  525. if ($save_user_absents) {
  526. foreach ($users_absent as $user_absent) {
  527. $uid = intval($user_absent);
  528. // check if user already was registered with the $calendar_id
  529. $sql = "SELECT user_id FROM $tbl_attendance_sheet WHERE c_id = $course_id AND user_id='$uid' AND attendance_calendar_id = '$calendar_id'";
  530. $rs = Database::query($sql);
  531. if (Database::num_rows($rs) == 0) {
  532. $sql = "INSERT INTO $tbl_attendance_sheet SET
  533. c_id = $course_id,
  534. user_id ='$uid',
  535. attendance_calendar_id = '$calendar_id',
  536. presence = 0";
  537. Database::query($sql);
  538. $affected_rows += Database::affected_rows();
  539. } else {
  540. $sql = "UPDATE $tbl_attendance_sheet SET presence = 0 WHERE c_id = $course_id AND user_id ='$uid' AND attendance_calendar_id = '$calendar_id'";
  541. Database::query($sql);
  542. $affected_rows += Database::affected_rows();
  543. }
  544. }
  545. }*/
  546. if ($done_attendance) {
  547. // update done_attendance inside attendance calendar table
  548. $sql = "UPDATE $tbl_attendance_calendar SET done_attendance = 1 WHERE c_id = $course_id AND id = '$calendar_id'";
  549. Database::query($sql);
  550. }
  551. // Save user's results
  552. $this->update_users_results($user_ids, $attendance_id);
  553. if ($affected_rows) {
  554. //save attendance sheet log
  555. $lastedit_user_id = api_get_user_id();
  556. $calendar_date_value = $calendar_data['date_time'];
  557. $this->save_attendance_sheet_log($attendance_id, api_get_utc_datetime(), $lastedit_type, $lastedit_user_id, $calendar_date_value);
  558. }
  559. return $affected_rows;
  560. }
  561. /**
  562. * Unstable version! Needs review! Use only for speed boosts!
  563. * Add attendaces sheet inside table, by group to reduce db stress
  564. * @param array Array of calendar_id, user_id, status, attendance_id, course_id, date, all assumed pre-filtered
  565. * @param bool Whether to save absent users
  566. * @param bool Whether to set done_attendance to 1 in db
  567. * @return int affected rows
  568. */
  569. public function attendance_sheet_group_add($list, $save_user_absents = true, $done_attendance = true) {
  570. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  571. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  572. $user_ids = array();
  573. $date_now = api_get_utc_datetime();
  574. //$calendar_date_value = $calendar_data['date_time'];
  575. $lastedit_user_id = api_get_user_id();
  576. $lastedit_type = self::DONE_ATTENDANCE_LOG_TYPE;
  577. $sql = "INSERT INTO $tbl_attendance_sheet (c_id, user_id, attendance_calendar_id, presence) VALUES ";
  578. foreach ($list as $row) {
  579. if ($row[0] != null) { //ignore placeholders
  580. $sql .= "(".intval($row[4]).','.intval($row[1]).','.intval($row[0]).','.$row[2].'),';
  581. $user_ids[] = intval($row[1]);
  582. }
  583. if ($done_attendance) {
  584. // update done_attendance inside attendance calendar table
  585. $sql2 = "UPDATE $tbl_attendance_calendar SET done_attendance = 1 WHERE c_id = ".intval($row[4])." AND id = '".intval($row[0])."'";
  586. $r2 = Database::query($sql2);
  587. }
  588. // Save user's results
  589. $this->update_users_results(array(intval($row[1])), $attendance_id);
  590. //save attendance sheet log
  591. $this->save_attendance_sheet_log(intval($row[3]), $date_now, $lastedit_type, $lastedit_user_id, $row[5]);
  592. }
  593. $sql = substr($sql,0,-1);
  594. $r = Database::query($sql);
  595. $users_absent = array_diff($user_ids, $user_results);
  596. $affected_rows = Database::affected_rows($r);
  597. return $affected_rows;
  598. }
  599. /**
  600. * update users' attendance results
  601. * @param array registered users inside current course
  602. * @param int attendance id
  603. * @return void
  604. */
  605. public function update_users_results($user_ids, $attendance_id) {
  606. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  607. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  608. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  609. $course_id = $this->get_course_int_id();
  610. $attendance_id = intval($attendance_id);
  611. // fill results about presence of students
  612. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  613. $calendar_ids = array();
  614. // get all dates from calendar by current attendance
  615. foreach ($attendance_calendar as $cal) {
  616. $calendar_ids[] = $cal['id'];
  617. }
  618. //See BT#5419
  619. $status_that_score_point_to_users = self::get_status_that_score_point_to_users();
  620. $status_that_score_point_to_users = "'".implode("','", $status_that_score_point_to_users)."'";
  621. // Get count of presences by users inside current attendance and save like results
  622. $count_presences = 0;
  623. if (count($user_ids) > 0) {
  624. foreach ($user_ids as $uid) {
  625. $count_presences = 0;
  626. if (count($calendar_ids) > 0) {
  627. $sql = "SELECT count(presence) as count_presences
  628. FROM $tbl_attendance_sheet
  629. WHERE c_id = $course_id AND
  630. user_id = '$uid' AND
  631. attendance_calendar_id IN (".implode(',', $calendar_ids).") AND
  632. presence IN ($status_that_score_point_to_users)";
  633. $rs_count = Database::query($sql);
  634. $row_count = Database::fetch_array($rs_count);
  635. $count_presences = $row_count['count_presences'];
  636. }
  637. // Save results
  638. $sql = "SELECT id FROM $tbl_attendance_result WHERE c_id = $course_id AND user_id = '$uid' AND attendance_id='$attendance_id'";
  639. $rs_check_result = Database::query($sql);
  640. if (Database::num_rows($rs_check_result) > 0) {
  641. // update result
  642. $sql = "UPDATE $tbl_attendance_result SET
  643. score = '$count_presences'
  644. WHERE c_id = $course_id AND user_id='$uid' AND attendance_id='$attendance_id'";
  645. Database::query($sql);
  646. } else {
  647. // insert new result
  648. $aid = $this->get_last_attendance_result_id($course_id)+1;
  649. $sql = "INSERT INTO $tbl_attendance_result SET
  650. c_id = $course_id ,
  651. id = $aid ,
  652. user_id = '$uid',
  653. attendance_id = '$attendance_id',
  654. score = '$count_presences'";
  655. Database::query($sql);
  656. }
  657. }
  658. }
  659. // update attendance qualify max
  660. $count_done_calendar = $this->get_done_attendance_calendar($attendance_id);
  661. $sql = "UPDATE $tbl_attendance SET attendance_qualify_max='$count_done_calendar' WHERE c_id = $course_id AND id = '$attendance_id'";
  662. Database::query($sql);
  663. }
  664. /**
  665. * update attendance_sheet_log table, is used as history of an attendance sheet
  666. * @param int Attendance id
  667. * @param string Last edit datetime
  668. * @param string Event type ('locked_attendance', 'done_attendance_sheet' ...)
  669. * @param int Last edit user id
  670. * @param string Calendar datetime value (optional, when event type is 'done_attendance_sheet')
  671. * @return int Affected rows
  672. */
  673. public function save_attendance_sheet_log($attendance_id, $lastedit_date, $lastedit_type, $lastedit_user_id, $calendar_date_value = null) {
  674. $course_id = $this->get_course_int_id();
  675. // define table
  676. $tbl_attendance_sheet_log = Database::get_course_table(TABLE_ATTENDANCE_SHEET_LOG);
  677. // protect data
  678. $attendance_id = intval($attendance_id);
  679. $lastedit_date = Database::escape_string($lastedit_date);
  680. $lastedit_type = Database::escape_string($lastedit_type);
  681. $lastedit_user_id = intval($lastedit_user_id);
  682. if (isset($calendar_date_value)) {
  683. $calendar_date_value = Database::escape_string($calendar_date_value);
  684. } else {
  685. $calendar_date_value = '';
  686. }
  687. // save data
  688. $aid = $this->get_last_attendance_sheet_log_id($course_id)+1;
  689. $ins = "INSERT INTO $tbl_attendance_sheet_log(c_id, id, attendance_id, lastedit_date, lastedit_type, lastedit_user_id, calendar_date_value)
  690. VALUES ($course_id, $aid, $attendance_id, '$lastedit_date', '$lastedit_type', $lastedit_user_id, '$calendar_date_value')";
  691. $result = Database::query($ins);
  692. return Database::affected_rows($result);
  693. }
  694. /**
  695. * Get number of done attendances inside current sheet
  696. * @param int attendance id
  697. * @return int number of done attendances
  698. */
  699. public function get_done_attendance_calendar($attendance_id) {
  700. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  701. $attendance_id = intval($attendance_id);
  702. $course_id = $this->get_course_int_id();
  703. $sql = "SELECT count(done_attendance) as count
  704. FROM $tbl_attendance_calendar
  705. WHERE c_id = $course_id AND
  706. attendance_id = '$attendance_id' AND
  707. done_attendance = 1";
  708. $rs = Database::query($sql);
  709. $row = Database::fetch_array($rs);
  710. $count = $row['count'];
  711. return $count;
  712. }
  713. /**
  714. * Get results of faults (absents) by user
  715. * @param int user id
  716. * @param int attendance id
  717. * @return array results containing number of faults, total done attendance, porcent of faults and color depend on result (red, orange)
  718. */
  719. public function get_faults_of_user($user_id, $attendance_id) {
  720. $user_id = intval($user_id);
  721. $attendance_id = intval($attendance_id);
  722. $results = array();
  723. $attendance_data = $this->get_attendance_by_id($attendance_id);
  724. $calendar_count = $this->get_number_of_attendance_calendar($attendance_id);
  725. $total_done_attendance = $attendance_data['attendance_qualify_max'];
  726. $attendance_user_score = $this->get_user_score($user_id, $attendance_id);
  727. //This is the main change of the BT#1381
  728. //$total_done_attendance = $calendar_count;
  729. // calculate results
  730. $faults = $total_done_attendance - $attendance_user_score;
  731. $faults = $faults > 0 ? $faults:0;
  732. $faults_porcent = $calendar_count > 0 ?round(($faults*100)/$calendar_count,0):0;
  733. $results['faults'] = $faults;
  734. $results['total'] = $calendar_count;
  735. $results['faults_porcent'] = $faults_porcent;
  736. $color_bar = 'success';
  737. if ($faults_porcent > 25 ) {
  738. $color_bar = 'important';
  739. } elseif ($faults_porcent > 10) {
  740. $color_bar = 'warning';
  741. } elseif ($faults_porcent > 1) {
  742. $color_bar = 'info';
  743. }
  744. $results['color_bar'] = $color_bar;
  745. return $results;
  746. }
  747. /**
  748. * Get results of faults average for all courses by user
  749. * @param int user id
  750. * @return array results containing number of faults, total done attendance, porcent of faults and color depend on result (red, orange)
  751. */
  752. public function get_faults_average_inside_courses($user_id) {
  753. // get all courses of current user
  754. $courses = CourseManager::get_courses_list_by_user_id($user_id, true);
  755. $user_id = intval($user_id);
  756. $results = array();
  757. $total_faults = $total_weight = $porcent = 0;
  758. foreach ($courses as $course) {
  759. //$course_code = $course['code'];
  760. //$course_info = api_get_course_info($course_code);
  761. $course_id = $course['real_id'];
  762. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  763. $attendances_by_course = $this->get_attendances_list($course_id);
  764. foreach ($attendances_by_course as $attendance) {
  765. // get total faults and total weight
  766. $total_done_attendance = $attendance['attendance_qualify_max'];
  767. $sql = "SELECT score FROM $tbl_attendance_result
  768. WHERE c_id = $course_id AND user_id = $user_id AND attendance_id = ".$attendance['id'];
  769. $rs = Database::query($sql);
  770. $score = 0;
  771. if (Database::num_rows($rs) > 0) {
  772. $row = Database::fetch_array($rs);
  773. $score = $row['score'];
  774. }
  775. $faults = $total_done_attendance-$score;
  776. $faults = $faults > 0 ? $faults:0;
  777. $total_faults += $faults;
  778. $total_weight += $total_done_attendance;
  779. }
  780. }
  781. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  782. $results['faults'] = $total_faults;
  783. $results['total'] = $total_weight;
  784. $results['porcent'] = $porcent;
  785. return $results;
  786. }
  787. /**
  788. * Get results of faults average by course
  789. * @param int user id
  790. * @param int Session id (optional)
  791. * @return array results containing number of faults, total done attendance, porcent of faults and color depend on result (red, orange)
  792. */
  793. public function get_faults_average_by_course($user_id, $courseId, $session_id = null) {
  794. // Database tables and variables
  795. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  796. $user_id = intval($user_id);
  797. $courseId = intval($courseId);
  798. $results = array();
  799. $total_faults = $total_weight = $porcent = 0;
  800. $attendances_by_course = $this->get_attendances_list($courseId, $session_id);
  801. foreach ($attendances_by_course as $attendance) {
  802. // Get total faults and total weight
  803. $total_done_attendance = $attendance['attendance_qualify_max'];
  804. $sql = "SELECT score FROM $tbl_attendance_result WHERE c_id = $courseId AND user_id = $user_id AND attendance_id=".$attendance['id'];
  805. $rs = Database::query($sql);
  806. $score = 0;
  807. if (Database::num_rows($rs) > 0) {
  808. $row = Database::fetch_array($rs);
  809. $score = $row['score'];
  810. }
  811. $faults = $total_done_attendance-$score;
  812. $faults = $faults > 0 ? $faults:0;
  813. $total_faults += $faults;
  814. $total_weight += $total_done_attendance;
  815. }
  816. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  817. $results['faults'] = $total_faults;
  818. $results['total'] = $total_weight;
  819. $results['porcent'] = $porcent;
  820. return $results;
  821. }
  822. /**
  823. * Get registered users' attendance sheet inside current course
  824. * @param int attendance id
  825. * @param int user id for showing data for only one user (optional)
  826. * @return array users attendance sheet data
  827. */
  828. public function get_users_attendance_sheet($attendance_id, $user_id = 0) {
  829. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  830. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  831. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  832. $calendar_ids = array();
  833. // get all dates from calendar by current attendance
  834. foreach ($attendance_calendar as $cal) {
  835. $calendar_ids[] = $cal['id'];
  836. }
  837. $course_id = $this->get_course_int_id();
  838. $data = array();
  839. if (empty($user_id)) {
  840. // get all registered users inside current course
  841. $users = $this->get_users_rel_course();
  842. $user_ids = array_keys($users);
  843. if (count($calendar_ids) > 0 && count($user_ids) > 0) {
  844. foreach ($user_ids as $uid) {
  845. $sql = "SELECT * FROM $tbl_attendance_sheet
  846. WHERE c_id = $course_id AND
  847. user_id = '$uid' AND
  848. attendance_calendar_id IN(".implode(',', $calendar_ids).") ";
  849. $res = Database::query($sql);
  850. if (Database::num_rows($res) > 0) {
  851. while ($row = Database::fetch_array($res)) {
  852. $data[$uid][$row['attendance_calendar_id']]['presence'] = $row['presence'];
  853. }
  854. }
  855. }
  856. }
  857. } else {
  858. // Get attendance for current user
  859. $user_id = intval($user_id);
  860. if (count($calendar_ids) > 0) {
  861. $sql = "SELECT cal.date_time, att.presence
  862. FROM $tbl_attendance_sheet att
  863. INNER JOIN $tbl_attendance_calendar cal
  864. ON cal.id = att.attendance_calendar_id
  865. WHERE att.c_id = $course_id AND
  866. cal.c_id = $course_id AND
  867. att.user_id = '$user_id' AND
  868. att.attendance_calendar_id IN(".implode(',',$calendar_ids).")
  869. ORDER BY date_time";
  870. $res = Database::query($sql);
  871. if (Database::num_rows($res) > 0) {
  872. while ($row = Database::fetch_array($res)) {
  873. $row['date_time'] = api_convert_and_format_date($row['date_time'], null, date_default_timezone_get());
  874. $data[$user_id][] = $row;
  875. }
  876. }
  877. }
  878. }
  879. return $data;
  880. }
  881. /**
  882. * Get next attendance calendar without presences (done attendances)
  883. * @param int attendance id
  884. * @return int attendance calendar id
  885. */
  886. public function get_next_attendance_calendar_id($attendance_id) {
  887. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  888. $attendance_id = intval($attendance_id);
  889. $course_id = $this->get_course_int_id();
  890. $sql = "SELECT id FROM $tbl_attendance_calendar
  891. WHERE c_id = $course_id AND attendance_id = '$attendance_id' AND done_attendance = 0 ORDER BY date_time limit 1";
  892. $rs = Database::query($sql);
  893. $next_calendar_id = 0;
  894. if (Database::num_rows($rs) > 0) {
  895. $row = Database::fetch_array($rs);
  896. $next_calendar_id = $row['id'];
  897. }
  898. return $next_calendar_id;
  899. }
  900. /**
  901. * Get next attendance calendar datetime without presences (done attendances)
  902. * @param int attendance id
  903. * @return int UNIX time format datetime
  904. */
  905. public function get_next_attendance_calendar_datetime($attendance_id) {
  906. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  907. $course_id = $this->get_course_int_id();
  908. $attendance_id = intval($attendance_id);
  909. $sql = "SELECT id, date_time FROM $tbl_attendance_calendar WHERE c_id = $course_id AND attendance_id = '$attendance_id' AND done_attendance = 0 ORDER BY date_time limit 1";
  910. $rs = Database::query($sql);
  911. $next_calendar_datetime = 0;
  912. if (Database::num_rows($rs) > 0) {
  913. $row = Database::fetch_array($rs);
  914. $next_calendar_datetime = api_get_local_time($row['date_time']);
  915. }
  916. return $next_calendar_datetime;
  917. }
  918. /**
  919. * Get user' score from current attendance
  920. * @param int user id
  921. * @param int attendance id
  922. * @return int score
  923. */
  924. public function get_user_score($user_id, $attendance_id) {
  925. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  926. $user_id = intval($user_id);
  927. $attendance_id = intval($attendance_id);
  928. $course_id = $this->get_course_int_id();
  929. $sql = "SELECT score FROM $tbl_attendance_result
  930. WHERE c_id = $course_id AND user_id='$user_id' AND attendance_id='$attendance_id'";
  931. $rs = Database::query($sql);
  932. $score = 0;
  933. if (Database::num_rows($rs) > 0) {
  934. $row = Database::fetch_array($rs);
  935. $score = $row['score'];
  936. }
  937. return $score;
  938. }
  939. /**
  940. * Get attendance calendar data by id
  941. * @param int attendance calendar id
  942. * @return array attendance calendar data
  943. */
  944. public function get_attendance_calendar_by_id($calendar_id) {
  945. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  946. $calendar_id = intval($calendar_id);
  947. $course_id = $this->get_course_int_id();
  948. $sql = "SELECT * FROM $tbl_attendance_calendar WHERE c_id = $course_id AND id = '$calendar_id' ";
  949. $rs = Database::query($sql);
  950. $data = array();
  951. if (Database::num_rows($rs) > 0) {
  952. while ($row = Database::fetch_array($rs)) {
  953. $row['date_time'] = api_get_local_time($row['date_time']);
  954. $data = $row;
  955. }
  956. }
  957. return $data;
  958. }
  959. /**
  960. * Get all attendance calendar data inside current attendance
  961. * @param int attendance id
  962. * @return array attendance calendar data
  963. */
  964. public function get_attendance_calendar($attendance_id, $type = 'all', $calendar_id = null) {
  965. global $dateFormatShort, $timeNoSecFormat;
  966. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  967. $attendance_id = intval($attendance_id);
  968. $course_id = $this->get_course_int_id();
  969. $sql = "SELECT * FROM $tbl_attendance_calendar WHERE c_id = $course_id AND attendance_id = '$attendance_id' ";
  970. if (!in_array($type, array('today', 'all', 'all_done', 'all_not_done','calendar_id'))) {
  971. $type = 'all';
  972. }
  973. switch ($type) {
  974. case 'calendar_id':
  975. $calendar_id = intval($calendar_id);
  976. if (!empty($calendar_id)) {
  977. $sql.= " AND id = $calendar_id";
  978. }
  979. break;
  980. case 'today':
  981. //$sql .= ' AND DATE_FORMAT(date_time,"%d-%m-%Y") = DATE_FORMAT("'.api_get_utc_datetime().'", "%d-%m-%Y" )';
  982. break;
  983. case 'all_done':
  984. $sql .= " AND done_attendance = 1 ";
  985. break;
  986. case 'all_not_done':
  987. $sql .= " AND done_attendance = 0 ";
  988. break;
  989. case 'all':
  990. default:
  991. break;
  992. }
  993. $sql .= " ORDER BY date_time ";
  994. $rs = Database::query($sql);
  995. $data = array();
  996. if (Database::num_rows($rs) > 0) {
  997. while ($row = Database::fetch_array($rs,'ASSOC')) {
  998. $row['db_date_time'] = $row['date_time'];
  999. $row['date_time'] = api_get_local_time($row['date_time']);
  1000. $row['date'] = api_format_date($row['date_time'], DATE_FORMAT_SHORT);
  1001. $row['time'] = api_format_date($row['date_time'], TIME_NO_SEC_FORMAT);
  1002. if ($type == 'today') {
  1003. if (date('d-m-Y', api_strtotime($row['date_time'], 'UTC')) == date('d-m-Y', time())) {
  1004. $data[] = $row;
  1005. }
  1006. } else {
  1007. $data[] = $row;
  1008. }
  1009. }
  1010. }
  1011. return $data;
  1012. }
  1013. /**
  1014. * Get number of attendance calendar inside current attendance
  1015. * @param int attendance id
  1016. * @return int number of dates in attendance calendar
  1017. */
  1018. public function get_number_of_attendance_calendar($attendance_id) {
  1019. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1020. $attendance_id = intval($attendance_id);
  1021. $course_id = $this->get_course_int_id();
  1022. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1023. WHERE c_id = $course_id AND attendance_id = '$attendance_id'";
  1024. $rs = Database::query($sql);
  1025. $row = Database::fetch_row($rs);
  1026. $count = $row[0];
  1027. return $count;
  1028. }
  1029. /**
  1030. * Get count dates inside attendance calendar by attendance id
  1031. * @param int attendance id
  1032. * @return int count of dates
  1033. */
  1034. public function get_count_dates_inside_attendance_calendar($attendance_id) {
  1035. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1036. $attendance_id = intval($attendance_id);
  1037. $course_id = $this->get_course_int_id();
  1038. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1039. WHERE c_id = $course_id AND attendance_id = '$attendance_id'";
  1040. $rs = Database::query($sql);
  1041. $count = 0;
  1042. if (Database::num_rows($rs) > 0) {
  1043. $row = Database::fetch_row($rs);
  1044. $count = $row[0];
  1045. }
  1046. return $count;
  1047. }
  1048. /**
  1049. * check if all calendar of an attendance is done
  1050. * @param int attendance id
  1051. * @return bool True if all calendar is done, otherwise false
  1052. */
  1053. public function is_all_attendance_calendar_done($attendance_id) {
  1054. $attendance_id = intval($attendance_id);
  1055. $done_calendar = $this->get_done_attendance_calendar($attendance_id);
  1056. $count_dates_in_calendar = $this->get_count_dates_inside_attendance_calendar($attendance_id);
  1057. $number_of_dates = $this->get_number_of_attendance_calendar($attendance_id);
  1058. $result = false;
  1059. if ($number_of_dates && (intval($count_dates_in_calendar) == intval($done_calendar))) {
  1060. $result = true;
  1061. }
  1062. return $result;
  1063. }
  1064. /**
  1065. * check if an attendance is locked
  1066. * @param int attendance id
  1067. * @param bool
  1068. */
  1069. public static function is_locked_attendance($attendance_id) {
  1070. //use gradebook lock
  1071. $result = api_resource_is_locked_by_gradebook($attendance_id, LINK_ATTENDANCE);
  1072. return $result;
  1073. }
  1074. public function get_attendance_calendar_data_by_date($attendance_id, $date_time) {
  1075. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1076. $course_id = $this->get_course_int_id();
  1077. $attendance_id = intval($attendance_id);
  1078. // check if datetime already exists inside the table
  1079. $sql = "SELECT * FROM $tbl_attendance_calendar
  1080. WHERE c_id = $course_id AND
  1081. date_time = '".Database::escape_string($date_time)."' AND
  1082. attendance_id = '$attendance_id'";
  1083. $result = Database::query($sql);
  1084. if (Database::num_rows($result)) {
  1085. return Database::fetch_array($result, 'ASSOC');
  1086. } else {
  1087. return false;
  1088. }
  1089. }
  1090. /**
  1091. * Add new datetime inside attendance calendar table
  1092. * @param int attendance id
  1093. * @return int affected rows
  1094. */
  1095. public function attendance_calendar_add($attendance_id, $return_inserted_id = false) {
  1096. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1097. $affected_rows = 0;
  1098. $cal_id = null;
  1099. $attendance_id = intval($attendance_id);
  1100. $course_id = $this->get_course_int_id();
  1101. $calendar_info = $this->get_attendance_calendar_data_by_date($attendance_id, $this->date_time);
  1102. if ($calendar_info == false) {
  1103. $aid = $this->get_last_attendance_calendar_id($course_id)+1;
  1104. $sql = "INSERT INTO $tbl_attendance_calendar SET
  1105. c_id = $course_id,
  1106. id = $aid,
  1107. date_time = '".Database::escape_string($this->date_time)."',
  1108. attendance_id = '$attendance_id'";
  1109. $result = Database::query($sql);
  1110. $cal_id = $aid;
  1111. $affected_rows = Database::affected_rows($result);
  1112. }
  1113. // update locked attendance
  1114. $is_all_calendar_done = $this->is_all_attendance_calendar_done($attendance_id);
  1115. if (!$is_all_calendar_done) {
  1116. $unlock = self::lock_attendance($attendance_id, false);
  1117. } else {
  1118. $unlock = self::lock_attendance($attendance_id);
  1119. }
  1120. if ($return_inserted_id) {
  1121. return $cal_id;
  1122. }
  1123. return $affected_rows;
  1124. }
  1125. /**
  1126. * save repeated date inside attendance calendar table
  1127. * @param int attendance id
  1128. * @param int start date in tms
  1129. * @param int end date in tms
  1130. * @param string repeat type daily, weekly, monthlyByDate
  1131. */
  1132. public function attendance_repeat_calendar_add($attendance_id, $start_date, $end_date, $repeat_type) {
  1133. $attendance_id = intval($attendance_id);
  1134. // save start date
  1135. $datetimezone = api_get_utc_datetime($start_date);
  1136. $this->set_date_time($datetimezone);
  1137. $res = $this->attendance_calendar_add($attendance_id);
  1138. //86400 = 24 hours in seconds
  1139. //604800 = 1 week in seconds
  1140. // Saves repeated dates
  1141. switch($repeat_type) {
  1142. case 'daily':
  1143. $j = 1;
  1144. for ($i = $start_date + 86400; ($i <= $end_date); $i += 86400) {
  1145. //$datetimezone = api_get_utc_date_add(api_get_utc_datetime($start_date), 0 , 0, $j); //to support europe timezones
  1146. $datetimezone = api_get_utc_datetime($i);
  1147. $this->set_date_time($datetimezone);
  1148. $res = $this->attendance_calendar_add($attendance_id);
  1149. $j++;
  1150. }
  1151. break;
  1152. exit;
  1153. case 'weekly':
  1154. $j = 1;
  1155. for ($i = $start_date + 604800; ($i <= $end_date); $i += 604800) {
  1156. $datetimezone = api_get_utc_datetime($i);
  1157. //$datetimezone = api_get_utc_date_add(api_get_utc_datetime($start_date), 0 , 0, $j*7); //to support europe timezones
  1158. $this->set_date_time($datetimezone);
  1159. $res = $this->attendance_calendar_add($attendance_id);
  1160. $j++;
  1161. }
  1162. break;
  1163. case 'monthlyByDate':
  1164. $j = 1;
  1165. //@todo fix bug with february
  1166. for ($i = $start_date + 2419200; ($i <= $end_date); $i += 2419200) {
  1167. $datetimezone = api_get_utc_datetime($i);
  1168. //$datetimezone = api_get_utc_date_add(api_get_utc_datetime($start_date), 0 , $j); //to support europe timezones
  1169. $this->set_date_time($datetimezone);
  1170. $res = $this->attendance_calendar_add($attendance_id);
  1171. $j++;
  1172. }
  1173. break;
  1174. }
  1175. }
  1176. /**
  1177. * Adds x months to a UNIX timestamp
  1178. * @param int The timestamp
  1179. * @param int The number of years to add
  1180. * @return int The new timestamp
  1181. */
  1182. private function add_month($timestamp, $num=1) {
  1183. $values = api_get_utc_datetime($timestamp);
  1184. $values = str_replace(array(':','-',' '), '/', $values);
  1185. list($y, $m, $d, $h, $n, $s) = split('/',$values);
  1186. if($m+$num>12) {
  1187. $y += floor($num/12);
  1188. $m += $num%12;
  1189. } else {
  1190. $m += $num;
  1191. }
  1192. //date_default_timezone_set('UTC');
  1193. // return mktime($h, $n, $s, $m, $d, $y);
  1194. $result = api_strtotime($y.'-'.$m.'-'.$d.' '.$h.':'.$n.':'.$s, 'UTC');
  1195. if (!empty($result)) {
  1196. return $result;
  1197. }
  1198. return false;
  1199. }
  1200. /**
  1201. * edit a datetime inside attendance calendar table
  1202. * @param int attendance calendar id
  1203. * @param int attendance id
  1204. * @return int affected rows
  1205. */
  1206. public function attendance_calendar_edit($calendar_id, $attendance_id) {
  1207. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1208. $affected_rows = 0;
  1209. $attendance_id = intval($attendance_id);
  1210. $course_id = api_get_course_int_id();
  1211. // check if datetime already exists inside the table
  1212. $sql = "SELECT id FROM $tbl_attendance_calendar
  1213. WHERE c_id = $course_id AND date_time = '".Database::escape_string($this->date_time)."' AND attendance_id = '$attendance_id'";
  1214. $rs = Database::query($sql);
  1215. if (Database::num_rows($rs) == 0) {
  1216. $sql = "UPDATE $tbl_attendance_calendar SET date_time='".Database::escape_string($this->date_time)."' WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1217. $result = Database::query($sql);
  1218. $affected_rows = Database::affected_rows($result);
  1219. }
  1220. // update locked attendance
  1221. $is_all_calendar_done = self::is_all_attendance_calendar_done($attendance_id);
  1222. if (!$is_all_calendar_done) {
  1223. $unlock = self::lock_attendance($attendance_id, false);
  1224. } else {
  1225. $unlock = self::lock_attendance($attendance_id);
  1226. }
  1227. return $affected_rows;
  1228. }
  1229. /**
  1230. * delete a datetime from attendance calendar table
  1231. * @param int attendance calendar id
  1232. * @param int attendance id
  1233. * @param bool true for removing all calendar inside current attendance, false for removing by calendar id
  1234. * @return int affected rows
  1235. */
  1236. public function attendance_calendar_delete($calendar_id, $attendance_id , $all_delete = false) {
  1237. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1238. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1239. $session_id = api_get_session_id();
  1240. $attendance_id = intval($attendance_id);
  1241. // get all registered users inside current course
  1242. $users = $this->get_users_rel_course();
  1243. $user_ids = array_keys($users);
  1244. $course_id = api_get_course_int_id();
  1245. if ($all_delete) {
  1246. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  1247. $calendar_ids = array();
  1248. // get all dates from calendar by current attendance
  1249. if (!empty($attendance_calendar)) {
  1250. foreach ($attendance_calendar as $cal) {
  1251. // delete all data from attendance sheet
  1252. $sql = "DELETE FROM $tbl_attendance_sheet WHERE c_id = $course_id AND attendance_calendar_id = '".intval($cal['id'])."'";
  1253. Database::query($sql);
  1254. // delete data from attendance calendar
  1255. $sql = "DELETE FROM $tbl_attendance_calendar WHERE c_id = $course_id AND id = '".intval($cal['id'])."'";
  1256. $result = Database::query($sql);
  1257. }
  1258. }
  1259. } else {
  1260. // delete just one row from attendance sheet by the calendar id
  1261. $sql = "DELETE FROM $tbl_attendance_sheet WHERE c_id = $course_id AND attendance_calendar_id = '".intval($calendar_id)."'";
  1262. Database::query($sql);
  1263. // delete data from attendance calendar
  1264. $sql = "DELETE FROM $tbl_attendance_calendar WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1265. $result = Database::query($sql);
  1266. }
  1267. $affected_rows = Database::affected_rows($result);
  1268. // update users' results
  1269. $this->update_users_results($user_ids, $attendance_id);
  1270. return $affected_rows;
  1271. }
  1272. /**
  1273. * buid a string datetime from array
  1274. * @param array array containing data e.g: $array('Y'=>'2010', 'F' => '02', 'd' => '10', 'H' => '12', 'i' => '30')
  1275. * @return string date and time e.g: '2010-02-10 12:30:00'
  1276. */
  1277. public function build_datetime_from_array($array) {
  1278. return Text::return_datetime_from_array($array);
  1279. }
  1280. /** Setters for fields of attendances tables **/
  1281. public function set_session_id($session_id) {
  1282. $this->session_id = intval($session_id);
  1283. }
  1284. public function set_course_id($course_id) {
  1285. $this->course_id = Database::escape_string($course_id);
  1286. }
  1287. public function set_date_time($datetime) {
  1288. $this->date_time = $datetime;
  1289. }
  1290. public function set_name($name) {
  1291. $this->name = $name;
  1292. }
  1293. public function set_description($description) {
  1294. $this->description = $description;
  1295. }
  1296. public function set_attendance_qualify_title($attendance_qualify_title) {
  1297. $this->attendance_qualify_title = $attendance_qualify_title;
  1298. }
  1299. public function set_attendance_weight($attendance_weight) {
  1300. $this->attendance_weight = $attendance_weight;
  1301. }
  1302. /** Getters for fields of attendances tables **/
  1303. public function get_session_id() {
  1304. return isset($this->session_id) ? $this->session_id : api_get_session_id();
  1305. }
  1306. public function get_course_id() {
  1307. return isset($this->course_id) ? $this->course_id : api_get_course_id();
  1308. }
  1309. public function get_date_time() {
  1310. return $this->date_time;
  1311. }
  1312. public function get_name() {
  1313. return $this->name;
  1314. }
  1315. public function get_description() {
  1316. return $this->description;
  1317. }
  1318. public function get_attendance_qualify_title() {
  1319. return $this->attendance_qualify_title;
  1320. }
  1321. public function get_attendance_weight() {
  1322. return $this->attendance_weight;
  1323. }
  1324. public function get_course_int_id() {
  1325. return isset($this->course_int_id) ? $this->course_int_id : api_get_course_int_id();
  1326. }
  1327. public function set_course_int_id($course_id) {
  1328. $this->course_int_id = intval($course_id);
  1329. }
  1330. public function get_attendance_states() {
  1331. $attendance_states = array(
  1332. '1' => array('label' => get_lang('Present'), 'class' => 'btn-success') ,
  1333. '2' => array('label' => get_lang('Late'), 'class' => 'btn-info') ,
  1334. '3' => array('label' => get_lang('VeryLate'), 'class' => 'btn-warning') ,
  1335. '0' => array('label' => get_lang('Absent'), 'class' => 'btn-danger') ,
  1336. '4' => array('label' => get_lang('Default'), 'class' => 'btn') ,
  1337. );
  1338. return $attendance_states;
  1339. }
  1340. public function get_status_that_score_point_to_users() {
  1341. return array(
  1342. 1, //presence
  1343. 2, //late
  1344. 3, //very late
  1345. );
  1346. }
  1347. public function get_default_attendance_state() {
  1348. return 4;
  1349. }
  1350. public function get_attendance_state_button($state_id, $add_label = false, $extra_attributes = array()) {
  1351. $state_list = $this->get_attendance_states();
  1352. if (isset($state_list[$state_id])) {
  1353. $label = '&nbsp;';
  1354. if ($add_label) {
  1355. $label = $state_list[$state_id]['label'];
  1356. }
  1357. $default_class = isset($extra_attributes['class']) ? $extra_attributes['class'] : null;
  1358. $extra_attributes['class'] = $default_class." btn ".$state_list[$state_id]['class'];
  1359. return Display::url($label, '', $extra_attributes);
  1360. }
  1361. return null;
  1362. }
  1363. /**
  1364. * Gets the last attendance ID for this course (helper for auto_increment substitution)
  1365. * @param int Course ID (if not provided, will be guessed from context)
  1366. * @result int 0 if none, the max "id" value for this course, if found
  1367. */
  1368. function get_last_attendance_id($course_id=null)
  1369. {
  1370. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  1371. if (empty($course_id)) {
  1372. $course_id = api_get_course_int_id();
  1373. } else {
  1374. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  1375. }
  1376. $id = 0;
  1377. $sql = "SELECT max(id) FROM $tbl_attendance WHERE c_id = $course_id";
  1378. $res = Database::query($sql);
  1379. if ($res === false or Database::num_rows($res)===0) {
  1380. return $id;
  1381. } //else
  1382. $row = Database::fetch_row($res);
  1383. return $row[0];
  1384. }
  1385. /**
  1386. * Gets the last attendance result ID for this course (helper for auto_increment substitution)
  1387. * @param int Course ID (if not provided, will be guessed from context)
  1388. * @result int 0 if none, the max "id" value for this course, if found
  1389. */
  1390. function get_last_attendance_result_id($course_id=null)
  1391. {
  1392. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE_RESULT);
  1393. if (empty($course_id)) {
  1394. $course_id = api_get_course_int_id();
  1395. } else {
  1396. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  1397. }
  1398. $id = 0;
  1399. $sql = "SELECT max(id) FROM $tbl_attendance WHERE c_id = $course_id";
  1400. $res = Database::query($sql);
  1401. if ($res === false or Database::num_rows($res)===0) {
  1402. return $id;
  1403. } //else
  1404. $row = Database::fetch_row($res);
  1405. return $row[0];
  1406. }
  1407. /**
  1408. * Gets the last attendance sheet log ID for this course (helper for auto_increment substitution)
  1409. * @param int Course ID (if not provided, will be guessed from context)
  1410. * @result int 0 if none, the max "id" value for this course, if found
  1411. */
  1412. function get_last_attendance_sheet_log_id($course_id=null)
  1413. {
  1414. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE_SHEET_LOG);
  1415. if (empty($course_id)) {
  1416. $course_id = api_get_course_int_id();
  1417. } else {
  1418. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  1419. }
  1420. $id = 0;
  1421. $sql = "SELECT max(id) FROM $tbl_attendance WHERE c_id = $course_id";
  1422. $res = Database::query($sql);
  1423. if ($res === false or Database::num_rows($res)===0) {
  1424. return $id;
  1425. } //else
  1426. $row = Database::fetch_row($res);
  1427. return $row[0];
  1428. }
  1429. /**
  1430. * Gets the last attendance calendar ID for this course (helper for auto_increment substitution)
  1431. * @param int Course ID (if not provided, will be guessed from context)
  1432. * @result int 0 if none, the max "id" value for this course, if found
  1433. */
  1434. function get_last_attendance_calendar_id($course_id=null)
  1435. {
  1436. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1437. if (empty($course_id)) {
  1438. $course_id = api_get_course_int_id();
  1439. } else {
  1440. $course_id = filter_var($course_id, FILTER_SANITIZE_NUMBER_INT);
  1441. }
  1442. $id = 0;
  1443. $sql = "SELECT max(id) FROM $tbl_attendance WHERE c_id = $course_id";
  1444. $res = Database::query($sql);
  1445. if ($res === false or Database::num_rows($res)===0) {
  1446. return $id;
  1447. } //else
  1448. $row = Database::fetch_row($res);
  1449. return $row[0];
  1450. }
  1451. }