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

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