File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.11: download - view: text, annotated - select for diffs
Mon Dec 18 16:11:18 2017 UTC (6 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Option to suppress menu when displaying loncommon::simple_error_page()
  within a wrapped page.

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

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