Annotation of loncom/interface/lonexttool.pm, revision 1.4
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Launch External Tool Provider (LTI)
3: #
1.4 ! raeburn 4: # $Id: lonexttool.pm,v 1.3 2016/01/26 14:30:25 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: };
1.4 ! raeburn 105: $toolhash{'crslabel'} = $toolsettings{'crslabel'};
! 106: $toolhash{'crstitle'} = $toolsettings{'crstitle'};
1.3 raeburn 107: $is_tool = 1;
108: if ($target eq 'tex') {
109: $r->print(&mt('External Tool'));
110: } else {
111: my $submittext = &mt('Launch [_1]',$toolhash{'title'});
112: if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && ($toolhash{'url'} ne '')) {
1.4 ! raeburn 113: my %lti = <i_params($r,$cnum,$cdom,$submittext,\%toolhash);
1.3 raeburn 114: $r->print(&launch_html($toolhash{'url'},$toolhash{'key'},
115: $toolhash{'secret'},$submittext,\%lti));
116: } else {
117: $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
118: }
119: }
1.1 raeburn 120: }
121: }
1.3 raeburn 122: }
123: unless ($is_tool) {
1.1 raeburn 124: if ($target ne 'tex') {
1.3 raeburn 125: $r->print('<div>'.&mt('Invalid Call').'</div>');
1.1 raeburn 126: } else {
1.3 raeburn 127: $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
1.1 raeburn 128: }
129: }
130: return OK;
131: }
132:
133: sub lti_params {
1.4 ! raeburn 134: my ($r,$cnum,$cdom,$submittext,$toolsref) = @_;
1.1 raeburn 135: my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
1.4 ! raeburn 136: $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);
1.1 raeburn 137: if (ref($toolsref) eq 'HASH') {
138: $version = $toolsref->{'version'};
139: $toolname = $toolsref->{'title'};
140: $passback = $toolsref->{'passback'};
141: $roster = $toolsref->{'roster'};
142: $msgtype = $toolsref->{'messagetype'};
143: if (ref($toolsref->{'fields'}) eq 'HASH') {
144: %fields = %{$toolsref->{'fields'}};
145: }
146: if (ref($toolsref->{'roles'}) eq 'HASH') {
147: %rolesmap = %{$toolsref->{'roles'}};
148: }
149: if (ref($toolsref->{'display'}) eq 'HASH') {
150: %display = %{$toolsref->{'display'}};
151: }
152: if (ref($toolsref->{'custom'}) eq 'HASH') {
153: %custom = %{$toolsref->{'custom'}};
154: }
1.4 ! raeburn 155: $crslabel = $toolsref->{'crslabel'};
! 156: $crstitle = $toolsref->{'crstitle'};
1.1 raeburn 157: }
158: if ($version eq '') {
159: $version = 'LTI-1p0';
160: }
161: if ($context_type eq '') {
162: $context_type = 'CourseSection';
163: }
164: if ($msgtype eq '') {
165: $msgtype = 'basic-lti-launch-request';
166: }
1.4 ! raeburn 167: if ($crslabel eq '') {
! 168: $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
! 169: }
! 170: if ($crstitle eq '') {
! 171: $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'},;
! 172: }
1.1 raeburn 173: my $lonhost = $r->dir_config('lonHostID');
174: my $loncaparev = $r->dir_config('lonVersion');
175: my $uname = $env{'user.name'};
176: my $udom = $env{'user.domain'};
177: my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
1.3 raeburn 178: my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
179: my $ltirole = $rolesmap{$roleprefix};
1.1 raeburn 180: unless (grep(/^\Q$ltirole\E$/,@possroles)) {
181: $ltirole = 'Learner';
182: }
183: my $digest_user = &Encode::decode_utf8($uname.':'.$udom);
184: $digest_user = &Digest::SHA::sha1_hex($digest_user);
185: if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') {
186: @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
187: $env{'course.'.$env{'request.course.id'}.'.languages'}));
188: } else {
1.2 raeburn 189: my %langhash = &Apache::loncommon::getlangs($uname,$udom);
1.1 raeburn 190: if ($langhash{'languages'} ne '') {
191: @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
192: } else {
193: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
194: if ($domdefs{'lang_def'} ne '') {
195: @userlangs = ($domdefs{'lang_def'});
196: }
197: }
198: }
199: if (scalar(@userlangs) == 1) {
200: $locale = $userlangs[0];
201: }
202: my ($title,$digest_symb);
203: my ($symb) = &Apache::lonnet::whichuser();
204: if ($symb) {
205: $digest_symb = &Encode::decode_utf8($symb);
206: $digest_symb = &Digest::SHA::sha1_hex($digest_symb);
207: my $navmap = Apache::lonnavmaps::navmap->new();
208: if (ref($navmap)) {
209: my $res = $navmap->getBySymb($symb);
210: if (ref($res)) {
211: $title = $res->compTitle();
212: }
213: }
214: }
1.4 ! raeburn 215: my $domdesc = &Apache::lonnet::domain($cdom);
! 216: my $primary_id = &Apache::lonnet::domain($cdom,'primary');
! 217: my $int_dom = &Apache::lonnet::internet_dom($primary_id);
! 218: my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom);
! 219:
1.1 raeburn 220: my %ltiparams = (
221: lti_version => $version,
222: lti_message_type => $msgtype,
223: resource_link_title => $title,
224: resource_link_id => $digest_symb,
225: tool_consumer_instance_guid => $lonhost,
1.4 ! raeburn 226: tool_consumer_instance_description => $domdesc,
1.1 raeburn 227: tool_consumer_info_product_family_code => 'loncapa',
1.4 ! raeburn 228: tool_consumer_instance_name => $int_dom,
! 229: tool_consumer_instance_url => $portal_url,
1.1 raeburn 230: tool_consumer_info_version => $loncaparev,
231: user_id => $digest_user,
232: roles => $ltirole,
233: context_id => $env{'request.course.id'},
234: context_type => $context_type,
1.4 ! raeburn 235: context_label => $crslabel,
! 236: context_title => $crstitle,
1.1 raeburn 237: launch_presentation_locale => $locale,
238: );
1.3 raeburn 239: my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
240: my $crshostname = &Apache::lonnet::hostname($crshome);
1.1 raeburn 241: if ($crshostname) {
1.3 raeburn 242: my $crsprotocol = $Apache::lonnet::protocol{$crshome};
1.1 raeburn 243: unless ($crsprotocol eq 'https') {
244: $crsprotocol = 'http';
245: }
246: if ($passback) {
247: if ($ltirole eq 'Learner') {
248: $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltipassback';
249: $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
250: $ltiparams{'lis_result_sourcedid'} = ''; #FIXME
251: }
252: }
253: if ($roster) {
254: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
255: $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/ltiroster';
256: $ltiparams{'ext_ims_lis_memberships_id'} = ''; #FIXME
257: }
258: }
259: }
260: if ($display{'target'}) {
261: $ltiparams{'launch_presentation_document_target'} = $display{'target'};
262: }
263: if ($display{'width'}) {
264: $ltiparams{'launch_presentation_width'} = $display{'width'};
265: }
266: if ($display{'height'}) {
267: $ltiparams{'launch_presentation_height'} = $display{'height'};
268: }
269: if ($fields{'firstname'}) {
270: $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'};
271: }
272: if ($fields{'lastname'}) {
273: $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'};
274: }
275: if ($fields{'fullname'}) {
276: $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom);
277: }
278: if ($fields{'email'}) {
279: my %emails = &Apache::loncommon::getemails($uname,$udom);
280: my $contact_email;
1.3 raeburn 281: foreach my $type ('permanentemail','critnotification','notification') {
282: if ($emails{$type} =~ /\@/) {
283: $contact_email = $emails{$type};
1.1 raeburn 284: last;
285: }
286: }
1.2 raeburn 287: $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
1.1 raeburn 288: }
1.4 ! raeburn 289: if ($fields{'user'}) {
! 290: $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
! 291: }
1.1 raeburn 292: if (keys(%custom)) {
293: foreach my $key (keys(%custom)) {
294: $ltiparams{'custom_'.$key} = $custom{$key};
295: }
296: }
297: foreach my $key (keys(%ltiparams)) {
298: $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
299: }
1.3 raeburn 300: $ltiparams{'basiclti_submit'} = $submittext;
1.1 raeburn 301: return %ltiparams;
302: }
303:
304: sub launch_html {
1.3 raeburn 305: my ($url,$key,$secret,$submittext,$paramsref) = @_;
1.1 raeburn 306: my $hashref = &sign_params($url,$key,$secret,$paramsref);
307: my $form = <<"END";
308: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
309: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
310: <body>
311: <div id="LCltiLaunch">
1.3 raeburn 312: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$url" method="post" encType="application/x-www-form-urlencoded">
1.1 raeburn 313: END
314: if (ref($hashref) eq 'HASH') {
315: foreach my $item (keys(%{$hashref})) {
1.3 raeburn 316: my $type = 'hidden';
317: if ($item eq 'basiclti_submit') {
318: $type = 'submit';
319: }
320: $form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n";
1.1 raeburn 321: }
322: }
323: $form .= "</form></div>\n";
324: $form .= <<"ENDJS";
325: <script type="text/javascript">
326: document.getElementById("LCltiLaunch").style.display = "none";
327: nei = document.createElement('input');
328: nei.setAttribute('type','hidden');
1.3 raeburn 329: nei.setAttribute('name','basiclti_submit');
1.1 raeburn 330: nei.setAttribute('value','$submittext');
1.3 raeburn 331: document.getElementById("LCltiLaunchFormId").appendChild(nei);
1.1 raeburn 332: document.LCltiLaunchForm.submit();
333: </script>
334: ENDJS
335: $form .= "</body></html>\n";
336: return $form;
337: }
338:
339: sub sign_params {
340: my ($url,$key,$secret,$paramsref) = @_;
341: my $nonce = Digest::SHA::sha1_hex(sprintf("%06x%06x",rand(0xfffff0),rand(0xfffff0)));
342: my $request = Net::OAuth->request("request token")->new(
343: consumer_key => $key,
344: consumer_secret => $secret,
345: request_url => $url,
346: request_method => 'POST',
347: signature_method => 'HMAC-SHA1',
348: timestamp => time,
349: nonce => $nonce,
350: callback => 'about:blank',
351: extra_params => $paramsref,
352: version => '1.0',
353: );
354: $request->sign;
355: return $request->to_hash();
356: }
357:
358: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>