Commit 0d2d10d7 by Alexander Kryklia

custom parameters

parent 3e3d89e3
...@@ -11,7 +11,7 @@ import urllib ...@@ -11,7 +11,7 @@ import urllib
from xmodule.editing_module import MetadataOnlyEditingDescriptor from xmodule.editing_module import MetadataOnlyEditingDescriptor
from xmodule.x_module import XModule from xmodule.x_module import XModule
from pkg_resources import resource_string from pkg_resources import resource_string
from xblock.core import String, Scope from xblock.core import String, Scope, List
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -29,7 +29,8 @@ class LTIFields(object): ...@@ -29,7 +29,8 @@ class LTIFields(object):
""" """
client_key = String(help="Client key", default='', scope=Scope.settings) client_key = String(help="Client key", default='', scope=Scope.settings)
client_secret = String(help="Client secret", default='', scope=Scope.settings) client_secret = String(help="Client secret", default='', scope=Scope.settings)
lti_url = String(help="URL of the tool", default='', scope=Scope.settings) launch_url = String(help="URL of the tool", default='', scope=Scope.settings)
custom_parameters = List(help="Custom parameters", scope=Scope.settings)
class LTIModule(LTIFields, XModule): class LTIModule(LTIFields, XModule):
...@@ -44,14 +45,29 @@ class LTIModule(LTIFields, XModule): ...@@ -44,14 +45,29 @@ class LTIModule(LTIFields, XModule):
# these params do not participate in oauth signing # these params do not participate in oauth signing
params = { params = {
'lti_url': self.lti_url, 'launch_url': self.launch_url,
'element_id': self.location.html_id(), 'element_id': self.location.html_id(),
'element_class': self.location.category, 'element_class': self.location.category,
} }
params.update(self.oauth_params())
parsed_custom_parameters = {}
for custom_parameter in self.custom_parameters:
try:
param_name, param_value = custom_parameter.split('=')
except ValueError:
raise Exception('Could not parse custom parameter: {0}. \
Should be "x=y" string.'.format(custom_parameter))
# LTI specs: 'custom_' should be prepended before each custom parameter
parsed_custom_parameters.update(
{u'custom_' + unicode(param_name): unicode(param_value)}
)
params.update({'custom_parameters': parsed_custom_parameters})
params.update(self.oauth_params(parsed_custom_parameters))
return self.system.render_template('lti.html', params) return self.system.render_template('lti.html', params)
def oauth_params(self): def oauth_params(self, custom_parameters):
"""Obtains LTI html from provider""" """Obtains LTI html from provider"""
client = requests.auth.Client( client = requests.auth.Client(
client_key=unicode(self.client_key), client_key=unicode(self.client_key),
...@@ -73,11 +89,15 @@ class LTIModule(LTIFields, XModule): ...@@ -73,11 +89,15 @@ class LTIModule(LTIFields, XModule):
'lti_version': 'LTI-1p0', 'lti_version': 'LTI-1p0',
'role': 'student' 'role': 'student'
} }
# appending custom parameter for signing
body.update(custom_parameters)
# This is needed for body encoding: # This is needed for body encoding:
headers = {'Content-Type': 'application/x-www-form-urlencoded'} headers = {'Content-Type': 'application/x-www-form-urlencoded'}
_, headers, _ = client.sign( _, headers, _ = client.sign(
unicode(self.lti_url), unicode(self.launch_url),
http_method=u'POST', http_method=u'POST',
body=body, body=body,
headers=headers) headers=headers)
...@@ -101,5 +121,5 @@ class LTIModule(LTIFields, XModule): ...@@ -101,5 +121,5 @@ class LTIModule(LTIFields, XModule):
class LTIModuleDescriptor(LTIFields, MetadataOnlyEditingDescriptor): class LTIModuleDescriptor(LTIFields, MetadataOnlyEditingDescriptor):
"""LTI Descriptor. No export/import to html.""" """LTI Descriptor. No export/import to xml."""
module_class = LTIModule module_class = LTIModule
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
## module JavaScript will trigget a "submit" on the form, and the ## module JavaScript will trigget a "submit" on the form, and the
## result will be rendered to the below iFrame. ## result will be rendered to the below iFrame.
<form <form
action="${lti_url}" action="${launch_url}"
name="ltiLaunchForm" name="ltiLaunchForm"
class="ltiLaunchForm" class="ltiLaunchForm"
method="post" method="post"
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
<input name="role" value="student" /> <input name="role" value="student" />
<input name="oauth_signature" value="${oauth_signature}" /> <input name="oauth_signature" value="${oauth_signature}" />
% for param_name, param_value in custom_parameters.items():
<input name="${param_name}" value="${param_value}" />
%endfor
<input type="submit" value="Press to Launch" /> <input type="submit" value="Press to Launch" />
</form> </form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment