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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.193   ! raeburn     4: # $Id: lonroles.pm,v 1.192 2008/05/15 01:00:37 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.1       harris41   30: package Apache::lonroles;
                     31: 
                     32: use strict;
1.118     albertel   33: use Apache::lonnet;
1.7       www        34: use Apache::lonuserstate();
1.1       harris41   35: use Apache::Constants qw(:common);
1.2       www        36: use Apache::File();
1.26      www        37: use Apache::lonmenu;
1.29      albertel   38: use Apache::loncommon;
1.104     raeburn    39: use Apache::lonhtmlcommon;
1.57      www        40: use Apache::lonannounce;
1.72      www        41: use Apache::lonlocal;
1.151     www        42: use Apache::lonpageflip();
1.167     albertel   43: use Apache::lonnavdisplay();
1.120     albertel   44: use GDBM_File;
1.170     albertel   45: use LONCAPA qw(:DEFAULT :match);
1.149     www        46:  
1.1       harris41   47: 
1.62      matthew    48: sub redirect_user {
1.95      albertel   49:     my ($r,$title,$url,$msg,$launch_nav) = @_;
1.62      matthew    50:     $msg = $title if (! defined($msg));
1.73      www        51:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew    52:     &Apache::loncommon::no_cache($r);
                     53:     $r->send_http_header;
                     54:     my $swinfo=&Apache::lonmenu::rawconfig();
1.96      albertel   55:     my $navwindow;
1.95      albertel   56:     if ($launch_nav eq 'on') {
1.166     albertel   57: 	$navwindow.=&Apache::lonnavdisplay::launch_win('now',undef,undef,
                     58: 						       ($url =~ m-^/adm/whatsnew-));
1.96      albertel   59:     } else {
                     60: 	$navwindow.=&Apache::lonnavmaps::close();
1.95      albertel   61:     }
1.147     albertel   62:     my $start_page = &Apache::loncommon::start_page('Switching Role',undef,
                     63: 						    {'redirect' => [1,$url],});
                     64:     my $end_page   = &Apache::loncommon::end_page();
                     65: 
1.92      www        66: # Note to style police: 
                     67: # This must only replace the spaces, nothing else, or it bombs elsewhere.
                     68:     $url=~s/ /\%20/g;
1.93      albertel   69:     $r->print(<<ENDREDIR);
1.147     albertel   70: $start_page
1.96      albertel   71: <script type="text/javascript">
1.62      matthew    72: $swinfo
                     73: </script>
1.96      albertel   74: $navwindow
1.62      matthew    75: <h1>$msg</h1>
1.147     albertel   76: $end_page
1.62      matthew    77: ENDREDIR
                     78:     return;
                     79: }
                     80: 
1.150     www        81: sub error_page {
                     82:     my ($r,$error,$dest)=@_;
                     83:     &Apache::loncommon::content_type($r,'text/html');
                     84:     &Apache::loncommon::no_cache($r);
                     85:     $r->send_http_header;
                     86:     return OK if $r->header_only;
                     87:     $r->print(&Apache::loncommon::start_page('Problems during Course Initialization').
                     88: 	      '<script type="text/javascript">'.
                     89: 	      &Apache::lonmenu::rawconfig().'</script>'.
                     90: 	      '<p>'.&mt('The following problems occurred:').
                     91: 	      $error.
1.156     albertel   92: 	      '</p><br /><a href="'.$dest.'">'.&mt('Continue').'</a>'.
1.150     www        93: 	      &Apache::loncommon::end_page());
                     94: }
                     95: 
1.1       harris41   96: sub handler {
1.10      www        97: 
1.1       harris41   98:     my $r = shift;
                     99: 
1.6       www       100:     my $now=time;
1.118     albertel  101:     my $then=$env{'user.login.time'};
1.6       www       102:     my $envkey;
1.107     raeburn   103:     my %dcroles = ();
                    104:     my $numdc = &check_fordc(\%dcroles,$then);
1.148     albertel  105:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.10      www       106: 
1.6       www       107: # ================================================================== Roles Init
1.118     albertel  108:     if ($env{'form.selectrole'}) {
1.188     www       109: 
                    110:         my $locknum=&Apache::lonnet::get_locks();
                    111:         if ($locknum) { return 409; }
                    112: 
1.134     www       113:         if ($env{'form.newrole'}) {
                    114:             $env{'form.'.$env{'form.newrole'}}=1;
                    115: 	}
1.118     albertel  116: 	if ($env{'request.course.id'}) {
1.185     raeburn   117:             # Check if user is CC trying to select a course role
                    118:             if ($env{'form.switchrole'}) {
                    119:                 if (!defined($env{'user.role.'.$env{'form.switchrole'}})) {
                    120:                     &adhoc_course_role($then);
                    121:                 }
                    122:             }
1.118     albertel  123: 	    my %temp=('logout_'.$env{'request.course.id'} => time);
1.33      www       124: 	    &Apache::lonnet::put('email_status',\%temp);
1.118     albertel  125: 	    &Apache::lonnet::delenv('user.state.'.$env{'request.course.id'});
1.100     albertel  126: 	}
1.186     raeburn   127: 	&Apache::lonnet::appenv({"request.course.id"   => '',
                    128: 			 	 "request.course.fn"   => '',
                    129: 				 "request.course.uri"  => '',
                    130: 				 "request.course.sec"  => '',
                    131: 				 "request.role"        => 'cm',
                    132:                                  "request.role.adv"    => $env{'user.adv'},
                    133: 				 "request.role.domain" => $env{'user.domain'}});
1.182     www       134: # Check if user is a DC trying to enter a course or author space and needs privs to be created
1.107     raeburn   135:         if ($numdc > 0) {
1.118     albertel  136:             foreach my $envkey (keys %env) {
1.182     www       137: # Is this an ad-hoc CC-role?
1.146     raeburn   138:                 if (my ($domain,$coursenum) =
1.171     albertel  139: 		    ($envkey =~ m-^form\.cc\./($match_domain)/($match_courseid)$-)) {
1.189     www       140:                    # See if that is even allowed
1.190     www       141:                    my %crsenv=&Apache::lonnet::get('environment',['internal.courseowner'],$domain,$coursenum);
                    142:                    # First find course owner
                    143:                    my ($owneruser,$ownerdomain)=split(/\:/,$crsenv{'internal.courseowner'});
                    144:                    # Check if course owner blocked cc-access
                    145:                     if (($owneruser) && ($ownerdomain)) {
                    146:                        my %blocked=&Apache::lonnet::get('environment',['domcoord.cc'],$ownerdomain,$owneruser);
                    147:                        if ($blocked{'domcoord.cc'} eq 'blocked') {
                    148:                           $env{'user.error.msg'}=':::1:Course owner '.$owneruser.' in domain '.$ownerdomain.' blocked domain coordinator access';
                    149:                           last;
                    150:                        }
                    151:                     }
1.146     raeburn   152:                     if ($dcroles{$domain}) {
1.182     www       153:                         &check_privs($domain,$coursenum,$then,$now,'cc');
                    154:                     }
                    155:                     last;
                    156:                 }
1.193   ! raeburn   157: # Is this an ad-hoc CA-role?
1.183     www       158:                 if (my ($domain,$user) =
                    159: 		    ($envkey =~ m-^form\.ca\./($match_domain)/($match_username)$-)) {
1.193   ! raeburn   160:                    # Check if author blocked ca-access
1.190     www       161:                     my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],$domain,$user);
                    162:                     if ($blocked{'domcoord.author'} eq 'blocked') {
                    163:                        delete($env{$envkey});
                    164:                        $env{'user.error.msg'}=':::1:User '.$user.' in domain '.$domain.' blocked domain coordinator access';
                    165:                        last;
                    166:                     }
1.193   ! raeburn   167:                     if ($dcroles{$domain}) {
        !           168:                         my ($server_status,$home) = &check_author_homeserver($user,$domain);
        !           169:                         if (($server_status eq 'ok') || ($server_status eq 'switchserver')) {
        !           170:                             &check_privs($domain,$user,$then,$now,'ca');
        !           171:                             if ($server_status eq 'switchserver') {
        !           172:                                 my $trolecode = 'ca./'.$domain.'/'.$user; 
        !           173:                                 my $switchserver = '/adm/switchserver?'
        !           174:                                                   .'otherserver='.$home.'&role='.$trolecode;
        !           175:                                 $r->internal_redirect($switchserver);
        !           176:                             }
        !           177:                         } else {
        !           178:                             delete($env{$envkey});
        !           179:                         }
1.183     www       180:                     } else {
                    181:                         delete($env{$envkey});
1.182     www       182:                     }
                    183:                     last;
                    184:                 }
1.107     raeburn   185:             }
                    186:         }
                    187: 
1.118     albertel  188:         foreach $envkey (keys %env) {
1.40      matthew   189:             next if ($envkey!~/^user\.role\./);
1.102     raeburn   190:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
                    191:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.118     albertel  192:             if ($env{'form.'.$trolecode}) {
1.55      albertel  193: 		if ($tstatus eq 'is') {
                    194: 		    $where=~s/^\///;
                    195: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.137     raeburn   196: # check for course groups
                    197:                     my %coursegroups = &Apache::lonnet::get_active_groups(
                    198:                           $env{'user.domain'},$env{'user.name'},$cdom, $cnum);
                    199:                     my $cgrps = join(':',keys(%coursegroups));
                    200: 
1.111     albertel  201: # store role if recent_role list being kept
1.118     albertel  202:                     if ($env{'environment.recentroles'}) {
1.158     albertel  203:                         my %frozen_roles =
                    204:                            &Apache::lonhtmlcommon::get_recent_frozen('roles',$env{'environment.recentrolesn'});
1.111     albertel  205: 			&Apache::lonhtmlcommon::store_recent('roles',
1.158     albertel  206: 							     $trolecode,' ',$frozen_roles{$trolecode});
1.111     albertel  207:                     }
                    208: 
                    209: 
1.53      www       210: # check for keyed access
1.55      albertel  211: 		    if (($role eq 'st') && 
1.118     albertel  212:                        ($env{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
1.89      www       213: # who is key authority?
                    214: 			my $authdom=$cdom;
                    215: 			my $authnum=$cnum;
1.118     albertel  216: 			if ($env{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
1.89      www       217: 			    ($authnum,$authdom)=
1.172     albertel  218: 				split(/:/,$env{'course.'.$cdom.'_'.$cnum.'.keyauth'});
1.89      www       219: 			}
                    220: # check with key authority
                    221: 			unless (&Apache::lonnet::validate_access_key(
1.118     albertel  222: 				     $env{'environment.key.'.$cdom.'_'.$cnum},
1.89      www       223: 					     $authdom,$authnum)) {
1.53      www       224: # there is no valid key
1.118     albertel  225: 			     if ($env{'form.newkey'}) {
1.53      www       226: # student attempts to register a new key
1.89      www       227: 				 &Apache::loncommon::content_type($r,'text/html');
                    228: 				 &Apache::loncommon::no_cache($r);
                    229: 				 $r->send_http_header;
                    230: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  231: 				 my $start_page=&Apache::loncommon::start_page
1.89      www       232: 				    ('Verifying Access Key to Unlock this Course');
1.147     albertel  233: 				 my $end_page=&Apache::loncommon::end_page();
1.90      www       234: 				 my $buttontext=&mt('Enter Course');
                    235: 				 my $message=&mt('Successfully registered key');
                    236: 				 my $assignresult=
                    237: 				     &Apache::lonnet::assign_access_key(
1.118     albertel  238: 						     $env{'form.newkey'},
1.90      www       239: 						     $authdom,$authnum,
1.91      www       240: 						     $cdom,$cnum,
1.118     albertel  241:                                                      $env{'user.domain'},
                    242: 						     $env{'user.name'},
1.90      www       243: 	      'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
                    244:                                                      $trolecode);
                    245: 				 unless ($assignresult eq 'ok') {
                    246: 				     $assignresult=~s/^error\:\s*//;
                    247: 				     $message=&mt($assignresult).
                    248: 				     '<br /><a href="/adm/logout">'.
1.89      www       249: 				     &mt('Logout').'</a>';
1.90      www       250: 				     $buttontext=&mt('Re-Enter Key');
                    251: 				 }
1.89      www       252: 				 $r->print(<<ENDENTEREDKEY);
1.147     albertel  253: $start_page
1.179     raeburn   254: <script type="text/javascript">
1.89      www       255: $swinfo
                    256: </script>
                    257: <form method="post">
                    258: <input type="hidden" name="selectrole" value="1" />
                    259: <input type="hidden" name="$trolecode" value="1" />
1.90      www       260: <font size="+2">$message</font><br />
1.89      www       261: <input type="submit" value="$buttontext" />
                    262: </form>
1.147     albertel  263: $end_page
1.89      www       264: ENDENTEREDKEY
                    265:                                  return OK;
1.55      albertel  266: 			     } else {
1.53      www       267: # print form to enter a new key
1.73      www       268: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  269: 				 &Apache::loncommon::no_cache($r);
                    270: 				 $r->send_http_header;
                    271: 				 my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  272: 				 my $start_page=&Apache::loncommon::start_page
1.55      albertel  273: 				    ('Enter Access Key to Unlock this Course');
1.147     albertel  274: 				 my $end_page=&Apache::loncommon::end_page();
1.55      albertel  275: 				 $r->print(<<ENDENTERKEY);
1.147     albertel  276: $start_page
1.179     raeburn   277: <script type="text/javascript">
1.53      www       278: $swinfo
                    279: </script>
                    280: <form method="post">
1.89      www       281: <input type="hidden" name="selectrole" value="1" />
                    282: <input type="hidden" name="$trolecode" value="1" />
1.118     albertel  283: <input type="text" size="20" name="newkey" value="$env{'form.newkey'}" />
1.53      www       284: <input type="submit" value="Enter key" />
                    285: </form>
1.147     albertel  286: $end_page
1.53      www       287: ENDENTERKEY
1.55      albertel  288: 				 return OK;
                    289: 			     }
                    290: 			 }
                    291: 		     }
1.118     albertel  292: 		    &Apache::lonnet::log($env{'user.domain'},
                    293: 					 $env{'user.name'},
                    294: 					 $env{'user.home'},
1.87      www       295: 					 "Role ".$trolecode);
1.101     albertel  296: 		    
1.56      www       297: 		    &Apache::lonnet::appenv(
1.186     raeburn   298: 					   {'request.role'        => $trolecode,
                    299: 					    'request.role.domain' => $cdom,
                    300: 					    'request.course.sec'  => $csec,
                    301:                                             'request.course.groups' => $cgrps});
1.101     albertel  302:                     my $tadv=0;
1.62      matthew   303: 
1.125     www       304: 		    if (($cnum) && ($role ne 'ca') && ($role ne 'aa')) {
1.152     raeburn   305:                         my $msg;
1.55      albertel  306: 			my ($furl,$ferr)=
                    307: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
1.118     albertel  308: 			if (($env{'form.orgurl'}) && 
                    309: 			    ($env{'form.orgurl'}!~/^\/adm\/flip/)) {
                    310: 			    my $dest=$env{'form.orgurl'};
1.117     albertel  311: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   312: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.150     www       313:                             if (($ferr) && ($tadv)) {
                    314: 				&error_page($r,$ferr,$dest);
                    315: 			    } else {
                    316: 				$r->internal_redirect($dest);
                    317: 			    }
1.55      albertel  318: 			    return OK;
                    319: 			} else {
1.155     albertel  320: 			    if (!$env{'request.course.id'}) {
1.55      albertel  321: 				&Apache::lonnet::appenv(
1.186     raeburn   322: 				      {"request.course.id"  => $cdom.'_'.$cnum});
1.61      www       323: 				$furl='/adm/roles?tryagain=1';
1.55      albertel  324: 				$msg=
1.157     albertel  325: 				    '<h1><span class="LC_error">'.
1.162     albertel  326: 				    &mt('Could not initialize [_1] at this time.',
                    327: 					$env{'course.'.$cdom.'_'.$cnum.'.description'}).
1.157     albertel  328: 				    '</span></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
1.55      albertel  329: 			    }
1.117     albertel  330: 			    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn   331: 			    &Apache::lonnet::appenv({'request.role.adv'=>$tadv});
1.152     raeburn   332: 
1.150     www       333: 			    if (($ferr) && ($tadv)) {
                    334: 				&error_page($r,$ferr,$furl);
                    335: 			    } else {
                    336: 				# Check to see if the user is a CC entering a course 
                    337: 				# for the first time
                    338: 				my (undef, undef, $role, $courseid) = split(/\./, $envkey);
                    339: 				if (substr($courseid, 0, 1) eq '/') {
                    340: 				    $courseid = substr($courseid, 1);
                    341: 				}
                    342: 				$courseid =~ s/\//_/;
                    343: 				if ($role eq 'cc' && $env{'course.' . $courseid . 
                    344: 							      '.course.helper.not.run'}) {
                    345: 				    $furl = "/adm/helper/course.initialization.helper";
                    346: 				    # Send the user to the course they selected
                    347: 				} elsif ($env{'request.course.id'}) {
1.185     raeburn   348:                                     if ($env{'form.destinationurl'}) {
                    349:                                         my $dest = $env{'form.destinationurl'};
                    350:                                         &redirect_user($r,&mt('Entering [_1]',
                    351:                                                       $env{'course.'.$courseid.'.description'}),
                    352:                                                $dest,$msg,
                    353:                                                $env{'environment.remotenavmap'});
                    354:                                         return OK;
                    355:                                     }
1.150     www       356: 				    if (&Apache::lonnet::allowed('whn',
                    357: 								 $env{'request.course.id'})
                    358: 					|| &Apache::lonnet::allowed('whn',
                    359: 								    $env{'request.course.id'}.'/'
                    360: 								    .$env{'request.course.sec'})
                    361: 					) {
                    362: 					my $startpage = &courseloadpage($courseid);
                    363: 					unless ($startpage eq 'firstres') {         
1.162     albertel  364: 					    $msg = &mt('Entering [_1] ....',
                    365: 						       $env{'course.'.$courseid.'.description'});
1.150     www       366: 					    &redirect_user($r,&mt('New in course'),
                    367: 							   '/adm/whatsnew?refpage=start',$msg,
                    368: 							   $env{'environment.remotenavmap'});
                    369: 					    return OK;
                    370: 					}
                    371: 				    }
                    372: 				}
1.151     www       373: # Are we allowed to look at the first resource?
1.169     albertel  374: 				if ($furl !~ m|^/adm/|) {
1.151     www       375: # Guess not ...
                    376: 				    $furl=&Apache::lonpageflip::first_accessible_resource();
                    377: 				}
1.162     albertel  378:                                 $msg = &mt('Entering [_1] ...',
                    379: 					   $env{'course.'.$courseid.'.description'});
                    380: 				&redirect_user($r,&mt('Entering [_1]',
                    381: 						      $env{'course.'.$courseid.'.description'}),
1.150     www       382: 					       $furl,$msg,
                    383: 					       $env{'environment.remotenavmap'});
1.58      bowersj2  384: 			    }
1.124     albertel  385: 			    return OK;
1.55      albertel  386: 			}
                    387: 		    }
1.62      matthew   388:                     #
                    389:                     # Send the user to the construction space they selected
1.125     www       390:                     if ($role =~ /^(au|ca|aa)$/) {
1.62      matthew   391:                         my $redirect_url = '/priv/';
                    392:                         if ($role eq 'au') {
1.118     albertel  393:                             $redirect_url.=$env{'user.name'};
1.62      matthew   394:                         } else {
                    395:                             $where =~ /\/(.*)$/;
                    396:                             $redirect_url .= $1;
                    397:                         }
                    398:                         $redirect_url .= '/';
1.78      sakharuk  399:                         &redirect_user($r,&mt('Entering Construction Space'),
1.62      matthew   400:                                        $redirect_url);
                    401:                         return OK;
                    402:                     }
1.104     raeburn   403:                     if ($role eq 'dc') {
1.108     raeburn   404:                         my $redirect_url = '/adm/menu/';
                    405:                         &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
1.104     raeburn   406:                                        $redirect_url);
1.108     raeburn   407:                         return OK;
1.104     raeburn   408:                     }
1.55      albertel  409: 		}
                    410:             }
1.6       www       411:         }
1.40      matthew   412:     }
1.44      www       413: 
1.10      www       414: 
1.6       www       415: # =============================================================== No Roles Init
1.10      www       416: 
1.73      www       417:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  418:     &Apache::loncommon::no_cache($r);
1.10      www       419:     $r->send_http_header;
                    420:     return OK if $r->header_only;
                    421: 
1.52      www       422:     my $swinfo=&Apache::lonmenu::rawconfig();
1.147     albertel  423:     my $start_page=&Apache::loncommon::start_page('User Roles');
1.134     www       424:     my $standby=&mt('Role selected. Please stand by.');
1.135     albertel  425:     $standby=~s/\n/\\n/g;
1.184     raeburn   426:     my $noscript='<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       427: 
1.10      www       428:     $r->print(<<ENDHEADER);
1.147     albertel  429: $start_page
1.163     www       430: <br />
1.179     raeburn   431: <noscript>
                    432: $noscript
                    433: </noscript>
                    434: <script type="text/javascript">
1.26      www       435: $swinfo
                    436: window.focus();
1.134     www       437: 
                    438: active=true;
                    439: 
                    440: function enterrole (thisform,rolecode,buttonname) {
                    441:     if (active) {
                    442: 	active=false;
                    443:         document.title='$standby';
                    444:         window.status='$standby';
                    445: 	thisform.newrole.value=rolecode;
                    446: 	thisform.submit();
                    447:     } else {
                    448:        alert('$standby');
                    449:     }   
                    450: }
1.26      www       451: </script>
1.10      www       452: ENDHEADER
1.6       www       453: 
1.2       www       454: # ------------------------------------------ Get Error Message from Environment
                    455: 
1.118     albertel  456:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$env{'user.error.msg'});
                    457:     if ($env{'user.error.msg'}) {
1.55      albertel  458: 	$r->log_reason(
1.118     albertel  459:    "$msg for $env{'user.name'} domain $env{'user.domain'} access $priv",$fn);
1.12      www       460:     }
1.1       harris41  461: 
1.61      www       462: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       463: 
1.118     albertel  464:     my $advanced=$env{'user.adv'};
1.61      www       465:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
1.118     albertel  466:     my $tryagain=$env{'form.tryagain'};
1.6       www       467: 
1.2       www       468: # -------------------------------------------------------- Generate Page Output
1.6       www       469: # --------------------------------------------------------------- Error Header?
1.2       www       470:     if ($error) {
1.187     bisitz    471:         $r->print("<h1>".&mt('LON-CAPA Access Control')."</h1>");
1.174     albertel  472: 	$r->print("<!-- LONCAPAACCESSCONTROLERRORSCREEN --><hr /><pre>");
                    473: 	if ($priv ne '') {
1.187     bisitz    474:             $r->print(&mt('Access  : ').&Apache::lonnet::plaintext($priv)."\n");
1.174     albertel  475: 	}
                    476: 	if ($fn ne '') {
1.187     bisitz    477:             $r->print(&mt('Resource: ').&Apache::lonenc::check_encrypt($fn)."\n");
1.174     albertel  478: 	}
                    479: 	if ($msg ne '') {
1.187     bisitz    480:             $r->print(&mt('Action  : ').$msg."\n");
1.174     albertel  481: 	}
                    482: 	$r->print("</pre><hr />");
1.120     albertel  483: 	my $url=$fn;
                    484: 	my $last;
                    485: 	if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
                    486: 		&GDBM_READER(),0640)) {
                    487: 	    $last=$hash{'last_known'};
                    488: 	    untie(%hash);
                    489: 	}
1.149     www       490: 	if ($last) { $fn.='?symb='.&escape($last); }
1.120     albertel  491: 
                    492: 	&Apache::londocs::changewarning($r,undef,'You have modified your course recently, [_1] may fix this access problem.',
                    493: 					&Apache::lonenc::check_encrypt($fn));
1.2       www       494:     } else {
1.118     albertel  495:         if ($env{'user.error.msg'}) {
1.25      www       496: 	    $r->print(
1.157     albertel  497:  '<h3><span class="LC_error">'.
                    498:  &mt('You need to choose another user role or enter a specific course for this function').'</span></h3>');
1.25      www       499: 	}
1.2       www       500:     }
1.6       www       501: # -------------------------------------------------------- Choice or no choice?
1.2       www       502:     if ($nochoose) {
1.177     www       503: 	$r->print("<h2>".&mt('Sorry ...')."</h2>\n<span class='LC_error'>".
                    504: 		  &mt('This action is currently not authorized.').'</span>'.
1.150     www       505: 		  &Apache::loncommon::end_page());
                    506: 	return OK;
1.6       www       507:     } else {
1.18      www       508:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    509:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       510:         }
1.84      www       511:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.116     albertel  512:         $r->print('<input type="hidden" name="orgurl" value="'.$fn.'" />');
                    513:         $r->print('<input type="hidden" name="selectrole" value="1" />');
1.134     www       514:         $r->print('<input type="hidden" name="newrole" value="" />');
1.6       www       515:     }
1.118     albertel  516:     if ($env{'user.adv'}) {
1.63      www       517: 	$r->print(
1.177     www       518: 	      '<br /><span class="LC_rolesinfo"><label>'.&mt('Show all roles').': <input type="checkbox" name="showall"');
1.118     albertel  519: 	if ($env{'form.showall'}) { $r->print(' checked="checked" '); }
1.177     www       520: 	$r->print(' /></label><input type="submit" value="'.&mt('Display').'" /></span>');
1.63      www       521:     }
1.4       www       522: 
1.75      albertel  523:     my (%roletext,%sortrole,%roleclass);
1.84      www       524:     my $countactive=0;
1.191     raeburn   525:     my $countfuture=0;
                    526:     my $countwill=0;
1.84      www       527:     my $inrole=0;
                    528:     my $possiblerole='';
1.191     raeburn   529:     my %futureroles;
                    530:     my %roles_nextlogin;
1.118     albertel  531:     foreach $envkey (sort keys %env) {
1.35      matthew   532:         my $button = 1;
1.49      www       533:         my $switchserver='';
1.75      albertel  534: 	my $roletext;
                    535: 	my $sortkey;
1.2       www       536:         if ($envkey=~/^user\.role\./) {
1.102     raeburn   537:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                    538:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.136     raeburn   539:             next if (!defined($role) || $role eq '' || $role =~ /^gr/);
1.102     raeburn   540:             $tremark='';
                    541:             $tpstart='&nbsp;';
                    542:             $tpend='&nbsp;';
                    543:             $tfont='#000000';
1.4       www       544:             if ($tstart) {
1.74      www       545:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
1.4       www       546:             }
                    547:             if ($tend) {
1.74      www       548:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
1.4       www       549:             }
1.118     albertel  550:             if ($env{'request.role'} eq $trolecode) {
1.6       www       551: 		$tstatus='selected';
                    552:             }
1.4       www       553:             my $tbg;
1.164     albertel  554:             if (($tstatus eq 'is') 
                    555: 		|| ($tstatus eq 'selected') 
                    556: 		|| ($tstatus eq 'will') 
                    557: 		|| ($tstatus eq 'future') 
                    558:                 || ($env{'form.showall'})) {
1.35      matthew   559:                 if ($tstatus eq 'is') {
                    560:                     $tbg='#77FF77';
1.47      www       561:                     $tfont='#003300';
1.84      www       562: 		    $possiblerole=$trolecode;
                    563: 		    $countactive++;
1.35      matthew   564:                 } elsif ($tstatus eq 'future') {
                    565:                     $tbg='#FFFF77';
1.49      www       566:                     $button=0;
1.191     raeburn   567:                     $futureroles{$trolecode} = $tstart.':'.$tend;
                    568:                     $countfuture ++;
1.35      matthew   569:                 } elsif ($tstatus eq 'will') {
                    570:                     $tbg='#FFAA77';
1.72      www       571:                     $tremark.=&mt('Active at next login. ');
1.191     raeburn   572:                     $roles_nextlogin{$trolecode} = $tstart.':'.$tend;
                    573:                     $countwill ++;
1.35      matthew   574:                 } elsif ($tstatus eq 'expired') {
                    575:                     $tbg='#FF7777';
1.47      www       576:                     $tfont='#330000';
1.49      www       577:                     $button=0;
1.35      matthew   578:                 } elsif ($tstatus eq 'will_not') {
                    579:                     $tbg='#AAFF77';
1.72      www       580:                     $tremark.=&mt('Expired after logout. ');
1.35      matthew   581:                 } elsif ($tstatus eq 'selected') {
                    582:                     $tbg='#11CC55';
1.47      www       583:                     $tfont='#002200';
1.84      www       584: 		    $inrole=1;
1.86      albertel  585: 		    $countactive++;
1.72      www       586:                     $tremark.=&mt('Currently selected. ');
1.35      matthew   587:                 }
                    588:                 my $trole;
                    589:                 if ($role =~ /^cr\//) {
                    590:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.132     albertel  591: 		    if ($tremark) { $tremark.='<br />'; }
                    592:                     $tremark.=&mt('Defined by ').$rauthor.
1.72      www       593: 			&mt(' at ').$rdomain.'.';
1.159     albertel  594: 		}
                    595: 		$trole=Apache::lonnet::plaintext($role);
1.35      matthew   596:                 my $ttype;
                    597:                 my $twhere;
                    598:                 my ($tdom,$trest,$tsection)=
                    599:                     split(/\//,Apache::lonnet::declutter($where));
                    600:                 # First, Co-Authorship roles
1.125     www       601:                 if (($role eq 'ca') || ($role eq 'aa')) {
1.39      stredwic  602:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
1.83      albertel  603: 		    my $allowed=0;
                    604: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    605: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    606:                     if (!$allowed) {
1.49      www       607: 			$button=0;
1.130     albertel  608:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       609:                     }
1.35      matthew   610:                     #next if ($home eq 'no_host');
1.176     albertel  611:                     $home = &Apache::lonnet::hostname($home);
1.78      sakharuk  612:                     $ttype='Construction Space';
1.72      www       613:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                    614: 			': '.$tdom.'<br />'.
                    615:                         ' '.&mt('Server').':&nbsp;'.$home;
1.118     albertel  616:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82      www       617: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1.75      albertel  618: 		    $sortkey=$role."$trest:$tdom";
1.35      matthew   619:                 } elsif ($role eq 'au') {
                    620:                     # Authors
                    621:                     my $home = &Apache::lonnet::homeserver
1.118     albertel  622:                         ($env{'user.name'},$env{'user.domain'});
1.83      albertel  623: 		    my $allowed=0;
                    624: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    625: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    626:                     if (!$allowed) {
1.49      www       627: 			$button=0;
1.143     albertel  628:                         $switchserver='otherserver='.$home.'&role='.$trolecode;
1.49      www       629:                     }
1.35      matthew   630:                     #next if ($home eq 'no_host');
1.176     albertel  631:                     $home = &Apache::lonnet::hostname($home);
1.78      sakharuk  632:                     $ttype='Construction Space';
1.72      www       633:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                    634: 			':&nbsp;'.$home;
1.118     albertel  635:                     $env{'course.'.$tdom.'_'.$trest.'.description'}='ca';
                    636: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$env{'user.name'}.'/');
1.75      albertel  637: 		    $sortkey=$role;
1.35      matthew   638:                 } elsif ($trest) {
                    639:                     my $tcourseid=$tdom.'_'.$trest;
1.153     raeburn   640:                     $ttype = &Apache::loncommon::course_type($tcourseid);
                    641:                     $trole = &Apache::lonnet::plaintext($role,$ttype);
1.118     albertel  642:                     if ($env{'course.'.$tcourseid.'.description'}) {
                    643:                         $twhere=$env{'course.'.$tcourseid.'.description'};
1.80      albertel  644: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.72      www       645:                         unless ($twhere eq &mt('Currently not available')) {
1.55      albertel  646: 			    $twhere.=' <font size="-2">'.
1.72      www       647:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       648:                                     '</font>';
1.55      albertel  649: 			}
1.8       www       650:                     } else {
1.105     raeburn   651:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
1.35      matthew   652:                         if (%newhash) {
1.80      albertel  653: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1.77      albertel  654: 				"\0".$envkey;
1.49      www       655:                             $twhere=$newhash{'description'}.
                    656:                               ' <font size="-2">'.
1.72      www       657:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       658:                               '</font>';
1.152     raeburn   659:                             $ttype = $newhash{'type'};
1.153     raeburn   660:                             $trole = &Apache::lonnet::plaintext($role,$ttype);
1.35      matthew   661:                         } else {
1.72      www       662:                             $twhere=&mt('Currently not available');
1.118     albertel  663:                             $env{'course.'.$tcourseid.'.description'}=$twhere;
1.80      albertel  664: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.152     raeburn   665:                             $ttype = 'Unavailable';
1.35      matthew   666:                         }
1.8       www       667:                     }
1.121     albertel  668:                     if ($tsection) {
1.175     albertel  669:                         $twhere.='<br />'.&mt('Section').': '.$tsection;
1.121     albertel  670: 		    }
1.72      www       671: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1.35      matthew   672:                 } elsif ($tdom) {
1.78      sakharuk  673:                     $ttype='Domain';
1.35      matthew   674:                     $twhere=$tdom;
1.75      albertel  675: 		    $sortkey=$role.$twhere;
1.35      matthew   676:                 } else {
1.78      sakharuk  677:                     $ttype='System';
1.72      www       678:                     $twhere=&mt('system wide');
1.75      albertel  679: 		    $sortkey=$role.$twhere;
1.13      www       680:                 }
1.152     raeburn   681:                 $roletext.=&build_roletext($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver);
1.75      albertel  682: 		$roletext{$envkey}=$roletext;
                    683: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                    684: 		$sortrole{$sortkey}=$envkey;
                    685: 		$roleclass{$envkey}=$ttype;
1.55      albertel  686: 	    }
1.4       www       687:         }
1.75      albertel  688:     }
1.84      www       689: # No active roles
                    690:     if ($countactive==0) {
                    691: 	if ($inrole) {
                    692: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
                    693: 	} else {
                    694: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
                    695: 	}
1.191     raeburn   696:         &findcourse_advice($r);
                    697: 	$r->print('</form>');
                    698:         if ($countfuture) {
                    699:             $r->print(&mt('The following [quant,_1,role,roles] will become active in the future:',$countfuture));
                    700:             my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,
                    701:                                                $nochoose);
                    702:             &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,
                    703:                             \%roletext);
                    704:             my $tremark='';
                    705:             my $tfont='#003300';
                    706:             if ($env{'request.role'} eq 'cm') {
                    707:                 $r->print('<tr bgcolor="#11CC55">');
                    708:                 $tremark=&mt('Currently selected. ');
                    709:                 $tfont='#002200';
                    710:             } else {
                    711:                 $r->print('<tr bgcolor="#77FF77">');
                    712:             }
                    713:             $r->print('<td></td><td colspan="3"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.&mt('No role specified').
1.192     raeburn   714:                       '</span></font></td><td><font color="'.$tfont.'">'.$tremark.
1.191     raeburn   715:                       '&nbsp;</font></td></tr>'."\n");
                    716: 
                    717:             $r->print('</table>');
                    718:         }
                    719:         $r->print(&Apache::loncommon::end_page());
1.84      www       720: 	return OK;
                    721: # Is there only one choice?
1.118     albertel  722:     } elsif (($countactive==1) && ($env{'request.role'} eq 'cm')) {
1.84      www       723: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
1.179     raeburn   724: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />'.
                    725:             '<noscript><br /><input type="submit" name="submit" value="'.&mt('Continue').'" /></noscript>');
1.84      www       726: 	$r->print("</form>\n");
                    727: 	$r->rflush();
1.179     raeburn   728: 	$r->print('<script type="text/javascript">document.forms.rolechoice.submit();</script>');
1.147     albertel  729: 	$r->print(&Apache::loncommon::end_page());
1.84      www       730: 	return OK;
                    731:     }
                    732: # More than one possible role
                    733: # ----------------------------------------------------------------------- Table
1.181     albertel  734:     unless ((!&Apache::lonmenu::show_course()) || ($nochoose)) {
1.173     albertel  735: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
1.84      www       736:     }
1.191     raeburn   737:     my $doheaders = &roletable_headers($r,\%roleclass,\%sortrole,$nochoose);
1.118     albertel  738:     if ($env{'environment.recentroles'}) {
1.111     albertel  739:         my %recent_roles =
1.118     albertel  740:                &Apache::lonhtmlcommon::get_recent('roles',$env{'environment.recentrolesn'});
1.111     albertel  741: 	my $output='';
                    742: 	foreach (sort(keys(%recent_roles))) {
                    743: 	    if (defined($roletext{'user.role.'.$_})) {
                    744: 		$output.=$roletext{'user.role.'.$_};
1.170     albertel  745:                 if ($_ =~ m-dc\./($match_domain)/- 
                    746: 		    && $dcroles{$1}) {
1.192     raeburn   747: 		    $output .= &adhoc_roles_row($1,'recent');
1.133     albertel  748:                 }
1.113     raeburn   749: 	    } elsif ($numdc > 0) {
                    750:                 unless ($_ =~/^error\:/) {
                    751:                     $output.=&display_cc_role('user.role.'.$_);
                    752:                 }
                    753:             } 
1.111     albertel  754: 	}
                    755: 	if ($output) {
1.132     albertel  756: 	    $r->print("<tr><td align='center' colspan='5'><font face='arial'>".
1.192     raeburn   757: 		      &mt('Recent Roles')."</font></td></tr>");
1.111     albertel  758: 	    $r->print($output);
1.114     raeburn   759:             $doheaders ++;
1.111     albertel  760: 	}
                    761:     }
                    762: 
1.104     raeburn   763:     if ($numdc > 0) {
1.112     raeburn   764:         $r->print(&coursepick_jscript());
1.193   ! raeburn   765:         $r->print(&Apache::loncommon::coursebrowser_javascript().
        !           766:                   &Apache::loncommon::authorbrowser_javascript());
1.108     raeburn   767:     }
1.191     raeburn   768:     &print_rolerows($r,$doheaders,\%roleclass,\%sortrole,\%dcroles,\%roletext);
1.14      www       769:     my $tremark='';
1.47      www       770:     my $tfont='#003300';
1.118     albertel  771:     if ($env{'request.role'} eq 'cm') {
1.19      www       772: 	$r->print('<tr bgcolor="#11CC55">');
1.72      www       773:         $tremark=&mt('Currently selected. ');
1.47      www       774:         $tfont='#002200';
1.14      www       775:     } else {
                    776:         $r->print('<tr bgcolor="#77FF77">');
                    777:     }
                    778:     unless ($nochoose) {
1.118     albertel  779: 	if ($env{'request.role'} ne 'cm') {
1.134     www       780: 	    $r->print('<td><input type="submit" value="'.
1.192     raeburn   781: 		      &mt('Select').'" name="cm" /></td>');
1.55      albertel  782: 	} else {
                    783: 	    $r->print('<td>&nbsp;</td>');
                    784: 	}
1.14      www       785:     }
1.177     www       786:     $r->print('<td colspan="3"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.&mt('No role specified').
1.192     raeburn   787:       '</span></font></td><td><font color="'.$tfont.'">'.$tremark.
1.47      www       788:       '&nbsp;</font></td></tr>'."\n");
1.4       www       789: 
                    790:     $r->print('</table>');
                    791:     unless ($nochoose) {
                    792: 	$r->print("</form>\n");
                    793:     }
1.22      harris41  794: # ------------------------------------------------------------ Privileges Info
1.118     albertel  795:     if (($advanced) && (($env{'user.error.msg'}) || ($error))) {
1.127     albertel  796: 	$r->print('<hr /><h2>Current Privileges</h2>');
1.175     albertel  797: 	$r->print(&privileges_info());
1.4       www       798:     }
1.66      www       799:     $r->print(&Apache::lonnet::getannounce());
1.65      www       800:     if ($advanced) {
                    801: 	$r->print('<p><small><i>This is LON-CAPA '.
1.85      www       802: 		  $r->dir_config('lonVersion').'</i><br />'.
                    803: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
1.65      www       804:     }
1.147     albertel  805:     $r->print(&Apache::loncommon::end_page());
1.1       harris41  806:     return OK;
1.102     raeburn   807: }
                    808: 
1.191     raeburn   809: sub roletable_headers {
                    810:     my ($r,$roleclass,$sortrole,$nochoose) = @_;
                    811:     my $doheaders;
                    812:     if ((ref($sortrole) eq 'HASH') && (ref($roleclass) eq 'HASH')) {
                    813:         $r->print('<br /><table id="LC_rolesmenu"><tr>');
                    814:         if (!$nochoose) { $r->print('<th>&nbsp;</th>'); }
                    815:         $r->print('<th>'.&mt('User Role').'</th><th>'.&mt('Extent')
                    816:                   .'</th><th>'.&mt('Start').'</th><th>'.&mt('End')
                    817:                   .'</th></tr>'."\n");
                    818:         $doheaders=-1;
                    819:         my @roletypes = &roletypes();
                    820:         foreach my $type (@roletypes) {
                    821:             my $haverole=0;
                    822:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                    823:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                    824:                     $haverole=1;
                    825:                 }
                    826:             }
                    827:             if ($haverole) { $doheaders++; }
                    828:         }
                    829:     }
                    830:     return $doheaders;
                    831: }
                    832: 
                    833: sub roletypes {
                    834:     my @types = ('Domain','Construction Space','Course','Unavailable','System');
                    835:     return @types; 
                    836: }
                    837: 
                    838: sub print_rolerows {
                    839:     my ($r,$doheaders,$roleclass,$sortrole,$dcroles,$roletext) = @_;
                    840:     if ((ref($roleclass) eq 'HASH') && (ref($sortrole) eq 'HASH')) {
                    841:         my @types = &roletypes();
                    842:         foreach my $type (@types) {
                    843:             my $output;
                    844:             foreach my $which (sort {uc($a) cmp uc($b)} (keys(%{$sortrole}))) {
                    845:                 if ($roleclass->{$sortrole->{$which}} =~ /^\Q$type\E/) {
                    846:                     if (ref($roletext) eq 'HASH') {
                    847:                         $output.=$roletext->{$sortrole->{$which}};
                    848:                         if ($sortrole->{$which} =~ m-dc\./($match_domain)/-) {
                    849:                             if (ref($dcroles) eq 'HASH') {
                    850:                                 if ($dcroles->{$1}) {
1.192     raeburn   851:                                     $output .= &adhoc_roles_row($1,'');
1.191     raeburn   852:                                 }
                    853:                             }
                    854:                         }
                    855:                     }
                    856:                 }
                    857:             }
                    858:             if ($output) {
                    859:                 if ($doheaders > 0) {
                    860:                     $r->print("<tr>".
                    861:                               "<td align='center' colspan='5'><font face='arial'>".
                    862:                               &mt($type)."</font></td></tr>");
                    863:                 }
                    864:                 $r->print($output);
                    865:             }
                    866:         }
                    867:     }
                    868: }
                    869: 
                    870: sub findcourse_advice {
                    871:     my ($r) = @_;
                    872:     my $domdesc = &Apache::lonnet::domain($env{'user.domain'},'description');
                    873:     if (&check_autoenroll($env{'user.domain'})) {
                    874:         $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).'
                    875: <ul>
                    876:  <li>'.&mt('The course has yet to be created.').'</li>
                    877:  <li>'.&mt('Automatic enrollment of registered students has not been enabled for the course.').'</li>
                    878:  <li>'.&mt('You are in a section of course for which automatic enrollment in the corresponding LON-CAPA course is not active.').'</li>
                    879:  <li>'.&mt('The start date for automated enrollment has yet to be reached.').'</li>
                    880:  <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>
                    881:  </ul>');
                    882:     } else {
                    883:         $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 />');
                    884:     }
                    885:     $r->print('<p>'.&mt('The [_1]Course Catalog[_2] provides information about all [_3] classes for which LON-CAPA courses have been created.','<a href="/adm/coursecatalog">','</a>',$domdesc).'<br />');
                    886:     $r->print(&mt('You can search the course catalog for courses which permit self-enrollment, if you would like to enroll in a course.').'</p>');
                    887:     return;
                    888: }
                    889: 
1.175     albertel  890: sub privileges_info {
                    891:     my ($which) = @_;
                    892:     my $output;
                    893: 
                    894:     $which ||= $env{'request.role'};
                    895: 
                    896:     foreach my $envkey (sort(keys(%env))) {
                    897: 	next if ($envkey!~/^user\.priv\.\Q$which\E\.(.*)/);
                    898: 
                    899: 	my $where=$1;
                    900: 	my $ttype;
                    901: 	my $twhere;
                    902: 	my (undef,$tdom,$trest,$tsec)=split(m{/},$where);
                    903: 	if ($trest) {
                    904: 	    if ($env{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
                    905: 		$ttype='Construction Space';
                    906: 		$twhere='User: '.$trest.', Domain: '.$tdom;
                    907: 	    } else {
                    908: 		$ttype= &Apache::loncommon::course_type($tdom.'_'.$trest);
                    909: 		$twhere=$env{'course.'.$tdom.'_'.$trest.'.description'};
                    910: 		if ($tsec) {
                    911: 		    my $sec_type = 'Section';
                    912: 		    if (exists($env{"user.role.gr.$where"})) {
                    913: 			$sec_type = 'Group';
                    914: 		    }
                    915: 		    $twhere.=' ('.$sec_type.': '.$tsec.')';
                    916: 		}
                    917: 	    }
                    918: 	} elsif ($tdom) {
                    919: 	    $ttype='Domain';
                    920: 	    $twhere=$tdom;
                    921: 	} else {
                    922: 	    $ttype='System';
                    923: 	    $twhere='/';
                    924: 	}
                    925: 	$output .= "\n<h3>".$ttype.': '.$twhere.'</h3>'."\n<ul>";
                    926: 	foreach my $priv (sort(split(/:/,$env{$envkey}))) {
                    927: 	    next if (!$priv);
                    928: 
                    929: 	    my ($prv,$restr)=split(/\&/,$priv);
                    930: 	    my $trestr='';
                    931: 	    if ($restr ne 'F') {
                    932: 		$trestr.=' ('.
                    933: 		    join(', ',
                    934: 			 map { &Apache::lonnet::plaintext($_) } 
                    935: 			     (split('',$restr))).') ';
                    936: 	    }
                    937: 	    $output .= "\n\t".
                    938: 		'<li>'.&Apache::lonnet::plaintext($prv).$trestr.'</li>';
                    939: 	}
                    940: 	$output .= "\n".'</ul>';
                    941:     }
                    942:     return $output;
                    943: }
                    944: 
1.102     raeburn   945: sub role_status {
                    946:     my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
                    947:     my @pwhere = ();
1.118     albertel  948:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.102     raeburn   949:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                    950:         unless (!defined($$role) || $$role eq '') {
                    951:             $$where=join('.',@pwhere);
                    952:             $$trolecode=$$role.'.'.$$where;
1.118     albertel  953:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
1.102     raeburn   954:             $$tstatus='is';
1.105     raeburn   955:             if ($$tstart && $$tstart>$then) {
                    956: 		$$tstatus='future';
                    957: 		if ($$tstart<$now) { $$tstatus='will'; }
1.102     raeburn   958:             }
                    959:             if ($$tend) {
                    960:                 if ($$tend<$then) {
                    961:                     $$tstatus='expired';
1.103     raeburn   962:                 } elsif ($$tend<$now) {
1.104     raeburn   963:                     $$tstatus='will_not';
1.102     raeburn   964:                 }
                    965:             }
                    966:         }
                    967:     }
                    968: }
1.1       harris41  969: 
1.110     raeburn   970: sub build_roletext {
1.152     raeburn   971:     my ($trolecode,$tdom,$trest,$tstatus,$tryagain,$advanced,$tremark,$tbg,$tfont,$trole,$twhere,$tpstart,$tpend,$nochoose,$button,$switchserver) = @_;
1.177     www       972:     my $roletext='<tr bgcolor="'.$tbg.'" class="LC_rolesmenu_'.$tstatus.'">';
1.132     albertel  973:     my $is_dc=($trolecode =~ m/^dc\./);
                    974:     my $rowspan=($is_dc) ? ''
                    975:                          : ' rowspan="2" ';
                    976: 
1.110     raeburn   977:     unless ($nochoose) {
1.134     www       978:         my $buttonname=$trolecode;
                    979:         $buttonname=~s/\W//g;
1.110     raeburn   980:         if (!$button) {
                    981:             if ($switchserver) {
1.177     www       982:                 $roletext.='<td'.$rowspan.'><span class="LC_rolesinfo"><a href="/adm/switchserver?'.
                    983:                 $switchserver.'">'.&mt('Switch Server').'</a></span></td>';
1.110     raeburn   984:             } else {
1.164     albertel  985:                 $roletext.=('<td'.$rowspan.'>&nbsp;</td>');
1.110     raeburn   986:             }
                    987:         } elsif ($tstatus eq 'is') {
1.134     www       988:             $roletext.='<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    989:                         &mt('Select').'" onClick="javascript:enterrole(this.form,\''.
1.192     raeburn   990:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn   991:         } elsif ($tryagain) {
                    992:             $roletext.=
1.134     www       993:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    994:                 &mt('Try Selecting Again').'" onClick="javascript:enterrole(this.form,\''.
1.192     raeburn   995:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn   996:         } elsif ($advanced) {
                    997:             $roletext.=
1.134     www       998:                 '<td'.$rowspan.'><input name="'.$buttonname.'" type="button" value="'.
                    999:                 &mt('Re-Initialize').'" onClick="javascript:enterrole(this.form,\''.
1.192     raeburn  1000:                         $trolecode."','".$buttonname.'\');" /></td>';
1.110     raeburn  1001:         } else {
1.132     albertel 1002:             $roletext.='<td'.$rowspan.'>&nbsp;</td>';
1.110     raeburn  1003:         }
                   1004:     }
1.165     albertel 1005:     if ($trolecode !~ m/^(dc|ca|au|aa)\./) {
                   1006: 	$tremark.=&Apache::lonannounce::showday(time,1,
                   1007: 			 &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                   1008:     }
1.110     raeburn  1009:     $roletext.='<td><font color="'.$tfont.'">'.$trole.
1.121     albertel 1010: 	       '</font></td><td><font color="'.$tfont.'">'.$twhere.
1.110     raeburn  1011:                '</font></td><td><font color="'.$tfont.'">'.$tpstart.
                   1012:                '</font></td><td><font color="'.$tfont.'">'.$tpend.
1.132     albertel 1013:                '</font></td></tr>';
                   1014:     if (!$is_dc) {
1.177     www      1015: 	$roletext.='<tr bgcolor="'.$tbg.'"><td colspan="4"><font color="'.$tfont.'"><span class="LC_rolesinfo">'.$tremark.
                   1016: 	    '</span>&nbsp;</font></td></tr><tr><td colspan="5" height="3"></td></tr>'."\n";
1.132     albertel 1017:     }
1.110     raeburn  1018:     return $roletext;
                   1019: }
                   1020: 
1.193   ! raeburn  1021: sub check_author_homeserver {
1.183     www      1022:     my ($uname,$udom)=@_;
1.193   ! raeburn  1023:     if (($uname eq '') || ($udom eq '')) {
        !          1024:         return ('fail','');
        !          1025:     }
1.183     www      1026:     my $home = &Apache::lonnet::homeserver($uname,$udom);
1.193   ! raeburn  1027:     if (&Apache::lonnet::host_domain($home) ne $udom) {
        !          1028:         return ('fail',$home);
        !          1029:     }
1.183     www      1030:     my @ids=&Apache::lonnet::current_machine_ids();
1.193   ! raeburn  1031:     if (grep(/^\Q$home\E$/,@ids)) {
        !          1032:         return ('ok',$home);
        !          1033:     } else {
        !          1034:         return ('switchserver',$home);
1.183     www      1035:     }
                   1036: }
                   1037: 
1.110     raeburn  1038: sub check_privs {
1.182     www      1039:     my ($cdom,$cnum,$then,$now,$checkrole) = @_;
                   1040:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum; 
1.118     albertel 1041:     if ($env{$cckey}) {
1.110     raeburn  1042:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
                   1043:         &role_status($cckey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
                   1044:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.182     www      1045:             &set_privileges($cdom,$cnum,$checkrole);
1.110     raeburn  1046:         }
                   1047:     } else {
1.182     www      1048:         &set_privileges($cdom,$cnum,$checkrole);
1.110     raeburn  1049:     }
                   1050: }
                   1051: 
1.104     raeburn  1052: sub check_fordc {
                   1053:     my ($dcroles,$then) = @_;
                   1054:     my $numdc = 0;
1.118     albertel 1055:     if ($env{'user.adv'}) {
                   1056:         foreach my $envkey (sort keys %env) {
1.170     albertel 1057:             if ($envkey=~/^user\.role\.dc\.\/($match_domain)\/$/) {
1.104     raeburn  1058:                 my $dcdom = $1;
                   1059:                 my $livedc = 1;
1.118     albertel 1060:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
1.105     raeburn  1061:                 if ($tstart && $tstart>$then) { $livedc = 0; }
                   1062:                 if ($tend   && $tend  <$then) { $livedc = 0; }
1.104     raeburn  1063:                 if ($livedc) {
                   1064:                     $$dcroles{$dcdom} = $envkey;
1.105     raeburn  1065:                     $numdc++;
1.104     raeburn  1066:                 }
                   1067:             }
                   1068:         }
                   1069:     }
                   1070:     return $numdc;
                   1071: }
                   1072: 
1.185     raeburn  1073: sub adhoc_course_role {
                   1074:     my ($then) = @_; 
                   1075:     my ($cdom,$cnum);
                   1076:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1077:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1078:     if (&check_forcc($cdom,$cnum,$then)) {
                   1079:         my $setprivs;
                   1080:         if (!defined($env{'user.role.'.$env{'form.selectrole'}})) {
                   1081:             $setprivs = 1;
                   1082:         } else {
                   1083:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.selectrole'}});
                   1084:             if (($start && ($start>$then || $start == -1)) ||
                   1085:                 ($end && $end<$then)) {
                   1086:                 $setprivs = 1;
                   1087:             }
                   1088:         } 
                   1089:         if ($setprivs) {
                   1090:             if ($env{'form.switchrole'} =~ m-^(in|ta|ep|ad|st|cr)([\w/]*)\./\Q$cdom\E/\Q$cnum\E(/?\w*)$-) {
                   1091:                 my $role = $1;
                   1092:                 my $custom_role = $2;
                   1093:                 my $usec = $3;
                   1094:                 if ($role eq 'cr') {
                   1095:                     if ($custom_role =~ m-^$match_domain/$match_courseid/\w+$-) {
                   1096:                         $role .= $custom_role;
                   1097:                     } else {
                   1098:                         return;
                   1099:                     }
                   1100:                 }
                   1101:                 my (%userroles,%newrole,%newgroups);
                   1102:                 my $area = '/'.$cdom.'/'.$cnum;
                   1103:                 my $spec = $role.'.'.$area;
                   1104:                 if ($usec ne '') {
                   1105:                     $spec .= '/'.$usec;
                   1106:                     $area .= '/'.$usec;
                   1107:                 }
                   1108:                 &Apache::lonnet::standard_roleprivs(\%newrole,$role,$cdom,$spec,$cnum,$area);
                   1109:                 &Apache::lonnet::set_userprivs(\%userroles,\%newrole,%newgroups);
                   1110:                 my $adhocstart = $then-1;
                   1111:                 $userroles{'user.role.'.$spec} = $adhocstart.'.';
1.186     raeburn  1112:                 &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.185     raeburn  1113:             }
                   1114:         }
                   1115:     }
                   1116:     return;
                   1117: }
                   1118: 
                   1119: sub check_forcc {
                   1120:     my ($cdom,$cnum,$then) = @_;
                   1121:     my $is_cc;
                   1122:     if ($cdom ne '' && $cnum ne '') {
                   1123:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
                   1124:             my $envkey = 'user.role.cc./'.$cdom.'/'.$cnum;
                   1125:             if (defined($env{$envkey})) {
                   1126:                 $is_cc = 1;
                   1127:                 my ($tstart,$tend)=split(/\./,$env{$envkey});
                   1128:                 if ($tstart && $tstart>$then) { $is_cc = 0; }
                   1129:                 if ($tend   && $tend  <$then) { $is_cc = 0; }
                   1130:             }
                   1131:         }
                   1132:     }
                   1133:     return $is_cc;
                   1134: }
                   1135: 
1.108     raeburn  1136: sub courselink {
1.193   ! raeburn  1137:     my ($dcdom,$rowtype) = @_;
1.109     raeburn  1138:     my $courseform=&Apache::loncommon::selectcourse_link
1.152     raeburn  1139:                    ('rolechoice','dccourse'.$rowtype.'_'.$dcdom,
                   1140:                     'dcdomain'.$rowtype.'_'.$dcdom,'coursedesc'.$rowtype.'_'.
1.173     albertel 1141:                     $dcdom,$dcdom,undef);
1.138     raeburn  1142:     my $hiddenitems = '<input type="hidden" name="dcdomain'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                   1143:                       '<input type="hidden" name="origdom'.$rowtype.'_'.$dcdom.'" value="'.$dcdom.'" />'.
                   1144:                       '<input type="hidden" name="dccourse'.$rowtype.'_'.$dcdom.'" value="" />'.
                   1145:                       '<input type="hidden" name="coursedesc'.$rowtype.'_'.$dcdom.'" value="" />';
1.112     raeburn  1146:     return $courseform.$hiddenitems;
1.109     raeburn  1147: }
                   1148: 
                   1149: sub coursepick_jscript {
1.184     raeburn  1150:     my %lt = &Apache::lonlocal::texthash(
                   1151:                   plsu => "Please use the 'Select Course' link to open a separate pick course window where you may select the course you wish to enter.",
                   1152:                   youc => 'You can only use this screen to select courses in the current domain.',
                   1153:              );
1.104     raeburn  1154:     my $verify_script = <<"END";
1.179     raeburn  1155: <script type="text/javascript">
1.108     raeburn  1156: function verifyCoursePick(caller) {
                   1157:     var numbutton = getIndex(caller)
1.112     raeburn  1158:     var pickedCourse = document.rolechoice.elements[numbutton+4].value
                   1159:     var pickedDomain = document.rolechoice.elements[numbutton+2].value
                   1160:     if (document.rolechoice.elements[numbutton+2].value == document.rolechoice.elements[numbutton+3].value) {
1.104     raeburn  1161:         if (pickedCourse != '') {
1.108     raeburn  1162:             if (numbutton != -1) {
                   1163:                 var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
                   1164:                 document.rolechoice.elements[numbutton+1].name = courseTarget
                   1165:                 document.rolechoice.submit()
                   1166:             }
1.104     raeburn  1167:         }
                   1168:         else {
1.184     raeburn  1169:             alert("$lt{'plsu'}");
1.104     raeburn  1170:         }
                   1171:     }
                   1172:     else {
1.184     raeburn  1173:         alert("$lt{'youc'}")
1.104     raeburn  1174:     }
                   1175: }
1.109     raeburn  1176: function getIndex(caller) {
1.108     raeburn  1177:     for (var i=0;i<document.rolechoice.elements.length;i++) {
1.109     raeburn  1178:         if (document.rolechoice.elements[i] == caller) {
1.108     raeburn  1179:             return i;
                   1180:         }
                   1181:     }
                   1182:     return -1;
                   1183: }
1.104     raeburn  1184: </script>
                   1185: END
1.109     raeburn  1186:     return $verify_script;
1.104     raeburn  1187: }
                   1188: 
1.193   ! raeburn  1189: sub coauthorlink {
        !          1190:     my ($dcdom,$rowtype) = @_;
        !          1191:     my $coauthorform=&Apache::loncommon::selectauthor_link('rolechoice',$dcdom);
        !          1192:     my $hiddenitems = '<input type="hidden" name="adhoccauname'.$rowtype.'_'.$dcdom.'" value="" />';
        !          1193:     return $coauthorform.$hiddenitems;
        !          1194: }
        !          1195: 
1.113     raeburn  1196: sub display_cc_role {
                   1197:     my $rolekey = shift;
                   1198:     my $roletext;
1.118     albertel 1199:     my $advanced = $env{'user.adv'};
                   1200:     my $tryagain = $env{'form.tryagain'};
1.113     raeburn  1201:     unless ($rolekey =~/^error\:/) {
1.171     albertel 1202:         if ($rolekey =~ m-^user\.role.cc\./($match_domain)/($match_courseid)$-) {
1.113     raeburn  1203:             my $tcourseid = $1.'_'.$2;
                   1204:             my $trolecode = 'cc./'.$1.'/'.$2;
                   1205:             my $twhere;
1.152     raeburn  1206:             my $ttype;
1.113     raeburn  1207:             my $tbg='#77FF77';
                   1208:             my $tfont='#003300';
                   1209:             my %newhash=&Apache::lonnet::coursedescription($tcourseid);
                   1210:             if (%newhash) {
                   1211:                 $twhere=$newhash{'description'}.
                   1212:                         ' <font size="-2">'.
                   1213:                         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$2,$1,$tfont).
                   1214:                         '</font>';
1.153     raeburn  1215:                 $ttype = $newhash{'type'};
1.113     raeburn  1216:             } else {
                   1217:                 $twhere=&mt('Currently not available');
1.118     albertel 1218:                 $env{'course.'.$tcourseid.'.description'}=$twhere;
1.110     raeburn  1219:             }
1.153     raeburn  1220:             my $trole = &Apache::lonnet::plaintext('cc',$ttype);
1.113     raeburn  1221:             $twhere.="<br />".&mt('Domain').":".$1;
1.152     raeburn  1222:             $roletext = &build_roletext($trolecode,$1,$2,'is',$tryagain,$advanced,'',$tbg,$tfont,$trole,$twhere,'','','',1,'');
1.104     raeburn  1223:         }
                   1224:     }
1.152     raeburn  1225:     return ($roletext);
1.104     raeburn  1226: }
                   1227: 
1.192     raeburn  1228: sub adhoc_roles_row {
1.138     raeburn  1229:     my ($dcdom,$rowtype) = @_;
1.132     albertel 1230:     my $output = '<tr bgcolor="#77FF77">'.
1.192     raeburn  1231:                  ' <td colspan="5"><table><tr><td><span class="LC_rolesinfo">'
                   1232:                  .&mt('[_1]Ad hoc[_2] roles in domain [_3] --',
                   1233:                  '<span class="LC_cusr_emph">','</span>',$dcdom).'</span></td><td>';
1.193   ! raeburn  1234:     my $selectcclink = &courselink($dcdom,$rowtype);
1.173     albertel 1235:     my $ccrole = &Apache::lonnet::plaintext('cc');
1.182     www      1236:     my $carole = &Apache::lonnet::plaintext('ca');
1.193   ! raeburn  1237:     my $selectcalink = &coauthorlink($dcdom,$rowtype);
1.182     www      1238:     $output.= '<span class="LC_rolesinfo">'.
1.193   ! raeburn  1239:             &mt('[_1]: [_2]',$ccrole,$selectcclink).
1.192     raeburn  1240:             '</span><br /></td><td>&nbsp;&nbsp;</td><td><span class="LC_rolesinfo">'.
1.193   ! raeburn  1241:             &mt('[_1]: [_2]',$carole,$selectcalink).
1.192     raeburn  1242:             '</span><br /></td></tr></table></td></tr>'.
                   1243:             '<tr><td colspan="5" height="3"></td></tr>'."\n";
1.108     raeburn  1244:     return $output;
                   1245: }
                   1246: 
1.104     raeburn  1247: sub recent_filename {
                   1248:     my $area=shift;
1.149     www      1249:     return 'nohist_recent_'.&escape($area);
1.104     raeburn  1250: }
                   1251: 
1.106     raeburn  1252: sub set_privileges {
1.182     www      1253: # role can be cc or ca
                   1254:     my ($dcdom,$pickedcourse,$role) = @_;
1.106     raeburn  1255:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   1256:     my $spec = $role.'.'.$area;
1.168     albertel 1257:     my %userroles = &Apache::lonnet::set_arearole($role,$area,'','',
                   1258: 						  $env{'user.domain'},
                   1259: 						  $env{'user.name'});
1.106     raeburn  1260:     my %ccrole = ();
                   1261:     &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
1.154     albertel 1262:     my ($author,$adv)= &Apache::lonnet::set_userprivs(\%userroles,\%ccrole);
1.186     raeburn  1263:     &Apache::lonnet::appenv(\%userroles,[$role,'cm']);
1.184     raeburn  1264: 
1.118     albertel 1265:     &Apache::lonnet::log($env{'user.domain'},
                   1266:                          $env{'user.name'},
                   1267:                          $env{'user.home'},
1.106     raeburn  1268:                         "Role ".$role);
                   1269:     &Apache::lonnet::appenv(
1.186     raeburn  1270:                           {'request.role'        => $spec,
1.106     raeburn  1271:                           'request.role.domain' => $dcdom,
1.186     raeburn  1272:                           'request.course.sec'  => ''});
1.106     raeburn  1273:     my $tadv=0;
                   1274:     if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
1.186     raeburn  1275:     &Apache::lonnet::appenv({'request.role.adv'    => $tadv});
1.106     raeburn  1276: }
                   1277: 
1.139     raeburn  1278: sub courseloadpage {
                   1279:     my ($courseid) = @_;
                   1280:     my $startpage;
1.144     albertel 1281:     my %entry_settings = &Apache::lonnet::get('nohist_whatsnew',
                   1282: 					      [$courseid.':courseinit']);
1.139     raeburn  1283:     my ($tmp) = %entry_settings;
1.144     albertel 1284:     unless ($tmp =~ /^error: 2 /) {
1.139     raeburn  1285:         $startpage = $entry_settings{$courseid.':courseinit'};
                   1286:     }
                   1287:     if ($startpage eq '') {
                   1288:         if (exists($env{'environment.course_init_display'})) {
                   1289:             $startpage = $env{'environment.course_init_display'};
                   1290:         }
                   1291:     }
                   1292:     return $startpage;
                   1293: }
                   1294: 
1.191     raeburn  1295: sub check_autoenroll {
                   1296:     my ($dom) = @_;
                   1297:     my $run_enroll = 0;
                   1298:     my $settings;
                   1299:     my %domconfig =
                   1300:         &Apache::lonnet::get_dom('configuration',['autoenroll'],$dom);
                   1301:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   1302:         $settings = $domconfig{'autoenroll'};
                   1303:         if ($settings->{'run'} eq '1') {
                   1304:             $run_enroll = 1;
                   1305:         }
                   1306:     } else {
                   1307:         $run_enroll = &localenroll::run($dom);
                   1308:     }
                   1309:     return $run_enroll;
                   1310: }
                   1311: 
                   1312: 
1.1       harris41 1313: 1;
                   1314: __END__
1.32      harris41 1315: 
                   1316: =head1 NAME
                   1317: 
                   1318: Apache::lonroles - User Roles Screen
                   1319: 
                   1320: =head1 SYNOPSIS
                   1321: 
                   1322: Invoked by /etc/httpd/conf/srm.conf:
                   1323: 
                   1324:  <Location /adm/roles>
                   1325:  PerlAccessHandler       Apache::lonacc
                   1326:  SetHandler perl-script
                   1327:  PerlHandler Apache::lonroles
                   1328:  ErrorDocument     403 /adm/login
                   1329:  ErrorDocument	  500 /adm/errorhandler
                   1330:  </Location>
1.64      bowersj2 1331: 
                   1332: =head1 OVERVIEW
                   1333: 
                   1334: =head2 Choosing Roles
                   1335: 
                   1336: C<lonroles> is a handler that allows a user to switch roles in
                   1337: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                   1338: default role that a user has before selecting a role, as widely as
                   1339: possible, but certain handlers for example need specification which
                   1340: course they should act on, etc. Both in this scenario, and when the
                   1341: handler determines via C<lonnet>'s C<&allowed> function that a certain
                   1342: action is not allowed, C<lonroles> is used as error handler. This
                   1343: allows the user to select another role which may have permission to do
                   1344: what they were trying to do. C<lonroles> can also be accessed via the
                   1345: B<CRS> button in the Remote Control. 
                   1346: 
                   1347: =begin latex
                   1348: 
                   1349: \begin{figure}
                   1350: \begin{center}
                   1351: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                   1352:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                   1353: \end{center}
                   1354: \end{figure}
                   1355: 
                   1356: =end latex
                   1357: 
                   1358: =head2 Role Initialization
                   1359: 
                   1360: 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 1361: 
                   1362: =head1 INTRODUCTION
                   1363: 
                   1364: This module enables a user to select what role he wishes to
                   1365: operate under (instructor, student, teaching assistant, course
                   1366: coordinator, etc).  These roles are pre-established by the actions
                   1367: of upper-level users.
                   1368: 
                   1369: This is part of the LearningOnline Network with CAPA project
                   1370: described at http://www.lon-capa.org.
                   1371: 
                   1372: =head1 HANDLER SUBROUTINE
                   1373: 
                   1374: This routine is called by Apache and mod_perl.
                   1375: 
                   1376: =over 4
                   1377: 
                   1378: =item *
                   1379: 
                   1380: Roles Initialization (yes/no)
                   1381: 
                   1382: =item *
                   1383: 
                   1384: Get Error Message from Environment
                   1385: 
                   1386: =item *
                   1387: 
                   1388: Who is this?
                   1389: 
                   1390: =item *
                   1391: 
                   1392: Generate Page Output
                   1393: 
                   1394: =item *
                   1395: 
                   1396: Choice or no choice
                   1397: 
                   1398: =item *
                   1399: 
                   1400: Table
                   1401: 
                   1402: =item *
                   1403: 
                   1404: Privileges
                   1405: 
                   1406: =back
                   1407: 
                   1408: =cut

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