Annotation of loncom/interface/lonexttool.pm, revision 1.5

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>