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

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

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