File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.92: download - view: text, annotated - select for diffs
Fri May 21 19:16:35 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: version_1_1_99_0, HEAD
Bug #3021: spaces in filename of first course resource

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: #
    4: # $Id: lonroles.pm,v 1.92 2004/05/21 19:16:35 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::lonroles;
   31: 
   32: use strict;
   33: use Apache::lonnet();
   34: use Apache::lonuserstate();
   35: use Apache::Constants qw(:common);
   36: use Apache::File();
   37: use Apache::lonmenu;
   38: use Apache::loncommon;
   39: use Apache::lonannounce;
   40: use Apache::lonlocal;
   41: 
   42: sub redirect_user {
   43:     my ($r,$title,$url,$msg) = @_;
   44:     $msg = $title if (! defined($msg));
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     &Apache::loncommon::no_cache($r);
   47:     $r->send_http_header;
   48:     my $swinfo=&Apache::lonmenu::rawconfig();
   49:     my $bodytag=&Apache::loncommon::bodytag('Switching Role');
   50: # 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_topic
  284:      ("General_Intro",&mt("Click here for help")).'</td><td>'.
  285:       &Apache::loncommon::help_open_faq(1,&mt('Click here for FAQ')).'</td><td>'.
  286:       &Apache::loncommon::help_open_bug('',&mt('Click here to report bugs')).'</td></tr></table>';
  287:     $r->print(<<ENDHEADER);
  288: <html>
  289: <head>
  290: <title>LON-CAPA User Roles</title>
  291: </head>
  292: $bodytag
  293: $helptag<br />
  294: <script>
  295: $swinfo
  296: window.focus();
  297: </script>
  298: ENDHEADER
  299: 
  300: # ------------------------------------------ Get Error Message from Environment
  301: 
  302:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
  303:     if ($ENV{'user.error.msg'}) {
  304: 	$r->log_reason(
  305:    "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
  306:     }
  307: 
  308: # ------------------------------------------------- Can this user re-init, etc?
  309: 
  310:     my $advanced=$ENV{'user.adv'};
  311:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
  312:     my $tryagain=$ENV{'form.tryagain'};
  313: 
  314: # -------------------------------------------------------- Generate Page Output
  315: # --------------------------------------------------------------- Error Header?
  316:     if ($error) {
  317: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  318:         $r->print("<hr><pre>Access  : ".
  319:                   Apache::lonnet::plaintext($priv)."\n");
  320:         $r->print("Resource: $fn\n");
  321:         $r->print("Action  : $msg\n</pre><hr>");
  322:     } else {
  323:         if ($ENV{'user.error.msg'}) {
  324: 	    $r->print(
  325:  '<h3><font color=red>'.
  326:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
  327: 	}
  328:     }
  329: # -------------------------------------------------------- Choice or no choice?
  330:     if ($nochoose) {
  331:         if ($advanced) {
  332: 	    $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
  333:         } else {
  334: 	    $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
  335: 		      &mt('This resource might be part of'));
  336: 	    if ($ENV{'request.course.id'}) {
  337: 		$r->print(&mt(' another'));
  338: 	    } else {
  339: 		$r->print(&mt(' a certain'));
  340: 	    } 
  341: 	    $r->print(&mt(' course.').'</body></html>');
  342: 	    return OK;
  343:         } 
  344:     } else {
  345:         if ($advanced) {
  346: 	    $r->print(&mt("Your home server is ").
  347: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
  348:                       ($ENV{'user.name'},$ENV{'user.domain'})}.
  349: 		      "<br />\n");
  350: 	    $r->print(&mt(
  351:       "Author and Co-Author roles may not be available on servers other than your home server."));
  352:         }
  353:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
  354:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
  355:         }
  356:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
  357:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  358:         $r->print('<input type=hidden name=selectrole value=1>');
  359:     }
  360:     if ($ENV{'user.adv'}) {
  361: 	$r->print(
  362: 	      '<br />'.&mt('Show all roles').': <input type="checkbox" name="showall"');
  363: 	if ($ENV{'form.showall'}) { $r->print(' checked'); }
  364: 	$r->print('><input type=submit value="'.&mt('Display').'">');
  365:     }
  366: 
  367:     my (%roletext,%sortrole,%roleclass);
  368:     my $countactive=0;
  369:     my $inrole=0;
  370:     my $possiblerole='';
  371:     foreach $envkey (sort keys %ENV) {
  372:         my $button = 1;
  373:         my $switchserver='';
  374: 	my $roletext;
  375: 	my $sortkey;
  376:         if ($envkey=~/^user\.role\./) {
  377: 	    my (undef,undef,$role,@pwhere)=split(/\./,$envkey);
  378:             next if (!defined($role) || $role eq '');
  379:             my $where=join('.',@pwhere);
  380:             my $trolecode=$role.'.'.$where;
  381:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  382:             my $tremark='';
  383:             my $tstatus='is';
  384:             my $tpstart='&nbsp;';
  385:             my $tpend='&nbsp;';
  386:             my $tfont='#000000';
  387:             if ($tstart) {
  388: 		if ($tstart>$then) { 
  389:                     $tstatus='future';
  390:                     if ($tstart<$now) { $tstatus='will'; }
  391:                 }
  392:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
  393:             }
  394:             if ($tend) {
  395:                 if ($tend<$then) { 
  396:                     $tstatus='expired'; 
  397:                 } elsif ($tend<$now) { 
  398:                     $tstatus='will_not'; 
  399:                 }
  400:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
  401:             }
  402:             if ($ENV{'request.role'} eq $trolecode) {
  403: 		$tstatus='selected';
  404:             }
  405:             my $tbg;
  406:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
  407:                 ($ENV{'form.showall'})) {
  408:                 if ($tstatus eq 'is') {
  409:                     $tbg='#77FF77';
  410:                     $tfont='#003300';
  411: 		    $possiblerole=$trolecode;
  412: 		    $countactive++;
  413:                 } elsif ($tstatus eq 'future') {
  414:                     $tbg='#FFFF77';
  415:                     $button=0;
  416:                 } elsif ($tstatus eq 'will') {
  417:                     $tbg='#FFAA77';
  418:                     $tremark.=&mt('Active at next login. ');
  419:                 } elsif ($tstatus eq 'expired') {
  420:                     $tbg='#FF7777';
  421:                     $tfont='#330000';
  422:                     $button=0;
  423:                 } elsif ($tstatus eq 'will_not') {
  424:                     $tbg='#AAFF77';
  425:                     $tremark.=&mt('Expired after logout. ');
  426:                 } elsif ($tstatus eq 'selected') {
  427:                     $tbg='#11CC55';
  428:                     $tfont='#002200';
  429: 		    $inrole=1;
  430: 		    $countactive++;
  431:                     $tremark.=&mt('Currently selected. ');
  432:                 }
  433:                 my $trole;
  434:                 if ($role =~ /^cr\//) {
  435:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  436:                     $tremark.='<br>'.&mt('Defined by ').$rauthor.
  437: 			&mt(' at ').$rdomain.'.';
  438:                     $trole=$rrole;
  439:                 } else {
  440:                     $trole=Apache::lonnet::plaintext($role);
  441:                 }
  442:                 my $ttype;
  443:                 my $twhere;
  444:                 my ($tdom,$trest,$tsection)=
  445:                     split(/\//,Apache::lonnet::declutter($where));
  446:                 # First, Co-Authorship roles
  447:                 if ($role eq 'ca') {
  448:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
  449: 		    my $allowed=0;
  450: 		    my @ids=&Apache::lonnet::current_machine_ids();
  451: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  452:                     if (!$allowed) {
  453: 			$button=0;
  454:                         $switchserver=&Apache::lonnet::escape('http://'.
  455:                          $Apache::lonnet::hostname{$home}.
  456:                          '/adm/login?domain='.$ENV{'user.domain'}.
  457: 			  '&username='.$ENV{'user.name'}.
  458:                           '&firsturl=/priv/'.$trest);
  459:                     }
  460:                     #next if ($home eq 'no_host');
  461:                     $home = $Apache::lonnet::hostname{$home};
  462:                     $ttype='Construction Space';
  463:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
  464: 			': '.$tdom.'<br />'.
  465:                         ' '.&mt('Server').':&nbsp;'.$home;
  466:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  467: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
  468: 		    $sortkey=$role."$trest:$tdom";
  469:                 } elsif ($role eq 'au') {
  470:                     # Authors
  471:                     my $home = &Apache::lonnet::homeserver
  472:                         ($ENV{'user.name'},$ENV{'user.domain'});
  473: 		    my $allowed=0;
  474: 		    my @ids=&Apache::lonnet::current_machine_ids();
  475: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  476:                     if (!$allowed) {
  477: 			$button=0;
  478:                         $switchserver=&Apache::lonnet::escape('http://'.
  479:                          $Apache::lonnet::hostname{$home}.
  480:                           '/adm/login?domain='.$ENV{'user.domain'}.
  481: 			   '&username='.$ENV{'user.name'}.
  482:                            '&firsturl=/priv/'.$ENV{'user.name'});
  483:                     }
  484:                     #next if ($home eq 'no_host');
  485:                     $home = $Apache::lonnet::hostname{$home};
  486:                     $ttype='Construction Space';
  487:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
  488: 			':&nbsp;'.$home;
  489:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  490: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$ENV{'user.name'}.'/');
  491: 		    $sortkey=$role;
  492:                 } elsif ($trest) {
  493:                     $ttype='Course';
  494:                     if ($tsection) {
  495:                         $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
  496: 		    }
  497:                     my $tcourseid=$tdom.'_'.$trest;
  498:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
  499:                         $twhere=$ENV{'course.'.$tcourseid.'.description'};
  500: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  501:                         unless ($twhere eq &mt('Currently not available')) {
  502: 			    $twhere.=' <font size="-2">'.
  503:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  504:                                     '</font>';
  505: 			}
  506:                     } else {
  507:                         my %newhash=Apache::lonnet::coursedescription
  508:                             ($tcourseid);
  509:                         if (%newhash) {
  510: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
  511: 				"\0".$envkey;
  512:                             $twhere=$newhash{'description'}.
  513:                               ' <font size="-2">'.
  514:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  515:                               '</font>';
  516:                         } else {
  517:                             $twhere=&mt('Currently not available');
  518:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  519: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  520:                         }
  521:                     }
  522: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
  523:                 } elsif ($tdom) {
  524:                     $ttype='Domain';
  525:                     $twhere=$tdom;
  526: 		    $sortkey=$role.$twhere;
  527:                 } else {
  528:                     $ttype='System';
  529:                     $twhere=&mt('system wide');
  530: 		    $sortkey=$role.$twhere;
  531:                 }
  532:  
  533:                 $roletext.='<tr bgcolor='.$tbg.'>';
  534:                 unless ($nochoose) {
  535:                     if (!$button) {
  536: 			if ($switchserver) {
  537: 			    $roletext.='<td><a href="/adm/logout?handover='.
  538:                               $switchserver.'">'.&mt('Switch Server').'</a></td>';
  539:                         } else {
  540:                             $roletext.=('<td>&nbsp;</td>');
  541:                         }
  542:                     } elsif ($tstatus eq 'is') {
  543:                         $roletext.=('<td><input type=submit value="'.
  544: 				  &mt('Select').'" name="'.
  545:                                   $trolecode.'"></td>');
  546:                     } elsif ($tryagain) {
  547:                         $roletext.=
  548: 			    '<td><input type=submit value="'.
  549: 		  &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
  550:                     } elsif ($advanced) {
  551:                         $roletext.=
  552:                             '<td><input type=submit value="'.
  553: 		        &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
  554:                     } else {
  555:                         $roletext.='<td>&nbsp;</td>';
  556:                     }
  557:                 }
  558:                 $tremark.=&Apache::lonannounce::showday(time,1,
  559:                          &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
  560:                 
  561: 		$roletext.='<td><font color="'.$tfont.'">'.$trole.
  562:                       '</font></td><td><font color="'.$tfont.'">'.$ttype.
  563:                       '</font></td><td><font color="'.$tfont.'">'.$twhere.
  564:                       '</font></td><td><font color="'.$tfont.'">'.$tpstart.
  565:                       '</font></td><td><font color="'.$tfont.'">'.$tpend.
  566:                       '</font></td><td><font color="'.$tfont.'">'.$tremark.
  567:                       '&nbsp;</font></td></tr>'."\n";
  568: 		$roletext{$envkey}=$roletext;
  569: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
  570: 		$sortrole{$sortkey}=$envkey;
  571: 		$roleclass{$envkey}=$ttype;
  572: 	    }
  573:         }
  574:     }
  575: # No active roles
  576:     if ($countactive==0) {
  577: 	if ($inrole) {
  578: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
  579: 	} else {
  580: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
  581: 	}
  582: 	$r->print('</form></body></html>');
  583: 	return OK;
  584: # Is there only one choice?
  585:     } elsif (($countactive==1) && ($ENV{'request.role'} eq 'cm')) {
  586: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
  587: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />');
  588: 	$r->print("</form>\n");
  589: 	$r->rflush();
  590: 	$r->print('<script>document.forms.rolechoice.submit();</script>');
  591: 	$r->print('</body></html>');
  592: 	return OK;
  593:     }
  594: # More than one possible role
  595: # ----------------------------------------------------------------------- Table
  596:     unless (($advanced) || ($nochoose)) {
  597: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
  598:     }
  599:     $r->print('<br /><table><tr>');
  600:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
  601:     $r->print('<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
  602:          '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
  603: 	      &mt('Remark').'</th></tr>'."\n");
  604:     my $doheaders=-1;
  605:     foreach my $type ('Construction Space','Course','Domain','System') {
  606: 	my $haverole=0;
  607: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  608: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  609: 		$haverole=1;
  610: 	    }
  611: 	}
  612: 	if ($haverole) { $doheaders++; }
  613:     }
  614:     foreach my $type ('Construction Space','Course','Domain','System') {
  615: 	my $output;
  616: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  617: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  618: 		$output.=&mt($roletext{$sortrole{$which}});
  619: 	    }
  620: 	}
  621: 	if ($output) {
  622: 	    if ($doheaders > 0) {
  623: 		$r->print("<tr bgcolor='#BBffBB'>".
  624: 			  "<td align='center' colspan='7'>".&mt($type)."</td>");
  625: 	    }
  626: 	    $r->print($output);	    
  627: 	}
  628:     }
  629:     my $tremark='';
  630:     my $tfont='#003300';
  631:     if ($ENV{'request.role'} eq 'cm') {
  632: 	$r->print('<tr bgcolor="#11CC55">');
  633:         $tremark=&mt('Currently selected. ');
  634:         $tfont='#002200';
  635:     } else {
  636:         $r->print('<tr bgcolor="#77FF77">');
  637:     }
  638:     unless ($nochoose) {
  639: 	if ($ENV{'request.role'} ne 'cm') {
  640: 	    $r->print('<td><input type=submit value="'.
  641: 		      &mt('Select').'" name="cm"></td>');
  642: 	} else {
  643: 	    $r->print('<td>&nbsp;</td>');
  644: 	}
  645:     }
  646:     $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
  647:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
  648:       '&nbsp;</font></td></tr>'."\n");
  649: 
  650:     $r->print('</table>');
  651:     unless ($nochoose) {
  652: 	$r->print("</form>\n");
  653:     }
  654: # ------------------------------------------------------------ Privileges Info
  655:     if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
  656: 	$r->print('<hr><h2>Current Privileges</h2>');
  657: 
  658: 	foreach $envkey (sort keys %ENV) {
  659: 	    if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
  660: 		my $where=$envkey;
  661: 		$where=~s/^user\.priv\.$ENV{'request.role'}\.//;
  662: 		my $ttype;
  663: 		my $twhere;
  664: 		my ($tdom,$trest,$tsec)=
  665: 		    split(/\//,Apache::lonnet::declutter($where));
  666: 		if ($trest) {
  667: 		    if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
  668: 			$ttype='Construction Space';
  669: 			$twhere='User: '.$trest.', Domain: '.$tdom;
  670: 		    } else {
  671: 			$ttype='Course';
  672: 			$twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
  673: 			if ($tsec) {
  674: 			    $twhere.=' (Section/Group: '.$tsec.')';
  675: 			}
  676: 		    }
  677: 		} elsif ($tdom) {
  678: 		    $ttype='Domain';
  679: 		    $twhere=$tdom;
  680: 		} else {
  681: 		    $ttype='System';
  682: 		    $twhere='/';
  683: 		}
  684: 		$r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  685: 		foreach (sort split(/:/,$ENV{$envkey})) {
  686: 		    if ($_) {
  687: 			my ($prv,$restr)=split(/\&/,$_);
  688: 			my $trestr='';
  689: 			if ($restr ne 'F') {
  690: 			    my $i;
  691: 			    $trestr.=' (';
  692: 			    for ($i=0;$i<length($restr);$i++) {
  693: 				$trestr.=
  694: 			       Apache::lonnet::plaintext(substr($restr,$i,1));
  695: 				if ($i<length($restr)-1) { $trestr.=', '; }
  696: 			    }
  697: 			    $trestr.=')';
  698: 			}
  699: 			$r->print('<li>'.
  700: 				  Apache::lonnet::plaintext($prv).$trestr.
  701: 				  '</li>');
  702: 		    }
  703: 		}
  704: 		$r->print('</ul>');
  705: 	    }
  706: 	}
  707:     }
  708:     $r->print(&Apache::lonnet::getannounce());
  709:     if ($advanced) {
  710: 	$r->print('<p><small><i>This is LON-CAPA '.
  711: 		  $r->dir_config('lonVersion').'</i><br />'.
  712: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
  713:     }
  714:     $r->print("</body></html>\n");
  715:     return OK;
  716: } 
  717: 
  718: 1;
  719: __END__
  720: 
  721: =head1 NAME
  722: 
  723: Apache::lonroles - User Roles Screen
  724: 
  725: =head1 SYNOPSIS
  726: 
  727: Invoked by /etc/httpd/conf/srm.conf:
  728: 
  729:  <Location /adm/roles>
  730:  PerlAccessHandler       Apache::lonacc
  731:  SetHandler perl-script
  732:  PerlHandler Apache::lonroles
  733:  ErrorDocument     403 /adm/login
  734:  ErrorDocument	  500 /adm/errorhandler
  735:  </Location>
  736: 
  737: =head1 OVERVIEW
  738: 
  739: =head2 Choosing Roles
  740: 
  741: C<lonroles> is a handler that allows a user to switch roles in
  742: mid-session. LON-CAPA attempts to work with "No Role Specified", the
  743: default role that a user has before selecting a role, as widely as
  744: possible, but certain handlers for example need specification which
  745: course they should act on, etc. Both in this scenario, and when the
  746: handler determines via C<lonnet>'s C<&allowed> function that a certain
  747: action is not allowed, C<lonroles> is used as error handler. This
  748: allows the user to select another role which may have permission to do
  749: what they were trying to do. C<lonroles> can also be accessed via the
  750: B<CRS> button in the Remote Control. 
  751: 
  752: =begin latex
  753: 
  754: \begin{figure}
  755: \begin{center}
  756: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
  757:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
  758: \end{center}
  759: \end{figure}
  760: 
  761: =end latex
  762: 
  763: =head2 Role Initialization
  764: 
  765: 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.
  766: 
  767: =head1 INTRODUCTION
  768: 
  769: This module enables a user to select what role he wishes to
  770: operate under (instructor, student, teaching assistant, course
  771: coordinator, etc).  These roles are pre-established by the actions
  772: of upper-level users.
  773: 
  774: This is part of the LearningOnline Network with CAPA project
  775: described at http://www.lon-capa.org.
  776: 
  777: =head1 HANDLER SUBROUTINE
  778: 
  779: This routine is called by Apache and mod_perl.
  780: 
  781: =over 4
  782: 
  783: =item *
  784: 
  785: Roles Initialization (yes/no)
  786: 
  787: =item *
  788: 
  789: Get Error Message from Environment
  790: 
  791: =item *
  792: 
  793: Who is this?
  794: 
  795: =item *
  796: 
  797: Generate Page Output
  798: 
  799: =item *
  800: 
  801: Choice or no choice
  802: 
  803: =item *
  804: 
  805: Table
  806: 
  807: =item *
  808: 
  809: Privileges
  810: 
  811: =back
  812: 
  813: =cut

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