File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.20.2.1: download - view: text, annotated - select for diffs
Thu Apr 9 21:11:36 2020 UTC (4 years ago) by raeburn
Branches: version_2_11_2_uiuc
CVS tags: version_2_11_3_msu
- For 2.11.2 (modified)
  Remove passback and roster options, and related routines.

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

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