-
Notifications
You must be signed in to change notification settings - Fork 86
/
grades.js
309 lines (284 loc) · 9.69 KB
/
grades.js
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
297
298
299
300
301
302
303
304
305
306
307
308
309
/* eslint-disable import/no-self-import */
import { StrictDict } from 'utils';
import { Headings, GradeFormats } from 'data/constants/grades';
import { formatDateForDisplay } from 'data/actions/utils';
import { getLocalizedSlash } from 'i18n/utils';
import simpleSelectorFactory from '../utils';
import * as module from './grades';
/**
* getRowsProcessed(historyEntryData)
* @param {object} historyEntryData - the data param from a bulk management history entry
* @return {string} - user-facing summary of total/skipped/failed rows.
*/
export const getRowsProcessed = ({
processed_rows: processed,
saved_rows: saved,
total_rows: total,
}) => {
const failed = processed - saved;
const skipped = total - processed;
const summaryEntries = [`${total} Students: ${saved} processed`];
if (skipped > 0) { summaryEntries.push(`${skipped} skipped`); }
if (failed > 0) { summaryEntries.push(`${failed} failed`); }
return summaryEntries.join(', ');
};
/**
* formatGradeOverrideForDisplay(historyArray)
* returns the grade override history results in display format.
* @param {object[]} historyArray - array of gradeOverrideHistoryResults
* @return {object[]} - display-formatted history results ({ date, grader, reason, adjustedGrade })
*/
export const formatGradeOverrideForDisplay = historyArray => historyArray.map(item => ({
date: formatDateForDisplay(new Date(item.history_date)),
grader: item.history_user,
reason: item.override_reason,
adjustedGrade: item.earned_graded_override,
}));
export const minGrade = '0';
export const maxGrade = '100';
/**
* formatMaxCourseGrade(percentGrade)
* Takes a percent grade and returns it unless it is equal to the max grade
* @param {string} percentGrade - grade percentage
* @return {string} percent grade or null
*/
export const formatMaxCourseGrade = (percentGrade) => (
(percentGrade === maxGrade) ? null : percentGrade
);
/**
* formatMinCourseGrade(percentGrade)
* Takes a percent grade and returns it unless it is equal to the min grade
* @param {string} percentGrade - grade percentage
* @return {string} percent grade or null
*/
export const formatMinCourseGrade = (percentGrade) => (
(percentGrade === minGrade) ? null : percentGrade
);
/**
* formatMaxAssignmentGrade(percentGrade, options)
* Takes a percent grade and returns it unless it is equal to the max grade or
* the assignment id is set.
* @param {string} percentGrade - grade percentage
* @param {object} options - options object ({ assignmentId });
* @return {string} percent grade or null
*/
export const formatMaxAssignmentGrade = (percentGrade, options) => (
(percentGrade === maxGrade || !options.assignmentId) ? null : percentGrade
);
/**
* formatMinAssignmentGrade(percentGrade, options)
* Takes a percent grade and returns it unless it is equal to the min grade or
* the assignment id is set.
* @param {string} percentGrade - grade percentage
* @param {object} options - options object ({ assignmentId });
* @return {string} percent grade or null
*/
export const formatMinAssignmentGrade = (percentGrade, options) => (
(percentGrade === minGrade || !options.assignmentId) ? null : percentGrade
);
/**
* headingMapper(category, label='All')
* Takes category and label filters and returns a method that will take a section breakdown
* and return the appropriate table headings.
* @param {string} category - assignment filter type
* @param {string} label - assignment filter label
* @return {string[]} - list of table headers
*/
export const headingMapper = (category, label = 'All') => {
const filters = {
all: section => section.label,
byCategory: section => section.label && section.category === category,
byLabel: section => section.label && section.label === label,
};
let filter;
if (label === 'All') {
filter = category === 'All' ? filters.all : filters.byCategory;
} else {
filter = filters.byLabel;
}
const {
username,
fullName,
email,
totalGrade,
} = Headings;
const filteredLabels = (entry) => entry.filter(filter).map(s => s.label);
return (entry) => (
entry
? [username, fullName, email, ...filteredLabels(entry), totalGrade]
: []
);
};
/**
* transformHistoryEntry(rawEntry)
* Takes a raw bulkManagementHistory entry and formats it for consumption
* @param {object} rawEntry - raw history entry to transform
* @return {object} - transformed history entry
* ({ timeUploaded, originalFilename, resultsSummary, ... })
*/
export const transformHistoryEntry = ({
modified,
original_filename: originalFilename,
data,
unique_id: courseId,
id,
...rest
}) => ({
timeUploaded: formatDateForDisplay(new Date(modified)),
originalFilename,
resultsSummary: {
rowId: id,
text: module.getRowsProcessed(data),
},
...rest,
});
/**
* roundGrade(val)
* Takes a number and rounds it to 2 decimal places
* defaults to 0
* @param {number=0} val - value to round.
* @return {number} - rounded number
*/
export const roundGrade = val => parseFloat((val || 0).toFixed(2));
export const subsectionGrade = StrictDict({
/**
* subsectionGrade.absolute(subsection)
* returns rounded {earned}/{possible} if attempted, else ${earned}
* @param {object} subsection - grade subsection entry
* @return {string} - absolute-formatted subsection grade string
*/
[GradeFormats.absolute]: (subsection) => {
const earned = module.roundGrade(subsection.score_earned);
const possible = module.roundGrade(subsection.score_possible);
return subsection.attempted ? `${earned}${getLocalizedSlash()}${possible}` : `${earned}`;
},
/**
* subsectionGrade.percent(subsection)
* returns rounded percent times 100
* @param {object} subsection - grade subsection entry
* @return {string} - percent-formatted subsection grade string
*/
[GradeFormats.percent]: (subsection) => (
module.roundGrade(subsection.percent * 100)
),
});
// Selectors
/**
* allGrades(state)
* returns the top-level redux grades state.
* @param {object} state - redux state
* @return {object} - redux grades state
*/
export const allGrades = ({ grades: { results } }) => results;
/**
* bulkImportError(state)
* returns the stringified bulkManagement import error messages.
* @param {object} state - redux state
* @return {string} - bulk import error messages joined into a display form
* (or empty string if there are none)
*/
export const bulkImportError = ({ grades: { bulkManagement } }) => (
(!!bulkManagement && bulkManagement.errorMessages)
? `Errors while processing: ${bulkManagement.errorMessages.join('; ')};`
: ''
);
/**
* bulkManagementHistory(state)
* returns the bulkManagement history entries from the grades state
* @param {object} state - redux state
* @return {object[]} - list of bulkManagement history entries
*/
export const bulkManagementHistory = ({ grades: { bulkManagement } }) => (
(bulkManagement.history || [])
);
/**
* bulkManagementHistoryEntries(state)
* returns transformed history entries from bulkManagement grades data.
* @param {object} state - redux state
* @return {object[]} - list of transformed bulkManagement history entries
*/
export const bulkManagementHistoryEntries = (state) => (
module.bulkManagementHistory(state).map(module.transformHistoryEntry)
);
/**
* getExampleSectionBreakdown(state)
* returns section breakdown of first grades result.
* @param {object} state - redux state
* @return {object[]} - section breakdown of first grades result.
*/
export const getExampleSectionBreakdown = ({ grades }) => (
(grades.results[0] || {}).section_breakdown || []
);
/**
* gradeOverrides(state)
* returns the gradeOverride history results
* @param {object} state - redux state
* @return {object[]} - grade override history result entries
*/
export const gradeOverrides = ({ grades }) => grades.gradeOverrideHistoryResults;
/**
* hasOverrideErrors(state)
* returns true iff gradeOverrideHistoryErrors are present
* @param {object} state - redux state
* @return {bool} - are there grade override history errors?
*/
export const hasOverrideErrors = ({ grades }) => !!grades.gradeOverrideHistoryError;
/**
* uploadSuccess(state)
* @param {object} state - redux state
* @return {bool} - is bulkManagement.uploadSuccess true?
*/
export const uploadSuccess = ({ grades: { bulkManagement } }) => (
!!bulkManagement && bulkManagement.uploadSuccess
);
const simpleSelectors = simpleSelectorFactory(
({ grades }) => grades,
[
'courseId',
'filteredUsersCount',
'totalUsersCount',
'gradeFormat',
'nextPage',
'prevPage',
'showSpinner',
'gradeOverrideCurrentEarnedGradedOverride',
'gradeOverrideHistoryError',
'gradeOriginalEarnedGraded',
'gradeOriginalPossibleGraded',
'showSuccess',
],
);
const gradeData = ({ grades }) => ({
courseId: grades.courseId,
filteredUsersCount: grades.filteredUsersCount,
totalUsersCount: grades.totalUsersCount,
gradeFormat: grades.gradeFormat,
showSpinner: grades.showSpinner,
gradeOverrideCurrentEarnedGradedOverride: grades.gradeOverrideCurrentEarnedGradedOverride,
gradeOverrideHistoryError: grades.gradeOverrideHistoryError,
gradeOverrideHistoryResults: grades.gradeOverrideHistoryResults,
gradeOriginalEarnedGraded: grades.gradeOriginalEarnedGraded,
gradeOriginalPossibleGraded: grades.gradeOriginalPossibleGraded,
nextPage: grades.nextPage,
prevPage: grades.prevPage,
showSuccess: grades.showSuccess,
});
export default StrictDict({
bulkImportError,
formatGradeOverrideForDisplay,
formatMinAssignmentGrade,
formatMaxAssignmentGrade,
formatMaxCourseGrade,
formatMinCourseGrade,
hasOverrideErrors,
headingMapper,
roundGrade,
subsectionGrade,
...simpleSelectors,
gradeData,
allGrades,
bulkManagementHistoryEntries,
getExampleSectionBreakdown,
gradeOverrides,
uploadSuccess,
});