Annotation of loncom/interface/lonexttool.pm, revision 1.3
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Launch External Tool Provider (LTI)
3: #
1.3 ! raeburn 4: # $Id: lonexttool.pm,v 1.2 2016/01/25 20:13:02 raeburn Exp $
1.1 raeburn 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: =pod
30:
31: =head1 NAME
32:
33: Apache::lonexttool - Tool Provider launcher
34:
35: =head1 SYNOPSIS
36:
37:
38: =head1 OVERVIEW
39:
40: =cut
41:
42: package Apache::lonexttool;
43:
44: use strict;
45: use Apache::Constants qw(:common :http);
46: use Net::OAuth;
47: use Encode;
48: use Digest::SHA;
49: use HTML::Entities;
50: use Apache::lonlocal;
51: use Apache::lonnet;
52: use Apache::loncommon;
53:
54: sub handler {
55: my $r=shift;
56: &Apache::loncommon::content_type($r,'text/html');
57: $r->send_http_header;
58:
59: return OK if $r->header_only;
60:
61: my $target=$env{'form.grade_target'};
62: # ------------------------------------------------------------ Print the screen
63: if ($target eq 'tex') {
64: $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
65: }
66:
67: # Is this even in a course?
68: unless ($env{'request.course.id'}) {
69: if ($target ne 'tex') {
70: &Apache::loncommon::simple_error_page($r,'','Not in a course');
71: } else {
72: $r->print('\textbf{Not in a course}\end{document}');
73: }
74: return OK;
75: }
76:
1.3 ! raeburn 77: my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
1.1 raeburn 78: $marker=~s/\D//g;
79:
80: if (!$marker) {
81: if ($target ne 'tex') {
1.3 ! raeburn 82: $r->print(&mt('Invalid Call'));
1.1 raeburn 83: } else {
1.3 ! raeburn 84: $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
1.1 raeburn 85: }
86: return OK;
87: }
88:
89: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
90: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
91: my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
1.3 ! raeburn 92: my $is_tool;
1.1 raeburn 93:
1.3 ! raeburn 94: if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
! 95: my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
! 96: if ($toolsettings{'id'}) {
! 97: my %ltitools = &Apache::lonnet::get_domain_ltitools($cdom);
! 98: if (ref($ltitools{$toolsettings{'id'}}) eq 'HASH') {
! 99: my %toolhash = %{$ltitools{$toolsettings{'id'}}};
! 100: $toolhash{'display'} = {
! 101: target => $toolsettings{'target'},
! 102: width => $toolsettings{'width'},
! 103: height => $toolsettings{'height'},
! 104: };
! 105: $is_tool = 1;
! 106: if ($target eq 'tex') {
! 107: $r->print(&mt('External Tool'));
! 108: } else {
! 109: my $submittext = &mt('Launch [_1]',$toolhash{'title'});
! 110: if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {
! 111: my %lti = <i_params($r,$submittext,\%toolhash);
! 112: $r->print(&launch_html($toolhash{'url'},$toolhash{'key'},
! 113: $toolhash{'secret'},$submittext,\%lti));
! 114: } else {
! 115: $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
! 116: }
! 117: }
1.1 raeburn 118: }
119: }
1.3 ! raeburn 120: }
! 121: unless ($is_tool) {
1.1 raeburn 122: if ($target ne 'tex') {
1.3 ! raeburn 123: $r->print('<div>'.&mt('Invalid Call').'</div>');
1.1 raeburn 124: } else {
1.3 ! raeburn 125: $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
1.1 raeburn 126: }
127: }
128: return OK;
129: }
130:
131: sub lti_params {
1.3 ! raeburn 132: my ($r,$submittext,$toolsref) = @_;
1.1 raeburn 133: my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
134: %fields,%rolesmap,%display,%custom,@userlangs);
135: if (ref($toolsref) eq 'HASH') {
136: $version = $toolsref->{'version'};
137: $toolname = $toolsref->{'title'};
138: $passback = $toolsref->{'passback'};
139: $roster = $toolsref->{'roster'};
140: $msgtype = $toolsref->{'messagetype'};
141: if (ref($toolsref->{'fields'}) eq 'HASH') {
142: %fields = %{$toolsref->{'fields'}};
143: }
144: if (ref($toolsref->{'roles'}) eq 'HASH') {
145: %rolesmap = %{$toolsref->{'roles'}};
146: }
147: if (ref($toolsref->{'display'}) eq 'HASH') {
148: %display = %{$toolsref->{'display'}};
149: }
150: if (ref($toolsref->{'custom'}) eq 'HASH') {
151: %custom = %{$toolsref->{'custom'}};
152: }
153: }
154: if ($version eq '') {
155: $version = 'LTI-1p0';
156: }
157: if ($context_type eq '') {
158: $context_type = 'CourseSection';
159: }
160: if ($msgtype eq '') {
161: $msgtype = 'basic-lti-launch-request';
162: }
163: my $lonhost = $r->dir_config('lonHostID');
164: my $loncaparev = $r->dir_config('lonVersion');
165: my $uname = $env{'user.name'};
166: my $udom = $env{'user.domain'};
167: my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
1.3 ! raeburn 168: my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
! 169: my $ltirole = $rolesmap{$roleprefix};
1.1 raeburn 170: unless (grep(/^\Q$ltirole\E$/,@possroles)) {
171: $ltirole = 'Learner';
172: }
173: my $digest_user = &Encode::decode_utf8($uname.':'.$udom);
174: $digest_user = &Digest::SHA::sha1_hex($digest_user);
175: if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') {
176: @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
177: $env{'course.'.$env{'request.course.id'}.'.languages'}));
178: } else {
1.2 raeburn 179: my %langhash = &Apache::loncommon::getlangs($uname,$udom);
1.1 raeburn 180: if ($langhash{'languages'} ne '') {
181: @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
182: } else {
183: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
184: if ($domdefs{'lang_def'} ne '') {
185: @userlangs = ($domdefs{'lang_def'});
186: }
187: }
188: }
189: if (scalar(@userlangs) == 1) {
190: $locale = $userlangs[0];
191: }
192: my ($title,$digest_symb);
193: my ($symb) = &Apache::lonnet::whichuser();
194: if ($symb) {
195: $digest_symb = &Encode::decode_utf8($symb);
196: $digest_symb = &Digest::SHA::sha1_hex($digest_symb);
197: my $navmap = Apache::lonnavmaps::navmap->new();
198: if (ref($navmap)) {
199: my $res = $navmap->getBySymb($symb);
200: if (ref($res)) {
201: $title = $res->compTitle();
202: }
203: }
204: }
205: my %ltiparams = (
206: lti_version => $version,
207: lti_message_type => $msgtype,
208: resource_link_title => $title,
209: resource_link_id => $digest_symb,
210: tool_consumer_instance_guid => $lonhost,
211: tool_consumer_info_product_family_code => 'loncapa',
212: tool_consumer_info_version => $loncaparev,
213: user_id => $digest_user,
214: lis_person_sourcedid => $uname.':'.$udom,
215: roles => $ltirole,
216: context_id => $env{'request.course.id'},
217: context_type => $context_type,
218: context_label => $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
219: context_title => $env{'course.'.$env{'request.course.id'}.'.description'},
220: launch_presentation_locale => $locale,
221: );
1.3 ! raeburn 222: my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
! 223: my $crshostname = &Apache::lonnet::hostname($crshome);
1.1 raeburn 224: if ($crshostname) {
1.3 ! raeburn 225: my $crsprotocol = $Apache::lonnet::protocol{$crshome};
1.1 raeburn 226: unless ($crsprotocol eq 'https') {
227: $crsprotocol = 'http';
228: }
229: if ($passback) {
230: if ($ltirole eq 'Learner') {
231: $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltipassback';
232: $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
233: $ltiparams{'lis_result_sourcedid'} = ''; #FIXME
234: }
235: }
236: if ($roster) {
237: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
238: $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltiroster';
239: $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME
240: }
241: }
242: }
243: if ($display{'target'}) {
244: $ltiparams{'launch_presentation_document_target'} = $display{'target'};
245: }
246: if ($display{'width'}) {
247: $ltiparams{'launch_presentation_width'} = $display{'width'};
248: }
249: if ($display{'height'}) {
250: $ltiparams{'launch_presentation_height'} = $display{'height'};
251: }
252: if ($fields{'firstname'}) {
253: $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'};
254: }
255: if ($fields{'lastname'}) {
256: $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'};
257: }
258: if ($fields{'fullname'}) {
259: $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom);
260: }
261: if ($fields{'email'}) {
262: my %emails = &Apache::loncommon::getemails($uname,$udom);
263: my $contact_email;
1.3 ! raeburn 264: foreach my $type ('permanentemail','critnotification','notification') {
! 265: if ($emails{$type} =~ /\@/) {
! 266: $contact_email = $emails{$type};
1.1 raeburn 267: last;
268: }
269: }
1.2 raeburn 270: $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
1.1 raeburn 271: }
272: if (keys(%custom)) {
273: foreach my $key (keys(%custom)) {
274: $ltiparams{'custom_'.$key} = $custom{$key};
275: }
276: }
277: foreach my $key (keys(%ltiparams)) {
278: $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
279: }
1.3 ! raeburn 280: $ltiparams{'basiclti_submit'} = $submittext;
1.1 raeburn 281: return %ltiparams;
282: }
283:
284: sub launch_html {
1.3 ! raeburn 285: my ($url,$key,$secret,$submittext,$paramsref) = @_;
1.1 raeburn 286: my $hashref = &sign_params($url,$key,$secret,$paramsref);
287: my $form = <<"END";
288: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
289: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
290: <body>
291: <div id="LCltiLaunch">
1.3 ! raeburn 292: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$url" method="post" encType="application/x-www-form-urlencoded">
1.1 raeburn 293: END
294: if (ref($hashref) eq 'HASH') {
295: foreach my $item (keys(%{$hashref})) {
1.3 ! raeburn 296: my $type = 'hidden';
! 297: if ($item eq 'basiclti_submit') {
! 298: $type = 'submit';
! 299: }
! 300: $form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n";
1.1 raeburn 301: }
302: }
303: $form .= "</form></div>\n";
304: $form .= <<"ENDJS";
305: <script type="text/javascript">
306: document.getElementById("LCltiLaunch").style.display = "none";
307: nei = document.createElement('input');
308: nei.setAttribute('type','hidden');
1.3 ! raeburn 309: nei.setAttribute('name','basiclti_submit');
1.1 raeburn 310: nei.setAttribute('value','$submittext');
1.3 ! raeburn 311: document.getElementById("LCltiLaunchFormId").appendChild(nei);
1.1 raeburn 312: document.LCltiLaunchForm.submit();
313: </script>
314: ENDJS
315: $form .= "</body></html>\n";
316: return $form;
317: }
318:
319: sub sign_params {
320: my ($url,$key,$secret,$paramsref) = @_;
321: my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0)));
322: my $request = Net::OAuth->request("request token")->new(
323: consumer_key => $key,
324: consumer_secret => $secret,
325: request_url => $url,
326: request_method => 'POST',
327: signature_method => 'HMAC-SHA1',
328: timestamp => time,
329: nonce => $nonce,
330: callback => 'about:blank',
331: extra_params => $paramsref,
332: version => '1.0',
333: );
334: $request->sign;
335: return $request->to_hash();
336: }
337:
338: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>