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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: # User Roles Screen
1.31      www         3: #
1.87    ! www         4: # $Id: lonroles.pm,v 1.86 2004/02/19 21:45:22 albertel Exp $
1.31      www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.32      harris41   28: ###
1.22      harris41   29: 
1.1       harris41   30: package Apache::lonroles;
                     31: 
                     32: use strict;
                     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.57      www        39: use Apache::lonannounce;
1.72      www        40: use Apache::lonlocal;
1.1       harris41   41: 
1.62      matthew    42: sub redirect_user {
                     43:     my ($r,$title,$url,$msg) = @_;
                     44:     $msg = $title if (! defined($msg));
1.73      www        45:     &Apache::loncommon::content_type($r,'text/html');
1.62      matthew    46:     &Apache::loncommon::no_cache($r);
                     47:     $r->send_http_header;
                     48:     my $swinfo=&Apache::lonmenu::rawconfig();
                     49:     my $bodytag=&Apache::loncommon::bodytag('Switching Role');
                     50:     $r->print (<<ENDREDIR);
                     51: <head><title>$title</title>
                     52: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$url">
                     53: </head>
                     54: <html>
                     55: $bodytag
                     56: <script>
                     57: $swinfo
                     58: </script>
                     59: <h1>$msg</h1>
                     60: </body>
                     61: </html>
                     62: ENDREDIR
                     63:     return;
                     64: }
                     65: 
1.1       harris41   66: sub handler {
1.10      www        67: 
1.1       harris41   68:     my $r = shift;
                     69: 
1.6       www        70:     my $now=time;
                     71:     my $then=$ENV{'user.login.time'};
                     72:     my $envkey;
                     73: 
1.10      www        74: 
1.6       www        75: # ================================================================== Roles Init
                     76: 
                     77:     if ($ENV{'form.selectrole'}) {
1.33      www        78: 	if ($ENV{'request.course.id'}) {
                     79: 	    my %temp=('logout_'.$ENV{'request.course.id'} => time);
                     80: 	    &Apache::lonnet::put('email_status',\%temp);
                     81:         }
1.55      albertel   82: 	&Apache::lonnet::appenv("request.course.id"   => '',
                     83: 				"request.course.fn"   => '',
                     84: 				"request.course.uri"  => '',
                     85: 				"request.course.sec"  => '',
                     86: 				"request.role"        => 'cm',
1.56      www        87:                                 "request.role.adv"    => $ENV{'user.adv'},
1.55      albertel   88: 				"request.role.domain" => $ENV{'user.domain'});
1.13      www        89:         foreach $envkey (keys %ENV) {
1.40      matthew    90:             next if ($envkey!~/^user\.role\./);
                     91: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
1.6       www        92:             my $where=join('.',@pwhere);
                     93:             my $trolecode=$role.'.'.$where;
                     94:             if ($ENV{'form.'.$trolecode}) {
1.55      albertel   95: 		my ($tstart,$tend)=split(/\./,$ENV{$envkey});
                     96: 		my $tstatus='is';
                     97: 		if ($tstart) {
                     98: 		    if ($tstart>$then) { 
                     99: 			$tstatus='future';
                    100: 		    }
                    101: 		}
                    102: 		if ($tend) {
                    103: 		    if ($tend<$then) { $tstatus='expired'; }
                    104: 		    if ($tend<$now) { $tstatus='will_not'; }
                    105: 		}
                    106: 		if ($tstatus eq 'is') {
                    107: 		    $where=~s/^\///;
                    108: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
1.53      www       109: # check for keyed access
1.55      albertel  110: 		    if (($role eq 'st') && 
                    111:                        ($ENV{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
                    112: 		         unless (&Apache::lonnet::validate_access_key(
                    113: 				     $ENV{'environment.key.'.$cdom.'_'.$cnum},
                    114: 					     $cdom,$cnum)) {
1.53      www       115: # there is no valid key
1.55      albertel  116: 			     if ($ENV{'form.newkey'}) {
1.53      www       117: # student attempts to register a new key
1.55      albertel  118: 			     } else {
1.53      www       119: # print form to enter a new key
1.73      www       120: 				 &Apache::loncommon::content_type($r,'text/html');
1.55      albertel  121: 				 &Apache::loncommon::no_cache($r);
                    122: 				 $r->send_http_header;
                    123: 				 my $swinfo=&Apache::lonmenu::rawconfig();
                    124: 				 my $bodytag=&Apache::loncommon::bodytag
                    125: 				    ('Enter Access Key to Unlock this Course');
                    126: 				 $r->print(<<ENDENTERKEY);
1.53      www       127: <head><title>Entering Course Access Key</title>
                    128: </head>
                    129: <html>
                    130: $bodytag
                    131: <script>
                    132: $swinfo
                    133: </script>
                    134: <form method="post">
                    135: <input type="hidden" name="selectrole" value="$ENV{'form.selectrole'}" />
                    136: <input type="text" size="20" name="newkey" value="$ENV{'form.newkey'}" />
                    137: <input type="submit" value="Enter key" />
                    138: </form>
                    139: </body></html>
                    140: ENDENTERKEY
1.55      albertel  141: 				 return OK;
                    142: 			     }
                    143: 			 }
                    144: 		     }
1.87    ! www       145: 		    &Apache::lonnet::log($ENV{'user.domain'},
        !           146: 					 $ENV{'user.name'},
        !           147: 					 $ENV{'user.home'},
        !           148: 					 "Role ".$trolecode);
1.56      www       149:                     my $tadv=0;
                    150:                     if (($trolecode!~/^st/) && 
                    151:                         ($trolecode!~/^ta/) && 
                    152:                         ($trolecode!~/^cm/)) { $tadv=1; }
                    153: 		    &Apache::lonnet::appenv(
                    154:                                            'request.role'        => $trolecode,
                    155: 					   'request.role.adv'    => $tadv,
                    156: 					   'request.role.domain' => $cdom,
                    157: 					   'request.course.sec'  => $csec);
1.72      www       158: 		    my $msg=&mt('Entering course ...');
1.62      matthew   159: 
1.55      albertel  160: 		    if (($cnum) && ($role ne 'ca')) {
                    161: 			my ($furl,$ferr)=
                    162: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
                    163: 			if (($ENV{'form.orgurl'}) && 
                    164: 			    ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
1.67      albertel  165: 			    my $dest=$ENV{'form.orgurl'};
1.71      albertel  166: 			    if ( &Apache::lonnet::mod_perl_version() == 2 ) {
1.67      albertel  167: 				&Apache::lonnet::cleanenv();
1.69      albertel  168: 			    }
1.67      albertel  169: 			    $r->internal_redirect($dest);
1.55      albertel  170: 			    return OK;
                    171: 			} else {
                    172: 			    unless ($ENV{'request.course.id'}) {
                    173: 				&Apache::lonnet::appenv(
                    174: 				      "request.course.id"  => $cdom.'_'.$cnum);
1.61      www       175: 				$furl='/adm/roles?tryagain=1';
1.55      albertel  176: 				$msg=
1.72      www       177: 				    '<h1><font color=red>'.
                    178: 			 &mt('Could not initialize course at this time.').
                    179: 		    '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
1.55      albertel  180: 			    }
1.58      bowersj2  181: 
                    182: 			    # Check to see if the user is a CC entering a course 
                    183: 			    # for the first time
                    184: 			    my (undef, undef, $role, $courseid) = split(/\./, $envkey);
                    185: 			    if (substr($courseid, 0, 1) eq '/') {
                    186: 				$courseid = substr($courseid, 1);
                    187: 			    }
                    188: 			    $courseid =~ s/\//_/;
                    189: 			    if ($role eq 'cc' && $ENV{'course.' . $courseid . 
                    190: 							  '.course.helper.not.run'}) {
                    191: 				$furl = "/adm/helper/course.initialization.helper";
                    192: 			    }
1.62      matthew   193:                             #
                    194:                             # Send the user to the course they selected
1.78      sakharuk  195:                             &redirect_user($r,&mt('Entering Course'),
1.62      matthew   196:                                            $furl,$msg);
1.20      www       197:                             return OK;
1.55      albertel  198: 			}
                    199: 		    }
1.62      matthew   200:                     #
                    201:                     # Send the user to the construction space they selected
                    202:                     if ($role =~ /^(au|ca)$/) {
                    203:                         my $redirect_url = '/priv/';
                    204:                         if ($role eq 'au') {
                    205:                             $redirect_url.=$ENV{'user.name'};
                    206:                         } else {
                    207:                             $where =~ /\/(.*)$/;
                    208:                             $redirect_url .= $1;
                    209:                         }
                    210:                         $redirect_url .= '/';
1.78      sakharuk  211:                         &redirect_user($r,&mt('Entering Construction Space'),
1.62      matthew   212:                                        $redirect_url);
                    213:                         return OK;
                    214:                     }
1.55      albertel  215: 		}
                    216:             }
1.6       www       217:         }
1.40      matthew   218:     }
1.44      www       219: 
1.10      www       220: 
1.6       www       221: # =============================================================== No Roles Init
1.10      www       222: 
1.73      www       223:     &Apache::loncommon::content_type($r,'text/html');
1.30      albertel  224:     &Apache::loncommon::no_cache($r);
1.10      www       225:     $r->send_http_header;
                    226:     return OK if $r->header_only;
                    227: 
1.52      www       228:     my $swinfo=&Apache::lonmenu::rawconfig();
1.41      www       229:     my $bodytag=&Apache::loncommon::bodytag('User Roles');
1.82      www       230:     my $helptag='<table><tr><td>'.&Apache::loncommon::help_open_topic
                    231:      ("General_Intro",&mt("Click here for help")).'</td><td>'.
                    232:       &Apache::loncommon::help_open_faq(1,&mt('Click here for FAQ')).'</td><td>'.
                    233:       &Apache::loncommon::help_open_bug('',&mt('Click here to report bugs')).'</td></tr></table>';
1.10      www       234:     $r->print(<<ENDHEADER);
                    235: <html>
                    236: <head>
                    237: <title>LON-CAPA User Roles</title>
1.41      www       238: </head>
                    239: $bodytag
1.45      www       240: $helptag<br />
1.26      www       241: <script>
                    242: $swinfo
                    243: window.focus();
                    244: </script>
1.10      www       245: ENDHEADER
1.6       www       246: 
1.2       www       247: # ------------------------------------------ Get Error Message from Environment
                    248: 
                    249:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
1.12      www       250:     if ($ENV{'user.error.msg'}) {
1.55      albertel  251: 	$r->log_reason(
                    252:    "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
1.12      www       253:     }
1.1       harris41  254: 
1.61      www       255: # ------------------------------------------------- Can this user re-init, etc?
1.6       www       256: 
1.61      www       257:     my $advanced=$ENV{'user.adv'};
                    258:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
                    259:     my $tryagain=$ENV{'form.tryagain'};
1.6       www       260: 
1.2       www       261: # -------------------------------------------------------- Generate Page Output
1.6       www       262: # --------------------------------------------------------------- Error Header?
1.2       www       263:     if ($error) {
                    264: 	$r->print("<h1>LON-CAPA Access Control</h1>");
1.4       www       265:         $r->print("<hr><pre>Access  : ".
                    266:                   Apache::lonnet::plaintext($priv)."\n");
                    267:         $r->print("Resource: $fn\n");
                    268:         $r->print("Action  : $msg\n</pre><hr>");
1.2       www       269:     } else {
1.25      www       270:         if ($ENV{'user.error.msg'}) {
                    271: 	    $r->print(
1.72      www       272:  '<h3><font color=red>'.
                    273:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
1.25      www       274: 	}
1.2       www       275:     }
1.6       www       276: # -------------------------------------------------------- Choice or no choice?
1.2       www       277:     if ($nochoose) {
1.6       www       278:         if ($advanced) {
1.72      www       279: 	    $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
1.6       www       280:         } else {
1.72      www       281: 	    $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
                    282: 		      &mt('This resource might be part of'));
1.55      albertel  283: 	    if ($ENV{'request.course.id'}) {
1.72      www       284: 		$r->print(&mt(' another'));
1.55      albertel  285: 	    } else {
1.72      www       286: 		$r->print(&mt(' a certain'));
1.55      albertel  287: 	    } 
1.72      www       288: 	    $r->print(&mt(' course.').'</body></html>');
1.55      albertel  289: 	    return OK;
1.6       www       290:         } 
                    291:     } else {
                    292:         if ($advanced) {
1.72      www       293: 	    $r->print(&mt("Your home server is ").
1.55      albertel  294: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
                    295:                       ($ENV{'user.name'},$ENV{'user.domain'})}.
                    296: 		      "<br />\n");
1.72      www       297: 	    $r->print(&mt(
                    298:       "Author and Co-Author roles may not be available on servers other than your home server."));
1.17      www       299:         }
1.18      www       300:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
                    301:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
1.6       www       302:         }
1.84      www       303:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
1.6       www       304:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
                    305:         $r->print('<input type=hidden name=selectrole value=1>');
                    306:     }
1.63      www       307:     if ($ENV{'user.adv'}) {
                    308: 	$r->print(
1.72      www       309: 	      '<br />'.&mt('Show all roles').': <input type="checkbox" name="showall"');
1.63      www       310: 	if ($ENV{'form.showall'}) { $r->print(' checked'); }
1.72      www       311: 	$r->print('><input type=submit value="'.&mt('Display').'">');
1.63      www       312:     }
1.4       www       313: 
1.75      albertel  314:     my (%roletext,%sortrole,%roleclass);
1.84      www       315:     my $countactive=0;
                    316:     my $inrole=0;
                    317:     my $possiblerole='';
1.3       albertel  318:     foreach $envkey (sort keys %ENV) {
1.35      matthew   319:         my $button = 1;
1.49      www       320:         my $switchserver='';
1.75      albertel  321: 	my $roletext;
                    322: 	my $sortkey;
1.2       www       323:         if ($envkey=~/^user\.role\./) {
1.40      matthew   324: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
1.46      matthew   325:             next if (!defined($role) || $role eq '');
1.4       www       326:             my $where=join('.',@pwhere);
1.6       www       327:             my $trolecode=$role.'.'.$where;
1.4       www       328:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
                    329:             my $tremark='';
                    330:             my $tstatus='is';
                    331:             my $tpstart='&nbsp;';
                    332:             my $tpend='&nbsp;';
1.47      www       333:             my $tfont='#000000';
1.4       www       334:             if ($tstart) {
                    335: 		if ($tstart>$then) { 
1.35      matthew   336:                     $tstatus='future';
                    337:                     if ($tstart<$now) { $tstatus='will'; }
1.4       www       338:                 }
1.74      www       339:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
1.4       www       340:             }
                    341:             if ($tend) {
1.23      www       342:                 if ($tend<$then) { 
1.35      matthew   343:                     $tstatus='expired'; 
1.23      www       344:                 } elsif ($tend<$now) { 
1.35      matthew   345:                     $tstatus='will_not'; 
1.23      www       346:                 }
1.74      www       347:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
1.4       www       348:             }
1.6       www       349:             if ($ENV{'request.role'} eq $trolecode) {
                    350: 		$tstatus='selected';
                    351:             }
1.4       www       352:             my $tbg;
1.35      matthew   353:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
                    354:                 ($ENV{'form.showall'})) {
                    355:                 if ($tstatus eq 'is') {
                    356:                     $tbg='#77FF77';
1.47      www       357:                     $tfont='#003300';
1.84      www       358: 		    $possiblerole=$trolecode;
                    359: 		    $countactive++;
1.35      matthew   360:                 } elsif ($tstatus eq 'future') {
                    361:                     $tbg='#FFFF77';
1.49      www       362:                     $button=0;
1.35      matthew   363:                 } elsif ($tstatus eq 'will') {
                    364:                     $tbg='#FFAA77';
1.72      www       365:                     $tremark.=&mt('Active at next login. ');
1.35      matthew   366:                 } elsif ($tstatus eq 'expired') {
                    367:                     $tbg='#FF7777';
1.47      www       368:                     $tfont='#330000';
1.49      www       369:                     $button=0;
1.35      matthew   370:                 } elsif ($tstatus eq 'will_not') {
                    371:                     $tbg='#AAFF77';
1.72      www       372:                     $tremark.=&mt('Expired after logout. ');
1.35      matthew   373:                 } elsif ($tstatus eq 'selected') {
                    374:                     $tbg='#11CC55';
1.47      www       375:                     $tfont='#002200';
1.84      www       376: 		    $inrole=1;
1.86      albertel  377: 		    $countactive++;
1.72      www       378:                     $tremark.=&mt('Currently selected. ');
1.35      matthew   379:                 }
                    380:                 my $trole;
                    381:                 if ($role =~ /^cr\//) {
                    382:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
1.72      www       383:                     $tremark.='<br>'.&mt('Defined by ').$rauthor.
                    384: 			&mt(' at ').$rdomain.'.';
1.35      matthew   385:                     $trole=$rrole;
1.8       www       386:                 } else {
1.35      matthew   387:                     $trole=Apache::lonnet::plaintext($role);
                    388:                 }
                    389:                 my $ttype;
                    390:                 my $twhere;
                    391:                 my ($tdom,$trest,$tsection)=
                    392:                     split(/\//,Apache::lonnet::declutter($where));
                    393:                 # First, Co-Authorship roles
                    394:                 if ($role eq 'ca') {
1.39      stredwic  395:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
1.83      albertel  396: 		    my $allowed=0;
                    397: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    398: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    399:                     if (!$allowed) {
1.49      www       400: 			$button=0;
1.51      www       401:                         $switchserver=&Apache::lonnet::escape('http://'.
                    402:                          $Apache::lonnet::hostname{$home}.
                    403:                          '/adm/login?domain='.$ENV{'user.domain'}.
                    404: 			  '&username='.$ENV{'user.name'}.
                    405:                           '&firsturl=/priv/'.$trest);
1.49      www       406:                     }
1.35      matthew   407:                     #next if ($home eq 'no_host');
                    408:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  409:                     $ttype='Construction Space';
1.72      www       410:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
                    411: 			': '.$tdom.'<br />'.
                    412:                         ' '.&mt('Server').':&nbsp;'.$home;
1.35      matthew   413:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82      www       414: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
1.75      albertel  415: 		    $sortkey=$role."$trest:$tdom";
1.35      matthew   416:                 } elsif ($role eq 'au') {
                    417:                     # Authors
                    418:                     my $home = &Apache::lonnet::homeserver
1.39      stredwic  419:                         ($ENV{'user.name'},$ENV{'user.domain'});
1.83      albertel  420: 		    my $allowed=0;
                    421: 		    my @ids=&Apache::lonnet::current_machine_ids();
                    422: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                    423:                     if (!$allowed) {
1.49      www       424: 			$button=0;
1.51      www       425:                         $switchserver=&Apache::lonnet::escape('http://'.
                    426:                          $Apache::lonnet::hostname{$home}.
                    427:                           '/adm/login?domain='.$ENV{'user.domain'}.
                    428: 			   '&username='.$ENV{'user.name'}.
                    429:                            '&firsturl=/priv/'.$ENV{'user.name'});
1.49      www       430:                     }
1.35      matthew   431:                     #next if ($home eq 'no_host');
                    432:                     $home = $Apache::lonnet::hostname{$home};
1.78      sakharuk  433:                     $ttype='Construction Space';
1.72      www       434:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
                    435: 			':&nbsp;'.$home;
1.35      matthew   436:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
1.82      www       437: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$ENV{'user.name'}.'/');
1.75      albertel  438: 		    $sortkey=$role;
1.35      matthew   439:                 } elsif ($trest) {
1.78      sakharuk  440:                     $ttype='Course';
1.35      matthew   441:                     if ($tsection) {
1.72      www       442:                         $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
1.37      albertel  443: 		    }
1.35      matthew   444:                     my $tcourseid=$tdom.'_'.$trest;
                    445:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
1.47      www       446:                         $twhere=$ENV{'course.'.$tcourseid.'.description'};
1.80      albertel  447: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.72      www       448:                         unless ($twhere eq &mt('Currently not available')) {
1.55      albertel  449: 			    $twhere.=' <font size="-2">'.
1.72      www       450:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       451:                                     '</font>';
1.55      albertel  452: 			}
1.8       www       453:                     } else {
1.35      matthew   454:                         my %newhash=Apache::lonnet::coursedescription
                    455:                             ($tcourseid);
                    456:                         if (%newhash) {
1.80      albertel  457: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
1.77      albertel  458: 				"\0".$envkey;
1.49      www       459:                             $twhere=$newhash{'description'}.
                    460:                               ' <font size="-2">'.
1.72      www       461:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
1.49      www       462:                               '</font>';
1.35      matthew   463:                         } else {
1.72      www       464:                             $twhere=&mt('Currently not available');
1.35      matthew   465:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
1.80      albertel  466: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
1.35      matthew   467:                         }
1.8       www       468:                     }
1.72      www       469: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
1.35      matthew   470:                 } elsif ($tdom) {
1.78      sakharuk  471:                     $ttype='Domain';
1.35      matthew   472:                     $twhere=$tdom;
1.75      albertel  473: 		    $sortkey=$role.$twhere;
1.35      matthew   474:                 } else {
1.78      sakharuk  475:                     $ttype='System';
1.72      www       476:                     $twhere=&mt('system wide');
1.75      albertel  477: 		    $sortkey=$role.$twhere;
1.13      www       478:                 }
1.35      matthew   479:  
1.75      albertel  480:                 $roletext.='<tr bgcolor='.$tbg.'>';
1.35      matthew   481:                 unless ($nochoose) {
                    482:                     if (!$button) {
1.49      www       483: 			if ($switchserver) {
1.75      albertel  484: 			    $roletext.='<td><a href="/adm/logout?handover='.
                    485:                               $switchserver.'">'.&mt('Switch Server').'</a></td>';
1.49      www       486:                         } else {
1.75      albertel  487:                             $roletext.=('<td>&nbsp;</td>');
1.49      www       488:                         }
1.35      matthew   489:                     } elsif ($tstatus eq 'is') {
1.75      albertel  490:                         $roletext.=('<td><input type=submit value="'.
1.72      www       491: 				  &mt('Select').'" name="'.
1.35      matthew   492:                                   $trolecode.'"></td>');
1.61      www       493:                     } elsif ($tryagain) {
1.75      albertel  494:                         $roletext.=
                    495: 			    '<td><input type=submit value="'.
                    496: 		  &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
1.61      www       497:                     } elsif ($advanced) {
1.75      albertel  498:                         $roletext.=
                    499:                             '<td><input type=submit value="'.
                    500: 		        &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
1.35      matthew   501:                     } else {
1.75      albertel  502:                         $roletext.='<td>&nbsp;</td>';
1.35      matthew   503:                     }
1.6       www       504:                 }
1.57      www       505:                 $tremark.=&Apache::lonannounce::showday(time,1,
                    506:                          &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
                    507:                 
1.75      albertel  508: 		$roletext.='<td><font color="'.$tfont.'">'.$trole.
1.47      www       509:                       '</font></td><td><font color="'.$tfont.'">'.$ttype.
                    510:                       '</font></td><td><font color="'.$tfont.'">'.$twhere.
                    511:                       '</font></td><td><font color="'.$tfont.'">'.$tpstart.
                    512:                       '</font></td><td><font color="'.$tfont.'">'.$tpend.
                    513:                       '</font></td><td><font color="'.$tfont.'">'.$tremark.
1.75      albertel  514:                       '&nbsp;</font></td></tr>'."\n";
                    515: 		$roletext{$envkey}=$roletext;
                    516: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
                    517: 		$sortrole{$sortkey}=$envkey;
                    518: 		$roleclass{$envkey}=$ttype;
1.55      albertel  519: 	    }
1.4       www       520:         }
1.75      albertel  521:     }
1.84      www       522: # No active roles
                    523:     if ($countactive==0) {
                    524: 	if ($inrole) {
                    525: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
                    526: 	} else {
                    527: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
                    528: 	}
                    529: 	$r->print('</form></body></html>');
                    530: 	return OK;
                    531: # Is there only one choice?
                    532:     } elsif ($countactive==1) {
                    533: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
                    534: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />');
                    535: 	$r->print("</form>\n");
                    536: 	$r->rflush();
                    537: 	$r->print('<script>document.forms.rolechoice.submit();</script>');
                    538: 	$r->print('</body></html>');
                    539: 	return OK;
                    540:     }
                    541: # More than one possible role
                    542: # ----------------------------------------------------------------------- Table
                    543:     unless (($advanced) || ($nochoose)) {
                    544: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
                    545:     }
                    546:     $r->print('<br /><table><tr>');
                    547:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
                    548:     $r->print('<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
                    549:          '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
                    550: 	      &mt('Remark').'</th></tr>'."\n");
1.76      albertel  551:     my $doheaders=-1;
1.78      sakharuk  552:     foreach my $type ('Construction Space','Course','Domain','System') {
1.76      albertel  553: 	my $haverole=0;
1.75      albertel  554: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    555: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
1.76      albertel  556: 		$haverole=1;
1.75      albertel  557: 	    }
1.76      albertel  558: 	}
                    559: 	if ($haverole) { $doheaders++; }
                    560:     }
1.78      sakharuk  561:     foreach my $type ('Construction Space','Course','Domain','System') {
1.76      albertel  562: 	my $output;
                    563: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
                    564: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
1.78      sakharuk  565: 		$output.=&mt($roletext{$sortrole{$which}});
1.76      albertel  566: 	    }
                    567: 	}
                    568: 	if ($output) {
                    569: 	    if ($doheaders > 0) {
1.77      albertel  570: 		$r->print("<tr bgcolor='#BBffBB'>".
1.79      sakharuk  571: 			  "<td align='center' colspan='7'>".&mt($type)."</td>");
1.76      albertel  572: 	    }
                    573: 	    $r->print($output);	    
1.75      albertel  574: 	}
1.4       www       575:     }
1.14      www       576:     my $tremark='';
1.47      www       577:     my $tfont='#003300';
1.14      www       578:     if ($ENV{'request.role'} eq 'cm') {
1.19      www       579: 	$r->print('<tr bgcolor="#11CC55">');
1.72      www       580:         $tremark=&mt('Currently selected. ');
1.47      www       581:         $tfont='#002200';
1.14      www       582:     } else {
                    583:         $r->print('<tr bgcolor="#77FF77">');
                    584:     }
                    585:     unless ($nochoose) {
1.55      albertel  586: 	if ($ENV{'request.role'} ne 'cm') {
1.72      www       587: 	    $r->print('<td><input type=submit value="'.
                    588: 		      &mt('Select').'" name="cm"></td>');
1.55      albertel  589: 	} else {
                    590: 	    $r->print('<td>&nbsp;</td>');
                    591: 	}
1.14      www       592:     }
1.72      www       593:     $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
1.47      www       594:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
                    595:       '&nbsp;</font></td></tr>'."\n");
1.4       www       596: 
                    597:     $r->print('</table>');
                    598:     unless ($nochoose) {
                    599: 	$r->print("</form>\n");
                    600:     }
1.22      harris41  601: # ------------------------------------------------------------ Privileges Info
1.55      albertel  602:     if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
                    603: 	$r->print('<hr><h2>Current Privileges</h2>');
1.4       www       604: 
1.55      albertel  605: 	foreach $envkey (sort keys %ENV) {
                    606: 	    if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
                    607: 		my $where=$envkey;
                    608: 		$where=~s/^user\.priv\.$ENV{'request.role'}\.//;
                    609: 		my $ttype;
                    610: 		my $twhere;
                    611: 		my ($tdom,$trest,$tsec)=
                    612: 		    split(/\//,Apache::lonnet::declutter($where));
                    613: 		if ($trest) {
                    614: 		    if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
                    615: 			$ttype='Construction Space';
                    616: 			$twhere='User: '.$trest.', Domain: '.$tdom;
                    617: 		    } else {
                    618: 			$ttype='Course';
                    619: 			$twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
                    620: 			if ($tsec) {
                    621: 			    $twhere.=' (Section/Group: '.$tsec.')';
                    622: 			}
                    623: 		    }
                    624: 		} elsif ($tdom) {
                    625: 		    $ttype='Domain';
                    626: 		    $twhere=$tdom;
                    627: 		} else {
                    628: 		    $ttype='System';
                    629: 		    $twhere='/';
                    630: 		}
                    631: 		$r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
                    632: 		foreach (sort split(/:/,$ENV{$envkey})) {
                    633: 		    if ($_) {
                    634: 			my ($prv,$restr)=split(/\&/,$_);
                    635: 			my $trestr='';
                    636: 			if ($restr ne 'F') {
                    637: 			    my $i;
                    638: 			    $trestr.=' (';
                    639: 			    for ($i=0;$i<length($restr);$i++) {
                    640: 				$trestr.=
                    641: 			       Apache::lonnet::plaintext(substr($restr,$i,1));
                    642: 				if ($i<length($restr)-1) { $trestr.=', '; }
                    643: 			    }
                    644: 			    $trestr.=')';
                    645: 			}
                    646: 			$r->print('<li>'.
                    647: 				  Apache::lonnet::plaintext($prv).$trestr.
                    648: 				  '</li>');
                    649: 		    }
                    650: 		}
                    651: 		$r->print('</ul>');
                    652: 	    }
                    653: 	}
1.4       www       654:     }
1.66      www       655:     $r->print(&Apache::lonnet::getannounce());
1.65      www       656:     if ($advanced) {
                    657: 	$r->print('<p><small><i>This is LON-CAPA '.
1.85      www       658: 		  $r->dir_config('lonVersion').'</i><br />'.
                    659: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
1.65      www       660:     }
1.1       harris41  661:     $r->print("</body></html>\n");
                    662:     return OK;
                    663: } 
                    664: 
                    665: 1;
                    666: __END__
1.32      harris41  667: 
                    668: =head1 NAME
                    669: 
                    670: Apache::lonroles - User Roles Screen
                    671: 
                    672: =head1 SYNOPSIS
                    673: 
                    674: Invoked by /etc/httpd/conf/srm.conf:
                    675: 
                    676:  <Location /adm/roles>
                    677:  PerlAccessHandler       Apache::lonacc
                    678:  SetHandler perl-script
                    679:  PerlHandler Apache::lonroles
                    680:  ErrorDocument     403 /adm/login
                    681:  ErrorDocument	  500 /adm/errorhandler
                    682:  </Location>
1.64      bowersj2  683: 
                    684: =head1 OVERVIEW
                    685: 
                    686: =head2 Choosing Roles
                    687: 
                    688: C<lonroles> is a handler that allows a user to switch roles in
                    689: mid-session. LON-CAPA attempts to work with "No Role Specified", the
                    690: default role that a user has before selecting a role, as widely as
                    691: possible, but certain handlers for example need specification which
                    692: course they should act on, etc. Both in this scenario, and when the
                    693: handler determines via C<lonnet>'s C<&allowed> function that a certain
                    694: action is not allowed, C<lonroles> is used as error handler. This
                    695: allows the user to select another role which may have permission to do
                    696: what they were trying to do. C<lonroles> can also be accessed via the
                    697: B<CRS> button in the Remote Control. 
                    698: 
                    699: =begin latex
                    700: 
                    701: \begin{figure}
                    702: \begin{center}
                    703: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
                    704:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
                    705: \end{center}
                    706: \end{figure}
                    707: 
                    708: =end latex
                    709: 
                    710: =head2 Role Initialization
                    711: 
                    712: 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  713: 
                    714: =head1 INTRODUCTION
                    715: 
                    716: This module enables a user to select what role he wishes to
                    717: operate under (instructor, student, teaching assistant, course
                    718: coordinator, etc).  These roles are pre-established by the actions
                    719: of upper-level users.
                    720: 
                    721: This is part of the LearningOnline Network with CAPA project
                    722: described at http://www.lon-capa.org.
                    723: 
                    724: =head1 HANDLER SUBROUTINE
                    725: 
                    726: This routine is called by Apache and mod_perl.
                    727: 
                    728: =over 4
                    729: 
                    730: =item *
                    731: 
                    732: Roles Initialization (yes/no)
                    733: 
                    734: =item *
                    735: 
                    736: Get Error Message from Environment
                    737: 
                    738: =item *
                    739: 
                    740: Who is this?
                    741: 
                    742: =item *
                    743: 
                    744: Generate Page Output
                    745: 
                    746: =item *
                    747: 
                    748: Choice or no choice
                    749: 
                    750: =item *
                    751: 
                    752: Table
                    753: 
                    754: =item *
                    755: 
                    756: Privileges
                    757: 
                    758: =back
                    759: 
                    760: =cut

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