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

Adds warning when setting same quiz to class multiple times #1071

Merged
merged 3 commits into from
Sep 10, 2024
Merged
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
26 changes: 20 additions & 6 deletions src/app/components/elements/modals/QuizSettingModal.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, {ChangeEvent, useState} from "react";
import {ContentSummaryDTO, IsaacQuizDTO, QuizFeedbackMode} from "../../../../IsaacApiTypes";
import {ContentSummaryDTO, IsaacQuizDTO, QuizAssignmentDTO, QuizFeedbackMode} from "../../../../IsaacApiTypes";
import {
AppDispatch,
closeActiveModal,
selectors,
useAppDispatch,
useAppSelector,
useAssignQuizMutation,
useGetGroupsQuery
useGetGroupsQuery,
useGetQuizAssignmentsSetByMeQuery,
} from "../../../state";
import {assignMultipleQuiz, isDefined, Item, selectOnChange, TODAY} from "../../../services";
import range from "lodash/range";
Expand All @@ -16,6 +17,7 @@ import {IsaacSpinner} from "../../handlers/IsaacSpinner";
import {ShowLoadingQuery} from "../../handlers/ShowLoadingQuery";
import {StyledSelect} from "../inputs/StyledSelect";
import {Button, FormFeedback, Label, UncontrolledTooltip} from "reactstrap";
import { AppGroup } from "../../../../IsaacAppTypes";


type QuizFeedbackOption = Item<QuizFeedbackMode>;
Expand Down Expand Up @@ -58,6 +60,7 @@ export function QuizSettingModal({quiz, dueDate: initialDueDate, scheduledStartD
const [dueDate, setDueDate] = useState<Date | null>(initialDueDate ?? null);
const [scheduledStartDate, setScheduledStartDate] = useState<Date | null>(initialScheduledStartDate ?? null);
const [feedbackMode, setFeedbackMode] = useState<QuizFeedbackMode | null>(initialFeedbackMode ?? null);
const {data: quizAssignments} = useGetQuizAssignmentsSetByMeQuery();

const yearRange = range(currentYear, currentYear + 5);

Expand Down Expand Up @@ -86,7 +89,10 @@ export function QuizSettingModal({quiz, dueDate: initialDueDate, scheduledStartD
});
}

const groupInvalid = validated.has('group') && selectedGroups.length === 0;
const isAssignmentSetToThisGroup = (group: Item<number>, assignment?: QuizAssignmentDTO) => assignment ? (assignment.quizId === quiz.id && assignment.groupId === group.value && (assignment.dueDate ? assignment.dueDate.valueOf() < Date.now() : true)) : false;
const alreadyAssignedToAGroup = selectedGroups.some(group => quizAssignments?.some(assignment => isAssignmentSetToThisGroup(group, assignment)));

const groupInvalid = validated.has('group') && selectedGroups.length === 0 || alreadyAssignedToAGroup;
const dueDateInvalid = isDefined(dueDate) && ((scheduledStartDate ? scheduledStartDate.valueOf() > dueDate.valueOf() : false) || dueDate.valueOf() < Date.now());
const scheduledStartDateInvalid = isDefined(scheduledStartDate) && scheduledStartDate.valueOf() < TODAY().valueOf();
const feedbackModeInvalid = validated.has('feedbackMode') && feedbackMode === null;
Expand All @@ -99,7 +105,11 @@ export function QuizSettingModal({quiz, dueDate: initialDueDate, scheduledStartD
query={groupsQuery}
defaultErrorTitle={"Error fetching groups"}
thenRender={groups => {
const groupOptions: Item<number>[] = groups.map(g => ({label: g.groupName as string, value: g.id as number}));
function appGroupToItem(g: AppGroup): Item<number> {return {label: g.groupName as string, value: g.id as number}; }
function quizToAssignment(g: AppGroup): QuizAssignmentDTO | undefined { return quizAssignments?.find(assignment => assignment.quizId === quiz.id && assignment.groupId === g.id); }

const groupOptions = groups.map((g: AppGroup) => ({...appGroupToItem(g), isDisabled: isAssignmentSetToThisGroup(appGroupToItem(g), quizToAssignment(g))}));

return <StyledSelect isMulti placeholder="Select groups"
options={groupOptions}
onChange={(s) => {
Expand All @@ -111,13 +121,17 @@ export function QuizSettingModal({quiz, dueDate: initialDueDate, scheduledStartD
isSearchable
menuPortalTarget={document.body}
styles={{
option: (base, selectProps) => ({
...base,
backgroundColor: selectProps.isFocused && selectProps.isDisabled ? 'transparent' : base.backgroundColor
}),
control: (styles) => ({...styles, ...(groupInvalid ? {borderColor: '#dc3545'} : {})}),
menuPortal: base => ({...base, zIndex: 9999}),
}}
/>;
}}
/>
{groupInvalid && <FormFeedback className="d-block" valid={false}>You must select a group</FormFeedback>}
{groupInvalid && (selectedGroups.length === 0 ? <FormFeedback className="d-block" valid={false}>You must select a group</FormFeedback> : <FormFeedback className="d-block" valid={false}>You cannot reassign a test to this group(s) until the due date has passed.</FormFeedback>)}
</Label>
<Label className="w-100 mb-4">What level of feedback should students get:<br/>
<StyledSelect
Expand Down Expand Up @@ -168,7 +182,7 @@ export function QuizSettingModal({quiz, dueDate: initialDueDate, scheduledStartD
</Button>
<Button
className={"float-end mb-4"}
disabled={selectedGroups.length === 0 || !feedbackMode || isAssigning || dueDateInvalid || scheduledStartDateInvalid}
disabled={groupInvalid || !feedbackMode || isAssigning || dueDateInvalid || scheduledStartDateInvalid}
onMouseEnter={() => setValidated(new Set(['group', 'feedbackMode']))}
onClick={assign}
>
Expand Down