File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.5: download - view: text, annotated - select for diffs
Thu Nov 30 01:52:14 2017 UTC (6 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- LON-CAPA as LTI Consumer.
  - Value in custom field passed to Tool Provider on launch can be the value
    of an item in %env. (Include LONCAPA::env{<var name>} when adding the
    custom field in the domain configuration, where <var name> is the
    env var for which the value is to be sent).
  - Course Coordinator can set a path to append to the standard URL to
    which the signed POSTed data are sent. (Path can be different for
    each tool instance).

    1: # The LearningOnline Network with CAPA
    2: # Launch External Tool Provider (LTI)
    3: #
    4: # $Id: lonexttool.pm,v 1.5 2017/11/30 01:52:14 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:                 $toolhash{'crsappend'} = $toolsettings{'crsappend'};
  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 '')) {
  114:                         my %lti = &lti_params($r,$cnum,$cdom,$submittext,\%toolhash);
  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));
  121:                     } else {
  122:                         $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
  123:                     }
  124:                 }
  125:             }
  126:         }
  127:     }
  128:     unless ($is_tool) {
  129:         if ($target ne 'tex') {
  130:             $r->print('<div>'.&mt('Invalid Call').'</div>');
  131:         } else {
  132:             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
  133:         }
  134:     }
  135:     return OK;
  136: }
  137: 
  138: sub lti_params {
  139:     my ($r,$cnum,$cdom,$submittext,$toolsref) = @_;
  140:     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
  141:         $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs);
  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:         }
  160:         $crslabel = $toolsref->{'crslabel'};
  161:         $crstitle = $toolsref->{'crstitle'};
  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:     }
  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:     }
  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);
  183:     my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
  184:     my $ltirole = $rolesmap{$roleprefix};
  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 {
  194:         my %langhash = &Apache::loncommon::getlangs($uname,$udom);
  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:     }
  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: 
  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,
  231:         tool_consumer_instance_description     => $domdesc,
  232:         tool_consumer_info_product_family_code => 'loncapa',
  233:         tool_consumer_instance_name            => $int_dom,  
  234:         tool_consumer_instance_url             => $portal_url,
  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,
  240:         context_label                          => $crslabel,
  241:         context_title                          => $crstitle,
  242:         launch_presentation_locale             => $locale,
  243:     );
  244:     my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
  245:     my $crshostname = &Apache::lonnet::hostname($crshome);
  246:     if ($crshostname) {
  247:         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
  248:         unless ($crsprotocol eq 'https') {
  249:             $crsprotocol = 'http';
  250:         } 
  251:         if ($passback) {
  252:             if ($ltirole eq 'Learner') {
  253:                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/passback';
  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'})) {
  260:                 $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'//'.$crshostname.'/adm/service/roster';
  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;
  286:         foreach my $type ('permanentemail','critnotification','notification') {
  287:             if ($emails{$type} =~ /\@/) {
  288:                 $contact_email = $emails{$type};
  289:                 last;
  290:             }
  291:         }
  292:         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
  293:     }
  294:     if ($fields{'user'}) {
  295:         $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom; 
  296:     }
  297:     if (keys(%custom)) {
  298:         foreach my $key (keys(%custom)) {
  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;
  307:         }
  308:     }
  309:     foreach my $key (keys(%ltiparams)) {
  310:         $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
  311:     }
  312:     $ltiparams{'basiclti_submit'} = $submittext;
  313:     return %ltiparams;
  314: }
  315: 
  316: sub launch_html {
  317:     my ($url,$key,$secret,$submittext,$paramsref) = @_;
  318:     my $hashref = &sign_params($url,$key,$secret,$paramsref);
  319:     my $action = &HTML::Entities::encode($url,'<>&"');
  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">
  325: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
  326: END
  327:     if (ref($hashref) eq 'HASH') {
  328:         foreach my $item (keys(%{$hashref})) {
  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";
  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');
  342:     nei.setAttribute('name','basiclti_submit');
  343:     nei.setAttribute('value','$submittext');
  344:     document.getElementById("LCltiLaunchFormId").appendChild(nei);
  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>