attendance.lib.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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_configuration_value('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) {
  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. global $_course;
  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 = api_get_course_id();
  265. $course_id = api_get_course_int_id();
  266. $title_gradebook= Database::escape_string($this->attendance_qualify_title);
  267. $value_calification = 0;
  268. $weight_calification = floatval($this->attendance_weight);
  269. $sql = "INSERT INTO $tbl_attendance SET
  270. c_id = $course_id,
  271. name ='".Database::escape_string($this->name)."',
  272. description = '".Database::escape_string($this->description)."',
  273. attendance_qualify_title = '$title_gradebook',
  274. attendance_weight = '$weight_calification',
  275. session_id = '$session_id'";
  276. Database::query($sql);
  277. $affected_rows = Database::affected_rows();
  278. $last_id = 0;
  279. if (!empty($affected_rows)) {
  280. // save inside item property table
  281. $last_id = Database::insert_id();
  282. api_item_property_update($_course, TOOL_ATTENDANCE, $last_id,"AttendanceAdded", $user_id);
  283. }
  284. // add link to gradebook
  285. if ($link_to_gradebook && !empty($this->category_id)) {
  286. $description = '';
  287. $link_info = is_resource_in_course_gradebook($course_code,7,$last_id,$session_id);
  288. $link_id = $link_info['id'];
  289. if (!$link_info) {
  290. add_resource_to_course_gradebook(
  291. $this->category_id,
  292. $course_code,
  293. 7,
  294. $last_id,
  295. $title_gradebook,
  296. $weight_calification,
  297. $value_calification,
  298. $description,
  299. 1,
  300. $session_id
  301. );
  302. } else {
  303. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_id.'');
  304. }
  305. }
  306. return $last_id;
  307. }
  308. /**
  309. * edit attendances inside table
  310. * @param int attendance id
  311. * @param bool true for adding link in gradebook or false otherwise (optional)
  312. * @return int last id
  313. */
  314. public function attendance_edit($attendance_id, $link_to_gradebook = false)
  315. {
  316. $_course = api_get_course_info();
  317. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  318. $table_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  319. $session_id = api_get_session_id();
  320. $user_id = api_get_user_id();
  321. $attendance_id = intval($attendance_id);
  322. $course_code = api_get_course_id();
  323. $course_id = api_get_course_int_id();
  324. $title_gradebook = Database::escape_string($this->attendance_qualify_title);
  325. $value_calification = 0;
  326. $weight_calification= floatval($this->attendance_weight);
  327. if (!empty($attendance_id)) {
  328. $sql = "UPDATE $tbl_attendance
  329. SET name ='".Database::escape_string($this->name)."',
  330. description = '".Database::escape_string($this->description)."',
  331. attendance_qualify_title = '".$title_gradebook."',
  332. attendance_weight = '".$weight_calification."'
  333. WHERE c_id = $course_id AND id = '$attendance_id'";
  334. Database::query($sql);
  335. api_item_property_update($_course, TOOL_ATTENDANCE, $attendance_id,"AttendanceUpdated", $user_id);
  336. // add link to gradebook
  337. if ($link_to_gradebook && !empty($this->category_id)) {
  338. $description = '';
  339. $link_id = is_resource_in_course_gradebook($course_code, 7, $attendance_id, $session_id);
  340. if (!$link_id) {
  341. add_resource_to_course_gradebook(
  342. $this->category_id,
  343. $course_code,
  344. 7,
  345. $attendance_id,
  346. $title_gradebook,
  347. $weight_calification,
  348. $value_calification,
  349. $description,
  350. 1,
  351. $session_id
  352. );
  353. } else {
  354. Database::query('UPDATE '.$table_link.' SET weight='.$weight_calification.' WHERE id='.$link_id.'');
  355. }
  356. }
  357. return $attendance_id;
  358. }
  359. return null;
  360. }
  361. /**
  362. * Restore attendance
  363. * @param int|array one or many attendances id
  364. * @return int affected rows
  365. */
  366. public function attendance_restore($attendance_id)
  367. {
  368. $_course = api_get_course_info();
  369. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  370. $user_id = api_get_user_id();
  371. $course_id = api_get_course_int_id();
  372. if (is_array($attendance_id)) {
  373. foreach ($attendance_id as $id) {
  374. $id = intval($id);
  375. $sql = "UPDATE $tbl_attendance SET active = 1
  376. WHERE c_id = $course_id AND id = '$id'";
  377. Database::query($sql);
  378. $affected_rows = Database::affected_rows();
  379. if (!empty($affected_rows)) {
  380. // update row item property table
  381. api_item_property_update($_course, TOOL_ATTENDANCE, $id,"restore", $user_id);
  382. }
  383. }
  384. } else {
  385. $attendance_id = intval($attendance_id);
  386. $sql = "UPDATE $tbl_attendance SET active = 1
  387. WHERE c_id = $course_id AND id = '$attendance_id'";
  388. Database::query($sql);
  389. $affected_rows = Database::affected_rows();
  390. if (!empty($affected_rows)) {
  391. // update row item property table
  392. api_item_property_update($_course, TOOL_ATTENDANCE, $attendance_id,"restore", $user_id);
  393. }
  394. }
  395. return $affected_rows;
  396. }
  397. /**
  398. * Delete attendances
  399. * @param int|array $attendance_id one or many attendances id
  400. * @return int affected rows
  401. */
  402. public function attendance_delete($attendance_id)
  403. {
  404. $_course = api_get_course_info();
  405. $tbl_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  406. $user_id = api_get_user_id();
  407. $course_id = api_get_course_int_id();
  408. if (is_array($attendance_id)) {
  409. foreach ($attendance_id as $id) {
  410. $id = intval($id);
  411. $sql = "UPDATE $tbl_attendance SET active = 2
  412. WHERE c_id = $course_id AND id = '$id'";
  413. Database::query($sql);
  414. $affected_rows = Database::affected_rows();
  415. if (!empty($affected_rows)) {
  416. // update row item property table
  417. api_item_property_update($_course, TOOL_ATTENDANCE, $id, "delete", $user_id);
  418. }
  419. }
  420. } else {
  421. $attendance_id = intval($attendance_id);
  422. $sql = "UPDATE $tbl_attendance SET active = 2
  423. WHERE c_id = $course_id AND id = '$attendance_id'";
  424. Database::query($sql);
  425. $affected_rows = Database::affected_rows();
  426. if (!empty($affected_rows)) {
  427. // update row item property table
  428. api_item_property_update(
  429. $_course,
  430. TOOL_ATTENDANCE,
  431. $attendance_id,
  432. "delete",
  433. $user_id
  434. );
  435. }
  436. }
  437. return $affected_rows;
  438. }
  439. /**
  440. * Changes visibility
  441. * @param int|array $attendanceId one or many attendances id
  442. * @param status
  443. * @return int affected rows
  444. */
  445. public function changeVisibility($attendanceId, $status = 1)
  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 = api_get_course_int_id();
  451. $status = intval($status);
  452. $action = 'visible';
  453. if ($status == 0) {
  454. $action = 'invisible';
  455. }
  456. if (is_array($attendanceId)) {
  457. foreach ($attendanceId as $id) {
  458. $id = intval($id);
  459. $sql = "UPDATE $tbl_attendance SET active = $status
  460. WHERE c_id = $course_id AND id = '$id'";
  461. Database::query($sql);
  462. $affected_rows = Database::affected_rows();
  463. if (!empty($affected_rows)) {
  464. // update row item property table
  465. api_item_property_update($_course, TOOL_ATTENDANCE, $id, $action, $user_id);
  466. }
  467. }
  468. } else {
  469. $attendanceId = intval($attendanceId);
  470. $sql = "UPDATE $tbl_attendance SET active = $status
  471. WHERE c_id = $course_id AND id = '$attendanceId'";
  472. Database::query($sql);
  473. $affected_rows = Database::affected_rows();
  474. if (!empty($affected_rows)) {
  475. // update row item property table
  476. api_item_property_update(
  477. $_course,
  478. TOOL_ATTENDANCE,
  479. $attendanceId,
  480. $action,
  481. $user_id
  482. );
  483. }
  484. }
  485. return $affected_rows;
  486. }
  487. /**
  488. * Lock or unlock an attendance
  489. * @param int attendance id
  490. * @param bool True to lock or false otherwise
  491. */
  492. public function lock_attendance($attendance_id, $lock = true)
  493. {
  494. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  495. $course_id = api_get_course_int_id();
  496. $attendance_id = intval($attendance_id);
  497. $locked = ($lock)?1:0;
  498. $upd = "UPDATE $tbl_attendance SET locked = $locked
  499. WHERE c_id = $course_id AND id = $attendance_id";
  500. Database::query($upd);
  501. $affected_rows = Database::affected_rows();
  502. if ($affected_rows && $lock) {
  503. //save attendance sheet log
  504. $lastedit_date = Date('Y-m-d H:i:s');
  505. $lastedit_type = self::LOCKED_ATTENDANCE_LOG_TYPE;
  506. $lastedit_user_id = api_get_user_id();
  507. $this->save_attendance_sheet_log(
  508. $attendance_id,
  509. $lastedit_date,
  510. $lastedit_type,
  511. $lastedit_user_id
  512. );
  513. }
  514. return $affected_rows;
  515. }
  516. /**
  517. * Get registered users inside current course
  518. * @param int $attendance_id attendance id for showing attendance result field (optional)
  519. * @return array users data
  520. */
  521. public function get_users_rel_course($attendance_id = 0)
  522. {
  523. $current_session_id = api_get_session_id();
  524. $current_course_id = api_get_course_id();
  525. if (!empty($current_session_id)) {
  526. $a_course_users = CourseManager:: get_user_list_from_course_code(
  527. $current_course_id,
  528. $current_session_id,
  529. '',
  530. 'lastname'
  531. );
  532. } else {
  533. $a_course_users = CourseManager:: get_user_list_from_course_code(
  534. $current_course_id,
  535. 0,
  536. '',
  537. 'lastname'
  538. );
  539. }
  540. // get registered users inside current course
  541. $a_users = array();
  542. foreach ($a_course_users as $key =>$user_data) {
  543. $value = array();
  544. $uid = $user_data['user_id'];
  545. $status = $user_data['status'];
  546. $user_status_in_session = null;
  547. $user_status_in_course = null;
  548. if (api_get_session_id()) {
  549. $user_status_in_session = SessionManager::get_user_status_in_course_session(
  550. $uid,
  551. $current_course_id,
  552. $current_session_id
  553. );
  554. } else {
  555. $user_status_in_course = CourseManager::get_user_in_course_status(
  556. $uid,
  557. $current_course_id
  558. );
  559. }
  560. //Not taking into account DRH or COURSEMANAGER
  561. if ($uid <= 1 || $status == DRH || $user_status_in_course == COURSEMANAGER || $user_status_in_session == 2) continue;
  562. if (!empty($attendance_id)) {
  563. $user_faults = $this->get_faults_of_user($uid, $attendance_id);
  564. $value['attendance_result'] = $user_faults['faults'].'/'.$user_faults['total'].' ('.$user_faults['faults_porcent'].'%)';
  565. $value['result_color_bar'] = $user_faults['color_bar'];
  566. }
  567. // user's picture
  568. $image_path = UserManager::get_user_picture_path_by_id($uid, 'web', false);
  569. $user_profile = UserManager::get_picture_user($uid, $image_path['file'], 22, USER_IMAGE_SIZE_SMALL, ' width="22" height="22" ');
  570. if (!empty($image_path['file'])) {
  571. $photo = '<center><a class="thickbox" href="'.$image_path['dir'].$image_path['file'].'" ><img src="'.$user_profile['file'].'" '.$user_profile['style'].' alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" title="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" /></a></center>';
  572. } else {
  573. $photo = '<center><img src="'.$user_profile['file'].'" '.$user_profile['style'].' alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" title="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" /></center>';
  574. }
  575. $value['photo'] = $photo;
  576. $value['firstname'] = $user_data['firstname'];
  577. $value['lastname'] = $user_data['lastname'];
  578. $value['username'] = $user_data['username'];
  579. $value['user_id'] = $uid;
  580. //Sending only 5 items in the array instead of 60
  581. $a_users[$key] = $value;
  582. }
  583. return $a_users;
  584. }
  585. /**
  586. * add attendances sheet inside table
  587. * @param int $calendar_id attendance calendar id
  588. * @param array $users_present present users during current class
  589. * @param int $attendance_id
  590. * @return int affected rows
  591. */
  592. public function attendance_sheet_add($calendar_id, $users_present, $attendance_id)
  593. {
  594. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  595. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  596. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  597. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  598. $calendar_id = intval($calendar_id);
  599. $attendance_id = intval($attendance_id);
  600. $users = $this->get_users_rel_course();
  601. $course_id = api_get_course_int_id();
  602. $user_ids = array_keys($users);
  603. $users_absent = array_diff($user_ids,$users_present);
  604. $affected_rows = 0;
  605. // get last edit type
  606. $calendar_data = $this->get_attendance_calendar_by_id($calendar_id);
  607. $lastedit_type = self::DONE_ATTENDANCE_LOG_TYPE;
  608. if ($calendar_data['done_attendance']) {
  609. $lastedit_type = self::UPDATED_ATTENDANCE_LOG_TYPE;
  610. }
  611. // save users present in class
  612. foreach ($users_present as $user_present) {
  613. $uid = intval($user_present);
  614. // check if user already was registered with the $calendar_id
  615. $sql = "SELECT user_id FROM $tbl_attendance_sheet
  616. WHERE c_id = $course_id AND user_id='$uid' AND attendance_calendar_id = '$calendar_id'";
  617. $rs = Database::query($sql);
  618. if (Database::num_rows($rs) == 0) {
  619. $sql = "INSERT INTO $tbl_attendance_sheet SET
  620. c_id = $course_id,
  621. user_id = '$uid',
  622. attendance_calendar_id = '$calendar_id',
  623. presence = 1";
  624. Database::query($sql);
  625. $affected_rows += Database::affected_rows();
  626. } else {
  627. $sql = "UPDATE $tbl_attendance_sheet SET presence = 1
  628. WHERE c_id = $course_id AND user_id ='$uid' AND attendance_calendar_id = '$calendar_id'";
  629. Database::query($sql);
  630. $affected_rows += Database::affected_rows();
  631. }
  632. }
  633. // save users absent in class
  634. foreach ($users_absent as $user_absent) {
  635. $uid = intval($user_absent);
  636. // check if user already was registered with the $calendar_id
  637. $sql = "SELECT user_id FROM $tbl_attendance_sheet
  638. WHERE c_id = $course_id AND user_id='$uid' AND attendance_calendar_id = '$calendar_id'";
  639. $rs = Database::query($sql);
  640. if (Database::num_rows($rs) == 0) {
  641. $sql = "INSERT INTO $tbl_attendance_sheet SET
  642. c_id = $course_id,
  643. user_id ='$uid',
  644. attendance_calendar_id = '$calendar_id',
  645. presence = 0";
  646. Database::query($sql);
  647. $affected_rows += Database::affected_rows();
  648. } else {
  649. $sql = "UPDATE $tbl_attendance_sheet SET presence = 0
  650. WHERE c_id = $course_id AND user_id ='$uid' AND attendance_calendar_id = '$calendar_id'";
  651. Database::query($sql);
  652. $affected_rows += Database::affected_rows();
  653. }
  654. }
  655. // update done_attendance inside attendance calendar table
  656. $sql = "UPDATE $tbl_attendance_calendar SET done_attendance = 1
  657. WHERE c_id = $course_id AND id = '$calendar_id'";
  658. Database::query($sql);
  659. // save users' results
  660. $this->update_users_results($user_ids, $attendance_id);
  661. if ($affected_rows) {
  662. //save attendance sheet log
  663. $lastedit_date = Date('Y-m-d H:i:s');
  664. $lastedit_user_id = api_get_user_id();
  665. $calendar_date_value = $calendar_data['date_time'];
  666. $this->save_attendance_sheet_log(
  667. $attendance_id,
  668. $lastedit_date,
  669. $lastedit_type,
  670. $lastedit_user_id,
  671. $calendar_date_value
  672. );
  673. }
  674. return $affected_rows;
  675. }
  676. /**
  677. * update users' attendance results
  678. * @param array $user_ids registered users inside current course
  679. * @param int $attendance_id
  680. * @return void
  681. */
  682. public function update_users_results($user_ids, $attendance_id)
  683. {
  684. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  685. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  686. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  687. $tbl_attendance = Database::get_course_table(TABLE_ATTENDANCE);
  688. $course_id = api_get_course_int_id();
  689. $attendance_id = intval($attendance_id);
  690. // fill results about presence of students
  691. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  692. $calendar_ids = array();
  693. // get all dates from calendar by current attendance
  694. foreach ($attendance_calendar as $cal) {
  695. $calendar_ids[] = $cal['id'];
  696. }
  697. // get count of presences by users inside current attendance and save like results
  698. $count_presences = 0;
  699. if (count($user_ids) > 0) {
  700. foreach ($user_ids as $uid) {
  701. $count_presences = 0;
  702. if (count($calendar_ids) > 0) {
  703. $sql = "SELECT count(presence) as count_presences
  704. FROM $tbl_attendance_sheet
  705. WHERE
  706. c_id = $course_id AND
  707. user_id = '$uid' AND
  708. attendance_calendar_id IN(".implode(',',$calendar_ids).") AND
  709. presence = 1";
  710. $rs_count = Database::query($sql);
  711. $row_count = Database::fetch_array($rs_count);
  712. $count_presences = $row_count['count_presences'];
  713. }
  714. // save results
  715. $sql = "SELECT id FROM $tbl_attendance_result
  716. WHERE c_id = $course_id AND user_id='$uid' AND attendance_id='$attendance_id'";
  717. $rs_check_result = Database::query($sql);
  718. if (Database::num_rows($rs_check_result) > 0) {
  719. // update result
  720. $sql = "UPDATE $tbl_attendance_result SET
  721. score='$count_presences'
  722. WHERE c_id = $course_id AND user_id='$uid' AND attendance_id='$attendance_id'";
  723. Database::query($sql);
  724. } else {
  725. // insert new result
  726. $sql = "INSERT INTO $tbl_attendance_result SET
  727. c_id = $course_id ,
  728. user_id = '$uid',
  729. attendance_id = '$attendance_id',
  730. score = '$count_presences'";
  731. Database::query($sql);
  732. }
  733. }
  734. }
  735. // update attendance qualify max
  736. $count_done_calendar = self::get_done_attendance_calendar($attendance_id);
  737. $sql = "UPDATE $tbl_attendance SET attendance_qualify_max='$count_done_calendar'
  738. WHERE c_id = $course_id AND id = '$attendance_id'";
  739. Database::query($sql);
  740. }
  741. /**
  742. * update attendance_sheet_log table, is used as history of an attendance sheet
  743. * @param int Attendance id
  744. * @param string Last edit datetime
  745. * @param string Event type ('locked_attendance', 'done_attendance_sheet' ...)
  746. * @param int Last edit user id
  747. * @param string Calendar datetime value (optional, when event type is 'done_attendance_sheet')
  748. * @return int Affected rows
  749. */
  750. public function save_attendance_sheet_log($attendance_id, $lastedit_date, $lastedit_type, $lastedit_user_id, $calendar_date_value = null)
  751. {
  752. $course_id = api_get_course_int_id();
  753. // define table
  754. $tbl_attendance_sheet_log = Database::get_course_table(TABLE_ATTENDANCE_SHEET_LOG);
  755. // protect data
  756. $attendance_id = intval($attendance_id);
  757. $lastedit_date = Database::escape_string($lastedit_date);
  758. $lastedit_type = Database::escape_string($lastedit_type);
  759. $lastedit_user_id = intval($lastedit_user_id);
  760. if (isset($calendar_date_value)) {
  761. $calendar_date_value = Database::escape_string($calendar_date_value);
  762. } else {
  763. $calendar_date_value = '';
  764. }
  765. // save data
  766. $ins = "INSERT INTO $tbl_attendance_sheet_log (c_id, attendance_id, lastedit_date, lastedit_type, lastedit_user_id, calendar_date_value)
  767. VALUES ($course_id, $attendance_id, '$lastedit_date', '$lastedit_type', $lastedit_user_id, '$calendar_date_value')";
  768. Database::query($ins);
  769. return Database::affected_rows();
  770. }
  771. /**
  772. * Get number of done attendances inside current sheet
  773. * @param int attendance id
  774. * @return int number of done attendances
  775. */
  776. public static function get_done_attendance_calendar($attendance_id)
  777. {
  778. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  779. $attendance_id = intval($attendance_id);
  780. $course_id = api_get_course_int_id();
  781. $sql = "SELECT count(done_attendance) as count
  782. FROM $tbl_attendance_calendar
  783. WHERE c_id = $course_id AND attendance_id = '$attendance_id' AND done_attendance=1";
  784. $rs = Database::query($sql);
  785. $row = Database::fetch_array($rs);
  786. $count = $row['count'];
  787. return $count;
  788. }
  789. /**
  790. * Get results of faults (absents) by user
  791. * @param int user id
  792. * @param int attendance id
  793. * @return array results containing number of faults, total done attendance,
  794. * percent of faults and color depend on result (red, orange)
  795. */
  796. public function get_faults_of_user($user_id, $attendance_id)
  797. {
  798. // initializing database table and variables
  799. $user_id = intval($user_id);
  800. $attendance_id = intval($attendance_id);
  801. $results = array();
  802. $attendance_data = $this->get_attendance_by_id($attendance_id);
  803. $calendar_count = self::get_number_of_attendance_calendar($attendance_id);
  804. $total_done_attendance = $attendance_data['attendance_qualify_max'];
  805. $attendance_user_score = $this->get_user_score($user_id, $attendance_id);
  806. //This is the main change of the BT#1381
  807. //$total_done_attendance = $calendar_count;
  808. // calculate results
  809. $faults = $total_done_attendance - $attendance_user_score;
  810. $faults = $faults > 0 ? $faults:0;
  811. $faults_porcent = $calendar_count > 0 ?round(($faults*100)/$calendar_count,0):0;
  812. $results['faults'] = $faults;
  813. $results['total'] = $calendar_count;
  814. $results['faults_porcent'] = $faults_porcent;
  815. $color_bar = '';
  816. if ($faults_porcent > 25 ) {
  817. $color_bar = '#f28989';
  818. } else if ($faults_porcent > 10) {
  819. $color_bar = '#F90';
  820. }
  821. $results['color_bar'] = $color_bar;
  822. return $results;
  823. }
  824. /**
  825. * Get results of faults average for all courses by user
  826. * @param int $user_id
  827. * @return array results containing number of faults, total done attendance,
  828. * percentage of faults and color depend on result (red, orange)
  829. */
  830. public function get_faults_average_inside_courses($user_id)
  831. {
  832. // get all courses of current user
  833. $courses = CourseManager::get_courses_list_by_user_id($user_id, true);
  834. $user_id = intval($user_id);
  835. $results = array();
  836. $total_faults = $total_weight = $porcent = 0;
  837. foreach ($courses as $course) {
  838. //$course_code = $course['code'];
  839. //$course_info = api_get_course_info($course_code);
  840. $course_id = $course['real_id'];
  841. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  842. $attendances_by_course = $this->get_attendances_list($course_id);
  843. foreach ($attendances_by_course as $attendance) {
  844. // get total faults and total weight
  845. $total_done_attendance = $attendance['attendance_qualify_max'];
  846. $sql = "SELECT score
  847. FROM $tbl_attendance_result
  848. WHERE
  849. c_id = $course_id AND
  850. user_id = $user_id AND
  851. attendance_id = ".$attendance['id'];
  852. $rs = Database::query($sql);
  853. $score = 0;
  854. if (Database::num_rows($rs) > 0) {
  855. $row = Database::fetch_array($rs);
  856. $score = $row['score'];
  857. }
  858. $faults = $total_done_attendance-$score;
  859. $faults = $faults > 0 ? $faults:0;
  860. $total_faults += $faults;
  861. $total_weight += $total_done_attendance;
  862. }
  863. }
  864. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  865. $results['faults'] = $total_faults;
  866. $results['total'] = $total_weight;
  867. $results['porcent'] = $porcent;
  868. return $results;
  869. }
  870. /**
  871. * Get results of faults average by course
  872. * @param int $user_id
  873. * @param string $course_code
  874. * @param int Session id (optional)
  875. * @return array results containing number of faults,
  876. * total done attendance, porcent of faults and color depend on result (red, orange)
  877. */
  878. public function get_faults_average_by_course($user_id, $course_code, $session_id = null)
  879. {
  880. // Database tables and variables
  881. $course_info = api_get_course_info($course_code);
  882. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  883. $user_id = intval($user_id);
  884. $results = array();
  885. $total_faults = $total_weight = $porcent = 0;
  886. $attendances_by_course = $this->get_attendances_list($course_info['real_id'], $session_id);
  887. foreach ($attendances_by_course as $attendance) {
  888. // Get total faults and total weight
  889. $total_done_attendance = $attendance['attendance_qualify_max'];
  890. $sql = "SELECT score FROM $tbl_attendance_result
  891. WHERE
  892. c_id = {$course_info['real_id']} AND
  893. user_id = $user_id AND
  894. attendance_id=".$attendance['id'];
  895. $rs = Database::query($sql);
  896. $score = 0;
  897. if (Database::num_rows($rs) > 0) {
  898. $row = Database::fetch_array($rs);
  899. $score = $row['score'];
  900. }
  901. $faults = $total_done_attendance-$score;
  902. $faults = $faults > 0 ? $faults:0;
  903. $total_faults += $faults;
  904. $total_weight += $total_done_attendance;
  905. }
  906. $porcent = $total_weight > 0 ?round(($total_faults*100)/$total_weight,0):0;
  907. $results['faults'] = $total_faults;
  908. $results['total'] = $total_weight;
  909. $results['porcent'] = $porcent;
  910. return $results;
  911. }
  912. /**
  913. * Get registered users' attendance sheet inside current course
  914. * @param int $attendance_id
  915. * @param int $user_id for showing data for only one user (optional)
  916. * @return array users attendance sheet data
  917. */
  918. public function get_users_attendance_sheet($attendance_id, $user_id = 0)
  919. {
  920. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  921. $tbl_attendance_calendar= Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  922. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  923. $calendar_ids = array();
  924. // get all dates from calendar by current attendance
  925. foreach ($attendance_calendar as $cal) {
  926. $calendar_ids[] = $cal['id'];
  927. }
  928. $course_id = api_get_course_int_id();
  929. $data = array();
  930. if (empty($user_id)) {
  931. // get all registered users inside current course
  932. $users = $this->get_users_rel_course();
  933. $user_ids = array_keys($users);
  934. if (count($calendar_ids) > 0 && count($user_ids) > 0) {
  935. foreach ($user_ids as $uid) {
  936. $sql = "SELECT * FROM $tbl_attendance_sheet
  937. WHERE
  938. c_id = $course_id AND
  939. user_id = '$uid' AND
  940. attendance_calendar_id IN(".implode(',',$calendar_ids).")
  941. ";
  942. $res = Database::query($sql);
  943. if (Database::num_rows($res) > 0) {
  944. while ($row = Database::fetch_array($res)) {
  945. $data[$uid][$row['attendance_calendar_id']]['presence'] = $row['presence'];
  946. }
  947. }
  948. }
  949. }
  950. } else {
  951. // Get attendance for current user
  952. $user_id = intval($user_id);
  953. if (count($calendar_ids) > 0) {
  954. $sql = "SELECT cal.date_time, att.presence
  955. FROM $tbl_attendance_sheet att
  956. INNER JOIN $tbl_attendance_calendar cal
  957. ON cal.id = att.attendance_calendar_id
  958. WHERE
  959. att.c_id = $course_id AND
  960. cal.c_id = $course_id AND
  961. att.user_id = '$user_id' AND
  962. att.attendance_calendar_id IN (".implode(',',$calendar_ids).")
  963. ORDER BY date_time";
  964. $res = Database::query($sql);
  965. if (Database::num_rows($res) > 0) {
  966. while ($row = Database::fetch_array($res)) {
  967. $row['date_time'] = api_convert_and_format_date($row['date_time'], null, date_default_timezone_get());
  968. $data[$user_id][] = $row;
  969. }
  970. }
  971. }
  972. }
  973. return $data;
  974. }
  975. /**
  976. * Get next attendance calendar without presences (done attendances)
  977. * @param int attendance id
  978. * @return int attendance calendar id
  979. */
  980. public function get_next_attendance_calendar_id($attendance_id)
  981. {
  982. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  983. $attendance_id = intval($attendance_id);
  984. $course_id = api_get_course_int_id();
  985. $sql = "SELECT id FROM $tbl_attendance_calendar
  986. WHERE
  987. c_id = $course_id AND
  988. attendance_id = '$attendance_id' AND
  989. done_attendance = 0
  990. ORDER BY date_time
  991. LIMIT 1";
  992. $rs = Database::query($sql);
  993. $next_calendar_id = 0;
  994. if (Database::num_rows($rs) > 0) {
  995. $row = Database::fetch_array($rs);
  996. $next_calendar_id = $row['id'];
  997. }
  998. return $next_calendar_id;
  999. }
  1000. /**
  1001. * Get next attendance calendar datetime without presences (done attendances)
  1002. * @param int attendance id
  1003. * @return int UNIX time format datetime
  1004. */
  1005. public function get_next_attendance_calendar_datetime($attendance_id)
  1006. {
  1007. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1008. $course_id = api_get_course_int_id();
  1009. $attendance_id = intval($attendance_id);
  1010. $sql = "SELECT id, date_time FROM $tbl_attendance_calendar
  1011. WHERE
  1012. c_id = $course_id AND
  1013. attendance_id = '$attendance_id' AND
  1014. done_attendance = 0
  1015. ORDER BY date_time
  1016. LIMIT 1";
  1017. $rs = Database::query($sql);
  1018. $next_calendar_datetime = 0;
  1019. if (Database::num_rows($rs) > 0) {
  1020. $row = Database::fetch_array($rs);
  1021. $next_calendar_datetime = api_get_local_time($row['date_time']);
  1022. }
  1023. return $next_calendar_datetime;
  1024. }
  1025. /**
  1026. * Get user' score from current attendance
  1027. * @param int $user_id
  1028. * @param int $attendance_id
  1029. * @return int score
  1030. */
  1031. public function get_user_score($user_id, $attendance_id)
  1032. {
  1033. $tbl_attendance_result = Database::get_course_table(TABLE_ATTENDANCE_RESULT);
  1034. $user_id = intval($user_id);
  1035. $attendance_id = intval($attendance_id);
  1036. $course_id = api_get_course_int_id();
  1037. $sql = "SELECT score FROM $tbl_attendance_result
  1038. WHERE
  1039. c_id = $course_id AND
  1040. user_id='$user_id' AND
  1041. attendance_id='$attendance_id'";
  1042. $rs = Database::query($sql);
  1043. $score = 0;
  1044. if (Database::num_rows($rs) > 0) {
  1045. $row = Database::fetch_array($rs);
  1046. $score = $row['score'];
  1047. }
  1048. return $score;
  1049. }
  1050. /**
  1051. * Get attendance calendar data by id
  1052. * @param int attendance calendar id
  1053. * @return array attendance calendar data
  1054. */
  1055. public function get_attendance_calendar_by_id($calendar_id)
  1056. {
  1057. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1058. $calendar_id = intval($calendar_id);
  1059. $course_id = api_get_course_int_id();
  1060. $sql = "SELECT * FROM $tbl_attendance_calendar
  1061. WHERE c_id = $course_id AND id = '$calendar_id' ";
  1062. $rs = Database::query($sql);
  1063. $data = array();
  1064. if (Database::num_rows($rs) > 0) {
  1065. while ($row = Database::fetch_array($rs)) {
  1066. $row['date_time'] = api_get_local_time($row['date_time']);
  1067. $data = $row;
  1068. }
  1069. }
  1070. return $data;
  1071. }
  1072. /**
  1073. * Get all attendance calendar data inside current attendance
  1074. * @param int attendance id
  1075. * @return array attendance calendar data
  1076. */
  1077. public function get_attendance_calendar($attendance_id, $type = 'all', $calendar_id = null)
  1078. {
  1079. global $dateFormatShort, $timeNoSecFormat;
  1080. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1081. $attendance_id = intval($attendance_id);
  1082. $course_id = api_get_course_int_id();
  1083. $sql = "SELECT * FROM $tbl_attendance_calendar
  1084. WHERE c_id = $course_id AND attendance_id = '$attendance_id' ";
  1085. if (!in_array($type, array('today', 'all', 'all_done', 'all_not_done','calendar_id'))) {
  1086. $type = 'all';
  1087. }
  1088. switch ($type) {
  1089. case 'calendar_id':
  1090. $calendar_id = intval($calendar_id);
  1091. if (!empty($calendar_id)) {
  1092. $sql.= " AND id = $calendar_id";
  1093. }
  1094. break;
  1095. case 'today':
  1096. //$sql .= ' AND DATE_FORMAT(date_time,"%d-%m-%Y") = DATE_FORMAT("'.api_get_utc_datetime().'", "%d-%m-%Y" )';
  1097. break;
  1098. case 'all_done':
  1099. $sql .= " AND done_attendance = 1 ";
  1100. break;
  1101. case 'all_not_done':
  1102. $sql .= " AND done_attendance = 0 ";
  1103. break;
  1104. case 'all':
  1105. default:
  1106. break;
  1107. }
  1108. $sql .= " ORDER BY date_time ";
  1109. $rs = Database::query($sql);
  1110. $data = array();
  1111. if (Database::num_rows($rs) > 0) {
  1112. while ($row = Database::fetch_array($rs,'ASSOC')) {
  1113. $row['db_date_time'] = $row['date_time'];
  1114. $row['date_time'] = api_get_local_time($row['date_time']);
  1115. $row['date'] = api_format_date($row['date_time'], DATE_FORMAT_SHORT);
  1116. $row['time'] = api_format_date($row['date_time'], TIME_NO_SEC_FORMAT);
  1117. if ($type == 'today') {
  1118. if (date('d-m-Y', api_strtotime($row['date_time'], 'UTC')) == date('d-m-Y', time())) {
  1119. $data[] = $row;
  1120. }
  1121. } else {
  1122. $data[] = $row;
  1123. }
  1124. }
  1125. }
  1126. return $data;
  1127. }
  1128. /**
  1129. * Get number of attendance calendar inside current attendance
  1130. * @param int $attendance_id
  1131. * @return int number of dates in attendance calendar
  1132. */
  1133. public static function get_number_of_attendance_calendar($attendance_id)
  1134. {
  1135. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1136. $attendance_id = intval($attendance_id);
  1137. $course_id = api_get_course_int_id();
  1138. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1139. WHERE c_id = $course_id AND attendance_id = '$attendance_id'";
  1140. $rs = Database::query($sql);
  1141. $row = Database::fetch_row($rs);
  1142. $count = $row[0];
  1143. return $count;
  1144. }
  1145. /**
  1146. * Get count dates inside attendance calendar by attendance id
  1147. * @param int $attendance_id
  1148. * @return int count of dates
  1149. */
  1150. public static function get_count_dates_inside_attendance_calendar($attendance_id)
  1151. {
  1152. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1153. $attendance_id = intval($attendance_id);
  1154. $course_id = api_get_course_int_id();
  1155. $sql = "SELECT count(id) FROM $tbl_attendance_calendar
  1156. WHERE
  1157. c_id = $course_id AND
  1158. attendance_id = '$attendance_id'";
  1159. $rs = Database::query($sql);
  1160. $count = 0;
  1161. if (Database::num_rows($rs) > 0) {
  1162. $row = Database::fetch_row($rs);
  1163. $count = $row[0];
  1164. }
  1165. return $count;
  1166. }
  1167. /**
  1168. * check if all calendar of an attendance is done
  1169. * @param int $attendance_id
  1170. * @return bool True if all calendar is done, otherwise false
  1171. */
  1172. public static function is_all_attendance_calendar_done($attendance_id)
  1173. {
  1174. $attendance_id = intval($attendance_id);
  1175. $done_calendar = self::get_done_attendance_calendar($attendance_id);
  1176. $count_dates_in_calendar = self::get_count_dates_inside_attendance_calendar($attendance_id);
  1177. $number_of_dates = self::get_number_of_attendance_calendar($attendance_id);
  1178. $result = false;
  1179. if ($number_of_dates && (intval($count_dates_in_calendar) == intval($done_calendar))) {
  1180. $result = true;
  1181. }
  1182. return $result;
  1183. }
  1184. /**
  1185. * check if an attendance is locked
  1186. * @param int $attendance_id
  1187. * @param bool
  1188. */
  1189. public static function is_locked_attendance($attendance_id)
  1190. {
  1191. //use gradebook lock
  1192. $result = api_resource_is_locked_by_gradebook($attendance_id, LINK_ATTENDANCE);
  1193. return $result;
  1194. }
  1195. /**
  1196. * Add new datetime inside attendance calendar table
  1197. * @param int attendance id
  1198. * @return int affected rows
  1199. */
  1200. public function attendance_calendar_add($attendance_id)
  1201. {
  1202. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1203. $affected_rows = 0;
  1204. $attendance_id = intval($attendance_id);
  1205. $course_id = api_get_course_int_id();
  1206. // check if datetime already exists inside the table
  1207. $sql = "SELECT id FROM $tbl_attendance_calendar
  1208. WHERE
  1209. c_id = $course_id AND
  1210. date_time='".Database::escape_string($this->date_time)."' AND
  1211. attendance_id = '$attendance_id'";
  1212. $rs = Database::query($sql);
  1213. if (Database::num_rows($rs) == 0) {
  1214. $sql = "INSERT INTO $tbl_attendance_calendar SET
  1215. c_id = $course_id,
  1216. date_time = '".Database::escape_string($this->date_time)."',
  1217. attendance_id = '$attendance_id'";
  1218. Database::query($sql);
  1219. $affected_rows = Database::affected_rows();
  1220. }
  1221. // update locked attendance
  1222. $is_all_calendar_done = self::is_all_attendance_calendar_done($attendance_id);
  1223. if (!$is_all_calendar_done) {
  1224. self::lock_attendance($attendance_id, false);
  1225. } else {
  1226. self::lock_attendance($attendance_id);
  1227. }
  1228. return $affected_rows;
  1229. }
  1230. /**
  1231. * save repeated date inside attendance calendar table
  1232. * @param int $attendance_id
  1233. * @param int $start_date start date in tms
  1234. * @param int $end_date end date in tms
  1235. * @param string $repeat_type daily, weekly, monthlyByDate
  1236. */
  1237. public function attendance_repeat_calendar_add($attendance_id, $start_date, $end_date, $repeat_type)
  1238. {
  1239. $attendance_id = intval($attendance_id);
  1240. // save start date
  1241. $datetimezone = api_get_utc_datetime($start_date);
  1242. $this->set_date_time($datetimezone);
  1243. $res = $this->attendance_calendar_add($attendance_id);
  1244. //86400 = 24 hours in seconds
  1245. //604800 = 1 week in seconds
  1246. // Saves repeated dates
  1247. switch($repeat_type) {
  1248. case 'daily':
  1249. $j = 1;
  1250. for ($i = $start_date + 86400; ($i <= $end_date); $i += 86400) {
  1251. $datetimezone = api_get_utc_datetime($i);
  1252. $this->set_date_time($datetimezone);
  1253. $this->attendance_calendar_add($attendance_id);
  1254. $j++;
  1255. }
  1256. break;
  1257. case 'weekly':
  1258. $j = 1;
  1259. for ($i = $start_date + 604800; ($i <= $end_date); $i += 604800) {
  1260. $datetimezone = api_get_utc_datetime($i);
  1261. $this->set_date_time($datetimezone);
  1262. $this->attendance_calendar_add($attendance_id);
  1263. $j++;
  1264. }
  1265. break;
  1266. case 'monthlyByDate':
  1267. $j = 1;
  1268. //@todo fix bug with february
  1269. for ($i = $start_date + 2419200; ($i <= $end_date); $i += 2419200) {
  1270. $datetimezone = api_get_utc_datetime($i);
  1271. $this->set_date_time($datetimezone);
  1272. $this->attendance_calendar_add($attendance_id);
  1273. $j++;
  1274. }
  1275. break;
  1276. }
  1277. }
  1278. /**
  1279. * Adds x months to a UNIX timestamp
  1280. * @param int The timestamp
  1281. * @param int The number of years to add
  1282. * @return int The new timestamp
  1283. */
  1284. private function add_month($timestamp, $num=1)
  1285. {
  1286. $values = api_get_utc_datetime($timestamp);
  1287. $values = str_replace(array(':','-',' '), '/', $values);
  1288. list($y, $m, $d, $h, $n, $s) = split('/',$values);
  1289. if($m+$num>12) {
  1290. $y += floor($num/12);
  1291. $m += $num%12;
  1292. } else {
  1293. $m += $num;
  1294. }
  1295. //date_default_timezone_set('UTC');
  1296. // return mktime($h, $n, $s, $m, $d, $y);
  1297. $result = api_strtotime($y.'-'.$m.'-'.$d.' '.$h.':'.$n.':'.$s, 'UTC');
  1298. if (!empty($result)) {
  1299. return $result;
  1300. }
  1301. return false;
  1302. }
  1303. /**
  1304. * edit a datetime inside attendance calendar table
  1305. * @param int attendance calendar id
  1306. * @param int attendance id
  1307. * @return int affected rows
  1308. */
  1309. public function attendance_calendar_edit($calendar_id, $attendance_id)
  1310. {
  1311. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1312. $affected_rows = 0;
  1313. $attendance_id = intval($attendance_id);
  1314. $course_id = api_get_course_int_id();
  1315. // check if datetime already exists inside the table
  1316. $sql = "SELECT id FROM $tbl_attendance_calendar
  1317. WHERE
  1318. c_id = $course_id AND
  1319. date_time = '".Database::escape_string($this->date_time)."' AND
  1320. attendance_id = '$attendance_id'";
  1321. $rs = Database::query($sql);
  1322. if (Database::num_rows($rs) == 0) {
  1323. $sql = "UPDATE $tbl_attendance_calendar
  1324. SET date_time='".Database::escape_string($this->date_time)."'
  1325. WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1326. Database::query($sql);
  1327. $affected_rows = Database::affected_rows();
  1328. }
  1329. // update locked attendance
  1330. $is_all_calendar_done = self::is_all_attendance_calendar_done($attendance_id);
  1331. if (!$is_all_calendar_done) {
  1332. self::lock_attendance($attendance_id, false);
  1333. } else {
  1334. self::lock_attendance($attendance_id);
  1335. }
  1336. return $affected_rows;
  1337. }
  1338. /**
  1339. * delete a datetime from attendance calendar table
  1340. * @param int attendance calendar id
  1341. * @param int attendance id
  1342. * @param bool true for removing all calendar inside current attendance, false for removing by calendar id
  1343. * @return int affected rows
  1344. */
  1345. public function attendance_calendar_delete($calendar_id, $attendance_id , $all_delete = false)
  1346. {
  1347. $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1348. $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET);
  1349. $attendance_id = intval($attendance_id);
  1350. // get all registered users inside current course
  1351. $users = $this->get_users_rel_course();
  1352. $user_ids = array_keys($users);
  1353. $course_id = api_get_course_int_id();
  1354. if ($all_delete) {
  1355. $attendance_calendar = $this->get_attendance_calendar($attendance_id);
  1356. // get all dates from calendar by current attendance
  1357. if (!empty($attendance_calendar)) {
  1358. foreach ($attendance_calendar as $cal) {
  1359. // delete all data from attendance sheet
  1360. $sql = "DELETE FROM $tbl_attendance_sheet
  1361. WHERE c_id = $course_id AND attendance_calendar_id = '".intval($cal['id'])."'";
  1362. Database::query($sql);
  1363. // delete data from attendance calendar
  1364. $sql = "DELETE FROM $tbl_attendance_calendar
  1365. WHERE c_id = $course_id AND id = '".intval($cal['id'])."'";
  1366. Database::query($sql);
  1367. }
  1368. }
  1369. } else {
  1370. // delete just one row from attendance sheet by the calendar id
  1371. $sql = "DELETE FROM $tbl_attendance_sheet
  1372. WHERE c_id = $course_id AND attendance_calendar_id = '".intval($calendar_id)."'";
  1373. Database::query($sql);
  1374. // delete data from attendance calendar
  1375. $sql = "DELETE FROM $tbl_attendance_calendar
  1376. WHERE c_id = $course_id AND id = '".intval($calendar_id)."'";
  1377. Database::query($sql);
  1378. }
  1379. $affected_rows = Database::affected_rows();
  1380. // update users' results
  1381. $this->update_users_results($user_ids, $attendance_id);
  1382. return $affected_rows;
  1383. }
  1384. /**
  1385. * buid a string datetime from array
  1386. * @param array array containing data e.g:
  1387. * $array('Y'=>'2010', 'F' => '02', 'd' => '10', 'H' => '12', 'i' => '30')
  1388. * @return string date and time e.g: '2010-02-10 12:30:00'
  1389. */
  1390. public function build_datetime_from_array($array)
  1391. {
  1392. return return_datetime_from_array($array);
  1393. }
  1394. /** Setters for fields of attendances tables **/
  1395. public function set_session_id($session_id)
  1396. {
  1397. $this->session_id = $session_id;
  1398. }
  1399. public function set_course_id($course_id)
  1400. {
  1401. $this->course_id = $course_id;
  1402. }
  1403. public function set_date_time($datetime)
  1404. {
  1405. $this->date_time = $datetime;
  1406. }
  1407. public function set_name($name)
  1408. {
  1409. $this->name = $name;
  1410. }
  1411. public function set_description($description)
  1412. {
  1413. $this->description = $description;
  1414. }
  1415. public function set_attendance_qualify_title($attendance_qualify_title)
  1416. {
  1417. $this->attendance_qualify_title = $attendance_qualify_title;
  1418. }
  1419. public function set_attendance_weight($attendance_weight)
  1420. {
  1421. $this->attendance_weight = $attendance_weight;
  1422. }
  1423. /** Getters for fields of attendances tables **/
  1424. public function get_session_id()
  1425. {
  1426. return $this->session_id;
  1427. }
  1428. public function get_course_id()
  1429. {
  1430. return $this->course_id;
  1431. }
  1432. public function get_date_time()
  1433. {
  1434. return $this->date_time;
  1435. }
  1436. public function get_name()
  1437. {
  1438. return $this->name;
  1439. }
  1440. public function get_description()
  1441. {
  1442. return $this->description;
  1443. }
  1444. public function get_attendance_qualify_title()
  1445. {
  1446. return $this->attendance_qualify_title;
  1447. }
  1448. public function get_attendance_weight()
  1449. {
  1450. return $this->attendance_weight;
  1451. }
  1452. /**
  1453. * @param string $startDate in UTC time
  1454. * @param string $endDate in UTC time
  1455. *
  1456. * @return array
  1457. */
  1458. public function getAttendanceLogin($startDate, $endDate)
  1459. {
  1460. if (empty($startDate) || $startDate == '0000-00-00' ||
  1461. empty($endDate) || $endDate == '0000-00-00'
  1462. ) {
  1463. return false;
  1464. }
  1465. $sessionId = api_get_session_id();
  1466. $courseCode = api_get_course_id();
  1467. if (!empty($sessionId)) {
  1468. $users = CourseManager:: get_user_list_from_course_code(
  1469. $courseCode,
  1470. $sessionId,
  1471. '',
  1472. 'lastname',
  1473. 0
  1474. );
  1475. } else {
  1476. $users = CourseManager:: get_user_list_from_course_code(
  1477. $courseCode,
  1478. 0,
  1479. '',
  1480. 'lastname',
  1481. STUDENT
  1482. );
  1483. }
  1484. $dateTimeStartOriginal = new DateTime($startDate);
  1485. $dateTimeStart = new DateTime($startDate);
  1486. $dateTimeEnd= new DateTime($endDate);
  1487. $interval = $dateTimeStart->diff($dateTimeEnd);
  1488. $days = intval($interval->format('%a'));
  1489. $dateList = array($dateTimeStart->format('Y-m-d'));
  1490. $headers = array(
  1491. get_lang('User'),
  1492. $dateTimeStart->format('Y-m-d')
  1493. );
  1494. for ($i = 0; $i < $days; $i++) {
  1495. $dateTimeStart = $dateTimeStart->add(new DateInterval('P1D'));
  1496. $date = $dateTimeStart->format('Y-m-d');
  1497. $dateList[] = $date;
  1498. $headers[] = $date;
  1499. }
  1500. $accessData = CourseManager::getCourseAccessPerCourseAndSession(
  1501. $courseCode,
  1502. $sessionId,
  1503. $dateTimeStartOriginal->format('Y-m-d H:i:s'),
  1504. $dateTimeEnd->format('Y-m-d H:i:s')
  1505. );
  1506. $results = array();
  1507. if (!empty($accessData)) {
  1508. foreach ($accessData as $data) {
  1509. $onlyDate = substr($data['login_course_date'], 0, 10);
  1510. $results[$data['user_id']][$onlyDate] = true;
  1511. }
  1512. }
  1513. return array(
  1514. 'users' => $users,
  1515. 'dateList' => $dateList,
  1516. 'headers' => $headers,
  1517. 'results' => $results
  1518. );
  1519. }
  1520. /**
  1521. * @param string $startDate in UTC time
  1522. * @param string $endDate in UTC time
  1523. *
  1524. * @return string
  1525. */
  1526. public function getAttendanceLoginTable($startDate, $endDate)
  1527. {
  1528. $data = $this->getAttendanceLogin($startDate, $endDate);
  1529. if (!$data) {
  1530. return null;
  1531. }
  1532. $headers = $data['headers'];
  1533. $dateList = $data['dateList'];
  1534. $users = $data['users'];
  1535. $results = $data['results'];
  1536. $table = new HTML_Table(array('class' => 'data_table'));
  1537. $row = 0;
  1538. $column = 0;
  1539. foreach ($headers as $header) {
  1540. $table->setHeaderContents($row, $column, $header);
  1541. $column++;
  1542. }
  1543. $row = 1;
  1544. foreach ($users as $user) {
  1545. $table->setCellContents(
  1546. $row,
  1547. 0,
  1548. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'
  1549. );
  1550. $row ++;
  1551. }
  1552. $column = 1;
  1553. $row = 1;
  1554. foreach ($users as $user) {
  1555. foreach ($dateList as $date) {
  1556. $status = null;
  1557. if (isset($results[$user['user_id']]) &&
  1558. isset($results[$user['user_id']][$date])
  1559. ) {
  1560. $status = 'X';
  1561. }
  1562. $table->setCellContents($row, $column, $status);
  1563. $column++;
  1564. }
  1565. $row++;
  1566. $column = 1;
  1567. }
  1568. return $table->toHtml();
  1569. }
  1570. /**
  1571. * @param string $startDate in UTC time
  1572. * @param string $endDate in UTC time
  1573. *
  1574. * @return string
  1575. */
  1576. public function exportAttendanceLogin($startDate, $endDate)
  1577. {
  1578. $data = $this->getAttendanceLogin($startDate, $endDate);
  1579. if (!$data) {
  1580. return null;
  1581. }
  1582. $users = $data['users'];
  1583. $results = $data['results'];
  1584. $table = new HTML_Table(array('class' => 'data_table'));
  1585. $table->setHeaderContents(0, 0, get_lang('User'));
  1586. $table->setHeaderContents(0, 1, get_lang('Date'));
  1587. $row = 1;
  1588. foreach ($users as $user) {
  1589. $table->setCellContents(
  1590. $row,
  1591. 0,
  1592. $user['lastname'].' '.$user['firstname'].' ('.$user['username'].')'
  1593. );
  1594. $row++;
  1595. }
  1596. $table->setColAttributes(0, array('style' => 'width:28%'));
  1597. $row = 1;
  1598. foreach ($users as $user) {
  1599. if (isset($results[$user['user_id']]) &&
  1600. !empty($results[$user['user_id']])
  1601. ) {
  1602. $dates = implode(', ', array_keys($results[$user['user_id']]));
  1603. $table->setCellContents($row, 1, $dates);
  1604. }
  1605. $row++;
  1606. }
  1607. //$tableToString = null;
  1608. //$sessionInfo = api_get_session_info(api_get_session_id());
  1609. //if (!empty($sessionInfo)) {
  1610. /*$tableToString .= '<strong>'.get_lang('PeriodToDisplay').'</strong>: '.
  1611. sprintf(get_lang('FromDateXToDateY'), $startDate, $endDate);*/
  1612. //}
  1613. $tableToString = $table->toHtml();
  1614. $params = array(
  1615. 'filename' => get_lang('Attendance') . '_' . api_get_utc_datetime(),
  1616. 'pdf_title' => get_lang('Attendance'),
  1617. 'course_code' => api_get_course_id(),
  1618. 'show_real_course_teachers' => true
  1619. );
  1620. $pdf = new PDF('A4', null, $params);
  1621. $pdf->html_to_pdf_with_template($tableToString);
  1622. }
  1623. }