|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Api |
| 4 | + module V8 |
| 5 | + module Organizations |
| 6 | + module Courses |
| 7 | + class StudyrightEligibilityController < Api::V8::BaseController |
| 8 | + include Swagger::Blocks |
| 9 | + |
| 10 | + swagger_path '/api/v8/org/{organization_slug}/courses/{course_name}/eligible_students' do |
| 11 | + operation :get do |
| 12 | + key :description, "Returns all users from the course who have at least 90% of every part's points and are applying for study right, in a json format. Course is searched by name, only 2019 programming mooc course is valid" |
| 13 | + key :produces, ['application/json'] |
| 14 | + key :tags, ['point'] |
| 15 | + parameter '$ref': '#/parameters/path_organization_slug' |
| 16 | + parameter '$ref': '#/parameters/path_course_name' |
| 17 | + response 403, '$ref': '#/responses/error' |
| 18 | + response 404, '$ref': '#/responses/error' |
| 19 | + response 200 do |
| 20 | + key :description, 'Users in json' |
| 21 | + schema do |
| 22 | + key :type, :array |
| 23 | + items do |
| 24 | + key :'$ref', :UserBasicInfo |
| 25 | + end |
| 26 | + end |
| 27 | + end |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + skip_authorization_check |
| 32 | + |
| 33 | + def eligible_students |
| 34 | + unauthorize_guest! |
| 35 | + |
| 36 | + return respond_with_error("This feature is only for MOOC-organization's 2019 programming MOOC") unless params[:course_name] == '2019-ohjelmointi' && params[:organization_slug] == 'mooc' |
| 37 | + |
| 38 | + course = Course.find_by!(name: "#{params[:organization_slug]}-#{params[:course_name]}") |
| 39 | + |
| 40 | + authorize! :read, course |
| 41 | + |
| 42 | + applied_students = UserAppDatum.where(field_name: 'applies_for_study_right', value: 't', namespace: 'ohjelmoinnin-mooc-2019').each { |datum| datum.user_id } |
| 43 | + |
| 44 | + authorize! :read, applied_students |
| 45 | + |
| 46 | + eligible_student_ids = [] |
| 47 | + |
| 48 | + applied_students.map do |user| |
| 49 | + drop = false |
| 50 | + course.exercise_group_completion_counts_for_user(user).map do |group, info| |
| 51 | + if info[:progress] < 0.9 |
| 52 | + drop = true |
| 53 | + end |
| 54 | + end |
| 55 | + eligible_student_ids.push(user) unless drop |
| 56 | + end |
| 57 | + |
| 58 | + eligible_students = [] |
| 59 | + |
| 60 | + eligible_student_ids.map do |user_id| |
| 61 | + u = User.find(user_id) |
| 62 | + info = { |
| 63 | + id: u.id, |
| 64 | + username: u.login, |
| 65 | + email: u.email, |
| 66 | + administrator: u.administrator |
| 67 | + } |
| 68 | + eligible_students.push(info) |
| 69 | + end |
| 70 | + |
| 71 | + render json: { |
| 72 | + eligible_students: eligible_students |
| 73 | + } |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments