File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.94: download - view: text, annotated - select for diffs
Sun Jul 4 05:53:35 2004 UTC (20 years ago) by albertel
Branches: MAIN
CVS tags: version_1_2_X, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
- add in text describing ?

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

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