Module: Lti2Commons::Core

Defined in:
lib/lti2_commons/lib/lti2_commons/lti2_launch.rb

Instance Method Summary collapse

Instance Method Details

#lti2_launch(user, link, return_url) ⇒ Object



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
# File 'lib/lti2_commons/lib/lti2_commons/lti2_launch.rb', line 11

def lti2_launch(user, link, return_url)
  tool_proxy = link.resource.tool.get_tool_proxy

  tool = link.resource.tool
  tool_name = tool_proxy.first_at('tool_profile.product_instance.product_info.product_name.default_value')
  fail "Tool #{tool_name} is currently disabled" unless tool.is_enabled

  base_url = tool_proxy.select('tool_profile.base_url_choice', 'selector.applies_to',
                               'MessageHandler', 'default_base_url')
  resource_type = link.resource.resource_type
  resource_handler_node = tool_proxy.search('tool_profile.resource_handler', { 'resource_type' => resource_type }, '@')
  resource_handler = JsonWrapper.new(resource_handler_node)
  message = resource_handler.search('@..message', { 'message_type' => 'basic-lti-launch-request' }, '@')
  path = message['path']
  tp_parameters = message['parameter']
  service_endpoint = base_url + path

  enrollment = Enrollment.where(admin_user_id: user.id, course_id: link.course.id).first
  if enrollment
    role = enrollment.role
  else
    role = user.role
  end

  tool_consumer_registry = Rails.application.config.tool_consumer_registry
  parameters = {
    'lti_version' => tool_proxy.first_at('lti_version'),
    'lti_message_type' => 'basic-lti-launch-request',
    'resource_link_id' => link.id.to_s,
    'user_id' => user.id.to_s,
    'roles' => role,
    'launch_presentation_return_url' => "#{tool_consumer_registry.tc_deployment_url}#{return_url}",

    # optional
    'context_id' => link.course.id.to_s
  }

  # add parameters from ToolProxy
  tp_parameters.each do |parameter|
    name = parameter['name']
    if parameter.key? 'fixed'
      value = parameter['fixed']
    elsif parameter.key? 'variable'
      value = parameter['variable']
    else
      value = '[link-to-resolve]'
    end
    parameters[name] = value
  end

  # add parameters from Link
  link_parameter_json = link.link_parameters
  if link_parameter_json
    link_parameters = JSON.parse(link_parameter_json)
    parameters.merge! link_parameters
  end

  # auto-create result (if required) and add to reference to parameters
  capabilities = resource_handler.first_at('message[0].enabled_capability')
  if capabilities && capabilities.include?('Result.autocreate')
    if link.grade_item_id
      grade_result = GradeResult.where(link_id: link.id, admin_user_id: user.id).first
      # TEST ONLY
      # grade_result.delete if grade_result
      # grade_result = nil

      if grade_result
        # note that a nil grade_result still let's us through
        unless grade_result.result.nil?
          fail 'Grade result associated with this tool launch has already been filled in'
        end
      else
        grade_result = GradeResult.new
        grade_result.link_id = link.id
        grade_result.admin_user_id = user.id
        grade_result.save
      end
    end
  end

  # perform variable substition on all parameters
  resolver = Resolver.new
  resolver.add_resolver('$User', user.method(:user_resolver))
  resolver.add_resolver('$Person', user.method(:person_resolver))
  course = Course.find(parameters['context_id'])
  resolver.add_resolver('$CourseOffering', course.method(:course_resolver))
  resolver.add_resolver('$Result', grade_result.method(:grade_result_resolver)) if grade_result

  # and resolve and normalize
  final_parameters = {}
  parameters.each do |k, v|
    # only apply substitution when the value looks like a candidate
    if v =~ /^\$\w+\.\w/
      resolved_value = resolver.resolve(v)
    else
      resolved_value = v
    end
    if known_lti2_parameters.include?(k) || deprecated_lti_parameters.include?(k)
      final_parameters[k] = v
    else
      name = 'custom_' + k
      lti1_name = slugify(name)
      final_parameters[lti1_name] = resolved_value unless name == lti1_name
      final_parameters[name] = resolved_value
    end
  end

  key = link.resource.tool.key
  secret = link.resource.tool.secret

  signed_request = Signer.create_signed_request(service_endpoint, 'post', key, secret, final_parameters)
  puts "TC Signed Request: #{signed_request.signature_base_string}"
  puts 'before'
  puts Rails.application.config.wire_log.inspect
  puts 'after'
  body = MessageSupport.create_lti_message_body(
    service_endpoint, final_parameters, Rails.application.config.wire_log, 'Lti Launch')
  puts body
  body
end