Annotation of loncom/interface/lonexttool.pm, revision 1.11
1.1 raeburn 1: # The LearningOnline Network with CAPA
2: # Launch External Tool Provider (LTI)
3: #
1.11 ! raeburn 4: # $Id: lonexttool.pm,v 1.10 2017/12/07 16:06:04 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 Encode;
47: use Digest::SHA;
48: use HTML::Entities;
49: use Apache::lonlocal;
50: use Apache::lonnet;
51: use Apache::loncommon;
1.9 raeburn 52: use LONCAPA::ltiutils;
1.1 raeburn 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') {
1.11 ! raeburn 70: &Apache::loncommon::simple_error_page($r,'','Not in a course',
! 71: {'only_body' => 1});
1.1 raeburn 72: } else {
73: $r->print('\textbf{Not in a course}\end{document}');
74: }
75: return OK;
76: }
77:
1.3 raeburn 78: my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
1.1 raeburn 79: $marker=~s/\D//g;
80:
81: if (!$marker) {
82: if ($target ne 'tex') {
1.3 raeburn 83: $r->print(&mt('Invalid Call'));
1.1 raeburn 84: } else {
1.3 raeburn 85: $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
1.1 raeburn 86: }
87: return OK;
88: }
89:
90: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
91: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
92: my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
1.3 raeburn 93: my $is_tool;
1.1 raeburn 94:
1.3 raeburn 95: if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
96: my %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
97: if ($toolsettings{'id'}) {
1.6 raeburn 98: my $idx = $toolsettings{'id'};
1.7 raeburn 99: my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
1.6 raeburn 100: if (ref($ltitools{$idx}) eq 'HASH') {
101: my %toolhash = %{$ltitools{$idx}};
1.3 raeburn 102: $toolhash{'display'} = {
103: target => $toolsettings{'target'},
104: width => $toolsettings{'width'},
105: height => $toolsettings{'height'},
106: };
1.4 raeburn 107: $toolhash{'crslabel'} = $toolsettings{'crslabel'};
108: $toolhash{'crstitle'} = $toolsettings{'crstitle'};
1.5 raeburn 109: $toolhash{'crsappend'} = $toolsettings{'crsappend'};
1.3 raeburn 110: $is_tool = 1;
1.6 raeburn 111: my $launchok = 1;
1.3 raeburn 112: if ($target eq 'tex') {
113: $r->print(&mt('External Tool'));
114: } else {
1.6 raeburn 115: my $now = time;
116: if ($toolhash{'passback'}) {
1.9 raeburn 117: if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
118: \%toolsettings,\%toolhash) eq 'ok') {
119: $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
120: } else {
1.6 raeburn 121: undef($launchok);
122: }
123: }
124: if ($toolhash{'roster'}) {
1.9 raeburn 125: if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
126: \%toolsettings,\%toolhash) eq 'ok') {
127: $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
128: }
1.6 raeburn 129: }
1.3 raeburn 130: my $submittext = &mt('Launch [_1]',$toolhash{'title'});
1.6 raeburn 131: if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') &&
132: ($toolhash{'url'} ne '') && ($launchok)) {
133: my %lti = <i_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash);
1.5 raeburn 134: my $url = $toolhash{'url'};
135: if ($toolhash{'crsappend'} ne '') {
136: $url .= $toolhash{'crsappend'};
137: }
138: $r->print(&launch_html($url,$toolhash{'key'},$toolhash{'secret'},
139: $submittext,\%lti));
1.3 raeburn 140: } else {
141: $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
142: }
143: }
1.1 raeburn 144: }
145: }
1.3 raeburn 146: }
147: unless ($is_tool) {
1.1 raeburn 148: if ($target ne 'tex') {
1.3 raeburn 149: $r->print('<div>'.&mt('Invalid Call').'</div>');
1.1 raeburn 150: } else {
1.3 raeburn 151: $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
1.1 raeburn 152: }
153: }
154: return OK;
155: }
156:
157: sub lti_params {
1.6 raeburn 158: my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
1.1 raeburn 159: my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
1.6 raeburn 160: $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
161: %display,%custom,@userlangs);
1.1 raeburn 162: if (ref($toolsref) eq 'HASH') {
163: $version = $toolsref->{'version'};
164: $toolname = $toolsref->{'title'};
165: $passback = $toolsref->{'passback'};
1.6 raeburn 166: $gradesecret = $toolsref->{'gradesecret'};
1.1 raeburn 167: $roster = $toolsref->{'roster'};
1.6 raeburn 168: $rostersecret = $toolsref->{'rostersecret'};
1.1 raeburn 169: $msgtype = $toolsref->{'messagetype'};
170: if (ref($toolsref->{'fields'}) eq 'HASH') {
171: %fields = %{$toolsref->{'fields'}};
172: }
173: if (ref($toolsref->{'roles'}) eq 'HASH') {
174: %rolesmap = %{$toolsref->{'roles'}};
175: }
176: if (ref($toolsref->{'display'}) eq 'HASH') {
177: %display = %{$toolsref->{'display'}};
178: }
179: if (ref($toolsref->{'custom'}) eq 'HASH') {
180: %custom = %{$toolsref->{'custom'}};
181: }
1.4 raeburn 182: $crslabel = $toolsref->{'crslabel'};
183: $crstitle = $toolsref->{'crstitle'};
1.1 raeburn 184: }
185: if ($version eq '') {
186: $version = 'LTI-1p0';
187: }
188: if ($context_type eq '') {
189: $context_type = 'CourseSection';
190: }
191: if ($msgtype eq '') {
192: $msgtype = 'basic-lti-launch-request';
193: }
1.4 raeburn 194: if ($crslabel eq '') {
195: $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
196: }
197: if ($crstitle eq '') {
198: $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'},;
199: }
1.1 raeburn 200: my $lonhost = $r->dir_config('lonHostID');
201: my $loncaparev = $r->dir_config('lonVersion');
202: my $uname = $env{'user.name'};
203: my $udom = $env{'user.domain'};
204: my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
1.3 raeburn 205: my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
206: my $ltirole = $rolesmap{$roleprefix};
1.1 raeburn 207: unless (grep(/^\Q$ltirole\E$/,@possroles)) {
208: $ltirole = 'Learner';
209: }
210: my $digest_user = &Encode::decode_utf8($uname.':'.$udom);
211: $digest_user = &Digest::SHA::sha1_hex($digest_user);
212: if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') {
213: @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
214: $env{'course.'.$env{'request.course.id'}.'.languages'}));
215: } else {
1.2 raeburn 216: my %langhash = &Apache::loncommon::getlangs($uname,$udom);
1.1 raeburn 217: if ($langhash{'languages'} ne '') {
218: @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
219: } else {
220: my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
221: if ($domdefs{'lang_def'} ne '') {
222: @userlangs = ($domdefs{'lang_def'});
223: }
224: }
225: }
226: if (scalar(@userlangs) == 1) {
227: $locale = $userlangs[0];
228: }
229: my ($title,$digest_symb);
230: my ($symb) = &Apache::lonnet::whichuser();
231: if ($symb) {
232: $digest_symb = &Encode::decode_utf8($symb);
233: $digest_symb = &Digest::SHA::sha1_hex($digest_symb);
234: my $navmap = Apache::lonnavmaps::navmap->new();
235: if (ref($navmap)) {
236: my $res = $navmap->getBySymb($symb);
237: if (ref($res)) {
238: $title = $res->compTitle();
239: }
240: }
241: }
1.4 raeburn 242: my $domdesc = &Apache::lonnet::domain($cdom);
243: my $primary_id = &Apache::lonnet::domain($cdom,'primary');
244: my $int_dom = &Apache::lonnet::internet_dom($primary_id);
245: my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom);
246:
1.1 raeburn 247: my %ltiparams = (
248: lti_version => $version,
249: lti_message_type => $msgtype,
250: resource_link_title => $title,
251: resource_link_id => $digest_symb,
252: tool_consumer_instance_guid => $lonhost,
1.4 raeburn 253: tool_consumer_instance_description => $domdesc,
1.1 raeburn 254: tool_consumer_info_product_family_code => 'loncapa',
1.4 raeburn 255: tool_consumer_instance_name => $int_dom,
256: tool_consumer_instance_url => $portal_url,
1.1 raeburn 257: tool_consumer_info_version => $loncaparev,
258: user_id => $digest_user,
259: roles => $ltirole,
260: context_id => $env{'request.course.id'},
261: context_type => $context_type,
1.4 raeburn 262: context_label => $crslabel,
263: context_title => $crstitle,
1.1 raeburn 264: launch_presentation_locale => $locale,
265: );
1.3 raeburn 266: my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
267: my $crshostname = &Apache::lonnet::hostname($crshome);
1.1 raeburn 268: if ($crshostname) {
1.3 raeburn 269: my $crsprotocol = $Apache::lonnet::protocol{$crshome};
1.1 raeburn 270: unless ($crsprotocol eq 'https') {
271: $crsprotocol = 'http';
1.6 raeburn 272: }
273: if (($passback) || ($roster)) {
274: if ($passback) {
1.8 raeburn 275: $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
1.1 raeburn 276: $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
1.6 raeburn 277: if ($gradesecret) {
1.9 raeburn 278: my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
1.10 raeburn 279: $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid);
1.6 raeburn 280: }
281: }
282: if ($roster) {
283: if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
1.8 raeburn 284: $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
1.6 raeburn 285: if ($rostersecret) {
1.9 raeburn 286: my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
1.10 raeburn 287: $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
1.6 raeburn 288: }
289: }
290: }
291: my %digesthash;
292: if ($ltiparams{'lis_result_sourcedid'}) {
293: $digesthash{$ltiparams{'lis_result_sourcedid'}} = "$idx\0".time;
294: }
295: if ($ltiparams{'ext_ims_lis_memberships_id'}) {
296: $digesthash{$ltiparams{'ext_ims_lis_memberships_id'}} = "$idx\0".time;
1.1 raeburn 297: }
1.6 raeburn 298: if (($digest_symb) && ($gradesecret || $rostersecret)) {
299: $digesthash{$digest_symb} = $symb;
300: }
301: if (($passback) && ($gradesecret)) {
302: $digesthash{$digest_user} = $uname.':'.$udom;
303: }
304: if (keys(%digesthash)) {
305: &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
1.1 raeburn 306: }
307: }
308: }
309: if ($display{'target'}) {
310: $ltiparams{'launch_presentation_document_target'} = $display{'target'};
311: }
312: if ($display{'width'}) {
313: $ltiparams{'launch_presentation_width'} = $display{'width'};
314: }
315: if ($display{'height'}) {
316: $ltiparams{'launch_presentation_height'} = $display{'height'};
317: }
318: if ($fields{'firstname'}) {
319: $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'};
320: }
321: if ($fields{'lastname'}) {
322: $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'};
323: }
324: if ($fields{'fullname'}) {
325: $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom);
326: }
327: if ($fields{'email'}) {
328: my %emails = &Apache::loncommon::getemails($uname,$udom);
329: my $contact_email;
1.3 raeburn 330: foreach my $type ('permanentemail','critnotification','notification') {
331: if ($emails{$type} =~ /\@/) {
332: $contact_email = $emails{$type};
1.1 raeburn 333: last;
334: }
335: }
1.2 raeburn 336: $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
1.1 raeburn 337: }
1.4 raeburn 338: if ($fields{'user'}) {
1.6 raeburn 339: $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
1.4 raeburn 340: }
1.1 raeburn 341: if (keys(%custom)) {
342: foreach my $key (keys(%custom)) {
1.5 raeburn 343: my $value = $custom{$key};
344: $value =~ s/^\s+|\s+\$//g;
345: if ($value =~ /^\QLONCAPA::env{\E([^\}]+)\}$/) {
346: if (exists($env{$1})) {
347: $value = $env{$1};
348: }
349: }
350: $ltiparams{'custom_'.$key} = $value;
1.1 raeburn 351: }
352: }
353: foreach my $key (keys(%ltiparams)) {
354: $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
355: }
1.3 raeburn 356: $ltiparams{'basiclti_submit'} = $submittext;
1.1 raeburn 357: return %ltiparams;
358: }
359:
360: sub launch_html {
1.3 raeburn 361: my ($url,$key,$secret,$submittext,$paramsref) = @_;
1.9 raeburn 362: my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref);
1.5 raeburn 363: my $action = &HTML::Entities::encode($url,'<>&"');
1.1 raeburn 364: my $form = <<"END";
365: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
366: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
367: <body>
368: <div id="LCltiLaunch">
1.5 raeburn 369: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
1.1 raeburn 370: END
371: if (ref($hashref) eq 'HASH') {
372: foreach my $item (keys(%{$hashref})) {
1.3 raeburn 373: my $type = 'hidden';
374: if ($item eq 'basiclti_submit') {
375: $type = 'submit';
376: }
377: $form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n";
1.1 raeburn 378: }
379: }
380: $form .= "</form></div>\n";
381: $form .= <<"ENDJS";
382: <script type="text/javascript">
383: document.getElementById("LCltiLaunch").style.display = "none";
384: nei = document.createElement('input');
385: nei.setAttribute('type','hidden');
1.3 raeburn 386: nei.setAttribute('name','basiclti_submit');
1.1 raeburn 387: nei.setAttribute('value','$submittext');
1.3 raeburn 388: document.getElementById("LCltiLaunchFormId").appendChild(nei);
1.1 raeburn 389: document.LCltiLaunchForm.submit();
390: </script>
391: ENDJS
392: $form .= "</body></html>\n";
393: return $form;
394: }
395:
396: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>