forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmissionStateMap.js
More file actions
121 lines (105 loc) · 4.5 KB
/
Copy pathSubmissionStateMap.js
File metadata and controls
121 lines (105 loc) · 4.5 KB
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
/*
* Copyright (C) 2016 - present Instructure, Inc.
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import _ from 'underscore';
import GradingPeriodsHelper from 'jsx/grading/helpers/GradingPeriodsHelper';
const TOOLTIP_KEYS = {
UNPUBLISHED_ASSIGNMENT: 'unpublished_assignment',
NOT_IN_ANY_GP: 'not_in_any_grading_period',
IN_ANOTHER_GP: 'in_another_grading_period',
IN_CLOSED_GP: 'in_closed_grading_period',
NONE: null
};
function submissionGradingPeriodInformation (assignment, student) {
const submissionInfo = assignment.effectiveDueDates[student.id] || {};
return {
gradingPeriodID: submissionInfo.grading_period_id,
inClosedGradingPeriod: submissionInfo.in_closed_grading_period
};
}
function visibleToStudent (assignment, student) {
if (!assignment.only_visible_to_overrides) return true;
return _.contains(assignment.assignment_visibility, student.id);
}
function cellMappingsForMultipleGradingPeriods (assignment, student, selectedGradingPeriodID, isAdmin) {
const specificPeriodSelected = !GradingPeriodsHelper.isAllGradingPeriods(selectedGradingPeriodID);
const { gradingPeriodID, inClosedGradingPeriod } = submissionGradingPeriodInformation(assignment, student);
if (specificPeriodSelected && !gradingPeriodID) {
return { locked: true, hideGrade: true, tooltip: TOOLTIP_KEYS.NOT_IN_ANY_GP };
} else if (specificPeriodSelected && selectedGradingPeriodID !== gradingPeriodID) {
return { locked: true, hideGrade: true, tooltip: TOOLTIP_KEYS.IN_ANOTHER_GP };
} else if (!isAdmin && inClosedGradingPeriod) {
return { locked: true, hideGrade: false, tooltip: TOOLTIP_KEYS.IN_CLOSED_GP };
} else {
return { locked: false, hideGrade: false, tooltip: TOOLTIP_KEYS.NONE };
}
}
function cellMapForSubmission (assignment, student, hasGradingPeriods, selectedGradingPeriodID, isAdmin) {
if (!assignment.published) {
return { locked: true, hideGrade: true, tooltip: TOOLTIP_KEYS.UNPUBLISHED_ASSIGNMENT };
} else if (!visibleToStudent(assignment, student)) {
return { locked: true, hideGrade: true, tooltip: TOOLTIP_KEYS.NONE };
} else if (hasGradingPeriods) {
return cellMappingsForMultipleGradingPeriods(assignment, student, selectedGradingPeriodID, isAdmin);
} else {
return { locked: false, hideGrade: false, tooltip: TOOLTIP_KEYS.NONE };
}
}
function missingSubmission (student, assignment) {
const submission = { assignment_id: assignment.id, user_id: student.id, missing: false };
const dueDates = assignment.effectiveDueDates[student.id] || {};
if (dueDates.due_at != null && new Date(dueDates.due_at) < new Date()) {
submission.missing = true;
}
return submission;
}
class SubmissionStateMap {
constructor ({ hasGradingPeriods, selectedGradingPeriodID, isAdmin }) {
this.hasGradingPeriods = hasGradingPeriods;
this.selectedGradingPeriodID = selectedGradingPeriodID;
this.isAdmin = isAdmin;
this.submissionCellMap = {};
this.submissionMap = {};
}
setup (students, assignments) {
students.forEach((student) => {
this.submissionCellMap[student.id] = {};
this.submissionMap[student.id] = {};
_.each(assignments, (assignment) => {
this.setSubmissionCellState(student, assignment, student[`assignment_${assignment.id}`]);
});
});
}
setSubmissionCellState (student, assignment, submission) {
this.submissionMap[student.id][assignment.id] = submission || missingSubmission(student, assignment);
const params = [
assignment,
student,
this.hasGradingPeriods,
this.selectedGradingPeriodID,
this.isAdmin
];
this.submissionCellMap[student.id][assignment.id] = cellMapForSubmission(...params);
}
getSubmission (userId, assignmentId) {
return (this.submissionMap[userId] || {})[assignmentId];
}
getSubmissionState ({ user_id: userId, assignment_id: assignmentId }) {
return (this.submissionCellMap[userId] || {})[assignmentId];
}
}
export default SubmissionStateMap;