File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.86: download - view: text, annotated - select for diffs
Thu Feb 19 21:45:22 2004 UTC (20 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- selected role is an active role

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

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