File:  [LON-CAPA] / loncom / interface / lontiny.pm
Revision 1.11: download - view: text, annotated - select for diffs
Sun Jun 26 04:03:48 2022 UTC (2 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  - Set request.linkprotuser in %env for students accessing deep-linked items
    with LTI link protection which includes username from other LMS.
  - Set "only_body" for intermediate pages shown while session is being set up
    for LTI link-protected access.
  - Provide appropriate feedback when a deep link is followed but the user
    only has a future and/or expired role in the target course.

    1: # The LearningOnline Network with CAPA
    2: # Extract domain, courseID, and symb from a shortened URL,
    3: # and switch role to a role in designated course.
    4: #
    5: # $Id: lontiny.pm,v 1.11 2022/06/26 04:03:48 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: package Apache::lontiny;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http);
   34: use Apache::lonnet;
   35: use Apache::loncommon;
   36: use Apache::lonhtmlcommon;
   37: use Apache::lonroles;
   38: use Apache::lonuserstate;
   39: use Apache::lonnavmaps;
   40: use Apache::lonlocal;
   41: use LONCAPA qw(:DEFAULT :match);
   42: 
   43: sub handler {
   44:     my $r = shift;
   45:     my %user;
   46:     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
   47:     if ($handle ne '') {
   48:         my $lonidsdir=$r->dir_config('lonIDsDir');
   49:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   50:         if ($r->uri =~ m{^/tiny/($match_domain)/(\w+)$}) {
   51:             my ($cdom,$key) = ($1,$2);
   52:             if (&Apache::lonnet::domain($cdom) ne '') {
   53:                 my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
   54:                 my $tinyurl;
   55:                 my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
   56:                 if (defined($cached)) {
   57:                     $tinyurl = $result;
   58:                 } else {
   59:                     my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
   60:                     if ($currtiny{$key} ne '') {
   61:                         $tinyurl = $currtiny{$key};
   62:                         &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
   63:                     }
   64:                 }
   65:                 if ($tinyurl) {
   66:                     my ($cnum,$symb) = split(/\&/,$tinyurl);
   67:                     if ($cnum =~ /^$match_courseid$/) {
   68:                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
   69:                         if ($chome ne 'no_host') {
   70:                             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['ttoken']);
   71:                             my ($linkprot,$linkprotuser,$ltoken);
   72:                             if ($env{'form.ttoken'}) {
   73:                                 my %link_info = &Apache::lonnet::tmpget($env{'form.ttoken'});
   74:                                 if ($link_info{'origurl'} eq $r->uri) {
   75:                                     if ($link_info{'ltoken'}) {
   76:                                         $ltoken = $link_info{'ltoken'};
   77:                                         my %ltoken_info = &Apache::lonnet::tmpget($link_info{'ltoken'});
   78:                                         $linkprot = $ltoken_info{'linkprot'};
   79:                                         $linkprotuser = $ltoken_info{'linkprotuser'};
   80:                                     } elsif ($link_info{'linkprot'}) {
   81:                                         $linkprot = $link_info{'linkprot'};
   82:                                         if ($link_info{'linkprotuser'}) {
   83:                                             $linkprotuser = $link_info{'linkprotuser'};
   84:                                         }
   85:                                     }
   86:                                 }
   87:                             }
   88:                             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
   89:                                 # Check for ttoken
   90:                                 my $newlauncher = &launch_check($r->uri,$symb);
   91:                                 my ($map,$resid,$url) = &Apache::lonnet::decode_symb($symb);
   92:                                 if (&Apache::lonnet::is_on_map($url)) {
   93:                                     my $realuri;
   94:                                     if ((&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) &&
   95:                                         (!$env{'request.role.adv'})) {
   96:                                         $env{'user.error.msg'}=$r->uri.':bre:1:1:Access to resource denied';
   97:                                         return HTTP_NOT_ACCEPTABLE;
   98:                                     }
   99:                                     if ((&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) &&
  100:                                         (!$env{'request.role.adv'})) {
  101:                                         $realuri = &Apache::lonenc::encrypted(&Apache::lonnet::clutter($url));
  102:                                         if (($url =~ /\.sequence$/) &&
  103:                                             ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
  104:                                             $realuri .= '?navmap=1';
  105:                                         } else {
  106:                                             $realuri .= '?symb='.&Apache::lonenc::encrypted($symb);
  107:                                         }
  108:                                     } else {
  109:                                         $realuri = &Apache::lonnet::clutter($url);
  110:                                         if (($url =~ /\.sequence$/) &&
  111:                                             ($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement')) {
  112:                                             $realuri .= '?navmap=1';
  113:                                         } else {
  114:                                             $realuri .= '?symb='.$symb;
  115:                                         }
  116:                                     }
  117:                                     my $update;
  118:                                     # Check if course needs to be re-initialized
  119:                                     if ($newlauncher) {
  120:                                         $update = 1;
  121:                                     } else {
  122:                                         my $loncaparev = $r->dir_config('lonVersion');
  123:                                         my ($result,@reinit) = &Apache::loncommon::needs_coursereinit($loncaparev);
  124:                                         if ($result eq 'update') {
  125:                                             $update = 1;
  126:                                         } elsif (!-e $env{'request.course.fn'}.'.db') {
  127:                                             $update = 1;
  128:                                         } elsif (!$env{'request.role.adv'}) {
  129:                                             my $navmap = Apache::lonnavmaps::navmap->new();
  130:                                             if (ref($navmap)) {
  131:                                                 my $res = $navmap->getBySymb($symb);
  132:                                                 if (ref($res)) {
  133:                                                     my ($enc_in_bighash,$enc_in_parm);
  134:                                                     $enc_in_bighash = $res->encrypted();
  135:                                                     if (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i) {
  136:                                                         $enc_in_parm = 1;
  137:                                                     }
  138:                                                     if ($enc_in_bighash ne $enc_in_parm) {
  139:                                                         $update = 1;
  140:                                                     }
  141:                                                 }
  142:                                             }
  143:                                         }
  144:                                     }
  145:                                     if ($update) {
  146:                                         my ($furl,$ferr)=
  147:                                             &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  148:                                         if ($ferr) {
  149:                                             $env{'user.error.msg'}=$r->uri.':bre:0:0:Course not initialized';
  150:                                             $env{'user.reinit'} = 1;
  151:                                             return HTTP_NOT_ACCEPTABLE;
  152:                                         }
  153:                                     }
  154:                                     my $host = $r->headers_in->get('Host');
  155:                                     if (!$host) {
  156:                                         $r->internal_redirect($realuri);
  157:                                         return OK;
  158:                                     } else {
  159:                                         my $protocol = 'http';
  160:                                         if ($r->get_server_port == 443) {
  161:                                             $protocol = 'https';
  162:                                         }
  163:                                         my $location = $protocol.'://'.$host.$realuri;
  164:                                         $r->headers_out->set(Location => $location);
  165:                                         return REDIRECT;
  166:                                     }
  167:                                 }
  168:                             } else {
  169:                                 my %crsenv = &Apache::lonnet::coursedescription("$cdom/$cnum");
  170:                                 my @possroles = ('in','ta','ep','st','cr','ad');
  171:                                 if ($crsenv{'type'} eq 'Community') {
  172:                                     unshift(@possroles,'co');
  173:                                 } else {
  174:                                     unshift(@possroles,'cc');
  175:                                 }
  176:                                 my %roleshash =
  177:                                     &Apache::lonnet::get_my_roles($env{'user.uname'},$env{'user.domain'},
  178:                                                                   'userroles',['previous','active','future'],
  179:                                                                   \@possroles,[$cdom],1);
  180:                                 my (%possroles,$hassection,%active,%expired,%future);
  181:                                 if (keys(%roleshash)) {
  182:                                     my $now = time;
  183:                                     foreach my $entry (keys(%roleshash)) {
  184:                                         if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
  185:                                             my ($role,$sec) = ($1,$2);
  186:                                             $possroles{$role} = $sec;
  187:                                             if ($sec ne '') {
  188:                                                 $hassection = 1;
  189:                                             }
  190:                                             my ($tstart,$tend)=split(/\:/,$roleshash{$entry});
  191:                                             my $status = 'active';
  192:                                             if (($tend) && ($tend<=$now)) {
  193:                                                 $status = 'previous';
  194:                                             }
  195:                                             if (($tstart) && ($now<$tstart)) {
  196:                                                 $status = 'future';
  197:                                             }
  198:                                             if ($status eq 'active') {
  199:                                                 $active{$role} = $sec;
  200:                                             } elsif ($status eq 'previous') {
  201:                                                 $expired{$tend} = $role.':'.$sec;
  202:                                             } elsif ($status eq 'future') {
  203:                                                 $future{$tstart} = $role.':'.$sec;
  204:                                             }
  205:                                         }
  206:                                     }
  207:                                 }
  208:                                 my @allposs = keys(%active);
  209:                                 if ($env{'request.lti.login'}) {
  210:                                     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
  211:                                     if ($env{'request.lti.target'} eq '') {
  212:                                         if ($env{'form.ltitarget'} eq 'iframe') {
  213:                                             &Apache::lonnet::appenv({'request.lti.target' => 'iframe'});
  214:                                             delete($env{'form.ltitarget'});
  215:                                         }
  216:                                     }
  217:                                     if ($env{'form.selectrole'}) {
  218:                                         foreach my $role (@allposs) {
  219:                                             my $newrole = "$role./$cdom/$cnum";
  220:                                             if ($possroles{$allposs[0]} ne '') {
  221:                                                 $newrole .= "/$possroles{$role}";
  222:                                             }
  223:                                             if ($env{"form.$newrole"}) {
  224:                                                 my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  225:                                                                    '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
  226:                                                 if ($env{'form.ltitarget'} eq 'iframe') {
  227:                                                     $destination .= '&ltitarget=iframe';
  228:                                                 }
  229:                                                 &do_redirect($r,$destination);
  230:                                                 return OK;
  231:                                             }
  232:                                         }
  233:                                     }
  234:                                 }
  235:                                 if (@allposs == 0) {
  236:                                     &show_roles($r,\%crsenv,\%active,'','',\%future,\%expired,$linkprot,$linkprotuser,$ltoken);
  237:                                 } elsif (@allposs == 1) {
  238:                                     my $newrole = "$allposs[0]./$cdom/$cnum";
  239:                                     $newrole = "$allposs[0]./$cdom/$cnum";
  240:                                     if ($possroles{$allposs[0]} ne '') {
  241:                                         $newrole .= "/$possroles{$allposs[0]}";
  242:                                     }
  243:                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  244:                                                        '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
  245:                                     if ($env{'form.ttoken'}) {
  246:                                         $destination .= '&ttoken='.$env{'form.ttoken'};
  247:                                     }
  248:                                     &do_redirect($r,$destination,$linkprot);
  249:                                 } elsif (keys(%possroles) > 1) {
  250:                                     if (grep(/^(cc|co)$/,@allposs)) {
  251:                                         my $newrole;
  252:                                         if (exists($possroles{'cc'})) {
  253:                                             $newrole = 'cc';
  254:                                         } else {
  255:                                             $newrole = 'co';
  256:                                         }
  257:                                         $newrole .= "./$cdom/$cnum";
  258:                                         my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
  259:                                                            '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
  260:                                         if ($env{'form.ttoken'}) {
  261:                                             $destination .= '&ttoken='.$env{'form.ttoken'};
  262:                                         }
  263:                                         &do_redirect($r,$destination,$linkprot);
  264:                                     } else {
  265:                                         my $hascustom;
  266:                                         if (grep(/^cr\//,@allposs)) {
  267:                                             $hascustom = 1;
  268:                                         }
  269:                                         &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
  270:                                     }
  271:                                 }
  272:                                 return OK;
  273:                             }
  274:                         }
  275:                     }
  276:                 }
  277:             }
  278:         }
  279:         &generic_error($r);
  280:         return OK;
  281:     } else {
  282:         return FORBIDDEN;
  283:     }
  284: }
  285: 
  286: sub launch_check {
  287:     my ($linkuri,$symb) = @_;
  288:     my ($linkprotector,$linkproturi,$linkkey,$newlauncher);
  289:     if ($env{'form.ttoken'}) {
  290:         my %link_info = &Apache::lonnet::tmpget($env{'form.ttoken'});
  291:         &Apache::lonnet::tmpdel($env{'form.ttoken'});
  292:         delete($env{'form.ttoken'});
  293:         if ($link_info{'ltoken'}) {
  294:             unless (($link_info{'linkprot'}) || ($link_info{'linkkey'} ne '')) {
  295:                 my %ltoken_info = &Apache::lonnet::tmpget($link_info{'ltoken'});
  296:                 if ($ltoken_info{'linkprot'}) {
  297:                     $link_info{'linkprot'} = $ltoken_info{'linkprot'};
  298:                 } elsif ($ltoken_info{'linkkey'} ne '') {
  299:                     $link_info{'linkkey'} = $ltoken_info{'linkkey'};
  300:                 }
  301:             }
  302:             &Apache::lonnet::tmpdel($link_info{'ltoken'});
  303:         }
  304:         if ($link_info{'linkprot'}) {
  305:             ($linkprotector,$linkproturi) = split(/:/,$link_info{'linkprot'},2);
  306:             if ($env{'user.linkprotector'}) {
  307:                 my @protectors = split(/,/,$env{'user.linkprotector'});
  308:                 unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
  309:                     push(@protectors,$linkprotector);
  310:                     @protectors = sort { $a <=> $b } @protectors;
  311:                     &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
  312:                 }
  313:             } else {
  314:                 &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
  315:             }
  316:             if ($env{'user.linkproturi'}) {
  317:                 my @proturis = split(/,/,$env{'user.linkproturi'});
  318:                 unless(grep(/^\Q$linkproturi\E$/,@proturis)) {
  319:                     push(@proturis,$linkproturi);
  320:                     @proturis = sort(@proturis);
  321:                     &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
  322:                 }
  323:             } else {
  324:                 &Apache::lonnet::appenv({'user.linkproturi' => $linkproturi});
  325:             }
  326:         } elsif ($link_info{'linkkey'} ne '') {
  327:             $linkkey = $link_info{'linkkey'};
  328:             my $keyedlinkuri = $linkuri;
  329:             if ($env{'user.deeplinkkey'} ne '') {
  330:                 my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
  331:                 unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
  332:                     push(@linkkeys,$linkkey);
  333:                     &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
  334:                 }
  335:             } else {
  336:                 &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
  337:             }
  338:             if ($env{'user.keyedlinkuri'}) {
  339:                 my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
  340:                 unless (grep(/^\Q$keyedlinkuri\E$/,@keyeduris)) {
  341:                     push(@keyeduris,$keyedlinkuri);
  342:                     &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
  343:                 }
  344:             } else {
  345:                 &Apache::lonnet::appenv({'user.keyedlinkuri' => $keyedlinkuri});
  346:             }
  347:         }
  348:         if ($link_info{'checklaunch'}) {
  349:             $newlauncher = 1;
  350:         }
  351:     }
  352:     my $currdeeplinklogin = $env{'request.deeplink.login'};
  353:     my $deeplink;
  354:     if ($symb =~ /\.(page|sequence)$/) {
  355:         my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($symb))[2]);
  356:         my $navmap = Apache::lonnavmaps::navmap->new();
  357:         if (ref($navmap)) {
  358:             $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
  359:         }
  360:     } else {
  361:         $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);
  362:     }
  363:     if ($deeplink ne '') {
  364:         my $disallow;
  365:         my ($state,$others,$listed,$scope,$protect,$display,$target) = split(/,/,$deeplink);
  366:         if (($protect ne 'none') && ($protect ne '')) {
  367:             my ($acctype,$item) = split(/:/,$protect);
  368:             if ($acctype =~ /lti(c|d)$/) {
  369:                 my $ltitype = $1;
  370:                 if ($linkprotector) {
  371:                     unless ($linkprotector.':'.$linkproturi eq $item.$ltitype.':'.$linkuri) {
  372:                         $disallow = 1;
  373:                     }
  374:                 } else {
  375:                     $disallow = 1;
  376:                 }
  377:             } elsif ($acctype eq 'key') {
  378:                 if ($linkkey ne '') {
  379:                     unless ($linkkey eq $item) {
  380:                         $disallow = 1;
  381:                     }
  382:                 } else {
  383:                     $disallow = 1;
  384:                 }
  385:             }
  386:         }
  387:         if ($disallow) {
  388:             if ($currdeeplinklogin eq $linkuri) {
  389:                 &Apache::lonnet::delenv('request.deeplink.login');
  390:                 if ($env{'request.deeplink.target'} ne '') {
  391:                     &Apache::lonnet::delenv('request.deeplink.target');
  392:                 }
  393:             }
  394:         } else {
  395:             unless ($currdeeplinklogin eq $linkuri) {
  396:                 if (($linkprotector) || ($linkkey ne '')) {
  397:                     if ($linkprotector) {
  398:                         &Apache::lonnet::appenv({'request.linkprot' => $linkprotector.':'.$linkproturi});
  399:                     } elsif ($env{'request.linkprot'}) {
  400:                         &Apache::lonnet::delenv({'request.linkprot'});
  401:                     }
  402:                     if ($linkkey ne '') {
  403:                         &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
  404:                     } elsif ($env{'request.linkkey'} ne '') {
  405:                         &Apache::lonnet::delenv({'request.linkkey'});
  406:                     }
  407:                     $newlauncher = 1;
  408:                 }
  409:             }
  410:             &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
  411:             if ($target ne '') {
  412:                 &Apache::lonnet::appenv({'request.deeplink.target' => $target});
  413:             } elsif ($env{'request.deeplink.target'} ne '') {
  414:                 &Apache::lonnet::delenv('request.deeplink.target');
  415:             }
  416:         }
  417:     } else {
  418:         if ($linkprotector) {
  419:             &Apache::lonnet::appenv({'request.linkprot' => $linkprotector.':'.$linkproturi});
  420:         } elsif ($env{'request.linkprot'}) {
  421:             &Apache::lonnet::delenv({'request.linkprot'});
  422:         }
  423:         if ($linkkey ne '') {
  424:             &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
  425:         } else {
  426:             &Apache::lonnet::delenv({'request.linkkey'});
  427:         }
  428:         &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
  429:         if ($env{'request.deeplink.target'} ne '') {
  430:             &Apache::lonnet::delenv('request.deeplink.target');
  431:         }
  432:     }
  433:     return $newlauncher;
  434: }
  435: 
  436: sub do_redirect {
  437:     my ($r,$destination,$linkprot) = @_;
  438:     my $windowname = 'loncapaclient';
  439:     if ($env{'request.lti.login'}) {
  440:         $windowname .= 'lti';
  441:     }
  442:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
  443:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
  444:     if ($linkprot) {
  445:         $args = {'only_body' => 1,
  446:                  'redirect'  => [0,$destination],};
  447:     }
  448:     &Apache::loncommon::content_type($r,'text/html');
  449:     $r->send_http_header;
  450:     if ($linkprot) {
  451:         $r->print(&Apache::loncommon::start_page('Valid link','',$args).
  452:                   &Apache::loncommon::end_page());
  453:     } else {
  454:         $r->print(&Apache::loncommon::start_page('Valid link',$header,$args).
  455:                   &Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";').
  456:                   '<h1>'.&mt('Welcome').'</h1>'.
  457:                   '<p>'.&mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>').'</p><p>'.
  458:                   '<a href="'.$destination.'">'.&mt('Continue').'</a></p>'.
  459:                   &Apache::loncommon::end_page());
  460:     }
  461:     return;
  462: }
  463: 
  464: sub show_roles {
  465:     my ($r,$crsenv,$possroles,$hassection,$hascustom,$futureroles,$expiredroles,$linkprot,$linkprotuser,$ltoken) = @_;
  466:     my ($crsdesc,$crstype,$cdom,$cnum,$header,$title,$preamble,$datatable,$js,$args);
  467:     if (ref($crsenv) eq 'HASH') {
  468:         $crsdesc = $crsenv->{'description'};
  469:         $crstype = $crsenv->{'type'};
  470:         $cdom = $crsenv->{'domain'};
  471:         $cnum = $crsenv->{'num'};
  472:     }
  473:     if ($crstype eq '') {
  474:         $crstype = 'Course';
  475:     }
  476:     my $lc_crstype = lc($crstype);
  477:     if ($crsdesc ne '') {
  478:         $header = &mt("The page you requested belongs to the following $lc_crstype: [_1]",
  479:                       '<i>'.$crsdesc.'</i>');
  480:     }
  481:     if (ref($possroles) eq 'HASH') {
  482:         if (keys(%{$possroles}) > 0) {
  483:             $args = {'bread_crumbs' => [{'href' => '','text' => "Choose role in $lc_crstype"},],};
  484:             if ($linkprot) {
  485:                 $args = {'only_body' => 1};
  486:             }
  487:             $title = 'Choose a role'; #Do not localize.
  488:             if ($crstype eq 'Community') {
  489:                 $preamble = &mt('You have the following active roles in this community:');
  490:             } else { 
  491:                 $preamble = &mt('You have the following active roles in this course:');
  492:             }
  493:             $datatable = '<form name="" action="/adm/roles">'.
  494:                          '<input type="hidden" name="newrole" value="" />'."\n".
  495:                          '<input type="hidden" name="selectrole" value="1" />'."\n".
  496:                          '<input type="hidden" name="destinationurl" value="'.&HTML::Entities::encode($r->uri,'&<>"').'" />'."\n";
  497:             if ($env{'form.ttoken'}) {
  498:                 $datatable .= '<input type="hidden" name="ttoken" value="'.$env{'form.ttoken'}.'" />'."\n";
  499:             }
  500:             $datatable .= &Apache::loncommon::start_data_table().
  501:                           &Apache::loncommon::start_data_table_header_row().
  502:                           '<th></th><th>'.&mt('User role').'</th>';
  503:             if ($hassection) {
  504:                 $datatable .= '<th>'.&mt('Section').'</th>';
  505:             }
  506:             if ($hascustom) {
  507:                 $datatable .= '<th>'.&mt('Information').'</th>';
  508:             }
  509:             $datatable .= &Apache::loncommon::end_data_table_header_row();
  510:             my @available = sort(keys(%{$possroles}));
  511:             foreach my $role ('ad','in','ta','ep','st','cr') {
  512:                 foreach my $key (@available) {
  513:                     if ($key =~ m{^$role($|/)}) {
  514:                         my $trolecode = "$key./$cdom/$cnum";
  515:                         my $rolename = &Apache::lonnet::plaintext($key,$crstype,$cdom.'_'.$cnum);
  516:                         my $sec = $possroles->{$key};
  517:                         if ($sec ne '') {
  518:                             $trolecode .= '/'.$sec;
  519:                         }
  520:                         my $buttonname=$trolecode;
  521:                         $buttonname=~s/\W//g;
  522:                         $datatable .= &Apache::loncommon::start_data_table_row().
  523:                                       '<td><input name="'.$buttonname.'" type="button" value="'.
  524:                                       &mt('Select').'" onclick="javascript:enterrole(this.form,'.
  525:                                       "'$trolecode','$buttonname'".');" /></td>';
  526:                         if ($key =~ /^cr\//) {
  527:                             my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$key);
  528:                             $datatable .= '<td><span class="LC_nobreak">'.$rolename.'</span></td>';
  529:                             if ($hassection) {
  530:                                 $datatable .= '<td>'.$sec.'</td>';
  531:                             }
  532:                             $datatable.= '<td><span class="LC_fontsize_small LC_cusr_emph">'.
  533:                                           &mt('Custom role defined by [_1]',$rauthor.':'.$rdomain).
  534:                                           '</td>';
  535:                         } else {
  536:                             if ($hassection) {
  537:                                 $datatable .= '<td>'.$rolename.'</td>';
  538:                                 if ($hascustom) {
  539:                                     $datatable .= '<td colspan="2">'.$sec.'</td>';
  540:                                 } else {
  541:                                     $datatable .= '<td>'.$sec.'</td>';
  542:                                 }
  543:                             } elsif ($hascustom) {
  544:                                 $datatable .= '<td colspan="2">'.$rolename.'</td>';
  545:                             } else {
  546:                                 $datatable .= '<td>'.$rolename.'</td>';
  547:                             }
  548:                         }
  549:                         $datatable .= &Apache::loncommon::end_data_table_row();
  550:                     }
  551:                 }
  552:             }
  553:             $datatable .= &Apache::loncommon::end_data_table().
  554:                           '</form>';
  555:             my $standby = &mt('Role selected. Please stand by.');
  556:             $js = <<"ENDJS";
  557: <script type="text/javascript">
  558: // <![CDATA[
  559: 
  560: active=true;
  561: 
  562: function enterrole (thisform,rolecode,buttonname) {
  563:     if (active) {
  564:         active=false;
  565:         document.title='$standby';
  566:         window.status='$standby';
  567:         thisform.newrole.value=rolecode;
  568:         thisform.submit();
  569:     } else {
  570:        alert('$standby');
  571:     }
  572: }
  573: 
  574: // ]]>
  575: </script>
  576: ENDJS
  577:         } else {
  578:             if ($linkprot) {
  579:                 $title = 'No access';
  580:                 $preamble = '<p>'.&mt('Access unavailable for this LON-CAPA content.').'</p>';
  581:                 $args->{'only_body'} = 1;
  582:             } else {
  583:                 $title = 'No active role';
  584:                 $preamble = '<p>'.&mt("You have no active roles in this $lc_crstype so the page is currently unavailable to you.").'</p>';
  585:                 $args = {'bread_crumbs' => [{'href' => '','text' => 'Role status'},],};
  586:             }
  587:             $header = &mt('No access for: [_1]','<b>'.&Apache::loncommon::plainname($env{'user.name'},
  588:                                                                                     $env{'user.domain'}).'</b>');
  589:             if ((ref($futureroles) eq 'HASH') && (keys(%{$futureroles}) > 0)) {
  590:                 my @future = sort { $a <=> $b } (keys(%{$futureroles}));
  591:                 $preamble .= '<p>'.&mt('Access will begin: [_1].',&Apache::lonlocal::locallocaltime($future[0])).
  592:                              ' '.&mt('Please try again then.').'</p>';
  593:             } elsif ((ref($expiredroles) eq 'HASH') && (keys(%{$expiredroles}) > 0)) {
  594:                 my @expired = sort { $b <=> $a } (keys(%{$expiredroles}));
  595:                 $preamble .= '<p>'.&mt('Access ended: [_1].',&Apache::lonlocal::locallocaltime($expired[0])).'</p>';
  596:             } elsif ($linkprot) {
  597:                 if ($linkprotuser) {
  598:                     my ($uname,$udom) = split(/:/,$linkprotuser,2);
  599:                     $preamble .= '<p>'.&mt('As you followed a link from another system, while logged into that other system with the username: [_1], it is recommended that you contact your instructor.','<i>'.$uname.'</i>').'</p>';
  600:                 } else {
  601:                     my $relogin;
  602:                     my %data = (
  603:                                 origurl => $r->uri,
  604:                                 linkprot => $linkprot,
  605:                     );
  606:                     my $token =
  607:                         &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'retry');
  608:                     unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
  609:                             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  610:                             $relogin = '/adm/relaunch?rtoken='.$token;
  611:                     }
  612:                     $preamble .= '<p>'.&mt('You might try logging in with a different username and/or domain.').' '.
  613:                                        &mt('You are currently logged in as: [_1] in domain: [_2]',
  614:                                            '<i>'.$env{'user.name'}.'</i>','<i>'.$env{'user.domain'}.'</i>').'</p>';
  615:                     if ($relogin) {
  616:                         $preamble .= '<p>'.&mt('[_1]Log-in again[_2]','<a href="'.$relogin.'" target="_self">','</a>').'</p>';
  617:                     }
  618:                 }
  619:             }
  620:             if ($env{'form.ttoken'}) {
  621:                 &Apache::lonnet::tmpdel($env{'form.ttoken'});
  622:             }
  623:             if ($ltoken) {
  624:                 &Apache::lonnet::tmpdel($ltoken);
  625:             }
  626:         }
  627:     }
  628:     &Apache::loncommon::content_type($r,'text/html');
  629:     $r->send_http_header;
  630:     $r->print(&Apache::loncommon::start_page($title,$js,$args).
  631:               '<h3>'.$header.'</h3>'.
  632:               '<div>'.$preamble.'</div>'.
  633:               $datatable.
  634:               &Apache::loncommon::end_page());
  635:     return;
  636: }
  637: 
  638: sub generic_error {
  639:     my ($r) = @_;
  640:     my $continuelink;
  641:     unless ($env{'request.lti.login'}) {
  642:         my $linktext;
  643:         if ($env{'user.adv'}) {
  644:             $linktext = &mt('Continue to your roles page');
  645:         } else {
  646:             $linktext = &mt('Continue to your courses page');
  647:         }
  648:         $continuelink='<a href="/adm/roles">'.$linktext.'</a>';
  649:     }
  650:     my $msg = &mt('The page you requested does not exist.');
  651:     &Apache::loncommon::content_type($r,'text/html');
  652:     $r->send_http_header;
  653:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Link status'},],};
  654:     $r->print(&Apache::loncommon::start_page('Invalid URL',undef,$args).
  655:               '<div class="LC_error">'.$msg.'</div>'.
  656:               '<p>'.$continuelink.'</p>'.
  657:               &Apache::loncommon::end_page());
  658:     return;
  659: }
  660: 
  661: 1;

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