attendance.lib.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains class used like library, provides functions for attendance tool.
  5. * It's also used like model to attendance_controller (MVC pattern)
  6. * @author Christian Fasanando <christian1827@gmail.com>
  7. * @author Julio Montoya <gugli100@gmail.com> improvements
  8. * @package chamilo.attendance
  9. *
  10. */
  11. class Attendance
  12. {
  13. private $session_id;
  14. private $course_id;
  15. private $date_time;
  16. private $name;
  17. private $description;
  18. private $attendance_qualify_title;
  19. private $attendance_weight;
  20. private $course_int_id;
  21. public $category_id;
  22. // constants
  23. const DONE_ATTENDANCE_LOG_TYPE = 'done_attendance_sheet';
  24. const UPDATED_ATTENDANCE_LOG_TYPE = 'updated_attendance_sheet';
  25. const LOCKED_ATTENDANCE_LOG_TYPE = 'locked_attendance_sheet';
  26. /**
  27. * Constructor
  28. */
  29. public function __construct()
  30. {
  31. //$this->course_int_id = api_get_course_int_id();
  32. }
  33. /**
  34. * Get the total number of attendance inside current course and current session
  35. * @see SortableTable#get_total_number_of_items()
  36. */
  37. public static function get_number_of_attendances($active = -1)
  38. {
  39. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  40. $session_id = api_get_session_id();
  41. $condition_session = api_get_session_condition($session_id);
  42. $course_id = api_get_course_int_id();
  43. $sql = "SELECT COUNT(att.id) AS total_number_of_items
  44. FROM $tbl_attendance att
  45. WHERE c_id = $course_id $condition_session ";
  46. if ($active == 1 || $active == 0) {
  47. $sql .= "AND att.active = $active";
  48. }
  49. $res = Database::query($sql);
  50. $obj = Database::fetch_object($res);
  51. return $obj->total_number_of_items;
  52. }
  53. /**
  54. * Get attendance list only the id, name and attendance_qualify_max fields
  55. * @param string course db name (optional)
  56. * @param int session id (optional)
  57. * @return array attendances list
  58. */
  59. public function get_attendances_list($course_id = '', $session_id = null)
  60. {
  61. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  62. $data = array();
  63. if (empty($course_id)) {
  64. $course_id = api_get_course_int_id();
  65. } else {
  66. $course_id = intval($course_id);
  67. }
  68. $session_id = isset($session_id)?intval($session_id):api_get_session_id();
  69. $condition_session = api_get_session_condition($session_id);
  70. // Get attendance data
  71. $sql = "SELECT id, name, attendance_qualify_max
  72. FROM $tbl_attendance
  73. WHERE c_id = $course_id AND active = 1 $condition_session ";
  74. $rs = Database::query($sql);
  75. if (Database::num_rows($rs) > 0) {
  76. while ($row = Database::fetch_array($rs,'ASSOC')) {
  77. $data[$row['id']] = $row;
  78. }
  79. }
  80. return $data;
  81. }
  82. /**
  83. * Get the attendaces to display on the current page (fill the sortable-table)
  84. * @param int offset of first user to recover
  85. * @param int Number of users to get
  86. * @param int Column to sort on
  87. * @param string Order (ASC,DESC)
  88. * @see SortableTable#get_table_data($from)
  89. */
  90. public static function get_attendance_data($from, $number_of_items, $column, $direction)
  91. {
  92. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  93. $course_id = api_get_course_int_id();
  94. $session_id = api_get_session_id();
  95. $condition_session = api_get_session_condition($session_id);
  96. $column = intval($column);
  97. $from = intval($from);
  98. $number_of_items = intval($number_of_items);
  99. if (!in_array($direction, array('ASC','DESC'))) {
  100. $direction = 'ASC';
  101. }
  102. $active_plus = '';
  103. if ((isset($_GET['isStudentView']) && $_GET['isStudentView'] == 'true') ||
  104. !api_is_allowed_to_edit(null, true)
  105. ) {
  106. $active_plus = ' AND att.active = 1';
  107. }
  108. $sql = "SELECT
  109. att.id AS col0,
  110. att.name AS col1,
  111. att.description AS col2,
  112. att.attendance_qualify_max AS col3,
  113. att.locked AS col4,
  114. att.active AS col5,
  115. att.session_id
  116. FROM $tbl_attendance att
  117. WHERE
  118. att.active <> 2 AND
  119. c_id = $course_id $active_plus $condition_session
  120. ORDER BY col$column $direction
  121. LIMIT $from,$number_of_items ";
  122. $res = Database::query($sql);
  123. $attendances = array ();
  124. $user_info = api_get_user_info();
  125. $allowDelete = api_get_setting('allow_delete_attendance');
  126. while ($attendance = Database::fetch_row($res)) {
  127. $student_param = '';
  128. if (api_is_drh() && $_GET['student_id']) {
  129. $student_param = '&student_id='.intval($_GET['student_id']);
  130. }
  131. $session_star = '';
  132. if (api_get_session_id() == $attendance[6]) {
  133. $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
  134. }
  135. if ($attendance[5] == 1) {
  136. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  137. api_get_user_id(),
  138. api_get_course_info()
  139. );
  140. if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
  141. // Link to edit
  142. $attendance[1] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$attendance[0].$student_param.'">'.$attendance[1].'</a>'.$session_star;
  143. } else {
  144. // Link to view
  145. $attendance[1] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list_no_edit&attendance_id='.$attendance[0].$student_param.'">'.$attendance[1].'</a>'.$session_star;
  146. }
  147. } else {
  148. $attendance[1] = '<a class="muted" href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$attendance[0].$student_param.'">'.$attendance[1].'</a>'.$session_star;
  149. }
  150. if ($attendance[5] == 1) {
  151. $attendance[3] = '<center>'.$attendance[3].'</center>';
  152. } else {
  153. $attendance[3] = '<center><span class="muted">'.$attendance[3].'</span></center>';
  154. }
  155. $attendance[3] = '<center>'.$attendance[3].'</center>';
  156. if (api_is_allowed_to_edit(null, true)) {
  157. $actions = '';
  158. $actions .= '<center>';
  159. if (api_is_platform_admin()) {
  160. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$attendance[0].'">'.
  161. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  162. // Visible
  163. if ($attendance[5] == 1) {
  164. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_set_invisible&attendance_id='.$attendance[0].'">'.
  165. Display::return_icon('visible.png', get_lang('Hide'), array(), ICON_SIZE_SMALL).'</a>';
  166. } else {
  167. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_set_visible&attendance_id='.$attendance[0].'">'.
  168. Display::return_icon('invisible.png', get_lang('Show'), array(), ICON_SIZE_SMALL).'</a>';
  169. $attendance[2] = '<span class="muted">'.$attendance[2].'</span>';
  170. }
  171. if ($allowDelete === 'true') {
  172. $actions .= '<a href="index.php?' . api_get_cidreq() . '&action=attendance_delete&attendance_id=' . $attendance[0]. '">' .
  173. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  174. }
  175. } else {
  176. $is_locked_attendance = self::is_locked_attendance($attendance[0]);
  177. if ($is_locked_attendance) {
  178. $actions .= Display::return_icon('edit_na.png', get_lang('Edit')).'&nbsp;';
  179. $actions .= Display::return_icon('visible.png', get_lang('Hide'));
  180. } else {
  181. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$attendance[0].'">'.
  182. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  183. if ($attendance[5] == 1) {
  184. $actions .= ' <a href="index.php?'.api_get_cidreq().'&action=attendance_set_invisible&attendance_id='.$attendance[0].'">'.
  185. Display::return_icon('visible.png', get_lang('Hide'), array(), ICON_SIZE_SMALL).'</a>';
  186. } else {
  187. $actions .= ' <a href="index.php?'.api_get_cidreq().'&action=attendance_set_visible&attendance_id='.$attendance[0].'">'.
  188. Display::return_icon('invisible.png', get_lang('Show'), array(), ICON_SIZE_SMALL).'</a>';
  189. $attendance[2] = '<span class="muted">'.$attendance[2].'</span>';
  190. }
  191. if ($allowDelete === 'true') {
  192. $actions .= ' <a href="index.php?' . api_get_cidreq() . '&action=attendance_delete&attendance_id=' . $attendance[0].'">' .
  193. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  194. }
  195. }
  196. }
  197. // display lock/unlock icon
  198. $is_done_all_calendar = self::is_all_attendance_calendar_done($attendance[0]);
  199. if ($is_done_all_calendar) {
  200. $locked = $attendance[4];
  201. if ($locked == 0) {
  202. if (api_is_platform_admin()) {
  203. $message_alert = get_lang('AreYouSureToLockTheAttendance');
  204. } else {
  205. $message_alert = get_lang('UnlockMessageInformation');
  206. }
  207. $actions .= '&nbsp;<a onclick="javascript:if(!confirm(\''.$message_alert.'\')) return false;" href="index.php?'.api_get_cidreq().'&action=lock_attendance&attendance_id='.$attendance[0].'">'.
  208. Display::return_icon('unlock.png', get_lang('LockAttendance')).'</a>';
  209. } else {
  210. if (api_is_platform_admin()) {
  211. $actions .= '&nbsp;<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToUnlockTheAttendance').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=unlock_attendance&attendance_id='.$attendance[0].'">'.
  212. Display::return_icon('locked.png', get_lang('UnlockAttendance')).'</a>';
  213. } else {
  214. $actions .= '&nbsp;'.Display::return_icon('locked_na.png', get_lang('LockedAttendance'));
  215. }
  216. }
  217. }
  218. $actions .= '</center>';
  219. $attendances[] = array($attendance[0], $attendance[1], $attendance[2], $attendance[3],$actions);
  220. } else {
  221. $attendance[0] = '&nbsp;';
  222. $attendances[] = array($attendance[0], $attendance[1], $attendance[2], $attendance[3]);
  223. }
  224. }
  225. return $attendances;
  226. }
  227. /**
  228. * Get the attendances by id to display on the current page
  229. * @param int $attendance_id
  230. * @return array attendance data
  231. */
  232. public function get_attendance_by_id($attendance_id)
  233. {
  234. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  235. $attendance_id = intval($attendance_id);
  236. $course_id = api_get_course_int_id();
  237. $attendance_data = array();
  238. $sql = "SELECT * FROM $tbl_attendance
  239. WHERE c_id = $course_id AND id = '$attendance_id'";
  240. $res = Database::query($sql);
  241. if (Database::num_rows($res) > 0) {
  242. while ($row = Database::fetch_array($res)) {
  243. $attendance_data = $row;
  244. }
  245. }
  246. return $attendance_data;
  247. }
  248. /**
  249. * Add attendances sheet inside table. This is the *list of* dates, not
  250. * a specific date in itself.
  251. * @param bool true for adding link in gradebook or false otherwise (optional)
  252. * @return int last attendance id
  253. */
  254. public function attendance_add($link_to_gradebook = false)
  255. {
  256. $_course = api_get_course_info();
  257. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  258. $table_link = Database:: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  259. $session_id = api_get_session_id();
  260. $user_id = api_get_user_id();
  261. $course_code = $_course['code'];
  262. $course_id = $_course['real_id'];
  263. $title_gradebook= $this->attendance_qualify_title;
  264. $value_calification = 0;
  265. $weight_calification = floatval($this->attendance_weight);
  266. $params = [
  267. 'c_id' => $course_id,
  268. 'name' => $this->name,
  269. 'description' => $this->description,
  270. 'attendance_qualify_title' => $title_gradebook,
  271. 'attendance_weight' => $weight_calification,
  272. 'session_id' => $session_id,
  273. 'active' => 1,
  274. 'attendance_qualify_max' => 0,
  275. 'locked' => 0
  276. ];
  277. $last_id = Database::insert($tbl_attendance, $params);
  278. if (!empty($last_id)) {
  279. $sql = "UPDATE $tbl_attendance SET id = iid WHERE iid = $last_id";
  280. Database::query($sql);
  281. api_item_property_update(
  282. $_course,
  283. TOOL_ATTENDANCE,
  284. $last_id,
  285. "AttendanceAdded",
  286. $user_id
  287. );
  288. }
  289. // add link to gradebook
  290. if ($link_to_gradebook && !empty($this->category_id)) {
  291. $description = '';
  292. $link_info = GradebookUtils::is_resource_in_course_gradebook(
  293. $course_code,
  294. 7,
  295. $last_id,
  296. $session_id
  297. );
  298. $link_id = $link_info['id'];
  299. if (!$link_info) {
  300. GradebookUtils::add_resource_to_course_gradebook(
  301. $this->category_id,
  302. $course_code,
  303. 7,
  304. $last_id,
  305. $title_gradebook,
  306. $weight_calification,
  307. $value_calification,
  308. $description,
  309. 1,
  310. $session_id
  311. );
  312. } else {
  313. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_id.'');
  314. }
  315. }
  316. return $last_id;
  317. }
  318. /**
  319. * edit attendances inside table
  320. * @param int attendance id
  321. * @param bool true for adding link in gradebook or false otherwise (optional)
  322. * @return int last id
  323. */
  324. public function attendance_edit($attendance_id, $link_to_gradebook = false)
  325. {
  326. $_course = api_get_course_info();
  327. $tbl_attendance = Database:: get_course_table(TABLE_ATTENDANCE);
  328. $table_link = Database:: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  329. $session_id = api_get_session_id();
  330. $user_id = api_get_user_id();
  331. $attendance_id = intval($attendance_id);
  332. $course_code = $_course['code'];
  333. $course_id = $_course['real_id'];
  334. $title_gradebook = $this->attendance_qualify_title;
  335. $value_calification = 0;
  336. $weight_calification = floatval($this->attendance_weight);
  337. if (!empty($attendance_id)) {
  338. $params = [
  339. 'name' => $this->name,
  340. 'description' => $this->description,
  341. 'attendance_qualify_title' => $title_gradebook,
  342. 'attendance_weight' => $weight_calification
  343. ];
  344. Database::update(
  345. $tbl_attendance,
  346. $params,
  347. ['c_id = ? AND id = ?' => [$course_id, $attendance_id]]
  348. );
  349. api_item_property_update(
  350. $_course,
  351. TOOL_ATTENDANCE,
  352. $attendance_id,
  353. "AttendanceUpdated",
  354. $user_id
  355. );
  356. // add link to gradebook
  357. if ($link_to_gradebook && !empty($this->category_id)) {
  358. $description = '';
  359. $link_info = GradebookUtils::is_resource_in_course_gradebook(
  360. $course_code,
  361. 7,
  362. $attendance_id,
  363. $session_id
  364. );
  365. if (!$link_info) {
  366. GradebookUtils::add_resource_to_course_gradebook(
  367. $this->category_id,
  368. $course_code,
  369. 7,
  370. $attendance_id,
  371. $title_gradebook,
  372. $weight_calification,
  373. $value_calification,
  374. $description,
  375. 1,
  376. $session_id
  377. );
  378. } else {
  379. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_info['id'].'');
  380. }
  381. }
  382. return $attendance_id;
  383. }
  384. return null;
  385. }
  386. /**
  387. * Restore attendance
  388. * @param int|array one or many attendances id
  389. * @return int affected rows
  390. */
  391. public function attendance_restore($attendance_id)
  392. {
  393. $_course = api_get_course_info();
  394. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  395. $user_id = api_get_user_id();
  396. $course_id = $_course['real_id'];
  397. if (is_array($attendance_id)) {
  398. foreach ($attendance_id as $id) {
  399. $id = intval($id);
  400. $sql = "UPDATE $tbl_attendance SET active = 1
  401. WHERE c_id = $course_id AND id = '$id'";
  402. $result = Database::query($sql);
  403. $affected_rows = Database::affected_rows($result);
  404. if (!empty($affected_rows)) {
  405. // update row item property table
  406. api_item_property_update(
  407. $_course,
  408. TOOL_ATTENDANCE,
  409. $id,
  410. "restore",
  411. $user_id
  412. );
  413. }
  414. }
  415. } else {
  416. $attendance_id = intval($attendance_id);
  417. $sql = "UPDATE $tbl_attendance SET active = 1
  418. WHERE c_id = $course_id AND id = '$attendance_id'";
  419. $result = Database::query($sql);
  420. $affected_rows = Database::affected_rows($result);
  421. if (!empty($affected_rows)) {
  422. // update row item property table
  423. api_item_property_update(
  424. $_course,
  425. TOOL_ATTENDANCE,
  426. $attendance_id,
  427. "restore",
  428. $user_id
  429. );
  430. }
  431. }
  432. return $affected_rows;
  433. }
  434. /**
  435. * Delete attendances
  436. * @param int|array $attendance_id one or many attendances id
  437. * @return int affected rows
  438. */
  439. public function attendance_delete($attendance_id)
  440. {
  441. $_course = api_get_course_info();
  442. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  443. $user_id = api_get_user_id();
  444. $course_id = $_course['real_id'];
  445. if (is_array($attendance_id)) {
  446. foreach ($attendance_id as $id) {
  447. $id = intval($id);
  448. $sql = "UPDATE $tbl_attendance SET active = 2
  449. WHERE c_id = $course_id AND id = '$id'";
  450. $result = Database::query($sql);
  451. $affected_rows = Database::affected_rows($result);
  452. if (!empty($affected_rows)) {
  453. // update row item property table
  454. api_item_property_update(
  455. $_course,
  456. TOOL_ATTENDANCE,
  457. $id,
  458. "delete",
  459. $user_id
  460. );
  461. }
  462. }
  463. } else {
  464. $attendance_id= intval($attendance_id);
  465. $sql = "UPDATE $tbl_attendance SET active = 2
  466. WHERE c_id = $course_id AND id = '$attendance_id'";
  467. $result = Database::query($sql);
  468. $affected_rows = Database::affected_rows($result);
  469. if (!empty($affected_rows)) {
  470. // update row item property table
  471. api_item_property_update(
  472. $_course,
  473. TOOL_ATTENDANCE,
  474. $attendance_id,
  475. "delete",
  476. $user_id
  477. );
  478. }
  479. }
  480. return $affected_rows;
  481. }
  482. /**
  483. * Changes visibility
  484. * @param int|array $attendanceId one or many attendances id
  485. * @param int status
  486. *
  487. * @return int affected rows
  488. */
  489. public function changeVisibility($attendanceId, $status = 1)
  490. {
  491. $_course = api_get_course_info();
  492. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  493. $user_id = api_get_user_id();
  494. $course_id = $_course['real_id'];
  495. $status = intval($status);
  496. $action = 'visible';
  497. if ($status == 0) {
  498. $action = 'invisible';
  499. }
  500. if (is_array($attendanceId)) {
  501. foreach ($attendanceId as $id) {
  502. $id = intval($id);
  503. $sql = "UPDATE $tbl_attendance SET active = $status
  504. WHERE c_id = $course_id AND id = '$id'";
  505. $result = Database::query($sql);
  506. $affected_rows = Database::affected_rows($result);
  507. if (!empty($affected_rows)) {
  508. // update row item property table
  509. api_item_property_update($_course, TOOL_ATTENDANCE, $id, $action, $user_id);
  510. }
  511. }
  512. } else {
  513. $attendanceId = intval($attendanceId);
  514. $sql = "UPDATE $tbl_attendance SET active = $status
  515. WHERE c_id = $course_id AND id = '$attendanceId'";
  516. $result = Database::query($sql);
  517. $affected_rows = Database::affected_rows($result);
  518. if (!empty($affected_rows)) {
  519. // update row item property table
  520. api_item_property_update(
  521. $_course,
  522. TOOL_ATTENDANCE,
  523. $attendanceId,
  524. $action,
  525. $user_id
  526. );
  527. }
  528. }
  529. return $affected_rows;
  530. }
  531. /**
  532. * Lock or unlock an attendance
  533. * @param int attendance id
  534. * @param bool True to lock or false otherwise
  535. */
  536. public function lock_attendance($attendance_id, $lock = true)
  537. {
  538. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  539. $course_id = api_get_course_int_id();
  540. $attendance_id = intval($attendance_id);
  541. $locked = ($lock)?1:0;
  542. $upd = "UPDATE $tbl_attendance SET locked = $locked
  543. WHERE c_id = $course_id AND id = $attendance_id";
  544. $result = Database::query($upd);
  545. $affected_rows = Database::affected_rows($result);
  546. if ($affected_rows && $lock) {
  547. // Save attendance sheet log
  548. $lastedit_date = api_get_utc_datetime();
  549. $lastedit_type = self::LOCKED_ATTENDANCE_LOG_TYPE;
  550. $lastedit_user_id = api_get_user_id();
  551. $this->save_attendance_sheet_log(
  552. $attendance_id,
  553. $lastedit_date,
  554. $lastedit_type,
  555. $lastedit_user_id
  556. );
  557. }
  558. return $affected_rows;
  559. }
  560. /**
  561. * Get registered users inside current course
  562. * @param int $attendance_id attendance id for showing attendance result field (optional)
  563. * @param int $groupId
  564. * @return array users data
  565. */
  566. public function get_users_rel_course($attendance_id = 0, $groupId = null)
  567. {
  568. $current_session_id = api_get_session_id();
  569. $current_course_id = api_get_course_id();
  570. $currentCourseIntId = api_get_course_int_id();
  571. $studentInGroup = array();
  572. if (!empty($current_session_id)) {
  573. $a_course_users = CourseManager:: get_user_list_from_course_code(
  574. $current_course_id,
  575. $current_session_id,
  576. '',
  577. 'lastname'
  578. );
  579. } else {
  580. $a_course_users = CourseManager:: get_user_list_from_course_code(
  581. $current_course_id,
  582. 0,
  583. '',
  584. 'lastname'
  585. );
  586. }
  587. if (!empty($groupId)) {
  588. $students = GroupManager::getStudents($groupId);
  589. if (!empty($students)) {
  590. foreach ($students as $student) {
  591. $studentInGroup[$student['user_id']] = true;
  592. }
  593. }
  594. }
  595. // get registered users inside current course
  596. $a_users = array();
  597. foreach ($a_course_users as $key => $user_data) {
  598. $value = array();
  599. $uid = $user_data['user_id'];
  600. $userInfo = api_get_user_info($uid);
  601. $status = $user_data['status'];
  602. if (!empty($groupId)) {
  603. if (!isset($studentInGroup[$uid])) {
  604. continue;
  605. }
  606. }
  607. $user_status_in_session = null;
  608. $user_status_in_course = null;
  609. if (api_get_session_id()) {
  610. $user_status_in_session = SessionManager::get_user_status_in_course_session(
  611. $uid,
  612. $currentCourseIntId,
  613. $current_session_id
  614. );
  615. } else {
  616. $user_status_in_course = CourseManager::get_user_in_course_status(
  617. $uid,
  618. $current_course_id
  619. );
  620. }
  621. // Not taking into account DRH or COURSEMANAGER
  622. if ($uid <= 1 ||
  623. $status == DRH ||
  624. $user_status_in_course == COURSEMANAGER ||
  625. $user_status_in_session == 2
  626. ) {
  627. continue;
  628. }
  629. if (!empty($attendance_id)) {
  630. $user_faults = $this->get_faults_of_user($uid, $attendance_id, $groupId);
  631. $value['attendance_result'] = $user_faults['faults'].'/'.$user_faults['total'].' ('.$user_faults['faults_porcent'].'%)';
  632. $value['result_color_bar'] = $user_faults['color_bar'];
  633. }
  634. $photo = Display::img($userInfo['avatar_small'], $userInfo['complete_name'], [], false);
  635. $value['photo'] = $photo;
  636. $value['firstname'] = $user_data['firstname'];
  637. $value['lastname'] = $user_data['lastname'];
  638. $value['username'] = $user_data['username'];
  639. $value['user_id'] = $uid;
  640. //Sending only 5 items in the array instead of 60
  641. $a_users[$key] = $value;
  642. }
  643. return $a_users;
  644. }
  645. /**
  646. * add attendances sheet inside table
  647. * @param int $calendar_id attendance calendar id
  648. * @param array $users_present present users during current class
  649. * @param int $attendance_id
  650. * @return int affected rows
  651. */
  652. public function attendance_sheet_add($calendar_id, $users_present, $attendance_id)
  653. {
  654. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  655. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  656. $calendar_id = intval($calendar_id);
  657. $attendance_id = intval($attendance_id);
  658. $users = $this->get_users_rel_course();
  659. $course_id = api_get_course_int_id();
  660. $user_ids = array_keys($users);
  661. $users_absent = array_diff($user_ids,$users_present);
  662. $affected_rows = 0;
  663. // get last edit type
  664. $calendar_data = $this->get_attendance_calendar_by_id($calendar_id);
  665. $lastedit_type = self::DONE_ATTENDANCE_LOG_TYPE;
  666. if ($calendar_data['done_attendance']) {
  667. $lastedit_type = self::UPDATED_ATTENDANCE_LOG_TYPE;
  668. }
  669. // save users present in class
  670. foreach ($users_present as $user_present) {
  671. $uid = intval($user_present);
  672. // check if user already was registered with the $calendar_id
  673. $sql = "SELECT user_id FROM $tbl_attendance_sheet
  674. WHERE c_id = $course_id AND user_id='$uid' AND attendance_calendar_id = '$calendar_id'";
  675. $rs = Database::query($sql);
  676. if (Database::num_rows($rs) == 0) {
  677. $sql = "INSERT INTO $tbl_attendance_sheet SET
  678. c_id = $course_id,
  679. user_id = '$uid',
  680. attendance_calendar_id = '$calendar_id',
  681. presence = 1";
  682. $result = Database::query($sql);
  683. $affected_rows += Database::affected_rows($result);
  684. } else {
  685. $sql = "UPDATE $tbl_attendance_sheet SET presence = 1
  686. WHERE
  687. c_id = $course_id AND
  688. user_id ='$uid' AND
  689. attendance_calendar_id = '$calendar_id'
  690. ";
  691. $result = Database::query($sql);
  692. $affected_rows += Database::affected_rows($result);
  693. }
  694. }
  695. // save users absent in class
  696. foreach ($users_absent as $user_absent) {
  697. $uid = intval($user_absent);
  698. // check if user already was registered with the $calendar_id
  699. $sql = "SELECT user_id FROM $tbl_attendance_sheet
  700. WHERE c_id = $course_id AND user_id='$uid' AND attendance_calendar_id = '$calendar_id'";
  701. $rs = Database::query($sql);
  702. if (Database::num_rows($rs) == 0) {
  703. $sql = "INSERT INTO $tbl_attendance_sheet SET
  704. c_id = $course_id,
  705. user_id ='$uid',
  706. attendance_calendar_id = '$calendar_id',
  707. presence = 0";
  708. $result = Database::query($sql);
  709. Database::insert_id();
  710. $affected_rows += Database::affected_rows($result);
  711. } else {
  712. $sql = "UPDATE $tbl_attendance_sheet SET presence = 0
  713. WHERE
  714. c_id = $course_id AND
  715. user_id ='$uid' AND
  716. attendance_calendar_id = '$calendar_id'";
  717. $result = Database::query($sql);
  718. $affected_rows += Database::affected_rows($result);
  719. }
  720. }
  721. // update done_attendance inside attendance calendar table
  722. $sql = "UPDATE $tbl_attendance_calendar SET done_attendance = 1
  723. WHERE c_id = $course_id AND id = '$calendar_id'";
  724. Database::query($sql);
  725. // save users' results
  726. $this->update_users_results($user_ids, $attendance_id);
  727. if ($affected_rows) {
  728. //save attendance sheet log
  729. $lastedit_date = api_get_utc_datetime();
  730. $lastedit_user_id = api_get_user_id();
  731. $calendar_date_value = $calendar_data['date_time'];
  732. $this->save_attendance_sheet_log(
  733. $attendance_id,
  734. $lastedit_date,
  735. $lastedit_type,
  736. $lastedit_user_id,
  737. $calendar_date_value
  738. );
  739. }
  740. return $affected_rows;
  741. }
  742. /**
  743. * update users' attendance results
  744. * @param array $user_ids registered users inside current course
  745. * @param int $attendance_id
  746. * @return void
  747. */
  748. public function update_users_results($user_ids, $attendance_id)
  749. {
  750. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  751. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  752. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  753. $course_id = api_get_course_int_id();
  754. $attendance_id = intval($attendance_id);
  755. // fill results about presence of students
  756. $attendance_calendar = $this->get_attendance_calendar(
  757. $attendance_id,
  758. 'all',
  759. null,
  760. null,
  761. true
  762. );
  763. $calendar_ids = array();
  764. // get all dates from calendar by current attendance
  765. foreach ($attendance_calendar as $cal) {
  766. $calendar_ids[] = $cal['id'];
  767. }
  768. // get count of presences by users inside current attendance and save like results
  769. if (count($user_ids) > 0) {
  770. foreach ($user_ids as $uid) {
  771. $count_presences = 0;
  772. if (count($calendar_ids) > 0) {
  773. $sql = "SELECT count(presence) as count_presences
  774. FROM $tbl_attendance_sheet
  775. WHERE
  776. c_id = $course_id AND
  777. user_id = '$uid' AND
  778. attendance_calendar_id IN (".implode(',', $calendar_ids).") AND
  779. presence = 1";
  780. $rs_count = Database::query($sql);
  781. $row_count = Database::fetch_array($rs_count);
  782. $count_presences = $row_count['count_presences'];
  783. }
  784. // save results
  785. $sql = "SELECT id FROM $tbl_attendance_result
  786. WHERE
  787. c_id = $course_id AND
  788. user_id = '$uid' AND
  789. attendance_id = '$attendance_id' ";
  790. $rs_check_result = Database::query($sql);
  791. if (Database::num_rows($rs_check_result) > 0) {
  792. // update result
  793. $sql = "UPDATE $tbl_attendance_result SET
  794. score = '$count_presences'
  795. WHERE
  796. c_id = $course_id AND
  797. user_id='$uid' AND
  798. attendance_id='$attendance_id'";
  799. Database::query($sql);
  800. } else {
  801. // insert new result
  802. $sql = "INSERT INTO $tbl_attendance_result SET
  803. c_id = $course_id ,
  804. user_id = '$uid',
  805. attendance_id = '$attendance_id',
  806. score = '$count_presences'";
  807. Database::query($sql);
  808. $insertId = Database::insert_id();
  809. if ($insertId) {
  810. $sql = "UPDATE $tbl_attendance_result SET id = iid WHERE iid = $insertId";
  811. Database::query($sql);
  812. }
  813. }
  814. }
  815. }
  816. // update attendance qualify max
  817. $count_done_calendar = self::get_done_attendance_calendar($attendance_id);
  818. $sql = "UPDATE $tbl_attendance SET
  819. attendance_qualify_max = '$count_done_calendar'
  820. WHERE c_id = $course_id AND id = '$attendance_id'";
  821. Database::query($sql);
  822. }
  823. /**
  824. * update attendance_sheet_log table, is used as history of an attendance sheet
  825. * @param int Attendance id
  826. * @param string Last edit datetime
  827. * @param string Event type ('locked_attendance', 'done_attendance_sheet' ...)
  828. * @param int Last edit user id
  829. * @param string Calendar datetime value (optional, when event type is 'done_attendance_sheet')
  830. * @return int Affected rows
  831. */
  832. public function save_attendance_sheet_log(
  833. $attendance_id,
  834. $lastedit_date,
  835. $lastedit_type,
  836. $lastedit_user_id,
  837. $calendar_date_value = null
  838. ) {
  839. $course_id = api_get_course_int_id();
  840. // define table
  841. $tbl_attendance_sheet_log = Database::get_course_table(TABLE_ATTENDANCE_SHEET_LOG);
  842. // protect data
  843. $attendance_id = intval($attendance_id);
  844. $lastedit_user_id = intval($lastedit_user_id);
  845. if (isset($calendar_date_value)) {
  846. $calendar_date_value = $calendar_date_value;
  847. } else {
  848. $calendar_date_value = '';
  849. }
  850. // save data
  851. $params = [
  852. 'c_id' => $course_id,
  853. 'attendance_id' => $attendance_id,
  854. 'lastedit_date' => $lastedit_date,
  855. 'lastedit_type' => $lastedit_type,
  856. 'lastedit_user_id' => $lastedit_user_id,
  857. 'calendar_date_value' => $calendar_date_value,
  858. ];
  859. $insertId = Database::insert($tbl_attendance_sheet_log, $params);
  860. if ($insertId) {
  861. $sql = "UPDATE $tbl_attendance_sheet_log SET id = iid WHERE iid = $insertId";
  862. Database::query($sql);
  863. }
  864. return $insertId;
  865. }
  866. /**
  867. * Get number of done attendances inside current sheet
  868. * @param int attendance id
  869. * @return int number of done attendances
  870. */
  871. public static function get_done_attendance_calendar($attendance_id)
  872. {
  873. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  874. $attendance_id = intval($attendance_id);
  875. $course_id = api_get_course_int_id();
  876. $sql = "SELECT count(done_attendance) as count
  877. FROM $tbl_attendance_calendar
  878. WHERE
  879. c_id = $course_id AND
  880. attendance_id = '$attendance_id' AND
  881. done_attendance = 1
  882. ";
  883. $rs = Database::query($sql);
  884. $row = Database::fetch_array($rs);
  885. $count = $row['count'];
  886. return $count;
  887. }
  888. /**
  889. * Get results of faults (absents) by user
  890. * @param int $user_id
  891. * @param int $attendance_id
  892. * @param int $groupId
  893. * @return array results containing number of faults, total done attendance,
  894. * percent of faults and color depend on result (red, orange)
  895. */
  896. public function get_faults_of_user($user_id, $attendance_id, $groupId = null)
  897. {
  898. // initializing database table and variables
  899. $user_id = intval($user_id);
  900. $attendance_id = intval($attendance_id);
  901. $results = array();
  902. $calendar_count = self::get_number_of_attendance_calendar($attendance_id, $groupId, NULL, $user_id);
  903. // $total_done_attendance = $attendance_data['attendance_qualify_max'];
  904. $total_done_attendance = self::get_number_of_attendance_calendar($attendance_id, $groupId, true, $user_id);
  905. $attendance_user_score = $this->get_user_score($user_id, $attendance_id, $groupId);
  906. //This is the main change of the BT#1381
  907. //$total_done_attendance = $calendar_count;
  908. // calculate results
  909. $faults = $total_done_attendance - $attendance_user_score;
  910. if (empty($calendar_count)) {
  911. $faults = 0;
  912. }
  913. $faults = $faults > 0 ? $faults : 0;
  914. $faults_porcent = $calendar_count > 0 ?round(($faults*100)/$calendar_count,0):0;
  915. $results['faults'] = $faults;
  916. $results['total'] = $calendar_count;
  917. $results['faults_porcent'] = $faults_porcent;
  918. $color_bar = '';
  919. if ($faults_porcent > 25) {
  920. $color_bar = '#f28989';
  921. } else if ($faults_porcent > 10) {
  922. $color_bar = '#F90';
  923. }
  924. $results['color_bar'] = $color_bar;
  925. return $results;
  926. }
  927. /**
  928. * Get results of faults average for all courses by user
  929. * @param int $user_id
  930. * @return array results containing number of faults, total done attendance,
  931. * percentage of faults and color depend on result (red, orange)
  932. */
  933. public function get_faults_average_inside_courses($user_id)
  934. {
  935. // get all courses of current user
  936. $courses = CourseManager::get_courses_list_by_user_id($user_id, true);
  937. $user_id = intval($user_id);
  938. $results = array();
  939. $total_faults = $total_weight = $porcent = 0;
  940. foreach ($courses as $course) {
  941. //$course_code = $course['code'];
  942. //$course_info = api_get_course_info($course_code);
  943. $course_id = $course['real_id'];
  944. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  945. $attendances_by_course = $this->get_attendances_list($course_id);
  946. foreach ($attendances_by_course as $attendance) {
  947. // get total faults and total weight
  948. $total_done_attendance = $attendance['attendance_qualify_max'];
  949. $sql = "SELECT score
  950. FROM $tbl_attendance_result
  951. WHERE
  952. c_id = $course_id AND
  953. user_id = $user_id AND
  954. attendance_id = ".$attendance['id'];
  955. $rs = Database::query($sql);
  956. $score = 0;
  957. if (Database::num_rows($rs) > 0) {
  958. $row = Database::fetch_array($rs);
  959. $score = $row['score'];
  960. }
  961. $faults = $total_done_attendance-$score;
  962. $faults = $faults > 0 ? $faults:0;
  963. $total_faults += $faults;
  964. $total_weight += $total_done_attendance;
  965. }
  966. }
  967. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  968. $results['faults'] = $total_faults;
  969. $results['total'] = $total_weight;
  970. $results['porcent'] = $porcent;
  971. return $results;
  972. }
  973. /**
  974. * Get results of faults average by course
  975. * @param int $user_id
  976. * @param string $course_code
  977. * @param int Session id (optional)
  978. * @return array results containing number of faults,
  979. * total done attendance, porcent of faults and color depend on result (red, orange)
  980. */
  981. public function get_faults_average_by_course($user_id, $course_code, $session_id = null)
  982. {
  983. // Database tables and variables
  984. $course_info = api_get_course_info($course_code);
  985. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  986. $user_id = intval($user_id);
  987. $results = array();
  988. $total_faults = $total_weight = $porcent = 0;
  989. $attendances_by_course = $this->get_attendances_list($course_info['real_id'], $session_id);
  990. foreach ($attendances_by_course as $attendance) {
  991. // Get total faults and total weight
  992. $total_done_attendance = $attendance['attendance_qualify_max'];
  993. $sql = "SELECT score FROM $tbl_attendance_result
  994. WHERE
  995. c_id = {$course_info['real_id']} AND
  996. user_id = $user_id AND
  997. attendance_id=".$attendance['id'];
  998. $rs = Database::query($sql);
  999. $score = 0;
  1000. if (Database::num_rows($rs) > 0) {
  1001. $row = Database::fetch_array($rs);
  1002. $score = $row['score'];
  1003. }
  1004. $faults = $total_done_attendance-$score;
  1005. $faults = $faults > 0 ? $faults:0;
  1006. $total_faults += $faults;
  1007. $total_weight += $total_done_attendance;
  1008. }
  1009. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  1010. $results['faults'] = $total_faults;
  1011. $results['total'] = $total_weight;
  1012. $results['porcent'] = $porcent;
  1013. return $results;
  1014. }
  1015. /**
  1016. * Get registered users' attendance sheet inside current course
  1017. * @param int $attendance_id
  1018. * @param int $user_id for showing data for only one user (optional)
  1019. * @return array users attendance sheet data
  1020. */
  1021. public function get_users_attendance_sheet($attendance_id, $user_id = 0, $groupId = null)
  1022. {
  1023. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1024. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1025. $attendance_calendar = $this->get_attendance_calendar($attendance_id, 'all', null, $groupId);
  1026. $calendar_ids = array();
  1027. // get all dates from calendar by current attendance
  1028. foreach ($attendance_calendar as $cal) {
  1029. $calendar_ids[] = $cal['id'];
  1030. }
  1031. $course_id = api_get_course_int_id();
  1032. $data = array();
  1033. if (empty($user_id)) {
  1034. // get all registered users inside current course
  1035. $users = $this->get_users_rel_course();
  1036. $user_ids = array_keys($users);
  1037. if (count($calendar_ids) > 0 && count($user_ids) > 0) {
  1038. foreach ($user_ids as $uid) {
  1039. $sql = "SELECT * FROM $tbl_attendance_sheet
  1040. WHERE
  1041. c_id = $course_id AND
  1042. user_id = '$uid' AND
  1043. attendance_calendar_id IN(".implode(',',$calendar_ids).")
  1044. ";
  1045. $res = Database::query($sql);
  1046. if (Database::num_rows($res) > 0) {
  1047. while ($row = Database::fetch_array($res)) {
  1048. $data[$uid][$row['attendance_calendar_id']]['presence'] = $row['presence'];
  1049. }
  1050. }
  1051. }
  1052. }
  1053. } else {
  1054. // Get attendance for current user
  1055. $user_id = intval($user_id);
  1056. if (count($calendar_ids) > 0) {
  1057. $sql = "SELECT cal.date_time, att.presence
  1058. FROM $tbl_attendance_sheet att
  1059. INNER JOIN $tbl_attendance_calendar cal
  1060. ON cal.id = att.attendance_calendar_id
  1061. WHERE
  1062. att.c_id = $course_id AND
  1063. cal.c_id = $course_id AND
  1064. att.user_id = '$user_id' AND
  1065. att.attendance_calendar_id IN (".implode(',',$calendar_ids).")
  1066. ORDER BY date_time";
  1067. $res = Database::query($sql);
  1068. if (Database::num_rows($res) > 0) {
  1069. while ($row = Database::fetch_array($res)) {
  1070. $row['date_time'] = api_convert_and_format_date($row['date_time'], null, date_default_timezone_get());
  1071. $data[$user_id][] = $row;
  1072. }
  1073. }
  1074. }
  1075. }
  1076. return $data;
  1077. }
  1078. /**
  1079. * Get next attendance calendar without presences (done attendances)
  1080. * @param int attendance id
  1081. * @return int attendance calendar id
  1082. */
  1083. public function get_next_attendance_calendar_id($attendance_id)
  1084. {
  1085. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1086. $attendance_id = intval($attendance_id);
  1087. $course_id = api_get_course_int_id();
  1088. $sql = "SELECT id FROM $tbl_attendance_calendar
  1089. WHERE
  1090. c_id = $course_id AND
  1091. attendance_id = '$attendance_id' AND
  1092. done_attendance = 0
  1093. ORDER BY date_time
  1094. LIMIT 1";
  1095. $rs = Database::query($sql);
  1096. $next_calendar_id = 0;
  1097. if (Database::num_rows($rs) > 0) {
  1098. $row = Database::fetch_array($rs);
  1099. $next_calendar_id = $row['id'];
  1100. }
  1101. return $next_calendar_id;
  1102. }
  1103. /**
  1104. * Get next attendance calendar datetime without presences (done attendances)
  1105. * @param int attendance id
  1106. * @return int UNIX time format datetime
  1107. */
  1108. public function get_next_attendance_calendar_datetime($attendance_id)
  1109. {
  1110. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1111. $course_id = api_get_course_int_id();
  1112. $attendance_id = intval($attendance_id);
  1113. $sql = "SELECT id, date_time FROM $tbl_attendance_calendar
  1114. WHERE
  1115. c_id = $course_id AND
  1116. attendance_id = '$attendance_id' AND
  1117. done_attendance = 0
  1118. ORDER BY date_time
  1119. LIMIT 1";
  1120. $rs = Database::query($sql);
  1121. $next_calendar_datetime = 0;
  1122. if (Database::num_rows($rs) > 0) {
  1123. $row = Database::fetch_array($rs);
  1124. $next_calendar_datetime = api_get_local_time($row['date_time']);
  1125. }
  1126. return $next_calendar_datetime;
  1127. }
  1128. /**
  1129. * Get user' score from current attendance
  1130. * @param int $user_id
  1131. * @param int $attendance_id
  1132. * @return int score
  1133. */
  1134. public function get_user_score($user_id, $attendance_id, $groupId = null)
  1135. {
  1136. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  1137. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1138. $tbl_attendance_cal_rel_group = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1139. $tbl_attendance_cal = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1140. $user_id = intval($user_id);
  1141. $attendance_id = intval($attendance_id);
  1142. $course_id = api_get_course_int_id();
  1143. if (empty($groupId)) {
  1144. $sql = "SELECT score FROM $tbl_attendance_result
  1145. WHERE
  1146. c_id = $course_id AND
  1147. user_id='$user_id' AND
  1148. attendance_id='$attendance_id'";
  1149. } else {
  1150. $sql = "SELECT count(presence) as score FROM $tbl_attendance_sheet
  1151. WHERE
  1152. c_id = $course_id AND
  1153. user_id='$user_id' AND
  1154. presence = 1 AND
  1155. attendance_calendar_id IN (
  1156. SELECT calendar_id FROM $tbl_attendance_cal_rel_group crg
  1157. INNER JOIN $tbl_attendance_cal c
  1158. ON (crg.calendar_id = c.id)
  1159. WHERE
  1160. crg.c_id = $course_id AND
  1161. crg.group_id = $groupId AND
  1162. c.attendance_id = $attendance_id
  1163. )
  1164. ";
  1165. }
  1166. $rs = Database::query($sql);
  1167. $score = 0;
  1168. if (Database::num_rows($rs) > 0) {
  1169. $row = Database::fetch_array($rs);
  1170. $score = $row['score'];
  1171. }
  1172. return $score;
  1173. }
  1174. /**
  1175. * Get attendance calendar data by id
  1176. * @param int attendance calendar id
  1177. * @return array attendance calendar data
  1178. */
  1179. public function get_attendance_calendar_by_id($calendar_id)
  1180. {
  1181. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1182. $calendar_id = intval($calendar_id);
  1183. $course_id = api_get_course_int_id();
  1184. $sql = "SELECT * FROM $tbl_attendance_calendar
  1185. WHERE c_id = $course_id AND id = '$calendar_id' ";
  1186. $rs = Database::query($sql);
  1187. $data = array();
  1188. if (Database::num_rows($rs) > 0) {
  1189. while ($row = Database::fetch_array($rs)) {
  1190. $row['date_time'] = api_get_local_time($row['date_time']);
  1191. $data = $row;
  1192. }
  1193. }
  1194. return $data;
  1195. }
  1196. /**
  1197. * Get all attendance calendar data inside current attendance
  1198. * @param int $attendance_id
  1199. * @param string $type
  1200. * @param int $calendar_id
  1201. * @param int $groupId
  1202. * @param bool $showAll = false show group calendar items or not
  1203. *
  1204. * @return array attendance calendar data
  1205. */
  1206. public function get_attendance_calendar(
  1207. $attendance_id,
  1208. $type = 'all',
  1209. $calendar_id = null,
  1210. $groupId = null,
  1211. $showAll = false
  1212. ) {
  1213. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1214. $tbl_acrg = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1215. $attendance_id = intval($attendance_id);
  1216. $course_id = api_get_course_int_id();
  1217. $groupCondition = null;
  1218. if ($showAll) {
  1219. $sql = "SELECT * FROM $tbl_attendance_calendar c
  1220. WHERE c_id = $course_id AND attendance_id = '$attendance_id'";
  1221. } else {
  1222. $sql = "SELECT * FROM $tbl_attendance_calendar c
  1223. WHERE
  1224. c_id = $course_id AND
  1225. attendance_id = '$attendance_id' AND
  1226. id NOT IN (
  1227. SELECT calendar_id FROM $tbl_acrg
  1228. WHERE c_id = $course_id AND group_id != 0 AND group_id IS NOT NULL
  1229. )
  1230. ";
  1231. }
  1232. if (!empty($groupId)) {
  1233. $groupId = intval($groupId);
  1234. $sql = "SELECT c.* FROM $tbl_attendance_calendar c
  1235. INNER JOIN $tbl_acrg g
  1236. ON c.c_id = g.c_id AND c.id = g.calendar_id
  1237. WHERE
  1238. c.c_id = $course_id AND
  1239. g.group_id = '$groupId' AND
  1240. c.attendance_id = '$attendance_id'
  1241. ";
  1242. }
  1243. if (!in_array($type, array('today', 'all', 'all_done', 'all_not_done','calendar_id'))) {
  1244. $type = 'all';
  1245. }
  1246. switch ($type) {
  1247. case 'calendar_id':
  1248. $calendar_id = intval($calendar_id);
  1249. if (!empty($calendar_id)) {
  1250. $sql.= " AND c.id = $calendar_id";
  1251. }
  1252. break;
  1253. case 'today':
  1254. //$sql .= ' AND DATE_FORMAT(date_time,"%d-%m-%Y") = DATE_FORMAT("'.api_get_utc_datetime().'", "%d-%m-%Y" )';
  1255. break;
  1256. case 'all_done':
  1257. $sql .= " AND done_attendance = 1 ";
  1258. break;
  1259. case 'all_not_done':
  1260. $sql .= " AND done_attendance = 0 ";
  1261. break;
  1262. case 'all':
  1263. default:
  1264. break;
  1265. }
  1266. $sql .= " ORDER BY date_time ";
  1267. $rs = Database::query($sql);
  1268. $data = array();
  1269. if (Database::num_rows($rs) > 0) {
  1270. while ($row = Database::fetch_array($rs,'ASSOC')) {
  1271. $row['db_date_time'] = $row['date_time'];
  1272. $row['date_time'] = api_get_local_time($row['date_time']);
  1273. $row['date'] = api_format_date($row['date_time'], DATE_FORMAT_SHORT);
  1274. $row['time'] = api_format_date($row['date_time'], TIME_NO_SEC_FORMAT);
  1275. $row['groups'] = $this->getGroupListByAttendanceCalendar($row['id'], $course_id);
  1276. if ($type == 'today') {
  1277. if (date('d-m-Y', api_strtotime($row['date_time'], 'UTC')) == date('d-m-Y', time())) {
  1278. $data[] = $row;
  1279. }
  1280. } else {
  1281. $data[] = $row;
  1282. }
  1283. }
  1284. }
  1285. return $data;
  1286. }
  1287. /**
  1288. * Get number of attendance calendar inside current attendance
  1289. * @param int $attendance_id
  1290. * @param int $groupId
  1291. * @return int number of dates in attendance calendar
  1292. */
  1293. public static function get_number_of_attendance_calendar($attendance_id, $groupId = 0, $done_attendance = NULL, $userId = 0)
  1294. {
  1295. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1296. $calendarRelGroup = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1297. $tbl_groupRelUser = Database::get_course_table(TABLE_GROUP_USER);
  1298. $attendance_id = intval($attendance_id);
  1299. $groupId = intval($groupId);
  1300. $course_id = api_get_course_int_id();
  1301. $where_attendance = '';
  1302. if ($done_attendance) {
  1303. $where_attendance = ' done_attendance = 1 AND ';
  1304. }
  1305. if (empty($userId)) {
  1306. if (empty($groupId)) {
  1307. $sql = "SELECT count(a.id)
  1308. FROM $tbl_attendance_calendar a
  1309. WHERE
  1310. c_id = $course_id AND
  1311. $where_attendance
  1312. attendance_id = '$attendance_id' AND
  1313. id NOT IN (
  1314. SELECT calendar_id FROM $calendarRelGroup
  1315. WHERE
  1316. c_id = $course_id AND
  1317. group_id != 0 AND
  1318. group_id IS NOT NULL
  1319. )
  1320. ";
  1321. } else {
  1322. $sql = "SELECT count(a.id)
  1323. FROM $tbl_attendance_calendar a
  1324. INNER JOIN $calendarRelGroup g
  1325. ON (a.id = g.calendar_id AND a.c_id = g.c_id)
  1326. WHERE
  1327. a.c_id = $course_id AND
  1328. $where_attendance
  1329. attendance_id = '$attendance_id' AND
  1330. group_id = $groupId
  1331. ";
  1332. }
  1333. } else {
  1334. if (empty($groupId)) {
  1335. $sql = "SELECT count(a.id)
  1336. FROM $tbl_attendance_calendar a
  1337. WHERE
  1338. c_id = $course_id AND
  1339. $where_attendance
  1340. attendance_id = '$attendance_id' AND
  1341. id NOT IN (
  1342. SELECT calendar_id FROM $calendarRelGroup
  1343. WHERE
  1344. c_id = $course_id AND
  1345. group_id != 0 AND
  1346. group_id IS NOT NULL AND
  1347. group_id NOT IN (
  1348. SELECT group_id
  1349. FROM $tbl_groupRelUser
  1350. WHERE user_id = $userId
  1351. )
  1352. )
  1353. ";
  1354. } else {
  1355. $sql = "SELECT count(a.id)
  1356. FROM $tbl_attendance_calendar a
  1357. INNER JOIN $calendarRelGroup g
  1358. ON (a.id = g.calendar_id AND a.c_id = g.c_id)
  1359. WHERE
  1360. a.c_id = $course_id AND
  1361. $where_attendance
  1362. attendance_id = '$attendance_id' AND
  1363. group_id = $groupId
  1364. ";
  1365. }
  1366. }
  1367. $rs = Database::query($sql);
  1368. $row = Database::fetch_row($rs);
  1369. $count = $row[0];
  1370. return $count;
  1371. }
  1372. /**
  1373. * Get count dates inside attendance calendar by attendance id
  1374. * @param int $attendance_id
  1375. * @return int count of dates
  1376. */
  1377. public static function get_count_dates_inside_attendance_calendar($attendance_id)
  1378. {
  1379. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1380. $attendance_id = intval($attendance_id);
  1381. $course_id = api_get_course_int_id();
  1382. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1383. WHERE
  1384. c_id = $course_id AND
  1385. attendance_id = '$attendance_id'";
  1386. $rs = Database::query($sql);
  1387. $count = 0;
  1388. if (Database::num_rows($rs) > 0) {
  1389. $row = Database::fetch_row($rs);
  1390. $count = $row[0];
  1391. }
  1392. return $count;
  1393. }
  1394. /**
  1395. * check if all calendar of an attendance is done
  1396. * @param int $attendance_id
  1397. * @return bool True if all calendar is done, otherwise false
  1398. */
  1399. public static function is_all_attendance_calendar_done($attendance_id)
  1400. {
  1401. $attendance_id = intval($attendance_id);
  1402. $done_calendar = self::get_done_attendance_calendar($attendance_id);
  1403. $count_dates_in_calendar = self::get_count_dates_inside_attendance_calendar($attendance_id);
  1404. $number_of_dates = self::get_number_of_attendance_calendar($attendance_id);
  1405. $result = false;
  1406. if ($number_of_dates && (intval($count_dates_in_calendar) == intval($done_calendar))) {
  1407. $result = true;
  1408. }
  1409. return $result;
  1410. }
  1411. /**
  1412. * check if an attendance is locked
  1413. * @param int $attendance_id
  1414. * @param bool
  1415. */
  1416. public static function is_locked_attendance($attendance_id)
  1417. {
  1418. //use gradebook lock
  1419. $result = api_resource_is_locked_by_gradebook($attendance_id, LINK_ATTENDANCE);
  1420. return $result;
  1421. }
  1422. /**
  1423. * Add new datetime inside attendance calendar table
  1424. * @param int $attendance_id
  1425. * @param array $groupList
  1426. * @return int affected rows
  1427. */
  1428. public function attendance_calendar_add($attendance_id, $groupList = array())
  1429. {
  1430. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1431. $affected_rows = 0;
  1432. $attendance_id = intval($attendance_id);
  1433. $course_id = api_get_course_int_id();
  1434. // check if datetime already exists inside the table
  1435. /*$sql = "SELECT id FROM $tbl_attendance_calendar
  1436. WHERE
  1437. c_id = $course_id AND
  1438. date_time='".Database::escape_string($this->date_time)."' AND
  1439. attendance_id = '$attendance_id'";
  1440. $rs = Database::query($sql);
  1441. if (Database::num_rows($rs) == 0) {*/
  1442. $params = array(
  1443. 'c_id' => $course_id,
  1444. 'date_time' => $this->date_time,
  1445. 'attendance_id' => $attendance_id,
  1446. 'done_attendance' => 0
  1447. );
  1448. $id = Database::insert($tbl_attendance_calendar, $params);
  1449. if ($id) {
  1450. $sql = "UPDATE $tbl_attendance_calendar SET id = iid WHERE iid = $id";
  1451. Database::query($sql);
  1452. $affected_rows++;
  1453. }
  1454. $this->addAttendanceCalendarToGroup($id, $course_id, $groupList);
  1455. //}
  1456. // update locked attendance
  1457. $is_all_calendar_done = self::is_all_attendance_calendar_done($attendance_id);
  1458. if (!$is_all_calendar_done) {
  1459. self::lock_attendance($attendance_id, false);
  1460. } else {
  1461. self::lock_attendance($attendance_id);
  1462. }
  1463. return $affected_rows;
  1464. }
  1465. /**
  1466. * @param int $calendarId
  1467. * @param int $courseId
  1468. * @param array $groupList
  1469. */
  1470. public function addAttendanceCalendarToGroup($calendarId, $courseId, $groupList)
  1471. {
  1472. if (empty($groupList)) {
  1473. return false;
  1474. }
  1475. $table = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1476. foreach ($groupList as $groupId) {
  1477. if (empty($groupId)) {
  1478. continue;
  1479. }
  1480. $result = $this->getAttendanceCalendarGroup(
  1481. $calendarId,
  1482. $courseId,
  1483. $groupId
  1484. );
  1485. if (empty($result)) {
  1486. $params = array(
  1487. 'calendar_id' => $calendarId,
  1488. 'c_id' => $courseId,
  1489. 'group_id' => $groupId,
  1490. );
  1491. Database::insert($table, $params);
  1492. }
  1493. }
  1494. }
  1495. /**
  1496. * @param int $calendarId
  1497. * @param int $courseId
  1498. * @param int $groupId
  1499. * @return array
  1500. */
  1501. public function getGroupListByAttendanceCalendar($calendarId, $courseId)
  1502. {
  1503. $table = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1504. return Database::select(
  1505. '*',
  1506. $table,
  1507. array('where'=>
  1508. array(
  1509. 'calendar_id = ? AND c_id = ?' => array($calendarId, $courseId)
  1510. )
  1511. )
  1512. );
  1513. }
  1514. /**
  1515. * @param int $calendarId
  1516. * @param int $courseId
  1517. * @param int $groupId
  1518. * @return array
  1519. */
  1520. public function getAttendanceCalendarGroup($calendarId, $courseId, $groupId)
  1521. {
  1522. $table = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1523. return Database::select(
  1524. '*',
  1525. $table,
  1526. array('where'=>
  1527. array(
  1528. 'calendar_id = ? AND c_id = ? AND group_id = ?' => array($calendarId, $courseId, $groupId)
  1529. )
  1530. )
  1531. );
  1532. }
  1533. /**
  1534. * @param int $calendarId
  1535. * @param int $courseId
  1536. * @param int $groupId
  1537. *
  1538. * @return array
  1539. */
  1540. public function deleteAttendanceCalendarGroup($calendarId, $courseId)
  1541. {
  1542. $table = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1543. Database::delete(
  1544. $table,
  1545. array(
  1546. 'calendar_id = ? AND c_id = ?' => array($calendarId, $courseId)
  1547. )
  1548. );
  1549. }
  1550. /**
  1551. * save repeated date inside attendance calendar table
  1552. * @param int $attendance_id
  1553. * @param int $start_date start date in tms
  1554. * @param int $end_date end date in tms
  1555. * @param string $repeat_type daily, weekly, monthlyByDate
  1556. * @param array $groupList
  1557. */
  1558. public function attendance_repeat_calendar_add(
  1559. $attendance_id,
  1560. $start_date,
  1561. $end_date,
  1562. $repeat_type,
  1563. $groupList = array()
  1564. ) {
  1565. $attendance_id = intval($attendance_id);
  1566. // save start date
  1567. $datetimezone = api_get_utc_datetime($start_date);
  1568. $this->set_date_time($datetimezone);
  1569. $this->attendance_calendar_add($attendance_id, $groupList);
  1570. // 86400 = 24 hours in seconds
  1571. // 604800 = 1 week in seconds
  1572. // Saves repeated dates
  1573. switch ($repeat_type) {
  1574. case 'daily':
  1575. $j = 1;
  1576. for ($i = $start_date + 86400; ($i <= $end_date); $i += 86400) {
  1577. $datetimezone = api_get_utc_datetime($i);
  1578. $this->set_date_time($datetimezone);
  1579. $this->attendance_calendar_add($attendance_id, $groupList);
  1580. $j++;
  1581. }
  1582. break;
  1583. case 'weekly':
  1584. $j = 1;
  1585. for ($i = $start_date + 604800; ($i <= $end_date); $i += 604800) {
  1586. $datetimezone = api_get_utc_datetime($i);
  1587. $this->set_date_time($datetimezone);
  1588. $this->attendance_calendar_add($attendance_id, $groupList);
  1589. $j++;
  1590. }
  1591. break;
  1592. case 'monthlyByDate':
  1593. $j = 1;
  1594. //@todo fix bug with february
  1595. for ($i = $start_date + 2419200; ($i <= $end_date); $i += 2419200) {
  1596. $datetimezone = api_get_utc_datetime($i);
  1597. $this->set_date_time($datetimezone);
  1598. $this->attendance_calendar_add($attendance_id, $groupList);
  1599. $j++;
  1600. }
  1601. break;
  1602. }
  1603. }
  1604. /**
  1605. * Adds x months to a UNIX timestamp
  1606. * @param int The timestamp
  1607. * @param int The number of years to add
  1608. * @return int The new timestamp
  1609. */
  1610. private function add_month($timestamp, $num=1)
  1611. {
  1612. $values = api_get_utc_datetime($timestamp);
  1613. $values = str_replace(array(':','-',' '), '/', $values);
  1614. list($y, $m, $d, $h, $n, $s) = split('/',$values);
  1615. if($m+$num>12) {
  1616. $y += floor($num/12);
  1617. $m += $num%12;
  1618. } else {
  1619. $m += $num;
  1620. }
  1621. //date_default_timezone_set('UTC');
  1622. // return mktime($h, $n, $s, $m, $d, $y);
  1623. $result = api_strtotime($y.'-'.$m.'-'.$d.' '.$h.':'.$n.':'.$s, 'UTC');
  1624. if (!empty($result)) {
  1625. return $result;
  1626. }
  1627. return false;
  1628. }
  1629. /**
  1630. * edit a datetime inside attendance calendar table
  1631. * @param int attendance calendar id
  1632. * @param int attendance id
  1633. * @return int affected rows
  1634. */
  1635. public function attendance_calendar_edit($calendar_id, $attendance_id)
  1636. {
  1637. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1638. $affected_rows = 0;
  1639. $attendance_id = intval($attendance_id);
  1640. $course_id = api_get_course_int_id();
  1641. // check if datetime already exists inside the table
  1642. $sql = "SELECT id FROM $tbl_attendance_calendar
  1643. WHERE
  1644. c_id = $course_id AND
  1645. date_time = '".Database::escape_string($this->date_time)."' AND
  1646. attendance_id = '$attendance_id'";
  1647. $rs = Database::query($sql);
  1648. if (Database::num_rows($rs) == 0) {
  1649. $sql = "UPDATE $tbl_attendance_calendar
  1650. SET date_time='".Database::escape_string($this->date_time)."'
  1651. WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1652. Database::query($sql);
  1653. }
  1654. // update locked attendance
  1655. $is_all_calendar_done = self::is_all_attendance_calendar_done($attendance_id);
  1656. if (!$is_all_calendar_done) {
  1657. self::lock_attendance($attendance_id, false);
  1658. } else {
  1659. self::lock_attendance($attendance_id);
  1660. }
  1661. return $affected_rows;
  1662. }
  1663. /**
  1664. * delete a datetime from attendance calendar table
  1665. * @param int attendance calendar id
  1666. * @param int attendance id
  1667. * @param bool true for removing all calendar inside current attendance, false for removing by calendar id
  1668. * @return int affected rows
  1669. */
  1670. public function attendance_calendar_delete($calendar_id, $attendance_id , $all_delete = false)
  1671. {
  1672. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1673. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1674. $attendance_id = intval($attendance_id);
  1675. // get all registered users inside current course
  1676. $users = $this->get_users_rel_course();
  1677. $user_ids = array_keys($users);
  1678. $course_id = api_get_course_int_id();
  1679. $affected_rows = 0;
  1680. if ($all_delete) {
  1681. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  1682. // get all dates from calendar by current attendance
  1683. if (!empty($attendance_calendar)) {
  1684. foreach ($attendance_calendar as $cal) {
  1685. // delete all data from attendance sheet
  1686. $sql = "DELETE FROM $tbl_attendance_sheet
  1687. WHERE c_id = $course_id AND attendance_calendar_id = '".intval($cal['id'])."'";
  1688. Database::query($sql);
  1689. // delete data from attendance calendar
  1690. $sql = "DELETE FROM $tbl_attendance_calendar
  1691. WHERE c_id = $course_id AND id = '".intval($cal['id'])."'";
  1692. Database::query($sql);
  1693. $this->deleteAttendanceCalendarGroup($cal['id'], $course_id);
  1694. $affected_rows++;
  1695. }
  1696. }
  1697. } else {
  1698. // delete just one row from attendance sheet by the calendar id
  1699. $sql = "DELETE FROM $tbl_attendance_sheet
  1700. WHERE c_id = $course_id AND attendance_calendar_id = '".intval($calendar_id)."'";
  1701. Database::query($sql);
  1702. // delete data from attendance calendar
  1703. $sql = "DELETE FROM $tbl_attendance_calendar
  1704. WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1705. Database::query($sql);
  1706. $this->deleteAttendanceCalendarGroup($calendar_id, $course_id);
  1707. $affected_rows++;
  1708. }
  1709. // update users' results
  1710. $this->update_users_results($user_ids, $attendance_id);
  1711. return $affected_rows;
  1712. }
  1713. /** Setters for fields of attendances tables **/
  1714. public function set_session_id($session_id)
  1715. {
  1716. $this->session_id = $session_id;
  1717. }
  1718. public function set_course_id($course_id)
  1719. {
  1720. $this->course_id = $course_id;
  1721. }
  1722. public function set_date_time($datetime)
  1723. {
  1724. $this->date_time = $datetime;
  1725. }
  1726. public function set_name($name)
  1727. {
  1728. $this->name = $name;
  1729. }
  1730. public function set_description($description)
  1731. {
  1732. $this->description = $description;
  1733. }
  1734. public function set_attendance_qualify_title($attendance_qualify_title)
  1735. {
  1736. $this->attendance_qualify_title = $attendance_qualify_title;
  1737. }
  1738. public function set_attendance_weight($attendance_weight)
  1739. {
  1740. $this->attendance_weight = $attendance_weight;
  1741. }
  1742. /** Getters for fields of attendances tables **/
  1743. public function get_session_id()
  1744. {
  1745. return $this->session_id;
  1746. }
  1747. public function get_course_id()
  1748. {
  1749. return $this->course_id;
  1750. }
  1751. public function get_date_time()
  1752. {
  1753. return $this->date_time;
  1754. }
  1755. public function get_name()
  1756. {
  1757. return $this->name;
  1758. }
  1759. public function get_description()
  1760. {
  1761. return $this->description;
  1762. }
  1763. public function get_attendance_qualify_title()
  1764. {
  1765. return $this->attendance_qualify_title;
  1766. }
  1767. public function get_attendance_weight()
  1768. {
  1769. return $this->attendance_weight;
  1770. }
  1771. /**
  1772. * @param string $startDate in UTC time
  1773. * @param string $endDate in UTC time
  1774. *
  1775. * @return array
  1776. */
  1777. public function getAttendanceLogin($startDate, $endDate)
  1778. {
  1779. if (empty($startDate) || $startDate == '0000-00-00' ||
  1780. empty($endDate) || $endDate == '0000-00-00'
  1781. ) {
  1782. return false;
  1783. }
  1784. $sessionId = api_get_session_id();
  1785. $courseCode = api_get_course_id();
  1786. if (!empty($sessionId)) {
  1787. $users = CourseManager:: get_user_list_from_course_code(
  1788. $courseCode,
  1789. $sessionId,
  1790. '',
  1791. 'lastname',
  1792. 0
  1793. );
  1794. } else {
  1795. $users = CourseManager:: get_user_list_from_course_code(
  1796. $courseCode,
  1797. 0,
  1798. '',
  1799. 'lastname',
  1800. STUDENT
  1801. );
  1802. }
  1803. $dateTimeStartOriginal = new DateTime($startDate);
  1804. $dateTimeStart = new DateTime($startDate);
  1805. $dateTimeEnd= new DateTime($endDate);
  1806. $interval = $dateTimeStart->diff($dateTimeEnd);
  1807. $days = intval($interval->format('%a'));
  1808. $dateList = array($dateTimeStart->format('Y-m-d'));
  1809. $headers = array(
  1810. get_lang('User'),
  1811. $dateTimeStart->format('Y-m-d')
  1812. );
  1813. for ($i = 0; $i < $days; $i++) {
  1814. $dateTimeStart = $dateTimeStart->add(new DateInterval('P1D'));
  1815. $date = $dateTimeStart->format('Y-m-d');
  1816. $dateList[] = $date;
  1817. $headers[] = $date;
  1818. }
  1819. $accessData = CourseManager::getCourseAccessPerCourseAndSession(
  1820. $courseCode,
  1821. $sessionId,
  1822. $dateTimeStartOriginal->format('Y-m-d H:i:s'),
  1823. $dateTimeEnd->format('Y-m-d H:i:s')
  1824. );
  1825. $results = array();
  1826. if (!empty($accessData)) {
  1827. foreach ($accessData as $data) {
  1828. $onlyDate = substr($data['login_course_date'], 0, 10);
  1829. $results[$data['user_id']][$onlyDate] = true;
  1830. }
  1831. }
  1832. return array(
  1833. 'users' => $users,
  1834. 'dateList' => $dateList,
  1835. 'headers' => $headers,
  1836. 'results' => $results
  1837. );
  1838. }
  1839. /**
  1840. * @param string $startDate in UTC time
  1841. * @param string $endDate in UTC time
  1842. *
  1843. * @return string
  1844. */
  1845. public function getAttendanceLoginTable($startDate, $endDate)
  1846. {
  1847. $data = $this->getAttendanceLogin($startDate, $endDate);
  1848. if (!$data) {
  1849. return null;
  1850. }
  1851. $headers = $data['headers'];
  1852. $dateList = $data['dateList'];
  1853. $users = $data['users'];
  1854. $results = $data['results'];
  1855. $table = new HTML_Table(array('class' => 'data_table'));
  1856. $row = 0;
  1857. $column = 0;
  1858. foreach ($headers as $header) {
  1859. $table->setHeaderContents($row, $column, $header);
  1860. $column++;
  1861. }
  1862. $row = 1;
  1863. foreach ($users as $user) {
  1864. $table->setCellContents(
  1865. $row,
  1866. 0,
  1867. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'
  1868. );
  1869. $row ++;
  1870. }
  1871. $column = 1;
  1872. $row = 1;
  1873. foreach ($users as $user) {
  1874. foreach ($dateList as $date) {
  1875. $status = null;
  1876. if (isset($results[$user['user_id']]) &&
  1877. isset($results[$user['user_id']][$date])
  1878. ) {
  1879. $status = 'X';
  1880. }
  1881. $table->setCellContents($row, $column, $status);
  1882. $column++;
  1883. }
  1884. $row++;
  1885. $column = 1;
  1886. }
  1887. return $table->toHtml();
  1888. }
  1889. /**
  1890. * @param string $startDate in UTC time
  1891. * @param string $endDate in UTC time
  1892. *
  1893. * @return string
  1894. */
  1895. public function exportAttendanceLogin($startDate, $endDate)
  1896. {
  1897. $data = $this->getAttendanceLogin($startDate, $endDate);
  1898. if (!$data) {
  1899. return null;
  1900. }
  1901. $users = $data['users'];
  1902. $results = $data['results'];
  1903. $table = new HTML_Table(array('class' => 'data_table'));
  1904. $table->setHeaderContents(0, 0, get_lang('User'));
  1905. $table->setHeaderContents(0, 1, get_lang('Date'));
  1906. $row = 1;
  1907. foreach ($users as $user) {
  1908. $table->setCellContents(
  1909. $row,
  1910. 0,
  1911. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'
  1912. );
  1913. $row++;
  1914. }
  1915. $table->setColAttributes(0, array('style' => 'width:28%'));
  1916. $row = 1;
  1917. foreach ($users as $user) {
  1918. if (isset($results[$user['user_id']]) &&
  1919. !empty($results[$user['user_id']])
  1920. ) {
  1921. $dates = implode(', ', array_keys($results[$user['user_id']]));
  1922. $table->setCellContents($row, 1, $dates);
  1923. }
  1924. $row++;
  1925. }
  1926. //$tableToString = null;
  1927. //$sessionInfo = api_get_session_info(api_get_session_id());
  1928. //if (!empty($sessionInfo)) {
  1929. /*$tableToString .= '<strong>'.get_lang('PeriodToDisplay').'</strong>: '.
  1930. sprintf(get_lang('FromDateXToDateY'), $startDate, $endDate);*/
  1931. //}
  1932. $tableToString = $table->toHtml();
  1933. $params = array(
  1934. 'filename' => get_lang('Attendance') . '_' . api_get_utc_datetime(),
  1935. 'pdf_title' => get_lang('Attendance'),
  1936. 'course_code' => api_get_course_id(),
  1937. 'show_real_course_teachers' => true
  1938. );
  1939. $pdf = new PDF('A4', null, $params);
  1940. $pdf->html_to_pdf_with_template($tableToString);
  1941. }
  1942. }