GradingPeriodsHelper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. function validateDate(date, nullAllowed = false) {
  20. let valid = _.isDate(date);
  21. if (nullAllowed && !valid) {
  22. valid = date === null;
  23. }
  24. if (!valid) throw new Error(`\`${date}\` must be a Date or null`);
  25. }
  26. function validateGradingPeriodDates(gradingPeriods) {
  27. if (gradingPeriods == null) throw new Error(`\'${gradingPeriods}\' must be an array or object`);
  28. const dates = ["startDate", "endDate", "closeDate"];
  29. const periods = _.isArray(gradingPeriods) ? gradingPeriods : [gradingPeriods];
  30. _.each(periods, function(period) {
  31. _.each(dates, date => validateDate(period[date]));
  32. });
  33. return periods;
  34. }
  35. function validatePeriodID(id) {
  36. const valid = _.isString(id);
  37. if (!valid) throw new Error(`Grading period id \`${id}\` must be a String`);
  38. }
  39. class GradingPeriodsHelper {
  40. constructor(gradingPeriods) {
  41. this.gradingPeriods = validateGradingPeriodDates(gradingPeriods);
  42. }
  43. static isAllGradingPeriods(periodID) {
  44. validatePeriodID(periodID);
  45. return periodID === "0";
  46. }
  47. get earliestValidDueDate() {
  48. const orderedPeriods = _.sortBy(this.gradingPeriods, "startDate");
  49. const earliestOpenPeriod = _.find(orderedPeriods, { isClosed: false });
  50. if (earliestOpenPeriod) {
  51. return earliestOpenPeriod.startDate;
  52. } else {
  53. return null;
  54. }
  55. }
  56. gradingPeriodForDueAt(dueAt) {
  57. validateDate(dueAt, true);
  58. return _.find(this.gradingPeriods, (period) => {
  59. return this.isDateInGradingPeriod(dueAt, period.id, false);
  60. }) || null;
  61. }
  62. isDateInGradingPeriod(date, gradingPeriodID, runValidations=true) {
  63. if (runValidations) {
  64. validateDate(date, true);
  65. validatePeriodID(gradingPeriodID);
  66. }
  67. const gradingPeriod = _.find(this.gradingPeriods, { id: gradingPeriodID });
  68. if (!gradingPeriod) throw new Error(`No grading period has id \`${gradingPeriodID}\``);
  69. if (date === null) {
  70. return gradingPeriod.isLast;
  71. } else {
  72. return gradingPeriod.startDate < date && date <= gradingPeriod.endDate;
  73. }
  74. }
  75. isDateInClosedGradingPeriod(date) {
  76. const period = this.gradingPeriodForDueAt(date);
  77. return !!period && period.isClosed;
  78. }
  79. }
  80. export default GradingPeriodsHelper