class IMS::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

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]

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

Calls superclass method IMS::LTI::ToolBase::new
# File lib/ims/lti/tool_provider.rb, line 50
def initialize(consumer_key, consumer_secret, params={})
  super(consumer_key, consumer_secret, params)
  @outcome_requests = []
end

Public Instance Methods

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/ims/lti/tool_provider.rb, line 119
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
last_outcome_request() click to toggle source

Returns the most recent OutcomeRequest

# File lib/ims/lti/tool_provider.rb, line 99
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/ims/lti/tool_provider.rb, line 104
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/ims/lti/tool_provider.rb, line 56
def launch_request?
  lti_message_type == 'basic-lti-launch-request'
end
outcome_service?() click to toggle source

Check if the Tool Launch expects an Outcome Result

# File lib/ims/lti/tool_provider.rb, line 61
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/ims/lti/tool_provider.rb, line 84
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/ims/lti/tool_provider.rb, line 94
def post_read_result!
  new_request.post_read_result!
end
post_replace_result!(score) 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/ims/lti/tool_provider.rb, line 75
def post_replace_result!(score)
  new_request.post_replace_result!(score)
end
username(default=nil) click to toggle source

Return the full, given, or family name if set

# File lib/ims/lti/tool_provider.rb, line 66
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/ims/lti/tool_provider.rb, line 133
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