File:  [LON-CAPA] / loncom / interface / lonexttool.pm
Revision 1.29: download - view: text, annotated - select for diffs
Sat Jun 14 02:50:25 2025 UTC (2 weeks, 5 days ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Destination URL which differs from launch URL can be specified for an LTI
  Provider which does not support specifying this in another way (e.g., via
  custom parameter in launch payload, or as string appended to launch URL).

    1: # The LearningOnline Network with CAPA
    2: # Launch External Tool Provider (LTI)
    3: #
    4: # $Id: lonexttool.pm,v 1.29 2025/06/14 02:50:25 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 Apache::lonhomework;
   55: use Apache::structuretags;
   56: use LONCAPA::ltiutils;
   57: 
   58: sub handler {
   59:     my $r=shift;
   60:     &Apache::loncommon::content_type($r,'text/html');
   61:     $r->send_http_header;
   62: 
   63:     return OK if $r->header_only;
   64: 
   65:     my $target=$env{'form.grade_target'};
   66: # ------------------------------------------------------------ Print the screen
   67:     if ($target eq 'tex') {
   68:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   69:     } else {
   70:         $target = 'web';
   71:     }
   72: 
   73: # Is this even in a course?
   74:     unless ($env{'request.course.id'}) {
   75:         if ($target ne 'tex') {
   76:             &Apache::loncommon::simple_error_page($r,'','Not in a course',
   77:                                                   {'only_body' => 1});
   78:         } else {
   79:             $r->print('\textbf{Not in a course}\end{document}');
   80:         }
   81:         return OK;
   82:     }
   83: 
   84:     my ($marker,$exttool) = (split(m{/},$r->uri))[4,5];
   85:     $marker=~s/\D//g;
   86: 
   87:     if (!$marker) {
   88:         if ($target ne 'tex') {
   89:             $r->print(&mt('Invalid Call'));
   90:         } else {
   91:             $r->print('\textbf{'&mt('Invalid Call').'}\end{document}');
   92:         }
   93:         return OK;
   94:     }
   95: 
   96:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   97:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   98:     my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
   99:     my ($idx,$crstool,$is_tool,%toolhash,%toolsettings,$desturl,$delay);
  100: 
  101:     if ($r->uri eq "/adm/$cdom/$cnum/$marker/$exttool") {
  102:         %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$cdom,$cnum);
  103:         if ($toolsettings{'id'}) {
  104:             my %ltitools;
  105:             if ($toolsettings{'id'} =~ /^c(\d+)$/) {
  106:                 $idx = $1;
  107:                 $crstool = 1;
  108:                 %ltitools = &Apache::lonnet::get_course_lti($cnum,$cdom,'consumer');
  109:             } else {
  110:                 $idx = $toolsettings{'id'};
  111:                 %ltitools = &Apache::lonnet::get_domain_lti($cdom,'consumer');
  112:             }
  113:             if (ref($ltitools{$idx}) eq 'HASH') {
  114:                 %toolhash = %{$ltitools{$idx}};
  115:                 $toolhash{'display'} = {
  116:                                            target => $toolsettings{'target'},
  117:                                            width  => $toolsettings{'width'},
  118:                                            height => $toolsettings{'height'},
  119:                                        };
  120:                 foreach my $item (qw(crslabel crstitle crsappend gradable)) {
  121:                     $toolhash{$item} = $toolsettings{$item};
  122:                 }
  123:                 if ($toolhash{'returnurl'}) {
  124:                     my $gotreturnurl;
  125:                     if (ref($toolhash{'crsconf'}) eq 'HASH') {
  126:                         if ($toolhash{'crsconf'}{'returnurl'}) {
  127:                             foreach my $item (qw(returnurl backtourl)) {
  128:                                 $toolhash{$item} = $toolsettings{$item};
  129:                             }
  130:                             $gotreturnurl = 1;
  131:                         }
  132:                     }
  133:                     unless ($gotreturnurl) {
  134:                         $toolhash{'returnurl'} = 'default';
  135:                     }
  136:                 } else {
  137:                     $toolhash{'returnurl'} = 'none';
  138:                 }
  139:                 if ($toolhash{'desturl'}) {
  140:                     if ((ref($toolhash{'crsconf'}) eq 'HASH')) {
  141:                         if ($toolhash{'crsconf'}{'desturl'}) {
  142:                             if ((exists($toolsettings{'desturl'})) &&
  143:                                 ($toolsettings{'desturl'} =~ m{^(https?\://|/)})) {
  144:                                 $desturl = $toolsettings{'desturl'};
  145:                                 if ((exists($toolsettings{'delay'})) &&
  146:                                     ($toolsettings{'delay'} =~ /^\d+\.?\d*$/)) {
  147:                                     $delay = $toolsettings{'delay'};
  148:                                 }
  149:                             }
  150:                         } elsif ($toolhash{'defdest'} =~ m{^(https?\://|/)}) {
  151:                             $desturl = $toolhash{'defdest'};
  152:                             if ($toolhash{'defdelay'} =~ /^\d+\.?\d*$/) {
  153:                                 $delay = $toolhash{'defdelay'};
  154:                             }
  155:                         }
  156:                     }
  157:                 }
  158:                 $is_tool = 1;
  159:             }
  160:         }
  161:     }
  162:     unless ($is_tool) {
  163:         if ($target ne 'tex') {
  164:             $r->print('<div>'.&mt('Invalid Call').'</div>');
  165:         } else {
  166:             $r->print('\textbf{'.&mt(Invalid Call).'}\end{document}');
  167:         }
  168:         return OK;
  169:     }
  170: 
  171:     my ($symb,$status,$open,$close,$msg,$donebuttonresult,$donemsg);
  172:     if (($target eq 'tex') || ($toolhash{'gradable'})) {
  173:         ($symb) = &Apache::lonnet::whichuser();
  174:     }
  175:     if ($target eq 'tex') {
  176:         my $title = &Apache::lonnet::gettitle($symb);
  177:         $r->print(&mt('External Tool: [_1]','\textit{'.$title.'}').'\\\\');
  178:     }
  179:     if ($toolhash{'gradable'}) {
  180:         $Apache::lonhomework::browse = &Apache::lonnet::allowed('bre',$r->uri);
  181:         if ($env{'form.markaccess'}) {
  182:             my @interval=&Apache::lonnet::EXT('resource.0.interval',$symb);
  183:             my ($timelimit) = split(/_/,$interval[0]);
  184:             &Apache::lonnet::set_first_access($interval[1],$timelimit);
  185:         } elsif ($symb && $env{'form.LC_interval_done'} eq 'true') {
  186:             # Set the event timer to zero if the "done button" was clicked.  The button is
  187:             # part of the doneButton form created in lonmenu.pm
  188:             ($donebuttonresult,$donemsg) = &Apache::lonhomework::zero_timer($symb);
  189:             undef($env{'form.LC_interval_done'});
  190:             undef($env{'form.LC_interval_done_proctorpass'});
  191:         }
  192:         ($status,$msg) = &gradabletool_access_check($target);
  193:         undef($Apache::lonhomework::browse);
  194:         if ($status eq 'SHOW_ANSWER') {
  195:             $r->print(&display_score($target));
  196:             if ($target eq 'tex') {
  197:                 $r->print('\end{document}');
  198:             }
  199:             return OK;
  200:         } elsif ($status ne 'CAN_ANSWER') {
  201:             if ($target eq 'tex') {
  202:                 $r->print('\end{document}');
  203:             } else {
  204:                 $r->print($msg);
  205:             }
  206:             return OK;
  207:         }
  208:     } else {
  209:         my ($status,$open,$close,$msg)=&Apache::londatecheck::content_date_check();
  210:         if ($status ne 'OPEN') {
  211:             if ($target eq 'tex') {
  212:                 $r->print(&mt('Not open to be viewed').'\end{document}');
  213:             } else {
  214:                 $r->print($msg);
  215:             }
  216:             return OK;
  217:         } else {
  218:             ($status,$msg)=&Apache::lonipcheck::ip_access_check();
  219:             if ($status ne 'OPEN') {
  220:                 if ($target eq 'tex') {
  221:                     $r->print(&mt('Not open to be viewed').'\end{document}');
  222:                 } else {
  223:                     $r->print($msg);
  224:                 }
  225:                 return OK;
  226:             }
  227:         }
  228:     }
  229:     my $launchok = 1;
  230:     if ($target eq 'tex') {
  231:         $r->print('\end{document}');
  232:     } else {
  233:         my $now = time;
  234:         if ($toolhash{'passback'}) {
  235:             if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'grade',$now,
  236:                                                        \%toolsettings,\%toolhash) eq 'ok') {
  237:                 $toolhash{'gradesecret'} = $toolsettings{'gradesecret'};
  238:             } else {
  239:                 undef($launchok);
  240:             }
  241:         }
  242:         if ($toolhash{'roster'}) {
  243:             if (&LONCAPA::ltiutils::set_service_secret($cdom,$cnum,$marker,'roster',$now,
  244:                                                        \%toolsettings,\%toolhash) eq 'ok') {
  245:                 $toolhash{'rostersecret'} = $toolsettings{'rostersecret'};
  246:             }
  247:         }
  248:         my $submittext = &mt('Launch [_1]',$toolhash{'title'});
  249:         if (($toolhash{'url'} ne '') && ($launchok)) {
  250:             my %lti = &lti_params($r,$cnum,$cdom,$marker,$exttool,$idx,$submittext,\%toolhash);
  251:             my $url = $toolhash{'url'};
  252:             if ($toolhash{'crsappend'} ne '') {
  253:                 $url .= $toolhash{'crsappend'};
  254:             }
  255:             if ($desturl ne '') {
  256:                 my ($scheme,$provider,$path);
  257:                 if ($url =~ m{^https?\://}) {
  258:                     ($scheme,$provider,$path) = ($url =~ m{^(https?\://)([^/]+)(|/.+)$});
  259:                     if ($desturl =~ m{^/}) {
  260:                         if ($path eq $desturl) {
  261:                             undef($desturl);
  262:                         } else {
  263:                             $desturl = $scheme.$provider.$desturl;
  264:                         }
  265:                     } elsif ($desturl =~ m{^https?\://}) {
  266:                         if ($url eq $desturl) {
  267:                             undef($desturl);
  268:                         }
  269:                     }
  270:                 } elsif ($url eq $desturl) {
  271:                     undef($desturl);
  272:                 }
  273:             }
  274:             my %info = (
  275:                          method => $toolhash{'sigmethod'},
  276:                        );
  277:             $r->print(&launch_html($cdom,$cnum,$crstool,$url,$idx,
  278:                                    $toolhash{'cipher'},$submittext,
  279:                                    \%lti,\%info,$desturl,$delay));
  280:         } else {
  281:             $r->print('<div class="LC_warning">'.&mt('External Tool Unavailable').'</div>');
  282:         }
  283:     }
  284:     return OK;
  285: }
  286: 
  287: sub lti_params {
  288:     my ($r,$cnum,$cdom,$marker,$exttool,$idx,$submittext,$toolsref) = @_;
  289:     my ($version,$context_type,$msgtype,$toolname,$passback,$roster,$locale,
  290:         $crslabel,$crstitle,$gradesecret,$rostersecret,%fields,%rolesmap,
  291:         %display,%custom,@userlangs,$incdom,$returnurl,$backtourl);
  292:     if (ref($toolsref) eq 'HASH') {
  293:         $version = $toolsref->{'version'};
  294:         $toolname = $toolsref->{'title'};
  295:         $passback = $toolsref->{'passback'};
  296:         $gradesecret = $toolsref->{'gradesecret'};
  297:         $roster = $toolsref->{'roster'};
  298:         $rostersecret = $toolsref->{'rostersecret'};
  299:         $msgtype = $toolsref->{'messagetype'};
  300:         $incdom = $toolsref->{'incdom'};
  301:         if (ref($toolsref->{'fields'}) eq 'HASH') {
  302:             %fields = %{$toolsref->{'fields'}};
  303:         }
  304:         if (ref($toolsref->{'roles'}) eq 'HASH') {
  305:             %rolesmap = %{$toolsref->{'roles'}};
  306:         }
  307:         if (ref($toolsref->{'display'}) eq 'HASH') {
  308:             %display = %{$toolsref->{'display'}};
  309:         }
  310:         if (ref($toolsref->{'custom'}) eq 'HASH') {
  311:             %custom = %{$toolsref->{'custom'}};
  312:         }
  313:         $crslabel = $toolsref->{'crslabel'};
  314:         $crstitle = $toolsref->{'crstitle'};
  315:         $returnurl = $toolsref->{'returnurl'};
  316:         $backtourl = $toolsref->{'backtourl'};
  317:     }
  318:     if ($version eq '') {
  319:         $version = 'LTI-1p0';
  320:     }
  321:     if ($context_type eq '') {
  322:         $context_type = 'CourseSection';
  323:     }
  324:     if ($msgtype eq '') {
  325:         $msgtype = 'basic-lti-launch-request';
  326:     }
  327:     if ($crslabel eq '') {
  328:         $crslabel = $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
  329:     }
  330:     if ($crstitle eq '') {
  331:         $crstitle = $env{'course.'.$env{'request.course.id'}.'.description'};
  332:     }
  333:     my $lonhost = $r->dir_config('lonHostID');
  334:     my $loncaparev = $r->dir_config('lonVersion');
  335:     my $uname = $env{'user.name'};
  336:     my $udom = $env{'user.domain'};
  337:     my @possroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
  338:     my ($roleprefix) = ($env{'request.role'} =~ /^(\w+)\./);
  339:     my $ltirole = $rolesmap{$roleprefix};
  340:     unless (grep(/^\Q$ltirole\E$/,@possroles)) {
  341:         $ltirole = 'Learner';
  342:     }
  343:     my @possdigest;
  344:     my $digest_user = &Encode::decode('UTF-8',$uname.':'.$udom);
  345:     $digest_user = &Digest::SHA::sha1_hex($digest_user);
  346:     push(@possdigest,$digest_user);
  347:     if ($env{'course.'.$env{'request.course.id'}.'.languages'} ne '') {
  348:         @userlangs=(@userlangs,split(/\s*(\,|\;|\:)\s*/,
  349:                     $env{'course.'.$env{'request.course.id'}.'.languages'}));
  350:     } else {
  351:         my %langhash = &Apache::loncommon::getlangs($uname,$udom);
  352:         if ($langhash{'languages'} ne '') {
  353:             @userlangs = split(/\s*(\,|\;|\:)\s*/,$langhash{'languages'});
  354:         } else {
  355:             my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
  356:             if ($domdefs{'lang_def'} ne '') {
  357:                 @userlangs = ($domdefs{'lang_def'});
  358:             }
  359:         }
  360:     }
  361:     if (scalar(@userlangs) == 1) {
  362:         $locale = $userlangs[0];
  363:     }
  364:     my ($title,$digest_symb,$resource_link_id);
  365:     my $ignorecachednull = 1;
  366:     my ($symb) = &Apache::lonnet::whichuser('',$ignorecachednull);
  367:     my $uri = "/adm/$cdom/$cnum/$marker/$exttool";
  368:     if ($symb) {
  369:         $digest_symb = &Encode::decode('UTF-8',$symb);
  370:         $digest_symb = &Digest::SHA::sha1_hex($digest_symb);
  371:         $resource_link_id = $digest_symb;
  372:         push(@possdigest,$digest_symb);
  373:         my $navmap = Apache::lonnavmaps::navmap->new();
  374:         if (ref($navmap)) {
  375:             my $res = $navmap->getBySymb($symb);
  376:             if (ref($res)) {
  377:                 $title = $res->compTitle();
  378:             }
  379:         }
  380:     } elsif ($env{'httpref.'.$uri} eq '/adm/coursedoc') {
  381:         my $digest_suppurl = &Encode::decode('UTF-8',$uri);
  382:         $digest_suppurl = &Digest::SHA::sha1_hex($digest_suppurl);
  383:         $resource_link_id = $digest_suppurl;
  384:         $title = &mt('Supplemental Content').': '.$marker.'_'.$exttool;
  385:     } else {
  386:         my $digest_url = &Encode::decode('UTF-8',$uri);
  387:         $digest_url = &Digest::SHA::sha1_hex($digest_url);
  388:         $resource_link_id = $digest_url;
  389:         $title = &mt('External Tool').': '.$marker.'_'.$exttool;
  390:     }
  391:     my $domdesc = &Apache::lonnet::domain($cdom);
  392:     my $primary_id = &Apache::lonnet::domain($cdom,'primary');
  393:     my $int_dom = &Apache::lonnet::internet_dom($primary_id);
  394:     my $portal_url = &Apache::lonnet::course_portal_url($cnum,$cdom,$r);
  395: 
  396:     my %ltiparams = (
  397:         lti_version                            => $version,
  398:         lti_message_type                       => $msgtype,
  399:         resource_link_title                    => $title,
  400:         resource_link_id                       => $resource_link_id,
  401:         tool_consumer_instance_guid            => $lonhost,
  402:         tool_consumer_instance_description     => $domdesc,
  403:         tool_consumer_info_product_family_code => 'loncapa',
  404:         tool_consumer_instance_name            => $int_dom,
  405:         tool_consumer_instance_url             => $portal_url,
  406:         tool_consumer_info_version             => $loncaparev,
  407:         user_id                                => $digest_user,
  408:         roles                                  => $ltirole,
  409:         context_id                             => $env{'request.course.id'},
  410:         context_type                           => $context_type,
  411:         context_label                          => $crslabel,
  412:         context_title                          => $crstitle,
  413:         launch_presentation_locale             => $locale,
  414:     );
  415:     my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
  416:     my $crshostname = &Apache::lonnet::hostname($crshome);
  417:     if ($crshostname) {
  418:         my $crsprotocol = $Apache::lonnet::protocol{$crshome};
  419:         unless ($crsprotocol eq 'https') {
  420:             $crsprotocol = 'http';
  421:         }
  422:         if (($passback) || ($roster)) {
  423:             my (%currdigest,%digesthash);
  424:             if (@possdigest) {
  425:                 %currdigest = &Apache::lonnet::get('exttools',\@possdigest,
  426:                                                    $cdom,$cnum);
  427:             }
  428:             if ($passback) {
  429:                 $ltiparams{'lis_outcome_service_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/passback';
  430:                 $ltiparams{'ext_ims_lis_basic_outcome_url'} = $ltiparams{'lis_outcome_service_url'};
  431:                 if ($gradesecret) {
  432:                     my $uniqid = $digest_symb.':::'.$digest_user.':::'.$env{'request.course.id'};
  433:                     $ltiparams{'lis_result_sourcedid'} = &LONCAPA::ltiutils::get_service_id($gradesecret,$uniqid);
  434:                 }
  435:             }
  436:             if ($roster) {
  437:                 if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
  438:                     $ltiparams{'ext_ims_lis_memberships_url'} = $crsprotocol.'://'.$crshostname.'/adm/service/roster';
  439:                     if ($rostersecret) {
  440:                         my $uniqid = $digest_symb.':::'.$env{'request.course.id'};
  441:                         $ltiparams{'ext_ims_lis_memberships_id'} = &LONCAPA::ltiutils::get_service_id($rostersecret,$uniqid);
  442:                     }
  443:                 }
  444:             }
  445:             if (($digest_symb) && ($gradesecret || $rostersecret)) {
  446:                 unless ((exists($currdigest{$digest_symb})) && ($currdigest{$digest_symb} eq $symb)) {
  447:                     $digesthash{$digest_symb} = $symb;
  448:                 }
  449:             }
  450:             if (($passback) && ($gradesecret)) {
  451:                 unless ((exists($currdigest{$digest_user})) && ($currdigest{$digest_user} eq $uname.':'.$udom)) {
  452:                     $digesthash{$digest_user} = $uname.':'.$udom;
  453:                 }
  454:             }
  455:             if (keys(%digesthash)) {
  456:                 &Apache::lonnet::put('exttools',\%digesthash,$cdom,$cnum);
  457:             }
  458:         }
  459:     }
  460:     if ($display{'target'}) {
  461:         $ltiparams{'launch_presentation_document_target'} = $display{'target'};
  462:     }
  463:     if ($display{'width'}) {
  464:         $ltiparams{'launch_presentation_width'} = $display{'width'};
  465:     }
  466:     if ($display{'height'}) {
  467:         $ltiparams{'launch_presentation_height'} = $display{'height'};
  468:     }
  469:     if ($fields{'firstname'}) {
  470:         $ltiparams{'lis_person_name_given'} = $env{'environment.firstname'};
  471:     }
  472:     if ($fields{'lastname'}) {
  473:         $ltiparams{'lis_person_name_family'} = $env{'environment.lastname'};
  474:     }
  475:     if ($fields{'fullname'}) {
  476:         $ltiparams{'lis_person_name_full'} = &Apache::loncommon::plainname($uname,$udom);
  477:     }
  478:     if ($fields{'email'}) {
  479:         my %emails = &Apache::loncommon::getemails($uname,$udom);
  480:         my $contact_email;
  481:         foreach my $type ('permanentemail','critnotification','notification') {
  482:             if ($emails{$type} =~ /\@/) {
  483:                 $contact_email = $emails{$type};
  484:                 last;
  485:             }
  486:         }
  487:         $ltiparams{'lis_person_contact_email_primary'} = $contact_email;
  488:     }
  489:     if ($fields{'user'}) {
  490:         if ($incdom) {
  491:             $ltiparams{'lis_person_sourcedid'} = $uname.':'.$udom;
  492:         } else {
  493:             $ltiparams{'lis_person_sourcedid'} = $uname;
  494:         }
  495:     }
  496:     if (keys(%custom)) {
  497:         foreach my $key (keys(%custom)) {
  498:             my $value = $custom{$key};
  499:             $value =~ s/^\s+|\s+\$//g;
  500:             if ($value =~ /^\QLONCAPA::env{\E([^\}]+)\}$/) {
  501:                 if (exists($env{$1})) {
  502:                     $value = $env{$1};
  503:                 }
  504:             }
  505:             $ltiparams{'custom_'.$key} = $value;
  506:         }
  507:     }
  508:     foreach my $key (keys(%ltiparams)) {
  509:         $ltiparams{$key} = &Encode::decode('UTF-8',$ltiparams{$key});
  510:     }
  511:     $ltiparams{'basiclti_submit'} = $submittext;
  512:     if ($returnurl eq 'default') {
  513:         my $hostname = &Apache::lonnet::hostname($r->dir_config('lonHostID'));
  514:         my $protocol = 'http';
  515:         my $port = $r->get_server_port();
  516:         if ($port eq '443') {
  517:             $protocol = 'https';
  518:         }
  519:         if (&Apache::lonnet::get_proxy_alias()) {
  520:             my $hdrhost = $r->headers_in->get('Host');
  521:             if ($hdrhost ne '') {
  522:                 $hostname = $r->headers_in->get('Host');
  523:             }
  524:         }
  525:         my $location = $protocol.'://'.$hostname;
  526:         if ($display{'target'} eq 'iframe') {
  527:             my $return_url;
  528:             if ($symb) {
  529:                 my ($map,$id,$url)=&Apache::lonnet::decode_symb($symb);
  530:                 my $mapurl = &Apache::lonnet::clutter($map);
  531:                 my $mapsymb = &Apache::lonnet::symbread($map);
  532:                 if ((&Apache::lonnet::EXT('resource.0.encrypturl',$mapsymb) =~ /^yes$/i) &&
  533:                     (!$env{'request.role.adv'})) {
  534:                     $return_url = &Apache::lonenc::encrypted($mapurl);
  535:                 } else {
  536:                     $return_url = $mapurl;
  537:                 }
  538:                 $return_url .= '?navmap=1';
  539:             } elsif ($env{'httpref.'.$uri} eq '/adm/coursedoc') {
  540:                 $return_url = '/adm/supplemental';
  541:             } else {
  542:                 $return_url = '/adm/navmaps';
  543:             }
  544:             $ltiparams{'launch_presentation_return_url'} = $location.$return_url;
  545:         } else {
  546:             $uri = '/adm/wrapper'.$uri;
  547:             if ($env{'request.enc'}) {
  548:                 $uri = &Apache::lonenc::encrypted($uri);
  549:             }
  550:             $ltiparams{'launch_presentation_return_url'} = $location.$uri;
  551:         }
  552:     } elsif ($returnurl eq 'custom') {
  553:         if ($backtourl ne '') {
  554:             $ltiparams{'launch_presentation_return_url'} = $backtourl;
  555:         }
  556:     }
  557:     return %ltiparams;
  558: }
  559: 
  560: sub launch_html {
  561:     my ($cdom,$cnum,$crstool,$url,$idx,$keynum,$submittext,$paramsref,
  562:         $inforef,$desturl,$delay) = @_;
  563:     my ($status,$hashref) =
  564:         &Apache::lonnet::sign_lti($cdom,$cnum,$crstool,'tools','launch',$url,$idx,$keynum,
  565:                                   $paramsref,$inforef);
  566:     unless ($status eq 'ok') {
  567:         return '<div class="LC_warning">'.&mt('External Tool Unavailable').'</div>';
  568:     }
  569:     my ($action,$js_html,$header,$title,$bodytag,$form,$divsty,$delay_in_ms);
  570:     if ($delay ne '') {
  571:         $delay_in_ms = int(1000 * $delay);
  572:     }
  573:     $action = &HTML::Entities::encode($url,'<>&"');
  574:     $title = &mt('Launcher');
  575:     $header = <<"END";
  576: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  577: <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  578: END
  579:     if ($desturl) {
  580:         $divsty = 'none';
  581:         if ($delay_in_ms eq '') {
  582:             $delay_in_ms = 1000;
  583:         }
  584:         $bodytag = <<"END";
  585: <body onload="setTimeout(document.LCltiLaunchForm.submit(),$delay_in_ms);">
  586: END
  587:     } else {
  588:         $divsty = 'block';
  589:         $bodytag = <<"END";
  590: <body>
  591: END
  592:     }
  593:     $form = <<"END";
  594: <div id="LCltiLaunch" style="display:$divsty;">
  595: <form name="LCltiLaunchForm" id="LCltiLaunchFormId" action="$action" method="post" encType="application/x-www-form-urlencoded">
  596: END
  597:     if (ref($hashref) eq 'HASH') {
  598:         foreach my $item (keys(%{$hashref})) {
  599:             my $type = 'hidden';
  600:             if ($item eq 'basiclti_submit') {
  601:                 unless ($desturl) {
  602:                     $type = 'submit';
  603:                 }
  604:             }
  605:             $form .= '<input type="'.$type.'" name="'.$item.'" value="'.$hashref->{$item}.'" id="id_'.$item.'" />'."\n";
  606:         }
  607:     }
  608:     $form .= "</form></div>\n";
  609:     my $footer = <<"END";
  610: </body>
  611: </html>
  612: END
  613:     if ($desturl) {
  614:         $js_html = $header.$bodytag.$form.$footer;
  615:         &js_escape(\$js_html);
  616: 	&js_escape(\$desturl);
  617:         return <<"END";
  618: $header
  619: <head>
  620: <title>$title</title>
  621: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  622: <meta name="google" content="notranslate" />
  623: </head>
  624: <body onload="javascript:addcontent();">
  625: <iframe id="LC_launcher_iframe" style="position: absolute; width:0; height:0; border:0;">
  626: </iframe>
  627: <script type="text/javascript">
  628: // <![CDATA[
  629: function addcontent() {
  630:     if (document.getElementById('LC_launcher_iframe')) {
  631:         document.getElementById('LC_launcher_iframe').src = "data:text/html;charset=utf-8," + escape("$js_html");
  632:     }
  633:     setTimeout(() => {
  634:         document.location.href="$desturl";
  635:     }, $delay_in_ms);
  636: }
  637: // ]]>
  638: </script>
  639: $footer
  640: END
  641:     } else {
  642:         return <<"END";
  643: $header
  644: $bodytag
  645: $form
  646: <script type="text/javascript">
  647: // <![CDATA[
  648:     document.getElementById("LCltiLaunch").style.display = "none";
  649:     nei = document.createElement('input');
  650:     nei.setAttribute('type','hidden');
  651:     nei.setAttribute('name','basiclti_submit');
  652:     nei.setAttribute('value','$submittext');
  653:     document.getElementById("LCltiLaunchFormId").appendChild(nei);
  654:     document.LCltiLaunchForm.submit();
  655: // ]]>
  656: </script>
  657: $footer
  658: END
  659:     }
  660: }
  661: 
  662: sub gradabletool_access_check {
  663:     my ($target) = @_;
  664:     my ($result,$resource_due);
  665:     my $status;
  666:     my ($symb,$courseid,$udom,$uname) = &Apache::lonnet::whichuser();
  667:     my @targets;
  668:     if ($target) {
  669:         @targets = ($target);
  670:     } elsif (defined($env{'form.submitted'}) && defined($env{'form.validate'})) {
  671:         @targets = ('grade','web');
  672:     } else {
  673:         @targets = ('web');
  674:     }
  675:     foreach my $target (@targets) {
  676:         &Apache::structuretags::initialize_storage($symb);
  677:         &Apache::lonhomework::set_show_problem_status(&Apache::lonnet::EXT('resource.0.problemstatus'));
  678:         my ($accessmsg,$slot_name,$slot,$ipused);
  679:         ($status,$accessmsg,$slot_name,$slot,$ipused) =
  680:             &Apache::lonhomework::check_slot_access('0','tool',$symb);
  681:         if (( $status eq 'CLOSED' ) ||
  682:             ( $status eq 'UNCHECKEDOUT') ||
  683:             ( $status eq 'NOT_YET_VIEWED') ||
  684:             ( $status eq 'BANNED') ||
  685:             ( $status eq 'UNAVAILABLE') ||
  686:             ( $status eq 'NOT_IN_A_SLOT') ||
  687:             ( $status eq 'NOTRESERVABLE') ||
  688:             ( $status eq 'RESERVABLE') ||
  689:             ( $status eq 'RESERVABLE_LATER') ||
  690:             ( $status eq 'INVALID_ACCESS') ||
  691:             ( $status eq 'NEED_DIFFERENT_IP') ||
  692:             ( $status eq 'WAITING_FOR_GRADE')) {
  693:             $result = &Apache::structuretags::access_status_msg('tool',$status,$symb,
  694:                                                                 $target,$ipused,$accessmsg);
  695:         } elsif ($status eq 'NEEDS_CHECKIN') {
  696:             $result = &Apache::structuretags::checkin_prompt($target,$slot_name,$slot,'tool');
  697:         } elsif ($target eq 'web') {
  698:             if ($status eq 'CAN_ANSWER') {
  699:                 $resource_due = &Apache::lonhomework::due_date(0, $env{'request.symb'});
  700:                 if ($slot_name ne '') {
  701:                     $resource_due = &Apache::structuretags::selfcheckin_resource($resource_due,
  702:                                                                                  $slot_name,$slot,
  703:                                                                                  $env{'request.symb'});
  704:                 }
  705:             }
  706:         }
  707:         if (keys(%Apache::lonhomework::results)) {
  708:             &Apache::structuretags::finalize_storage();
  709:         }
  710:     }
  711:     return ($status,$result,$resource_due);
  712: }
  713: 
  714: sub display_score {
  715:     my ($target) = @_;
  716:     my $weight = &Apache::lonnet::EXT('resource.0.weight');
  717:     if ((!defined($weight)) || ($weight eq '')) { $weight=1; }
  718:     my $awarded = $Apache::lonhomework::history{'resource.0.awarded'};
  719:     if (!defined($awarded)) { $awarded=0; }
  720:     my $display='';
  721:     if ($target eq 'tex') {
  722:         $display = '\\\\';
  723:     }
  724:     if (!defined($awarded)) {
  725:         $display .= &mt('[_1] possible points.',$weight);
  726:     } else {
  727:         my $points = $awarded*$weight;
  728:         my $result = sprintf('%.2f',$points);
  729:         $display .= &mt('You have [_1] out of [quant,_2,possible point]',
  730:                        $result,$weight);
  731:     }
  732:     my $comment = $Apache::lonhomework::history{'resource.0.comment'};
  733:     if (!defined($comment) || $comment!~/\w/) {
  734:         $comment='';
  735:     } else {
  736:         if ($target eq 'tex') {
  737:             $comment = '\\\\'.$comment;
  738:         } else {
  739:             $comment='<br /><table><tr><td bgcolor="#FFFFDD">'.$comment.'</td></tr></table>';
  740:         }
  741:     }
  742:     my $gradeinfo = $Apache::lonhomework::history{'resource.0.gradeinfo'};
  743:     if (!defined($gradeinfo) || $gradeinfo!~/\w/) {
  744:         $gradeinfo='';
  745:     } else {
  746:         if ($target eq 'tex') {
  747:             $gradeinfo = '\\\\'.$gradeinfo;
  748:         } else {
  749:             $gradeinfo='<br /><table><tr><td bgcolor="#DDDDFF"><font size="+2">'.$gradeinfo.'</font></td></tr></table>';
  750:         }
  751:     }
  752:     return $display.$comment.$gradeinfo;
  753: }
  754: 
  755: 1;

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