({ rootGetters, state }, { course, members, classroom, sharedClassroomId })
| 145 | }, |
| 146 | |
| 147 | async assignCourse ({ rootGetters, state }, { course, members, classroom, sharedClassroomId }) { |
| 148 | const students = members.map(data => new User(data)) |
| 149 | |
| 150 | let courseInstance = state.courseInstanceByClassroom[classroom._id].find((ci) => ci.courseID === course._id) |
| 151 | if (courseInstance === undefined) { |
| 152 | courseInstance = new CourseInstance({ |
| 153 | courseID: course._id, |
| 154 | classroomID: classroom._id, |
| 155 | ownerID: classroom.ownerID, |
| 156 | aceConfig: {}, |
| 157 | }) |
| 158 | courseInstance.notyErrors = false |
| 159 | |
| 160 | await courseInstance.save() |
| 161 | } else { |
| 162 | courseInstance = new CourseInstance(courseInstance) |
| 163 | } |
| 164 | |
| 165 | // Automatically apply licenses to students if necessary |
| 166 | const prepaids = rootGetters['prepaids/getPrepaidsByTeacher'](classroom.ownerID) |
| 167 | const availablePrepaids = prepaids.available.map(data => new Prepaid(data)) |
| 168 | |
| 169 | const unenrolledStudents = students |
| 170 | .filter(user => !user.isEnrolled() || !user.prepaidIncludesCourse(course._id)) |
| 171 | |
| 172 | const totalSpotsAvailable = availablePrepaids.reduce((acc, prepaid) => { |
| 173 | if (prepaid.includesCourse(course._id)) { |
| 174 | return acc + prepaid.openSpots() |
| 175 | } else { |
| 176 | return acc |
| 177 | } |
| 178 | }, 0) |
| 179 | const canAssignCourses = totalSpotsAvailable >= unenrolledStudents.length |
| 180 | |
| 181 | if (!course.free && !canAssignCourses) { |
| 182 | const additionalLicensesNum = unenrolledStudents.length - totalSpotsAvailable |
| 183 | noty({ |
| 184 | text: `Oops! It looks like you need ${additionalLicensesNum} more license${additionalLicensesNum > 1 ? 's' : ''} to access the remaining chapters. Visit My Licenses to learn more!`, |
| 185 | layout: 'center', |
| 186 | type: 'error', |
| 187 | killer: true, |
| 188 | timeout: 5000 |
| 189 | }) |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | const numberEnrolled = unenrolledStudents.length |
| 194 | const courseName = utils.i18n(course, 'name') |
| 195 | if (numberEnrolled && !course.free) { |
| 196 | let confirmed = false |
| 197 | await new Promise((resolve) => noty({ |
| 198 | text: $.i18n.t('teachers.assign_course_confirm', { |
| 199 | numStudents: members.length, |
| 200 | courseName, |
| 201 | numberEnrolled, |
| 202 | }), |
| 203 | buttons: [ |
| 204 | { |
nothing calls this directly
no test coverage detected