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

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

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