Ruby script for API: how many (and which) submissions have comments from +1 teacher?

Document created by Adam Craik on May 22, 2018
Version 1Show Document
  • View in full screen mode

(please read comment below 1st)

require 'typhoeus'
require 'link_header'
require 'json'

@canvas_url = 'https://YOURCANVAS.test.instructure.com' #you will need to put your own Canvas url here (recommend you use test environment!)
@canvas_token = 'YOURTOKEN' #put your own token here!
@api_endpoint = '/api/v1/courses/[COURSE NO]' #update with the course number (placeholder is "[COURSE NO]" - square brackets not required)
@api_endpoint1 = '/assignments/[ASSIGNMENT NO]/submissions?include[]=submission_comments' #update with the assignment number (placeholder is "[ASSIGNMENT NO]" - square brackets not required)

#You should not change any of the code below, unless you know what you're doing (or you're learning by tinkering but go careful!)

@api_endpoint2 = '/enrollments?type[]=TeacherEnrollment'
@api_endpoint3 = '/enrollments?type[]=StudentEnrollment'

@commentarray = [] #creates empty global array for full data of comments call
@commenthash = Hash.new{|hsh,key| hsh[key] = [] } #creates empty hash with values as arrays for specific data from commentarray to go into
@teacherarray = [] #creates empty global array for teacher enrollments call
@studenthash = {} #creates empty hash for student enrollments call

 

#the following code gets data relating to student submissions of the desired course assignment:

request_url = "#{@canvas_url}#{@api_endpoint}#{@api_endpoint1}"
more_data = true
while more_data # while more_data is true keep looping through the data
# puts request_url
get_comments = Typhoeus::Request.new(
request_url, #we need a variable here because we need the api url to change
method: :get,
headers: { authorization: "Bearer #{@canvas_token}" }
)

get_comments.on_complete do |response|
#get next link
links = LinkHeader.parse(response.headers['link']).links
next_link = links.find { |link| link['rel'] == 'next' }
request_url = next_link.href if next_link
if next_link && "#{response.body}" != "[]"
more_data = true
else
more_data = false
end
#ends next link code

if response.code == 200
data = JSON.parse(response.body)
data.collect do |comments|
@commentarray.push(comments) #adds all data from enrollments JSON responses into the @commentarray
end
else
puts "Something went wrong! Response code was #{response.code}"
end
end

get_comments.run
end

studentarraycounter = 0
commentcounter = 0
more_comments = true
while more_comments
if !@commentarray[studentarraycounter].nil? && !@commentarray[studentarraycounter]["submission_comments"][commentcounter].nil?
@commenthash[@commentarray[studentarraycounter]["user_id"]].push @commentarray[studentarraycounter]["submission_comments"][commentcounter]["author_id"]
commentcounter +=1
more_comments = true

else
studentarraycounter +=1
commentcounter = 0
if !@commentarray[studentarraycounter].nil?
more_comments = true
else
more_comments = false
end
end
end

 

#the following code relates to teacher enrollment on the desired course:

request_url = "#{@canvas_url}#{@api_endpoint}#{@api_endpoint2}"
more_data = true
while more_data # while more_data is true keep looping through the data
# puts request_url
get_teachers = Typhoeus::Request.new(
request_url, #we need a variable here because we need the api url to change
method: :get,
headers: { authorization: "Bearer #{@canvas_token}" }
)

get_teachers.on_complete do |response|
#get next link
links = LinkHeader.parse(response.headers['link']).links
next_link = links.find { |link| link['rel'] == 'next' }
request_url = next_link.href if next_link
if next_link && "#{response.body}" != "[]"
more_data = true
else
more_data = false
end
#ends next link code

if response.code == 200
data = JSON.parse(response.body)
data.each do |teachers|
@teacherarray << teachers["user"]["id"] #adds just the teacher IDs into the @teacherarray
end
else
puts "Something went wrong! Response code was #{response.code}"
end
end

get_teachers.run
end

 

#the following code relates to student enrollment on the desired course:

request_url = "#{@canvas_url}#{@api_endpoint}#{@api_endpoint3}"
more_data = true
while more_data # while more_data is true keep looping through the data
# puts request_url
get_students = Typhoeus::Request.new(
request_url, #we need a variable here because we need the api url to change
method: :get,
headers: { authorization: "Bearer #{@canvas_token}" }
)

get_students.on_complete do |response|
#get next link
links = LinkHeader.parse(response.headers['link']).links
next_link = links.find { |link| link['rel'] == 'next' }
request_url = next_link.href if next_link
if next_link && "#{response.body}" != "[]"
more_data = true
else
more_data = false
end
#ends next link code

if response.code == 200
data = JSON.parse(response.body)
data.each do |scrapestudents|
@studenthash[scrapestudents["sis_user_id"]] = [scrapestudents["user_id"]]

end
else
puts "Something went wrong! Response code was #{response.code}"
end
end

get_students.run
end

 

#the remaining code now interrogates the manipulated data from the calls above and presents the findings (did 10% of submissions have comments from more than one teacher? If so, which students were these? )

totalsubs = @commenthash.length

@second_markedarray = @commenthash.select{|_, v| (v & @teacherarray).length > 1}.keys
marked = @commenthash.select{|_, v| (v & @teacherarray).length > 1}.keys.count
puts "#{totalsubs} students have submitted"
puts "#{marked} submissions were second marked"
done = (marked.to_f / totalsubs) * 100
if done >= 10
puts "MARKING COMPLETED: #{done.round(1)}% of submissions have been second marked."
else
puts "Only #{done.round(1)}% of submissions have been second marked!"
end

fin = @studenthash.select{|_, v| (v & @second_markedarray).length == 1}.keys
puts "This is the list of student no's (SIS ID's) that have been 2nd marked:"
puts fin
2 people found this helpful

Attachments

    Outcomes