File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.9: download - view: text, annotated - select for diffs
Thu Dec 7 15:36:34 2017 UTC (6 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Bug 6754 LTI Integration
- ltiutils.pm contains common LTI routines (to facilitate re-use).
  - &check_nonce() moved from ltiauth.pm to ltiutils.pm.
  - &set_callback_secret() moved from lonexttool.pm to ltiutils.pm, and
    renamed &set_service_secret().
  - &sign_params(), &get_tool_lock(), and &release_tool_lock() moved from
    lonexttool.pm to ltiutils.pm
- ltiroster.pm will service requests for course membership information
  from an LTI Provider, where domain configuration for Tool permits this
  service, and launch (by Instructor( included ext_ims_lis_memberships_id.

    1: # The LearningOnline Network with CAPA
    2: # Launch External Tool Provider (LTI)
    3: #
    4: # $Id: lonexttool.pm,v 1.9 2017/12/07 15:36:34 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 Encode;
   47: use Digest::SHA;
   48: use HTML::Entities;
   49: use Apache::lonlocal;
   50: use Apache::lonnet;
   51: use Apache::loncommon;
   52: use LONCAPA::ltiutils;
   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 $idx = $toolsettings{'id'};
   98:             my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
   99:             if (ref($ltitools{$idx}) eq 'HASH') {
  100:                 my %toolhash = %{$ltitools{$idx}}; 
  101:                 $toolhash{'display'} = {
  102:                                            target => $toolsettings{'target'},
  103:                                            width  => $toolsettings{'width'},
  104:                                            height => $toolsettings{'height'},
  105:                                        };
  106:                 $toolhash{'crslabel'} = $toolsettings{'crslabel'};
  107:                 $toolhash{'crstitle'} = $toolsettings{'crstitle'};
  108:                 $toolhash{'crsappend'} = $toolsettings{'crsappend'};
  109:                 $is_tool = 1;
  110:                 my $launchok = 1;
  111:                 if ($target eq 'tex') {
  112:                     $r->print(&mt('External Tool'));
  113:                 } else {
  114:                     my $now = time;
  115:                     if ($toolhash{'passback'}) {
  116:                         if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
  117:                                                                         \%toolsettings,\%toolhash) eq 'ok') {
  118:                             $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
  119:                         } else {
  120:                             undef($launchok);
  121:                         }
  122:                     }
  123:                     if ($toolhash{'roster'}) {
  124:                         if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
  125:                                                                 \%toolsettings,\%toolhash) eq 'ok') {
  126:                             $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
  127:                         }
  128:                     }
  129:                     my $submittext = &mt('Launch [_1]',$toolhash{'title'});
  130:                     if (($toolhash{'key'} ne '') && ($toolhash{'secret'} ne '') && 
  131:                         ($toolhash{'url'} ne '') && ($launchok)) {
  132:                         my %lti = &lti_params($r,$cnum,$cdom,$idx,$submittext,\%toolhash);
  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));
  139:                     } else {
  140:                         $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
  141:                     }
  142:                 }
  143:             }
  144:         }
  145:     }
  146:     unless ($is_tool) {
  147:         if ($target ne 'tex') {
  148:             $r->print('<div>'.&mt('Invalid Call').'</div>');
  149:         } else {
  150:             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
  151:         }
  152:     }
  153:     return OK;
  154: }
  155: 
  156: sub lti_params {
  157:     my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
  158:     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
  159:         $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
  160:         %display,%custom,@userlangs);
  161:     if (ref($toolsref) eq 'HASH') {
  162:         $version = $toolsref->{'version'};
  163:         $toolname = $toolsref->{'title'};
  164:         $passback = $toolsref->{'passback'};
  165:         $gradesecret = $toolsref->{'gradesecret'};
  166:         $roster = $toolsref->{'roster'};
  167:         $rostersecret = $toolsref->{'rostersecret'};
  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:         }
  181:         $crslabel = $toolsref->{'crslabel'};
  182:         $crstitle = $toolsref->{'crstitle'};
  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:     }
  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:     }
  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);
  204:     my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
  205:     my $ltirole = $rolesmap{$roleprefix};
  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 {
  215:         my %langhash = &Apache::loncommon::getlangs($uname,$udom);
  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:     }
  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: 
  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,
  252:         tool_consumer_instance_description     => $domdesc,
  253:         tool_consumer_info_product_family_code => 'loncapa',
  254:         tool_consumer_instance_name            => $int_dom,  
  255:         tool_consumer_instance_url             => $portal_url,
  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,
  261:         context_label                          => $crslabel,
  262:         context_title                          => $crstitle,
  263:         launch_presentation_locale             => $locale,
  264:     );
  265:     my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
  266:     my $crshostname = &Apache::lonnet::hostname($crshome);
  267:     if ($crshostname) {
  268:         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
  269:         unless ($crsprotocol eq 'https') {
  270:             $crsprotocol = 'http';
  271:         }
  272:         if (($passback) || ($roster)) {
  273:             if ($passback) {
  274:                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
  275:                 $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
  276:                 if ($gradesecret) {
  277:                     my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
  278:                     $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_unique_callback($gradesecret,$uniqid); 
  279:                 }
  280:             }
  281:             if ($roster) {
  282:                 if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
  283:                     $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
  284:                     if ($rostersecret) {
  285:                         my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
  286:                         $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_unique_callback($rostersecret,$uniqid);
  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; 
  296:             }
  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);
  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;
  329:         foreach my $type ('permanentemail','critnotification','notification') {
  330:             if ($emails{$type} =~ /\@/) {
  331:                 $contact_email = $emails{$type};
  332:                 last;
  333:             }
  334:         }
  335:         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
  336:     }
  337:     if ($fields{'user'}) {
  338:         $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
  339:     }
  340:     if (keys(%custom)) {
  341:         foreach my $key (keys(%custom)) {
  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;
  350:         }
  351:     }
  352:     foreach my $key (keys(%ltiparams)) {
  353:         $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
  354:     }
  355:     $ltiparams{'basiclti_submit'} = $submittext;
  356:     return %ltiparams;
  357: }
  358: 
  359: sub launch_html {
  360:     my ($url,$key,$secret,$submittext,$paramsref) = @_;
  361:     my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$paramsref);
  362:     my $action = &HTML::Entities::encode($url,'<>&"');
  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">
  368: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
  369: END
  370:     if (ref($hashref) eq 'HASH') {
  371:         foreach my $item (keys(%{$hashref})) {
  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";
  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');
  385:     nei.setAttribute('name','basiclti_submit');
  386:     nei.setAttribute('value','$submittext');
  387:     document.getElementById("LCltiLaunchFormId").appendChild(nei);
  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>