File:  [LON-CAPA] / loncom / interface / lontiny.pm
Revision 1.14: download - view: text, annotated - select for diffs
Sat Jul 2 17:23:02 2022 UTC (23 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Typos

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

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