Class: AJIMS::LTI::ToolProvider

Inherits:
Object
  • Object
show all
Includes:
Extensions::Base, LaunchParams, RequestValidator
Defined in:
lib/ajims/lti/tool_provider.rb

Overview

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

Constant Summary

Constants included from LaunchParams

LaunchParams::LAUNCH_DATA_PARAMETERS

Instance Attribute Summary collapse

Attributes included from RequestValidator

#oauth_signature_validator

Attributes included from LaunchParams

#custom_params, #ext_params, #non_spec_params

Instance Method Summary collapse

Methods included from RequestValidator

#request_oauth_nonce, #request_oauth_timestamp, #valid_request!, #valid_request?

Methods included from LaunchParams

#get_custom_param, #get_ext_param, #get_non_spec_param, #process_params, #roles=, #set_custom_param, #set_ext_param, #set_non_spec_param, #to_params

Methods included from Extensions::Base

#extend_outcome_request, #extend_outcome_response, #outcome_request_extensions, #outcome_response_extensions

Constructor Details

#initialize(consumer_key, consumer_secret, params = {}) ⇒ ToolProvider

Create a new ToolProvider

Parameters:

  • consumer_key (String)

    The OAuth consumer key

  • consumer_secret (String)

    The OAuth consumer secret

  • params (Hash) (defaults to: {})

    Set the launch parameters as described in LaunchParams



53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#consumer_keyObject

OAuth credentials



42
43
44
# File 'lib/ajims/lti/tool_provider.rb', line 42

def consumer_key
  @consumer_key
end

#consumer_secretObject

OAuth credentials



42
43
44
# File 'lib/ajims/lti/tool_provider.rb', line 42

def consumer_secret
  @consumer_secret
end

#lti_errorlogObject

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



46
47
48
# File 'lib/ajims/lti/tool_provider.rb', line 46

def lti_errorlog
  @lti_errorlog
end

#lti_errormsgObject

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



46
47
48
# File 'lib/ajims/lti/tool_provider.rb', line 46

def lti_errormsg
  @lti_errormsg
end

#lti_logObject

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



46
47
48
# File 'lib/ajims/lti/tool_provider.rb', line 46

def lti_log
  @lti_log
end

#lti_msgObject

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



46
47
48
# File 'lib/ajims/lti/tool_provider.rb', line 46

def lti_msg
  @lti_msg
end

#outcome_requestsObject

List of outcome requests made through this instance



44
45
46
# File 'lib/ajims/lti/tool_provider.rb', line 44

def outcome_requests
  @outcome_requests
end

Instance Method Details

#admin?Boolean

Convenience method for checking if the user has ‘administrator’ role

Returns:

  • (Boolean)


100
101
102
# File 'lib/ajims/lti/tool_provider.rb', line 100

def admin?
  has_role?('administrator')
end

#build_return_urlObject

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+there&lti_errorlog=error+happens"


173
174
175
176
177
178
179
180
181
182
183
# 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.encode_www_form_component(message)}"
    end
  end
  q_string = messages.any? ? ("?" + messages.join("&")) : ''
  launch_presentation_return_url + q_string
end

#content_developer?Boolean

Convenience method for checking if the user has ‘contentdeveloper’ role

Returns:

  • (Boolean)


80
81
82
# File 'lib/ajims/lti/tool_provider.rb', line 80

def content_developer?
  has_role?('ContentDeveloper')
end

#has_role?(role) ⇒ Boolean

Check whether the Launch Parameters have a role

Returns:

  • (Boolean)


64
65
66
67
# 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?Boolean

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

Returns:

  • (Boolean)


75
76
77
# File 'lib/ajims/lti/tool_provider.rb', line 75

def instructor?
  has_role?('instructor') || has_role?('faculty') || has_role?('staff')
end

#last_outcome_requestObject

Returns the most recent OutcomeRequest



153
154
155
# File 'lib/ajims/lti/tool_provider.rb', line 153

def last_outcome_request
  @outcome_requests.last
end

#last_outcome_success?Boolean

Convenience method for whether the last OutcomeRequest was successful

Returns:

  • (Boolean)


158
159
160
# 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?Boolean

Check if the request was an LTI Launch Request

Returns:

  • (Boolean)


110
111
112
# File 'lib/ajims/lti/tool_provider.rb', line 110

def launch_request?
  lti_message_type == 'basic-lti-launch-request'
end

#manager?Boolean

Convenience method for checking if the user has ‘Manager’ role

Returns:

  • (Boolean)


90
91
92
# File 'lib/ajims/lti/tool_provider.rb', line 90

def manager?
  has_role?('Manager')
end

#member?Boolean

Convenience method for checking if the user has ‘Member’ role

Returns:

  • (Boolean)


85
86
87
# File 'lib/ajims/lti/tool_provider.rb', line 85

def member?
  has_role?('Member')
end

#mentor?Boolean

Convenience method for checking if the user has ‘Mentor’ role

Returns:

  • (Boolean)


95
96
97
# File 'lib/ajims/lti/tool_provider.rb', line 95

def mentor?
  has_role?('Mentor')
end

#outcome_service?Boolean

Check if the Tool Launch expects an Outcome Result

Returns:

  • (Boolean)


115
116
117
# File 'lib/ajims/lti/tool_provider.rb', line 115

def outcome_service?
  !!(lis_outcome_service_url && lis_result_sourcedid)
end

#post_delete_result!OutcomeResponse

POSTs a delete request to the Tool Consumer

Creates a new OutcomeRequest object and stores it in @outcome_requests

Returns:



138
139
140
# File 'lib/ajims/lti/tool_provider.rb', line 138

def post_delete_result!
  new_request.post_delete_result!
end

#post_read_result!OutcomeResponse

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

Returns:



148
149
150
# 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) ⇒ OutcomeResponse

POSTs the given score to the Tool Consumer with a replaceResult

Creates a new OutcomeRequest object and stores it in @outcome_requests

Returns:



129
130
131
# 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: )
end

#student?Boolean

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

Returns:

  • (Boolean)


70
71
72
# File 'lib/ajims/lti/tool_provider.rb', line 70

def student?
  has_role?('learner') || has_role?('student')
end

#ta?Boolean

Convenience method for checking if the user has ‘TeachingAssistant’ role

Returns:

  • (Boolean)


105
106
107
# File 'lib/ajims/lti/tool_provider.rb', line 105

def ta?
  has_role?('TeachingAssistant')
end

#username(default = nil) ⇒ Object

Return the full, given, or family name if set



120
121
122
# 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