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