Annotation of loncom/interface/lonexttool.pm, revision 1.20.2.1

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Launch External Tool Provider (LTI)
                      3: #
1.20.2.1! raeburn     4: # $Id: lonexttool.pm,v 1.20 2018/08/14 18:20:17 raeburn Exp $
1.1       raeburn     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;
1.9       raeburn    52: use LONCAPA::ltiutils;
1.1       raeburn    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'}));
1.12      raeburn    65:     } else {
                     66:         $target = 'web';
1.1       raeburn    67:     }
                     68: 
                     69: # Is this even in a course?
                     70:     unless ($env{'request.course.id'}) {
                     71:         if ($target ne 'tex') {
1.11      raeburn    72:             &Apache::loncommon::simple_error_page($r,'','Not in a course',
                     73:                                                   {'only_body' => 1});
1.1       raeburn    74:         } else {
                     75:             $r->print('\textbf{Not in a course}\end{document}');
                     76:         }
                     77:         return OK;
                     78:     }
                     79: 
1.3       raeburn    80:     my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
1.1       raeburn    81:     $marker=~s/\D//g;
                     82: 
                     83:     if (!$marker) {
                     84:         if ($target ne 'tex') {
1.3       raeburn    85:             $r->print(&mt('Invalid Call'));
1.1       raeburn    86:         } else {
1.3       raeburn    87:             $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
1.1       raeburn    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'};
1.12      raeburn    95:     my ($idx,$is_tool,%toolhash,%toolsettings);
1.1       raeburn    96: 
1.3       raeburn    97:     if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
1.12      raeburn    98:         %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
1.3       raeburn    99:         if ($toolsettings{'id'}) {
1.12      raeburn   100:             $idx = $toolsettings{'id'};
1.7       raeburn   101:             my %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
1.6       raeburn   102:             if (ref($ltitools{$idx}) eq 'HASH') {
1.12      raeburn   103:                 %toolhash = %{$ltitools{$idx}};
1.3       raeburn   104:                 $toolhash{'display'} = {
                    105:                                            target => $toolsettings{'target'},
                    106:                                            width  => $toolsettings{'width'},
                    107:                                            height => $toolsettings{'height'},
                    108:                                        };
1.20.2.1! raeburn   109:                 foreach my $item (qw(crslabel crstitle crsappend)) {
1.16      raeburn   110:                     $toolhash{$item} = $toolsettings{$item};
                    111:                 }
1.3       raeburn   112:                 $is_tool = 1;
1.1       raeburn   113:             }
                    114:         }
1.3       raeburn   115:     }
                    116:     unless ($is_tool) {
1.1       raeburn   117:         if ($target ne 'tex') {
1.3       raeburn   118:             $r->print('<div>'.&mt('Invalid Call').'</div>');
1.1       raeburn   119:         } else {
1.3       raeburn   120:             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
1.1       raeburn   121:         }
1.12      raeburn   122:         return OK;
                    123:     }
                    124: 
1.15      raeburn   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:     }
1.20.2.1! raeburn   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);
1.12      raeburn   139:         }
1.20.2.1! raeburn   140:         return OK;
1.12      raeburn   141:     } else {
1.20.2.1! raeburn   142:         ($status,$msg)=&Apache::lonipcheck::ip_access_check();
1.12      raeburn   143:         if ($status ne 'OPEN') {
                    144:             if ($target eq 'tex') {
1.15      raeburn   145:                 $r->print(&mt('Not open to be viewed').'\end{document}');
1.12      raeburn   146:             } else {
                    147:                 $r->print($msg);
                    148:             }
                    149:             return OK;
                    150:         }
                    151:     }
                    152:     my $launchok = 1;
                    153:     if ($target eq 'tex') {
1.15      raeburn   154:         $r->print('\end{document}');
1.12      raeburn   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'},
1.14      raeburn   166:                                    $toolhash{'sigmethod'},$submittext,\%lti));
1.12      raeburn   167:         } else {
                    168:             $r->print('<div>'.&mt('External Tool Unavailable').'</div>');
                    169:         }
1.1       raeburn   170:     }
                    171:     return OK;
                    172: }
                    173: 
                    174: sub lti_params {
1.6       raeburn   175:     my ($r,$cnum,$cdom,$idx,$submittext,$toolsref) = @_;
1.1       raeburn   176:     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
1.20.2.1! raeburn   177:         $crslabel,$crstitle,%fields,%rolesmap,%display,%custom,@userlangs,$incdom);
1.1       raeburn   178:     if (ref($toolsref) eq 'HASH') {
                    179:         $version = $toolsref->{'version'};
                    180:         $toolname = $toolsref->{'title'};
                    181:         $msgtype = $toolsref->{'messagetype'};
1.16      raeburn   182:         $incdom = $toolsref->{'incdom'};
1.1       raeburn   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:         }
1.4       raeburn   195:         $crslabel = $toolsref->{'crslabel'};
                    196:         $crstitle = $toolsref->{'crstitle'};
1.1       raeburn   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:     }
1.4       raeburn   207:     if ($crslabel eq '') {
1.20      raeburn   208:         $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
1.4       raeburn   209:     }
                    210:     if ($crstitle eq '') {
1.20      raeburn   211:         $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'};
1.4       raeburn   212:     }
1.1       raeburn   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);
1.3       raeburn   218:     my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
                    219:     my $ltirole = $rolesmap{$roleprefix};
1.1       raeburn   220:     unless (grep(/^\Q$ltirole\E$/,@possroles)) {
                    221:         $ltirole = 'Learner';
                    222:     }
1.17      raeburn   223:     my @possdigest;
1.1       raeburn   224:     my $digest_user = &Encode::decode_utf8($uname.':'.$udom);
                    225:     $digest_user = &Digest::SHA::sha1_hex($digest_user);
1.17      raeburn   226:     push(@possdigest,$digest_user);
1.1       raeburn   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 {
1.2       raeburn   231:         my %langhash = &Apache::loncommon::getlangs($uname,$udom);
1.1       raeburn   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);
1.17      raeburn   249:         push(@possdigest,$digest_symb);
1.1       raeburn   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:     }
1.4       raeburn   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: 
1.1       raeburn   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,
1.4       raeburn   269:         tool_consumer_instance_description     => $domdesc,
1.1       raeburn   270:         tool_consumer_info_product_family_code => 'loncapa',
1.20      raeburn   271:         tool_consumer_instance_name            => $int_dom,
1.4       raeburn   272:         tool_consumer_instance_url             => $portal_url,
1.1       raeburn   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,
1.4       raeburn   278:         context_label                          => $crslabel,
                    279:         context_title                          => $crstitle,
1.1       raeburn   280:         launch_presentation_locale             => $locale,
                    281:     );
1.3       raeburn   282:     my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
                    283:     my $crshostname = &Apache::lonnet::hostname($crshome);
1.1       raeburn   284:     if ($crshostname) {
1.3       raeburn   285:         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
1.1       raeburn   286:         unless ($crsprotocol eq 'https') {
                    287:             $crsprotocol = 'http';
1.6       raeburn   288:         }
1.1       raeburn   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;
1.3       raeburn   311:         foreach my $type ('permanentemail','critnotification','notification') {
                    312:             if ($emails{$type} =~ /\@/) {
                    313:                 $contact_email = $emails{$type};
1.1       raeburn   314:                 last;
                    315:             }
                    316:         }
1.2       raeburn   317:         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
1.1       raeburn   318:     }
1.4       raeburn   319:     if ($fields{'user'}) {
1.16      raeburn   320:         if ($incdom) {
                    321:             $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
                    322:         } else {
                    323:             $ltiparams{'lis_person_sourcedid'} = $uname;
                    324:         }
1.4       raeburn   325:     }
1.1       raeburn   326:     if (keys(%custom)) {
                    327:         foreach my $key (keys(%custom)) {
1.5       raeburn   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;
1.1       raeburn   336:         }
                    337:     }
                    338:     foreach my $key (keys(%ltiparams)) {
                    339:         $ltiparams{$key} = &Encode::decode_utf8($ltiparams{$key});
                    340:     }
1.3       raeburn   341:     $ltiparams{'basiclti_submit'} = $submittext;
1.1       raeburn   342:     return %ltiparams;
                    343: }
                    344: 
                    345: sub launch_html {
1.14      raeburn   346:     my ($url,$key,$secret,$sigmethod,$submittext,$paramsref) = @_;
                    347:     my $hashref = &LONCAPA::ltiutils::sign_params($url,$key,$secret,$sigmethod,$paramsref);
1.5       raeburn   348:     my $action = &HTML::Entities::encode($url,'<>&"');
1.1       raeburn   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">
1.5       raeburn   354: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
1.1       raeburn   355: END
                    356:     if (ref($hashref) eq 'HASH') {
                    357:         foreach my $item (keys(%{$hashref})) {
1.3       raeburn   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";
1.1       raeburn   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');
1.3       raeburn   371:     nei.setAttribute('name','basiclti_submit');
1.1       raeburn   372:     nei.setAttribute('value','$submittext');
1.3       raeburn   373:     document.getElementById("LCltiLaunchFormId").appendChild(nei);
1.1       raeburn   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>