SubmissionStateMap.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import _ from 'underscore';
  19. import GradingPeriodsHelper from 'jsx/grading/helpers/GradingPeriodsHelper';
  20. function submissionGradingPeriodInformation (assignment, student) {
  21. const submissionInfo = assignment.effectiveDueDates[student.id] || {};
  22. return {
  23. gradingPeriodID: submissionInfo.grading_period_id,
  24. inClosedGradingPeriod: submissionInfo.in_closed_grading_period
  25. };
  26. }
  27. function visibleToStudent (assignment, student) {
  28. if (!assignment.only_visible_to_overrides) return true;
  29. return _.contains(assignment.assignment_visibility, student.id);
  30. }
  31. function gradingPeriodInfoForCell (assignment, student, selectedGradingPeriodID) {
  32. const specificPeriodSelected = !GradingPeriodsHelper.isAllGradingPeriods(selectedGradingPeriodID);
  33. const { gradingPeriodID, inClosedGradingPeriod } = submissionGradingPeriodInformation(assignment, student);
  34. const inNoGradingPeriod = !gradingPeriodID;
  35. const inOtherGradingPeriod = !!gradingPeriodID && specificPeriodSelected &&
  36. selectedGradingPeriodID !== gradingPeriodID;
  37. return {
  38. inNoGradingPeriod,
  39. inOtherGradingPeriod,
  40. inClosedGradingPeriod
  41. };
  42. }
  43. function cellMappingsForMultipleGradingPeriods (assignment, student, selectedGradingPeriodID, isAdmin) {
  44. const specificPeriodSelected = !GradingPeriodsHelper.isAllGradingPeriods(selectedGradingPeriodID);
  45. const { gradingPeriodID, inClosedGradingPeriod } = submissionGradingPeriodInformation(assignment, student);
  46. const gradingPeriodInfo = gradingPeriodInfoForCell(assignment, student, selectedGradingPeriodID);
  47. let cellMapping;
  48. if (specificPeriodSelected && !gradingPeriodID) {
  49. cellMapping = { locked: true, hideGrade: true };
  50. } else if (specificPeriodSelected && selectedGradingPeriodID !== gradingPeriodID) {
  51. cellMapping = { locked: true, hideGrade: true };
  52. } else if (!isAdmin && inClosedGradingPeriod) {
  53. cellMapping = { locked: true, hideGrade: false };
  54. } else {
  55. cellMapping = { locked: false, hideGrade: false };
  56. }
  57. return { ...cellMapping, ...gradingPeriodInfo };
  58. }
  59. function cellMapForSubmission (assignment, student, hasGradingPeriods, selectedGradingPeriodID, isAdmin) {
  60. if (!assignment.published) {
  61. return { locked: true, hideGrade: true };
  62. } else if (!visibleToStudent(assignment, student)) {
  63. return { locked: true, hideGrade: true };
  64. } else if (hasGradingPeriods) {
  65. return cellMappingsForMultipleGradingPeriods(assignment, student, selectedGradingPeriodID, isAdmin);
  66. } else {
  67. return { locked: false, hideGrade: false };
  68. }
  69. }
  70. function missingSubmission (student, assignment) {
  71. const submission = {
  72. assignment_id: assignment.id,
  73. user_id: student.id,
  74. excused: false,
  75. late: false,
  76. missing: false,
  77. seconds_late: 0
  78. };
  79. const dueDates = assignment.effectiveDueDates[student.id] || {};
  80. if (dueDates.due_at != null && new Date(dueDates.due_at) < new Date()) {
  81. submission.missing = true;
  82. }
  83. return submission;
  84. }
  85. class SubmissionStateMap {
  86. constructor ({ hasGradingPeriods, selectedGradingPeriodID, isAdmin }) {
  87. this.hasGradingPeriods = hasGradingPeriods;
  88. this.selectedGradingPeriodID = selectedGradingPeriodID;
  89. this.isAdmin = isAdmin;
  90. this.submissionCellMap = {};
  91. this.submissionMap = {};
  92. }
  93. setup (students, assignments) {
  94. students.forEach((student) => {
  95. this.submissionCellMap[student.id] = {};
  96. this.submissionMap[student.id] = {};
  97. _.each(assignments, (assignment) => {
  98. this.setSubmissionCellState(student, assignment, student[`assignment_${assignment.id}`]);
  99. });
  100. });
  101. }
  102. setSubmissionCellState (student, assignment, submission) {
  103. this.submissionMap[student.id][assignment.id] = submission || missingSubmission(student, assignment);
  104. const params = [
  105. assignment,
  106. student,
  107. this.hasGradingPeriods,
  108. this.selectedGradingPeriodID,
  109. this.isAdmin
  110. ];
  111. this.submissionCellMap[student.id][assignment.id] = cellMapForSubmission(...params);
  112. }
  113. getSubmission (userId, assignmentId) {
  114. return (this.submissionMap[userId] || {})[assignmentId];
  115. }
  116. getSubmissionState ({ user_id: userId, assignment_id: assignmentId }) {
  117. return (this.submissionCellMap[userId] || {})[assignmentId];
  118. }
  119. }
  120. export default SubmissionStateMap;