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

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

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