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

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

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