File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.4: download - view: text, annotated - select for diffs
Mon Jun 6 17:40:48 2016 UTC (7 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_msu, HEAD
- Bug 6754. Make LON-CAPA an LTI Tool Consumer (LTI 1.1).
  - Domain cofiguration to determine whether context_label, context_title,
    and launch_presentation_document_target LTI parameters can be configured
    for each instance of use of LTI Tool in a course.

    1: # The LearningOnline Network with CAPA
    2: # Launch External Tool Provider (LTI)
    3: #
    4: # $Id: lonexttool.pm,v 1.4 2016/06/06 17:40:48 raeburn Exp $
    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: 
   77:     my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
   78:     $marker=~s/\D//g;
   79: 
   80:     if (!$marker) {
   81:         if ($target ne 'tex') {
   82:             $r->print(&mt('Invalid Call'));
   83:         } else {
   84:             $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
   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'};
   92:     my $is_tool;
   93: 
   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:                                        };
  105:                 $toolhash{'crslabel'} = $toolsettings{'crslabel'};
  106:                 $toolhash{'crstitle'} = $toolsettings{'crstitle'};
  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 '')) {
  113:                         my %lti = &lti_params($r,$cnum,$cdom,$submittext,\%toolhash);
  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:                 }
  120:             }
  121:         }
  122:     }
  123:     unless ($is_tool) {
  124:         if ($target ne 'tex') {
  125:             $r->print('<div>'.&mt('Invalid Call').'</div>');
  126:         } else {
  127:             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
  128:         }
  129:     }
  130:     return OK;
  131: }
  132: 
  133: sub lti_params {
  134:     my ($r,$cnum,$cdom,$submittext,$toolsref) = @_;
  135:     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
  136:         $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);
  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:         }
  155:         $crslabel = $toolsref->{'crslabel'};
  156:         $crstitle = $toolsref->{'crstitle'};
  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:     }
  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:     }
  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);
  178:     my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
  179:     my $ltirole = $rolesmap{$roleprefix};
  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 {
  189:         my %langhash = &Apache::loncommon::getlangs($uname,$udom);
  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:     }
  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: 
  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,
  226:         tool_consumer_instance_description     => $domdesc,
  227:         tool_consumer_info_product_family_code => 'loncapa',
  228:         tool_consumer_instance_name            => $int_dom,  
  229:         tool_consumer_instance_url             => $portal_url,
  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,
  235:         context_label                          => $crslabel,
  236:         context_title                          => $crstitle,
  237:         launch_presentation_locale             => $locale,
  238:     );
  239:     my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
  240:     my $crshostname = &Apache::lonnet::hostname($crshome);
  241:     if ($crshostname) {
  242:         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
  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;
  281:         foreach my $type ('permanentemail','critnotification','notification') {
  282:             if ($emails{$type} =~ /\@/) {
  283:                 $contact_email = $emails{$type};
  284:                 last;
  285:             }
  286:         }
  287:         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
  288:     }
  289:     if ($fields{'user'}) {
  290:         $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; 
  291:     }
  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:     }
  300:     $ltiparams{'basiclti_submit'} = $submittext;
  301:     return %ltiparams;
  302: }
  303: 
  304: sub launch_html {
  305:     my ($url,$key,$secret,$submittext,$paramsref) = @_;
  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">
  312: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$url" method="post" encType="application/x-www-form-urlencoded">
  313: END
  314:     if (ref($hashref) eq 'HASH') {
  315:         foreach my $item (keys(%{$hashref})) {
  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";
  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');
  329:     nei.setAttribute('name','basiclti_submit');
  330:     nei.setAttribute('value','$submittext');
  331:     document.getElementById("LCltiLaunchFormId").appendChild(nei);
  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>