Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: working on the LMS side #447

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
let open = false;

const getCompletedCoursesCount = (pathway) => {
return pathway.courses?.filter((course) => course.progress_rate === 100).length || 0;
return pathway.pathway_course?.filter((course) => course.progress_rate === 100).length || 0;
};

// this return the first course it can find in the array that is been taken but not yet completed.
const getCurrentCourse = (pathway) => {
return pathway.courses?.find(
return pathway.pathway_course?.find(
(course) => course.is_unlocked === true && course.progress_rate !== 100
);
};
Expand Down Expand Up @@ -54,15 +54,15 @@
>
{getCompletedCoursesCount(course) || 0}
{$t('lms_pathway.of')}
{course.courses.length}
{course?.total_course}
{$t('lms_pathway.completed')}
<ChevronDown />
</button>
</div>

<span class="flex flex-col lg:flex-row gap-3 items-start pb-5">
<img
src={getCurrentCourse(course).logo ||
src={getCurrentCourse(course)?.logo ||
'/images/classroomio-course-img-template.jpg'}
alt="course"
class="hidden lg:block lg:w-[60px] lg:h-[60px]"
Expand Down
87 changes: 84 additions & 3 deletions apps/dashboard/src/lib/components/Org/Pathway/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
// import { supabase } from '$lib/utils/functions/supabase';
// import { get } from 'svelte/store';
// import { isOrgAdmin } from '$lib/utils/store/org';

// export async function fetchPathways(profileId: string | undefined, orgId: string | undefined) {
// if (!orgId || !profileId) return;

// const match: {
// member_profile_id?: string;
// } = {};
// // Filter by profile_id if role isn't admin within organization
// if (!get(isOrgAdmin)) {
// match.member_profile_id = profileId;
// }

// const { data: allPathways } = await supabase
// .rpc('get_pathways', {
// org_id_arg: orgId,
// profile_id_arg: profileId
// })
// .match(match);

// if (!Array.isArray(allPathways)) {
// return {
// allPathways: []
// };
// }

// return { allPathways };
// }

import { supabase } from '$lib/utils/functions/supabase';
import { get } from 'svelte/store';
import { isOrgAdmin } from '$lib/utils/store/org';
Expand All @@ -8,23 +39,73 @@ export async function fetchPathways(profileId: string | undefined, orgId: string
const match: {
member_profile_id?: string;
} = {};

// Filter by profile_id if role isn't admin within organization
if (!get(isOrgAdmin)) {
match.member_profile_id = profileId;
}

const { data: allPathways } = await supabase
// Step 1: Fetch pathways using the RPC function
const { data: allPathways, error: pathwayError } = await supabase
.rpc('get_pathways', {
org_id_arg: orgId,
profile_id_arg: profileId
})
.match(match);

if (!Array.isArray(allPathways)) {
if (pathwayError || !Array.isArray(allPathways)) {
console.error('Error fetching pathways:', pathwayError);
return {
allPathways: []
};
}

return { allPathways };
// Step 2: Fetch courses for each pathway
const pathwayIds = allPathways.map((pathway: any) => pathway.id);

const { data: allCourses, error: courseError } = await supabase
.from('pathway_course')
.select(
`
id,
pathway_id,
order,
course_id,
course (
id,
title,
logo,
description,
banner_image,
created_at,
lesson (
is_complete
),
group_id (
groupmember (
id
)
)
)
`
)
.in('pathway_id', pathwayIds);

if (courseError || !Array.isArray(allCourses)) {
console.error('Error fetching courses:', courseError);
return {
allPathways
};
}

// Step 3: Attach courses to their respective pathways and rename pathway_course to courses
const pathwaysWithCourses = allPathways.map((pathway: any) => {
const courses = allCourses.filter((course: any) => course.pathway_id === pathway.id);
return {
...pathway,
pathway_course: courses // Rename pathway_course to courses
};
});

return { allPathways: pathwaysWithCourses };
}
4 changes: 2 additions & 2 deletions apps/dashboard/src/routes/lms/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { t } from '$lib/utils/functions/translations';
import VisitOrgSiteButton from '$lib/components/Buttons/VisitOrgSite.svelte';
import { lmsCourses } from '$lib/components/LMS/store';
import type { LmsCourse } from '$lib/components/LMS/store';
import type { LMSCourse } from '$lib/components/LMS/store';
import { fetchPathways } from '$lib/components/Org/Pathway/api';

let hasFetched = false;
Expand Down Expand Up @@ -56,7 +56,7 @@
}
}

function calcTotalProgress(courses: LmsCourse[] | any) {
function calcTotalProgress(courses: LMSCourse[] | any) {
totalCompleted = courses.reduce((acc, cur) => acc + (cur.progress_rate || 0), 0);
totalLessons = courses.reduce((acc, cur) => acc + (cur.total_lessons || 0), 0);
progressPercentage = Math.round((totalCompleted / totalLessons) * 100) || 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
logo: pathway.logo || '',
is_published: !!pathway.is_published,
prerequisite: pathway.prerequisite,
metadata: pathway.metadata,
metadata: pathway.landingpage,
lms_certificate: pathway.lms_certificate,
courses_certificate: pathway.courses_certificate
};
Expand Down
Loading