File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.318: download - view: text, annotated - select for diffs
Thu Oct 20 19:53:58 2016 UTC (7 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Links to ad hoc roles for DC and DH roles in Roles Hotlist.

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

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