Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | 1x 12x 1x 12x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 3x 3x 3x 9x 3x 3x 9x 9x 3x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 9x 9x 9x 9x 3x 18x 3x | import React, { useEffect, useState } from "react";
import { useLocation, useParams } from "react-router-dom";
import axios from "axios";
import { useTheme } from "@mui/material/styles";
import { Alert, Box, Card, CardContent, Chip, Grid, Stack, Typography } from "@mui/material";
import AssignmentIcon from "@mui/icons-material/Assignment";
import GroupsIcon from "@mui/icons-material/Groups";
import QuizIcon from "@mui/icons-material/Quiz";
import FunctionsIcon from "@mui/icons-material/Functions";
import AlignVerticalCenterIcon from "@mui/icons-material/AlignVerticalCenter";
import ArrowUpwardIcon from "@mui/icons-material/ArrowUpward";
import ArrowDownwardIcon from "@mui/icons-material/ArrowDownward";
import PieChartIcon from "@mui/icons-material/PieChart";
import EqualizerIcon from "@mui/icons-material/Equalizer";
import BarChartIcon from "@mui/icons-material/BarChart";
import ListAltIcon from "@mui/icons-material/ListAlt";
import { API_ENDPOINTS } from "@/api/endpoints";
import { utcToLocalTime } from "@/utils/helpers/utcToLocalTime";
import SimpleLoading from "@/utils/components/SimpleLoading";
import { useUpgradeDialog } from "@/utils/components/feature-access/UpgradeDialogProvider";
import useFeatureFlag from "@/utils/hooks/useFeatureFlag";
import { getFeatureMeta, isFeatureNotEnabled403, parseFeatureKeyFrom403 } from "@/utils/features/featureAccess";
import {
getUpgradeDialogOptionsForQuota,
} from "@/utils/features/teacherMadeAssignmentQuota";
import StatCard from "@/app/teacher-page/page/dashboard/components/StatCard";
import { scoreBandColor, toNumber } from "@/app/teacher-page/components/data-analytics/assignment-summary/scoreBands";
import PassFailDonut from "@/app/teacher-page/components/data-analytics/assignment-summary/PassFailDonut";
import ScoreDistributionChart from "@/app/teacher-page/components/data-analytics/assignment-summary/ScoreDistributionChart";
import CategoryPerformanceChart from "@/app/teacher-page/components/data-analytics/assignment-summary/CategoryPerformanceChart";
import CategoryPerformanceTable from "@/app/teacher-page/components/data-analytics/assignment-summary/CategoryPerformanceTable";
const MiniStat = ({ label, value }) => (
<Box>
<Typography variant="caption" color="text.secondary" sx={{ display: "block" }}>
{label}
</Typography>
<Typography variant="subtitle2" fontWeight={700}>
{value}
</Typography>
</Box>
);
const SectionCard = ({ icon, title, subtitle, children }) => (
<Card elevation={0} sx={{ border: (t) => `1px solid ${t.palette.divider}`, borderRadius: 3, height: "100%" }}>
<CardContent>
<Stack direction="row" alignItems="center" spacing={1} sx={{ mb: subtitle ? 0.25 : 2 }}>
{icon}
<Typography variant="subtitle1" fontWeight={700}>
{title}
</Typography>
</Stack>
{subtitle && (
<Typography variant="caption" color="text.secondary" sx={{ display: "block", mb: 2 }}>
{subtitle}
</Typography>
)}
{children}
</CardContent>
</Card>
);
const AssignmentSummary = () => {
const theme = useTheme();
const location = useLocation();
const { classCode } = useParams();
// location.state is only populated when navigating here via an in-app Link/navigate
// call — a direct URL visit or hard refresh loses it, so this must stay optional.
const assignmentId = location.state?.assignmentId;
const { openUpgradeDialog } = useUpgradeDialog();
const { features, getFeature } = useFeatureFlag("teacher");
const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);
const fetchAnalytics = async () => {
try {
setLoading(true);
const response = await axios.get(
API_ENDPOINTS.teacher.assignment.analytics.summary(classCode, assignmentId)
);
Eif (response.status == 200) {
setData(response.data);
setLoading(false);
}
} catch (error) {
console.error("Error:", error.response?.data || error.message);
if (isFeatureNotEnabled403(error)) {
// `isFeatureNotEnabled403` above is true only when `parseFeatureKeyFrom403` already
// produced a key, so the old `?? fallback` here could never be reached.
// Removed rather than left to imply the fallback matters. (Allan Ninal)
const featureKey = parseFeatureKeyFrom403(error);
const featureMeta = getFeature(featureKey) || getFeatureMeta(features, featureKey);
openUpgradeDialog({
...getUpgradeDialogOptionsForQuota(null, features),
title: "Assignment analytics are locked",
featureKey,
featureLabel: "Assignment Summary",
featureMeta,
minPlan: featureMeta?.min_plan ?? "intermediate",
});
}
setLoading(false);
}
};
useEffect(() => {
Eif (assignmentId) {
fetchAnalytics();
}
}, []);
Iif (!assignmentId) {
return (
<Alert severity="warning" sx={{ mt: 3 }}>
We couldn't determine which assignment to load. Please navigate here from the assignments list.
</Alert>
);
}
if (loading) {
return <SimpleLoading showLoading={loading} />;
}
if (!data) {
return null;
}
const { assignment_details: details, assignment_summary: summary, categories } = data;
const mean = toNumber(summary.mean);
const median = toNumber(summary.median);
const minimum = toNumber(summary.minimum);
const maximum = toNumber(summary.maximum);
const firstQuartile = toNumber(summary.first_quartile);
const thirdQuartile = toNumber(summary.third_quartile);
const passed = toNumber(summary.total_passed) ?? 0;
const failed = toNumber(summary.total_failed) ?? 0;
const now = new Date();
const closeDate = details.date_close ? new Date(details.date_close) : null;
const isClosed = closeDate ? closeDate < now : false;
const statCards = [
{
id: "assignment-summary-statcard-submissions",
icon: <GroupsIcon />,
value: summary.total_submissions,
label: "Submissions",
color: theme.palette.primary.main,
},
{
id: "assignment-summary-statcard-questions",
icon: <QuizIcon />,
value: summary.total_questions,
label: "Questions",
color: theme.palette.info.main,
},
{
id: "assignment-summary-statcard-mean",
icon: <FunctionsIcon />,
value: mean !== null ? mean.toFixed(1) : "—",
label: "Mean",
color: scoreBandColor(theme, mean),
},
{
id: "assignment-summary-statcard-median",
icon: <AlignVerticalCenterIcon />,
value: median !== null ? median.toFixed(1) : "—",
label: "Median",
color: scoreBandColor(theme, median),
},
{
id: "assignment-summary-statcard-highest",
icon: <ArrowUpwardIcon />,
value: summary.maximum,
label: "Highest Score",
color: theme.palette.success.main,
},
{
id: "assignment-summary-statcard-lowest",
icon: <ArrowDownwardIcon />,
value: summary.minimum,
label: "Lowest Score",
color: theme.palette.error.main,
},
];
return (
<Box id="assignment-summary-page">
<Card elevation={0} sx={{ border: (t) => `1px solid ${t.palette.divider}`, borderRadius: 3, mb: 3 }}>
<CardContent>
<Stack
direction={{ xs: "column", sm: "row" }}
justifyContent="space-between"
alignItems={{ xs: "flex-start", sm: "center" }}
spacing={1.5}
>
<Stack direction="row" alignItems="center" spacing={1.5}>
<AssignmentIcon color="primary" />
<Box>
<Typography variant="h6" fontWeight={700}>
{details.title}
</Typography>
{details.description && (
<Typography variant="body2" color="text.secondary">
{details.description}
</Typography>
)}
</Box>
</Stack>
<Stack direction="row" spacing={1}>
<Chip label={details.type} size="small" color="primary" variant="outlined" />
<Chip
label={isClosed ? "Closed" : "Open"}
size="small"
color={isClosed ? "default" : "success"}
variant={isClosed ? "outlined" : "filled"}
/>
</Stack>
</Stack>
<Stack direction={{ xs: "column", sm: "row" }} spacing={3} sx={{ mt: 2 }}>
<Typography variant="body2" color="text.secondary">
Opened: {utcToLocalTime(details.date_open)}
</Typography>
<Typography variant="body2" color="text.secondary">
Closed: {utcToLocalTime(details.date_close)}
</Typography>
</Stack>
</CardContent>
</Card>
<Grid container spacing={{ xs: 1.5, sm: 2 }} sx={{ mb: 3 }}>
{statCards.map((s) => (
<Grid id={s.id} size={{ xs: 6, sm: 4, md: 2 }} key={s.label}>
<StatCard {...s} loading={false} />
</Grid>
))}
</Grid>
<Grid container spacing={3} sx={{ mb: 3 }}>
<Grid size={{ xs: 12, md: 5 }}>
<SectionCard icon={<PieChartIcon color="primary" />} title="Pass / Fail">
<PassFailDonut passed={passed} failed={failed} />
</SectionCard>
</Grid>
<Grid size={{ xs: 12, md: 7 }}>
<SectionCard icon={<EqualizerIcon color="primary" />} title="Score Distribution">
<ScoreDistributionChart
minimum={minimum}
firstQuartile={firstQuartile}
median={median}
thirdQuartile={thirdQuartile}
maximum={maximum}
mean={mean}
/>
<Stack
direction="row"
flexWrap="wrap"
justifyContent="space-between"
sx={{ mt: 2, pt: 2, borderTop: (t) => `1px solid ${t.palette.divider}`, rowGap: 1.5 }}
>
<MiniStat label="Range" value={summary.range} />
<MiniStat label="First Quartile" value={summary.first_quartile} />
<MiniStat label="Third Quartile" value={summary.third_quartile} />
<MiniStat label="Standard Deviation" value={summary.std_dev} />
</Stack>
</SectionCard>
</Grid>
</Grid>
<Grid container spacing={3} sx={{ mb: 3 }}>
<Grid size={{ xs: 12 }}>
<SectionCard
icon={<BarChartIcon color="primary" />}
title={`Performance by ${details.type === "STAAR" ? "Category" : "Topic"}`}
subtitle="Weakest areas listed first"
>
<CategoryPerformanceChart categories={categories} />
</SectionCard>
</Grid>
</Grid>
<SectionCard icon={<ListAltIcon color="primary" />} title="Category Breakdown">
<CategoryPerformanceTable
categories={categories}
columnLabel={details.type === "STAAR" ? "Category" : "Topic"}
/>
</SectionCard>
</Box>
);
};
export default AssignmentSummary;
|