Annotation of loncom/auth/lonroles.pm, revision 1.322

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.322   ! raeburn     4: # $Id: lonroles.pm,v 1.321 2016/11/08 23:09:33 raeburn Exp $
1.31      www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.32      harris41   28: ###
1.22      harris41   29: 
1.210     jms        30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: Apache::lonroles - User Roles Screen
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: Invoked by /etc/httpd/conf/srm.conf:
                     39: 
                     40:  <Location /adm/roles>
                     41:  PerlAccessHandler       Apache::lonacc
                     42:  SetHandler perl-script
                     43:  PerlHandler Apache::lonroles
                     44:  ErrorDocument     403 /adm/login
                     45:  ErrorDocument	  500 /adm/errorhandler
                     46:  </Location>
                     47: 
                     48: =head1 OVERVIEW
                     49: 
                     50: =head2 Choosing Roles
                     51: 
                     52: C<lonroles> is a handler that allows a user to switch roles in
                     53: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                     54: default role that a user has before selecting a role, as widely as
                     55: possible, but certain handlers for example need specification which
                     56: course they should act on, etc. Both in this scenario, and when the
                     57: handler determines via C<lonnet>'s C<&allowed> function that a certain
                     58: action is not allowed, C<lonroles> is used as error handler. This
                     59: allows the user to select another role which may have permission to do
1.246     droeschl   60: what they were trying to do.
1.210     jms        61: 
                     62: =begin latex
                     63: 
                     64: \begin{figure}
                     65: \begin{center}
                     66: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                     67:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                     68: \end{center}
                     69: \end{figure}
                     70: 
                     71: =end latex
                     72: 
                     73: =head2 Role Initialization
                     74: 
                     75: The privileges for a user are established at login time and stored in the session environment. As a consequence, a new role does not become active till the next login. Handlers are able to query for privileges using C<lonnet>'s C<&allowed> function. When a user first logs in, their role is the "common" role, which means that they have the sum of all of their privileges. During a session it might become necessary to choose a particular role, which as a consequence also limits the user to only the privileges in that particular role.
                     76: 
                     77: =head1 INTRODUCTION
                     78: 
                     79: This module enables a user to select what role he wishes to
                     80: operate under (instructor, student, teaching assistant, course
                     81: coordinator, etc).  These roles are pre-established by the actions
                     82: of upper-level users.
                     83: 
                     84: This is part of the LearningOnline Network with CAPA project
                     85: described at http://www.lon-capa.org.
                     86: 
                     87: =head1 HANDLER SUBROUTINE
                     88: 
                     89: This routine is called by Apache and mod_perl.
                     90: 
                     91: =over 4
                     92: 
                     93: =item *
                     94: 
                     95: Roles Initialization (yes/no)
                     96: 
                     97: =item *
                     98: 
                     99: Get Error Message from Environment
                    100: 
                    101: =item *
                    102: 
                    103: Who is this?
                    104: 
                    105: =item *
                    106: 
                    107: Generate Page Output
                    108: 
                    109: =item *
                    110: 
                    111: Choice or no choice
                    112: 
                    113: =item *
                    114: 
                    115: Table
                    116: 
                    117: =item *
                    118: 
                    119: Privileges
                    120: 
                    121: =back
                    122: 
                    123: =cut
                    124: 
                    125: 
1.1       harris41  126: package Apache::lonroles;
                    127: 
                    128: use strict;
1.118     albertel  129: use Apache::lonnet;
1.7       www       130: use Apache::lonuserstate();
1.304     musolffc  131: use Apache::Constants qw(:common REDIRECT);
1.2       www       132: use Apache::File();
1.26      www       133: use Apache::lonmenu;
1.29      albertel  134: use Apache::loncommon;
1.104     raeburn   135: use Apache::lonhtmlcommon;
1.57      www       136: use Apache::lonannounce;
1.72      www       137: use Apache::lonlocal;
1.151     www       138: use Apache::lonpageflip();
1.167     albertel  139: use Apache::lonnavdisplay();
1.241     raeburn   140: use Apache::loncoursequeueadmin;
1.279     raeburn   141: use Apache::longroup;
1.283     raeburn   142: use Apache::lonrss;
1.313     raeburn   143: use Apache::lonplacementtest;
1.120     albertel  144: use GDBM_File;
1.170     albertel  145: use LONCAPA qw(:DEFAULT :match);
1.201     raeburn   146: use HTML::Entities;
1.276     raeburn   147: 
1.1       harris41  148: 
1.62      matthew   149: sub redirect_user {
1.245     droeschl  150:     my ($r,$title,$url,$msg) = @_;
1.62      matthew   151:     $msg = $title if (! defined($msg));
1.73      www       152:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew   153:     &Apache::loncommon::no_cache($r);
                    154:     $r->send_http_header;
1.228     bisitz    155: 
                    156:     # Breadcrumbs
                    157:     my $brcrum = [{'href' => $url,
                    158:                    'text' => 'Switching Role'},];
1.147     albertel  159:     my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
1.228     bisitz    160:                                                     {'redirect' => [1,$url],
                    161:                                                      'bread_crumbs' => $brcrum,});
1.147     albertel  162:     my $end_page   = &Apache::loncommon::end_page();
                    163: 
1.92      www       164: # Note to style police: 
                    165: # This must only replace the spaces, nothing else, or it bombs elsewhere.
                    166:     $url=~s/ /\%20/g;
1.93      albertel  167:     $r->print(<<ENDREDIR);
1.147     albertel  168: $start_page
1.222     bisitz    169: <p>$msg</p>
1.147     albertel  170: $end_page
1.62      matthew   171: ENDREDIR
                    172:     return;
                    173: }
                    174: 
1.150     www       175: sub error_page {
                    176:     my ($r,$error,$dest)=@_;
                    177:     &Apache::loncommon::content_type($r,'text/html');
                    178:     &Apache::loncommon::no_cache($r);
                    179:     $r->send_http_header;
                    180:     return OK if $r->header_only;
1.228     bisitz    181:     # Breadcrumbs
                    182:     my $brcrum = [{'href' => $dest,
                    183:                    'text' => 'Problems during Course Initialization'},];
                    184:     $r->print(&Apache::loncommon::start_page('Problems during Course Initialization',
                    185:                                              undef,
                    186:                                              {'bread_crumbs' => $brcrum,})
                    187:     );
                    188:     $r->print(
1.225     bisitz    189:         '<script type="text/javascript">'.
                    190:         '// <![CDATA['.
                    191:         &Apache::lonmenu::rawconfig().
                    192:         '// ]]>'.
                    193:         '</script>'.
                    194: 	      '<p class="LC_error">'.&mt('The following problems occurred:').
1.228     bisitz    195:           '<br />'.
1.150     www       196: 	      $error.
1.228     bisitz    197: 	      '</p><br /><a href="'.$dest.'">'.&mt('Continue').'</a>'
                    198:     );
                    199:     $r->print(&Apache::loncommon::end_page());
1.150     www       200: }
                    201: 
1.1       harris41  202: sub handler {
1.10      www       203: 
1.1       harris41  204:     my $r = shift;
                    205: 
1.308     raeburn   206:     # Check for critical messages and redirect if present.
1.304     musolffc  207:     my ($redirect,$url) = &Apache::loncommon::critical_redirect(300);
                    208:     if ($redirect) {
                    209:         &Apache::loncommon::content_type($r,'text/html');
                    210:         $r->header_out(Location => $url);
                    211:         return REDIRECT;
                    212:     }
                    213: 
1.6       www       214:     my $now=time;
1.118     albertel  215:     my $then=$env{'user.login.time'};
1.226     raeburn   216:     my $refresh=$env{'user.refresh.time'};
1.260     raeburn   217:     my $update=$env{'user.update.time'};
1.226     raeburn   218:     if (!$refresh) {
                    219:         $refresh = $then;
                    220:     }
1.260     raeburn   221:     if (!$update) {
                    222:         $update = $then;
                    223:     }
                    224: 
1.274     raeburn   225:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    226: 
                    227: # -------------------------------------------------- Check if setting hot list 
                    228:     my $hotlist;
                    229:     if ($env{'form.action'} eq 'verify_and_change_rolespref') {
                    230:         $hotlist = &Apache::lonpreferences::verify_and_change_rolespref($r);
                    231:     }
                    232: 
1.260     raeburn   233: # -------------------------------------------------------- Check for new roles
                    234:     my $updateresult;
1.274     raeburn   235:     if ($env{'form.state'} eq 'doupdate') {
1.260     raeburn   236:         my $show_course=&Apache::loncommon::show_course();
                    237:         my $checkingtxt;
                    238:         if ($show_course) {
                    239:             $checkingtxt = &mt('Checking for new courses ...');
                    240:         } else {
                    241:             $checkingtxt = &mt('Checking for new roles ...');
                    242:         }
1.274     raeburn   243:         $updateresult = $checkingtxt;
1.260     raeburn   244:         $updateresult .= &update_session_roles();
                    245:         &Apache::lonnet::appenv({'user.update.time'  => $now});
                    246:         $update = $now;
1.272     raeburn   247:         &Apache::loncoursequeueadmin::reqauthor_check();
1.270     raeburn   248:     }
                    249: 
                    250: # -------------------------------------------------- Check for author requests
                    251:     my $reqauthor;
1.274     raeburn   252:     if ($env{'form.state'} eq 'requestauthor') {
1.272     raeburn   253:        $reqauthor = &Apache::loncoursequeueadmin::process_reqauthor(\$update);
1.260     raeburn   254:     }
                    255: 
1.6       www       256:     my $envkey;
1.107     raeburn   257:     my %dcroles = ();
1.317     raeburn   258:     my %dhroles = ();
                    259:     my ($numdc,$numdh,$numadhoc) = &check_for_adhoc(\%dcroles,\%dhroles,$update,$then);
1.304     musolffc  260:     my $loncaparev = $r->dir_config('lonVersion');
1.10      www       261: 
1.6       www       262: # ================================================================== Roles Init
1.118     albertel  263:     if ($env{'form.selectrole'}) {
1.188     www       264: 
                    265:         my $locknum=&Apache::lonnet::get_locks();
                    266:         if ($locknum) { return 409; }
                    267: 
1.315     raeburn   268:         my $custom_adhoc;
1.134     www       269:         if ($env{'form.newrole'}) {
                    270:             $env{'form.'.$env{'form.newrole'}}=1;
1.315     raeburn   271: # Check if this is a Domain Helpdesk role trying to enter a course
                    272:             if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/\w+\./\1/$match_courseid$}) {
1.317     raeburn   273:                 if ($dhroles{$1}) {
1.315     raeburn   274:                     $custom_adhoc = 1;
                    275:                 }
                    276:             }
1.134     www       277: 	}
1.118     albertel  278: 	if ($env{'request.course.id'}) {
1.185     raeburn   279:             # Check if user is CC trying to select a course role
                    280:             if ($env{'form.switchrole'}) {
1.252     raeburn   281:                 my $switch_is_active;
                    282:                 if (defined($env{'user.role.'.$env{'form.switchrole'}})) {
                    283:                     my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
                    284:                     if (!$end || $end > $now) {
1.260     raeburn   285:                         if (!$start || $start < $update) {
1.252     raeburn   286:                             $switch_is_active = 1;
                    287:                         }
                    288:                     }
                    289:                 }
                    290:                 unless ($switch_is_active) {
1.260     raeburn   291:                     &adhoc_course_role($refresh,$update,$then);
1.185     raeburn   292:                 }
                    293:             }
1.118     albertel  294: 	    my %temp=('logout_'.$env{'request.course.id'} => time);
1.33      www       295: 	    &Apache::lonnet::put('email_status',\%temp);
1.118     albertel  296: 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100     albertel  297: 	}
1.310     raeburn   298: 	&Apache::lonnet::appenv({"request.course.id"           => '',
                    299: 			 	 "request.course.fn"           => '',
                    300: 				 "request.course.uri"          => '',
                    301: 				 "request.course.sec"          => '',
                    302:                                  "request.course.tied"         => '',
                    303:                                  "request.course.timechecked"  => '',
                    304: 				 "request.role"                => 'cm',
                    305:                                  "request.role.adv"            => $env{'user.adv'},
                    306: 				 "request.role.domain"         => $env{'user.domain'}});
1.315     raeburn   307: # Check if Domain Helpdesk role trying to enter a course needs privs to be created
1.322   ! raeburn   308:         if ($env{'form.newrole'} =~ m{^cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)(?:/(\w+)|$)}) {
1.315     raeburn   309:             my $cdom = $1;
                    310:             my $rolename = $2;
                    311:             my $cnum = $3;
1.322   ! raeburn   312:             my $sec = $4;
1.315     raeburn   313:             if ($custom_adhoc) {
                    314:                 my %adhocroles = &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
                    315:                                                                   'adhocroles.'.$cdom);
                    316:                 if (keys(%adhocroles)) {
                    317:                     my @adhoc = split(',',$adhocroles{'adhocroles.'.$cdom});
                    318:                     if (grep(/^\Q$rolename\E$/,@adhoc)) {
                    319:                         if (&Apache::lonnet::check_adhoc_privs($cdom,$cnum,$update,$refresh,$now,
1.322   ! raeburn   320:                                                                "cr/$cdom/$cdom".'-domainconfig/'.$rolename,undef,$sec)) {
1.316     raeburn   321:                             &Apache::lonnet::appenv({"environment.internal.$cdom.$cnum.cr/$cdom/$cdom".'-domainconfig/'."$rolename.adhoc" => time});
1.315     raeburn   322:                         }
                    323:                     }
                    324:                 }
                    325:             }
1.319     raeburn   326:         } elsif (($numdc > 0) || ($numdh > 0)) {
1.182     www       327: # Check if user is a DC trying to enter a course or author space and needs privs to be created
1.319     raeburn   328: # Check if user is a DH trying to enter a course and needs privs to be created
1.296     raeburn   329:             foreach my $envkey (keys(%env)) {
1.240     raeburn   330: # Is this an ad-hoc Coordinator role?
1.319     raeburn   331:                 if ($numdc) {
                    332:                     if (my ($ccrole,$domain,$coursenum) =
                    333: 		        ($envkey =~ m-^form\.(cc|co)\./($match_domain)/($match_courseid)$-)) {
                    334:                         if ($dcroles{$domain}) {
                    335:                             if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,
                    336:                                                                    $update,$refresh,$now,$ccrole)) {
                    337:                                 &Apache::lonnet::appenv({"environment.internal.$domain.$coursenum.$ccrole.adhoc" => time});
                    338:                             }
1.275     raeburn   339:                         }
1.319     raeburn   340:                         last;
1.182     www       341:                     }
1.193     raeburn   342: # Is this an ad-hoc CA-role?
1.319     raeburn   343:                     if (my ($domain,$user) =
                    344: 		        ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
                    345:                         if (($domain eq $env{'user.domain'}) && ($user eq $env{'user.name'})) {
                    346:                             delete($env{$envkey});
                    347:                             $env{'form.au./'.$domain.'/'} = 1;
1.206     raeburn   348:                             my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    349:                             if ($server_status eq 'switchserver') {
1.319     raeburn   350:                                 my $trolecode = 'au./'.$domain.'/';
1.248     raeburn   351:                                 my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
1.206     raeburn   352:                                 $r->internal_redirect($switchserver);
1.285     raeburn   353:                                 return OK;
1.206     raeburn   354:                             }
                    355:                             last;
                    356:                         }
1.319     raeburn   357:                         if (my ($castart,$caend) = ($env{'user.role.ca./'.$domain.'/'.$user} =~ /^(\d*)\.(\d*)$/)) {
                    358:                             if (((($castart) && ($castart < $now)) || !$castart) && 
                    359:                                 ((!$caend) || (($caend) && ($caend > $now)))) {
                    360:                                 my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    361:                                 if ($server_status eq 'switchserver') {
                    362:                                     my $trolecode = 'ca./'.$domain.'/'.$user;
                    363:                                     my $switchserver = '/adm/switchserver?otherserver='.$home.'&amp;role='.$trolecode;
                    364:                                     $r->internal_redirect($switchserver);
                    365:                                     return OK;
                    366:                                 }
                    367:                                 last;
                    368:                             }
                    369:                         }
                    370:                         # Check if author blocked ca-access
                    371:                         my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
                    372:                         if ($blocked{'domcoord.author'} eq 'blocked') {
                    373:                             delete($env{$envkey});
                    374:                             $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
                    375:                             last;
                    376:                         }
                    377:                         if ($dcroles{$domain}) {
                    378:                             my ($server_status,$home) = &check_author_homeserver($user,$domain);
                    379:                             if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
                    380:                                 &Apache::lonnet::check_adhoc_privs($domain,$user,$update,
                    381:                                                                    $refresh,$now,'ca');
                    382:                                 if ($server_status eq 'switchserver') {
                    383:                                     my $trolecode = 'ca./'.$domain.'/'.$user; 
                    384:                                     my $switchserver = '/adm/switchserver?'
                    385:                                                       .'otherserver='.$home.'&amp;role='.$trolecode;
                    386:                                     $r->internal_redirect($switchserver);
                    387:                                     return OK;
                    388:                                 }
                    389:                             } else {
                    390:                                 delete($env{$envkey});
1.193     raeburn   391:                             }
                    392:                         } else {
                    393:                             delete($env{$envkey});
                    394:                         }
1.319     raeburn   395:                         last;
1.182     www       396:                     }
                    397:                 }
1.319     raeburn   398:                 if ($numdh) {
                    399: # Is this an ad hoc custom role in a course/community?
1.322   ! raeburn   400:                     if (my ($domain,$rolename,$coursenum,$sec) = ($envkey =~ m{^form\.cr/($match_domain)/\1\-domainconfig/(\w+)\./\1/($match_courseid)(?:/(\w+)|$)})) {
        !           401:                         if ($dhroles{$domain}) {
1.320     raeburn   402:                             my @adhoc; 
                    403:                             if ($env{'environment.adhocroles.'.$domain}) {
                    404:                                 @adhoc = split(',',$env{'environment.adhocroles.'.$domain});
                    405:                             } else {
                    406:                                 my %adhocroles = &Apache::lonnet::userenvironment($env{'user.domain'},$env{'user.name'},
                    407:                                                                                   'adhocroles.'.$domain);
                    408:                                 if (keys(%adhocroles)) {
                    409:                                     @adhoc = split(',',$adhocroles{'adhocroles.'.$domain});
                    410:                                 }
                    411:                             }
                    412:                             if ((@adhoc > 0) && ($rolename ne '')) {
                    413:                                 if (grep(/^\Q$rolename\E$/,@adhoc)) {
                    414:                                     if (&Apache::lonnet::check_adhoc_privs($domain,$coursenum,$update,$refresh,$now,
1.322   ! raeburn   415:                                                                            "cr/$domain/$domain".'-domainconfig/'.$rolename,
        !           416:                                                                            undef,$sec)) {
1.320     raeburn   417:                                         &Apache::lonnet::appenv({"environment.internal.$domain.$coursenum.cr/$domain/$domain".
                    418:                                                                  '-domainconfig/'."$rolename.adhoc" => time});
                    419:                                     }
                    420:                                 } else {
                    421:                                     delete($env{$envkey});
                    422:                                 }
                    423:                             } else {
                    424:                                 delete($env{$envkey});
                    425:                             }
                    426:                         } else {
                    427:                             delete($env{$envkey});
                    428:                         }
                    429:                         last;
                    430:                     }
                    431:                 }
                    432:             }
1.107     raeburn   433:         }
1.296     raeburn   434:         foreach $envkey (keys(%env)) {
1.40      matthew   435:             next if ($envkey!~/^user\.role\./);
1.102     raeburn   436:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
1.260     raeburn   437:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.218     raeburn   438:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
1.118     albertel  439:             if ($env{'form.'.$trolecode}) {
1.55      albertel  440: 		if ($tstatus eq 'is') {
                    441: 		    $where=~s/^\///;
                    442: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.255     raeburn   443:                     if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
                    444:                         my $home = $env{'course.'.$cdom.'_'.$cnum.'.home'};
                    445:                         my @ids = &Apache::lonnet::current_machine_ids();
                    446:                         unless ($loncaparev eq '' && $home && grep(/^\Q$home\E$/,@ids)) {
                    447:                             my %curr_reqd_hash = &Apache::lonnet::userenvironment($cdom,$cnum,'internal.releaserequired');
1.256     raeburn   448:                             if ($curr_reqd_hash{'internal.releaserequired'} ne '') {
1.255     raeburn   449:                                 my ($switchserver,$switchwarning) =
1.310     raeburn   450:                                     &Apache::loncommon::check_release_required($loncaparev,$cdom.'_'.$cnum,$trolecode,
                    451:                                                                                $curr_reqd_hash{'internal.releaserequired'});
1.256     raeburn   452:                                 if ($switchwarning ne '' || $switchserver ne '') {
                    453:                                     &Apache::loncommon::content_type($r,'text/html');
                    454:                                     &Apache::loncommon::no_cache($r);
                    455:                                     $r->send_http_header;
1.310     raeburn   456:                                     $r->print(&Apache::loncommon::check_release_result($switchwarning,$switchserver));
1.256     raeburn   457:                                     return OK;
1.255     raeburn   458:                                 }
                    459:                             }
                    460:                         }
                    461:                     }
1.137     raeburn   462: # check for course groups
                    463:                     my %coursegroups = &Apache::lonnet::get_active_groups(
                    464:                           $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
                    465:                     my $cgrps = join(':',keys(%coursegroups));
                    466: 
1.111     albertel  467: # store role if recent_role list being kept
1.118     albertel  468:                     if ($env{'environment.recentroles'}) {
1.158     albertel  469:                         my %frozen_roles =
                    470:                            &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
1.111     albertel  471: 			&Apache::lonhtmlcommon::store_recent('roles',
1.158     albertel  472: 							     $trolecode,' ',$frozen_roles{$trolecode});
1.111     albertel  473:                     }
                    474: 
                    475: 
1.53      www       476: # check for keyed access
1.55      albertel  477: 		    if (($role eq 'st') && 
1.118     albertel  478:                        ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89      www       479: # who is key authority?
                    480: 			my $authdom=$cdom;
                    481: 			my $authnum=$cnum;
1.118     albertel  482: 			if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89      www       483: 			    ($authnum,$authdom)=
1.172     albertel  484: 				split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89      www       485: 			}
                    486: # check with key authority
                    487: 			unless (&Apache::lonnet::validate_access_key(
1.118     albertel  488: 				     $env{'environment.key.'.$cdom.'_'.$cnum},
1.89      www       489: 					     $authdom,$authnum)) {
1.53      www       490: # there is no valid key
1.118     albertel  491: 			     if ($env{'form.newkey'}) {
1.53      www       492: # student attempts to register a new key
1.89      www       493: 				 &Apache::loncommon::content_type($r,'text/html');
                    494: 				 &Apache::loncommon::no_cache($r);
                    495: 				 $r->send_http_header;
                    496: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  497: 				 my $start_page=&Apache::loncommon::start_page
1.89      www       498: 				    ('Verifying Access Key to Unlock this Course');
1.147     albertel  499: 				 my $end_page=&Apache::loncommon::end_page();
1.90      www       500: 				 my $buttontext=&mt('Enter Course');
                    501: 				 my $message=&mt('Successfully registered key');
                    502: 				 my $assignresult=
                    503: 				     &Apache::lonnet::assign_access_key(
1.118     albertel  504: 						     $env{'form.newkey'},
1.90      www       505: 						     $authdom,$authnum,
1.91      www       506: 						     $cdom,$cnum,
1.118     albertel  507:                                                      $env{'user.domain'},
                    508: 						     $env{'user.name'},
1.204     bisitz    509:                                                      &mt('Assigned from [_1] at [_2] for [_3]'
                    510:                                                         ,$ENV{'REMOTE_ADDR'}
                    511:                                                         ,&Apache::lonlocal::locallocaltime()
                    512:                                                         ,$trolecode)
                    513:                                                      );
1.90      www       514: 				 unless ($assignresult eq 'ok') {
                    515: 				     $assignresult=~s/^error\:\s*//;
                    516: 				     $message=&mt($assignresult).
                    517: 				     '<br /><a href="/adm/logout">'.
1.89      www       518: 				     &mt('Logout').'</a>';
1.90      www       519: 				     $buttontext=&mt('Re-Enter Key');
                    520: 				 }
1.89      www       521: 				 $r->print(<<ENDENTEREDKEY);
1.147     albertel  522: $start_page
1.179     raeburn   523: <script type="text/javascript">
1.225     bisitz    524: // <![CDATA[
1.89      www       525: $swinfo
1.225     bisitz    526: // ]]>
1.89      www       527: </script>
1.225     bisitz    528: <form action="" method="post">
1.89      www       529: <input type="hidden" name="selectrole" value="1" />
                    530: <input type="hidden" name="$trolecode" value="1" />
1.211     tempelho  531: <span class="LC_fontsize_large">$message</span><br />
1.89      www       532: <input type="submit" value="$buttontext" />
                    533: </form>
1.147     albertel  534: $end_page
1.89      www       535: ENDENTEREDKEY
                    536:                                  return OK;
1.55      albertel  537: 			     } else {
1.53      www       538: # print form to enter a new key
1.73      www       539: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  540: 				 &Apache::loncommon::no_cache($r);
                    541: 				 $r->send_http_header;
                    542: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  543: 				 my $start_page=&Apache::loncommon::start_page
1.55      albertel  544: 				    ('Enter Access Key to Unlock this Course');
1.147     albertel  545: 				 my $end_page=&Apache::loncommon::end_page();
1.55      albertel  546: 				 $r->print(<<ENDENTERKEY);
1.147     albertel  547: $start_page
1.179     raeburn   548: <script type="text/javascript">
1.225     bisitz    549: // <![CDATA[
1.53      www       550: $swinfo
1.225     bisitz    551: // ]]>
1.53      www       552: </script>
1.225     bisitz    553: <form action="" method="post">
1.89      www       554: <input type="hidden" name="selectrole" value="1" />
                    555: <input type="hidden" name="$trolecode" value="1" />
1.118     albertel  556: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
1.53      www       557: <input type="submit" value="Enter key" />
                    558: </form>
1.147     albertel  559: $end_page
1.53      www       560: ENDENTERKEY
1.55      albertel  561: 				 return OK;
                    562: 			     }
                    563: 			 }
                    564: 		     }
1.118     albertel  565: 		    &Apache::lonnet::log($env{'user.domain'},
                    566: 					 $env{'user.name'},
                    567: 					 $env{'user.home'},
1.87      www       568: 					 "Role ".$trolecode);
1.101     albertel  569: 		    
1.56      www       570: 		    &Apache::lonnet::appenv(
1.186     raeburn   571: 					   {'request.role'        => $trolecode,
                    572: 					    'request.role.domain' => $cdom,
                    573: 					    'request.course.sec'  => $csec,
                    574:                                             'request.course.groups' => $cgrps});
1.101     albertel  575:                     my $tadv=0;
1.62      matthew   576: 
1.125     www       577: 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.152     raeburn   578:                         my $msg;
1.55      albertel  579: 			my ($furl,$ferr)=
                    580: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1.284     raeburn   581:                         unless ($ferr) {
                    582:                             unless (($env{'form.switchrole'}) || 
                    583:                                     ($env{"environment.internal.$cdom.$cnum.$role.adhoc"})) {
                    584:                                 &Apache::lonnet::put('nohist_crslastlogin',
                    585:                                     {$env{'user.name'}.':'.$env{'user.domain'}.
                    586:                                      ':'.$csec.':'.$role => $now},$cdom,$cnum);
                    587:                             }
                    588:                             my ($feeds,$syllabus_time);
1.283     raeburn   589:                             &Apache::lonrss::advertisefeeds($cnum,$cdom,undef,\$feeds);
1.284     raeburn   590:                             &Apache::lonnet::appenv({'request.course.feeds' => $feeds});
1.289     raeburn   591:                             &Apache::lonnet::get_numsuppfiles($cnum,$cdom,1);
1.284     raeburn   592:                             unless ($env{'course.'.$cdom.'_'.$cnum.'.updatedsyllabus'}) {
                    593:                                 unless (($env{'course.'.$cdom.'_'.$cnum.'.externalsyllabus'}) ||
                    594:                                         ($env{'course.'.$cdom.'_'.$cnum.'.uploadedsyllabus'})) {
                    595:                                     my %syllabus=&Apache::lonnet::dump('syllabus',$cdom,$cnum);
                    596:                                     $syllabus_time = $syllabus{'uploaded.lastmodified'};
                    597:                                     if ($syllabus_time) {
                    598:                                         &Apache::lonnet::appenv({'request.course.syllabustime' => $syllabus_time});
                    599:                                     }
                    600:                                 }
                    601:                             }
1.275     raeburn   602:                         }
1.118     albertel  603: 			if (($env{'form.orgurl'}) && 
1.292     raeburn   604: 			    ($env{'form.orgurl'}!~/^\/adm\/flip/) &&
                    605: 			    ($env{'form.orgurl'} ne '/adm/roles')) {
1.118     albertel  606: 			    my $dest=$env{'form.orgurl'};
1.219     raeburn   607:                             if ($env{'form.symb'}) {
                    608:                                 if ($dest =~ /\?/) {
                    609:                                     $dest .= '&';
                    610:                                 } else {
1.292     raeburn   611:                                     $dest .= '?';
1.219     raeburn   612:                                 }
                    613:                                 $dest .= 'symb='.$env{'form.symb'};
                    614:                             }
1.117     albertel  615: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   616: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.150     www       617:                             if (($ferr) && ($tadv)) {
                    618: 				&error_page($r,$ferr,$dest);
                    619: 			    } else {
1.255     raeburn   620:                                 if ($dest =~ m{^/adm/coursedocs\?folderpath}) {
                    621:                                     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) { 
                    622:                                         my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
1.268     raeburn   623:                                         &Apache::loncommon::update_content_constraints($cdom,$cnum,$chome,
                    624:                                                                                        $cdom.'_'.$cnum);
1.255     raeburn   625:                                     }
                    626:                                 }
1.150     www       627: 				$r->internal_redirect($dest);
                    628: 			    }
1.55      albertel  629: 			    return OK;
                    630: 			} else {
1.155     albertel  631: 			    if (!$env{'request.course.id'}) {
1.55      albertel  632: 				&Apache::lonnet::appenv(
1.186     raeburn   633: 				      {"request.course.id"  => $cdom.'_'.$cnum});
1.61      www       634: 				$furl='/adm/roles?tryagain=1';
1.221     bisitz    635:                 $msg='<p><span class="LC_error">'
                    636:                     .&mt('Could not initialize [_1] at this time.',
                    637:                          $env{'course.'.$cdom.'_'.$cnum.'.description'})
                    638:                     .'</span></p>'
                    639:                     .'<p>'.&mt('Please try again.').'</p>'
                    640:                     .'<p>'.$ferr.'</p>';
1.55      albertel  641: 			    }
1.117     albertel  642: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   643: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.152     raeburn   644: 
1.150     www       645: 			    if (($ferr) && ($tadv)) {
                    646: 				&error_page($r,$ferr,$furl);
                    647: 			    } else {
                    648: 				# Check to see if the user is a CC entering a course 
                    649: 				# for the first time
1.240     raeburn   650: 				if ((($role eq 'cc') || ($role eq 'co')) 
1.297     raeburn   651:                                     && ($env{'course.'.$cdom.'_'.$cnum.'.course.helper.not.run'})) { 
1.150     www       652: 				    $furl = "/adm/helper/course.initialization.helper";
                    653: 				    # Send the user to the course they selected
                    654: 				} elsif ($env{'request.course.id'}) {
1.313     raeburn   655:                                     if ((&Apache::loncommon::course_type() eq 'Placement') && 
                    656:                                         (!$env{'request.role.adv'})) {
                    657:                                         my ($score,$incomplete) = 
                    658:                                             &Apache::lonplacementtest::check_completion(undef,undef,1);
                    659:                                         if (($incomplete) && ($incomplete < 100)) {
                    660:                                             &redirect_user($r, &mt('Entering [_1]',
                    661:                                                           $env{'course.'.$cdom.'_'.$cnum.'.description'}),
                    662:                                                           '/adm/placement', $msg);
                    663:                                             return OK;
                    664:                                         }
                    665:                                     }
1.276     raeburn   666:                                     my ($dest,$destsymb,$checkenc);
                    667:                                     $dest = $env{'form.destinationurl'};
                    668:                                     $destsymb = $env{'form.destsymb'};
                    669:                                     if ($dest ne '') {
                    670:                                         if ($env{'form.switchrole'}) {
                    671:                                             if ($destsymb ne '') {
                    672:                                                 if ($destsymb !~ m{^/enc/}) {
                    673:                                                     unless ($env{'request.role.adv'}) {
                    674:                                                         $checkenc = 1;
                    675:                                                     }
                    676:                                                 }
                    677:                                             }
                    678:                                             if ($dest =~ m{^/enc/}) {
                    679:                                                 if ($env{'request.role.adv'}) {
                    680:                                                     $dest = &Apache::lonenc::unencrypted($dest);
                    681:                                                     if ($destsymb eq '') {
1.277     raeburn   682:                                                         ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]*)/);
1.276     raeburn   683:                                                         $destsymb = &unescape($destsymb);
                    684:                                                     }
                    685:                                                 }
                    686:                                             } else {
                    687:                                                 if ($destsymb eq '') {
1.280     raeburn   688:                                                     ($destsymb) = ($dest =~ /(?:\?|\&)symb=([^\&]+)/);
1.276     raeburn   689:                                                     $destsymb = &unescape($destsymb);
                    690:                                                 }
                    691:                                                 unless ($env{'request.role.adv'}) {
                    692:                                                     $checkenc = 1;
                    693:                                                 }
                    694:                                             }
                    695:                                             if (($checkenc) && ($destsymb ne '')) {
                    696:                                                 my ($encstate,$unencsymb,$res);
1.281     raeburn   697:                                                 $unencsymb = &Apache::lonnet::symbclean($destsymb);
1.276     raeburn   698:                                                 (undef,undef,$res) = &Apache::lonnet::decode_symb($unencsymb);
                    699:                                                 &Apache::lonnet::symbverify($unencsymb,$res,\$encstate);
                    700:                                                 if ($encstate) {
                    701:                                                     if (($dest ne '') && ($dest !~ m{^/enc/})) {
                    702:                                                         $dest=&Apache::lonenc::encrypted($dest);
                    703:                                                     }
                    704:                                                 }
                    705:                                             }
                    706:                                         }
1.277     raeburn   707:                                         unless (($dest =~ m{^/enc/}) || ($dest =~ /(\?|\&)symb=.+___\d+___.+/)) {
1.276     raeburn   708:                                             if (($destsymb ne '') && ($destsymb !~ m{^/enc/})) {
                    709:                                                 my $esc_symb = &escape($destsymb);
                    710:                                                 $dest .= '?symb='.$esc_symb;
                    711:                                             }
1.203     raeburn   712:                                         }
1.245     droeschl  713:                                         &redirect_user($r, &mt('Entering [_1]',
1.297     raeburn   714:                                                        $env{'course.'.$cdom.'_'.$cnum.'.description'}),
1.245     droeschl  715:                                                        $dest, $msg);
1.185     raeburn   716:                                         return OK;
                    717:                                     }
1.150     www       718: 				    if (&Apache::lonnet::allowed('whn',
                    719: 								 $env{'request.course.id'})
                    720: 					|| &Apache::lonnet::allowed('whn',
                    721: 								    $env{'request.course.id'}.'/'
                    722: 								    .$env{'request.course.sec'})
                    723: 					) {
1.297     raeburn   724: 					my $startpage = &courseloadpage($env{'request.course.id'});
1.150     www       725: 					unless ($startpage eq 'firstres') {         
1.204     bisitz    726: 					    $msg = &mt('Entering [_1] ...',
1.297     raeburn   727: 						       $env{'course.'.$env{'request.course.id'}.'.description'});
1.245     droeschl  728: 					    &redirect_user($r, &mt('New in course'),
                    729:                                        '/adm/whatsnew?refpage=start', $msg);
1.150     www       730: 					    return OK;
                    731: 					}
                    732: 				    }
                    733: 				}
1.300     raeburn   734:                                 # Are we allowed to look at the first resource?
1.311     raeburn   735:                                 my $access;
1.299     musolffc  736:                                 if ($furl =~ m{^(/adm/wrapper|)/ext/}) {
1.300     raeburn   737:                                     # If it's an external resource,
1.299     musolffc  738:                                     # strip off the symb argument and possible query
                    739:                                     my ($exturl,$symb) = ($furl =~ m{^(.+)(?:\?|\&)symb=(.+)$});
                    740:                                     # Unencode $symb
                    741:                                     $symb = &unescape($symb);
                    742:                                     # Then check for permission
1.311     raeburn   743:                                     $access = &Apache::lonnet::allowed('bre',$exturl,$symb);
1.300     raeburn   744:                                 # For other resources just check for permission
1.311     raeburn   745:                                 } else {
                    746:                                     $access = &Apache::lonnet::allowed('bre',$furl);
                    747:                                 }
                    748:                                 if (!$access) {
1.299     musolffc  749:                                     $furl = &Apache::lonpageflip::first_accessible_resource();
1.311     raeburn   750:                                 } elsif ($access eq 'B') {
                    751:                                     $furl = '/adm/navmaps?showOnlyHomework=1';
1.299     musolffc  752:                                 }
1.162     albertel  753:                                 $msg = &mt('Entering [_1] ...',
1.297     raeburn   754: 					   $env{'course.'.$cdom.'_'.$cnum.'.description'});
1.245     droeschl  755: 				&redirect_user($r, &mt('Entering [_1]',
1.297     raeburn   756:                                $env{'course.'.$cdom.'_'.$cnum.'.description'}),
1.245     droeschl  757:                                $furl, $msg);
1.58      bowersj2  758: 			    }
1.124     albertel  759: 			    return OK;
1.55      albertel  760: 			}
                    761: 		    }
1.62      matthew   762:                     #
                    763:                     # Send the user to the construction space they selected
1.125     www       764:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew   765:                         my $redirect_url = '/priv/';
                    766:                         if ($role eq 'au') {
1.262     www       767:                             $redirect_url.=$env{'user.domain'}.'/'.$env{'user.name'};
1.62      matthew   768:                         } else {
1.263     www       769:                             $redirect_url .= $where;
1.62      matthew   770:                         }
                    771:                         $redirect_url .= '/';
1.288     raeburn   772:                         &redirect_user($r,&mt('Entering Authoring Space'),
1.62      matthew   773:                                        $redirect_url);
                    774:                         return OK;
                    775:                     }
1.104     raeburn   776:                     if ($role eq 'dc') {
1.108     raeburn   777:                         my $redirect_url = '/adm/menu/';
                    778:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn   779:                                        $redirect_url);
1.108     raeburn   780:                         return OK;
1.104     raeburn   781:                     }
1.315     raeburn   782:                     if ($role eq 'dh') {
                    783:                         my $redirect_url = '/adm/menu/';
                    784:                         &redirect_user($r,&mt('Loading Domain Helpdesk Menu'),
                    785:                                        $redirect_url);
                    786:                         return OK;
                    787:                     }
1.220     raeburn   788:                     if ($role eq 'sc') {
                    789:                         my $redirect_url = '/adm/grades?command=scantronupload';
                    790:                         &redirect_user($r,&mt('Loading Data Upload Page'),
                    791:                                        $redirect_url);
                    792:                         return OK;
                    793:                     }
1.55      albertel  794: 		}
                    795:             }
1.6       www       796:         }
1.40      matthew   797:     }
1.44      www       798: 
1.10      www       799: 
1.6       www       800: # =============================================================== No Roles Init
1.10      www       801: 
1.73      www       802:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  803:     &Apache::loncommon::no_cache($r);
1.10      www       804:     $r->send_http_header;
                    805:     return OK if $r->header_only;
                    806: 
1.224     raeburn   807:     my $crumbtext = 'User Roles';
                    808:     my $pagetitle = 'My Roles';
                    809:     my $recent = &mt('Recent Roles');
1.287     raeburn   810:     my $standby = &mt('Role selected. Please stand by.');
1.224     raeburn   811:     my $show_course=&Apache::loncommon::show_course();
                    812:     if ($show_course) {
                    813:         $crumbtext = 'Courses';
                    814:         $pagetitle = 'My Courses';
                    815:         $recent = &mt('Recent Courses');
1.287     raeburn   816:         $standby = &mt('Course selected. Please stand by.'); 
1.224     raeburn   817:     }
                    818:     my $brcrum =[{href=>"/adm/roles",text=>$crumbtext}];
1.274     raeburn   819: 
                    820:     my %roles_in_env;
                    821:     my $showcount = &roles_from_env(\%roles_in_env,$update); 
                    822: 
1.52      www       823:     my $swinfo=&Apache::lonmenu::rawconfig();
1.302     raeburn   824:     my %domdefs=&Apache::lonnet::get_domain_defaults($env{'user.domain'}); 
                    825:     my $cattype = 'std';
                    826:     if ($domdefs{'catauth'}) {
                    827:         $cattype = $domdefs{'catauth'};
                    828:     }
1.313     raeburn   829:     my $placementonly;
                    830:     if ($showcount == 1) {
                    831:         if ($env{'request.course.id'}) {
                    832:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                    833:                 $placementonly = 1;
                    834:             }
                    835:         } else {
                    836:             foreach my $rolecode (keys(%roles_in_env)) {
                    837:                 my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
                    838:                 if ($cid) {
                    839:                     my %coursedescription =
                    840:                         &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                    841:                     if ($coursedescription{'type'} eq 'Placement') {
                    842:                         $placementonly = 1;
                    843:                     }
                    844:                     last;
                    845:                 }
                    846:             }
                    847:         }
                    848:     }
                    849:     my ($start_page,$funcs);
                    850:     if ($placementonly) {
                    851:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,
                    852:                                                   {bread_crumbs=>$brcrum,crstype=>'Placement'});
                    853:     } else {
                    854:         $funcs = &get_roles_functions($showcount,$cattype);
1.314     raeburn   855:         my $crumbsright;
                    856:         if ($env{'browser.mobile'}) {
                    857:             $crumbsright = $funcs;
                    858:             undef($funcs);
                    859:         }
                    860:         $start_page=&Apache::loncommon::start_page($pagetitle,undef,{bread_crumbs=>$brcrum,
                    861:                                                                      bread_crumbs_component=>$crumbsright});
1.313     raeburn   862:     }
1.312     damieng   863:     &js_escape(\$standby);
1.274     raeburn   864:     my $noscript='<br /><span class="LC_error">'.&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.').'<br />'.&mt('As this is not the case, most functionality in the system will be unavailable.').'</span><br />';
1.163     www       865: 
1.10      www       866:     $r->print(<<ENDHEADER);
1.147     albertel  867: $start_page
1.274     raeburn   868: $funcs
1.179     raeburn   869: <noscript>
                    870: $noscript
                    871: </noscript>
                    872: <script type="text/javascript">
1.225     bisitz    873: // <![CDATA[
1.26      www       874: $swinfo
                    875: window.focus();
1.134     www       876: 
                    877: active=true;
                    878: 
                    879: function enterrole (thisform,rolecode,buttonname) {
                    880:     if (active) {
                    881: 	active=false;
                    882:         document.title='$standby';
                    883:         window.status='$standby';
                    884: 	thisform.newrole.value=rolecode;
                    885: 	thisform.submit();
                    886:     } else {
                    887:        alert('$standby');
1.260     raeburn   888:     }
                    889: }
                    890: 
1.274     raeburn   891: function rolesView (caller) {
                    892:     if ((caller == 'showall') || (caller == 'noshowall')) {
                    893:         document.rolechoice.display.value = caller;
                    894:     } else {
                    895:         if ((caller == 'doupdate') || (caller == 'requestauthor') ||
                    896:             (caller == 'queued')) { 
                    897:             document.rolechoice.state.value = caller;
                    898:         }
                    899:     }
                    900:     document.rolechoice.selectrole.value='';
                    901:     document.rolechoice.submit();
1.270     raeburn   902: }
                    903: 
1.225     bisitz    904: // ]]>
1.26      www       905: </script>
1.10      www       906: ENDHEADER
1.6       www       907: 
1.2       www       908: # ------------------------------------------ Get Error Message from Environment
                    909: 
1.118     albertel  910:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                    911:     if ($env{'user.error.msg'}) {
1.55      albertel  912: 	$r->log_reason(
1.118     albertel  913:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www       914:     }
1.1       harris41  915: 
1.61      www       916: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       917: 
1.118     albertel  918:     my $advanced=$env{'user.adv'};
1.61      www       919:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel  920:     my $tryagain=$env{'form.tryagain'};
1.209     raeburn   921:     my $reinit=$env{'user.reinit'};
                    922:     delete $env{'user.reinit'};
1.6       www       923: 
1.2       www       924: # -------------------------------------------------------- Generate Page Output
1.6       www       925: # --------------------------------------------------------------- Error Header?
1.2       www       926:     if ($error) {
1.187     bisitz    927:         $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
1.174     albertel  928: 	$r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
                    929: 	if ($priv ne '') {
1.187     bisitz    930:             $r->print(&mt('Access  : ').&Apache::lonnet::plaintext($priv)."\n");
1.174     albertel  931: 	}
                    932: 	if ($fn ne '') {
1.187     bisitz    933:             $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
1.174     albertel  934: 	}
                    935: 	if ($msg ne '') {
1.187     bisitz    936:             $r->print(&mt('Action  : ').$msg."\n");
1.174     albertel  937: 	}
                    938: 	$r->print("</pre><hr />");
1.120     albertel  939: 	my $url=$fn;
                    940: 	my $last;
                    941: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                    942: 		&GDBM_READER(),0640)) {
                    943: 	    $last=$hash{'last_known'};
                    944: 	    untie(%hash);
                    945: 	}
1.149     www       946: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel  947: 
                    948: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                    949: 					&Apache::lonenc::check_encrypt($fn));
1.2       www       950:     } else {
1.118     albertel  951:         if ($env{'user.error.msg'}) {
1.209     raeburn   952:             if ($reinit) {
                    953:                 $r->print(
                    954:  '<h3><span class="LC_error">'.
1.234     raeburn   955:  &mt('As your session file for the course or community has expired, you will need to re-select it.').'</span></h3>');
1.209     raeburn   956:             } else {
                    957: 	        $r->print(
1.157     albertel  958:  '<h3><span class="LC_error">'.
1.235     bisitz    959:  &mt('You need to choose another user role or enter a specific course or community for this function.').
                    960:  '</span></h3>');
1.209     raeburn   961: 	    }
                    962:         }
1.2       www       963:     }
                    964:     if ($nochoose) {
1.177     www       965: 	$r->print("<h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
                    966: 		  &mt('This action is currently not authorized.').'</span>'.
1.150     www       967: 		  &Apache::loncommon::end_page());
                    968: 	return OK;
1.6       www       969:     } else {
1.274     raeburn   970:         if ($updateresult || $reqauthor || $hotlist) {
                    971:             my $showresult = '<div>';
                    972:             if ($updateresult) {
                    973:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($updateresult);
                    974:             }
                    975:             if ($reqauthor) {
                    976:                 $showresult .= &Apache::lonhtmlcommon::confirm_success($reqauthor);
                    977:             }
                    978:             if ($hotlist) {
                    979:                 $showresult .= $hotlist;
                    980:             } 
                    981:             $showresult .= '</div>';
                    982:             $r->print($showresult);
                    983:         } elsif ($env{'form.state'} eq 'queued') {
                    984:             $r->print(&get_queued());
1.270     raeburn   985:         }
1.18      www       986:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    987:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       988:         }
1.274     raeburn   989:         my $display = ($env{'form.display'} =~ /^(showall)$/);
1.84      www       990:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel  991:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                    992:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www       993:         $r->print('<input type="hidden" name="newrole" value="" />');
1.274     raeburn   994:         $r->print('<input type="hidden" name="display" value="'.$display.'" />');
                    995:         $r->print('<input type="hidden" name="state" value="" />');
1.6       www       996:     }
1.259     raeburn   997:     $r->rflush();
1.226     raeburn   998: 
                    999:     my (%roletext,%sortrole,%roleclass,%futureroles,%timezones);
                   1000:     my ($countactive,$countfuture,$inrole,$possiblerole) = 
1.274     raeburn  1001:         &gather_roles($update,$refresh,$now,$reinit,$nochoose,\%roles_in_env,\%roletext,
                   1002:                       \%sortrole,\%roleclass,\%futureroles,\%timezones,$loncaparev);
1.226     raeburn  1003:     $refresh = $now;
                   1004:     &Apache::lonnet::appenv({'user.refresh.time'  => $refresh});
1.313     raeburn  1005:     if ($countactive == 1) {
                   1006:         if ($env{'request.course.id'}) {
                   1007:             if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
                   1008:                 $placementonly = 1;
                   1009:             }
                   1010:         } elsif ($possiblerole) {
                   1011:             if ($possiblerole =~ m{^st\./($match_domain)/($match_courseid)(?:/|$)}) {
                   1012:                 if ($env{'course.'.$1.'_'.$2.'.type'} eq 'Placement') {
                   1013:                     $placementonly = 1;
                   1014:                 }
                   1015:             }
                   1016:         }
                   1017:     }
                   1018:     if ((($cattype eq 'std') || ($cattype eq 'domonly')) && (!$env{'user.adv'}) &&
                   1019:           (!$placementonly)) {
1.196     raeburn  1020:         if ($countactive > 0) {
                   1021:             my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn  1022:             my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&'); 
1.233     bisitz   1023:             $r->print(
                   1024:                 '<p>'
1.295     bisitz   1025:                .&mt('[_1]Visit the [_2]Course/Community Catalog[_3][_4]'
                   1026:                    .' to view all [_5] LON-CAPA courses and communities.'
1.233     bisitz   1027:                    ,'<b>'
                   1028:                    ,'<a href="/adm/coursecatalog?showdom='.$esc_dom.'">'
1.295     bisitz   1029:                    ,'</a>'
                   1030:                    ,'</b>'
                   1031:                    ,'"'.$domdesc.'"')
1.233     bisitz   1032:                .'<br />'
1.235     bisitz   1033:                .&mt('If a course or community is [_1]not[_2] in your list of current courses and communities below,'
1.233     bisitz   1034:                    .' you may be able to enroll if self-enrollment is permitted.'
                   1035:                    ,'<b>','</b>')
                   1036:                .'</p>'
                   1037:             );
1.196     raeburn  1038:         }
                   1039:     }
                   1040: 
1.84      www      1041: # No active roles
                   1042:     if ($countactive==0) {
1.306     raeburn  1043:         &requestcourse_advice($r,$cattype,$inrole); 
1.191     raeburn  1044: 	$r->print('</form>');
                   1045:         if ($countfuture) {
                   1046:             $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
                   1047:             my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
                   1048:                                                $nochoose);
                   1049:             &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
                   1050:                             \%roletext);
                   1051:             my $tremark='';
1.212     bisitz   1052:             my $tbg;
1.191     raeburn  1053:             if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1054:                 $tbg="LC_roles_selected";
1.204     bisitz   1055:                 $tremark=&mt('Currently selected.').' ';
1.191     raeburn  1056:             } else {
1.212     bisitz   1057:                 $tbg="LC_roles_is";
1.191     raeburn  1058:             }
1.212     bisitz   1059:             $r->print(&Apache::loncommon::start_data_table_row()
                   1060:                      .'<td class="'.$tbg.'">&nbsp;</td>'
                   1061:                      .'<td colspan="3">'
                   1062:                      .&mt('No role specified')
                   1063:                      .'</td>'
                   1064:                      .'<td>'.$tremark.'&nbsp;</td>'
                   1065:                      .&Apache::loncommon::end_data_table_row()
                   1066:             );
1.191     raeburn  1067: 
1.212     bisitz   1068:             $r->print(&Apache::loncommon::end_data_table());
1.191     raeburn  1069:         }
                   1070:         $r->print(&Apache::loncommon::end_page());
1.84      www      1071: 	return OK;
1.313     raeburn  1072:     } elsif (($placementonly) && ($env{'request.role'} eq 'cm')) {
                   1073: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>
                   1074: 	          <input type="hidden" name="'.$possiblerole.'" value="1" />
                   1075:                   <noscript><br />
                   1076:                   <input type="submit" name="submit" value="'.&mt('Continue').'" />
                   1077:                   </noscript></form>');
                   1078: 	$r->rflush();
                   1079: 	$r->print('<script type="text/javascript">document.forms.rolechoice.submit();</script>');
                   1080: 	$r->print(&Apache::loncommon::end_page());
                   1081: 	return OK;
1.84      www      1082:     }
                   1083: # ----------------------------------------------------------------------- Table
1.247     raeburn  1084: 
1.317     raeburn  1085:     if (($numdc > 0) || (($numdh > 0) && ($numadhoc > 0))) {
1.247     raeburn  1086:         $r->print(&coursepick_jscript());
                   1087:         $r->print(&Apache::loncommon::coursebrowser_javascript().
                   1088:                   &Apache::loncommon::authorbrowser_javascript());
                   1089:     }
                   1090: 
1.224     raeburn  1091:     unless ((!&Apache::loncommon::show_course()) || ($nochoose) || ($countactive==1)) {
1.173     albertel 1092: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
1.84      www      1093:     }
1.229     raeburn  1094:     if ($env{'form.destinationurl'}) {
                   1095:         $r->print('<input type="hidden" name="destinationurl" value="'.
                   1096:                   $env{'form.destinationurl'}.'" />');
                   1097:         if ($env{'form.destsymb'} ne '') {
                   1098:             $r->print('<input type="hidden" name="destsymb" value="'.
                   1099:                       $env{'form.destsymb'}.'" />');
                   1100:         }
                   1101:     }
1.247     raeburn  1102: 
1.191     raeburn  1103:     my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
1.118     albertel 1104:     if ($env{'environment.recentroles'}) {
1.111     albertel 1105:         my %recent_roles =
1.118     albertel 1106:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel 1107: 	my $output='';
1.247     raeburn  1108: 	foreach my $role (sort(keys(%recent_roles))) {
                   1109: 	    if (ref($roletext{'user.role.'.$role}) eq 'ARRAY') {
1.223     raeburn  1110: 		$output.= &Apache::loncommon::start_data_table_row().
1.247     raeburn  1111:                           $roletext{'user.role.'.$role}->[0].
1.223     raeburn  1112:                           &Apache::loncommon::end_data_table_row();
1.249     raeburn  1113:                 if ($roletext{'user.role.'.$role}->[1] ne '') {
                   1114:                     $output .= &Apache::loncommon::continue_data_table_row().
                   1115:                                $roletext{'user.role.'.$role}->[1].
                   1116:                                &Apache::loncommon::end_data_table_row();
                   1117:                 }
1.318     raeburn  1118:                 if ($role =~ m{^dc\./($match_domain)/$} 
1.170     albertel 1119: 		    && $dcroles{$1}) {
1.192     raeburn  1120: 		    $output .= &adhoc_roles_row($1,'recent');
1.318     raeburn  1121:                 } elsif ($role =~ m{^dh\./($match_domain)/$}
1.317     raeburn  1122:                          && ($env{'environment.adhocroles.'.$1} ne '')) {
                   1123:                     $output .= &adhoc_customroles_row($1,'recent');
1.133     albertel 1124:                 }
1.113     raeburn  1125: 	    } elsif ($numdc > 0) {
1.247     raeburn  1126:                 unless ($role =~/^error\:/) {
1.249     raeburn  1127:                     my ($roletext,$role_text_end) = &display_cc_role('user.role.'.$role);
1.259     raeburn  1128:                     if ($roletext) {
                   1129:                         $output.= &Apache::loncommon::start_data_table_row().
                   1130:                                   $roletext.
                   1131:                                   &Apache::loncommon::end_data_table_row();
                   1132:                         if ($role_text_end) {
                   1133:                             $output .= &Apache::loncommon::continue_data_table_row().
                   1134:                                        $role_text_end.
                   1135:                                        &Apache::loncommon::end_data_table_row();
                   1136:                         }
                   1137:                     }
1.113     raeburn  1138:                 }
1.247     raeburn  1139:             }
1.111     albertel 1140: 	}
                   1141: 	if ($output) {
1.212     bisitz   1142: 	    $r->print(&Apache::loncommon::start_data_table_empty_row()
                   1143:                      .'<td align="center" colspan="5">'
1.224     raeburn  1144:                      .$recent
1.212     bisitz   1145:                      .'</td>'
                   1146:                      .&Apache::loncommon::end_data_table_empty_row()
                   1147:             );
1.111     albertel 1148: 	    $r->print($output);
1.114     raeburn  1149:             $doheaders ++;
1.111     albertel 1150: 	}
                   1151:     }
1.191     raeburn  1152:     &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
1.202     raeburn  1153:     if ($countactive > 1) {
                   1154:         my $tremark='';
1.212     bisitz   1155:         my $tbg;
1.202     raeburn  1156:         if ($env{'request.role'} eq 'cm') {
1.212     bisitz   1157:             $tbg="LC_roles_selected";
1.204     bisitz   1158:             $tremark=&mt('Currently selected.').' ';
1.202     raeburn  1159:         } else {
1.212     bisitz   1160:                 $tbg="LC_roles_is";
1.202     raeburn  1161:         }
1.212     bisitz   1162:         $r->print(&Apache::loncommon::start_data_table_row());
1.202     raeburn  1163:         unless ($nochoose) {
                   1164: 	    if ($env{'request.role'} ne 'cm') {
1.212     bisitz   1165: 	        $r->print('<td class="'.$tbg.'"><input type="submit" value="'.
1.202     raeburn  1166: 		          &mt('Select').'" name="cm" /></td>');
                   1167: 	    } else {
1.212     bisitz   1168: 	        $r->print('<td class="'.$tbg.'">&nbsp;</td>');
1.202     raeburn  1169: 	    }
                   1170:         }
1.212     bisitz   1171:         $r->print('<td colspan="3">'
                   1172:                  .&mt('No role specified')
                   1173:                  .'</td>'
                   1174:                  .'<td>'.$tremark.'&nbsp;</td>'
                   1175:                  .&Apache::loncommon::end_data_table_row()
                   1176:         );
1.202     raeburn  1177:     } 
1.212     bisitz   1178:     $r->print(&Apache::loncommon::end_data_table());
1.4       www      1179:     unless ($nochoose) {
                   1180: 	$r->print("</form>\n");
                   1181:     }
1.22      harris41 1182: # ------------------------------------------------------------ Privileges Info
1.118     albertel 1183:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.212     bisitz   1184: 	$r->print('<hr /><h2>'.&mt('Current Privileges').'</h2>');
1.175     albertel 1185: 	$r->print(&privileges_info());
1.4       www      1186:     }
1.267     bisitz   1187:     my $announcements = &Apache::lonnet::getannounce();
                   1188:     $r->print(
                   1189:         '<br />'.
                   1190:         '<h2>'.&mt('Announcements').'</h2>'.
                   1191:         $announcements
                   1192:     ) unless (!$announcements);
1.65      www      1193:     if ($advanced) {
1.201     raeburn  1194:         my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.231     bisitz   1195:         $r->print('<p><small><i>'
                   1196:                  .&mt('This LON-CAPA server is version [_1]',$r->dir_config('lonVersion'))
1.308     raeburn  1197:                  .'</i></small></p>');
1.65      www      1198:     }
1.147     albertel 1199:     $r->print(&Apache::loncommon::end_page());
1.1       harris41 1200:     return OK;
1.102     raeburn  1201: }
                   1202: 
1.274     raeburn  1203: sub roles_from_env {
                   1204:     my ($roleshash,$update) = @_;
                   1205:     my $count = 0;
                   1206:     if (ref($roleshash) eq 'HASH') {
                   1207:         foreach my $envkey (keys(%env)) {
                   1208:             if ($envkey =~ m{^user\.role\.(\w+)[./]}) {
                   1209:                 next if ($1 eq 'gr');
                   1210:                 $roleshash->{$envkey} = $env{$envkey};
                   1211:                 my ($start,$end) = split(/\./,$env{$envkey});
                   1212:                 unless ($end && $end<$update) {
                   1213:                     $count ++;
                   1214:                 }
                   1215:             }
                   1216:         }
                   1217:     }
                   1218:     return $count;
                   1219: }
                   1220: 
1.226     raeburn  1221: sub gather_roles {
1.274     raeburn  1222:     my ($update,$refresh,$now,$reinit,$nochoose,$roles_in_env,$roletext,$sortrole,$roleclass,$futureroles,
                   1223:         $timezones,$loncaparev) = @_;
1.226     raeburn  1224:     my ($countactive,$countfuture,$inrole,$possiblerole) = (0,0,0,'');
                   1225:     my $advanced = $env{'user.adv'};
                   1226:     my $tryagain = $env{'form.tryagain'};
1.254     raeburn  1227:     my @ids = &Apache::lonnet::current_machine_ids();
1.274     raeburn  1228:     if (ref($roles_in_env) eq 'HASH') {
                   1229:         foreach my $envkey (sort(keys(%{$roles_in_env}))) {
                   1230:             my $button = 1;
                   1231:             my $switchserver='';
                   1232:             my $switchwarning;
                   1233:             my ($role_text,$role_text_end,$sortkey,$role,$where,$trolecode,$tstart,
                   1234:                 $tend,$tremark,$tstatus,$tpstart,$tpend);
1.260     raeburn  1235:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.226     raeburn  1236:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
                   1237:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
                   1238:             $tremark='';
                   1239:             $tpstart='&nbsp;';
                   1240:             $tpend='&nbsp;';
                   1241:             if ($env{'request.role'} eq $trolecode) {
                   1242:                 $tstatus='selected';
                   1243:             }
                   1244:             my $tbg;
                   1245:             if (($tstatus eq 'is')
                   1246:                 || ($tstatus eq 'selected')
                   1247:                 || ($tstatus eq 'future')
1.274     raeburn  1248:                 || ($env{'form.display'} eq 'showall')) {
1.259     raeburn  1249:                 my $timezone = &role_timezone($where,$timezones);
                   1250:                 if ($tstart) {
                   1251:                     $tpstart=&Apache::lonlocal::locallocaltime($tstart,$timezone);
                   1252:                 }
                   1253:                 if ($tend) {
                   1254:                     $tpend=&Apache::lonlocal::locallocaltime($tend,$timezone);
                   1255:                 }
1.226     raeburn  1256:                 if ($tstatus eq 'is') {
                   1257:                     $tbg='LC_roles_is';
                   1258:                     $possiblerole=$trolecode;
                   1259:                     $countactive++;
                   1260:                 } elsif ($tstatus eq 'future') {
                   1261:                     $tbg='LC_roles_future';
                   1262:                     $button=0;
                   1263:                     $futureroles->{$trolecode} = $tstart.':'.$tend;
                   1264:                     $countfuture ++;
                   1265:                 } elsif ($tstatus eq 'expired') {
                   1266:                     $tbg='LC_roles_expired';
                   1267:                     $button=0;
                   1268:                 } elsif ($tstatus eq 'will_not') {
                   1269:                     $tbg='LC_roles_will_not';
                   1270:                     $tremark.=&mt('Expired after logout.').' ';
                   1271:                 } elsif ($tstatus eq 'selected') {
                   1272:                     $tbg='LC_roles_selected';
                   1273:                     $inrole=1;
                   1274:                     $countactive++;
                   1275:                     $tremark.=&mt('Currently selected.').' ';
                   1276:                 }
                   1277:                 my $trole;
                   1278:                 if ($role =~ /^cr\//) {
                   1279:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.322   ! raeburn  1280:                     unless ($rauthor eq $rdomain.'-domainconfig') {
        !          1281:                         if ($tremark) { $tremark.='<br />'; }
        !          1282:                         $tremark.=&mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
        !          1283:                     }
1.226     raeburn  1284:                 }
                   1285:                 $trole=Apache::lonnet::plaintext($role);
                   1286:                 my $ttype;
                   1287:                 my $twhere;
1.313     raeburn  1288:                 my $skipcal;
1.226     raeburn  1289:                 my ($tdom,$trest,$tsection)=
                   1290:                     split(/\//,Apache::lonnet::declutter($where));
                   1291:                 # First, Co-Authorship roles
                   1292:                 if (($role eq 'ca') || ($role eq 'aa')) {
                   1293:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   1294:                     my $allowed=0;
                   1295:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1296:                     if (!$allowed) {
                   1297:                         $button=0;
1.248     raeburn  1298:                         $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
1.226     raeburn  1299:                     }
                   1300:                     #next if ($home eq 'no_host');
                   1301:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1302:                     $ttype='Authoring Space';
1.226     raeburn  1303:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                   1304:                         ': '.$tdom.'<br />'.
                   1305:                         ' '.&mt('Server').':&nbsp;'.$home;
                   1306:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                   1307:                     $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
                   1308:                     $sortkey=$role."$trest:$tdom";
                   1309:                 } elsif ($role eq 'au') {
                   1310:                     # Authors
                   1311:                     my $home = &Apache::lonnet::homeserver
                   1312:                         ($env{'user.name'},$env{'user.domain'});
                   1313:                     my $allowed=0;
                   1314:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   1315:                     if (!$allowed) {
                   1316:                         $button=0;
1.248     raeburn  1317:                         $switchserver='otherserver='.$home.'&amp;role='.$trolecode;
1.226     raeburn  1318:                     }
                   1319:                     #next if ($home eq 'no_host');
                   1320:                     $home = &Apache::lonnet::hostname($home);
1.288     raeburn  1321:                     $ttype='Authoring Space';
1.226     raeburn  1322:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                   1323:                         ':&nbsp;'.$home;
                   1324:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                   1325:                     $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
                   1326:                     $sortkey=$role;
                   1327:                 } elsif ($trest) {
                   1328:                     my $tcourseid=$tdom.'_'.$trest;
                   1329:                     $ttype = &Apache::loncommon::course_type($tcourseid);
1.322   ! raeburn  1330:                     if ($role !~ /^cr/) {
        !          1331:                         $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
        !          1332:                     } elsif ($role =~ m{^cr/($match_domain)/\1-domainconfig/(\w+)$}) {
        !          1333:                         $trole = &mt('Helpdesk[_1]','&nbsp;'.$2);
        !          1334:                     } else {
        !          1335:                         $trole = (split(/\//,$role,4))[-1];
        !          1336:                     }
1.226     raeburn  1337:                     if ($env{'course.'.$tcourseid.'.description'}) {
1.254     raeburn  1338:                         my $home=$env{'course.'.$tcourseid.'.home'};
1.226     raeburn  1339:                         $twhere=$env{'course.'.$tcourseid.'.description'};
                   1340:                         $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.248     raeburn  1341:                         $twhere = &HTML::Entities::encode($twhere,'"<>&');
1.226     raeburn  1342:                         unless ($twhere eq &mt('Currently not available')) {
                   1343:                             $twhere.=' <span class="LC_fontsize_small">'.
                   1344:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1345:                                     '</span>';
1.254     raeburn  1346:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1347:                                 my $required = $env{'course.'.$tcourseid.'.internal.releaserequired'};
1.259     raeburn  1348:                                 if ($required ne '') {
                   1349:                                     ($switchserver,$switchwarning) = 
1.310     raeburn  1350:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1351:                                     if ($switchserver || $switchwarning) {
                   1352:                                         $button = 0;
                   1353:                                     }
1.254     raeburn  1354:                                 }
                   1355:                             }
1.226     raeburn  1356:                         }
                   1357:                     } else {
                   1358:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   1359:                         if (%newhash) {
                   1360:                             $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
                   1361:                                 "\0".$envkey;
1.248     raeburn  1362:                             $twhere=&HTML::Entities::encode($newhash{'description'},'"<>&').
                   1363:                                     ' <span class="LC_fontsize_small">'.
                   1364:                                      &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
                   1365:                                     '</span>';
1.226     raeburn  1366:                             $ttype = $newhash{'type'};
1.243     raeburn  1367:                             $trole = &Apache::lonnet::plaintext($role,$ttype,$tcourseid);
1.254     raeburn  1368:                             my $home = $newhash{'home'};
                   1369:                             unless ($home && grep(/^\Q$home\E$/,@ids) && $loncaparev eq '') {
1.255     raeburn  1370:                                 my $required = $newhash{'internal.releaserequired'};
1.259     raeburn  1371:                                 if ($required ne '') {
                   1372:                                     ($switchserver,$switchwarning) =
1.310     raeburn  1373:                                         &Apache::loncommon::check_release_required($loncaparev,$tcourseid,$trolecode,$required);
1.259     raeburn  1374:                                     if ($switchserver || $switchwarning) {
                   1375:                                         $button = 0;
                   1376:                                     }
1.254     raeburn  1377:                                 }
                   1378:                             }
1.226     raeburn  1379:                         } else {
                   1380:                             $twhere=&mt('Currently not available');
                   1381:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
                   1382:                             $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
                   1383:                             $ttype = 'Unavailable';
1.313     raeburn  1384:                             $skipcal = 1;
1.226     raeburn  1385:                         }
                   1386:                     }
1.313     raeburn  1387:                     if ($ttype eq 'Placement') {
                   1388:                         $ttype = 'Placement Test';
                   1389:                     }
1.226     raeburn  1390:                     if ($tsection) {
                   1391:                         $twhere.='<br />'.&mt('Section').': '.$tsection;
                   1392:                     }
                   1393:                     if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
                   1394:                 } elsif ($tdom) {
                   1395:                     $ttype='Domain';
                   1396:                     $twhere=$tdom;
                   1397:                     $sortkey=$role.$twhere;
                   1398:                 } else {
                   1399:                     $ttype='System';
                   1400:                     $twhere=&mt('system wide');
                   1401:                     $sortkey=$role.$twhere;
                   1402:                 }
                   1403:                 ($role_text,$role_text_end) =
                   1404:                     &build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,
                   1405:                                     $advanced,$tremark,$tbg,$trole,$twhere,$tpstart,
1.313     raeburn  1406:                                     $tpend,$nochoose,$button,$switchserver,$reinit,
                   1407:                                     $switchwarning,$skipcal);
1.226     raeburn  1408:                 $roletext->{$envkey}=[$role_text,$role_text_end];
                   1409:                 if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                   1410:                 $sortrole->{$sortkey}=$envkey;
                   1411:                 $roleclass->{$envkey}=$ttype;
                   1412:             }
                   1413:         }
                   1414:     }
                   1415:     return ($countactive,$countfuture,$inrole,$possiblerole);
                   1416: }
                   1417: 
1.215     raeburn  1418: sub role_timezone {
                   1419:     my ($where,$timezones) = @_;
                   1420:     my $timezone;
                   1421:     if (ref($timezones) eq 'HASH') { 
                   1422:         if ($where =~ m{^/($match_domain)/($match_courseid)}) {
                   1423:             my $cdom = $1;
                   1424:             my $cnum = $2;
                   1425:             if ($cdom && $cnum) {
                   1426:                 if (!exists($timezones->{$cdom.'_'.$cnum})) {
1.259     raeburn  1427:                     my $tz;
                   1428:                     if ($env{'course.'.$cdom.'_'.$cnum.'.description'}) {
                   1429:                         $tz = $env{'course.'.$cdom.'_'.$cnum.'.timezone'};
                   1430:                     } else {
                   1431:                         my %timehash =
                   1432:                             &Apache::lonnet::get('environment',['timezone'],$cdom,$cnum);
                   1433:                         $tz = $timehash{'timezone'};
                   1434:                     }
                   1435:                     if ($tz eq '') {
1.215     raeburn  1436:                         if (!exists($timezones->{$cdom})) {
                   1437:                             my %domdefaults = 
                   1438:                                 &Apache::lonnet::get_domain_defaults($cdom);
                   1439:                             if ($domdefaults{'timezone_def'} eq '') {
                   1440:                                 $timezones->{$cdom} = 'local';
                   1441:                             } else {
                   1442:                                 $timezones->{$cdom} = $domdefaults{'timezone_def'};
                   1443:                             }
                   1444:                         }
                   1445:                         $timezones->{$cdom.'_'.$cnum} = $timezones->{$cdom};
                   1446:                     } else {
                   1447:                         $timezones->{$cdom.'_'.$cnum} = 
1.259     raeburn  1448:                             &Apache::lonlocal::gettimezone($tz);
1.215     raeburn  1449:                     }
                   1450:                 }
                   1451:                 $timezone = $timezones->{$cdom.'_'.$cnum};
                   1452:             }
                   1453:         } else {
                   1454:             my ($tdom) = ($where =~ m{^/($match_domain)});
                   1455:             if ($tdom) {
                   1456:                 if (!exists($timezones->{$tdom})) {
                   1457:                     my %domdefaults = &Apache::lonnet::get_domain_defaults($tdom);
                   1458:                     if ($domdefaults{'timezone_def'} eq '') {
                   1459:                         $timezones->{$tdom} = 'local';
                   1460:                     } else {
                   1461:                         $timezones->{$tdom} = $domdefaults{'timezone_def'};
                   1462:                     }
                   1463:                 }
                   1464:                 $timezone = $timezones->{$tdom};
                   1465:             }
                   1466:         }
                   1467:         if ($timezone eq 'local') {
                   1468:             $timezone = undef;
                   1469:         }
                   1470:     }
                   1471:     return $timezone;
                   1472: }
                   1473: 
1.191     raeburn  1474: sub roletable_headers {
                   1475:     my ($r,$roleclass,$sortrole,$nochoose) = @_;
                   1476:     my $doheaders;
                   1477:     if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
1.212     bisitz   1478:         $r->print('<br />'
1.314     raeburn  1479:                  .&Apache::loncommon::start_data_table('LC_textsize_mobile')
1.212     bisitz   1480:                  .&Apache::loncommon::start_data_table_header_row()
                   1481:         );
1.191     raeburn  1482:         if (!$nochoose) { $r->print('<th>&nbsp;</th>'); }
1.212     bisitz   1483:         $r->print('<th>'.&mt('User Role').'</th>'
                   1484:                  .'<th>'.&mt('Extent').'</th>'
                   1485:                  .'<th>'.&mt('Start').'</th>'
                   1486:                  .'<th>'.&mt('End').'</th>'
                   1487:                  .&Apache::loncommon::end_data_table_header_row()
                   1488:         );
1.191     raeburn  1489:         $doheaders=-1;
                   1490:         my @roletypes = &roletypes();
                   1491:         foreach my $type (@roletypes) {
                   1492:             my $haverole=0;
                   1493:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   1494:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   1495:                     $haverole=1;
                   1496:                 }
                   1497:             }
                   1498:             if ($haverole) { $doheaders++; }
                   1499:         }
                   1500:     }
                   1501:     return $doheaders;
                   1502: }
                   1503: 
                   1504: sub roletypes {
1.313     raeburn  1505:     my @types = ('Domain','Authoring Space','Course','Placement Test','Community','Unavailable','System');
1.191     raeburn  1506:     return @types; 
                   1507: }
                   1508: 
                   1509: sub print_rolerows {
                   1510:     my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
                   1511:     if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
                   1512:         my @types = &roletypes();
                   1513:         foreach my $type (@types) {
                   1514:             my $output;
                   1515:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                   1516:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                   1517:                     if (ref($roletext) eq 'HASH') {
1.223     raeburn  1518:                         if (ref($roletext->{$sortrole->{$which}}) eq 'ARRAY') {
                   1519:                             $output.= &Apache::loncommon::start_data_table_row().
                   1520:                                       $roletext->{$sortrole->{$which}}->[0].
                   1521:                                       &Apache::loncommon::end_data_table_row();
1.251     raeburn  1522:                             if ($roletext->{$sortrole->{$which}}->[1] ne '') {
                   1523:                                 $output .= &Apache::loncommon::continue_data_table_row().
                   1524:                                            $roletext->{$sortrole->{$which}}->[1].
                   1525:                                            &Apache::loncommon::end_data_table_row();
                   1526:                             }
1.223     raeburn  1527:                         }
1.317     raeburn  1528:                         if ($sortrole->{$which} =~ m{^user\.role\.dc\./($match_domain)/}) {
1.191     raeburn  1529:                             if (ref($dcroles) eq 'HASH') {
                   1530:                                 if ($dcroles->{$1}) {
1.192     raeburn  1531:                                     $output .= &adhoc_roles_row($1,'');
1.191     raeburn  1532:                                 }
                   1533:                             }
1.317     raeburn  1534:                         } elsif (($sortrole->{$which} =~ m{^user\.role\.dh\./($match_domain)/}) &&
                   1535:                                  ($env{'environment.adhocroles.'.$1} ne '')) {
                   1536:                             $output .= &adhoc_customroles_row($1,'');
1.191     raeburn  1537:                         }
                   1538:                     }
                   1539:                 }
                   1540:             }
                   1541:             if ($output) {
                   1542:                 if ($doheaders > 0) {
1.212     bisitz   1543:                     $r->print(&Apache::loncommon::start_data_table_empty_row()
                   1544:                              .'<td align="center" colspan="5">'
                   1545:                              .&mt($type)
                   1546:                              .'</td>'
                   1547:                              .&Apache::loncommon::end_data_table_empty_row()
                   1548:                     );
1.191     raeburn  1549:                 }
                   1550:                 $r->print($output);
                   1551:             }
                   1552:         }
                   1553:     }
                   1554: }
                   1555: 
                   1556: sub findcourse_advice {
1.302     raeburn  1557:     my ($r,$cattype) = @_;
1.191     raeburn  1558:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
1.201     raeburn  1559:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.200     raeburn  1560:     if (&Apache::lonnet::auto_run(undef,$env{'user.domain'})) {
1.191     raeburn  1561:         $r->print(&mt('If you were expecting to see an active role listed for a particular course in the [_1] domain, it may be missing for one of the following reasons:',$domdesc).'
                   1562: <ul>
                   1563:  <li>'.&mt('The course has yet to be created.').'</li>
                   1564:  <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
                   1565:  <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
                   1566:  <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
                   1567:  <li>'.&mt('You registered for the course recently and there is a time lag between the time you register, and the time this information becomes available for the update of LON-CAPA course rosters.').'</li>
                   1568:  </ul>');
                   1569:     } else {
                   1570:         $r->print(&mt('If you were expecting to see an active role listed for a particular course, that course may not have been created yet.').'<br />');
                   1571:     }
1.302     raeburn  1572:     if (($cattype eq 'std') || ($cattype eq 'domonly')) {
                   1573:         $r->print('<h3>'.&mt('Self-Enrollment').'</h3>'.
                   1574:                   '<p>'.&mt('The [_1]Course/Community Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created, as well as any communities in the domain.','<a href="/adm/coursecatalog?showdom='.$esc_dom.'">','</a>',$domdesc).'<br />');
                   1575:         $r->print(&mt('You can search for courses and communities which permit self-enrollment, if you would like to enroll in one.').'</p>'.
                   1576:         &Apache::loncoursequeueadmin::queued_selfenrollment());
                   1577:     }
1.216     raeburn  1578:     return;
                   1579: }
                   1580: 
1.234     raeburn  1581: sub requestcourse_advice {
1.306     raeburn  1582:     my ($r,$cattype,$inrole) = @_;
1.234     raeburn  1583:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                   1584:     my $esc_dom = &HTML::Entities::encode($env{'user.domain'},'"<>&');
1.306     raeburn  1585:     my (%can_request,%request_doms,$output);
1.234     raeburn  1586:     &Apache::lonnet::check_can_request($env{'user.domain'},\%can_request,\%request_doms);
                   1587:     if (keys(%request_doms) > 0) {
                   1588:         my ($types,$typename) = &Apache::loncommon::course_types();
                   1589:         if ((ref($types) eq 'ARRAY') && (ref($typename) eq 'HASH')) { 
                   1590:             my (@reqdoms,@reqtypes);
                   1591:             foreach my $type (sort(keys(%request_doms))) {
                   1592:                 push(@reqtypes,$type); 
                   1593:                 if (ref($request_doms{$type}) eq 'ARRAY') {
                   1594:                     my $domstr = join(', ',map { &Apache::lonnet::domain($_) } sort(@{$request_doms{$type}}));
1.306     raeburn  1595:                     $output .=
1.238     bisitz   1596:                         '<li>'
                   1597:                        .&mt('[_1]'.$typename->{$type}.'[_2] in domain: [_3]',
                   1598:                             '<i>',
                   1599:                             '</i>',
                   1600:                             '<b>'.$domstr.'</b>')
1.306     raeburn  1601:                        .'</li>';
1.234     raeburn  1602:                     foreach my $dom (@{$request_doms{$type}}) {
                   1603:                         unless (grep(/^\Q$dom\E/,@reqdoms)) {
                   1604:                             push(@reqdoms,$dom);
                   1605:                         }
                   1606:                     }
                   1607:                 }
                   1608:             }
                   1609:             my @showtypes;
                   1610:             foreach my $type (@{$types}) {
                   1611:                 if (grep(/^\Q$type\E$/,@reqtypes)) {
                   1612:                     push(@showtypes,$type);
                   1613:                 }
                   1614:             }
                   1615:             my $requrl = '/adm/requestcourse';
                   1616:             if (@reqdoms == 1) {
                   1617:                 $requrl .= '?showdom='.$reqdoms[0];
                   1618:             }
                   1619:             if (@showtypes > 0) {
                   1620:                 $requrl.=(($requrl=~/\?/)?'&':'?').'crstype='.$showtypes[0];
                   1621:             }
                   1622:             if (@reqdoms == 1 || @showtypes > 0) {
                   1623:                 $requrl .= '&state=crstype&action=new';
1.306     raeburn  1624:             }
1.307     raeburn  1625:             if ($output) {
                   1626:                 $r->print('<h3>'.&mt('Request creation of a course or community').'</h3>'.
                   1627:                           '<p>'.
                   1628:                           &mt('You have rights to request the creation of courses and/or communities in the following domain(s):').
                   1629:                           '<ul>'.
                   1630:                           $output.
                   1631:                           '</ul>'.
                   1632:                           &mt('Use the [_1]request form[_2] to submit a request for creation of a new course or community.',
                   1633:                               '<a href="'.$requrl.'">','</a>').
                   1634:                           '</p>');
                   1635:             }
1.234     raeburn  1636:         }
1.302     raeburn  1637:     } elsif (!$env{'user.adv'}) {
1.306     raeburn  1638:        if ($inrole) {
                   1639:             $r->print('<h3>'.&mt('Currently no additional roles, courses or communities').'</h3>');
                   1640:         } else {
                   1641:             $r->print('<h3>'.&mt('Currently no active roles, courses or communities').'</h3>');
                   1642:         }
1.302     raeburn  1643:         &findcourse_advice($r,$cattype);
1.234     raeburn  1644:     }
                   1645:     return;
                   1646: }
                   1647: 
1.175     albertel 1648: sub privileges_info {
                   1649:     my ($which) = @_;
                   1650:     my $output;
                   1651: 
                   1652:     $which ||= $env{'request.role'};
                   1653: 
                   1654:     foreach my $envkey (sort(keys(%env))) {
                   1655: 	next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
                   1656: 
                   1657: 	my $where=$1;
                   1658: 	my $ttype;
                   1659: 	my $twhere;
                   1660: 	my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
                   1661: 	if ($trest) {
                   1662: 	    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
1.288     raeburn  1663: 		$ttype='Authoring Space';
1.175     albertel 1664: 		$twhere='User: '.$trest.', Domain: '.$tdom;
                   1665: 	    } else {
                   1666: 		$ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
                   1667: 		$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
                   1668: 		if ($tsec) {
                   1669: 		    my $sec_type = 'Section';
                   1670: 		    if (exists($env{"user.role.gr.$where"})) {
                   1671: 			$sec_type = 'Group';
                   1672: 		    }
                   1673: 		    $twhere.=' ('.$sec_type.': '.$tsec.')';
                   1674: 		}
                   1675: 	    }
                   1676: 	} elsif ($tdom) {
                   1677: 	    $ttype='Domain';
                   1678: 	    $twhere=$tdom;
                   1679: 	} else {
                   1680: 	    $ttype='System';
                   1681: 	    $twhere='/';
                   1682: 	}
1.204     bisitz   1683: 	$output .= "\n<h3>".&mt($ttype).': '.$twhere.'</h3>'."\n<ul>";
1.175     albertel 1684: 	foreach my $priv (sort(split(/:/,$env{$envkey}))) {
                   1685: 	    next if (!$priv);
                   1686: 
                   1687: 	    my ($prv,$restr)=split(/\&/,$priv);
                   1688: 	    my $trestr='';
                   1689: 	    if ($restr ne 'F') {
                   1690: 		$trestr.=' ('.
                   1691: 		    join(', ',
                   1692: 			 map { &Apache::lonnet::plaintext($_) } 
                   1693: 			     (split('',$restr))).') ';
                   1694: 	    }
                   1695: 	    $output .= "\n\t".
                   1696: 		'<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
                   1697: 	}
                   1698: 	$output .= "\n".'</ul>';
                   1699:     }
                   1700:     return $output;
                   1701: }
                   1702: 
1.110     raeburn  1703: sub build_roletext {
1.313     raeburn  1704:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$trole,$twhere,
                   1705:         $tpstart,$tpend,$nochoose,$button,$switchserver,$reinit,$switchwarning,$skipcal) = @_;
1.223     raeburn  1706:     my ($roletext,$roletext_end);
1.132     albertel 1707:     my $is_dc=($trolecode =~ m/^dc\./);
                   1708:     my $rowspan=($is_dc) ? ''
                   1709:                          : ' rowspan="2" ';
                   1710: 
1.110     raeburn  1711:     unless ($nochoose) {
1.134     www      1712:         my $buttonname=$trolecode;
                   1713:         $buttonname=~s/\W//g;
1.110     raeburn  1714:         if (!$button) {
                   1715:             if ($switchserver) {
1.212     bisitz   1716:                 $roletext.='<td'.$rowspan.' class="'.$tbg.'">'
                   1717:                           .'<a href="/adm/switchserver?'.$switchserver.'">'
                   1718:                           .&mt('Switch Server')
                   1719:                           .'</a></td>';
1.110     raeburn  1720:             } else {
1.212     bisitz   1721:                 $roletext.=('<td'.$rowspan.' class="'.$tbg.'">&nbsp;</td>');
1.110     raeburn  1722:             }
1.255     raeburn  1723:             if ($switchwarning) {
                   1724:                 if ($tremark eq '') {
                   1725:                     $tremark = $switchwarning;
                   1726:                 } else {
                   1727:                     $tremark .= '<br />'.$switchwarning;
                   1728:                 }
                   1729:             }
1.110     raeburn  1730:         } elsif ($tstatus eq 'is') {
1.212     bisitz   1731:             $roletext.='<td'.$rowspan.' class="'.$tbg.'">'.
                   1732:                         '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1733:                         &mt('Select').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1734:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1735:         } elsif ($tryagain) {
                   1736:             $roletext.=
1.212     bisitz   1737:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1738:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1739:                 &mt('Try Selecting Again').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1740:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1741:         } elsif ($advanced) {
                   1742:             $roletext.=
1.212     bisitz   1743:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1744:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1745:                 &mt('Re-Initialize').'" onclick="javascript:enterrole(this.form,\''.
1.192     raeburn  1746:                         $trolecode."','".$buttonname.'\');" /></td>';
1.209     raeburn  1747:         } elsif ($reinit) {
                   1748:             $roletext.= 
1.212     bisitz   1749:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1750:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1751:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  1752:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1753:         } else {
1.209     raeburn  1754:             $roletext.=
1.212     bisitz   1755:                 '<td'.$rowspan.' class="'.$tbg.'">'.
                   1756:                 '<input name="'.$buttonname.'" type="button" value="'.
1.225     bisitz   1757:                 &mt('Re-Select').'" onclick="javascript:enterrole(this.form,\''.
1.209     raeburn  1758:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1759:         }
                   1760:     }
1.313     raeburn  1761:     if (($trolecode !~ m/^(dc|ca|au|aa)\./)  && (!$skipcal)) {
1.165     albertel 1762: 	$tremark.=&Apache::lonannounce::showday(time,1,
                   1763: 			 &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                   1764:     }
1.212     bisitz   1765:     $roletext.='<td>'.$trole.'</td>'
                   1766:               .'<td>'.$twhere.'</td>'
                   1767:               .'<td>'.$tpstart.'</td>'
1.223     raeburn  1768:               .'<td>'.$tpend.'</td>';
1.132     albertel 1769:     if (!$is_dc) {
1.223     raeburn  1770:         $roletext_end = '<td colspan="4">'.
                   1771:                         $tremark.'&nbsp;'.
                   1772:                         '</td>';
1.132     albertel 1773:     }
1.223     raeburn  1774:     return ($roletext,$roletext_end);
1.110     raeburn  1775: }
                   1776: 
1.193     raeburn  1777: sub check_author_homeserver {
1.183     www      1778:     my ($uname,$udom)=@_;
1.193     raeburn  1779:     if (($uname eq '') || ($udom eq '')) {
                   1780:         return ('fail','');
                   1781:     }
1.183     www      1782:     my $home = &Apache::lonnet::homeserver($uname,$udom);
1.193     raeburn  1783:     if (&Apache::lonnet::host_domain($home) ne $udom) {
                   1784:         return ('fail',$home);
                   1785:     }
1.183     www      1786:     my @ids=&Apache::lonnet::current_machine_ids();
1.193     raeburn  1787:     if (grep(/^\Q$home\E$/,@ids)) {
                   1788:         return ('ok',$home);
                   1789:     } else {
                   1790:         return ('switchserver',$home);
1.183     www      1791:     }
                   1792: }
                   1793: 
1.317     raeburn  1794: sub check_for_adhoc {
                   1795:     my ($dcroles,$dhroles,$update,$then) = @_;
1.104     raeburn  1796:     my $numdc = 0;
1.317     raeburn  1797:     my $numdh = 0;
                   1798:     my $numadhoc = 0;
                   1799:     my $num_custom_adhoc = 0; 
1.118     albertel 1800:     if ($env{'user.adv'}) {
1.309     raeburn  1801:         foreach my $envkey (sort(keys(%env))) {
1.317     raeburn  1802:             if ($envkey=~/^user\.role\.(dc|dh)\.\/($match_domain)\/$/) {
                   1803:                 my $role = $1;
                   1804:                 my $roledom = $2;
                   1805:                 my $liverole = 1;
1.118     albertel 1806:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.260     raeburn  1807:                 my $limit = $update;
1.317     raeburn  1808:                 if ((($role eq 'dc') && ($env{'request.role'} eq 'dc./'.$roledom.'/')) ||
                   1809:                     (($role eq 'dh') && ($env{'request.role'} eq 'dh./'.$roledom.'/'))) {
1.260     raeburn  1810:                     $limit = $then;
                   1811:                 }
1.317     raeburn  1812:                 if ($tstart && $tstart>$limit) { $liverole = 0; }
                   1813:                 if ($tend   && $tend  <$limit) { $liverole = 0; }
                   1814:                 if ($liverole) {
                   1815:                     if ($role eq 'dc') {
                   1816:                         $dcroles->{$roledom} = $envkey;
                   1817:                         $numdc++;
                   1818:                     } else {
                   1819:                         $dhroles->{$roledom} = $envkey;
                   1820:                         if ($env{'environment.adhocroles.'.$roledom} ne '') {
                   1821:                             $numadhoc ++;
                   1822:                         }
                   1823:                         $numdh++;
                   1824:                     }
1.104     raeburn  1825:                 }
                   1826:             }
                   1827:         }
                   1828:     }
1.317     raeburn  1829:     return ($numdc,$numdh,$numadhoc);
1.104     raeburn  1830: }
                   1831: 
1.185     raeburn  1832: sub adhoc_course_role {
1.260     raeburn  1833:     my ($refresh,$update,$then) = @_;
1.239     raeburn  1834:     my ($cdom,$cnum,$crstype);
1.201     raeburn  1835:     $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1836:     $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.239     raeburn  1837:     $crstype = &Apache::loncommon::course_type();
1.260     raeburn  1838:     if (&check_forcc($cdom,$cnum,$refresh,$update,$then,$crstype)) {
1.185     raeburn  1839:         my $setprivs;
1.198     raeburn  1840:         if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
1.185     raeburn  1841:             $setprivs = 1;
                   1842:         } else {
1.198     raeburn  1843:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.switchrole'}});
1.232     raeburn  1844:             if (($start && ($start>$refresh || $start == -1)) ||
1.260     raeburn  1845:                 ($end && $end<$update)) {
1.185     raeburn  1846:                 $setprivs = 1;
                   1847:             }
1.232     raeburn  1848:         }
1.278     raeburn  1849:         unless ($setprivs) {
                   1850:             if (!exists($env{'user.priv.'.$env{'form.switchrole'}.'./'})) {
                   1851:                 $setprivs = 1;
                   1852:             }
                   1853:         }
1.185     raeburn  1854:         if ($setprivs) {
1.297     raeburn  1855:             if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)(.*?)\./\Q$cdom\E/\Q$cnum\E/?(\w*)$-) {
1.185     raeburn  1856:                 my $role = $1;
                   1857:                 my $custom_role = $2;
                   1858:                 my $usec = $3;
                   1859:                 if ($role eq 'cr') {
1.199     raeburn  1860:                     if ($custom_role =~ m-^/$match_domain/$match_username/\w+$-) {
1.185     raeburn  1861:                         $role .= $custom_role;
                   1862:                     } else {
                   1863:                         return;
                   1864:                     }
                   1865:                 }
1.208     raeburn  1866:                 my (%userroles,%newrole,%newgroups,%group_privs);
                   1867:                 my %cgroups =
                   1868:                     &Apache::lonnet::get_active_groups($env{'user.domain'},
                   1869:                                             $env{'user.name'},$cdom,$cnum);
1.321     raeburn  1870:                 my $ccrole;
                   1871:                 if ($crstype eq 'Community') {
                   1872:                     $ccrole = 'co';
                   1873:                 } else {
                   1874:                     $ccrole = 'cc';
                   1875:                 }
1.208     raeburn  1876:                 foreach my $group (keys(%cgroups)) {
                   1877:                     $group_privs{$group} =
1.321     raeburn  1878:                         $env{'user.priv.'.$ccrole.'./'.$cdom.'/'.$cnum.'./'.$cdom.'/'.$cnum.'/'.$group};
1.208     raeburn  1879:                 }
                   1880:                 $newgroups{'/'.$cdom.'/'.$cnum} = \%group_privs;
1.185     raeburn  1881:                 my $area = '/'.$cdom.'/'.$cnum;
                   1882:                 my $spec = $role.'.'.$area;
                   1883:                 if ($usec ne '') {
                   1884:                     $spec .= '/'.$usec;
                   1885:                     $area .= '/'.$usec;
                   1886:                 }
1.278     raeburn  1887:                 if ($role =~ /^cr/) {
                   1888:                     &Apache::lonnet::custom_roleprivs(\%newrole,$role,$cdom,$cnum,$spec,$area);
                   1889:                 } else {
                   1890:                     &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
                   1891:                 }
1.208     raeburn  1892:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,\%newgroups);
1.232     raeburn  1893:                 my $adhocstart = $refresh-1;
1.185     raeburn  1894:                 $userroles{'user.role.'.$spec} = $adhocstart.'.';
1.186     raeburn  1895:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.185     raeburn  1896:             }
                   1897:         }
                   1898:     }
                   1899:     return;
                   1900: }
                   1901: 
                   1902: sub check_forcc {
1.260     raeburn  1903:     my ($cdom,$cnum,$refresh,$update,$then,$crstype) = @_;
1.239     raeburn  1904:     my ($is_cc,$ccrole);
                   1905:     if ($crstype eq 'Community') {
                   1906:         $ccrole = 'co';
                   1907:     } else {
                   1908:         $ccrole = 'cc';
                   1909:     }
1.266     droeschl 1910:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
                   1911:         my $envkey = 'user.role.'.$ccrole.'./'.$cdom.'/'.$cnum;
                   1912:         if (defined($env{$envkey})) {
                   1913:             $is_cc = 1;
                   1914:             my ($tstart,$tend)=split(/\./,$env{$envkey});
                   1915:             my $limit = $update;
                   1916:             if ($env{'request.role'} eq $ccrole.'./'.$cdom.'/'.$cnum) {
                   1917:                 $limit = $then;
1.185     raeburn  1918:             }
1.266     droeschl 1919:             if ($tstart && $tstart>$refresh) { $is_cc = 0; }
                   1920:             if ($tend   && $tend  <$limit) { $is_cc = 0; }
1.185     raeburn  1921:         }
                   1922:     }
                   1923:     return $is_cc;
                   1924: }
                   1925: 
1.108     raeburn  1926: sub courselink {
1.317     raeburn  1927:     my ($roledom,$rowtype,$role) = @_;
1.109     raeburn  1928:     my $courseform=&Apache::loncommon::selectcourse_link
1.317     raeburn  1929:                    ('rolechoice','course'.$rowtype.'_'.$roledom.'_'.$role,
                   1930:                     'domain'.$rowtype.'_'.$roledom.'_'.$role,
                   1931:                     'coursedesc'.$rowtype.'_'.$roledom.'_'.$role,
                   1932:                     $roledom.':'.$role,undef,'Course/Community');
                   1933:     my $hiddenitems = '<input type="hidden" name="domain'.$rowtype.'_'.$roledom.'_'.$role.'" value="'.$roledom.'" />'.
                   1934:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$roledom.'_'.$role.'" value="'.$roledom.'" />'.
                   1935:                       '<input type="hidden" name="course'.$rowtype.'_'.$roledom.'_'.$role.'" value="" />'.
                   1936:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$roledom.'_'.$role.'" value="" />';
1.112     raeburn  1937:     return $courseform.$hiddenitems;
1.109     raeburn  1938: }
                   1939: 
                   1940: sub coursepick_jscript {
1.312     damieng  1941:     my %js_lt = &Apache::lonlocal::texthash(
1.239     raeburn  1942:                   plsu => "Please use the 'Select Course/Community' link to open a separate pick course window where you may select the course or community you wish to enter.",
1.234     raeburn  1943:                   youc => 'You can only use this screen to select courses and communities in the current domain.',
1.184     raeburn  1944:              );
1.312     damieng  1945:     &js_escape(\%js_lt);
1.104     raeburn  1946:     my $verify_script = <<"END";
1.179     raeburn  1947: <script type="text/javascript">
1.225     bisitz   1948: // <![CDATA[
1.108     raeburn  1949: function verifyCoursePick(caller) {
                   1950:     var numbutton = getIndex(caller)
1.112     raeburn  1951:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                   1952:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                   1953:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn  1954:         if (pickedCourse != '') {
1.108     raeburn  1955:             if (numbutton != -1) {
                   1956:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                   1957:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                   1958:                 document.rolechoice.submit()
                   1959:             }
1.104     raeburn  1960:         }
                   1961:         else {
1.312     damieng  1962:             alert("$js_lt{'plsu'}");
1.104     raeburn  1963:         }
                   1964:     }
                   1965:     else {
1.312     damieng  1966:         alert("$js_lt{'youc'}")
1.104     raeburn  1967:     }
                   1968: }
1.109     raeburn  1969: function getIndex(caller) {
1.108     raeburn  1970:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn  1971:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn  1972:             return i;
                   1973:         }
                   1974:     }
                   1975:     return -1;
                   1976: }
1.225     bisitz   1977: // ]]>
1.104     raeburn  1978: </script>
                   1979: END
1.109     raeburn  1980:     return $verify_script;
1.104     raeburn  1981: }
                   1982: 
1.193     raeburn  1983: sub coauthorlink {
                   1984:     my ($dcdom,$rowtype) = @_;
                   1985:     my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
                   1986:     my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
                   1987:     return $coauthorform.$hiddenitems;
                   1988: }
                   1989: 
1.113     raeburn  1990: sub display_cc_role {
                   1991:     my $rolekey = shift;
1.223     raeburn  1992:     my ($roletext,$roletext_end);
1.118     albertel 1993:     my $advanced = $env{'user.adv'};
                   1994:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn  1995:     unless ($rolekey =~/^error\:/) {
1.240     raeburn  1996:         if ($rolekey =~ m{^user\.role\.(cc|co)\./($match_domain)/($match_courseid)$}) {
                   1997:             my $ccrole = $1;
1.249     raeburn  1998:             my $tdom = $2;
                   1999:             my $trest = $3;
                   2000:             my $tcourseid = $tdom.'_'.$trest;
                   2001:             my $trolecode = $ccrole.'./'.$tdom.'/'.$trest;
1.113     raeburn  2002:             my $twhere;
1.152     raeburn  2003:             my $ttype;
1.313     raeburn  2004:             my $skipcal;
1.212     bisitz   2005:             my $tbg='LC_roles_is';
1.113     raeburn  2006:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   2007:             if (%newhash) {
                   2008:                 $twhere=$newhash{'description'}.
1.261     bisitz   2009:                         ' <span class="LC_fontsize_small">'.
1.249     raeburn  2010:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom).
1.211     tempelho 2011:                         '</span>';
1.153     raeburn  2012:                 $ttype = $newhash{'type'};
1.113     raeburn  2013:             } else {
                   2014:                 $twhere=&mt('Currently not available');
1.118     albertel 2015:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.313     raeburn  2016:                 $skipcal = 1;
1.110     raeburn  2017:             }
1.242     raeburn  2018:             my $trole = &Apache::lonnet::plaintext($ccrole,$ttype,$tcourseid);
1.258     raeburn  2019:             $twhere.="<br />".&mt('Domain').":".$tdom;
1.313     raeburn  2020:             ($roletext,$roletext_end) = &build_roletext($trolecode,$tdom,$trest,'is',$tryagain,$advanced,'',$tbg,$trole,$twhere,'','','',1,'','','',$skipcal);
1.104     raeburn  2021:         }
                   2022:     }
1.223     raeburn  2023:     return ($roletext,$roletext_end);
1.104     raeburn  2024: }
                   2025: 
1.192     raeburn  2026: sub adhoc_roles_row {
1.138     raeburn  2027:     my ($dcdom,$rowtype) = @_;
1.212     bisitz   2028:     my $output = &Apache::loncommon::continue_data_table_row()
1.314     raeburn  2029:                  .' <td colspan="5" class="LC_textsize_mobile">'
1.212     bisitz   2030:                  .&mt('[_1]Ad hoc[_2] roles in domain [_3] --'
                   2031:                      ,'<span class="LC_cusr_emph">','</span>',$dcdom)
1.227     bisitz   2032:                  .' ';
1.317     raeburn  2033:     my $role = 'cc';
                   2034:     my $selectcclink = &courselink($dcdom,$rowtype,$role);
1.239     raeburn  2035:     my $ccrole = &Apache::lonnet::plaintext('co',undef,undef,1);
1.182     www      2036:     my $carole = &Apache::lonnet::plaintext('ca');
1.193     raeburn  2037:     my $selectcalink = &coauthorlink($dcdom,$rowtype);
1.227     bisitz   2038:     $output.=$ccrole.': '.$selectcclink
1.249     raeburn  2039:             .' | '.$carole.': '.$selectcalink.'</td>'
1.212     bisitz   2040:             .&Apache::loncommon::end_data_table_row();
1.108     raeburn  2041:     return $output;
                   2042: }
                   2043: 
1.317     raeburn  2044: sub adhoc_customroles_row {
                   2045:     my ($dhdom,$rowtype) = @_;
                   2046:     my $output = &Apache::loncommon::continue_data_table_row()
                   2047:                  .' <td colspan="5" class="LC_textsize_mobile">'
                   2048:                  .&mt('[_1]Ad hoc[_2] course/community roles in domain [_3] --',
                   2049:                       '<span class="LC_cusr_emph">','</span>',$dhdom);
                   2050:     my @customroles = split(/,/,$env{'environment.adhocroles.'.$dhdom});
                   2051:     my $count = 0;
                   2052:     foreach my $role (@customroles) {
                   2053:         next if (($role eq '') || ($role =~ /\W/));
                   2054:         $output .= ' '.$role.': '.&courselink($dhdom,$rowtype,$role).' |';
                   2055:         $count ++;
                   2056:     }
                   2057:     if ($count) {
                   2058:         return $output;
                   2059:     }
                   2060:     return;
                   2061: }
                   2062: 
1.104     raeburn  2063: sub recent_filename {
                   2064:     my $area=shift;
1.149     www      2065:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  2066: }
                   2067: 
1.139     raeburn  2068: sub courseloadpage {
                   2069:     my ($courseid) = @_;
                   2070:     my $startpage;
1.144     albertel 2071:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   2072: 					      [$courseid.':courseinit']);
1.139     raeburn  2073:     my ($tmp) = %entry_settings;
1.144     albertel 2074:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  2075:         $startpage = $entry_settings{$courseid.':courseinit'};
                   2076:     }
                   2077:     if ($startpage eq '') {
                   2078:         if (exists($env{'environment.course_init_display'})) {
                   2079:             $startpage = $env{'environment.course_init_display'};
                   2080:         }
                   2081:     }
                   2082:     return $startpage;
                   2083: }
                   2084: 
1.260     raeburn  2085: sub update_session_roles {
                   2086:     my $then=$env{'user.login.time'};
                   2087:     my $refresh=$env{'user.refresh.time'};
                   2088:     if (!$refresh) {
                   2089:         $refresh = $then;
                   2090:     }
                   2091:     my $update = $env{'user.update.time'};
                   2092:     if (!$update) {
                   2093:         $update = $then;
                   2094:     }
                   2095:     my $now = time;
                   2096:     my %roleshash =
                   2097:         &Apache::lonnet::get_my_roles('','','userroles',
                   2098:                                       ['active','future','previous'],
                   2099:                                       undef,undef,1);
                   2100:     my ($msg,@newsec,$oldsec,$currrole_expired,@changed_roles,
1.264     raeburn  2101:         %changed_groups,%dbroles,%deletedroles,%allroles,%allgroups,
1.260     raeburn  2102:         %userroles,%checkedgroup,%crprivs,$hasgroups,%rolechange,
                   2103:         %groupchange,%newrole,%newgroup,%customprivchg,%groups_roles,
                   2104:         @rolecodes);
                   2105:     my @possroles = ('cr','st','ta','ad','ep','in','co','cc');
                   2106:     my %courseroles;
                   2107:     foreach my $item (keys(%roleshash)) {
                   2108:         my ($uname,$udom,$role,$remainder) = split(/:/,$item,4);
                   2109:         my ($tstart,$tend) = split(/:/,$roleshash{$item});
                   2110:         my ($section,$group,@group_privs);
                   2111:         if ($role =~ m{^gr/(\w*)$}) {
                   2112:             $role = 'gr';
                   2113:             my $priv = $1;
                   2114:             next if ($tstart eq '-1');
                   2115:             if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2116:                 if ($priv ne '') {
                   2117:                     push(@group_privs,$priv);
                   2118:                 }
                   2119:             }
                   2120:             if ($remainder =~ /:/) {
                   2121:                 (my $additional_privs,$group) =
                   2122:                     ($remainder =~ /^([\w:]+):([^:]+)$/);
                   2123:                 if ($additional_privs ne '') {
                   2124:                     if (&curr_role_status($tstart,$tend,$refresh,$now) eq 'active') {
                   2125:                         push(@group_privs,split(/:/,$additional_privs));
                   2126:                         @group_privs = sort(@group_privs);
                   2127:                     }
                   2128:                 }
                   2129:             } else {
                   2130:                 $group = $remainder;
                   2131:             }
                   2132:         } else {
                   2133:             $section = $remainder;
                   2134:         }
                   2135:         my $where = "/$udom/$uname";
                   2136:         if ($section ne '') {
                   2137:             $where .= "/$section";
                   2138:         } elsif ($group ne '') {
                   2139:             $where .= "/$group";
                   2140:         }
                   2141:         my $rolekey = "$role.$where";
                   2142:         my $envkey = "user.role.$rolekey";
                   2143:         $dbroles{$envkey} = 1;
                   2144:         if (($env{'request.role'} eq $rolekey) && ($role ne 'st')) {
                   2145:             if (&curr_role_status($tstart,$tend,$refresh,$now) ne 'active') {
                   2146:                 $currrole_expired = 1;
                   2147:             }
                   2148:         }
                   2149:         if ($env{$envkey} eq '') {
                   2150:             my $status_in_db =
1.271     raeburn  2151:                 &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2152:                 &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2153:             if (($role eq 'st') && ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2154:                 if ($status_in_db eq 'active') {
                   2155:                     if ($section eq '') {
                   2156:                         push(@newsec,'none');
                   2157:                     } else {
                   2158:                         push(@newsec,$section);
                   2159:                     }
                   2160:                 }
                   2161:             } else {
                   2162:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2163:                     push(@changed_roles,$role);
                   2164:                 }
                   2165:                 if ($status_in_db ne 'previous') {
                   2166:                     if ($role eq 'gr') {
                   2167:                         $newgroup{$rolekey} = $status_in_db;
                   2168:                         if ($status_in_db eq 'active') {
                   2169:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2170:                                 %{$courseroles{$udom}} =
                   2171:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2172:                                                                   ['active'],\@possroles,
                   2173:                                                                   [$udom],1);
                   2174:                             }
                   2175:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2176:                                                               $courseroles{$udom},
                   2177:                                                               \@rolecodes,\%groups_roles);
                   2178:                         }
                   2179:                     } else {
                   2180:                         $newrole{$rolekey} = $status_in_db;
                   2181:                     }
                   2182:                 }
                   2183:             }
                   2184:         } else {
                   2185:             my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2186:             if ($role eq 'gr') {
                   2187:                 if (&curr_role_status($currstart,$currend,$refresh,$update) ne 'previous') {
                   2188:                     $hasgroups = 1;
                   2189:                 }
                   2190:             }
                   2191:             if (($currstart ne $tstart) || ($currend ne $tend)) {
                   2192:                 my $status_in_env =
                   2193:                     &curr_role_status($currstart,$currend,$refresh,$update);
                   2194:                 my $status_in_db =
1.271     raeburn  2195:                     &curr_role_status($tstart,$tend,$now,$now);
1.260     raeburn  2196:                 if ($status_in_env ne $status_in_db) {
                   2197:                     if ($status_in_env eq 'active') {
                   2198:                         if ($role eq 'st') {
                   2199:                             if ($env{'request.role'} eq $rolekey) {
                   2200:                                 my $switchsection;
                   2201:                                 unless (ref($courseroles{$udom}) eq 'HASH') {
                   2202:                                     %{$courseroles{$udom}} =
                   2203:                                         &Apache::lonnet::get_my_roles('','','userroles',
                   2204:                                                                       ['active'],
                   2205:                                                                       \@possroles,[$udom],1);
                   2206:                                 }
                   2207:                                 foreach my $crsrole (keys(%{$courseroles{$udom}})) {
                   2208:                                     if ($crsrole =~ /^\Q$uname\E:\Q$udom\E:st/) {
                   2209:                                         $switchsection = 1;
                   2210:                                         last;
                   2211:                                     }
                   2212:                                 }
                   2213:                                 if ($switchsection) {
                   2214:                                     if ($section eq '') {
                   2215:                                         $oldsec = 'none';
                   2216:                                     } else {
                   2217:                                         $oldsec = $section;
                   2218:                                     }
                   2219:                                     &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2220:                                 } else {
                   2221:                                     $currrole_expired = 1;
                   2222:                                     next;
                   2223:                                 }
                   2224:                             }
                   2225:                         }
                   2226:                         unless ($rolekey eq $env{'request.role'}) {
                   2227:                             if ($role eq 'gr') {
                   2228:                                 &Apache::lonnet::delete_env_groupprivs($where,\%courseroles,\@possroles);
                   2229:                             } else {
                   2230:                                 &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2231:                                 &Apache::lonnet::delenv("user.priv.cm.$where",undef,['cm']);
                   2232:                             }
                   2233:                             &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2234:                         }
                   2235:                     } elsif ($status_in_db eq 'active') {
                   2236:                         if (($role eq 'st') &&
                   2237:                             ($env{'request.role'} =~ m{^\Q$role\E\.\Q/$udom/$uname\E})) {
                   2238:                             if ($section eq '') {
                   2239:                                 push(@newsec,'none');
                   2240:                             } else {
                   2241:                                 push(@newsec,$section);
                   2242:                             }
                   2243:                         } elsif ($role eq 'gr') {
                   2244:                             unless (ref($courseroles{$udom}) eq 'HASH') {
                   2245:                                 %{$courseroles{$udom}} =
                   2246:                                     &Apache::lonnet::get_my_roles('','','userroles',
                   2247:                                                                   ['active'],
                   2248:                                                                   \@possroles,[$udom],1);
                   2249:                             }
                   2250:                             &Apache::lonnet::get_groups_roles($udom,$uname,
                   2251:                                                               $courseroles{$udom},
                   2252:                                                               \@rolecodes,\%groups_roles);
                   2253:                         }
                   2254:                         &gather_roleprivs(\%allroles,\%allgroups,\%userroles,$where,$role,$tstart,$tend,$status_in_db);
                   2255:                     }
                   2256:                     unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2257:                         push(@changed_roles,$role);
                   2258:                     }
                   2259:                     if ($role eq 'gr') {
                   2260:                         $groupchange{"/$udom/$uname"}{$group} = $status_in_db;
                   2261:                     } else {
                   2262:                         $rolechange{$rolekey} = $status_in_db;
                   2263:                     }
                   2264:                 }
                   2265:             } else {
                   2266:                 if ($role eq 'gr') {
                   2267:                     unless ($checkedgroup{$where}) {
                   2268:                         my $status_in_db =
                   2269:                             &curr_role_status($tstart,$tend,$refresh,$now);
                   2270:                         if ($tstart eq '-1') {
                   2271:                             $status_in_db = 'deleted';
                   2272:                         }
                   2273:                         unless (ref($courseroles{$udom}) eq 'HASH') {
                   2274:                             %{$courseroles{$udom}} =
                   2275:                                 &Apache::lonnet::get_my_roles('','','userroles',
                   2276:                                                               ['active'],
                   2277:                                                               \@possroles,[$udom],1);
                   2278:                         }
                   2279:                         if (ref($courseroles{$udom}) eq 'HASH') {
                   2280:                             foreach my $item (keys(%{$courseroles{$udom}})) {
                   2281:                                 next unless ($item =~ /^\Q$uname\E/);
                   2282:                                 my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   2283:                                 my $area = '/'.$cdom.'/'.$cnum;
                   2284:                                 if ($crssec ne '') {
                   2285:                                     $area .= '/'.$crssec;
                   2286:                                 }
                   2287:                                 my $crsrolekey = $crsrole.'.'.$area;
                   2288:                                 my $currprivs = $env{'user.priv.'.$crsrole.'.'.$area.'.'.$where};
                   2289:                                 $currprivs =~ s/^://;
                   2290:                                 $currprivs =~ s/\&F$//;
                   2291:                                 my @curr_grp_privs = split(/\&F:/,$currprivs);
                   2292:                                 @curr_grp_privs = sort(@curr_grp_privs);
                   2293:                                 my @diffs;
                   2294:                                 if (@group_privs > 0 || @curr_grp_privs > 0) {
                   2295:                                     @diffs = &Apache::loncommon::compare_arrays(\@group_privs,\@curr_grp_privs);
                   2296:                                 }
                   2297:                                 if (@diffs == 0) {
                   2298:                                     last;
                   2299:                                 } else {
                   2300:                                     unless(grep(/^\Qgr\E$/,@rolecodes)) {
                   2301:                                         push(@rolecodes,'gr');
                   2302:                                     }
                   2303:                                     &gather_roleprivs(\%allroles,\%allgroups,
                   2304:                                                       \%userroles,$where,$role,
                   2305:                                                       $tstart,$tend,$status_in_db);
                   2306:                                     if ($status_in_db eq 'active') {
                   2307:                                         &Apache::lonnet::get_groups_roles($udom,$uname,
                   2308:                                                                           $courseroles{$udom},
                   2309:                                                                           \@rolecodes,\%groups_roles);
                   2310:                                     }
                   2311:                                     $changed_groups{$udom.'_'.$uname}{$group} = $status_in_db;
                   2312:                                     last;
                   2313:                                 }
                   2314:                             }
                   2315:                         }
                   2316:                         $checkedgroup{$where} = 1;
                   2317:                     }
                   2318:                 } elsif ($role =~ /^cr/) {
                   2319:                     my $status_in_db =
                   2320:                         &curr_role_status($tstart,$tend,$refresh,$now);
                   2321:                     my ($rdummy,$rest) = split(/\//,$role,2);
                   2322:                     my %currpriv;
                   2323:                     unless (exists($crprivs{$rest})) {
                   2324:                         my ($rdomain,$rauthor,$rrole)=split(/\//,$rest);
                   2325:                         my $homsvr=&Apache::lonnet::homeserver($rauthor,$rdomain);
                   2326:                         if (&Apache::lonnet::hostname($homsvr) ne '') {
                   2327:                             my ($rdummy,$roledef)=
                   2328:                             &Apache::lonnet::get('roles',["rolesdef_$rrole"],
                   2329:                                                  $rdomain,$rauthor);
                   2330:                             if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   2331:                                 my $i = 0;
                   2332:                                 my @scopes = ('sys','dom','crs');
                   2333:                                 my @privs = split(/\_/,$roledef);
                   2334:                                 foreach my $priv (@privs) {
                   2335:                                     my ($blank,@prv) = split(/:/,$priv);
                   2336:                                     @prv = map { $_ .= (/\&\w+$/ ? '':'&F') } @prv;
1.264     raeburn  2337:                                     if (@prv) {
                   2338:                                         $priv = ':'.join(':',sort(@prv));
                   2339:                                     }
1.260     raeburn  2340:                                     $crprivs{$rest}{$scopes[$i]} = $priv;
                   2341:                                     $i++;
                   2342:                                 }
                   2343:                             }
                   2344:                         }
                   2345:                     }
1.279     raeburn  2346:                     my $status_in_env =
                   2347:                         &curr_role_status($currstart,$currend,$refresh,$update);
                   2348:                     if ($status_in_env eq 'active') {
                   2349:                         $currpriv{sys} = $env{"user.priv.$rolekey./"};
                   2350:                         $currpriv{dom} = $env{"user.priv.$rolekey./$udom/"};
                   2351:                         $currpriv{crs} = $env{"user.priv.$rolekey.$where"};
                   2352:                         if (keys(%crprivs)) {
                   2353:                             if (($crprivs{$rest}{sys} ne $currpriv{sys}) ||
                   2354:                                 ($crprivs{$rest}{dom} ne $currpriv{dom})
1.260     raeburn  2355:  ||
1.279     raeburn  2356:                                 ($crprivs{$rest}{crs} ne $currpriv{crs})) {
                   2357:                                 &gather_roleprivs(\%allroles,\%allgroups,
                   2358:                                                   \%userroles,$where,$role,
                   2359:                                                   $tstart,$tend,$status_in_db);
                   2360:                                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2361:                                     push(@changed_roles,$role);
                   2362:                                 }
1.260     raeburn  2363:                                 $customprivchg{$rolekey} = $status_in_env;
                   2364:                             }
                   2365:                         }
                   2366:                     }
                   2367:                 }
                   2368:             }
                   2369:         }
                   2370:     }
                   2371:     foreach my $envkey (keys(%env)) {
                   2372:         next unless ($envkey =~ /^user\.role\./);
                   2373:         next if ($dbroles{$envkey});
                   2374:         next if ($envkey eq 'user.role.'.$env{'request.role'});
                   2375:         my ($currstart,$currend) = split(/\./,$env{$envkey});
                   2376:         my $status_in_env =
                   2377:             &curr_role_status($currstart,$currend,$refresh,$update);
                   2378:         my ($rolekey) = ($envkey =~ /^user\.role\.(.+)$/);
1.297     raeburn  2379:         my ($role,$rest)=split(m{\./},$rolekey,2);
                   2380:         $rest = '/'.$rest;
1.260     raeburn  2381:         if (&Apache::lonnet::delenv($envkey,undef,[$role])) {
                   2382:             if ($status_in_env eq 'active') {
                   2383:                 if ($role eq 'gr') {
                   2384:                     &Apache::lonnet::delete_env_groupprivs($rest,\%courseroles,
                   2385:                                                            \@possroles);
                   2386:                 } else {
                   2387:                     &Apache::lonnet::delenv("user.priv.$rolekey",undef,[$role]);
                   2388:                     &Apache::lonnet::delenv("user.priv.cm.$rest",undef,['cm']);
                   2389:                 }
                   2390:                 unless (grep(/^\Q$role\E$/,@changed_roles)) {
                   2391:                     push(@changed_roles,$role);
                   2392:                 }
                   2393:                 $deletedroles{$rolekey} = 1;
                   2394:             }
                   2395:         }
                   2396:     }
                   2397:     if (($oldsec) && (@newsec > 0)) {
                   2398:         if (@newsec > 1) {
1.274     raeburn  2399:             $msg = '<p class="LC_warning">'.&mt('The section has changed for your current role. Log-out and log-in again to select a role for the new section.').'</p>';
1.260     raeburn  2400:         } else {
                   2401:             my $newrole = $env{'request.role'};
                   2402:             if ($newsec[0] eq 'none') {
                   2403:                 $newrole =~ s{(/[^/])$}{};
                   2404:             } elsif ($oldsec eq 'none') {
                   2405:                 $newrole .= '/'.$newsec[0];
                   2406:             } else {
                   2407:                 $newrole =~ s{([^/]+)$}{$newsec[0]};
                   2408:             }
                   2409:             my $coursedesc = $env{'course.'.$env{'request.course.id'}.'.description'};
                   2410:             my ($curr_role) = ($env{'request.role'} =~ m{^(\w+)\./$match_domain/$match_courseid});
                   2411:             my %temp=('logout_'.$env{'request.course.id'} => time);
                   2412:             &Apache::lonnet::put('email_status',\%temp);
                   2413:             &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
                   2414:             &Apache::lonnet::appenv({"request.course.id"   => '',
                   2415:                                      "request.course.fn"   => '',
                   2416:                                      "request.course.uri"  => '',
                   2417:                                      "request.course.sec"  => '',
                   2418:                                      "request.role"        => 'cm',
                   2419:                                      "request.role.adv"    => $env{'user.adv'},
                   2420:                                      "request.role.domain" => $env{'user.domain'}});
                   2421:             my $rolename = &Apache::loncommon::plainname($curr_role);
                   2422:             $msg = '<p><form name="reselectrole" action="/adm/roles" method="post" />'.
                   2423:                    '<input type="hidden" name="newrole" value="" />'.
                   2424:                    '<input type="hidden" name="selectrole" value="1" />'.
                   2425:                    '<span class="LC_info">'.
                   2426:                    &mt('Your section has changed for your current [_1] role in [_2].',$rolename,$coursedesc).'</span><br />';
                   2427:             my $button = '<input type="button" name="sectionchanged" value="'.
                   2428:                          &mt('Re-Select').'" onclick="javascript:enterrole(this.form,'."'$newrole','sectionchanged'".')" />';
                   2429:             if ($newsec[0] eq 'none') {
                   2430:                 $msg .= &mt('[_1] to continue with your new section-less role.',$button);
                   2431:             } else {
                   2432:                 $msg .= &mt('[_1] to continue with your new role in section ([_2]).',$button,$newsec[0]);
                   2433:             }
                   2434:             $msg .= '</form></p>';
                   2435:         }
                   2436:     } elsif ($currrole_expired) {
1.274     raeburn  2437:         $msg .= '<p class="LC_warning">';
1.260     raeburn  2438:         if (&Apache::loncommon::show_course()) {
                   2439:             $msg .= &mt('Your role in the current course has expired.');
                   2440:         } else {
                   2441:             $msg .= &mt('Your current role has expired.');
                   2442:         }
1.274     raeburn  2443:         $msg .= '<br />'.&mt('However you can continue to use this role until you logout, click the "Re-Select" button, or your session has been idle for more than 24 hours.').'</p>';
1.260     raeburn  2444:     }
1.279     raeburn  2445:     &Apache::lonnet::set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   2446:     my ($curr_is_adv,$curr_role_adv,$curr_author,$curr_role_author);
                   2447:     $curr_author = $env{'user.author'};
                   2448:     if (($env{'request.role'} =~/^au/) || ($env{'request.role'} =~/^ca/) ||
                   2449:         ($env{'request.role'} =~/^aa/)) {
                   2450:         $curr_role_author=1;
                   2451:     }
                   2452:     $curr_is_adv = $env{'user.adv'};
                   2453:     $curr_role_adv = $env{'request.role.adv'};
                   2454:     if (keys(%userroles) > 0) {
                   2455:         foreach my $role (@changed_roles) {
                   2456:             unless(grep(/^\Q$role\E$/,@rolecodes)) {
                   2457:                 push(@rolecodes,$role);
                   2458:             }
                   2459:         }
                   2460:         unless(grep(/^\Qcm\E$/,@rolecodes)) {
                   2461:             push(@rolecodes,'cm');
                   2462:         }
                   2463:         &Apache::lonnet::appenv(\%userroles,\@rolecodes);
                   2464:     }
                   2465:     my %newenv;
                   2466:     if (&Apache::lonnet::is_advanced_user($env{'user.domain'},$env{'user.name'})) {
                   2467:         unless ($curr_is_adv) {
                   2468:             $newenv{'user.adv'} = 1;
                   2469:         }
                   2470:     } elsif ($curr_is_adv && !$curr_role_adv) {
                   2471:         &Apache::lonnet::delenv('user.adv');
                   2472:     }
                   2473:     my %authorroleshash =
                   2474:         &Apache::lonnet::get_my_roles('','','userroles',['active'],['au','ca','aa']);
                   2475:     if (keys(%authorroleshash)) {
                   2476:         unless ($curr_author) {
                   2477:             $newenv{'user.author'} = 1;
                   2478:         }
                   2479:     } elsif ($curr_author && !$curr_role_author) {
                   2480:         &Apache::lonnet::delenv('user.author');
                   2481:     }
                   2482:     if ($env{'request.course.id'}) {
                   2483:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2484:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   2485:         my (@activecrsgroups,$crsgroupschanged);
                   2486:         if ($env{'request.course.groups'}) {
                   2487:             @activecrsgroups = split(/:/,$env{'request.course.groups'});
                   2488:             foreach my $item (keys(%deletedroles)) {
                   2489:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   2490:                     if (grep(/^\Q$1\E$/,@activecrsgroups)) {
                   2491:                         $crsgroupschanged = 1;
                   2492:                         last;
                   2493:                     }
                   2494:                 }
                   2495:             }
                   2496:         }
                   2497:         unless ($crsgroupschanged) {
                   2498:             foreach my $item (keys(%newgroup)) {
                   2499:                 if ($item =~ m{^gr\./\Q$cdom\E/\Q$cnum\E/(\w+)$}) {
                   2500:                     if ($newgroup{$item} eq 'active') {
                   2501:                         $crsgroupschanged = 1;
                   2502:                         last;
                   2503:                     }
                   2504:                 }
                   2505:             }
                   2506:         }
                   2507:         if ((ref($changed_groups{$env{'request.course.id'}}) eq 'HASH') ||
                   2508:             (ref($groupchange{"/$cdom/$cnum"}) eq 'HASH') ||
                   2509:             ($crsgroupschanged)) {
                   2510:             my %grouproles =  &Apache::lonnet::get_my_roles('','','userroles',
                   2511:                                                             ['active'],['gr'],[$cdom],1);
                   2512:             my @activegroups;
                   2513:             foreach my $item (keys(%grouproles)) {
                   2514:                 next unless($item =~ /^\Q$cnum\E:\Q$cdom\E/);
                   2515:                 my $group;
                   2516:                 my ($crsn,$crsd,$role,$remainder) = split(/:/,$item,4);
                   2517:                 if ($remainder =~ /:/) {
                   2518:                     (my $other,$group) = ($remainder =~ /^([\w:]+):([^:]+)$/);
                   2519:                 } else {
                   2520:                     $group = $remainder;
                   2521:                 }
                   2522:                 if ($group ne '') {
                   2523:                     push(@activegroups,$group);
                   2524:                 }
                   2525:             }
                   2526:             $newenv{'request.course.groups'} = join(':',@activegroups);
                   2527:         }
                   2528:     }
                   2529:     if (keys(%newenv)) {
                   2530:         &Apache::lonnet::appenv(\%newenv);
                   2531:     }
1.260     raeburn  2532:     if (!@changed_roles || !(keys(%changed_groups))) {
1.264     raeburn  2533:         my ($rolesmsg,$groupsmsg);
1.260     raeburn  2534:         if (!@changed_roles) {
                   2535:             if (&Apache::loncommon::show_course()) {
1.264     raeburn  2536:                 $rolesmsg = &mt('No new courses or communities');
1.260     raeburn  2537:             } else {
1.264     raeburn  2538:                 $rolesmsg = &mt('No role changes');
1.260     raeburn  2539:             }
                   2540:         }
                   2541:         if ($hasgroups && !(keys(%changed_groups)) && !(grep(/gr/,@changed_roles))) {
1.264     raeburn  2542:             $groupsmsg = &mt('No changes in course/community groups');
1.260     raeburn  2543:         }
                   2544:         if (!@changed_roles && !(keys(%changed_groups))) {
1.264     raeburn  2545:             if (($msg ne '') || ($groupsmsg ne '')) {
                   2546:                 $msg .= '<ul>';
                   2547:                 if ($rolesmsg) {
                   2548:                     $msg .= '<li>'.$rolesmsg.'</li>';
                   2549:                 }
                   2550:                 if ($groupsmsg) {
                   2551:                     $msg .= '<li>'.$groupsmsg.'</li>';
                   2552:                 }
                   2553:                 $msg .= '</ul>';
                   2554:             } else {
1.270     raeburn  2555:                 $msg = '&nbsp;<span class="LC_cusr_emph">'.$rolesmsg.'</span><br />';
1.264     raeburn  2556:             }
1.260     raeburn  2557:             return $msg;
                   2558:         }
                   2559:     }
                   2560:     my $changemsg;
                   2561:     if (@changed_roles > 0) {
                   2562:         if (keys(%newgroup) > 0) {
                   2563:             my $groupmsg;
1.279     raeburn  2564:             my (%curr_groups,%groupdescs,$currcrs);
1.260     raeburn  2565:             foreach my $item (sort(keys(%newgroup))) {
                   2566:                 if (&is_active_course($item,$refresh,$update,\%roleshash)) {
1.279     raeburn  2567:                     if ($item =~ m{^gr\./($match_domain/$match_courseid)/(\w+)$}) {
                   2568:                         my ($cdom,$cnum) = split(/\//,$1);
                   2569:                         my $group = $2;
                   2570:                         if ($currcrs ne $cdom.'_'.$cnum) {
                   2571:                             if ($currcrs) {
                   2572:                                 $groupmsg .= '</ul><li>';
                   2573:                             }
                   2574:                             $groupmsg .= '<li><b>'.
                   2575:                                          $env{'course.'.$cdom.'_'.$cnum.'.description'}.'</b><ul>';
1.281     raeburn  2576:                             $currcrs = $cdom.'_'.$cnum;
1.279     raeburn  2577:                         }
                   2578:                         my $groupdesc;
                   2579:                         unless (ref($curr_groups{$cdom.'_'.$cnum}) eq 'HASH') {
                   2580:                             %{$curr_groups{$cdom.'_'.$cnum}} = 
                   2581:                                 &Apache::longroup::coursegroups($cdom,$cnum);
                   2582:                         }
                   2583:                         unless ((ref($groupdescs{$cdom.'_'.$cnum}) eq 'HASH') &&
                   2584:                             ($groupdescs{$cdom.'_'.$cnum}{$group})) {
                   2585: 
                   2586:                             my %groupinfo = 
                   2587:                                 &Apache::longroup::get_group_settings($curr_groups{$cdom.'_'.$cnum}{$group});
                   2588:                             $groupdescs{$cdom.'_'.$cnum}{$group} = 
                   2589:                                 &unescape($groupinfo{'description'});
                   2590:                         }
                   2591:                         $groupdesc = $groupdescs{$cdom.'_'.$cnum}{$group};
1.286     raeburn  2592:                         if ($groupdesc) {
                   2593:                             $groupmsg .= '<li>'.
                   2594:                                          &mt('[_1] with status: [_2].',
                   2595:                                          '<b>'.$groupdesc.'</b>',$newgroup{$item}).'</li>';
                   2596:                         }
1.279     raeburn  2597:                     }
                   2598:                 }
                   2599:                 if ($groupmsg) {
                   2600:                     $groupmsg .= '</ul></li>';
1.260     raeburn  2601:                 }
                   2602:             }
                   2603:             if ($groupmsg) {
                   2604:                 $changemsg .= '<li>'.
                   2605:                               &mt('Courses with new groups').'</li>'.
                   2606:                               '<ul>'.$groupmsg.'</ul></li>';
                   2607:             }
                   2608:         }
                   2609:         if (keys(%newrole) > 0) {
1.286     raeburn  2610:             my $newmsg;
1.260     raeburn  2611:             foreach my $item (sort(keys(%newrole))) {
1.279     raeburn  2612:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2613:                 if ($desc) {
                   2614:                     $newmsg .= '<li>'.
                   2615:                                &mt('[_1] with status: [_2].',
1.293     bisitz   2616:                                $desc,&mt($newrole{$item})).'</li>';
1.286     raeburn  2617:                 }
                   2618:             }
                   2619:             if ($newmsg) {
                   2620:                 $changemsg .= '<li>'.&mt('New roles').
                   2621:                               '<ul>'.$newmsg.'</ul>'.
                   2622:                               '</li>';
1.260     raeburn  2623:             }
                   2624:         }
                   2625:         if (keys(%customprivchg) > 0) {
1.286     raeburn  2626:             my $privmsg;
1.260     raeburn  2627:             foreach my $item (sort(keys(%customprivchg))) {
1.279     raeburn  2628:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2629:                 if ($desc) {
                   2630:                     $privmsg .= '<li>'.$desc.'</li>';
                   2631:                 }
1.260     raeburn  2632:             }
1.286     raeburn  2633:             if ($privmsg) {
                   2634:                 $changemsg .= '<li>'.
                   2635:                               &mt('Custom roles with privilege changes').
                   2636:                               '<ul>'.$privmsg.'</ul>'.
                   2637:                               '</li>';
                   2638:              }
1.260     raeburn  2639:         }
                   2640:         if (keys(%rolechange) > 0) {
1.286     raeburn  2641:             my $rolemsg;
1.260     raeburn  2642:             foreach my $item (sort(keys(%rolechange))) {
1.279     raeburn  2643:                 my $desc = &role_desc($item,$update,$refresh,$now);  
1.286     raeburn  2644:                 if ($desc) {
                   2645:                     $rolemsg .= '<li>'.
                   2646:                                 &mt('[_1] status now: [_2].',$desc,
                   2647:                                 $rolechange{$item}).'</li>';
                   2648:                 }
                   2649:             }
                   2650:             if ($rolemsg) {
1.260     raeburn  2651:                 $changemsg .= '<li>'.
1.286     raeburn  2652:                               &mt('Existing roles with status changes').'</li>'.
                   2653:                               '<ul>'.$rolemsg.'</ul>'.
                   2654:                               '</li>';
1.260     raeburn  2655:             }
                   2656:         }
                   2657:         if (keys(%deletedroles) > 0) {
1.286     raeburn  2658:             my $delmsg;
1.260     raeburn  2659:             foreach my $item (sort(keys(%deletedroles))) {
1.279     raeburn  2660:                 my $desc = &role_desc($item,$update,$refresh,$now);
1.286     raeburn  2661:                 if ($desc) {
                   2662:                     $delmsg .= '<li>'.$desc.'</li>';
                   2663:                 }
                   2664:             }
                   2665:             if ($delmsg) {
                   2666:                 $changemsg .= '<li>'.
                   2667:                               &mt('Existing roles now expired').'</li>'.
                   2668:                               '<ul>'.$delmsg.'</ul>'.
                   2669:                               '</li>';
1.260     raeburn  2670:             }
                   2671:         }
                   2672:     }
                   2673:     if ((keys(%changed_groups) > 0) || (keys(%groupchange) > 0)) {
                   2674:         my $groupchgmsg;
                   2675:         foreach my $key (sort(keys(%changed_groups))) {
                   2676:             my $crs = 'gr/'.$key;
                   2677:             $crs =~ s/_/\//;
                   2678:             if (&is_active_course($crs,$refresh,$update,\%roleshash)) {
                   2679:                 if (ref($changed_groups{$key}) eq 'HASH') {
                   2680:                     my @showgroups;
                   2681:                     foreach my $group (sort(keys(%{$changed_groups{$key}}))) {
                   2682:                         if ($changed_groups{$key}{$group} eq 'active') {
                   2683:                             push(@showgroups,$group);
                   2684:                         }
                   2685:                     }
                   2686:                     if (@showgroups > 0) {
                   2687:                         $groupchgmsg .= '<li>'.
                   2688:                                         &mt('Course: [_1], groups: [_2].',$key,
                   2689:                                         join(', ',@showgroups)).
                   2690:                                         '</li>';
                   2691:                     }
                   2692:                 }
                   2693:             }
                   2694:         }
                   2695:         if (keys(%groupchange) > 0) {
                   2696:             $groupchgmsg .= '<li>'.
                   2697:                           &mt('Existing course/community groups with status changes').'</li>'.
                   2698:                           '<ul>';
                   2699:             foreach my $crs (sort(keys(%groupchange))) {
1.279     raeburn  2700:                 my $cid = $crs;
                   2701:                 $cid=~s{^/}{};
                   2702:                 $cid=~s{/}{_};
                   2703:                 my $crsdesc = $env{'course.'.$cid.'.description'};
                   2704:                 my $cdom = $env{'course.'.$cid.'.domain'};
                   2705:                 my $cnum = $env{'course.'.$cid.'.num'};
                   2706:                 my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
                   2707:                 my %groupdesc; 
1.260     raeburn  2708:                 if (ref($groupchange{$crs}) eq 'HASH') {
1.279     raeburn  2709:                     $groupchgmsg .= '<li>'.&mt('Course/Community: [_1]','<b>'.$crsdesc.'</b><ul>');
1.260     raeburn  2710:                     foreach my $group (sort(keys(%{$groupchange{$crs}}))) {
1.279     raeburn  2711:                         unless ($groupdesc{$group}) {
                   2712:                             my %groupinfo = &Apache::longroup::get_group_settings($curr_groups{$group});
                   2713:                             $groupdesc{$group} =  &unescape($groupinfo{'description'});
                   2714:                         }
                   2715:                         $groupchgmsg .= '<li>'.&mt('Group: [_1] status now: [_2].','<b>'.$groupdesc{$group}.'</b>',$groupchange{$crs}{$group}).'</li>';
1.260     raeburn  2716:                     }
                   2717:                     $groupchgmsg .= '</ul></li>';
                   2718:                 }
                   2719:             }
                   2720:             $groupchgmsg .= '</ul></li>';
                   2721:         }
                   2722:         if ($groupchgmsg) {
                   2723:             $changemsg .= '<li>'.
                   2724:                           &mt('Courses with changes in groups').'</li>'.
                   2725:                           '<ul>'.$groupchgmsg.'</ul></li>';
                   2726:         }
                   2727:     }
                   2728:     if ($changemsg) {
                   2729:         $msg .= '<ul>'.$changemsg.'</ul>';
1.286     raeburn  2730:     } else {
                   2731:         if (&Apache::loncommon::show_course()) {
                   2732:             $msg = &mt('No new courses or communities');
                   2733:         } else {
                   2734:             $msg = &mt('No role changes');
                   2735:         }
1.260     raeburn  2736:     }
1.279     raeburn  2737:     return $msg;
                   2738: }
                   2739: 
                   2740: sub role_desc {
                   2741:     my ($item,$update,$refresh,$now) = @_;
                   2742:     my ($where,$trolecode,$role,$tstatus,$tend,$tstart,$twhere,
                   2743:         $trole,$tremark);
1.282     raeburn  2744:     &Apache::lonnet::role_status('user.role.'.$item,$update,$refresh,
                   2745:                                  $now,\$role,\$where,\$trolecode,
1.279     raeburn  2746:                                  \$tstatus,\$tstart,\$tend);
1.286     raeburn  2747:     return unless ($role);
1.279     raeburn  2748:     if ($role =~ /^cr\//) {
                   2749:         my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.298     bisitz   2750:         $tremark = &mt('Custom role defined by [_1].',$rauthor.':'.$rdomain);
1.279     raeburn  2751:     }
                   2752:     $trole=Apache::lonnet::plaintext($role);
                   2753:     my ($tdom,$trest,$tsection)=
                   2754:         split(/\//,Apache::lonnet::declutter($where));
                   2755:     if (($role eq 'ca') || ($role eq 'aa')) {
                   2756:         my $home = &Apache::lonnet::homeserver($trest,$tdom);
                   2757:         $home = &Apache::lonnet::hostname($home);
                   2758:         $twhere=&mt('User').':&nbsp;'.$trest.'&nbsp; '.&mt('Domain').
                   2759:                 ':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').':&nbsp;'.$home;
                   2760:     } elsif ($role eq 'au') {
                   2761:         my $home = &Apache::lonnet::homeserver
                   2762:                        ($env{'user.name'},$env{'user.domain'});
                   2763:         $home = &Apache::lonnet::hostname($home);
                   2764:         $twhere=&mt('Domain').':&nbsp;'.$tdom.'&nbsp; '.&mt('Server').
                   2765:                         ':&nbsp;'.$home;
                   2766:     } elsif ($trest) {
                   2767:         my $tcourseid=$tdom.'_'.$trest;
                   2768:         my $crstype = &Apache::loncommon::course_type($tcourseid);
                   2769:         $trole = &Apache::lonnet::plaintext($role,$crstype,$tcourseid);
                   2770:         if ($env{'course.'.$tcourseid.'.description'}) {
                   2771:             $twhere=$env{'course.'.$tcourseid.'.description'};
                   2772:         } else {
                   2773:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   2774:             if (%newhash) {
                   2775:                 $twhere=$newhash{'description'};
                   2776:             } else {
                   2777:                 $twhere=&mt('Currently not available');
1.260     raeburn  2778:             }
                   2779:         }
1.279     raeburn  2780:         if ($tsection) {
                   2781:             $twhere.= '&nbsp; '.&mt('Section').':&nbsp;'.$tsection;
1.260     raeburn  2782:         }
1.279     raeburn  2783:         if ($role ne 'st') {
                   2784:             $twhere.= '&nbsp; '.&mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  2785:         }
1.279     raeburn  2786:     } elsif ($tdom) {
                   2787:         $twhere = &mt('Domain').':&nbsp;'.$tdom;
1.260     raeburn  2788:     }
1.286     raeburn  2789:     my $output;
                   2790:     if ($trole) {
                   2791:         $output = $trole;
                   2792:         if ($twhere) {
                   2793:             $output .= " -- $twhere";
                   2794:         }
                   2795:         if ($tremark) {
                   2796:             $output .= '<br />'.$tremark;
                   2797:         }
1.260     raeburn  2798:     }
1.279     raeburn  2799:     return $output;
1.260     raeburn  2800: }
                   2801: 
                   2802: sub curr_role_status {
                   2803:     my ($start,$end,$refresh,$update) = @_;
                   2804:     if (($start) && ($start<0)) { return 'deleted' };
                   2805:     my $status = 'active';
                   2806:     if (($end) && ($end<=$update)) {
                   2807:         $status = 'previous';
                   2808:     }
                   2809:     if (($start) && ($refresh<$start)) {
                   2810:         $status = 'future';
                   2811:     }
                   2812:     return $status;
                   2813: }
                   2814: 
                   2815: sub gather_roleprivs {
                   2816:     my ($allroles,$allgroups,$userroles,$area,$role,$tstart,$tend,$status) = @_;
                   2817:     return unless ((ref($allroles) eq 'HASH') && (ref($allgroups) eq 'HASH') && (ref($userroles) eq 'HASH'));
                   2818:     if (($area ne '') && ($role ne '')) {
                   2819:         &Apache::lonnet::userrolelog($role,$env{'user.name'},$env{'user.domain'},
                   2820:                                      $area,$tstart,$tend);
                   2821:         my $spec=$role.'.'.$area;
                   2822:         $userroles->{'user.role.'.$spec} = $tstart.'.'.$tend;
                   2823:         my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   2824:         if ($status eq 'active') { 
                   2825:             if ($role =~ /^cr\//) {
                   2826:                 &Apache::lonnet::custom_roleprivs($allroles,$role,$tdomain,$trest,$spec,$area);
                   2827:             } elsif ($role eq 'gr') {
                   2828:                 my %rolehash = &Apache::lonnet::get('roles',[$area.'_'.$role],
                   2829:                                                     $env{'user.domain'},
                   2830:                                                     $env{'user.name'});
                   2831:                 my ($trole) = split(/_/,$rolehash{$area.'_'.$role},2);
                   2832:                 (undef,my $group_privs) = split(/\//,$trole);
                   2833:                 $group_privs = &unescape($group_privs);
                   2834:                 &Apache::lonnet::group_roleprivs($allgroups,$area,$group_privs,$tend,$tstart);
                   2835:             } else {
                   2836:                 &Apache::lonnet::standard_roleprivs($allroles,$role,$tdomain,$spec,$trest,$area);
                   2837:             }
                   2838:         }
                   2839:     }
                   2840:     return;
                   2841: }
                   2842: 
                   2843: sub is_active_course {
                   2844:     my ($rolekey,$refresh,$update,$roleshashref) = @_;
                   2845:     return unless(ref($roleshashref) eq 'HASH');
                   2846:     my ($role,$cdom,$cnum) = split(/\//,$rolekey);
                   2847:     my $is_active;
                   2848:     foreach my $key (keys(%{$roleshashref})) {
                   2849:         if ($key =~ /^\Q$cnum\E:\Q$cdom\E:/) {
                   2850:             my ($tstart,$tend) = split(/:/,$roleshashref->{$key});
                   2851:             my $status = &curr_role_status($tstart,$tend,$refresh,$update);
                   2852:             if ($status eq 'active') {
                   2853:                 $is_active = 1;
                   2854:                 last;
                   2855:             }
                   2856:         }
                   2857:     }
                   2858:     return $is_active;
                   2859: }
                   2860: 
1.274     raeburn  2861: sub get_roles_functions {
1.302     raeburn  2862:     my ($rolescount,$cattype) = @_;
1.274     raeburn  2863:     my @links;
                   2864:     push(@links,["javascript:rolesView('doupdate');",'start-here-22x22',&mt('Check for changes')]);
                   2865:     if ($env{'environment.canrequest.author'}) {
                   2866:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   2867:             push(@links,["javascript:rolesView('requestauthor');",'list-add-22x22',&mt('Request author role')]);
                   2868:         }
                   2869:     }
1.279     raeburn  2870:     if (($rolescount > 3) || ($env{'environment.recentroles'})) {
                   2871:         push(@links,['/adm/preferences?action=changerolespref&amp;returnurl=/adm/roles','role_hotlist-22x22',&mt('Hotlist')]);
                   2872:     }
1.274     raeburn  2873:     if (&Apache::lonmenu::check_for_rcrs()) {
                   2874:         push(@links,['/adm/requestcourse','rcrs-22x22',&mt('Request course')]);
                   2875:     }
                   2876:     if ($env{'form.state'} eq 'queued') {
                   2877:         push(@links,["javascript:rolesView('noqueued');",'selfenrl-queue-22x22',&mt('Hide queued')]);
                   2878:     } else {
                   2879:         push(@links,["javascript:rolesView('queued');",'selfenrl-queue-22x22',&mt('Show queued')]);
                   2880:     }
1.279     raeburn  2881:     if ($env{'user.adv'}) {
                   2882:         if ($env{'form.display'} eq 'showall') {
1.290     raeburn  2883:             push(@links,["javascript:rolesView('noshowall');",'edit-redo-22x22',&mt('Exclude expired')]);
1.279     raeburn  2884:         } else {
1.290     raeburn  2885:             push(@links,["javascript:rolesView('showall');",'edit-undo-22x22',&mt('Include expired')]);
1.279     raeburn  2886:         }
1.274     raeburn  2887:     }
1.302     raeburn  2888:     unless ($cattype eq 'none') {
1.291     raeburn  2889:         push(@links,['/adm/coursecatalog','ccat-22x22',&mt('Course catalog')]);
1.290     raeburn  2890:     }
1.314     raeburn  2891:     my $funcs;
                   2892:     if ($env{'browser.mobile'}) {
                   2893:         my @functions;
                   2894:         foreach my $link (@links) {
                   2895:             push(@functions,[$link->[0],$link->[2]]);
                   2896:         }
                   2897:         my $title = 'Display options';
                   2898:         if ($env{'user.adv'}) {
                   2899:             $title = 'Roles options';
                   2900:         }
                   2901:         $funcs = &Apache::lonmenu::create_submenu('','',$title,\@functions,1,'LC_breadcrumbs_hoverable');
                   2902:         $funcs = '<ol class="LC_primary_menu LC_floatright">'.$funcs.'</ol>';
                   2903:     } else {
                   2904:         $funcs = &Apache::lonhtmlcommon::start_funclist();
                   2905:         foreach my $link (@links) {
                   2906:             $funcs .= &Apache::lonhtmlcommon::add_item_funclist(
                   2907:                           '<a href="'.$link->[0].'" class="LC_menubuttons_link">'.
                   2908:                           '<img src="/res/adm/pages/'.$link->[1].'.png" class="LC_icon" alt="'.$link->[2].'" />'.
                   2909:                           $link->[2].'</a>');
                   2910:         }
                   2911:         $funcs .= &Apache::lonhtmlcommon::end_funclist();
                   2912:         $funcs = &Apache::loncommon::head_subbox($funcs);
1.274     raeburn  2913:     }
1.314     raeburn  2914:     return $funcs;
1.274     raeburn  2915: }
                   2916: 
                   2917: sub get_queued {
                   2918:     my ($output,%reqcrs);
                   2919:     my ($types,$typenames) = &Apache::loncommon::course_types();
                   2920:     my %statusinfo = &Apache::lonnet::dump('courserequests',$env{'user.domain'},
                   2921:                                            $env{'user.name'},'^status:');
                   2922:     foreach my $key (keys(%statusinfo)) {
                   2923:         next unless (($statusinfo{$key} eq 'approval') || ($statusinfo{$key} eq 'pending'));
1.297     raeburn  2924:         (undef,my($cdom,$cnum)) = split(/:/,$key);
1.274     raeburn  2925:         my $requestkey = $cdom.'_'.$cnum;
                   2926:         if ($requestkey =~ /^($match_domain)_($match_courseid)$/) {
                   2927:             my %history = &Apache::lonnet::restore($requestkey,'courserequests',
                   2928:                                                    $env{'user.domain'},$env{'user.name'});
                   2929:             next if ((exists($history{'status'})) && ($history{'status'} eq 'created'));
                   2930:             my $reqtime = $history{'reqtime'};
                   2931:             my $lastupdate = $history{'timestamp'};
                   2932:             my $showtype = $history{'crstype'};
                   2933:             if (defined($typenames->{$history{'crstype'}})) {
                   2934:                 $showtype = $typenames->{$history{'crstype'}};
                   2935:             }
                   2936:             my $description;
                   2937:             if (ref($history{'details'}) eq 'HASH') {
                   2938:                 $description = $history{details}{'cdescr'};
                   2939:             }
                   2940:             @{$reqcrs{$reqtime}} = ($description,$showtype); 
                   2941:         }
                   2942:     }
                   2943:     my @sortedtimes = sort {$a <=> $b} (keys(%reqcrs));
                   2944:     if (@sortedtimes > 0) {
                   2945:         $output .= '<p><b>'.&mt('Course/Community requests').'</b><br />'.
                   2946:                    &Apache::loncommon::start_data_table().
                   2947:                    &Apache::loncommon::start_data_table_header_row().
                   2948:                    '<th>'.&mt('Date requested').'</th>'.
                   2949:                    '<th>'.&mt('Course title').'</th>'.
                   2950:                    '<th>'.&mt('Course type').'</th>';
                   2951:                    &Apache::loncommon::end_data_table_header_row();
                   2952:         foreach my $reqtime (@sortedtimes) {
                   2953:             next unless (ref($reqcrs{$reqtime}) eq 'ARRAY');
                   2954:             $output .= &Apache::loncommon::start_data_table_row().
                   2955:                        '<td>'.&Apache::lonlocal::locallocaltime($reqtime).'</td>'.
                   2956:                        '<td>'.join('</td><td>',@{$reqcrs{$reqtime}}).'</td>'.
                   2957:                        &Apache::loncommon::end_data_table_row();
                   2958:         }
                   2959:         $output .= &Apache::loncommon::end_data_table().
                   2960:                    '<br /></p>';
                   2961:     }
                   2962:     my $queuedselfenroll = &Apache::loncoursequeueadmin::queued_selfenrollment(1);
                   2963:     if ($queuedselfenroll) {
                   2964:         $output .= '<p><b>'.&mt('Enrollment requests').'</b><br />'.
                   2965:                    $queuedselfenroll.'<br /></p>';
                   2966:     }
                   2967:     if ($env{'environment.canrequest.author'}) {
                   2968:         unless (&Apache::loncoursequeueadmin::is_active_author()) {
                   2969:             my $requestauthor;
                   2970:             my ($status,$timestamp) = split(/:/,$env{'environment.requestauthorqueued'});
                   2971:             if (($status eq 'approval') || ($status eq 'approved')) {
                   2972:                 $output .= '<p><b>'.&mt('Author role request').'</b><br />';
                   2973:                 if ($status eq 'approval') {
1.294     bisitz   2974:                     $output .= &mt('A request for Authoring Space submitted on [_1] is awaiting approval',
1.274     raeburn  2975:                                   &Apache::lonlocal::locallocaltime($timestamp));
                   2976:                 } elsif ($status eq 'approved') {
                   2977:                     my %roleshash =
                   2978:                         &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
                   2979:                                                       ['active'],['au'],[$env{'user.domain'}]);
                   2980:                     if (keys(%roleshash)) {
                   2981:                         $output .= '<span class="LC_info">'.
                   2982:                                    &mt('Your request for an author role has been approved.').'<br />'.
                   2983:                                    &mt('Use the "Check for changes" link to update your list of roles.').
                   2984:                                    '</span>';
                   2985:                     }
                   2986:                 }
                   2987:                 $output .= '</p>';
                   2988:             }
                   2989:         }
                   2990:     }
                   2991:     unless ($output) {
                   2992:         if ($env{'environment.canrequest.author'} || $env{'environment.canrequest.official'} ||
                   2993:             $env{'environment.canrequest.unofficial'} || $env{'environment.canrequest.community'}) {
                   2994:             $output = &mt('No requests for courses, communities or authoring currently queued');
                   2995:         } else {
                   2996:             $output = &mt('No enrollment requests currently queued awaiting approval');
                   2997:         }
                   2998:     }
                   2999:     return '<div class="LC_left_float"><fieldset><legend>'.&mt('Queued requests').'</legend>'.
                   3000:            $output.'</fieldset></div><br clear="all" />';
                   3001: }
                   3002: 
1.1       harris41 3003: 1;
                   3004: __END__
1.32      harris41 3005: 
                   3006: =head1 NAME
                   3007: 
                   3008: Apache::lonroles - User Roles Screen
                   3009: 
                   3010: =head1 SYNOPSIS
                   3011: 
                   3012: Invoked by /etc/httpd/conf/srm.conf:
                   3013: 
                   3014:  <Location /adm/roles>
                   3015:  PerlAccessHandler       Apache::lonacc
                   3016:  SetHandler perl-script
                   3017:  PerlHandler Apache::lonroles
                   3018:  ErrorDocument     403 /adm/login
                   3019:  ErrorDocument	  500 /adm/errorhandler
                   3020:  </Location>
1.64      bowersj2 3021: 
                   3022: =head1 OVERVIEW
                   3023: 
                   3024: =head2 Choosing Roles
                   3025: 
                   3026: C<lonroles> is a handler that allows a user to switch roles in
                   3027: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   3028: default role that a user has before selecting a role, as widely as
                   3029: possible, but certain handlers for example need specification which
                   3030: course they should act on, etc. Both in this scenario, and when the
                   3031: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   3032: action is not allowed, C<lonroles> is used as error handler. This
                   3033: allows the user to select another role which may have permission to do
1.246     droeschl 3034: what they were trying to do.
1.64      bowersj2 3035: 
                   3036: =begin latex
                   3037: 
                   3038: \begin{figure}
                   3039: \begin{center}
                   3040: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   3041:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   3042: \end{center}
                   3043: \end{figure}
                   3044: 
                   3045: =end latex
                   3046: 
                   3047: =head2 Role Initialization
                   3048: 
                   3049: The privileges for a user are established at login time and stored in the session environment. As a consequence, a new role does not become active till the next login. Handlers are able to query for privileges using C<lonnet>'s C<&allowed> function. When a user first logs in, their role is the "common" role, which means that they have the sum of all of their privileges. During a session it might become necessary to choose a particular role, which as a consequence also limits the user to only the privileges in that particular role.
1.32      harris41 3050: 
                   3051: =head1 INTRODUCTION
                   3052: 
                   3053: This module enables a user to select what role he wishes to
                   3054: operate under (instructor, student, teaching assistant, course
                   3055: coordinator, etc).  These roles are pre-established by the actions
                   3056: of upper-level users.
                   3057: 
                   3058: This is part of the LearningOnline Network with CAPA project
                   3059: described at http://www.lon-capa.org.
                   3060: 
                   3061: =head1 HANDLER SUBROUTINE
                   3062: 
                   3063: This routine is called by Apache and mod_perl.
                   3064: 
                   3065: =over 4
                   3066: 
                   3067: =item *
                   3068: 
                   3069: Roles Initialization (yes/no)
                   3070: 
                   3071: =item *
                   3072: 
                   3073: Get Error Message from Environment
                   3074: 
                   3075: =item *
                   3076: 
                   3077: Who is this?
                   3078: 
                   3079: =item *
                   3080: 
                   3081: Generate Page Output
                   3082: 
                   3083: =item *
                   3084: 
                   3085: Choice or no choice
                   3086: 
                   3087: =item *
                   3088: 
                   3089: Table
                   3090: 
                   3091: =item *
                   3092: 
                   3093: Privileges
                   3094: 
                   3095: =back
                   3096: 
                   3097: =cut

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