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

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

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