Files
classroomio/src/routes/courses/[id]/attendance/+page.svelte
T

215 lines
6.7 KiB
Svelte
Raw Normal View History

2021-09-04 22:33:35 +03:00
<script>
import { onMount } from 'svelte';
2023-08-07 09:53:07 +00:00
import {
Search,
Checkbox,
StructuredList,
StructuredListBody,
StructuredListCell,
StructuredListHead,
StructuredListRow,
Pagination
} from 'carbon-components-svelte';
import AudioConsoleIcon from 'carbon-icons-svelte/lib/AudioConsole.svelte';
import CourseContainer from '$lib/components/CourseContainer/index.svelte';
import PageNav from '$lib/components/PageNav/index.svelte';
import PageBody from '$lib/components/PageBody/index.svelte';
import Box from '$lib/components/Box/index.svelte';
import { course, group, setCourse } from '$lib/components/Course/store';
import { getLectureNo } from '$lib/components/Course/function';
import { fetchCourse } from '$lib/utils/services/courses';
import { lessons } from '$lib/components/Course/components/Lesson/store/lessons';
import { ROLE } from '$lib/utils/constants/roles';
import { takeAttendance } from '$lib/utils/services/attendance';
import { snackbarStore } from '$lib/components/Snackbar/store';
import { SNACKBAR_SEVERITY } from '$lib/components/Snackbar/constants';
import { attendance } from '$lib/utils/store/attendance';
import { profile } from '$lib/utils/store/user';
2021-09-04 22:33:35 +03:00
export let data;
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;
2023-08-07 09:53:07 +00:00
let searchValue = '';
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,
2023-08-07 09:53:07 +00:00
is_present
}
2021-09-05 22:16:42 +03:00
};
} else {
$attendance[student_id] = {
...$attendance[student_id],
[lesson_id]: {
id,
2023-08-07 09:53:07 +00:00
is_present
}
2021-09-05 22:16:42 +03:00
};
}
}
}
2021-10-22 14:55:33 +03:00
function handleAttendanceChange(e, student, lesson) {
if (isStudent) return;
2023-08-07 09:53:07 +00:00
const attendanceItem = $attendance[student.id] ? $attendance[student.id][lesson.id] || {} : {};
2021-10-22 14:55:33 +03:00
2021-11-13 21:29:26 +02:00
/*
This validation is useless. We shouldn't assume we already have an attendance item id
because it might be a new attendance.
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;
}
2021-11-13 21:29:26 +02:00
*/
2021-10-22 14:55:33 +03:00
const _data = {
2021-10-22 14:55:33 +03:00
id: attendanceItem.id,
student_id: student.id,
is_present: e.target.checked,
lesson_id: lesson.id,
2023-08-07 09:53:07 +00:00
course_id: data.courseId
2021-10-22 14:55:33 +03:00
};
takeAttendance(_data).then((res) => {
2021-10-22 14:55:33 +03:00
if (res.error) {
console.error(`res.error`, res.error);
$snackbarStore.open = true;
$snackbarStore.severity = SNACKBAR_SEVERITY.ERROR;
2023-08-07 09:53:07 +00:00
$snackbarStore.message = "Something isn't right. Please reload and take attendance again";
2021-10-22 14:55:33 +03:00
} else {
2021-11-13 21:29:26 +02:00
// console.log('res', JSON.stringify(res));
2021-10-22 14:55:33 +03:00
const { id, is_present } = res.data[0];
if ($attendance[student.id]) {
$attendance[student.id][lesson.id] = {
id,
2023-08-07 09:53:07 +00:00
is_present
2021-10-22 14:55:33 +03:00
};
} else {
$attendance[student.id] = {
[lesson.id]: {
id,
2023-08-07 09:53:07 +00:00
is_present
}
2021-10-22 14:55:33 +03:00
};
}
}
});
}
2023-08-07 09:53:07 +00:00
// function for the searchbar
function searchStudents(query) {
const lowercaseQuery = query.toLowerCase();
return students.filter((student) =>
student.profile.fullname.toLowerCase().includes(lowercaseQuery)
);
}
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: _data } = await fetchCourse(data.courseId);
setCourse(_data);
setAttendance(_data);
2021-09-04 22:33:35 +03:00
});
2021-09-05 22:16:42 +03:00
$: students = isStudent
2023-08-07 09:53:07 +00:00
? $group.people.filter((person) => !!person.profile && person.profile.id === $profile.id)
: $group.people.filter((person) => !!person.profile && person.role_id === ROLE.STUDENT);
console.log(students);
2021-09-04 22:33:35 +03:00
</script>
<CourseContainer bind:isStudent>
2021-09-04 22:33:35 +03:00
<PageNav title="Attendance" />
2021-09-05 22:16:42 +03:00
2023-08-07 09:53:07 +00:00
<section class="mx-9 my-5">
<div class="flex flex-row items-center justify-between">
<div class="flex">
<p class="flex items-center mr-5">
<Checkbox checked disabled /> Present
</p>
<p class="flex items-center">
<Checkbox disabled /> Absent
</p>
</div>
<div>
2023-08-07 09:53:07 +00:00
<Search
class="dark:text-slate-950"
placeholder="Search students"
bind:value={searchValue}
/>
</div>
</div>
</section>
<section class="my-5 mx-9">
<StructuredList class="m-0">
<!-- Moved the lesson headers outside the students loop -->
2023-08-20 19:08:48 +00:00
<StructuredListHead class="bg-primary-100">
2023-08-07 09:53:07 +00:00
<StructuredListRow head class="mx-7">
2023-08-20 19:08:48 +00:00
<StructuredListCell head class="text-primary-600 py-3">Student</StructuredListCell>
2023-08-07 09:53:07 +00:00
{#each $lessons as lesson, index}
2023-08-20 19:08:48 +00:00
<StructuredListCell head class="text-primary-600 py-3"
2023-08-07 09:53:07 +00:00
>Lesson 0{getLectureNo(index + 1)}</StructuredListCell
>
2021-09-05 22:16:42 +03:00
{/each}
2023-08-07 09:53:07 +00:00
</StructuredListRow>
</StructuredListHead>
{#each searchStudents(searchValue) as student}
<StructuredListBody>
<StructuredListRow>
<StructuredListCell>
<div class="w-1/4 flex items-center">
<p class="dark:text-white font-semibold">
{student.profile.fullname}
</p>
</div>
</StructuredListCell>
{#each $lessons as lesson}
<StructuredListCell class="">
<Checkbox
class={isStudent && 'cursor-not-allowed'}
disabled={isStudent}
checked={$attendance[student.id]
? $attendance[student.id][lesson.id]
? $attendance[student.id][lesson.id].is_present
: false
: false}
on:change={(e) => handleAttendanceChange(e, student, lesson)}
/>
</StructuredListCell>
{/each}
</StructuredListRow>
</StructuredListBody>
{/each}
</StructuredList>
{#if students.length === 0}
<div>
2021-09-11 19:27:11 +03:00
<Box>
<AudioConsoleIcon size={32} class="carbon-icon w-80" />
2023-08-07 09:53:07 +00:00
<h3 class="text-3xl text-gray-500 dark:text-white">No Student Added</h3>
2021-09-11 19:27:11 +03:00
</Box>
2023-08-07 09:53:07 +00:00
</div>
{/if}
<Pagination totalItems={10} pageSizes={[10, 15, 20]} />
</section>
</CourseContainer>