/
assessments_controller.rb
150 lines (135 loc) · 5.19 KB
/
assessments_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
#
# Copyright (C) 2014 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module LiveAssessments
# @API LiveAssessments
# Manage live assessments
#
# @model Assessment
# {
# "id": "Assessment",
# "description": "A simple assessment that collects pass/fail results for a student",
# "properties": {
# "id": {
# "type": "string",
# "example": "42",
# "description": "A unique identifier for this live assessment"
# },
# "key": {
# "type": "string",
# "example": "2014-05-27,outcome_52",
# "description": "A client specified unique identifier for the assessment"
# },
# "title": {
# "type": "string",
# "example": "May 27th Reading Assessment",
# "description": "A human readable title for the assessment"
# }
# }
# }
class AssessmentsController < ApplicationController
before_action :require_user
before_action :require_context
# @API Create or find a live assessment
#
# Creates or finds an existing live assessment with the given key and aligns it with
# the linked outcome
#
# @example_request
# {
# "assessments": [{
# "key": "2014-05-27-Outcome-52",
# "title": "Tuesday's LiveAssessment",
# "links": {
# "outcome": "1"
# }
# }]
# }
#
# @example_response
# {
# "links": {
# "assessments.results": "http://example.com/courses/1/live_assessments/5/results"
# },
# "assessments": [Assessment]
# }
#
def create
return unless authorized_action(Assessment.new(context: @context), @current_user, :create)
reject! "missing required key :assessments" unless params[:assessments].is_a?(Array)
@assessments = []
if params[:assessments].any? { |assessment_hash| assessment_hash.dig(:links, :outcome) }
return unless authorized_action(@context, @current_user, :manage_outcomes)
end
Assessment.transaction do
params[:assessments].each do |assessment_hash|
if (outcome_id = assessment_hash.dig(:links, :outcome))
@outcome = @context.linked_learning_outcomes.where(id: outcome_id).first
reject! "outcome must be linked to the context" unless @outcome
end
reject! "missing required key :title" if assessment_hash[:title].blank?
reject! "missing required key :key" if assessment_hash[:key].blank?
assessment = Assessment.where(context_id: @context.id, context_type: @context.class.to_s, key: assessment_hash[:key]).first_or_initialize
assessment.title = assessment_hash[:title]
assessment.save!
if @outcome
criterion = @outcome.rubric_criterion
mastery_score = criterion && (criterion[:mastery_points] / criterion[:points_possible])
@outcome.align(assessment, @context, mastery_type: "none", mastery_score:)
end
@assessments << assessment
end
end
render json: serialize_jsonapi(@assessments)
end
# @API List live assessments
#
# Returns a paginated list of live assessments.
#
# @example_response
# {
# "links": {
# "assessments.results": "http://example.com/courses/1/live_assessments/{assessments.id}/results"
# },
# "assessments": [Assessment]
# }
#
def index
return unless authorized_action(Assessment.new(context: @context), @current_user, :read)
@assessments = Assessment.for_context(@context)
@assessments, meta = Api.jsonapi_paginate(@assessments, self, polymorphic_url([:api_v1, @context, :live_assessments]))
render json: serialize_jsonapi(@assessments).merge(meta:)
end
protected
def serialize_jsonapi(assessments)
serialized = Canvas::APIArraySerializer.new(assessments, {
each_serializer: LiveAssessments::AssessmentSerializer,
controller: self,
scope: @current_user,
root: false,
include_root: false
}).as_json
{
links: {
"assessments.results" => polymorphic_url([:api_v1, @context]) + "/live_assessments/{assessments.id}/results"
},
assessments: serialized
}
end
end
end