-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
vericite.rb
393 lines (358 loc) · 17.7 KB
/
vericite.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# frozen_string_literal: true
#
# Copyright (C) 2016 - 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/>.
#
require "vericite_client"
module VeriCite
def self.state_from_similarity_score(similarity_score)
return "none" if similarity_score == 0
return "acceptable" if similarity_score < 25
return "warning" if similarity_score < 50
return "problem" if similarity_score < 75
"failure"
end
class Client
attr_accessor :account_id, :shared_secret, :host, :testing, :show_preliminary_score
def initialize(testing = false)
@host = Canvas::Plugin.find(:vericite).settings[:host] || "api.vericite.com"
account_id = Canvas::Plugin.find(:vericite).settings[:account_id]
shared_secret = Canvas::Plugin.find(:vericite).settings[:shared_secret]
show_preliminary_score = Canvas::Plugin.find(:vericite).settings[:show_preliminary_score] || false
raise "Account ID required" unless account_id
raise "Shared secret required" unless shared_secret
@account_id = account_id
@shared_secret = shared_secret
@show_preliminary_score = show_preliminary_score
@testing = testing
end
def id(obj)
if @testing
"test_#{obj.asset_string}"
else
"#{account_id}_#{obj.asset_string}"
end
end
def email(item)
# emails @example.com are, guaranteed by RFCs, to be like /dev/null :)
email = if item.is_a?(User)
item.email
end
email || "#{item.asset_string}@null.instructure.example.com"
end
def self.default_assignment_vericite_settings
{
originality_report_visibility: Canvas::Plugin.find(:vericite).settings[:release_to_students] || "immediate",
exclude_quoted: Canvas::Plugin.find(:vericite).settings[:exclude_quotes],
exclude_self_plag: Canvas::Plugin.find(:vericite).settings[:exclude_self_plag],
store_in_index: Canvas::Plugin.find(:vericite).settings[:store_in_index],
vericite: true
}
end
def self.normalize_assignment_vericite_settings(settings)
unless settings.nil?
valid_keys = VeriCite::Client.default_assignment_vericite_settings.keys
valid_keys << :created
settings = settings.slice(*valid_keys)
settings[:originality_report_visibility] = "immediate" unless %w[immediate after_grading after_due_date never].include?(settings[:originality_report_visibility])
%i[exclude_quoted exclude_self_plag store_in_index].each do |key|
bool = Canvas::Plugin.value_to_boolean(settings[key])
settings[key] = bool ? "1" : "0"
end
end
settings
end
def createOrUpdateAssignment(assignment, settings)
course = assignment.context
today = course.time_zone.today
settings = VeriCite::Client.normalize_assignment_vericite_settings(settings)
response = sendRequest(:create_assignment, settings.merge!({
user: course,
course:,
assignment:,
utp: "2",
dtstart: "#{today.strftime} 00:00:00",
dtdue: "#{today.strftime} 00:00:00",
dtpost: "#{today.strftime} 00:00:00",
late_accept_flag: "1",
post: true
}))
is_response_success?(response) ? { assignment_id: response[:assignment_id] } : response_error_hash(response)
end
# if asset_string is passed in, only submit that attachment
def submitPaper(submission, asset_string = nil)
student = submission.user
assignment = submission.assignment
course = assignment.context
opts = {
post: true,
utp: "1",
user: student,
course:,
assignment:,
tem: email(course),
role: submission.grants_right?(student, :grade) ? "Instructor" : "Learner"
}
responses = {}
if submission.submission_type == "online_upload"
attachments = submission.attachments.select { |a| a.vericiteable? && (asset_string.nil? || a.asset_string == asset_string) }
attachments.each do |a|
# do not resubmit if the score already exists
next unless submission.vericite_data_hash[a.asset_string][:similarity_score].blank?
paper_id = a.id
paper_title = File.basename(a.display_name, ".*")
paper_ext = a.extension
paper_type = a.content_type
if paper_ext.nil?
paper_ext = ""
end
paper_size = 100 # File.size(
responses[a.asset_string] = sendRequest(:submit_paper, { pid: paper_id, ptl: paper_title, pext: paper_ext, ptype: paper_type, psize: paper_size, pdata: a.open }.merge!(opts))
end
elsif submission.submission_type == "online_text_entry" && (asset_string.nil? || submission.asset_string == asset_string)
paper_id = Digest::SHA1.hexdigest submission.plaintext_body
paper_ext = "html"
paper_title = "InlineSubmission"
plain_text = "<html>#{submission.plaintext_body}</html>"
paper_type = "text/html"
paper_size = plain_text.bytesize
responses[submission.asset_string] = sendRequest(:submit_paper, { pid: paper_id, ptl: paper_title, pext: paper_ext, ptype: paper_type, psize: paper_size, pdata: plain_text }.merge!(opts))
else
raise "Unsupported submission type for VeriCite integration: #{submission.submission_type}"
end
responses.transform_values! do |res|
is_response_success?(res) ? { object_id: res[:returned_object_id] } : response_error_hash(res)
end
responses
end
def generateReport(submission, asset_string)
user = submission.user
assignment = submission.assignment
course = assignment.context
object_id = submission.vericite_data_hash.dig(asset_string, :object_id)
res = nil
res = sendRequest(:get_scores, oid: object_id, utp: "2", user:, course:, assignment:) if object_id
data = {}
if res
data[:similarity_score] = res[:similarity_score]
end
data
end
def submissionReportUrl(submission, current_user, asset_string)
user = submission.user
assignment = submission.assignment
course = assignment.context
object_id = submission.vericite_data_hash.dig(asset_string, :object_id)
response = sendRequest(:generate_report, oid: object_id, utp: "2", current_user:, user:, course:, assignment:)
if response.nil?
nil
else
response[:report_url]
end
end
def submissionStudentReportUrl(submission, current_user, asset_string)
user = submission.user
assignment = submission.assignment
course = assignment.context
object_id = submission.vericite_data_hash.dig(asset_string, :object_id)
response = sendRequest(:generate_report, oid: object_id, utp: "1", current_user:, user:, course:, assignment:, tem: email(course))
if response.nil?
nil
else
response[:report_url]
end
end
def sendRequest(command, args)
# default response is "ok" since VeriCite doesn't implement all functions
response = {}
begin
vericite_config = VeriCiteClient::Configuration.new
vericite_config.host = @host
vericite_config.base_path = "/lms/v1"
api_client = VeriCiteClient::ApiClient.new(vericite_config)
vericite_client = VeriCiteClient::DefaultApi.new(api_client)
user = args.delete :user
course = args.delete :course
assignment = args.delete :assignment
consumer = @account_id
consumer_secret = @shared_secret
case command
when :create_assignment
context_id = course.id
assignment_id = assignment.id
assignment_data = VeriCiteClient::AssignmentData.new
assignment_data.assignment_title = assignment.title || assignment_id
assignment_data.assignment_instructions = assignment.description || ""
assignment_data.assignment_exclude_quotes = args["exclude_quoted"] == "1"
assignment_data.assignment_exclude_self_plag = args["exclude_self_plag"] == "1"
assignment_data.assignment_store_in_index = args["store_in_index"] == "1"
assignment_data.assignment_due_date = 0
unless assignment.due_at.nil?
# convert to epoch time in milli
assignment_data.assignment_due_date = assignment.due_at.to_time.utc.to_i * 1000
end
assignment_data.assignment_grade = assignment.points_possible || -1
_data, status_code, _headers = vericite_client.assignments_context_id_assignment_id_post(context_id, assignment_id, consumer, consumer_secret, assignment_data)
# check status code
response[:return_code] = status_code
unless is_response_success?(response)
response[:return_message] = "An error has occurred while creating the VeriCite assignment."
response[:public_error_message] = response[:return_message]
raise "Failed to create assignment: #{assignment_id}, site #{context_id}"
end
# this is a flag to signal success
response[:assignment_id] = assignment.id
when :submit_paper
context_id = course.id
assignment_id = assignment.id
user_id = user.id
report_meta_data = VeriCiteClient::ReportMetaData.new
report_meta_data.user_first_name = user.first_name
report_meta_data.user_last_name = user.last_name
report_meta_data.user_email = email(user)
report_meta_data.user_role = args[:role]
if assignment
report_meta_data.assignment_title = assignment.title || assignment_id
end
if course
report_meta_data.context_title = course.name || context_id
end
external_content_data = VeriCiteClient::ExternalContentData.new
external_content_data.external_content_id = "#{consumer}/#{context_id}/#{assignment_id}/#{user_id}/#{args[:pid]}"
external_content_data.file_name = args[:ptl]
external_content_data.upload_content_type = args[:pext]
external_content_data.upload_content_length = args[:psize]
report_meta_data.external_content_data = external_content_data
# @return [Array<ExternalContentUploadInfo>]
data, status_code, _headers = vericite_client.reports_submit_request_context_id_assignment_id_user_id_post(context_id, assignment_id, user_id, consumer, consumer_secret, report_meta_data)
# check status code
response[:return_code] = status_code
unless is_response_success?(response)
response[:return_message] = "An error has occurred while submitting the paper to VeriCite."
response[:public_error_message] = response[:return_message]
raise "Failed to submit paper: #{external_content_data.external_content_id}"
end
data.each do |externalContentUploadInfo|
# API will return an upload URL to store the submission (throws an exception if it fails)
api_client.uploadfile(externalContentUploadInfo.url_post, args[:pdata], externalContentUploadInfo.headers)
end
# this is a flag to signal success
response[:returned_object_id] = external_content_data.external_content_id
when :get_scores
context_id = course.id
assignment_id = assignment.id
user_id = user.id
user_score_cache_key_prefix = "vericite_scores/#{consumer}/#{context_id}/#{assignment_id}/"
users_score_map = {}
# first check if the cache already has the user's score and if we haven't looked up this assignment lately:
users_score_map[user_id.to_s] = Rails.cache.read("#{user_score_cache_key_prefix}#{user_id}")
if users_score_map[user_id.to_s].nil? && Rails.cache.read(user_score_cache_key_prefix).nil?
# we already looked up this user in Redis, don't bother again (by setting {})
users_score_map[user_id.to_s] ||= {}
# we need to look up the user scores in VeriCite for this course
# @return [Array<ReportScoreReponse>]
data, status_code, _headers = vericite_client.reports_scores_context_id_get(context_id, consumer, consumer_secret, { assignment_id: })
# keep track of the assignment lookup api call
Rails.cache.write(user_score_cache_key_prefix, true, expires_in: 5.minutes)
# check status code
response[:return_code] = status_code
unless is_response_success?(response)
response[:return_message] = "An error has occurred while getting scores from VeriCite."
response[:public_error_message] = response[:return_message]
raise "Failed to get scores for site: #{context_id}, assignment: #{assignment_id}, user: #{user_id}, exId: #{args[:oid]}"
end
# create the user scores map and cache it
data.each do |reportScoreReponse|
next unless reportScoreReponse.score.is_a?(Integer) && reportScoreReponse.score >= 0 &&
(@show_preliminary_score || reportScoreReponse.preliminary.nil? || !reportScoreReponse.preliminary)
# keep track of this user's report scores
users_score_map[reportScoreReponse.user] ||= {}
users_score_map[reportScoreReponse.user][reportScoreReponse.external_content_id] = Float(reportScoreReponse.score)
end
# cache the user score map for a short period of time
users_score_map.each_key do |key|
Rails.cache.write("#{user_score_cache_key_prefix}#{key}", users_score_map[key], expires_in: 5.minutes)
end
else
# since we didn't have to consult VeriCite, set response status to 200
response[:return_code] = 200
end
# the user score map shouldn't be empty now (either grabbed from the cache or VeriCite)
users_score_map[user_id.to_s]&.each do |key, score|
if key == args[:oid] && score >= 0
response[:similarity_score] = score
end
end
when :generate_report
context_id = course.id
assignment_id_filter = assignment.id
user_id = user.id
current_user = args.delete :current_user
token_user = current_user.id
token_user_role = "Learner"
if args[:utp] == "2"
# instructor
token_user_role = "Instructor"
end
# @return [Array<ReportURLLinkReponse>]
data, status_code, _headers = vericite_client.reports_urls_context_id_get(context_id, assignment_id_filter, consumer, consumer_secret, token_user, token_user_role, { user_id_filter => user_id, external_content_id_filter => args[:oid] })
# check status code
response[:return_code] = status_code
unless is_response_success?(response)
response[:return_message] = "An error has occurred while getting the report URL from VeriCite."
response[:public_error_message] = response[:return_message]
raise "Failed to get the report url for site: #{context_id}, assignment: #{assignment_id}, user: #{user_id}, exId: #{args[:oid]}, token_user: #{token_user}, token_user_role: #{token_user_role}"
end
data.each do |reportURLLinkReponse|
# should only be 1 url
if reportURLLinkReponse.external_content_id == args[:oid]
# setting response URL is a signal for success
response[:report_url] = reportURLLinkReponse.url
end
end
end
rescue => e
Rails.logger.error("VeriCite: account_id: #{@account_id}, code: #{response[:return_code]}, error: #{e}")
if is_response_success?(response)
# we do not want to return a success code if there was an error
response[:return_code] = 100
end
unless response.key?(:return_message)
# we want a generic error message at a minimum
response[:return_message] = "VeriCite error during #{command} command, error: #{e}"
response[:public_error_message] = response[:return_message]
end
end # begin
return nil if @testing
response
end
private
SUCCESSFUL_RETURN_CODES = (200..299)
def is_response_success?(response)
response&.key?(:return_code) && SUCCESSFUL_RETURN_CODES.cover?(Integer(response[:return_code]))
rescue
false
end
def response_error_hash(response)
return {} if is_response_success?(response)
{
error_code: response[:return_code],
error_message: response[:return_message],
public_error_message: response[:public_error_message],
}
end
end
end