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

Timed Assessment: Timer starts only when user click "Start" while initiating Attempt #7548

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -31,6 +31,8 @@ class Course::Assessment::Submission::SubmissionsController < \
staff: 'staff',
staff_w_phantom: 'staff_w_phantom' }.freeze

FORCE_SUBMIT_DELAY = 5.minutes

def index
authorize!(:view_all_submissions, @assessment)

Expand Down Expand Up @@ -105,6 +107,20 @@ def generate_live_feedback
render json: response_body, status: response_status
end

def set_timer_started_at
unless @submission.timer_started_at
@submission.timer_started_at = Time.zone.now

raise ActiveRecord::Rollback unless @submission.save

Course::Assessment::Submission::ForceSubmitTimedSubmissionJob.
set(wait_until: @submission.timer_started_at + @assessment.time_limit.minutes + FORCE_SUBMIT_DELAY).
perform_later(@assessment, @submission_id, @submission.creator)
end

render json: { timerStartedAt: @submission.timer_started_at }
end

# Reload the current answer or reset it, depending on parameters.
# current_answer has the most recent copy of the answer.
def reload_answer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def force_submit(submission, submitter)
User.with_stamper(submitter) do
ActiveRecord::Base.transaction do
submission.update!('finalise' => 'true')
submission.update!('finalise' => 'true') if submission.attempting?

Check warning on line 25 in app/jobs/course/assessment/submission/force_submit_timed_submission_job.rb

View check run for this annotation

Codecov / codecov/patch

app/jobs/course/assessment/submission/force_submit_timed_submission_job.rb#L25

Added line #L25 was not covered by tests
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/course/assessment/assessment_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def allow_read_material
def allow_create_assessment_submission
can :create, Course::Assessment::Submission,
experience_points_record: { course_user: { user_id: user.id } }
can [:update, :generate_live_feedback], Course::Assessment::Submission, assessment_submission_attempting_hash(user)
can [:update, :generate_live_feedback, :set_timer_started_at],
Course::Assessment::Submission, assessment_submission_attempting_hash(user)
end

def allow_update_own_assessment_answer
Expand Down
11 changes: 0 additions & 11 deletions app/models/course/assessment/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ class Course::Assessment::Submission < ApplicationRecord

acts_as_experience_points_record

FORCE_SUBMIT_DELAY = 5.minutes

after_save :auto_grade_submission, if: :submitted?
after_save :retrieve_codaveri_feedback, if: :submitted?
after_create :create_force_submission_job, if: :attempting?

workflow do
state :attempting do
Expand Down Expand Up @@ -235,14 +232,6 @@ def assigned_questions
extending(Course::Assessment::QuestionsConcern)
end

def create_force_submission_job
return unless assessment.time_limit

Course::Assessment::Submission::ForceSubmitTimedSubmissionJob.
set(wait_until: created_at + assessment.time_limit.minutes + FORCE_SUBMIT_DELAY).
perform_later(assessment, id, creator)
end

# The answers with current_answer flag set to true, filtering out orphaned answers to questions which are no longer
# assigned to the submission for randomized assessment.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ json.submission do
json.id submission.course_user.id
end

json.timerStartedAt submission.timer_started_at if assessment.time_limit

submitter_course_user = submission.creator.course_users.find_by(course: submission.assessment.course)
end_at = assessment.time_for(submitter_course_user).end_at
bonus_end_at = assessment.time_for(submitter_course_user).bonus_end_at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ json.assessment do
json.filesDownloadable @assessment.files_downloadable?
json.csvDownloadable @assessment.csv_downloadable?
json.passwordProtected @assessment.session_password_protected?
json.hasTimeLimit @assessment.time_limit
json.canViewLogs can? :manage, @assessment
json.canPublishGrades can? :publish_grades, @assessment
json.canForceSubmit can? :force_submit_assessment_submission, @assessment
Expand All @@ -36,6 +37,7 @@ json.submissions @course_users do |course_user|
json.workflowState submission.workflow_state
json.grade submission.grade.to_f
json.pointsAwarded submission.current_points_awarded
json.timerStartedAt submission.timer_started_at if @assessment.time_limit
json.dateSubmitted submission.submitted_at&.iso8601
json.dateGraded submission.graded_at&.iso8601
json.logCount submission.log_count
Expand Down
6 changes: 6 additions & 0 deletions client/app/api/course/Assessment/Submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export default class SubmissionsAPI extends BaseAssessmentAPI {
);
}

setTimerStartAt(submissionId) {
return this.client.patch(
`${this.#urlPrefix}/${submissionId}/set_timer_started_at`,
);
}

reloadAnswer(submissionId, params) {
return this.client.post(
`${this.#urlPrefix}/${submissionId}/reload_answer`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const AssessmentForm = (props: AssessmentFormProps): JSX.Element => {
handleSubmit,
setError,
watch,
formState: { errors, isDirty },
formState: { errors, isDirty, dirtyFields },
} = useFormValidation(initialValues);

const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ const translations = defineMessages({
defaultMessage:
'When enabled, each submission will have its own timer and will automatically be finalised when its timer ends.',
},
changingTimeLimitWarning: {
id: 'course.assessment.AssessmentForm.changingTimeLimitWarning',
defaultMessage:
'Changing the Time Limit will create inconsistencies for the submissions \
in progress',
},
gradingMode: {
id: 'course.assessment.AssessmentForm.gradingMode',
defaultMessage: 'Grading mode',
Expand Down
22 changes: 22 additions & 0 deletions client/app/bundles/course/assessment/submission/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ export function unsubmit(submissionId) {
};
}

export function setTimerStartAt(submissionId, setExamNotice, setTimerNotice) {
return (dispatch) => {
dispatch({ type: actionTypes.SET_TIMER_STARTED_AT_REQUEST });

return CourseAPI.assessment.submissions
.setTimerStartAt(submissionId)
.then((response) => response.data)
.then((data) => {
dispatch({
type: actionTypes.SET_TIMER_STARTED_AT_SUCCESS,
payload: data,
});
setExamNotice(false);
setTimerNotice(false);
})
.catch(() => {
dispatch({ type: actionTypes.SET_TIMER_STARTED_AT_FAILURE });
dispatch(setNotification(translations.startTimedExamAssessmentFailed));
});
};
}

export function mark(submissionId, grades, exp) {
const payload = {
submission: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FC, useState } from 'react';
import { useParams } from 'react-router-dom';
import {
Button,
Dialog,
Expand All @@ -8,11 +9,12 @@ import {
Typography,
} from '@mui/material';

import { useAppSelector } from 'lib/hooks/store';
import { useAppDispatch, useAppSelector } from 'lib/hooks/store';
import useTranslation from 'lib/hooks/useTranslation';

import { TIME_LAPSE_NEW_SUBMISSION_MS, workflowStates } from '../constants';
import { remainingTimeDisplay } from '../pages/SubmissionEditIndex/TimeLimitBanner';
import { setTimerStartAt } from '../actions';
import { workflowStates } from '../constants';
import RemainingTimeTranslations from '../pages/SubmissionEditIndex/components/RemainingTimeTranslation';
import { getAssessment } from '../selectors/assessments';
import { getSubmission } from '../selectors/submissions';
import translations from '../translations';
Expand All @@ -23,27 +25,33 @@ const WarningDialog: FC = () => {
const assessment = useAppSelector(getAssessment);
const submission = useAppSelector(getSubmission);

const dispatch = useAppDispatch();

const { timeLimit, passwordProtected: isExamMode } = assessment;
const { workflowState, attemptedAt } = submission;
const { workflowState, timerStartedAt } = submission;

const isAttempting = workflowState === workflowStates.Attempting;
const isTimedMode = isAttempting && !!timeLimit;

const startTime = new Date(attemptedAt).getTime();
const currentTime = new Date().getTime();
const isNewSubmission = isTimedMode && !timerStartedAt;

const submissionTimeLimitAt = isTimedMode
? startTime + timeLimit * 60 * 1000
: null;
const currentTime = new Date().getTime();

const isNewSubmission =
currentTime - startTime < TIME_LAPSE_NEW_SUBMISSION_MS;
const submissionTimeLimitAt =
isTimedMode && timerStartedAt
? new Date(timerStartedAt).getTime() + timeLimit * 60 * 1000
: null;

const [examNotice, setExamNotice] = useState(isExamMode);
const [timedNotice, setTimedNotice] = useState(isTimedMode);

const { submissionId } = useParams();
if (!submissionId) {
return null;
}

const remainingTime =
isTimedMode && submissionTimeLimitAt! > currentTime
isTimedMode && timerStartedAt && submissionTimeLimitAt! > currentTime
? submissionTimeLimitAt! - currentTime
: null;

Expand All @@ -53,28 +61,41 @@ const WarningDialog: FC = () => {
if (examNotice && timedNotice) {
dialogTitle = t(translations.timedExamDialogTitle, {
isNewSubmission,
remainingTime: remainingTimeDisplay(
isNewSubmission ? timeLimit! * 60 * 1000 : remainingTime ?? 0,
remainingTime: (
<RemainingTimeTranslations
remainingTime={
isNewSubmission ? timeLimit! * 60 * 1000 : remainingTime ?? 0
}
/>
),
stillSomeTimeRemaining: !!remainingTime,
});
dialogMessage = t(translations.timedExamDialogMessage, {
stillSomeTimeRemaining: !!remainingTime,

stillSomeTimeRemaining: isNewSubmission || !!remainingTime,
});
dialogMessage = isNewSubmission
? t(translations.timedExamStartDialogMessage)
: t(translations.timedExamDialogMessage, {
stillSomeTimeRemaining: !!remainingTime,
});
} else if (examNotice) {
dialogTitle = t(translations.examDialogTitle);
dialogMessage = t(translations.examDialogMessage);
} else if (timedNotice) {
dialogTitle = t(translations.timedAssessmentDialogTitle, {
isNewSubmission,
remainingTime: remainingTimeDisplay(
isNewSubmission ? timeLimit! * 60 * 1000 : remainingTime ?? 0,
remainingTime: (
<RemainingTimeTranslations
remainingTime={
isNewSubmission ? timeLimit! * 60 * 1000 : remainingTime ?? 0
}
/>
),
stillSomeTimeRemaining: !!remainingTime,
});
dialogMessage = t(translations.timedAssessmentDialogMessage, {
stillSomeTimeRemaining: !!remainingTime,
stillSomeTimeRemaining: isNewSubmission || !!remainingTime,
});
dialogMessage = isNewSubmission
? t(translations.timedAssessmentStartDialogMessage)
: t(translations.timedAssessmentDialogMessage, {
stillSomeTimeRemaining: !!remainingTime,
});
}

return (
Expand All @@ -89,11 +110,17 @@ const WarningDialog: FC = () => {
<Button
color="primary"
onClick={() => {
setExamNotice(false);
setTimedNotice(false);
if (isNewSubmission) {
dispatch(
setTimerStartAt(submissionId, setExamNotice, setTimedNotice),
);
} else {
setExamNotice(false);
setTimedNotice(false);
}
}}
>
{t(translations.ok)}
{t(translations.start, { isNewSubmission })}
</Button>
</DialogActions>
</Dialog>
Expand Down
9 changes: 5 additions & 4 deletions client/app/bundles/course/assessment/submission/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export const MEGABYTES_TO_BYTES = 1024 * 1024;

export const BUFFER_TIME_TO_FORCE_SUBMIT_MS = 5 * 1000;

// calculate how long has it passed since the student starts the submission
// to still be considered a "newly created" submission
export const TIME_LAPSE_NEW_SUBMISSION_MS = 10 * 1000;

export const POLL_INTERVAL_MILLISECONDS = 2000;

export const workflowStates = {
Expand Down Expand Up @@ -303,6 +299,11 @@ const actionTypes = mirrorCreator([

// Fetch annotations for single answer
'FETCH_ANNOTATION_SUCCESS',

// Set timer upon starting timed assessment
'SET_TIMER_STARTED_AT_REQUEST',
'SET_TIMER_STARTED_AT_SUCCESS',
'SET_TIMER_STARTED_AT_FAILURE',
]);

export default actionTypes;
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ const SubmissionForm: FC<Props> = (props) => {
const initialValues = useAppSelector(getInitialAnswer);

const { autograded, timeLimit, tabbedView, questionIds } = assessment;
const { workflowState, attemptedAt } = submission;
const { workflowState, timerStartedAt } = submission;

const maxInitialStep = submission.maxStep ?? questionIds.length - 1;

const submissionId = getSubmissionId();

const hasSubmissionTimeLimit =
workflowState === workflowStates.Attempting && timeLimit;
workflowState === workflowStates.Attempting && timeLimit && timerStartedAt;
const submissionTimeLimitAt = hasSubmissionTimeLimit
? new Date(attemptedAt).getTime() + timeLimit * 60 * 1000
? new Date(timerStartedAt).getTime() + timeLimit * 60 * 1000
: null;

const isNewSubmission =
workflowState === workflowStates.Attempting && timeLimit && !timerStartedAt;

const initialStep = Math.min(maxInitialStep, Math.max(0, step || 0));

const [maxStep, setMaxStep] = useState(maxInitialStep);
Expand Down Expand Up @@ -172,7 +175,7 @@ const SubmissionForm: FC<Props> = (props) => {
});

return (
<div className="mt-4">
<div className={`mt-4 ${isNewSubmission && 'blur-xl'}`}>
<FormProvider {...methods}>
<form
encType="multipart/form-data"
Expand Down
Loading