1+ # frozen_string_literal: true
2+
13require 'portable_csv'
24
35class ParticipantsController < ApplicationController
@@ -16,7 +18,7 @@ def index
1618 end
1719 add_breadcrumb 'Participants'
1820
19- @ordinary_fields = %w( username email )
21+ @ordinary_fields = %w[ username email ]
2022 @extra_fields = UserField . all
2123 valid_fields = @ordinary_fields + @extra_fields . map ( &:name ) + [ 'include_administrators' ]
2224
@@ -34,7 +36,7 @@ def index
3436
3537 @courses = courses . order ( :name ) . to_a
3638
37- unless params [ 'group_completion_course_id' ] . blank ?
39+ if params [ 'group_completion_course_id' ] . present ?
3840 @group_completion_course = courses . find ( params [ 'group_completion_course_id' ] )
3941 @group_completion = @group_completion_course . exercise_group_completion_by_user
4042 end
@@ -61,8 +63,18 @@ def index
6163 def show
6264 @user = User . find ( params [ :id ] )
6365 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+ ]
6678
6779 if current_user . administrator?
6880 add_breadcrumb 'Participants' , :participants_path
@@ -77,32 +89,30 @@ def show
7789 @group_completion_ratios = { }
7890 for course_id in @awarded_points . keys
7991 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 )
94105 end
95106
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
101112 @submission_count = @submissions . count
102113 @submissions = @submissions . limit ( 100 ) unless !!params [ :view_all ]
103114
104115 Submission . eager_load_exercises ( @submissions )
105-
106116 end
107117
108118 def me
@@ -155,9 +165,7 @@ def index_csv
155165 @participants . each do |user |
156166 row = [ ]
157167 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 )
161169 end
162170 for field in @extra_fields
163171 if @visible_columns . include? ( field . name )
@@ -170,7 +178,7 @@ def index_csv
170178 group_data = @group_completion [ group ]
171179 points = group_data [ :points_by_user ] [ user . id ] || 0
172180 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 )
174182 row << percentage
175183 end
176184 end
@@ -183,6 +191,6 @@ def index_csv
183191 private
184192
185193 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?
187195 end
188196end
0 commit comments