File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.94.2.2: download - view: text, annotated - select for diffs
Mon Oct 11 18:27:49 2004 UTC (19 years, 9 months ago) by albertel
Branches: version_1_2_X
Diff to branchpoint 1.94: preferred, unified
- adding more 'Continue' links

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

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