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

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

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