attendance.lib.php 66 KB

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