class AJIMS::LTI::ToolProvider

Class for implementing an LTI Tool Provider

# Initialize TP object with OAuth creds and post parameters
provider = IMS::LTI::ToolProvider.new(consumer_key, consumer_secret, params)

# Verify OAuth signature by passing the request object
if provider.valid_request?(request)
  # success
else
  # handle invalid OAuth
end

if provider.outcome_service?
  # ready for grade write-back
else
  # normal tool launch without grade write-back
end

If the tool was launch as an outcome service you can POST a score to the TC. The POST calls all return an OutcomeResponse object which can be used to handle the response appropriately.

# post the score to the TC, score should be a float >= 0.0 and <= 1.0
# this returns an OutcomeResponse object
response = provider.post_replace_result!(score)
if response.success?
  # grade write worked
elsif response.processing?
elsif response.unsupported?
else
  # failed
end

Attributes

consumer_key[RW]

OAuth credentials

consumer_secret[RW]

OAuth credentials

lti_errorlog[RW]

Message to be sent back to the ToolConsumer when the user returns

lti_errormsg[RW]

Message to be sent back to the ToolConsumer when the user returns

lti_log[RW]

Message to be sent back to the ToolConsumer when the user returns

lti_msg[RW]

Message to be sent back to the ToolConsumer when the user returns

outcome_requests[RW]

List of outcome requests made through this instance

Public Class Methods

new(consumer_key, consumer_secret, params={}) click to toggle source

Create a new ToolProvider

@param consumer_key [String] The OAuth consumer key @param consumer_secret [String] The OAuth consumer secret @param params [Hash] Set the launch parameters as described in LaunchParams

# File lib/ajims/lti/tool_provider.rb, line 53
def initialize(consumer_key, consumer_secret, params={})
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @custom_params = {}
  @ext_params = {}
  @non_spec_params = {}
  @outcome_requests = []
  process_params(params)
end

Public Instance Methods

admin?() click to toggle source

Convenience method for checking if the user has 'administrator' role

# File lib/ajims/lti/tool_provider.rb, line 100
def admin?
  has_role?('administrator')
end
build_return_url() click to toggle source

If the Tool Consumer sent a URL for the user to return to this will add any set messages to the URL.

Example:

tc = IMS::LTI::tc.new
tc.launch_presentation_return_url = "http://example.com/return"
tc.lti_msg = "hi there"
tc.lti_errorlog = "error happens"

tc.build_return_url # => "http://example.com/return?lti_msg=hi%20there&lti_errorlog=error%20happens"
# File lib/ajims/lti/tool_provider.rb, line 173
def build_return_url
  return nil unless launch_presentation_return_url
  messages = []
  %w{lti_errormsg lti_errorlog lti_msg lti_log}.each do |m|
    if message = self.send(m)
      messages << "#{m}=#{URI.escape(message)}"
    end
  end
  q_string = messages.any? ? ("?" + messages.join("&")) : ''
  launch_presentation_return_url + q_string
end
content_developer?() click to toggle source

Convenience method for checking if the user has 'contentdeveloper' role

# File lib/ajims/lti/tool_provider.rb, line 80
def content_developer?
  has_role?('ContentDeveloper')
end
has_role?(role) click to toggle source

Check whether the Launch Parameters have a role

# File lib/ajims/lti/tool_provider.rb, line 64
def has_role?(role)
  role = role.downcase
  @roles && @roles.any?{|r| r.index(role)}
end
instructor?() click to toggle source

Convenience method for checking if the user has 'instructor' or 'faculty' or 'staff' role

# File lib/ajims/lti/tool_provider.rb, line 75
def instructor?
  has_role?('instructor') || has_role?('faculty') || has_role?('staff')
end
last_outcome_request() click to toggle source

Returns the most recent OutcomeRequest

# File lib/ajims/lti/tool_provider.rb, line 153
def last_outcome_request
  @outcome_requests.last
end
last_outcome_success?() click to toggle source

Convenience method for whether the last OutcomeRequest was successful

# File lib/ajims/lti/tool_provider.rb, line 158
def last_outcome_success?
  last_outcome_request && last_outcome_request.outcome_post_successful?
end
launch_request?() click to toggle source

Check if the request was an LTI Launch Request

# File lib/ajims/lti/tool_provider.rb, line 110
def launch_request?
  lti_message_type == 'basic-lti-launch-request'
end
manager?() click to toggle source

Convenience method for checking if the user has 'Manager' role

# File lib/ajims/lti/tool_provider.rb, line 90
def manager?
  has_role?('Manager')
end
member?() click to toggle source

Convenience method for checking if the user has 'Member' role

# File lib/ajims/lti/tool_provider.rb, line 85
def member?
  has_role?('Member')
end
mentor?() click to toggle source

Convenience method for checking if the user has 'Mentor' role

# File lib/ajims/lti/tool_provider.rb, line 95
def mentor?
  has_role?('Mentor')
end
outcome_service?() click to toggle source

Check if the Tool Launch expects an Outcome Result

# File lib/ajims/lti/tool_provider.rb, line 115
def outcome_service?
  !!(lis_outcome_service_url && lis_result_sourcedid)
end
post_delete_result!() click to toggle source

POSTs a delete request to the Tool Consumer

Creates a new OutcomeRequest object and stores it in @outcome_requests

@return [OutcomeResponse] the response from the Tool Consumer

# File lib/ajims/lti/tool_provider.rb, line 138
def post_delete_result!
  new_request.post_delete_result!
end
post_read_result!() click to toggle source

POSTs the given score to the Tool Consumer with a replaceResult, the returned OutcomeResponse will have the score

Creates a new OutcomeRequest object and stores it in @outcome_requests

@return [OutcomeResponse] the response from the Tool Consumer

# File lib/ajims/lti/tool_provider.rb, line 148
def post_read_result!
  new_request.post_read_result!
end
post_replace_result!(score, submitted_at: nil) click to toggle source

POSTs the given score to the Tool Consumer with a replaceResult

Creates a new OutcomeRequest object and stores it in @outcome_requests

@return [OutcomeResponse] the response from the Tool Consumer

# File lib/ajims/lti/tool_provider.rb, line 129
def post_replace_result!(score, submitted_at: nil)
  new_request.post_replace_result!(score, submitted_at: submitted_at)
end
student?() click to toggle source

Convenience method for checking if the user has 'learner' or 'student' role

# File lib/ajims/lti/tool_provider.rb, line 70
def student?
  has_role?('learner') || has_role?('student')
end
ta?() click to toggle source

Convenience method for checking if the user has 'TeachingAssistant' role

# File lib/ajims/lti/tool_provider.rb, line 105
def ta?
  has_role?('TeachingAssistant')
end
username(default=nil) click to toggle source

Return the full, given, or family name if set

# File lib/ajims/lti/tool_provider.rb, line 120
def username(default=nil)
  lis_person_name_given || lis_person_name_family || lis_person_name_full || default
end

Private Instance Methods

new_request() click to toggle source
# File lib/ajims/lti/tool_provider.rb, line 187
def new_request
  @outcome_requests << OutcomeRequest.new(:consumer_key => @consumer_key,
                     :consumer_secret => @consumer_secret,
                     :lis_outcome_service_url => lis_outcome_service_url,
                     :lis_result_sourcedid =>lis_result_sourcedid)

  extend_outcome_request(@outcome_requests.last)
end