2021-09-04 22:33:35 +03:00
|
|
|
<script context="module">
|
2021-09-05 22:16:42 +03:00
|
|
|
export async function preload({ params }) {
|
|
|
|
|
return { courseId: params.id };
|
2021-09-04 22:33:35 +03:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
import { onMount } from 'svelte';
|
2021-09-11 19:27:11 +03:00
|
|
|
import AudioConsole32 from 'carbon-icons-svelte/lib/AudioConsole32';
|
2021-09-04 22:33:35 +03:00
|
|
|
import CourseContainer from '../../../components/CourseContainer/index.svelte';
|
|
|
|
|
import PageNav from '../../../components/PageNav/index.svelte';
|
|
|
|
|
import PageBody from '../../../components/PageBody/index.svelte';
|
2021-09-11 19:27:11 +03:00
|
|
|
import Box from '../../../components/Box/index.svelte';
|
2021-09-05 22:16:42 +03:00
|
|
|
import {
|
|
|
|
|
course,
|
|
|
|
|
group,
|
|
|
|
|
setCourseData,
|
|
|
|
|
} from '../../../components/Course/store';
|
|
|
|
|
import { getLectureNo } from '../../../components/Course/function';
|
|
|
|
|
import { fetchCourse } from '../../../utils/services/courses';
|
|
|
|
|
import { lessons } from '../../../components/Course/components/Lesson/store/lessons';
|
|
|
|
|
import { ROLE } from '../../../utils/constants/roles';
|
|
|
|
|
import { takeAttendance } from '../../../utils/services/attendance';
|
|
|
|
|
import { snackbarStore } from '../../../components/Snackbar/store';
|
|
|
|
|
import { SNACKBAR_SEVERITY } from '../../../components/Snackbar/constants';
|
|
|
|
|
import { attendance } from '../../../utils/store/attendance';
|
2021-09-19 20:01:54 +03:00
|
|
|
import { profile } from '../../../utils/store/user';
|
2021-09-04 22:33:35 +03:00
|
|
|
|
2021-09-05 22:16:42 +03:00
|
|
|
export let courseId;
|
2021-09-04 22:33:35 +03:00
|
|
|
|
2021-09-05 22:16:42 +03:00
|
|
|
let borderBottomGrey = 'border-r-0 border-b border-l-0 border-gray-300';
|
|
|
|
|
let students = [];
|
2021-09-11 21:52:46 +03:00
|
|
|
let isStudent;
|
2021-09-05 22:16:42 +03:00
|
|
|
|
|
|
|
|
function setAttendance(courseData) {
|
|
|
|
|
for (const attendanceItem of courseData.attendance) {
|
|
|
|
|
const { student_id, lesson_id, is_present, id } = attendanceItem;
|
|
|
|
|
|
|
|
|
|
if (!$attendance[student_id]) {
|
|
|
|
|
$attendance[student_id] = {
|
|
|
|
|
[lesson_id]: {
|
|
|
|
|
id,
|
|
|
|
|
is_present,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
$attendance[student_id] = {
|
|
|
|
|
...$attendance[student_id],
|
|
|
|
|
[lesson_id]: {
|
|
|
|
|
id,
|
|
|
|
|
is_present,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 14:55:33 +03:00
|
|
|
function handleAttendanceChange(e, student, lesson) {
|
|
|
|
|
if (isStudent) return;
|
|
|
|
|
|
|
|
|
|
const attendanceItem = $attendance[student.id]
|
|
|
|
|
? $attendance[student.id][lesson.id] || {}
|
|
|
|
|
: {};
|
|
|
|
|
|
2021-11-02 09:52:05 +02:00
|
|
|
if (!attendanceItem.id) {
|
2021-10-22 14:55:33 +03:00
|
|
|
$snackbarStore.open = true;
|
|
|
|
|
$snackbarStore.severity = SNACKBAR_SEVERITY.ERROR;
|
|
|
|
|
$snackbarStore.message =
|
|
|
|
|
"Something isn't right. Please reload and take attendance again";
|
|
|
|
|
console.error(`Attendance Id Missing`, courseId, student.id, lesson.id);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
id: attendanceItem.id,
|
|
|
|
|
student_id: student.id,
|
|
|
|
|
is_present: e.target.checked,
|
|
|
|
|
lesson_id: lesson.id,
|
|
|
|
|
course_id: courseId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
takeAttendance(data).then((res) => {
|
|
|
|
|
if (res.error) {
|
|
|
|
|
console.error(`res.error`, res.error);
|
|
|
|
|
$snackbarStore.open = true;
|
|
|
|
|
$snackbarStore.severity = SNACKBAR_SEVERITY.ERROR;
|
|
|
|
|
$snackbarStore.message = "Something isn't right.";
|
|
|
|
|
} else {
|
|
|
|
|
console.log('res', JSON.stringify(res));
|
|
|
|
|
const { id, is_present } = res.data[0];
|
|
|
|
|
if ($attendance[student.id]) {
|
|
|
|
|
$attendance[student.id][lesson.id] = {
|
|
|
|
|
id,
|
|
|
|
|
is_present,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
$attendance[student.id] = {
|
|
|
|
|
[lesson.id]: {
|
|
|
|
|
id,
|
|
|
|
|
is_present,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-05 22:16:42 +03:00
|
|
|
onMount(async () => {
|
2021-09-06 11:10:54 +03:00
|
|
|
if ($course.id) {
|
|
|
|
|
if (!Object.keys($attendance).length) {
|
|
|
|
|
setAttendance($course);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-05 22:16:42 +03:00
|
|
|
|
|
|
|
|
const { data } = await fetchCourse(courseId);
|
|
|
|
|
setCourseData(data);
|
|
|
|
|
setAttendance(data);
|
2021-09-04 22:33:35 +03:00
|
|
|
});
|
2021-09-05 22:16:42 +03:00
|
|
|
|
2021-09-19 20:01:54 +03:00
|
|
|
$: students = isStudent
|
|
|
|
|
? $group.people.filter(
|
|
|
|
|
(person) => !!person.profile && person.profile.id === $profile.id
|
|
|
|
|
)
|
|
|
|
|
: $group.people.filter(
|
|
|
|
|
(person) => !!person.profile && person.role_id === ROLE.STUDENT
|
|
|
|
|
);
|
2021-09-04 22:33:35 +03:00
|
|
|
</script>
|
|
|
|
|
|
2021-09-19 20:01:54 +03:00
|
|
|
<CourseContainer bind:isStudent>
|
2021-09-04 22:33:35 +03:00
|
|
|
<PageNav title="Attendance" />
|
2021-09-05 22:16:42 +03:00
|
|
|
<PageBody>
|
|
|
|
|
<div class="table rounded-md border border-gray-300 w-full">
|
|
|
|
|
<div
|
|
|
|
|
class="flex items-center font-bold border-t-0 {borderBottomGrey} p-3"
|
|
|
|
|
>
|
|
|
|
|
<p class="w-1/4">Student</p>
|
|
|
|
|
{#each $lessons as lesson, index}
|
|
|
|
|
<p class="col" title={lesson.title}>
|
|
|
|
|
{getLectureNo(index + 1)}
|
|
|
|
|
</p>
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{#each students as student}
|
|
|
|
|
<div
|
|
|
|
|
class="flex relative items-center p-3 cursor-pointer {borderBottomGrey}"
|
|
|
|
|
>
|
|
|
|
|
<div class="w-1/4 flex items-center">
|
|
|
|
|
<img
|
|
|
|
|
alt="Student avatar"
|
|
|
|
|
src={student.profile.avatar_url}
|
|
|
|
|
class="w-8 h-8 rounded-full mr-2"
|
|
|
|
|
/>
|
|
|
|
|
<div class="text-sm">
|
|
|
|
|
<p class="font-semibold">{student.profile.fullname}</p>
|
2021-10-04 05:38:11 +00:00
|
|
|
{#if student.assigned_student_id}
|
2021-10-22 14:55:33 +03:00
|
|
|
<p>#{student.assigned_student_id}</p>
|
2021-10-04 05:38:11 +00:00
|
|
|
{/if}
|
2021-09-05 22:16:42 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{#each $lessons as lesson}
|
|
|
|
|
<p class="col">
|
|
|
|
|
<input
|
2021-09-11 21:52:46 +03:00
|
|
|
class="form-radio {isStudent && 'cursor-not-allowed'}"
|
2021-09-05 22:16:42 +03:00
|
|
|
type="checkbox"
|
2021-09-11 21:52:46 +03:00
|
|
|
disabled={isStudent}
|
2021-09-05 22:16:42 +03:00
|
|
|
checked={$attendance[student.id]
|
|
|
|
|
? $attendance[student.id][lesson.id]
|
|
|
|
|
? $attendance[student.id][lesson.id].is_present
|
|
|
|
|
: false
|
|
|
|
|
: false}
|
2021-10-22 14:55:33 +03:00
|
|
|
on:change={(e) => handleAttendanceChange(e, student, lesson)}
|
2021-09-05 22:16:42 +03:00
|
|
|
/>
|
|
|
|
|
</p>
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
2021-09-11 19:27:11 +03:00
|
|
|
{:else}
|
|
|
|
|
<Box>
|
|
|
|
|
<AudioConsole32 class="carbon-icon w-80" />
|
|
|
|
|
<h3 class="text-3xl text-gray-500">No Student Added</h3>
|
|
|
|
|
</Box>
|
2021-09-05 22:16:42 +03:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
</PageBody>
|
2021-09-04 22:33:35 +03:00
|
|
|
</CourseContainer>
|
2021-09-05 22:16:42 +03:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.col {
|
|
|
|
|
width: 50px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|