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

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

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