@@ -32,10 +32,24 @@ class ExercisesController < Api::V8::BaseController
32
32
def index
33
33
unauthorize_guest!
34
34
course = Course . find_by! ( id : params [ :course_id ] ) if params [ :course_id ]
35
+ authorize! :read , course
35
36
exercises = Exercise . includes ( :available_points ) . where ( course_id : course . id )
36
37
37
- visible = exercises . select { |ex | ex . visible_to? ( current_user ) }
38
- presentable = visible . map do |ex |
38
+ unlocked_exercises = course . unlocks
39
+ . where ( user_id : current_user . id )
40
+ . where ( [ 'valid_after IS NULL OR valid_after < ?' , Time . now ] )
41
+ . pluck ( :exercise_name )
42
+
43
+ unless current_user . administrator? || current_user . teacher? ( course . organization ) || current_user . assistant? ( course )
44
+ exercises = exercises . where ( hidden : false , disabled_status : 0 )
45
+ exercises = if unlocked_exercises . empty?
46
+ exercises . where ( unlock_spec : nil )
47
+ else
48
+ exercises . where ( [ "unlock_spec IS NULL OR name IN (#{ unlocked_exercises . map { |_ | '?' } . join ( ', ' ) } )" , *unlocked_exercises ] )
49
+ end . select { |e | e . _fast_visible_to? ( current_user ) }
50
+ end
51
+
52
+ presentable = exercises . map do |ex |
39
53
{
40
54
id : ex . id ,
41
55
available_points : ex . available_points ,
@@ -47,8 +61,7 @@ def index
47
61
}
48
62
end
49
63
50
- authorize_collection :read , visible
51
- present ( presentable )
64
+ render json : presentable
52
65
end
53
66
end
54
67
end
0 commit comments