Annotation of loncom/interface/lontiny.pm, revision 1.14

1.1       raeburn     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: #
1.14    ! raeburn     5: # $Id: lontiny.pm,v 1.13 2022/07/02 17:17:47 raeburn Exp $
1.1       raeburn     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;
1.5       raeburn    38: use Apache::lonuserstate;
                     39: use Apache::lonnavmaps;
1.1       raeburn    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);
1.6       raeburn    47:     if ($handle ne '') {
1.5       raeburn    48:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     49:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.1       raeburn    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') {
1.8       raeburn    70:                             &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['ttoken']);
1.12      raeburn    71:                             my ($linkprot,$linkprotuser,$linkprotexit,$ltoken);
1.11      raeburn    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'};
1.12      raeburn    80:                                         $linkprotexit = $ltoken_info{'linkprotexit'};
1.11      raeburn    81:                                     } elsif ($link_info{'linkprot'}) {
                     82:                                         $linkprot = $link_info{'linkprot'};
                     83:                                         if ($link_info{'linkprotuser'}) {
                     84:                                             $linkprotuser = $link_info{'linkprotuser'};
                     85:                                         }
1.12      raeburn    86:                                         if ($link_info{'linkprotexit'}) {
                     87:                                             $linkprotexit = $link_info{'linkprotexit'};
                     88:                                         }
1.11      raeburn    89:                                     }
                     90:                                 }
                     91:                             }
1.5       raeburn    92:                             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
1.8       raeburn    93:                                 # Check for ttoken
                     94:                                 my $newlauncher = &launch_check($r->uri,$symb);
1.5       raeburn    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
1.7       raeburn   123:                                     if ($newlauncher) {
1.5       raeburn   124:                                         $update = 1;
1.7       raeburn   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:                                                     }
1.5       raeburn   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';
1.1       raeburn   166:                                         }
1.5       raeburn   167:                                         my $location = $protocol.'://'.$host.$realuri;
                    168:                                         $r->headers_out->set(Location => $location);
                    169:                                         return REDIRECT;
1.1       raeburn   170:                                     }
                    171:                                 }
1.5       raeburn   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:                                 }
1.11      raeburn   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);
1.5       raeburn   185:                                 if (keys(%roleshash)) {
1.11      raeburn   186:                                     my $now = time;
1.5       raeburn   187:                                     foreach my $entry (keys(%roleshash)) {
                    188:                                         if ($entry =~ /^\Q$cnum:$cdom:\E([^:]+):([^:]*)$/) {
1.11      raeburn   189:                                             my ($role,$sec) = ($1,$2);
                    190:                                             $possroles{$role} = $sec;
                    191:                                             if ($sec ne '') {
1.5       raeburn   192:                                                 $hassection = 1;
                    193:                                             }
1.11      raeburn   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:                                             }
1.5       raeburn   209:                                         }
1.4       raeburn   210:                                     }
                    211:                                 }
1.11      raeburn   212:                                 my @allposs = keys(%active);
1.5       raeburn   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'});
1.4       raeburn   219:                                         }
1.5       raeburn   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'.
1.11      raeburn   229:                                                                    '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.5       raeburn   230:                                                 if ($env{'form.ltitarget'} eq 'iframe') {
                    231:                                                     $destination .= '&ltitarget=iframe';
                    232:                                                 }
                    233:                                                 &do_redirect($r,$destination);
                    234:                                                 return OK;
1.4       raeburn   235:                                             }
                    236:                                         }
                    237:                                     }
                    238:                                 }
1.5       raeburn   239:                                 if (@allposs == 0) {
1.12      raeburn   240:                                     &show_roles($r,\%crsenv,\%active,'','',\%future,\%expired,$linkprot,$linkprotuser,$linkprotexit,$ltoken);
1.5       raeburn   241:                                 } elsif (@allposs == 1) {
                    242:                                     my $newrole = "$allposs[0]./$cdom/$cnum";
                    243:                                     $newrole = "$allposs[0]./$cdom/$cnum";
                    244:                                     if ($possroles{$allposs[0]} ne '') {
1.11      raeburn   245:                                         $newrole .= "/$possroles{$allposs[0]}";
1.1       raeburn   246:                                     }
                    247:                                     my $destination .= '/adm/roles?selectrole=1&'.$newrole.'=1'.
1.11      raeburn   248:                                                        '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.8       raeburn   249:                                     if ($env{'form.ttoken'}) {
                    250:                                         $destination .= '&ttoken='.$env{'form.ttoken'};
                    251:                                     }
1.11      raeburn   252:                                     &do_redirect($r,$destination,$linkprot);
1.5       raeburn   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'.
1.11      raeburn   263:                                                            '&destinationurl='.&HTML::Entities::encode($r->uri,'&<>"');
1.8       raeburn   264:                                         if ($env{'form.ttoken'}) {
                    265:                                             $destination .= '&ttoken='.$env{'form.ttoken'};
                    266:                                         }
1.11      raeburn   267:                                         &do_redirect($r,$destination,$linkprot);
1.5       raeburn   268:                                     } else {
                    269:                                         my $hascustom;
                    270:                                         if (grep(/^cr\//,@allposs)) {
                    271:                                             $hascustom = 1;
                    272:                                         }
                    273:                                         &show_roles($r,\%crsenv,\%possroles,$hassection,$hascustom);
1.1       raeburn   274:                                     }
                    275:                                 }
1.5       raeburn   276:                                 return OK;
1.1       raeburn   277:                             }
                    278:                         }
                    279:                     }
                    280:                 }
                    281:             }
                    282:         }
                    283:         &generic_error($r);
                    284:         return OK;
                    285:     } else {
                    286:         return FORBIDDEN;
                    287:     }
                    288: }
                    289: 
1.7       raeburn   290: sub launch_check {
1.8       raeburn   291:     my ($linkuri,$symb) = @_;
1.7       raeburn   292:     my ($linkprotector,$linkproturi,$linkkey,$newlauncher);
1.8       raeburn   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'});
1.10      raeburn   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:             }
1.11      raeburn   306:             &Apache::lonnet::tmpdel($link_info{'ltoken'});
1.10      raeburn   307:         }
1.7       raeburn   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:             }
1.8       raeburn   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:             }
1.7       raeburn   351:         }
1.8       raeburn   352:         if ($link_info{'checklaunch'}) {
                    353:             $newlauncher = 1;
1.7       raeburn   354:         }
1.8       raeburn   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');
1.7       raeburn   363:         }
1.8       raeburn   364:     } else {
                    365:         $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);
1.7       raeburn   366:     }
1.8       raeburn   367:     if ($deeplink ne '') {
                    368:         my $disallow;
1.9       raeburn   369:         my ($state,$others,$listed,$scope,$protect,$display,$target) = split(/,/,$deeplink);
1.8       raeburn   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;
1.7       raeburn   380:                 }
1.8       raeburn   381:             } elsif ($acctype eq 'key') {
                    382:                 if ($linkkey ne '') {
                    383:                     unless ($linkkey eq $item) {
                    384:                         $disallow = 1;
1.7       raeburn   385:                     }
1.8       raeburn   386:                 } else {
                    387:                     $disallow = 1;
1.7       raeburn   388:                 }
1.8       raeburn   389:             }
                    390:         }
                    391:         if ($disallow) {
                    392:             if ($currdeeplinklogin eq $linkuri) {
                    393:                 &Apache::lonnet::delenv('request.deeplink.login');
1.10      raeburn   394:                 if ($env{'request.deeplink.target'} ne '') {
                    395:                     &Apache::lonnet::delenv('request.deeplink.target');
                    396:                 }
1.8       raeburn   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'}) {
1.14    ! raeburn   404:                         &Apache::lonnet::delenv('request.linkprot');
1.7       raeburn   405:                     }
1.8       raeburn   406:                     if ($linkkey ne '') {
                    407:                         &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
                    408:                     } elsif ($env{'request.linkkey'} ne '') {
1.13      raeburn   409:                         &Apache::lonnet::delenv('request.linkkey');
1.7       raeburn   410:                     }
1.8       raeburn   411:                     $newlauncher = 1;
1.7       raeburn   412:                 }
                    413:             }
                    414:             &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
1.9       raeburn   415:             if ($target ne '') {
                    416:                 &Apache::lonnet::appenv({'request.deeplink.target' => $target});
1.10      raeburn   417:             } elsif ($env{'request.deeplink.target'} ne '') {
                    418:                 &Apache::lonnet::delenv('request.deeplink.target');
1.9       raeburn   419:             }
1.7       raeburn   420:         }
                    421:     } else {
                    422:         if ($linkprotector) {
                    423:             &Apache::lonnet::appenv({'request.linkprot' => $linkprotector.':'.$linkproturi});
1.8       raeburn   424:         } elsif ($env{'request.linkprot'}) {
1.14    ! raeburn   425:             &Apache::lonnet::delenv('request.linkprot');
1.8       raeburn   426:         }
                    427:         if ($linkkey ne '') {
1.7       raeburn   428:             &Apache::lonnet::appenv({'request.linkkey' => $linkkey});
1.8       raeburn   429:         } else {
1.13      raeburn   430:             &Apache::lonnet::delenv('request.linkkey');
1.7       raeburn   431:         }
1.8       raeburn   432:         &Apache::lonnet::appenv({'request.deeplink.login' => $linkuri});
1.10      raeburn   433:         if ($env{'request.deeplink.target'} ne '') {
                    434:             &Apache::lonnet::delenv('request.deeplink.target');
                    435:         }
1.7       raeburn   436:     }
                    437:     return $newlauncher;
                    438: }
                    439: 
1.1       raeburn   440: sub do_redirect {
1.11      raeburn   441:     my ($r,$destination,$linkprot) = @_;
1.2       raeburn   442:     my $windowname = 'loncapaclient';
                    443:     if ($env{'request.lti.login'}) {
                    444:         $windowname .= 'lti';
                    445:     }
1.1       raeburn   446:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
                    447:     my $args = {'bread_crumbs' => [{'href' => '','text' => 'Role initialization'},],};
1.11      raeburn   448:     if ($linkprot) {
                    449:         $args = {'only_body' => 1,
                    450:                  'redirect'  => [0,$destination],};
                    451:     }
1.1       raeburn   452:     &Apache::loncommon::content_type($r,'text/html');
                    453:     $r->send_http_header;
1.11      raeburn   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:     }
1.1       raeburn   465:     return;
                    466: }
                    467: 
                    468: sub show_roles {
1.12      raeburn   469:     my ($r,$crsenv,$possroles,$hassection,$hascustom,$futureroles,$expiredroles,$linkprot,$linkprotuser,$linkprotexit,$ltoken) = @_;
1.1       raeburn   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"},],};
1.11      raeburn   488:             if ($linkprot) {
                    489:                 $args = {'only_body' => 1};
                    490:             }
1.1       raeburn   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">'.
1.8       raeburn   498:                          '<input type="hidden" name="newrole" value="" />'."\n".
                    499:                          '<input type="hidden" name="selectrole" value="1" />'."\n".
1.11      raeburn   500:                          '<input type="hidden" name="destinationurl" value="'.&HTML::Entities::encode($r->uri,'&<>"').'" />'."\n";
1.8       raeburn   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>';
1.1       raeburn   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 {
1.11      raeburn   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,
1.12      raeburn   609:                                 linkprotexit => $linkprotexit,
1.11      raeburn   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:             }
1.1       raeburn   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) = @_;
1.3       raeburn   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>';
1.1       raeburn   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>