attendance.lib.php 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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. // Initializing database table and variables
  62. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  63. $data = array();
  64. if (empty($course_id)) {
  65. $course_id = api_get_course_int_id();
  66. } else {
  67. $course_id = intval($course_id);
  68. }
  69. $session_id = isset($session_id)?intval($session_id):api_get_session_id();
  70. $condition_session = api_get_session_condition($session_id);
  71. // Get attendance data
  72. $sql = "SELECT id, name, attendance_qualify_max
  73. FROM $tbl_attendance
  74. WHERE c_id = $course_id AND active = 1 $condition_session ";
  75. $rs = Database::query($sql);
  76. if (Database::num_rows($rs) > 0) {
  77. while ($row = Database::fetch_array($rs,'ASSOC')) {
  78. $data[$row['id']] = $row;
  79. }
  80. }
  81. return $data;
  82. }
  83. /**
  84. * Get the attendaces to display on the current page (fill the sortable-table)
  85. * @param int offset of first user to recover
  86. * @param int Number of users to get
  87. * @param int Column to sort on
  88. * @param string Order (ASC,DESC)
  89. * @see SortableTable#get_table_data($from)
  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. $param_gradebook = '';
  126. if (isset($_SESSION['gradebook'])) {
  127. $param_gradebook = '&gradebook='.$_SESSION['gradebook'];
  128. }
  129. $user_info = api_get_user_info();
  130. $allowDelete = api_get_setting('allow_delete_attendance');
  131. while ($attendance = Database::fetch_row($res)) {
  132. $student_param = '';
  133. if (api_is_drh() && $_GET['student_id']) {
  134. $student_param = '&student_id='.intval($_GET['student_id']);
  135. }
  136. $session_star = '';
  137. if (api_get_session_id() == $attendance[6]) {
  138. $session_star = api_get_session_image(api_get_session_id(), $user_info['status']);
  139. }
  140. if ($attendance[5] == 1) {
  141. $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
  142. api_get_user_id(),
  143. api_get_course_info()
  144. );
  145. if (api_is_allowed_to_edit(null, true) || $isDrhOfCourse) {
  146. // Link to edit
  147. $attendance[1] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$attendance[0].$param_gradebook.$student_param.'">'.$attendance[1].'</a>'.$session_star;
  148. } else {
  149. // Link to view
  150. $attendance[1] = '<a href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list_no_edit&attendance_id='.$attendance[0].$param_gradebook.$student_param.'">'.$attendance[1].'</a>'.$session_star;
  151. }
  152. } else {
  153. $attendance[1] = '<a class="muted" href="index.php?'.api_get_cidreq().'&action=attendance_sheet_list&attendance_id='.$attendance[0].$param_gradebook.$student_param.'">'.$attendance[1].'</a>'.$session_star;
  154. }
  155. if ($attendance[5] == 1) {
  156. $attendance[3] = '<center>'.$attendance[3].'</center>';
  157. } else {
  158. $attendance[3] = '<center><span class="muted">'.$attendance[3].'</span></center>';
  159. }
  160. $attendance[3] = '<center>'.$attendance[3].'</center>';
  161. if (api_is_allowed_to_edit(null, true)) {
  162. $actions = '';
  163. $actions .= '<center>';
  164. if (api_is_platform_admin()) {
  165. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$attendance[0].$param_gradebook.'">'.
  166. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  167. // Visible
  168. if ($attendance[5] == 1) {
  169. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_set_invisible&attendance_id='.$attendance[0].$param_gradebook.'">'.
  170. Display::return_icon('visible.png', get_lang('Hide'), array(), ICON_SIZE_SMALL).'</a>';
  171. } else {
  172. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_set_visible&attendance_id='.$attendance[0].$param_gradebook.'">'.
  173. Display::return_icon('invisible.png', get_lang('Show'), array(), ICON_SIZE_SMALL).'</a>';
  174. $attendance[2] = '<span class="muted">'.$attendance[2].'</span>';
  175. }
  176. if ($allowDelete) {
  177. $actions .= '<a href="index.php?' . api_get_cidreq() . '&action=attendance_delete&attendance_id=' . $attendance[0] . $param_gradebook . '">' .
  178. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  179. }
  180. } else {
  181. $is_locked_attendance = self::is_locked_attendance($attendance[0]);
  182. if ($is_locked_attendance) {
  183. $actions .= Display::return_icon('edit_na.png', get_lang('Edit')).'&nbsp;';
  184. $actions .= Display::return_icon('visible.png', get_lang('Hide'));
  185. } else {
  186. $actions .= '<a href="index.php?'.api_get_cidreq().'&action=attendance_edit&attendance_id='.$attendance[0].$param_gradebook.'">'.
  187. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;';
  188. if ($attendance[5] == 1) {
  189. $actions .= ' <a href="index.php?'.api_get_cidreq().'&action=attendance_set_invisible&attendance_id='.$attendance[0].$param_gradebook.'">'.
  190. Display::return_icon('visible.png', get_lang('Hide'), array(), ICON_SIZE_SMALL).'</a>';
  191. } else {
  192. $actions .= ' <a href="index.php?'.api_get_cidreq().'&action=attendance_set_visible&attendance_id='.$attendance[0].$param_gradebook.'">'.
  193. Display::return_icon('invisible.png', get_lang('Show'), array(), ICON_SIZE_SMALL).'</a>';
  194. $attendance[2] = '<span class="muted">'.$attendance[2].'</span>';
  195. }
  196. if ($allowDelete === 'true') {
  197. $actions .= ' <a href="index.php?' . api_get_cidreq() . '&action=attendance_delete&attendance_id=' . $attendance[0] . $param_gradebook . '">' .
  198. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  199. }
  200. }
  201. }
  202. // display lock/unlock icon
  203. $is_done_all_calendar = self::is_all_attendance_calendar_done($attendance[0]);
  204. if ($is_done_all_calendar) {
  205. $locked = $attendance[4];
  206. if ($locked == 0) {
  207. if (api_is_platform_admin()) {
  208. $message_alert = get_lang('AreYouSureToLockTheAttendance');
  209. } else {
  210. $message_alert = get_lang('UnlockMessageInformation');
  211. }
  212. $actions .= '&nbsp;<a onclick="javascript:if(!confirm(\''.$message_alert.'\')) return false;" href="index.php?'.api_get_cidreq().'&action=lock_attendance&attendance_id='.$attendance[0].$param_gradebook.'">'.Display::return_icon('unlock.png', get_lang('LockAttendance')).'</a>';
  213. } else {
  214. if (api_is_platform_admin()) {
  215. $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].$param_gradebook.'">'.Display::return_icon('locked.png', get_lang('UnlockAttendance')).'</a>';
  216. } else {
  217. $actions .= '&nbsp;'.Display::return_icon('locked_na.png', get_lang('LockedAttendance'));
  218. }
  219. }
  220. }
  221. $actions .= '</center>';
  222. $attendances[] = array($attendance[0], $attendance[1], $attendance[2], $attendance[3],$actions);
  223. } else {
  224. $attendance[0] = '&nbsp;';
  225. $attendances[] = array($attendance[0], $attendance[1], $attendance[2], $attendance[3]);
  226. }
  227. }
  228. return $attendances;
  229. }
  230. /**
  231. * Get the attendances by id to display on the current page
  232. * @param int $attendance_id
  233. * @return array attendance data
  234. */
  235. public function get_attendance_by_id($attendance_id)
  236. {
  237. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  238. $attendance_id = intval($attendance_id);
  239. $course_id = api_get_course_int_id();
  240. $attendance_data = array();
  241. $sql = "SELECT * FROM $tbl_attendance
  242. WHERE c_id = $course_id AND id = '$attendance_id'";
  243. $res = Database::query($sql);
  244. if (Database::num_rows($res) > 0) {
  245. while ($row = Database::fetch_array($res)) {
  246. $attendance_data = $row;
  247. }
  248. }
  249. return $attendance_data;
  250. }
  251. /**
  252. * Add attendances sheet inside table. This is the *list of* dates, not
  253. * a specific date in itself.
  254. * @param bool true for adding link in gradebook or false otherwise (optional)
  255. * @return int last attendance id
  256. */
  257. public function attendance_add($link_to_gradebook = false)
  258. {
  259. $_course = api_get_course_info();
  260. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  261. $table_link = Database:: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  262. $session_id = api_get_session_id();
  263. $user_id = api_get_user_id();
  264. $course_code = $_course['code'];
  265. $course_id = $_course['real_id'];
  266. $title_gradebook= $this->attendance_qualify_title;
  267. $value_calification = 0;
  268. $weight_calification = floatval($this->attendance_weight);
  269. $params = [
  270. 'c_id' => $course_id,
  271. 'name' => $this->name,
  272. 'description' => $this->description,
  273. 'attendance_qualify_title' => $title_gradebook,
  274. 'attendance_weight' => $weight_calification,
  275. 'session_id' => $session_id
  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_id = GradebookUtils::is_resource_in_course_gradebook(
  360. $course_code,
  361. 7,
  362. $attendance_id,
  363. $session_id
  364. );
  365. if (!$link_id) {
  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_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 = '<img src ="'.$userInfo['avatar_small'].'" />';
  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. $count_presences = 0;
  770. if (count($user_ids) > 0) {
  771. foreach ($user_ids as $uid) {
  772. $count_presences = 0;
  773. if (count($calendar_ids) > 0) {
  774. $sql = "SELECT count(presence) as count_presences
  775. FROM $tbl_attendance_sheet
  776. WHERE
  777. c_id = $course_id AND
  778. user_id = '$uid' AND
  779. attendance_calendar_id IN (".implode(',', $calendar_ids).") AND
  780. presence = 1";
  781. $rs_count = Database::query($sql);
  782. $row_count = Database::fetch_array($rs_count);
  783. $count_presences = $row_count['count_presences'];
  784. }
  785. // save results
  786. $sql = "SELECT id FROM $tbl_attendance_result
  787. WHERE
  788. c_id = $course_id AND
  789. user_id = '$uid' AND
  790. attendance_id = '$attendance_id' ";
  791. $rs_check_result = Database::query($sql);
  792. if (Database::num_rows($rs_check_result) > 0) {
  793. // update result
  794. $sql = "UPDATE $tbl_attendance_result SET
  795. score = '$count_presences'
  796. WHERE
  797. c_id = $course_id AND
  798. user_id='$uid' AND
  799. attendance_id='$attendance_id'";
  800. Database::query($sql);
  801. } else {
  802. // insert new result
  803. $sql = "INSERT INTO $tbl_attendance_result SET
  804. c_id = $course_id ,
  805. user_id = '$uid',
  806. attendance_id = '$attendance_id',
  807. score = '$count_presences'";
  808. Database::query($sql);
  809. $insertId = Database::insert_id();
  810. if ($insertId) {
  811. $sql = "UPDATE $tbl_attendance_result SET id = iid WHERE iid = $insertId";
  812. Database::query($sql);
  813. }
  814. }
  815. }
  816. }
  817. // update attendance qualify max
  818. $count_done_calendar = self::get_done_attendance_calendar($attendance_id);
  819. $sql = "UPDATE $tbl_attendance SET
  820. attendance_qualify_max = '$count_done_calendar'
  821. WHERE c_id = $course_id AND id = '$attendance_id'";
  822. Database::query($sql);
  823. }
  824. /**
  825. * update attendance_sheet_log table, is used as history of an attendance sheet
  826. * @param int Attendance id
  827. * @param string Last edit datetime
  828. * @param string Event type ('locked_attendance', 'done_attendance_sheet' ...)
  829. * @param int Last edit user id
  830. * @param string Calendar datetime value (optional, when event type is 'done_attendance_sheet')
  831. * @return int Affected rows
  832. */
  833. public function save_attendance_sheet_log(
  834. $attendance_id,
  835. $lastedit_date,
  836. $lastedit_type,
  837. $lastedit_user_id,
  838. $calendar_date_value = null
  839. ) {
  840. $course_id = api_get_course_int_id();
  841. // define table
  842. $tbl_attendance_sheet_log = Database::get_course_table(TABLE_ATTENDANCE_SHEET_LOG);
  843. // protect data
  844. $attendance_id = intval($attendance_id);
  845. $lastedit_user_id = intval($lastedit_user_id);
  846. if (isset($calendar_date_value)) {
  847. $calendar_date_value = $calendar_date_value;
  848. } else {
  849. $calendar_date_value = '';
  850. }
  851. // save data
  852. $params = [
  853. 'c_id' => $course_id,
  854. 'attendance_id' => $attendance_id,
  855. 'lastedit_date' => $lastedit_date,
  856. 'lastedit_type' => $lastedit_type,
  857. 'lastedit_user_id' => $lastedit_user_id,
  858. 'calendar_date_value' => $calendar_date_value,
  859. ];
  860. $insertId = Database::insert($tbl_attendance_sheet_log, $params);
  861. if ($insertId) {
  862. $sql = "UPDATE $tbl_attendance_sheet_log SET id = iid WHERE iid = $insertId";
  863. Database::query($sql);
  864. }
  865. return $insertId;
  866. }
  867. /**
  868. * Get number of done attendances inside current sheet
  869. * @param int attendance id
  870. * @return int number of done attendances
  871. */
  872. public static function get_done_attendance_calendar($attendance_id)
  873. {
  874. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  875. $attendance_id = intval($attendance_id);
  876. $course_id = api_get_course_int_id();
  877. $sql = "SELECT count(done_attendance) as count
  878. FROM $tbl_attendance_calendar
  879. WHERE
  880. c_id = $course_id AND
  881. attendance_id = '$attendance_id' AND
  882. done_attendance = 1
  883. ";
  884. $rs = Database::query($sql);
  885. $row = Database::fetch_array($rs);
  886. $count = $row['count'];
  887. return $count;
  888. }
  889. /**
  890. * Get results of faults (absents) by user
  891. * @param int $user_id
  892. * @param int $attendance_id
  893. * @param int $groupId
  894. * @return array results containing number of faults, total done attendance,
  895. * percent of faults and color depend on result (red, orange)
  896. */
  897. public function get_faults_of_user($user_id, $attendance_id, $groupId = null)
  898. {
  899. // initializing database table and variables
  900. $user_id = intval($user_id);
  901. $attendance_id = intval($attendance_id);
  902. $results = array();
  903. $calendar_count = self::get_number_of_attendance_calendar($attendance_id, $groupId, NULL, $user_id);
  904. // $total_done_attendance = $attendance_data['attendance_qualify_max'];
  905. $total_done_attendance = self::get_number_of_attendance_calendar($attendance_id, $groupId, true, $user_id);
  906. $attendance_user_score = $this->get_user_score($user_id, $attendance_id, $groupId);
  907. //This is the main change of the BT#1381
  908. //$total_done_attendance = $calendar_count;
  909. // calculate results
  910. $faults = $total_done_attendance - $attendance_user_score;
  911. if (empty($calendar_count)) {
  912. $faults = 0;
  913. }
  914. $faults = $faults > 0 ? $faults : 0;
  915. $faults_porcent = $calendar_count > 0 ?round(($faults*100)/$calendar_count,0):0;
  916. $results['faults'] = $faults;
  917. $results['total'] = $calendar_count;
  918. $results['faults_porcent'] = $faults_porcent;
  919. $color_bar = '';
  920. if ($faults_porcent > 25) {
  921. $color_bar = '#f28989';
  922. } else if ($faults_porcent > 10) {
  923. $color_bar = '#F90';
  924. }
  925. $results['color_bar'] = $color_bar;
  926. return $results;
  927. }
  928. /**
  929. * Get results of faults average for all courses by user
  930. * @param int $user_id
  931. * @return array results containing number of faults, total done attendance,
  932. * percentage of faults and color depend on result (red, orange)
  933. */
  934. public function get_faults_average_inside_courses($user_id)
  935. {
  936. // get all courses of current user
  937. $courses = CourseManager::get_courses_list_by_user_id($user_id, true);
  938. $user_id = intval($user_id);
  939. $results = array();
  940. $total_faults = $total_weight = $porcent = 0;
  941. foreach ($courses as $course) {
  942. //$course_code = $course['code'];
  943. //$course_info = api_get_course_info($course_code);
  944. $course_id = $course['real_id'];
  945. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  946. $attendances_by_course = $this->get_attendances_list($course_id);
  947. foreach ($attendances_by_course as $attendance) {
  948. // get total faults and total weight
  949. $total_done_attendance = $attendance['attendance_qualify_max'];
  950. $sql = "SELECT score
  951. FROM $tbl_attendance_result
  952. WHERE
  953. c_id = $course_id AND
  954. user_id = $user_id AND
  955. attendance_id = ".$attendance['id'];
  956. $rs = Database::query($sql);
  957. $score = 0;
  958. if (Database::num_rows($rs) > 0) {
  959. $row = Database::fetch_array($rs);
  960. $score = $row['score'];
  961. }
  962. $faults = $total_done_attendance-$score;
  963. $faults = $faults > 0 ? $faults:0;
  964. $total_faults += $faults;
  965. $total_weight += $total_done_attendance;
  966. }
  967. }
  968. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  969. $results['faults'] = $total_faults;
  970. $results['total'] = $total_weight;
  971. $results['porcent'] = $porcent;
  972. return $results;
  973. }
  974. /**
  975. * Get results of faults average by course
  976. * @param int $user_id
  977. * @param string $course_code
  978. * @param int Session id (optional)
  979. * @return array results containing number of faults,
  980. * total done attendance, porcent of faults and color depend on result (red, orange)
  981. */
  982. public function get_faults_average_by_course($user_id, $course_code, $session_id = null)
  983. {
  984. // Database tables and variables
  985. $course_info = api_get_course_info($course_code);
  986. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  987. $user_id = intval($user_id);
  988. $results = array();
  989. $total_faults = $total_weight = $porcent = 0;
  990. $attendances_by_course = $this->get_attendances_list($course_info['real_id'], $session_id);
  991. foreach ($attendances_by_course as $attendance) {
  992. // Get total faults and total weight
  993. $total_done_attendance = $attendance['attendance_qualify_max'];
  994. $sql = "SELECT score FROM $tbl_attendance_result
  995. WHERE
  996. c_id = {$course_info['real_id']} AND
  997. user_id = $user_id AND
  998. attendance_id=".$attendance['id'];
  999. $rs = Database::query($sql);
  1000. $score = 0;
  1001. if (Database::num_rows($rs) > 0) {
  1002. $row = Database::fetch_array($rs);
  1003. $score = $row['score'];
  1004. }
  1005. $faults = $total_done_attendance-$score;
  1006. $faults = $faults > 0 ? $faults:0;
  1007. $total_faults += $faults;
  1008. $total_weight += $total_done_attendance;
  1009. }
  1010. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  1011. $results['faults'] = $total_faults;
  1012. $results['total'] = $total_weight;
  1013. $results['porcent'] = $porcent;
  1014. return $results;
  1015. }
  1016. /**
  1017. * Get registered users' attendance sheet inside current course
  1018. * @param int $attendance_id
  1019. * @param int $user_id for showing data for only one user (optional)
  1020. * @return array users attendance sheet data
  1021. */
  1022. public function get_users_attendance_sheet($attendance_id, $user_id = 0, $groupId = null)
  1023. {
  1024. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1025. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1026. $attendance_calendar = $this->get_attendance_calendar($attendance_id, 'all', null, $groupId);
  1027. $calendar_ids = array();
  1028. // get all dates from calendar by current attendance
  1029. foreach ($attendance_calendar as $cal) {
  1030. $calendar_ids[] = $cal['id'];
  1031. }
  1032. $course_id = api_get_course_int_id();
  1033. $data = array();
  1034. if (empty($user_id)) {
  1035. // get all registered users inside current course
  1036. $users = $this->get_users_rel_course();
  1037. $user_ids = array_keys($users);
  1038. if (count($calendar_ids) > 0 && count($user_ids) > 0) {
  1039. foreach ($user_ids as $uid) {
  1040. $sql = "SELECT * FROM $tbl_attendance_sheet
  1041. WHERE
  1042. c_id = $course_id AND
  1043. user_id = '$uid' AND
  1044. attendance_calendar_id IN(".implode(',',$calendar_ids).")
  1045. ";
  1046. $res = Database::query($sql);
  1047. if (Database::num_rows($res) > 0) {
  1048. while ($row = Database::fetch_array($res)) {
  1049. $data[$uid][$row['attendance_calendar_id']]['presence'] = $row['presence'];
  1050. }
  1051. }
  1052. }
  1053. }
  1054. } else {
  1055. // Get attendance for current user
  1056. $user_id = intval($user_id);
  1057. if (count($calendar_ids) > 0) {
  1058. $sql = "SELECT cal.date_time, att.presence
  1059. FROM $tbl_attendance_sheet att
  1060. INNER JOIN $tbl_attendance_calendar cal
  1061. ON cal.id = att.attendance_calendar_id
  1062. WHERE
  1063. att.c_id = $course_id AND
  1064. cal.c_id = $course_id AND
  1065. att.user_id = '$user_id' AND
  1066. att.attendance_calendar_id IN (".implode(',',$calendar_ids).")
  1067. ORDER BY date_time";
  1068. $res = Database::query($sql);
  1069. if (Database::num_rows($res) > 0) {
  1070. while ($row = Database::fetch_array($res)) {
  1071. $row['date_time'] = api_convert_and_format_date($row['date_time'], null, date_default_timezone_get());
  1072. $data[$user_id][] = $row;
  1073. }
  1074. }
  1075. }
  1076. }
  1077. return $data;
  1078. }
  1079. /**
  1080. * Get next attendance calendar without presences (done attendances)
  1081. * @param int attendance id
  1082. * @return int attendance calendar id
  1083. */
  1084. public function get_next_attendance_calendar_id($attendance_id)
  1085. {
  1086. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1087. $attendance_id = intval($attendance_id);
  1088. $course_id = api_get_course_int_id();
  1089. $sql = "SELECT id FROM $tbl_attendance_calendar
  1090. WHERE
  1091. c_id = $course_id AND
  1092. attendance_id = '$attendance_id' AND
  1093. done_attendance = 0
  1094. ORDER BY date_time
  1095. LIMIT 1";
  1096. $rs = Database::query($sql);
  1097. $next_calendar_id = 0;
  1098. if (Database::num_rows($rs) > 0) {
  1099. $row = Database::fetch_array($rs);
  1100. $next_calendar_id = $row['id'];
  1101. }
  1102. return $next_calendar_id;
  1103. }
  1104. /**
  1105. * Get next attendance calendar datetime without presences (done attendances)
  1106. * @param int attendance id
  1107. * @return int UNIX time format datetime
  1108. */
  1109. public function get_next_attendance_calendar_datetime($attendance_id)
  1110. {
  1111. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1112. $course_id = api_get_course_int_id();
  1113. $attendance_id = intval($attendance_id);
  1114. $sql = "SELECT id, date_time FROM $tbl_attendance_calendar
  1115. WHERE
  1116. c_id = $course_id AND
  1117. attendance_id = '$attendance_id' AND
  1118. done_attendance = 0
  1119. ORDER BY date_time
  1120. LIMIT 1";
  1121. $rs = Database::query($sql);
  1122. $next_calendar_datetime = 0;
  1123. if (Database::num_rows($rs) > 0) {
  1124. $row = Database::fetch_array($rs);
  1125. $next_calendar_datetime = api_get_local_time($row['date_time']);
  1126. }
  1127. return $next_calendar_datetime;
  1128. }
  1129. /**
  1130. * Get user' score from current attendance
  1131. * @param int $user_id
  1132. * @param int $attendance_id
  1133. * @return int score
  1134. */
  1135. public function get_user_score($user_id, $attendance_id, $groupId = null)
  1136. {
  1137. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  1138. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1139. $tbl_attendance_cal_rel_group = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1140. $tbl_attendance_cal = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1141. $user_id = intval($user_id);
  1142. $attendance_id = intval($attendance_id);
  1143. $course_id = api_get_course_int_id();
  1144. if (empty($groupId)) {
  1145. $sql = "SELECT score FROM $tbl_attendance_result
  1146. WHERE
  1147. c_id = $course_id AND
  1148. user_id='$user_id' AND
  1149. attendance_id='$attendance_id'";
  1150. } else {
  1151. $sql = "SELECT count(presence) as score FROM $tbl_attendance_sheet
  1152. WHERE
  1153. c_id = $course_id AND
  1154. user_id='$user_id' AND
  1155. presence = 1 AND
  1156. attendance_calendar_id IN (
  1157. SELECT calendar_id FROM $tbl_attendance_cal_rel_group crg
  1158. INNER JOIN $tbl_attendance_cal c
  1159. ON (crg.calendar_id = c.id)
  1160. WHERE
  1161. crg.c_id = $course_id AND
  1162. crg.group_id = $groupId AND
  1163. c.attendance_id = $attendance_id
  1164. )
  1165. ";
  1166. }
  1167. $rs = Database::query($sql);
  1168. $score = 0;
  1169. if (Database::num_rows($rs) > 0) {
  1170. $row = Database::fetch_array($rs);
  1171. $score = $row['score'];
  1172. }
  1173. return $score;
  1174. }
  1175. /**
  1176. * Get attendance calendar data by id
  1177. * @param int attendance calendar id
  1178. * @return array attendance calendar data
  1179. */
  1180. public function get_attendance_calendar_by_id($calendar_id)
  1181. {
  1182. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1183. $calendar_id = intval($calendar_id);
  1184. $course_id = api_get_course_int_id();
  1185. $sql = "SELECT * FROM $tbl_attendance_calendar
  1186. WHERE c_id = $course_id AND id = '$calendar_id' ";
  1187. $rs = Database::query($sql);
  1188. $data = array();
  1189. if (Database::num_rows($rs) > 0) {
  1190. while ($row = Database::fetch_array($rs)) {
  1191. $row['date_time'] = api_get_local_time($row['date_time']);
  1192. $data = $row;
  1193. }
  1194. }
  1195. return $data;
  1196. }
  1197. /**
  1198. * Get all attendance calendar data inside current attendance
  1199. * @param int $attendance_id
  1200. * @param string $type
  1201. * @param int $calendar_id
  1202. * @param int $groupId
  1203. * @param bool $showAll = false show group calendar items or not
  1204. *
  1205. * @return array attendance calendar data
  1206. */
  1207. public function get_attendance_calendar(
  1208. $attendance_id,
  1209. $type = 'all',
  1210. $calendar_id = null,
  1211. $groupId = null,
  1212. $showAll = false
  1213. ) {
  1214. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1215. $tbl_acrg = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1216. $attendance_id = intval($attendance_id);
  1217. $course_id = api_get_course_int_id();
  1218. $groupCondition = null;
  1219. if ($showAll) {
  1220. $sql = "SELECT * FROM $tbl_attendance_calendar c
  1221. WHERE c_id = $course_id AND attendance_id = '$attendance_id'";
  1222. } else {
  1223. $sql = "SELECT * FROM $tbl_attendance_calendar c
  1224. WHERE
  1225. c_id = $course_id AND
  1226. attendance_id = '$attendance_id' AND
  1227. id NOT IN (
  1228. SELECT calendar_id FROM $tbl_acrg
  1229. WHERE c_id = $course_id AND group_id != 0 AND group_id IS NOT NULL
  1230. )
  1231. ";
  1232. }
  1233. if (!empty($groupId)) {
  1234. $groupId = intval($groupId);
  1235. $sql = "SELECT c.* FROM $tbl_attendance_calendar c
  1236. INNER JOIN $tbl_acrg g
  1237. ON c.c_id = g.c_id AND c.id = g.calendar_id
  1238. WHERE
  1239. c.c_id = $course_id AND
  1240. g.group_id = '$groupId' AND
  1241. c.attendance_id = '$attendance_id'
  1242. ";
  1243. }
  1244. if (!in_array($type, array('today', 'all', 'all_done', 'all_not_done','calendar_id'))) {
  1245. $type = 'all';
  1246. }
  1247. switch ($type) {
  1248. case 'calendar_id':
  1249. $calendar_id = intval($calendar_id);
  1250. if (!empty($calendar_id)) {
  1251. $sql.= " AND c.id = $calendar_id";
  1252. }
  1253. break;
  1254. case 'today':
  1255. //$sql .= ' AND DATE_FORMAT(date_time,"%d-%m-%Y") = DATE_FORMAT("'.api_get_utc_datetime().'", "%d-%m-%Y" )';
  1256. break;
  1257. case 'all_done':
  1258. $sql .= " AND done_attendance = 1 ";
  1259. break;
  1260. case 'all_not_done':
  1261. $sql .= " AND done_attendance = 0 ";
  1262. break;
  1263. case 'all':
  1264. default:
  1265. break;
  1266. }
  1267. $sql .= " ORDER BY date_time ";
  1268. $rs = Database::query($sql);
  1269. $data = array();
  1270. if (Database::num_rows($rs) > 0) {
  1271. while ($row = Database::fetch_array($rs,'ASSOC')) {
  1272. $row['db_date_time'] = $row['date_time'];
  1273. $row['date_time'] = api_get_local_time($row['date_time']);
  1274. $row['date'] = api_format_date($row['date_time'], DATE_FORMAT_SHORT);
  1275. $row['time'] = api_format_date($row['date_time'], TIME_NO_SEC_FORMAT);
  1276. $row['groups'] = $this->getGroupListByAttendanceCalendar($row['id'], $course_id);
  1277. if ($type == 'today') {
  1278. if (date('d-m-Y', api_strtotime($row['date_time'], 'UTC')) == date('d-m-Y', time())) {
  1279. $data[] = $row;
  1280. }
  1281. } else {
  1282. $data[] = $row;
  1283. }
  1284. }
  1285. }
  1286. return $data;
  1287. }
  1288. /**
  1289. * Get number of attendance calendar inside current attendance
  1290. * @param int $attendance_id
  1291. * @param int $groupId
  1292. * @return int number of dates in attendance calendar
  1293. */
  1294. public static function get_number_of_attendance_calendar($attendance_id, $groupId = 0, $done_attendance = NULL, $userId = 0)
  1295. {
  1296. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1297. $calendarRelGroup = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP);
  1298. $tbl_groupRelUser = Database::get_course_table(TABLE_GROUP_USER);
  1299. $attendance_id = intval($attendance_id);
  1300. $groupId = intval($groupId);
  1301. $course_id = api_get_course_int_id();
  1302. $where_attendance = '';
  1303. if ($done_attendance) {
  1304. $where_attendance = ' done_attendance = 1 AND ';
  1305. }
  1306. if (empty($userId)) {
  1307. if (empty($groupId)) {
  1308. $sql = "SELECT count(a.id)
  1309. FROM $tbl_attendance_calendar a
  1310. WHERE
  1311. c_id = $course_id AND
  1312. $where_attendance
  1313. attendance_id = '$attendance_id' AND
  1314. id NOT IN (
  1315. SELECT calendar_id FROM $calendarRelGroup
  1316. WHERE
  1317. c_id = $course_id AND
  1318. group_id != 0 AND
  1319. group_id IS NOT NULL
  1320. )
  1321. ";
  1322. } else {
  1323. $sql = "SELECT count(a.id)
  1324. FROM $tbl_attendance_calendar a
  1325. INNER JOIN $calendarRelGroup g
  1326. ON (a.id = g.calendar_id AND a.c_id = g.c_id)
  1327. WHERE
  1328. a.c_id = $course_id AND
  1329. $where_attendance
  1330. attendance_id = '$attendance_id' AND
  1331. group_id = $groupId
  1332. ";
  1333. }
  1334. } else {
  1335. if (empty($groupId)) {
  1336. $sql = "SELECT count(a.id)
  1337. FROM $tbl_attendance_calendar a
  1338. WHERE
  1339. c_id = $course_id AND
  1340. $where_attendance
  1341. attendance_id = '$attendance_id' AND
  1342. id NOT IN (
  1343. SELECT calendar_id FROM $calendarRelGroup
  1344. WHERE
  1345. c_id = $course_id AND
  1346. group_id != 0 AND
  1347. group_id IS NOT NULL AND
  1348. group_id NOT IN (
  1349. SELECT group_id
  1350. FROM $tbl_groupRelUser
  1351. WHERE user_id = $userId
  1352. )
  1353. )
  1354. ";
  1355. } else {
  1356. $sql = "SELECT count(a.id)
  1357. FROM $tbl_attendance_calendar a
  1358. INNER JOIN $calendarRelGroup g
  1359. ON (a.id = g.calendar_id AND a.c_id = g.c_id)
  1360. WHERE
  1361. a.c_id = $course_id AND
  1362. $where_attendance
  1363. attendance_id = '$attendance_id' AND
  1364. group_id = $groupId
  1365. ";
  1366. }
  1367. }
  1368. $rs = Database::query($sql);
  1369. $row = Database::fetch_row($rs);
  1370. $count = $row[0];
  1371. return $count;
  1372. }
  1373. /**
  1374. * Get count dates inside attendance calendar by attendance id
  1375. * @param int $attendance_id
  1376. * @return int count of dates
  1377. */
  1378. public static function get_count_dates_inside_attendance_calendar($attendance_id)
  1379. {
  1380. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1381. $attendance_id = intval($attendance_id);
  1382. $course_id = api_get_course_int_id();
  1383. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1384. WHERE
  1385. c_id = $course_id AND
  1386. attendance_id = '$attendance_id'";
  1387. $rs = Database::query($sql);
  1388. $count = 0;
  1389. if (Database::num_rows($rs) > 0) {
  1390. $row = Database::fetch_row($rs);
  1391. $count = $row[0];
  1392. }
  1393. return $count;
  1394. }
  1395. /**
  1396. * check if all calendar of an attendance is done
  1397. * @param int $attendance_id
  1398. * @return bool True if all calendar is done, otherwise false
  1399. */
  1400. public static function is_all_attendance_calendar_done($attendance_id)
  1401. {
  1402. $attendance_id = intval($attendance_id);
  1403. $done_calendar = self::get_done_attendance_calendar($attendance_id);
  1404. $count_dates_in_calendar = self::get_count_dates_inside_attendance_calendar($attendance_id);
  1405. $number_of_dates = self::get_number_of_attendance_calendar($attendance_id);
  1406. $result = false;
  1407. if ($number_of_dates && (intval($count_dates_in_calendar) == intval($done_calendar))) {
  1408. $result = true;
  1409. }
  1410. return $result;
  1411. }
  1412. /**
  1413. * check if an attendance is locked
  1414. * @param int $attendance_id
  1415. * @param bool
  1416. */
  1417. public static function is_locked_attendance($attendance_id)
  1418. {
  1419. //use gradebook lock
  1420. $result = api_resource_is_locked_by_gradebook($attendance_id, LINK_ATTENDANCE);
  1421. return $result;
  1422. }
  1423. /**
  1424. * Add new datetime inside attendance calendar table
  1425. * @param int $attendance_id
  1426. * @param array $groupList
  1427. * @return int affected rows
  1428. */
  1429. public function attendance_calendar_add($attendance_id, $groupList = array())
  1430. {
  1431. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1432. $affected_rows = 0;
  1433. $attendance_id = intval($attendance_id);
  1434. $course_id = api_get_course_int_id();
  1435. // check if datetime already exists inside the table
  1436. /*$sql = "SELECT id FROM $tbl_attendance_calendar
  1437. WHERE
  1438. c_id = $course_id AND
  1439. date_time='".Database::escape_string($this->date_time)."' AND
  1440. attendance_id = '$attendance_id'";
  1441. $rs = Database::query($sql);
  1442. if (Database::num_rows($rs) == 0) {*/
  1443. $params = array(
  1444. 'c_id' => $course_id,
  1445. 'date_time' => $this->date_time,
  1446. 'attendance_id' => $attendance_id
  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. }