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

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

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