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