1
+ # frozen_string_literal: true
2
+
1
3
require 'portable_csv'
2
4
3
5
class ParticipantsController < ApplicationController
@@ -16,7 +18,7 @@ def index
16
18
end
17
19
add_breadcrumb 'Participants'
18
20
19
- @ordinary_fields = %w( username email )
21
+ @ordinary_fields = %w[ username email ]
20
22
@extra_fields = UserField . all
21
23
valid_fields = @ordinary_fields + @extra_fields . map ( &:name ) + [ 'include_administrators' ]
22
24
@@ -34,7 +36,7 @@ def index
34
36
35
37
@courses = courses . order ( :name ) . to_a
36
38
37
- unless params [ 'group_completion_course_id' ] . blank ?
39
+ if params [ 'group_completion_course_id' ] . present ?
38
40
@group_completion_course = courses . find ( params [ 'group_completion_course_id' ] )
39
41
@group_completion = @group_completion_course . exercise_group_completion_by_user
40
42
end
@@ -61,8 +63,18 @@ def index
61
63
def show
62
64
@user = User . find ( params [ :id ] )
63
65
authorize! :view_participant_information , @user
64
- # TODO: bit ugly
65
- @awarded_points = Hash [ AwardedPoint . where ( id : AwardedPoint . all_awarded ( @user ) ) . to_a . sort! . group_by ( &:course_id ) . map { |k , v | [ k , v . map ( &:name ) ] } ]
66
+ # TODO: bit ugly -- and now it's even worse!
67
+ @awarded_points = Hash [
68
+ AwardedPoint . where ( id : AwardedPoint . all_awarded ( @user ) )
69
+ . to_a
70
+ . sort!
71
+ . group_by ( &:course_id ) . map do |id , course_points |
72
+ [ id , {
73
+ awarded : course_points . reject ( &:awarded_after_soft_deadline? ) . map ( &:name ) ,
74
+ late : course_points . select ( &:awarded_after_soft_deadline? ) . map ( &:name )
75
+ } ]
76
+ end
77
+ ]
66
78
67
79
if current_user . administrator?
68
80
add_breadcrumb 'Participants' , :participants_path
@@ -77,32 +89,30 @@ def show
77
89
@group_completion_ratios = { }
78
90
for course_id in @awarded_points . keys
79
91
course = Course . find ( course_id )
80
- if !course . hide_submissions?
81
- @courses << course
82
-
83
- awarded = @awarded_points [ course . id ]
84
- missing = AvailablePoint . course_points ( course ) . order! . map ( &:name ) - awarded
85
- @missing_points [ course_id ] = missing
86
-
87
- if awarded . size + missing . size > 0
88
- @percent_completed [ course_id ] = 100 * ( awarded . size . to_f / ( awarded . size + missing . size ) )
89
- else
90
- @percent_completed [ course_id ] = 0
91
- end
92
- @group_completion_ratios [ course_id ] = course . exercise_group_completion_ratio_for_user ( @user )
93
- end
92
+ next if course . hide_submissions?
93
+ @courses << course
94
+
95
+ awarded = @awarded_points [ course . id ]
96
+ missing = AvailablePoint . course_points ( course ) . order! . map ( &:name ) - awarded [ :awarded ] - awarded [ :late ]
97
+ @missing_points [ course_id ] = missing
98
+
99
+ @percent_completed [ course_id ] = if awarded [ :awarded ] . size + awarded [ :late ] . size + missing . size > 0
100
+ 100 * ( ( awarded [ :awarded ] . size . to_f + awarded [ :late ] . size . to_f * course . soft_deadline_point_multiplier ) / ( awarded [ :awarded ] . size + awarded [ :late ] . size + missing . size ) )
101
+ else
102
+ 0
103
+ end
104
+ @group_completion_ratios [ course_id ] = course . exercise_group_completion_ratio_for_user ( @user )
94
105
end
95
106
96
- if current_user . administrator? || current_user . id == @user . id
97
- @submissions = @user . submissions . order ( 'created_at DESC' ) . includes ( :user ) . includes ( :course )
98
- else # teacher and assistant sees only submissions for own teacherd courses
99
- @submissions = @user . submissions . order ( 'created_at DESC' ) . includes ( :user , :course ) . where ( course : current_user . teaching_in_courses )
100
- end
107
+ @submissions = if current_user . administrator? || current_user . id == @user . id
108
+ @user . submissions . order ( 'created_at DESC' ) . includes ( :user ) . includes ( :course )
109
+ else # teacher and assistant sees only submissions for own teacherd courses
110
+ @user . submissions . order ( 'created_at DESC' ) . includes ( :user , :course ) . where ( course : current_user . teaching_in_courses )
111
+ end
101
112
@submission_count = @submissions . count
102
113
@submissions = @submissions . limit ( 100 ) unless !!params [ :view_all ]
103
114
104
115
Submission . eager_load_exercises ( @submissions )
105
-
106
116
end
107
117
108
118
def me
@@ -155,9 +165,7 @@ def index_csv
155
165
@participants . each do |user |
156
166
row = [ ]
157
167
for field in @ordinary_fields
158
- if @visible_columns . include? ( field )
159
- row << user . send ( field )
160
- end
168
+ row << user . send ( field ) if @visible_columns . include? ( field )
161
169
end
162
170
for field in @extra_fields
163
171
if @visible_columns . include? ( field . name )
@@ -170,7 +178,7 @@ def index_csv
170
178
group_data = @group_completion [ group ]
171
179
points = group_data [ :points_by_user ] [ user . id ] || 0
172
180
total = group_data [ :available_points ]
173
- percentage = sprintf ( '%.3f%%' , ( points . to_f / total . to_f ) * 100 )
181
+ percentage = format ( '%.3f%%' , ( points . to_f / total . to_f ) * 100 )
174
182
row << percentage
175
183
end
176
184
end
@@ -183,6 +191,6 @@ def index_csv
183
191
private
184
192
185
193
def set_organization
186
- @organization = Organization . find_by ( slug :params [ :organization_id ] ) unless params [ :organization_id ] . nil?
194
+ @organization = Organization . find_by ( slug : params [ :organization_id ] ) unless params [ :organization_id ] . nil?
187
195
end
188
196
end
0 commit comments