diff --git a/main/inc/lib/attendance.lib.php b/main/inc/lib/attendance.lib.php index 863a5e17f3..e2a0f1342d 100755 --- a/main/inc/lib/attendance.lib.php +++ b/main/inc/lib/attendance.lib.php @@ -1216,26 +1216,38 @@ class Attendance } /** - * Get registered users' attendance sheet inside current course. + * Get registered users' attendance sheet inside current course or course by id. * - * @param int $attendanceId - * @param int $user_id for showing data for only one user (optional) - * @param int $groupId + * @param int $attendanceId + * @param int $user_id for showing data for only one user (optional) + * @param int $groupId + * @param int $course_id if id = 0 get the current course + * @param DateTime $startDate Filter atttendance sheet with a start date + * @param DateTime $endDate Filter atttendance sheet with a end date * * @return array users attendance sheet data */ public function get_users_attendance_sheet( $attendanceId, $user_id = 0, - $groupId = 0 + $groupId = 0, + $course_id = 0, + DateTime $startDate = null, + DateTime $endDate = null ) { + //Get actual course or by course_id + $course_id = (0 == $course_id) ? api_get_course_int_id() : $course_id; $tbl_attendance_sheet = Database::get_course_table(TABLE_ATTENDANCE_SHEET); $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR); $attendance_calendar = $this->get_attendance_calendar( $attendanceId, 'all', null, - $groupId + $groupId, + true, + $course_id, + $startDate, + $endDate ); $calendar_ids = []; // get all dates from calendar by current attendance @@ -1243,7 +1255,16 @@ class Attendance $calendar_ids[] = $cal['id']; } - $course_id = api_get_course_int_id(); + $whereDate = ''; + if (!empty($startDate)) { + $whereDate .= " AND cal.date_time >= '".$startDate->format('Y-m-d H:i:s')."'"; + } + if (!empty($endDate)) { + $whereDate .= " AND cal.date_time <= '".$endDate->format('Y-m-d H:i:s')."'"; + } + + // moved at start of this function + // $course_id = api_get_course_int_id(); $data = []; if (empty($user_id)) { @@ -1280,6 +1301,7 @@ class Attendance cal.c_id = $course_id AND att.user_id = '$user_id' AND att.attendance_calendar_id IN (".implode(',', $calendar_ids).") + $whereDate ORDER BY date_time"; $res = Database::query($sql); if (Database::num_rows($res) > 0) { @@ -1433,13 +1455,16 @@ class Attendance } /** - * Get all attendance calendar data inside current attendance. + * Get all attendance calendar data inside current attendance or by course id. * - * @param int $attendanceId - * @param string $type - * @param int $calendar_id - * @param int $groupId - * @param bool $showAll = false show group calendar items or not + * @param int $attendanceId + * @param string $type + * @param int $calendar_id + * @param int $groupId + * @param bool $showAll = false show group calendar items or not + * @param int $course_id + * @param DateTime $startDate Filter calendar with a start date + * @param DateTime $endDate Filter calendar with a end date * * @return array attendance calendar data */ @@ -1448,16 +1473,27 @@ class Attendance $type = 'all', $calendar_id = null, $groupId = 0, - $showAll = false + $showAll = false, + $course_id = 0, + DateTime $startDate = null, + DateTime $endDate = null ) { $tbl_attendance_calendar = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR); $tbl_acrg = Database::get_course_table(TABLE_ATTENDANCE_CALENDAR_REL_GROUP); $attendanceId = intval($attendanceId); - $course_id = api_get_course_int_id(); - + $course_id = (0 == $course_id) ? api_get_course_int_id() : $course_id; + $whereDate = ''; + if (!empty($startDate)) { + $whereDate .= " AND c.date_time >= '".$startDate->format('Y-m-d H:i:s')."'"; + } + if (!empty($endDate)) { + $whereDate .= " AND c.date_time <= '".$endDate->format('Y-m-d H:i:s')."'"; + } if ($showAll) { $sql = "SELECT * FROM $tbl_attendance_calendar c - WHERE c_id = $course_id AND attendance_id = '$attendanceId'"; + WHERE c_id = $course_id + AND attendance_id = '$attendanceId' + $whereDate"; } else { $sql = "SELECT * FROM $tbl_attendance_calendar c WHERE @@ -1467,6 +1503,7 @@ class Attendance SELECT calendar_id FROM $tbl_acrg WHERE c_id = $course_id AND group_id != 0 AND group_id IS NOT NULL ) + $whereDate "; } @@ -2256,4 +2293,207 @@ class Attendance $pdf = new PDF('A4', null, $params); $pdf->html_to_pdf_with_template($tableToString); } + + /** + * Return all course for a student between dates and order by key date (Y-m-d). + * + * @param int $studentId + * @param \DateTime $startDate + * @param \DateTime $endDate + * @param bool $orderDesc + * + * @return array + */ + public function getCoursesWithAttendance( + $studentId = 0, + DateTime $startDate = null, + DateTime $endDate = null, + $orderDesc = false + ) { + // Lang variables + $presentString = get_lang('Present'); + //$absentString = get_lang('Absent'); + $absentString = '-'; + // Lang variables + $attendanceLib = new Attendance(); + $data = []; + /* + $specialCourses = CourseManager::returnSpecialCourses( + $studentId, false, false + ); + */ + $courses = CourseManager::returnCourses( + $studentId, + false, + false + ); + /* Get course with (in_category) and without (not_category) category */ + foreach ($courses as $coursesKey => $courseData) { + /* + * $coursesKey can be in_category or not_category for courses + * */ + $totalCoursesNoCategory = count($courseData); + for ($i = 0; $i < $totalCoursesNoCategory; $i++) { + $courseItem = $courseData[$i]; + $courseId = $courseItem['course_id']; + + /* Get all attendance by courses*/ + $attenances = $attendanceLib->get_attendances_list($courseId); + $temp = []; + $sheetsProccessed = []; + $tempDate = []; + foreach ($attenances as $attendanceData) { + $attendanceId = $attendanceData['id']; + $sheets = $attendanceLib->get_users_attendance_sheet( + $attendanceId, + $studentId, + 0, + $courseId, + $startDate, + $endDate + ); + + $sheetsProccessed[] = []; + foreach ($sheets as $sheetData) { + $totalb = count($sheetData); + $tempDate = []; + for ($ii = 0; $ii < $totalb; $ii++) { + $attendancesProccess = $sheetData[$ii]; + if (!empty($attendancesProccess)) { + $dateTemp = $attendancesProccess['0']; + $attendancesProccess[0] = $attendancesProccess[1]; + $attendancesProccess[1] = $dateTemp; + + $attendancesProccess[2] = $courseItem['title']; + $attendancesProccess['courseTitle'] = $courseItem['title']; + + $attendancesProccess[3] = $courseItem['real_id']; + $attendancesProccess['courseId'] = $courseItem['real_id']; + + $attendancesProccess[4] = $attendanceData['name']; + $attendancesProccess['attendanceName'] = $attendanceData['name']; + $attendancesProccess['courseCode'] = $courseItem['course_code']; + + $attendancesProccess[5] = $attendanceData['id']; + $attendancesProccess['attendanceId'] = $attendanceData['id']; + if ($attendancesProccess['presence'] == 1) { + $attendancesProccess['presence'] = $presentString; + $attendancesProccess[0] = 1; + } else { + $attendancesProccess['presence'] = $absentString; + $attendancesProccess[0] = 0; + } + $attendancesProccess['session'] = 0; + $attendancesProccess['sessionName'] = ''; + $tempDate[] = $attendancesProccess; + $dateKey = new DateTime($dateTemp); + /* + $attendancesProccess['teacher'] = ''; + if(isset($courseItem['teachers']) and isset($courseItem['teachers'][0])){ + $attendancesProccess['teacher'] = $courseItem['teachers'][0]['fullname']; + } + */ + $data[$dateKey->format('Y-m-d')][] = $attendancesProccess; + } + } + } + $sheetsProccessed[] = $tempDate; + $temp[] = $sheetsProccessed; + } + $courses['not_category'][$i]['attendanceSheet'] = $temp; + } + } + + /* Sessions */ + $sql = " + SELECT + session_id, + c_id + FROM + session_rel_course_rel_user + WHERE + user_id = $studentId"; + + $rs = Database::query($sql); + // get info from sessions + while ($row = Database::fetch_array($rs, 'ASSOC')) { + $courseId = $row['c_id']; + $sessionId = $row['session_id']; + $courseItem = api_get_course_info_by_id($courseId); + + $attenances = $attendanceLib->get_attendances_list( + $courseId, + $sessionId + ); + $temp = []; + $sheetsProccessed = []; + $tempDate = []; + foreach ($attenances as $attendanceData) { + $attendanceId = $attendanceData['id']; + $sheets = $attendanceLib->get_users_attendance_sheet( + $attendanceId, + $studentId, + 0, + $courseId, + $startDate, + $endDate + ); + + $sheetsProccessed[] = []; + foreach ($sheets as $sheetData) { + $totalb = count($sheetData); + $tempDate = []; + for ($ii = 0; $ii < $totalb; $ii++) { + $work = $sheetData[$ii]; + $attendancesProccess = $work; + if (!empty($attendancesProccess)) { + $dateTemp = $attendancesProccess['0']; + $attendancesProccess[0] = $attendancesProccess[1]; + $attendancesProccess[1] = $dateTemp; + $attendancesProccess[2] = $courseItem['title']; + $attendancesProccess['courseTitle'] = $courseItem['title']; + $attendancesProccess[3] = $courseItem['real_id']; + $attendancesProccess['courseId'] = $courseItem['real_id']; + $attendancesProccess[4] = $attendanceData['name']; + $attendancesProccess['attendanceName'] = $attendanceData['name']; + $attendancesProccess[5] = $attendanceData['id']; + $attendancesProccess['attendanceId'] = $attendanceData['id']; + $attendancesProccess['courseCode'] = $courseItem['official_code']; + if ($attendancesProccess['presence'] == 1) { + $attendancesProccess['presence'] = $presentString; + $attendancesProccess[0] = 1; + } else { + $attendancesProccess['presence'] = $absentString; + $attendancesProccess[0] = 0; + } + $attendancesProccess['session'] = $sessionId; + $attendancesProccess['sessionName'] = api_get_session_name($sessionId); + + $tempDate[] = $attendancesProccess; + $dateKey = new DateTime($dateTemp); + /* + $attendancesProccess['teacher'] = ''; + if(isset($courseItem['tutor_name']) ){ + $attendancesProccess['teacher'] = $courseItem['tutor_name']; + } + */ + $data[$dateKey->format('Y-m-d')][] = $attendancesProccess; + } + } + } + $sheetsProccessed[] = $tempDate; + $temp[] = $sheetsProccessed; + } + $courses['session'][$i]['attendanceSheet'] = $temp; + } + + /* Order desc by date, by default */ + if ($orderDesc == true) { + ksort($data); + } else { + krsort($data); + } + + return $data; + } } diff --git a/main/mySpace/myStudents.php b/main/mySpace/myStudents.php index a1bc62898a..534071755f 100755 --- a/main/mySpace/myStudents.php +++ b/main/mySpace/myStudents.php @@ -738,6 +738,18 @@ if (Skill::isAllowed($student_id, false)) { api_get_path(WEB_CODE_PATH).'badge/assign.php?'.http_build_query(['user' => $student_id]) ); } +// All Attendance +if (Skill::isAllowed($student_id, false)) { + echo Display::url( + Display::return_icon( + 'attendance.png', + get_lang('CountDoneAttendance'), + null, + ICON_SIZE_MEDIUM + ), + api_get_path(WEB_CODE_PATH).'mySpace/myStudents.php?action=all_attendance&student='.$student_id + ); +} $permissions = StudentFollowUpPlugin::getPermissions( $student_id, @@ -970,6 +982,115 @@ if (api_get_setting('allow_terms_conditions') === 'true') { ]; } +if (isset($_GET['action']) and $_GET['action'] == 'all_attendance') { + /*Display all attendances */ + // Varible for all attendance list + $startDate = new DateTime(); + $startDate = $startDate->modify('-1 week'); + if (isset($_GET['startDate'])) { + $startDate = new DateTime($_GET['startDate']); + } + $startDate = $startDate->setTime(0, 0, 0); + + $endDate = new DateTime(); + if (isset($_GET['endDate'])) { + $endDate = new DateTime($_GET['endDate']); + } + $endDate = $endDate->setTime(23, 59, 0); + + // $startDate = new DateTime(api_get_local_time($startDate)); + // $endDate = new DateTime(api_get_local_time($endDate)); + if ($startDate > $endDate) { + $dataTemp = $startDate; + $startDate = $endDate; + $endDate = $dataTemp; + } + $startDateText = api_get_local_time($startDate); + $endDateText = api_get_local_time($endDate); + // Varible for all attendance list + + /** Start date and end date*/ + $defaults['startDate'] = $startDateText; + $defaults['endDate'] = $endDateText; + $form = new FormValidator('all_attendance_list', 'GET', + 'myStudents.php?action=all_attendance&student='.$_GET['student'].'&startDate='.$defaults['startDate'].'&endDate='.$defaults['endDate'].'&&'.api_get_cidreq(), + ''); + $form->addElement('html', ''); + $form->addElement('html', ''); + + $form->addDateTimePicker('startDate', [ + get_lang('ExeStartTime'), + ], [ + 'form_name' => 'attendance_calendar_edit', + ], 5); + $form->addDateTimePicker('endDate', [ + get_lang('ExeEndTime'), + ], [ + 'form_name' => 'attendance_calendar_edit', + ], 5); + + $form->addButtonSave(get_lang('Submit')); + $form->setDefaults($defaults); + $form->display(); + /** Display dates */ + $attendance = new Attendance(); + $data = $attendance->getCoursesWithAttendance($student_id, $startDate, $endDate); + + // 'attendance from %s to %s' + $title = sprintf(get_lang('AttendanceFromTo'), $startDateText, $endDateText); + echo ' +

'.$title.'

+
+ + + + + + + + + + '; + // + foreach ($data as $attendanceData => $attendanceSheet) { + /* + * $attendanceData can be in_category or not_category for courses + * */ + + $totalAttendance = count($attendanceSheet); + for ($i = 0; $i < $totalAttendance; $i++) { + $attendanceWork = $attendanceSheet[$i]; + $courseInfoItem = api_get_course_info_by_id($attendanceWork['courseId']); + $date = api_get_local_time($attendanceWork[1]); + $sId = $attendanceWork['session']; + $printSession = ''; + if ($sId != 0) { + // get session name + $printSession = "(".$attendanceWork['sessionName'].")"; + } + // $teacher = isset($attendanceWork['teacher'])?$attendanceWork['teacher']:''; + echo ' + + + + + + '; + // + } + } + echo ' +
'.get_lang('DateExo').''.get_lang('Training').''.get_lang('Present').'
'.get_lang('Professor').'
'.$date.'' + .'' + .$attendanceWork['courseTitle']." $printSession ".' + '.$attendanceWork['presence'].'
'.$teacher.'
'; + /** Display dates */ + /*Display all attendances */ + Display::display_footer(); + exit(); +} $details = true; $tpl = new Template( '',