Class: CourseUser

Inherits:
ApplicationRecord show all
Includes:
LevelProgressConcern, StaffConcern, TodoConcern
Defined in:
app/models/course_user.rb

Defined Under Namespace

Modules: AchievementsConcern, LevelProgressConcern, StaffConcern, TodoConcern

Constant Summary collapse

STAFF_ROLES =

A set of roles which comprise the staff of a course, including the observer.

Set[:teaching_assistant, :manager, :owner, :observer].freeze
TEACHING_STAFF_ROLES =

A set of roles which comprise of the teaching staff of a course.

Set[:teaching_assistant, :manager, :owner].freeze
TA_AND_MANAGER_ROLES =

A set of roles which comprise the teaching assistants and managers of a course.

Set[:teaching_assistant, :manager].freeze
MANAGER_ROLES =

A set of roles which comprise the managers of a course.

Set[:manager, :owner].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TodoConcern

#create_todos_for_course_user, #delete_todos

Methods included from LevelProgressConcern

#current_level, #level_progress_percentage

Methods included from StaffConcern

#average_marking_time, #marking_time_stddev, #published_submissions

Instance Attribute Details

#achievement_countObject (readonly)

Returns the total number of achievements obtained by CourseUser in this course


56
57
58
59
# File 'app/models/course_user.rb', line 56

calculated :achievement_count, (lambda do
  Course::UserAchievement.select("count('*')").
    where('course_user_achievements.course_user_id = course_users.id')
end)

#experience_pointsObject (readonly)

Sums the total experience points for the course user. Default value is 0 when CourseUser does not have Course::ExperiencePointsRecord


41
42
43
44
# File 'app/models/course_user.rb', line 41

calculated :experience_points, (lambda do
  Course::ExperiencePointsRecord.selecting { coalesce(sum(points_awarded), 0) }.
    where('course_experience_points_records.course_user_id = course_users.id')
end)

#last_experience_points_recordObject (readonly)

Returns the time of the last awarded experience points record.


48
49
50
51
52
# File 'app/models/course_user.rb', line 48

calculated :last_experience_points_record, (lambda do
  Course::ExperiencePointsRecord.select(:awarded_at).limit(1).order(awarded_at: :desc).
    where('course_experience_points_records.course_user_id = course_users.id').
    where('course_experience_points_records.awarded_at IS NOT NULL')
end)

#last_obtained_achievementObject (readonly)

Returns the time of the last obtained achievement


63
64
65
66
# File 'app/models/course_user.rb', line 63

calculated :last_obtained_achievement, (lambda do
  Course::UserAchievement.select(:obtained_at).limit(1).order(obtained_at: :desc).
    where('course_user_achievements.course_user_id = course_users.id')
end)

Class Method Details

.user?(user) ⇒ Boolean

Test whether the current scope includes the current user.

Parameters:

  • user (User)

    The user to check

Returns:

  • (Boolean)

    True if the user exists in the current context


114
115
116
# File 'app/models/course_user.rb', line 114

def self.user?(user)
  all.exists?(user: user)
end

Instance Method Details

#manager_or_owner?Boolean

Test whether this course_user is a manager (i.e. manager or owner)

Returns:

  • (Boolean)

    True if course_user is a staff


121
122
123
# File 'app/models/course_user.rb', line 121

def manager_or_owner?
  MANAGER_ROLES.include?(role.to_sym)
end

#my_managersObject

Returns the managers of the groups I belong to in the course.

@return[Array]


160
161
162
163
164
# File 'app/models/course_user.rb', line 160

def my_managers
  my_groups = group_users.select(:group_id)
  CourseUser.joining { group_users.group }.merge(Course::GroupUser.manager).
    where.has { group_users.group.id.in(my_groups) }
end

#my_studentsObject

Returns my students in the course. If a course_user is the manager of a group, all other users in the group with the group role of normal will be considered as the students of the course_user.

@return[Array]


151
152
153
154
155
# File 'app/models/course_user.rb', line 151

def my_students
  my_groups = group_users.manager.select(:group_id)
  CourseUser.joining { group_users.group }.merge(Course::GroupUser.normal).
    where.has { group_users.group.id.in(my_groups) }
end

#real_student?Boolean

Test whether this course_user is a real student (i.e. not phantom and not staff)

Returns:

  • (Boolean)

142
143
144
# File 'app/models/course_user.rb', line 142

def real_student?
  student? && !phantom
end

#staff?Boolean

Test whether this course_user is a staff (i.e. teaching_assistant, manager, owner or observer)

Returns:

  • (Boolean)

    True if course_user is a staff


128
129
130
# File 'app/models/course_user.rb', line 128

def staff?
  STAFF_ROLES.include?(role.to_sym)
end

#teaching_staff?Boolean

Test whether this course_user is a teaching staff (i.e. teaching_assistant, manager or owner)

Returns:

  • (Boolean)

    True if course_user is a staff


135
136
137
# File 'app/models/course_user.rb', line 135

def teaching_staff?
  TEACHING_STAFF_ROLES.include?(role.to_sym)
end