forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenrollments_from_user_list.rb
57 lines (50 loc) · 1.91 KB
/
enrollments_from_user_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class EnrollmentsFromUserList
class << self
def process(list, course, opts={})
EnrollmentsFromUserList.new(course, opts).process(list)
end
end
attr_reader :students, :course
def initialize(course, opts={})
@course = course
@enrollment_type = opts[:enrollment_type] || 'StudentEnrollment'
@role_name = opts[:role_name]
@limit = opts[:limit]
@section = (opts[:course_section_id].present? ? @course.course_sections.active.find_by_id(opts[:course_section_id].to_i) : nil) || @course.default_section
@limit_privileges_to_course_section = opts[:limit_privileges_to_course_section]
@enrolled_users = {}
end
def process(list)
raise ArgumentError, "Must provide a UserList" unless list.is_a?(UserList)
@enrollments = []
list.addresses.slice!(0,@limit) if @limit
list.users.each { |user| enroll_user(user) }
@enrollments
end
protected
def enroll_user(user)
return unless user
return if @enrolled_users.has_key?(user.id)
@enrolled_users[user.id] = true
@course.enroll_user(user, @enrollment_type, :section => @section, :limit_privileges_to_course_section => @limit_privileges_to_course_section, :role_name => @role_name).tap do |e|
@enrollments << e if e
end
end
end