Merge pull request #1967 from edx/feature/cdodge/cap-num-enrollments
Add ability to cap number of enrollments in a course
This commit is contained in:
@@ -424,6 +424,28 @@ class CourseEnrollment(models.Model):
|
||||
|
||||
return enrollment
|
||||
|
||||
@classmethod
|
||||
def num_enrolled_in(cls, course_id):
|
||||
"""
|
||||
Returns the count of active enrollments in a course.
|
||||
|
||||
'course_id' is the course_id to return enrollments
|
||||
"""
|
||||
enrollment_number = CourseEnrollment.objects.filter(course_id=course_id, is_active=1).count()
|
||||
|
||||
return enrollment_number
|
||||
|
||||
@classmethod
|
||||
def is_course_full(cls, course):
|
||||
"""
|
||||
Returns a boolean value regarding whether a course has already reached it's max enrollment
|
||||
capacity
|
||||
"""
|
||||
is_course_full = False
|
||||
if course.max_student_enrollments_allowed is not None:
|
||||
is_course_full = cls.num_enrolled_in(course.location.course_id) >= course.max_student_enrollments_allowed
|
||||
return is_course_full
|
||||
|
||||
def update_enrollment(self, mode=None, is_active=None):
|
||||
"""
|
||||
Updates an enrollment for a user in a class. This includes options
|
||||
|
||||
@@ -561,6 +561,12 @@ def change_enrollment(request):
|
||||
if not has_access(user, course, 'enroll'):
|
||||
return HttpResponseBadRequest(_("Enrollment is closed"))
|
||||
|
||||
# see if we have already filled up all allowed enrollments
|
||||
is_course_full = CourseEnrollment.is_course_full(course)
|
||||
|
||||
if is_course_full:
|
||||
return HttpResponseBadRequest(_("Course is full"))
|
||||
|
||||
# If this course is available in multiple modes, redirect them to a page
|
||||
# where they can choose which mode they want.
|
||||
available_modes = CourseMode.modes_for_course(course_id)
|
||||
|
||||
@@ -13,7 +13,7 @@ from xmodule.seq_module import SequenceDescriptor, SequenceModule
|
||||
from xmodule.graders import grader_from_conf
|
||||
import json
|
||||
|
||||
from xblock.fields import Scope, List, String, Dict, Boolean
|
||||
from xblock.fields import Scope, List, String, Dict, Boolean, Integer
|
||||
from .fields import Date
|
||||
from xmodule.modulestore.locator import CourseLocator
|
||||
from django.utils.timezone import UTC
|
||||
@@ -384,6 +384,9 @@ class CourseFields(object):
|
||||
display_coursenumber = String(help="An optional display string for the course number that will get rendered in the LMS",
|
||||
scope=Scope.settings)
|
||||
|
||||
max_student_enrollments_allowed = Integer(help="Limit the number of students allowed to enroll in this course.",
|
||||
scope=Scope.settings)
|
||||
|
||||
class CourseDescriptor(CourseFields, SequenceDescriptor):
|
||||
module_class = SequenceModule
|
||||
|
||||
|
||||
Reference in New Issue
Block a user