Added the statistical page as a permission based page
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import Layout from "@/components/High/Layout";
|
||||
import UserDisplayList from "@/components/UserDisplayList";
|
||||
import IconCard from "@/dashboards/IconCard";
|
||||
import { useAllowedEntities } from "@/hooks/useEntityPermissions";
|
||||
import { Module } from "@/interfaces";
|
||||
import { EntityWithRoles } from "@/interfaces/entity";
|
||||
import { Assignment } from "@/interfaces/results";
|
||||
@@ -18,6 +19,7 @@ import { groupByExam } from "@/utils/stats";
|
||||
import { getStatsByUsers } from "@/utils/stats.be";
|
||||
import { filterAllowedUsers } from "@/utils/users.be";
|
||||
import { getEntitiesUsers } from "@/utils/users.be";
|
||||
import { clsx } from "clsx";
|
||||
import { withIronSessionSsr } from "iron-session/next";
|
||||
import { uniqBy } from "lodash";
|
||||
import moment from "moment";
|
||||
@@ -73,30 +75,7 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const averageLevelCalculator = (studentStats: Stat[]) => {
|
||||
const formattedStats = studentStats
|
||||
.map((s) => ({
|
||||
focus: students.find((u) => u.id === s.user)?.focus,
|
||||
score: s.score,
|
||||
module: s.module,
|
||||
}))
|
||||
.filter((f) => !!f.focus);
|
||||
const bandScores = formattedStats.map((s) => ({
|
||||
module: s.module,
|
||||
level: calculateBandScore(s.score.correct, s.score.total, s.module, s.focus!),
|
||||
}));
|
||||
|
||||
const levels: { [key in Module]: number } = {
|
||||
reading: 0,
|
||||
listening: 0,
|
||||
writing: 0,
|
||||
speaking: 0,
|
||||
level: 0,
|
||||
};
|
||||
bandScores.forEach((b) => (levels[b.module] += b.level));
|
||||
|
||||
return calculateAverageLevel(levels);
|
||||
};
|
||||
const allowedEntityStatistics = useAllowedEntities(user, entities, 'view_entity_statistics')
|
||||
|
||||
const UserDisplay = (displayUser: User) => (
|
||||
<div className="flex w-full p-4 gap-4 items-center hover:bg-mti-purple-ultralight cursor-pointer transition ease-in-out duration-300">
|
||||
@@ -158,17 +137,20 @@ export default function Dashboard({ user, users, entities, assignments, stats, g
|
||||
value={students.length}
|
||||
color="purple"
|
||||
/>
|
||||
{allowedEntityStatistics.length > 0 && (
|
||||
<IconCard Icon={BsPersonFillGear}
|
||||
onClick={() => router.push("/statistical")}
|
||||
label="Entity Statistics"
|
||||
value={entities.length}
|
||||
value={allowedEntityStatistics.length}
|
||||
color="purple"
|
||||
/>
|
||||
)}
|
||||
<IconCard
|
||||
Icon={BsEnvelopePaper}
|
||||
onClick={() => router.push("/assignments")}
|
||||
label="Assignments"
|
||||
value={assignments.filter((a) => !a.archived).length}
|
||||
className={clsx(allowedEntityStatistics.length === 0 && "col-span-2")}
|
||||
color="purple"
|
||||
/>
|
||||
<IconCard
|
||||
|
||||
@@ -73,6 +73,7 @@ const CLASSROOM_MANAGEMENT: PermissionLayout[] = [
|
||||
|
||||
const ENTITY_MANAGEMENT: PermissionLayout[] = [
|
||||
{ label: "View Entities", key: "view_entities" },
|
||||
{ label: "View Entity Statistics", key: "view_entity_statistics" },
|
||||
{ label: "Rename Entity", key: "rename_entity" },
|
||||
{ label: "Add to Entity", key: "add_to_entity" },
|
||||
{ label: "Remove from Entity", key: "remove_from_entity" },
|
||||
|
||||
@@ -53,6 +53,9 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
|
||||
const entityIDS = mapBy(user.entities, "id") || [];
|
||||
const entities = await getEntitiesWithRoles(isAdmin(user) ? undefined : entityIDS);
|
||||
const allowedEntities = findAllowedEntities(user, entities, 'view_entity_statistics')
|
||||
|
||||
if (allowedEntities.length === 0) return redirect("/")
|
||||
|
||||
const studentsAllowedEntities = findAllowedEntities(user, entities, 'view_students')
|
||||
const students = await getEntitiesUsers(mapBy(studentsAllowedEntities, 'id'), { type: "student" })
|
||||
@@ -61,7 +64,7 @@ export const getServerSideProps = withIronSessionSsr(async ({ req, res }) => {
|
||||
const sessions = await getSessionsByAssignments(mapBy(assignments, 'id'))
|
||||
const exams = await getExamsByIds(assignments.flatMap(a => a.exams))
|
||||
|
||||
return { props: serialize({ user, students, entities, assignments, sessions, exams }) };
|
||||
return { props: serialize({ user, students, entities: allowedEntities, assignments, sessions, exams }) };
|
||||
}, sessionOptions);
|
||||
|
||||
interface Item {
|
||||
|
||||
@@ -48,7 +48,8 @@ export type RolePermission =
|
||||
"edit_assignment" |
|
||||
"delete_assignment" |
|
||||
"start_assignment" |
|
||||
"archive_assignment"
|
||||
"archive_assignment" |
|
||||
"view_entity_statistics"
|
||||
|
||||
export const DEFAULT_PERMISSIONS: RolePermission[] = [
|
||||
"view_students",
|
||||
@@ -109,4 +110,5 @@ export const ADMIN_PERMISSIONS: RolePermission[] = [
|
||||
"delete_assignment",
|
||||
"start_assignment",
|
||||
"archive_assignment",
|
||||
"view_entity_statistics"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user