Source

api/course/CourseCollection.methods.ts

  1. import { CallPromiseMixin } from 'meteor/didericis:callpromise-mixin';
  2. import { ValidatedMethod } from 'meteor/mdg:validated-method';
  3. import { Courses } from './CourseCollection';
  4. import { CourseInstances } from './CourseInstanceCollection';
  5. import { AcademicTerms } from '../academic-term/AcademicTermCollection';
  6. import { nextAcademicTerm } from '../academic-term/AcademicTermUtilities';
  7. import { RadGradProperties } from '../radgrad/RadGradProperties';
  8. /**
  9. * Returns an array with two elements: a string with the shortName of the academicTerm, and an integer indicating the
  10. * current planned enrollment for the course in that academicTerm.
  11. * @param courseID The ID of the course.
  12. * @param termID The ID of the academicTerm.
  13. * @memberOf api/course
  14. */
  15. const getEnrollmentData = (courseID, termID) => {
  16. const academicTermShortName = AcademicTerms.getShortName(termID);
  17. const enrollment = CourseInstances.getCollection().find({ termID, courseID }).count();
  18. return [academicTermShortName, enrollment];
  19. };
  20. /**
  21. * Given a courseID, returns enrollment data for the upcoming 9 academicTerms.
  22. * The returned data is an object with fields courseID and enrollmentData.
  23. * CourseID is the course ID.
  24. * EnrollmentData is an array of arrays. Each interior array is a tuple: a string containing the shortname and an
  25. * integer indicating the enrollment data.
  26. * @memberOf api/course
  27. * @example
  28. * { courseID: 'xghuyf2132q3',
  29. * enrollmentData: [['Sp19', 0], ['Su19', 1], ['Fa19', 5],
  30. * ['Sp20', 25], ['Su20', 2], ['Fa20', 0],
  31. * ['Sp21', 1], ['Su21', 0], ['Fa21', 1]]
  32. * }
  33. */
  34. export const getFutureEnrollmentMethod = new ValidatedMethod({
  35. name: 'CourseCollection.getFutureEnrollment',
  36. mixins: [CallPromiseMixin],
  37. validate: null,
  38. run(courseID) {
  39. if (Meteor.isServer) {
  40. // Throw error if an invalid courseID is passed.
  41. Courses.assertDefined(courseID);
  42. // Create an array of the upcoming 9 academicTerms after the current academicTerm.
  43. let academicTermDoc = AcademicTerms.getCurrentAcademicTermDoc();
  44. let termsPerYear = 3;
  45. if (RadGradProperties.getQuarterSystem()) {
  46. termsPerYear = 4;
  47. }
  48. const academicTermList = [];
  49. for (let i = 0; i < 3 * termsPerYear; i++) {
  50. academicTermDoc = nextAcademicTerm(academicTermDoc);
  51. academicTermList.push(academicTermDoc);
  52. }
  53. // Map over these academicTerms and return a new list that includes the enrollment data for this course and academicTerm.
  54. const enrollmentData = academicTermList.map((doc) => getEnrollmentData(courseID, AcademicTerms.getID(doc)));
  55. return { courseID, enrollmentData };
  56. }
  57. return null;
  58. },
  59. });