Class: Course::Group

Inherits:
ApplicationRecord show all
Defined in:
app/models/course/group.rb

Instance Attribute Summary collapse

Instance Attribute Details

#average_achievement_countObject (readonly)

Returns the average number of achievements obtained by group users in this group who are students.


35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/course/group.rb', line 35

calculated :average_achievement_count, (lambda do
  Course::GroupUser.where('course_group_users.group_id = course_groups.id').
    joining { course_user.course_user_achievements.outer }.
    where('course_users.role = ?', CourseUser.roles[:student]).
    # CAST is used to force a float division (integer division by default).
    # greatest(#, 1) is used to avoid division by 0.
    selecting do
      cast(sql('count(course_user_achievements.id) as float')) /
      greatest(sql('count(distinct(course_group_users.course_user_id)), 1.0'))
    end
end)

#average_experience_pointsObject (readonly)

Returns the average experience points of group users in this group who are students.


20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/course/group.rb', line 20

calculated :average_experience_points, (lambda do
  Course::GroupUser.where('course_group_users.group_id = course_groups.id').
    joining { course_user.experience_points_records.outer }.
    where('course_users.role = ?', CourseUser.roles[:student]).
    # CAST is used to force a float division (integer division by default).
    # greatest(#, 1) is used to avoid division by 0.
    selecting do
      cast(sql('coalesce(sum(course_experience_points_records.points_awarded), 0.0) as float')) /
      greatest(sql('count(distinct(course_group_users.course_user_id)), 1.0'))
    end
end)

#last_obtained_achievementObject (readonly)

Returns the time of the last obtained achievement by group users in this group who are students.


50
51
52
53
54
55
# File 'app/models/course/group.rb', line 50

calculated :last_obtained_achievement, (lambda do
  Course::GroupUser.where('course_group_users.group_id = course_groups.id').
    joins(course_user: :course_user_achievements).
    where('course_users.role = ?', CourseUser.roles[:student]).
    select('course_user_achievements.obtained_at').limit(1).order('obtained_at DESC')
end)