File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.84: download - view: text, annotated - select for diffs
Thu Jan 29 00:44:06 2004 UTC (20 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
If there is only one choice, immediately select it. Also, better message
for users who currently have no roles.

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

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