|
| 1 | +require 'grape' |
| 2 | + |
| 3 | +module D2lIntegrationApi |
| 4 | + # The D2l API provides the frontend with the ability to register |
| 5 | + # integration details to connect units with D2L. This will allow |
| 6 | + # grade book items to be copied from portfolio results to D2L. |
| 7 | + class D2lApi < Grape::API |
| 8 | + helpers AuthenticationHelpers |
| 9 | + helpers AuthorisationHelpers |
| 10 | + helpers FileStreamHelper |
| 11 | + include LogHelper |
| 12 | + |
| 13 | + before do |
| 14 | + authenticated? |
| 15 | + end |
| 16 | + |
| 17 | + desc 'Get the D2L assessment mapping for a unit' |
| 18 | + get '/units/:unit_id/d2l' do |
| 19 | + unit = Unit.find(params[:unit_id]) |
| 20 | + |
| 21 | + unless authorise?(current_user, unit, :update) |
| 22 | + error!({ error: 'Not authorised to view D2L details' }, 403) |
| 23 | + end |
| 24 | + |
| 25 | + present unit.d2l_assessment_mapping, with: D2lIntegrationApi::Entities::D2lEntity |
| 26 | + end |
| 27 | + |
| 28 | + desc 'Create a D2L assessment mapping for a unit' |
| 29 | + params do |
| 30 | + requires :org_unit_id, type: String, desc: 'The org unit id for the D2L unit' |
| 31 | + optional :grade_object_id, type: Numeric, desc: 'The grade object id for the D2L unit' |
| 32 | + end |
| 33 | + post '/units/:unit_id/d2l' do |
| 34 | + unit = Unit.find(params[:unit_id]) |
| 35 | + |
| 36 | + unless authorise?(current_user, unit, :update) |
| 37 | + error!({ error: 'Not authorised to add D2L details' }, 403) |
| 38 | + end |
| 39 | + |
| 40 | + accepted_params = ActionController::Parameters.new(params).permit(:unit_id, :org_unit_id, :grade_object_id) |
| 41 | + |
| 42 | + d2l = D2lAssessmentMapping.create!(accepted_params) |
| 43 | + present d2l, with: D2lIntegrationApi::Entities::D2lEntity |
| 44 | + end |
| 45 | + |
| 46 | + desc 'Delete a D2L assessment mapping for a unit' |
| 47 | + delete '/units/:unit_id/d2l/:id' do |
| 48 | + unit = Unit.find(params[:unit_id]) |
| 49 | + |
| 50 | + unless authorise?(current_user, unit, :update) |
| 51 | + error!({ error: 'Not authorised to delete D2L details' }, 403) |
| 52 | + end |
| 53 | + |
| 54 | + d2l = unit.d2l_assessment_mapping |
| 55 | + |
| 56 | + if d2l.id != params[:id].to_i |
| 57 | + error!({ error: 'D2L details not found' }, 404) |
| 58 | + end |
| 59 | + |
| 60 | + d2l.destroy if d2l.present? |
| 61 | + status 204 |
| 62 | + end |
| 63 | + |
| 64 | + desc 'Update a D2L assessment mapping for a unit' |
| 65 | + params do |
| 66 | + optional :org_unit_id, type: String, desc: 'The org unit id for the D2L unit' |
| 67 | + optional :grade_object_id, type: Numeric, desc: 'The grade object id for the D2L unit' |
| 68 | + end |
| 69 | + put '/units/:unit_id/d2l/:id' do |
| 70 | + unit = Unit.find(params[:unit_id]) |
| 71 | + |
| 72 | + unless authorise?(current_user, unit, :update) |
| 73 | + error!({ error: 'Not authorised to update D2L details' }, 403) |
| 74 | + end |
| 75 | + |
| 76 | + d2l = unit.d2l_assessment_mapping |
| 77 | + |
| 78 | + if d2l.id != params[:id].to_i |
| 79 | + error!({ error: 'D2L details not found' }, 404) |
| 80 | + end |
| 81 | + |
| 82 | + accepted_params = ActionController::Parameters.new(params).permit(:org_unit_id, :grade_object_id) |
| 83 | + |
| 84 | + d2l.update!(accepted_params) |
| 85 | + present d2l, with: D2lIntegrationApi::Entities::D2lEntity |
| 86 | + end |
| 87 | + |
| 88 | + desc 'Initiate a login to D2L as a convenor or admin' |
| 89 | + post '/d2l/login_url' do |
| 90 | + unless authorise? current_user, User, :convene_units |
| 91 | + error!({ error: 'Not authorised to login to D2L' }, 403) |
| 92 | + end |
| 93 | + |
| 94 | + begin |
| 95 | + response = D2lIntegration.login_url(current_user) |
| 96 | + rescue StandardError => e |
| 97 | + error!({ error: e.message }, 500) |
| 98 | + end |
| 99 | + |
| 100 | + present response, with: Grape::Presenters::Presenter |
| 101 | + end |
| 102 | + |
| 103 | + desc 'Trigger the posting of grades to D2L' |
| 104 | + post '/units/:unit_id/d2l/grades' do |
| 105 | + unit = Unit.find(params[:unit_id]) |
| 106 | + |
| 107 | + unless authorise?(current_user, unit, :update) |
| 108 | + error!({ error: 'Not authorised to post grades to D2L' }, 403) |
| 109 | + end |
| 110 | + |
| 111 | + if unit.d2l_assessment_mapping.blank? |
| 112 | + error!({ error: 'Configure D2L details for unit before starting transfer' }, 403) |
| 113 | + end |
| 114 | + |
| 115 | + token = current_user.user_oauth_tokens.where(provider: :d2l).last |
| 116 | + if token.blank? || token.expires_at < 10.minutes.from_now |
| 117 | + error!({ error: 'Login to D2L before transferring results' }, 403) |
| 118 | + end |
| 119 | + |
| 120 | + D2lPostGradesJob.perform_async(unit.id, current_user.id) |
| 121 | + |
| 122 | + status 202 |
| 123 | + end |
| 124 | + |
| 125 | + desc 'Get the result of a grade transfer to D2L' |
| 126 | + get '/units/:unit_id/d2l/grades' do |
| 127 | + unit = Unit.find(params[:unit_id]) |
| 128 | + |
| 129 | + unless authorise?(current_user, unit, :update) |
| 130 | + error!({ error: 'Not authorised to view grade transfer results' }, 403) |
| 131 | + end |
| 132 | + |
| 133 | + file_path = D2lIntegration.result_file_path(unit) |
| 134 | + unless File.exist?(file_path) |
| 135 | + error!({ error: 'No grade transfer result found' }, 404) |
| 136 | + end |
| 137 | + |
| 138 | + content_type 'text/csv' |
| 139 | + |
| 140 | + stream_file(file_path) |
| 141 | + end |
| 142 | + |
| 143 | + desc 'Determing if grade results are available for a unit' |
| 144 | + get '/units/:unit_id/d2l/grades/available' do |
| 145 | + unit = Unit.find(params[:unit_id]) |
| 146 | + |
| 147 | + unless authorise?(current_user, unit, :update) |
| 148 | + error!({ error: 'Not authorised to view grade transfer results' }, 403) |
| 149 | + end |
| 150 | + |
| 151 | + file_path = D2lIntegration.result_file_path(unit) |
| 152 | + response = { |
| 153 | + available: File.exist?(file_path), |
| 154 | + running: D2lIntegration.d2l_grade_job_present?(unit) |
| 155 | + } |
| 156 | + |
| 157 | + present response, with: Grape::Presenters::Presenter |
| 158 | + end |
| 159 | + |
| 160 | + desc 'Determing if unit is weighted' |
| 161 | + get '/units/:unit_id/d2l/grades/weighted' do |
| 162 | + unit = Unit.find(params[:unit_id]) |
| 163 | + |
| 164 | + unless authorise?(current_user, unit, :update) |
| 165 | + error!({ error: 'Not authorised to view unit details' }, 403) |
| 166 | + end |
| 167 | + |
| 168 | + d2l = unit.d2l_assessment_mapping |
| 169 | + |
| 170 | + return false unless d2l.present? && d2l.org_unit_id.present? |
| 171 | + |
| 172 | + present D2lIntegration.grade_weighted?(d2l, current_user), with: Grape::Presenters::Presenter |
| 173 | + end |
| 174 | + |
| 175 | + desc 'Get D2L api endpoint' |
| 176 | + get '/d2l/endpoint' do |
| 177 | + unless authorise? current_user, User, :convene_units |
| 178 | + error!({ error: 'Not authorised to view D2L endpoint' }, 403) |
| 179 | + end |
| 180 | + |
| 181 | + present D2lIntegration.d2l_api_host, with: Grape::Presenters::Presenter |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments