attendance.lib.php 79 KB

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