attendance.lib.php 64 KB

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