File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.87: download - view: text, annotated - select for diffs
Fri Mar 5 01:55:01 2004 UTC (20 years, 3 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #2798: log role change

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: #
    4: # $Id: lonroles.pm,v 1.87 2004/03/05 01:55:01 www Exp $
    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: #
   28: ###
   29: 
   30: package Apache::lonroles;
   31: 
   32: use strict;
   33: use Apache::lonnet();
   34: use Apache::lonuserstate();
   35: use Apache::Constants qw(:common);
   36: use Apache::File();
   37: use Apache::lonmenu;
   38: use Apache::loncommon;
   39: use Apache::lonannounce;
   40: use Apache::lonlocal;
   41: 
   42: sub redirect_user {
   43:     my ($r,$title,$url,$msg) = @_;
   44:     $msg = $title if (! defined($msg));
   45:     &Apache::loncommon::content_type($r,'text/html');
   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: 
   66: sub handler {
   67: 
   68:     my $r = shift;
   69: 
   70:     my $now=time;
   71:     my $then=$ENV{'user.login.time'};
   72:     my $envkey;
   73: 
   74: 
   75: # ================================================================== Roles Init
   76: 
   77:     if ($ENV{'form.selectrole'}) {
   78: 	if ($ENV{'request.course.id'}) {
   79: 	    my %temp=('logout_'.$ENV{'request.course.id'} => time);
   80: 	    &Apache::lonnet::put('email_status',\%temp);
   81:         }
   82: 	&Apache::lonnet::appenv("request.course.id"   => '',
   83: 				"request.course.fn"   => '',
   84: 				"request.course.uri"  => '',
   85: 				"request.course.sec"  => '',
   86: 				"request.role"        => 'cm',
   87:                                 "request.role.adv"    => $ENV{'user.adv'},
   88: 				"request.role.domain" => $ENV{'user.domain'});
   89:         foreach $envkey (keys %ENV) {
   90:             next if ($envkey!~/^user\.role\./);
   91: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
   92:             my $where=join('.',@pwhere);
   93:             my $trolecode=$role.'.'.$where;
   94:             if ($ENV{'form.'.$trolecode}) {
   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);
  109: # check for keyed access
  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)) {
  115: # there is no valid key
  116: 			     if ($ENV{'form.newkey'}) {
  117: # student attempts to register a new key
  118: 			     } else {
  119: # print form to enter a new key
  120: 				 &Apache::loncommon::content_type($r,'text/html');
  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);
  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
  141: 				 return OK;
  142: 			     }
  143: 			 }
  144: 		     }
  145: 		    &Apache::lonnet::log($ENV{'user.domain'},
  146: 					 $ENV{'user.name'},
  147: 					 $ENV{'user.home'},
  148: 					 "Role ".$trolecode);
  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);
  158: 		    my $msg=&mt('Entering course ...');
  159: 
  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/)) {
  165: 			    my $dest=$ENV{'form.orgurl'};
  166: 			    if ( &Apache::lonnet::mod_perl_version() == 2 ) {
  167: 				&Apache::lonnet::cleanenv();
  168: 			    }
  169: 			    $r->internal_redirect($dest);
  170: 			    return OK;
  171: 			} else {
  172: 			    unless ($ENV{'request.course.id'}) {
  173: 				&Apache::lonnet::appenv(
  174: 				      "request.course.id"  => $cdom.'_'.$cnum);
  175: 				$furl='/adm/roles?tryagain=1';
  176: 				$msg=
  177: 				    '<h1><font color=red>'.
  178: 			 &mt('Could not initialize course at this time.').
  179: 		    '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
  180: 			    }
  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: 			    }
  193:                             #
  194:                             # Send the user to the course they selected
  195:                             &redirect_user($r,&mt('Entering Course'),
  196:                                            $furl,$msg);
  197:                             return OK;
  198: 			}
  199: 		    }
  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 .= '/';
  211:                         &redirect_user($r,&mt('Entering Construction Space'),
  212:                                        $redirect_url);
  213:                         return OK;
  214:                     }
  215: 		}
  216:             }
  217:         }
  218:     }
  219: 
  220: 
  221: # =============================================================== No Roles Init
  222: 
  223:     &Apache::loncommon::content_type($r,'text/html');
  224:     &Apache::loncommon::no_cache($r);
  225:     $r->send_http_header;
  226:     return OK if $r->header_only;
  227: 
  228:     my $swinfo=&Apache::lonmenu::rawconfig();
  229:     my $bodytag=&Apache::loncommon::bodytag('User Roles');
  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>';
  234:     $r->print(<<ENDHEADER);
  235: <html>
  236: <head>
  237: <title>LON-CAPA User Roles</title>
  238: </head>
  239: $bodytag
  240: $helptag<br />
  241: <script>
  242: $swinfo
  243: window.focus();
  244: </script>
  245: ENDHEADER
  246: 
  247: # ------------------------------------------ Get Error Message from Environment
  248: 
  249:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
  250:     if ($ENV{'user.error.msg'}) {
  251: 	$r->log_reason(
  252:    "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
  253:     }
  254: 
  255: # ------------------------------------------------- Can this user re-init, etc?
  256: 
  257:     my $advanced=$ENV{'user.adv'};
  258:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
  259:     my $tryagain=$ENV{'form.tryagain'};
  260: 
  261: # -------------------------------------------------------- Generate Page Output
  262: # --------------------------------------------------------------- Error Header?
  263:     if ($error) {
  264: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  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>");
  269:     } else {
  270:         if ($ENV{'user.error.msg'}) {
  271: 	    $r->print(
  272:  '<h3><font color=red>'.
  273:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
  274: 	}
  275:     }
  276: # -------------------------------------------------------- Choice or no choice?
  277:     if ($nochoose) {
  278:         if ($advanced) {
  279: 	    $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
  280:         } else {
  281: 	    $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
  282: 		      &mt('This resource might be part of'));
  283: 	    if ($ENV{'request.course.id'}) {
  284: 		$r->print(&mt(' another'));
  285: 	    } else {
  286: 		$r->print(&mt(' a certain'));
  287: 	    } 
  288: 	    $r->print(&mt(' course.').'</body></html>');
  289: 	    return OK;
  290:         } 
  291:     } else {
  292:         if ($advanced) {
  293: 	    $r->print(&mt("Your home server is ").
  294: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
  295:                       ($ENV{'user.name'},$ENV{'user.domain'})}.
  296: 		      "<br />\n");
  297: 	    $r->print(&mt(
  298:       "Author and Co-Author roles may not be available on servers other than your home server."));
  299:         }
  300:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
  301:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
  302:         }
  303:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
  304:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  305:         $r->print('<input type=hidden name=selectrole value=1>');
  306:     }
  307:     if ($ENV{'user.adv'}) {
  308: 	$r->print(
  309: 	      '<br />'.&mt('Show all roles').': <input type="checkbox" name="showall"');
  310: 	if ($ENV{'form.showall'}) { $r->print(' checked'); }
  311: 	$r->print('><input type=submit value="'.&mt('Display').'">');
  312:     }
  313: 
  314:     my (%roletext,%sortrole,%roleclass);
  315:     my $countactive=0;
  316:     my $inrole=0;
  317:     my $possiblerole='';
  318:     foreach $envkey (sort keys %ENV) {
  319:         my $button = 1;
  320:         my $switchserver='';
  321: 	my $roletext;
  322: 	my $sortkey;
  323:         if ($envkey=~/^user\.role\./) {
  324: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
  325:             next if (!defined($role) || $role eq '');
  326:             my $where=join('.',@pwhere);
  327:             my $trolecode=$role.'.'.$where;
  328:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  329:             my $tremark='';
  330:             my $tstatus='is';
  331:             my $tpstart='&nbsp;';
  332:             my $tpend='&nbsp;';
  333:             my $tfont='#000000';
  334:             if ($tstart) {
  335: 		if ($tstart>$then) { 
  336:                     $tstatus='future';
  337:                     if ($tstart<$now) { $tstatus='will'; }
  338:                 }
  339:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
  340:             }
  341:             if ($tend) {
  342:                 if ($tend<$then) { 
  343:                     $tstatus='expired'; 
  344:                 } elsif ($tend<$now) { 
  345:                     $tstatus='will_not'; 
  346:                 }
  347:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
  348:             }
  349:             if ($ENV{'request.role'} eq $trolecode) {
  350: 		$tstatus='selected';
  351:             }
  352:             my $tbg;
  353:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
  354:                 ($ENV{'form.showall'})) {
  355:                 if ($tstatus eq 'is') {
  356:                     $tbg='#77FF77';
  357:                     $tfont='#003300';
  358: 		    $possiblerole=$trolecode;
  359: 		    $countactive++;
  360:                 } elsif ($tstatus eq 'future') {
  361:                     $tbg='#FFFF77';
  362:                     $button=0;
  363:                 } elsif ($tstatus eq 'will') {
  364:                     $tbg='#FFAA77';
  365:                     $tremark.=&mt('Active at next login. ');
  366:                 } elsif ($tstatus eq 'expired') {
  367:                     $tbg='#FF7777';
  368:                     $tfont='#330000';
  369:                     $button=0;
  370:                 } elsif ($tstatus eq 'will_not') {
  371:                     $tbg='#AAFF77';
  372:                     $tremark.=&mt('Expired after logout. ');
  373:                 } elsif ($tstatus eq 'selected') {
  374:                     $tbg='#11CC55';
  375:                     $tfont='#002200';
  376: 		    $inrole=1;
  377: 		    $countactive++;
  378:                     $tremark.=&mt('Currently selected. ');
  379:                 }
  380:                 my $trole;
  381:                 if ($role =~ /^cr\//) {
  382:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  383:                     $tremark.='<br>'.&mt('Defined by ').$rauthor.
  384: 			&mt(' at ').$rdomain.'.';
  385:                     $trole=$rrole;
  386:                 } else {
  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') {
  395:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
  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) {
  400: 			$button=0;
  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);
  406:                     }
  407:                     #next if ($home eq 'no_host');
  408:                     $home = $Apache::lonnet::hostname{$home};
  409:                     $ttype='Construction Space';
  410:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
  411: 			': '.$tdom.'<br />'.
  412:                         ' '.&mt('Server').':&nbsp;'.$home;
  413:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  414: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
  415: 		    $sortkey=$role."$trest:$tdom";
  416:                 } elsif ($role eq 'au') {
  417:                     # Authors
  418:                     my $home = &Apache::lonnet::homeserver
  419:                         ($ENV{'user.name'},$ENV{'user.domain'});
  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) {
  424: 			$button=0;
  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'});
  430:                     }
  431:                     #next if ($home eq 'no_host');
  432:                     $home = $Apache::lonnet::hostname{$home};
  433:                     $ttype='Construction Space';
  434:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
  435: 			':&nbsp;'.$home;
  436:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  437: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$ENV{'user.name'}.'/');
  438: 		    $sortkey=$role;
  439:                 } elsif ($trest) {
  440:                     $ttype='Course';
  441:                     if ($tsection) {
  442:                         $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
  443: 		    }
  444:                     my $tcourseid=$tdom.'_'.$trest;
  445:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
  446:                         $twhere=$ENV{'course.'.$tcourseid.'.description'};
  447: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  448:                         unless ($twhere eq &mt('Currently not available')) {
  449: 			    $twhere.=' <font size="-2">'.
  450:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  451:                                     '</font>';
  452: 			}
  453:                     } else {
  454:                         my %newhash=Apache::lonnet::coursedescription
  455:                             ($tcourseid);
  456:                         if (%newhash) {
  457: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
  458: 				"\0".$envkey;
  459:                             $twhere=$newhash{'description'}.
  460:                               ' <font size="-2">'.
  461:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  462:                               '</font>';
  463:                         } else {
  464:                             $twhere=&mt('Currently not available');
  465:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  466: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  467:                         }
  468:                     }
  469: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
  470:                 } elsif ($tdom) {
  471:                     $ttype='Domain';
  472:                     $twhere=$tdom;
  473: 		    $sortkey=$role.$twhere;
  474:                 } else {
  475:                     $ttype='System';
  476:                     $twhere=&mt('system wide');
  477: 		    $sortkey=$role.$twhere;
  478:                 }
  479:  
  480:                 $roletext.='<tr bgcolor='.$tbg.'>';
  481:                 unless ($nochoose) {
  482:                     if (!$button) {
  483: 			if ($switchserver) {
  484: 			    $roletext.='<td><a href="/adm/logout?handover='.
  485:                               $switchserver.'">'.&mt('Switch Server').'</a></td>';
  486:                         } else {
  487:                             $roletext.=('<td>&nbsp;</td>');
  488:                         }
  489:                     } elsif ($tstatus eq 'is') {
  490:                         $roletext.=('<td><input type=submit value="'.
  491: 				  &mt('Select').'" name="'.
  492:                                   $trolecode.'"></td>');
  493:                     } elsif ($tryagain) {
  494:                         $roletext.=
  495: 			    '<td><input type=submit value="'.
  496: 		  &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
  497:                     } elsif ($advanced) {
  498:                         $roletext.=
  499:                             '<td><input type=submit value="'.
  500: 		        &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
  501:                     } else {
  502:                         $roletext.='<td>&nbsp;</td>';
  503:                     }
  504:                 }
  505:                 $tremark.=&Apache::lonannounce::showday(time,1,
  506:                          &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
  507:                 
  508: 		$roletext.='<td><font color="'.$tfont.'">'.$trole.
  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.
  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;
  519: 	    }
  520:         }
  521:     }
  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");
  551:     my $doheaders=-1;
  552:     foreach my $type ('Construction Space','Course','Domain','System') {
  553: 	my $haverole=0;
  554: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  555: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  556: 		$haverole=1;
  557: 	    }
  558: 	}
  559: 	if ($haverole) { $doheaders++; }
  560:     }
  561:     foreach my $type ('Construction Space','Course','Domain','System') {
  562: 	my $output;
  563: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  564: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  565: 		$output.=&mt($roletext{$sortrole{$which}});
  566: 	    }
  567: 	}
  568: 	if ($output) {
  569: 	    if ($doheaders > 0) {
  570: 		$r->print("<tr bgcolor='#BBffBB'>".
  571: 			  "<td align='center' colspan='7'>".&mt($type)."</td>");
  572: 	    }
  573: 	    $r->print($output);	    
  574: 	}
  575:     }
  576:     my $tremark='';
  577:     my $tfont='#003300';
  578:     if ($ENV{'request.role'} eq 'cm') {
  579: 	$r->print('<tr bgcolor="#11CC55">');
  580:         $tremark=&mt('Currently selected. ');
  581:         $tfont='#002200';
  582:     } else {
  583:         $r->print('<tr bgcolor="#77FF77">');
  584:     }
  585:     unless ($nochoose) {
  586: 	if ($ENV{'request.role'} ne 'cm') {
  587: 	    $r->print('<td><input type=submit value="'.
  588: 		      &mt('Select').'" name="cm"></td>');
  589: 	} else {
  590: 	    $r->print('<td>&nbsp;</td>');
  591: 	}
  592:     }
  593:     $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
  594:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
  595:       '&nbsp;</font></td></tr>'."\n");
  596: 
  597:     $r->print('</table>');
  598:     unless ($nochoose) {
  599: 	$r->print("</form>\n");
  600:     }
  601: # ------------------------------------------------------------ Privileges Info
  602:     if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
  603: 	$r->print('<hr><h2>Current Privileges</h2>');
  604: 
  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: 	}
  654:     }
  655:     $r->print(&Apache::lonnet::getannounce());
  656:     if ($advanced) {
  657: 	$r->print('<p><small><i>This is LON-CAPA '.
  658: 		  $r->dir_config('lonVersion').'</i><br />'.
  659: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
  660:     }
  661:     $r->print("</body></html>\n");
  662:     return OK;
  663: } 
  664: 
  665: 1;
  666: __END__
  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>
  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.
  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>