import ProgressBar from "@/components/Low/ProgressBar"; import useUsers from "@/hooks/useUsers"; import { Module } from "@/interfaces"; import { Assignment } from "@/interfaces/results"; import { calculateBandScore } from "@/utils/score"; import clsx from "clsx"; import moment from "moment"; import { BsBook, BsClipboard, BsHeadphones, BsMegaphone, BsPen } from "react-icons/bs"; import { usePDFDownload } from "@/hooks/usePDFDownload"; import { useAssignmentArchive } from "@/hooks/useAssignmentArchive"; import { uniqBy } from "lodash"; import { useAssignmentUnarchive } from "@/hooks/useAssignmentUnarchive"; import { useAssignmentRelease } from "@/hooks/useAssignmentRelease"; import { getUserName } from "@/utils/users"; import { User } from "@/interfaces/user"; import { EntityWithRoles } from "@/interfaces/entity"; interface Props { users: User[]; onClick?: () => void; allowDownload?: boolean; reload?: Function; allowArchive?: boolean; allowUnarchive?: boolean; allowExcelDownload?: boolean; entityObj?: EntityWithRoles } export default function AssignmentCard({ id, name, assigner, startDate, endDate, entityObj, assignees, results, exams, archived, onClick, allowDownload, reload, allowArchive, allowUnarchive, allowExcelDownload, users, released, }: Assignment & Props) { const renderPdfIcon = usePDFDownload("assignments"); const renderExcelIcon = usePDFDownload("assignments", "excel"); const renderArchiveIcon = useAssignmentArchive(id, reload); const renderUnarchiveIcon = useAssignmentUnarchive(id, reload); const renderReleaseIcon = useAssignmentRelease(id, reload); const calculateAverageModuleScore = (module: Module) => { const resultModuleBandScores = results.map((r) => { const moduleStats = r.stats.filter((s) => s.module === module); const correct = moduleStats.reduce((acc, curr) => acc + curr.score.correct, 0); const total = moduleStats.reduce((acc, curr) => acc + curr.score.total, 0); return calculateBandScore(correct, total, module, r.type); }); return resultModuleBandScores.length === 0 ? -1 : resultModuleBandScores.reduce((acc, curr) => acc + curr, 0) / results.length; }; const uniqModules = uniqBy(exams, (x) => x.module); const shouldRenderPDF = () => { if (released && allowDownload) { // in order to be downloadable, the assignment has to be released // the component should have the allowDownload prop // and the assignment should not have the level module return uniqModules.every(({ module }) => module !== "level"); } return false; }; const shouldRenderExcel = () => { if (released && allowExcelDownload) { // in order to be downloadable, the assignment has to be released // the component should have the allowExcelDownload prop // and the assignment should have the level module return uniqModules.some(({ module }) => module === "level"); } return false; }; return (