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

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

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