File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.106: download - view: text, annotated - select for diffs
Thu Nov 11 22:18:23 2004 UTC (19 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
DC can select a CC role in his/her domain, and receive CC privileges on the fly.  Work in progress. Access is not quite set right for pages.

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: #
    4: # $Id: lonroles.pm,v 1.106 2004/11/11 22:18:23 raeburn 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::lonhtmlcommon;
   40: use Apache::lonannounce;
   41: use Apache::lonlocal;
   42: 
   43: sub redirect_user {
   44:     my ($r,$title,$url,$msg,$launch_nav) = @_;
   45:     $msg = $title if (! defined($msg));
   46:     &Apache::loncommon::content_type($r,'text/html');
   47:     &Apache::loncommon::no_cache($r);
   48:     $r->send_http_header;
   49:     my $swinfo=&Apache::lonmenu::rawconfig();
   50:     my $navwindow;
   51:     if ($launch_nav eq 'on') {
   52: 	$navwindow.=&Apache::lonnavmaps::launch_win('now');
   53:     } else {
   54: 	$navwindow.=&Apache::lonnavmaps::close();
   55:     }
   56:     my $bodytag=&Apache::loncommon::bodytag('Switching Role');
   57: # Note to style police: 
   58: # This must only replace the spaces, nothing else, or it bombs elsewhere.
   59:     $url=~s/ /\%20/g;
   60:     $r->print(<<ENDREDIR);
   61: <head><title>$title</title>
   62: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$url">
   63: </head>
   64: <html>
   65: $bodytag
   66: <script type="text/javascript">
   67: $swinfo
   68: </script>
   69: $navwindow
   70: <h1>$msg</h1>
   71: <a href="$url">Continue</a>
   72: </body>
   73: </html>
   74: ENDREDIR
   75:     return;
   76: }
   77: 
   78: sub handler {
   79: 
   80:     my $r = shift;
   81: 
   82:     my $now=time;
   83:     my $then=$ENV{'user.login.time'};
   84:     my $envkey;
   85:     my $dcselect='';
   86: 
   87: 
   88: # ================================================================== Roles Init
   89:     if ($ENV{'form.selectrole'}) {
   90:         if ($ENV{'form.dcselected'}) {
   91:             my $dcdom = $ENV{'form.dcselected'};
   92:             my $dckey = 'user.role.dc./'.$dcdom.'/';
   93:             if ($ENV{$dckey}) {
   94:                 my ($dcstart,$dcend)=split(/\./,$ENV{$dckey});
   95:                 my $active_dc = 1;
   96:                 if ($dcstart) {
   97:                     if ($dcstart>$then) {
   98:                         $active_dc = 0;
   99:                     }
  100:                 }
  101:                 if ($dcend) {
  102:                     if ($dcend < $then) {
  103:                         $active_dc = 0;
  104:                     }
  105:                 }
  106:                 if ($active_dc) {
  107:                     $dcselect = $dcdom;
  108:                 }
  109:             }
  110:         }
  111: 	if ($ENV{'request.course.id'}) {
  112: 	    my %temp=('logout_'.$ENV{'request.course.id'} => time);
  113: 	    &Apache::lonnet::put('email_status',\%temp);
  114: 	    &Apache::lonnet::delenv('user.state.'.$ENV{'request.course.id'});
  115: 	}
  116: 	&Apache::lonnet::appenv("request.course.id"   => '',
  117: 				"request.course.fn"   => '',
  118: 				"request.course.uri"  => '',
  119: 				"request.course.sec"  => '',
  120: 				"request.role"        => 'cm',
  121:                                 "request.role.adv"    => $ENV{'user.adv'},
  122: 				"request.role.domain" => $ENV{'user.domain'});
  123: 
  124: # Check to see if the user is a DC trying to enter a course                            
  125: # course selection page
  126:         my $dcflag = 0;
  127:         if ($ENV{'form.dccourse'}) {
  128:             my $dcdom = $ENV{'form.dcdomain'};
  129:             my $pickedcourse = $ENV{'form.dccourse'};
  130:             if ($dcdom && $pickedcourse) {
  131:                 unless ($ENV{'user.role.cc./'.$dcdom.'/'.$pickedcourse}) {
  132:                     if ($ENV{'user.role.dc./'.$dcdom.'/'}) {
  133:                         &set_privileges($dcdom,$pickedcourse);
  134:                         my $msg=&mt('Entering course ...');
  135:                         my ($furl,$ferr)=&Apache::lonuserstate::readmap($dcdom.'/'.$pickedcourse);
  136:                         my $formaction = '/adm/roles/';
  137:                         my $courseid = $dcdom.'_'.$pickedcourse;
  138:                         &Apache::lonhtmlcommon::store_recent('cc_pickby_dc_'.$dcdom,
  139:                             $courseid,$formaction);
  140:                         # Send the user to the course they selected
  141:                         &redirect_user($r,&mt('Entering Course'),
  142:                                 $furl,$msg,
  143:                                 $ENV{'environment.remotenavmap'});
  144:                         return OK;
  145:                     }
  146:                 }
  147:             }
  148:         }
  149: 
  150:         foreach $envkey (keys %ENV) {
  151:             next if ($envkey!~/^user\.role\./);
  152:             my ($where,$trolecode,$role,$tstatus,$tend,$tstart);
  153:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
  154:             if ($ENV{'form.'.$trolecode}) {
  155: 		if ($tstatus eq 'is') {
  156: 		    $where=~s/^\///;
  157: 		    my ($cdom,$cnum,$csec)=split(/\//,$where);
  158: # check for keyed access
  159: 		    if (($role eq 'st') && 
  160:                        ($ENV{'course.'.$cdom.'_'.$cnum.'.keyaccess'} eq 'yes')) {
  161: # who is key authority?
  162: 			my $authdom=$cdom;
  163: 			my $authnum=$cnum;
  164: 			if ($ENV{'course.'.$cdom.'_'.$cnum.'.keyauth'}) {
  165: 			    ($authnum,$authdom)=
  166: 				split(/\W/,$ENV{'course.'.$cdom.'_'.$cnum.'.keyauth'});
  167: 			}
  168: # check with key authority
  169: 			unless (&Apache::lonnet::validate_access_key(
  170: 				     $ENV{'environment.key.'.$cdom.'_'.$cnum},
  171: 					     $authdom,$authnum)) {
  172: # there is no valid key
  173: 			     if ($ENV{'form.newkey'}) {
  174: # student attempts to register a new key
  175: 				 &Apache::loncommon::content_type($r,'text/html');
  176: 				 &Apache::loncommon::no_cache($r);
  177: 				 $r->send_http_header;
  178: 				 my $swinfo=&Apache::lonmenu::rawconfig();
  179: 				 my $bodytag=&Apache::loncommon::bodytag
  180: 				    ('Verifying Access Key to Unlock this Course');
  181: 				 my $buttontext=&mt('Enter Course');
  182: 				 my $message=&mt('Successfully registered key');
  183: 				 my $assignresult=
  184: 				     &Apache::lonnet::assign_access_key(
  185: 						     $ENV{'form.newkey'},
  186: 						     $authdom,$authnum,
  187: 						     $cdom,$cnum,
  188:                                                      $ENV{'user.domain'},
  189: 						     $ENV{'user.name'},
  190: 	      'Assigned from '.$ENV{'REMOTE_ADDR'}.' at '.localtime().' for '.
  191:                                                      $trolecode);
  192: 				 unless ($assignresult eq 'ok') {
  193: 				     $assignresult=~s/^error\:\s*//;
  194: 				     $message=&mt($assignresult).
  195: 				     '<br /><a href="/adm/logout">'.
  196: 				     &mt('Logout').'</a>';
  197: 				     $buttontext=&mt('Re-Enter Key');
  198: 				 }
  199: 				 $r->print(<<ENDENTEREDKEY);
  200: <head><title>Verifying Course Access Key</title>
  201: </head>
  202: <html>
  203: $bodytag
  204: <script>
  205: $swinfo
  206: </script>
  207: <form method="post">
  208: <input type="hidden" name="selectrole" value="1" />
  209: <input type="hidden" name="$trolecode" value="1" />
  210: <font size="+2">$message</font><br />
  211: <input type="submit" value="$buttontext" />
  212: </form>
  213: </body></html>
  214: ENDENTEREDKEY
  215:                                  return OK;
  216: 			     } else {
  217: # print form to enter a new key
  218: 				 &Apache::loncommon::content_type($r,'text/html');
  219: 				 &Apache::loncommon::no_cache($r);
  220: 				 $r->send_http_header;
  221: 				 my $swinfo=&Apache::lonmenu::rawconfig();
  222: 				 my $bodytag=&Apache::loncommon::bodytag
  223: 				    ('Enter Access Key to Unlock this Course');
  224: 				 $r->print(<<ENDENTERKEY);
  225: <head><title>Entering Course Access Key</title>
  226: </head>
  227: <html>
  228: $bodytag
  229: <script>
  230: $swinfo
  231: </script>
  232: <form method="post">
  233: <input type="hidden" name="selectrole" value="1" />
  234: <input type="hidden" name="$trolecode" value="1" />
  235: <input type="text" size="20" name="newkey" value="$ENV{'form.newkey'}" />
  236: <input type="submit" value="Enter key" />
  237: </form>
  238: </body></html>
  239: ENDENTERKEY
  240: 				 return OK;
  241: 			     }
  242: 			 }
  243: 		     }
  244: 		    &Apache::lonnet::log($ENV{'user.domain'},
  245: 					 $ENV{'user.name'},
  246: 					 $ENV{'user.home'},
  247: 					 "Role ".$trolecode);
  248: 		    
  249: 		    &Apache::lonnet::appenv(
  250: 					   'request.role'        => $trolecode,
  251: 					   'request.role.domain' => $cdom,
  252: 					   'request.course.sec'  => $csec);
  253:                     my $tadv=0;
  254: 		    if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
  255: 		    &Apache::lonnet::appenv('request.role.adv'    => $tadv);
  256: 
  257: 		    my $msg=&mt('Entering course ...');
  258: 
  259: 		    if (($cnum) && ($role ne 'ca')) {
  260: 			my ($furl,$ferr)=
  261: 			    &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
  262: 			if (($ENV{'form.orgurl'}) && 
  263: 			    ($ENV{'form.orgurl'}!~/^\/adm\/flip/)) {
  264: 			    my $dest=$ENV{'form.orgurl'};
  265: 			    if ( &Apache::lonnet::mod_perl_version() == 2 ) {
  266: 				&Apache::lonnet::cleanenv();
  267: 			    }
  268: 			    $r->internal_redirect($dest);
  269: 			    return OK;
  270: 			} else {
  271: 			    unless ($ENV{'request.course.id'}) {
  272: 				&Apache::lonnet::appenv(
  273: 				      "request.course.id"  => $cdom.'_'.$cnum);
  274: 				$furl='/adm/roles?tryagain=1';
  275: 				$msg=
  276: 				    '<h1><font color=red>'.
  277: 			 &mt('Could not initialize course at this time.').
  278: 		    '</font></h1><h3>'.&mt('Please try again.').'</h3>'.$ferr;
  279: 			    }
  280: 
  281: 			    # Check to see if the user is a CC entering a course 
  282: 			    # for the first time
  283: 			    my (undef, undef, $role, $courseid) = split(/\./, $envkey);
  284: 			    if (substr($courseid, 0, 1) eq '/') {
  285: 				$courseid = substr($courseid, 1);
  286: 			    }
  287: 			    $courseid =~ s/\//_/;
  288: 			    if ($role eq 'cc' && $ENV{'course.' . $courseid . 
  289: 							  '.course.helper.not.run'}) {
  290: 				$furl = "/adm/helper/course.initialization.helper";
  291: 			    }
  292:                             # Check to see if the user is a DC coming from the
  293:                             # course selection page
  294:                             my $dcflag = 0;
  295:                             if ($ENV{'form.dccourse'}) {
  296:                                 my $formaction = '/adm/roles/';
  297:                                 my ($dcdom,$pickedcourse) = split/_/,$courseid;
  298:                                 if ($ENV{'user.role.dc./'.$dcdom.'/'}) {
  299:                                     &Apache::lonhtmlcommon::store_recent('cc_pickby_dc_'.$dcdom,
  300:                                     $courseid,$formaction);
  301:                                 }
  302:                             }
  303:                             #
  304:                             # Send the user to the course they selected
  305:                             &redirect_user($r,&mt('Entering Course'),
  306:                                            $furl,$msg,
  307: 					   $ENV{'environment.remotenavmap'});
  308:                             return OK;
  309: 			}
  310: 		    }
  311:                     #
  312:                     # Send the user to the construction space they selected
  313:                     if ($role =~ /^(au|ca)$/) {
  314:                         my $redirect_url = '/priv/';
  315:                         if ($role eq 'au') {
  316:                             $redirect_url.=$ENV{'user.name'};
  317:                         } else {
  318:                             $where =~ /\/(.*)$/;
  319:                             $redirect_url .= $1;
  320:                         }
  321:                         $redirect_url .= '/';
  322:                         &redirect_user($r,&mt('Entering Construction Space'),
  323:                                        $redirect_url);
  324:                         return OK;
  325:                     }
  326:                     if ($role eq 'dc') {
  327:                         unless ($dcselect) {
  328:                             my $redirect_url = '/adm/menu/';
  329:                             &redirect_user($r,&mt('Loading Domain Coordinator Menu'),
  330:                                        $redirect_url);
  331:                             return OK;
  332:                         }
  333:                     }
  334: 		}
  335:             }
  336:         }
  337:     }
  338: 
  339: 
  340: # =============================================================== No Roles Init
  341: 
  342:     &Apache::loncommon::content_type($r,'text/html');
  343:     &Apache::loncommon::no_cache($r);
  344:     $r->send_http_header;
  345:     return OK if $r->header_only;
  346: 
  347:     my $swinfo=&Apache::lonmenu::rawconfig();
  348:     my $setDCchoice = &dc_script();
  349:     my $bodytag=&Apache::loncommon::bodytag('User Roles');
  350:     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>';
  351:     $r->print(<<ENDHEADER);
  352: <html>
  353: <head>
  354: <title>LON-CAPA User Roles</title>
  355: </head>
  356: $bodytag
  357: $helptag<br />
  358: <script>
  359: $swinfo
  360: window.focus();
  361: $setDCchoice
  362: </script>
  363: ENDHEADER
  364: 
  365: # ------------------------------------------ Get Error Message from Environment
  366: 
  367:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
  368:     if ($ENV{'user.error.msg'}) {
  369: 	$r->log_reason(
  370:    "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
  371:     }
  372: 
  373: # ------------------------------------------------- Can this user re-init, etc?
  374: 
  375:     my $advanced=$ENV{'user.adv'};
  376:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['tryagain']);
  377:     my $tryagain=$ENV{'form.tryagain'};
  378: 
  379: # -------------------------------------------------------- Generate Page Output
  380: # -------------------------------------------------------- Domain Coordinator? 
  381:     if ($dcselect ne '') {
  382:         &choosecourse_display($r,$dcselect,$then,$now);
  383:         return OK;
  384:     }
  385: 
  386: # --------------------------------------------------------------- Error Header?
  387:     if ($error) {
  388: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  389:         $r->print("<hr><pre>Access  : ".
  390:                   Apache::lonnet::plaintext($priv)."\n");
  391:         $r->print("Resource: $fn\n");
  392:         $r->print("Action  : $msg\n</pre><hr>");
  393:     } else {
  394:         if ($ENV{'user.error.msg'}) {
  395: 	    $r->print(
  396:  '<h3><font color=red>'.
  397:  &mt('You need to choose another user role or enter a specific course for this function').'</font></h3>');
  398: 	}
  399:     }
  400: # -------------------------------------------------------- Choice or no choice?
  401:     if ($nochoose) {
  402:         if ($advanced) {
  403: 	    $r->print("<h2>".&mt('Assigned User Roles')."</h2>\n");
  404:         } else {
  405: 	    $r->print("<h2>".&mt('Sorry ...')."</h2>\n".
  406: 		      &mt('This resource might be part of'));
  407: 	    if ($ENV{'request.course.id'}) {
  408: 		$r->print(&mt(' another'));
  409: 	    } else {
  410: 		$r->print(&mt(' a certain'));
  411: 	    } 
  412: 	    $r->print(&mt(' course.').'</body></html>');
  413: 	    return OK;
  414:         } 
  415:     } else {
  416:         if ($advanced) {
  417: 	    $r->print(&mt("Your home server is ").
  418: 		      $Apache::lonnet::hostname{&Apache::lonnet::homeserver
  419:                       ($ENV{'user.name'},$ENV{'user.domain'})}.
  420: 		      "<br />\n");
  421: 	    $r->print(&mt(
  422:       "Author and Co-Author roles may not be available on servers other than your home server."));
  423:         }
  424:         if (($ENV{'REDIRECT_QUERY_STRING'}) && ($fn)) {
  425:     	    $fn.='?'.$ENV{'REDIRECT_QUERY_STRING'};
  426:         }
  427:         $r->print('<form method="post" name="rolechoice" action="'.(($fn)?$fn:$r->uri).'">');
  428:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  429:         $r->print('<input type=hidden name=selectrole value=1>');
  430:     }
  431:     if ($ENV{'user.adv'}) {
  432: 	$r->print(
  433: 	      '<br />'.&mt('Show all roles').': <input type="checkbox" name="showall"');
  434: 	if ($ENV{'form.showall'}) { $r->print(' checked'); }
  435: 	$r->print('><input type=submit value="'.&mt('Display').'">');
  436:     }
  437: 
  438:     my %dcroles = ();
  439:     my $numdc = &check_fordc(\%dcroles,$then);
  440: 
  441:     my (%roletext,%sortrole,%roleclass);
  442:     my $countactive=0;
  443:     my $inrole=0;
  444:     my $possiblerole='';
  445:     foreach $envkey (sort keys %ENV) {
  446:         my $button = 1;
  447:         my $switchserver='';
  448: 	my $roletext;
  449: 	my $sortkey;
  450:         if ($envkey=~/^user\.role\./) {
  451:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend,$tfont);
  452:             &role_status($envkey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
  453:             next if (!defined($role) || $role eq '');
  454:             $tremark='';
  455:             $tpstart='&nbsp;';
  456:             $tpend='&nbsp;';
  457:             $tfont='#000000';
  458:             if ($tstart) {
  459:                 $tpstart=&Apache::lonlocal::locallocaltime($tstart);
  460:             }
  461:             if ($tend) {
  462:                 $tpend=&Apache::lonlocal::locallocaltime($tend);
  463:             }
  464:             if ($ENV{'request.role'} eq $trolecode) {
  465: 		$tstatus='selected';
  466:             }
  467:             my $tbg;
  468:             if (($tstatus eq 'is') || ($tstatus eq 'selected') ||
  469:                 ($ENV{'form.showall'})) {
  470:                 if ($tstatus eq 'is') {
  471:                     $tbg='#77FF77';
  472:                     $tfont='#003300';
  473: 		    $possiblerole=$trolecode;
  474: 		    $countactive++;
  475:                 } elsif ($tstatus eq 'future') {
  476:                     $tbg='#FFFF77';
  477:                     $button=0;
  478:                 } elsif ($tstatus eq 'will') {
  479:                     $tbg='#FFAA77';
  480:                     $tremark.=&mt('Active at next login. ');
  481:                 } elsif ($tstatus eq 'expired') {
  482:                     $tbg='#FF7777';
  483:                     $tfont='#330000';
  484:                     $button=0;
  485:                 } elsif ($tstatus eq 'will_not') {
  486:                     $tbg='#AAFF77';
  487:                     $tremark.=&mt('Expired after logout. ');
  488:                 } elsif ($tstatus eq 'selected') {
  489:                     $tbg='#11CC55';
  490:                     $tfont='#002200';
  491: 		    $inrole=1;
  492: 		    $countactive++;
  493:                     $tremark.=&mt('Currently selected. ');
  494:                 }
  495:                 my $trole;
  496:                 if ($role =~ /^cr\//) {
  497:                     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  498:                     $tremark.='<br>'.&mt('Defined by ').$rauthor.
  499: 			&mt(' at ').$rdomain.'.';
  500:                     $trole=$rrole;
  501:                 } else {
  502:                     $trole=Apache::lonnet::plaintext($role);
  503:                 }
  504:                 my $ttype;
  505:                 my $twhere;
  506:                 my ($tdom,$trest,$tsection)=
  507:                     split(/\//,Apache::lonnet::declutter($where));
  508:                 # First, Co-Authorship roles
  509:                 if ($role eq 'ca') {
  510:                     my $home = &Apache::lonnet::homeserver($trest,$tdom);
  511: 		    my $allowed=0;
  512: 		    my @ids=&Apache::lonnet::current_machine_ids();
  513: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  514:                     if (!$allowed) {
  515: 			$button=0;
  516:                         $switchserver=&Apache::lonnet::escape('http://'.
  517:                          $Apache::lonnet::hostname{$home}.
  518:                          '/adm/login?domain='.$ENV{'user.domain'}.
  519: 			  '&username='.$ENV{'user.name'}.
  520:                           '&firsturl=/priv/'.$trest.'/');
  521:                     }
  522:                     #next if ($home eq 'no_host');
  523:                     $home = $Apache::lonnet::hostname{$home};
  524:                     $ttype='Construction Space';
  525:                     $twhere=&mt('User').': '.$trest.'<br />'.&mt('Domain').
  526: 			': '.$tdom.'<br />'.
  527:                         ' '.&mt('Server').':&nbsp;'.$home;
  528:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  529: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$trest.'/');
  530: 		    $sortkey=$role."$trest:$tdom";
  531:                 } elsif ($role eq 'au') {
  532:                     # Authors
  533:                     my $home = &Apache::lonnet::homeserver
  534:                         ($ENV{'user.name'},$ENV{'user.domain'});
  535: 		    my $allowed=0;
  536: 		    my @ids=&Apache::lonnet::current_machine_ids();
  537: 		    foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  538:                     if (!$allowed) {
  539: 			$button=0;
  540:                         $switchserver=&Apache::lonnet::escape('http://'.
  541:                          $Apache::lonnet::hostname{$home}.
  542:                           '/adm/login?domain='.$ENV{'user.domain'}.
  543: 			   '&username='.$ENV{'user.name'}.
  544:                            '&firsturl=/priv/'.$ENV{'user.name'}.'/');
  545:                     }
  546:                     #next if ($home eq 'no_host');
  547:                     $home = $Apache::lonnet::hostname{$home};
  548:                     $ttype='Construction Space';
  549:                     $twhere=&mt('Domain').': '.$tdom.'<br />'.&mt('Server').
  550: 			':&nbsp;'.$home;
  551:                     $ENV{'course.'.$tdom.'_'.$trest.'.description'}='ca';
  552: 		    $tremark.=&Apache::lonhtmlcommon::authorbombs('/res/'.$tdom.'/'.$ENV{'user.name'}.'/');
  553: 		    $sortkey=$role;
  554:                 } elsif ($trest) {
  555:                     $ttype='Course';
  556:                     if ($tsection) {
  557:                         $ttype.='<br>'.&mt('Section/Group').': '.$tsection;
  558: 		    }
  559:                     my $tcourseid=$tdom.'_'.$trest;
  560:                     if ($ENV{'course.'.$tcourseid.'.description'}) {
  561:                         $twhere=$ENV{'course.'.$tcourseid.'.description'};
  562: 			$sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  563:                         unless ($twhere eq &mt('Currently not available')) {
  564: 			    $twhere.=' <font size="-2">'.
  565:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  566:                                     '</font>';
  567: 			}
  568:                     } else {
  569:                         my %newhash=&Apache::lonnet::coursedescription($tcourseid);
  570:                         if (%newhash) {
  571: 			    $sortkey=$role."\0".$tdom."\0".$newhash{'description'}.
  572: 				"\0".$envkey;
  573:                             $twhere=$newhash{'description'}.
  574:                               ' <font size="-2">'.
  575:         &Apache::loncommon::syllabuswrapper(&mt('Syllabus'),$trest,$tdom,$tfont).
  576:                               '</font>';
  577:                         } else {
  578:                             $twhere=&mt('Currently not available');
  579:                             $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  580: 			    $sortkey=$role."\0".$tdom."\0".$twhere."\0".$envkey;
  581:                         }
  582:                     }
  583: 		    if ($role ne 'st') { $twhere.="<br />".&mt('Domain').":".$tdom; }
  584:                 } elsif ($tdom) {
  585:                     $ttype='Domain';
  586:                     $twhere=$tdom;
  587: 		    $sortkey=$role.$twhere;
  588:                 } else {
  589:                     $ttype='System';
  590:                     $twhere=&mt('system wide');
  591: 		    $sortkey=$role.$twhere;
  592:                 }
  593:  
  594:                 $roletext.='<tr bgcolor='.$tbg.'>';
  595:                 unless ($nochoose) {
  596:                     if (!$button) {
  597: 			if ($switchserver) {
  598: 			    $roletext.='<td><a href="/adm/logout?handover='.
  599:                               $switchserver.'">'.&mt('Switch Server').'</a></td>';
  600:                         } else {
  601:                             $roletext.=('<td>&nbsp;</td>');
  602:                         }
  603:                     } elsif ($tstatus eq 'is') {
  604:                         $roletext.=('<td><input type=submit value="'.
  605: 				  &mt('Select').'" name="'.
  606:                                   $trolecode.'"></td>');
  607:                     } elsif ($tryagain) {
  608:                         $roletext.=
  609: 			    '<td><input type=submit value="'.
  610: 		  &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
  611:                     } elsif ($advanced) {
  612:                         $roletext.=
  613:                             '<td><input type=submit value="'.
  614: 		        &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
  615:                     } else {
  616:                         $roletext.='<td>&nbsp;</td>';
  617:                     }
  618:                 }
  619:                 $tremark.=&Apache::lonannounce::showday(time,1,
  620:                          &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
  621:                 
  622: 		$roletext.='<td><font color="'.$tfont.'">'.$trole.
  623:                       '</font></td><td><font color="'.$tfont.'">'.$ttype.
  624:                       '</font></td><td><font color="'.$tfont.'">'.$twhere.
  625:                       '</font></td><td><font color="'.$tfont.'">'.$tpstart.
  626:                       '</font></td><td><font color="'.$tfont.'">'.$tpend.
  627:                       '</font></td><td><font color="'.$tfont.'">'.$tremark.
  628:                       '&nbsp;</font></td></tr>'."\n";
  629: 		$roletext{$envkey}=$roletext;
  630: 		if (!$sortkey) {$sortkey=$twhere."\0".$envkey;}
  631: 		$sortrole{$sortkey}=$envkey;
  632: 		$roleclass{$envkey}=$ttype;
  633: 	    }
  634:         }
  635:     }
  636: # No active roles
  637:     if ($countactive==0) {
  638: 	if ($inrole) {
  639: 	    $r->print('<h2>'.&mt('Currently no additional roles or courses').'</h2>');
  640: 	} else {
  641: 	    $r->print('<h2>'.&mt('Currently no active roles or courses').'</h2>');
  642: 	}
  643: 	$r->print('</form></body></html>');
  644: 	return OK;
  645: # Is there only one choice?
  646:     } elsif (($countactive==1) && ($ENV{'request.role'} eq 'cm')) {
  647: 	$r->print('<h3>'.&mt('Please stand by.').'</h3>'.
  648: 	    '<input type="hidden" name="'.$possiblerole.'" value="1" />');
  649: 	$r->print("</form>\n");
  650: 	$r->rflush();
  651: 	$r->print('<script>document.forms.rolechoice.submit();</script>');
  652: 	$r->print('</body></html>');
  653: 	return OK;
  654:     }
  655: # More than one possible role
  656: # ----------------------------------------------------------------------- Table
  657:     unless (($advanced) || ($nochoose)) {
  658: 	$r->print("<h2>".&mt('Select a Course to Enter')."</h2>\n");
  659:     }
  660:     $r->print('<br /><table><tr>');
  661:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
  662:     $r->print('<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
  663:          '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
  664: 	      &mt('Remarks and Calendar Announcements').'</th></tr>'."\n");
  665:     my $doheaders=-1;
  666:     foreach my $type ('Construction Space','Course','Domain','System') {
  667: 	my $haverole=0;
  668: 	foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  669: 	    if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  670: 		$haverole=1;
  671: 	    }
  672: 	}
  673: 	if ($haverole) { $doheaders++; }
  674:     }
  675:     if ($numdc > 0) {
  676:          $r->print('<tr bgcolor="#BBffBB">'.
  677:                       '<td align="center" colspan="7">'.
  678:                       '<input type="hidden" name="all_cc_roles" value="1"/>'.
  679:                       '<input type="hidden" name="dcselected" value=""/>'.
  680:                       &mt('Domain').'</td>');
  681:          foreach my $dcdom (keys %dcroles) {
  682:              my $output = $roletext{$dcroles{$dcdom}};
  683:              my $ccrole = Apache::lonnet::plaintext('cc');
  684:              if ($ENV{'request.role'} =~ m-cc\./$dcdom/-) {
  685:                  $output.=$roletext{'user.role.'.$ENV{'request.role'}};
  686:              }
  687:              $r->print($output);
  688:              $r->print('<tr bgcolor="#77FF77">');
  689:              $r->print('<td><input type="button" value="'.
  690:                       &mt('Select').'" name="dcpick_'.$dcdom.'/" '.
  691:                       'onClick="setDCchoice('."'$dcdom'".')"></td>'.
  692:                       '<td><font color="#002200">'.
  693:                       $ccrole.'</font></td><td>'.&mt('Course').'</td>'.
  694:                       '<td><font color="#002200"><b>'.&mt('All courses').'</b>'.
  695:                       '<br />'.&mt('Domain').':'.$dcdom.'</font>'.
  696:                       '<td colspan="4"><font color="#002200">'.
  697:                       &mt('Course Coordinator access to all courses in domain').
  698:                       ': <b>'.$dcdom.'</b></font></td></tr>'."\n");
  699:          }
  700:          foreach my $type ('Construction Space','Course','Domain','System') {
  701:              my $output;
  702:              foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  703:                 my $skiprole = 0;
  704:                 foreach my $dcdom (keys %dcroles) {
  705:                     if ($sortrole{$which} =~ m-(dc)\./$dcdom/-) {
  706:                         $skiprole = 1;
  707:                         last;
  708:                     }
  709:                 }
  710:                 if (($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) && (!$skiprole)) {
  711:                     $output.=$roletext{$sortrole{$which}};
  712:                 }
  713:             }
  714:             if ($output) {
  715:                 if ($doheaders > 0) {
  716:                     $r->print("<tr bgcolor='#BBffBB'>".
  717:                           "<td align='center' colspan='7'>".&mt($type)."</td>
  718: ");
  719:                 }
  720:                 $r->print($output);
  721:             }
  722:         }
  723:     } else {
  724:         foreach my $type ('Construction Space','Course','Domain','System') {
  725: 	    my $output;
  726: 	    foreach my $which (sort {uc($a) cmp uc($b)} (keys(%sortrole))) {
  727: 	        if ($roleclass{$sortrole{$which}} =~ /^\Q$type\E/) { 
  728: 		    $output.=$roletext{$sortrole{$which}};
  729: 	        }
  730: 	    }
  731: 	    if ($output) {
  732: 	        if ($doheaders > 0) {
  733: 		    $r->print("<tr bgcolor='#BBffBB'>".
  734: 			      "<td align='center' colspan='7'>".&mt($type)."</td>");
  735: 	        }
  736: 	        $r->print($output);	
  737: 	    }
  738:         }
  739:     }
  740:     my $tremark='';
  741:     my $tfont='#003300';
  742:     if ($ENV{'request.role'} eq 'cm') {
  743: 	$r->print('<tr bgcolor="#11CC55">');
  744:         $tremark=&mt('Currently selected. ');
  745:         $tfont='#002200';
  746:     } else {
  747:         $r->print('<tr bgcolor="#77FF77">');
  748:     }
  749:     unless ($nochoose) {
  750: 	if ($ENV{'request.role'} ne 'cm') {
  751: 	    $r->print('<td><input type=submit value="'.
  752: 		      &mt('Select').'" name="cm"></td>');
  753: 	} else {
  754: 	    $r->print('<td>&nbsp;</td>');
  755: 	}
  756:     }
  757:     $r->print('<td colspan=5><font color="'.$tfont.'">'.&mt('No role specified').
  758:       '</font></td><td><font color="'.$tfont.'">'.$tremark.
  759:       '&nbsp;</font></td></tr>'."\n");
  760: 
  761:     $r->print('</table>');
  762:     unless ($nochoose) {
  763: 	$r->print("</form>\n");
  764:     }
  765: # ------------------------------------------------------------ Privileges Info
  766:     if (($advanced) && (($ENV{'user.error.msg'}) || ($error))) {
  767: 	$r->print('<hr><h2>Current Privileges</h2>');
  768: 
  769: 	foreach $envkey (sort keys %ENV) {
  770: 	    if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
  771: 		my $where=$envkey;
  772: 		$where=~s/^user\.priv\.$ENV{'request.role'}\.//;
  773: 		my $ttype;
  774: 		my $twhere;
  775: 		my ($tdom,$trest,$tsec)=
  776: 		    split(/\//,Apache::lonnet::declutter($where));
  777: 		if ($trest) {
  778: 		    if ($ENV{'course.'.$tdom.'_'.$trest.'.description'} eq 'ca') {
  779: 			$ttype='Construction Space';
  780: 			$twhere='User: '.$trest.', Domain: '.$tdom;
  781: 		    } else {
  782: 			$ttype='Course';
  783: 			$twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
  784: 			if ($tsec) {
  785: 			    $twhere.=' (Section/Group: '.$tsec.')';
  786: 			}
  787: 		    }
  788: 		} elsif ($tdom) {
  789: 		    $ttype='Domain';
  790: 		    $twhere=$tdom;
  791: 		} else {
  792: 		    $ttype='System';
  793: 		    $twhere='/';
  794: 		}
  795: 		$r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  796: 		foreach (sort split(/:/,$ENV{$envkey})) {
  797: 		    if ($_) {
  798: 			my ($prv,$restr)=split(/\&/,$_);
  799: 			my $trestr='';
  800: 			if ($restr ne 'F') {
  801: 			    my $i;
  802: 			    $trestr.=' (';
  803: 			    for ($i=0;$i<length($restr);$i++) {
  804: 				$trestr.=
  805: 			       Apache::lonnet::plaintext(substr($restr,$i,1));
  806: 				if ($i<length($restr)-1) { $trestr.=', '; }
  807: 			    }
  808: 			    $trestr.=')';
  809: 			}
  810: 			$r->print('<li>'.
  811: 				  Apache::lonnet::plaintext($prv).$trestr.
  812: 				  '</li>');
  813: 		    }
  814: 		}
  815: 		$r->print('</ul>');
  816: 	    }
  817: 	}
  818:     }
  819:     $r->print(&Apache::lonnet::getannounce());
  820:     if ($advanced) {
  821: 	$r->print('<p><small><i>This is LON-CAPA '.
  822: 		  $r->dir_config('lonVersion').'</i><br />'.
  823: 		  '<a href="/adm/logout">'.&mt('Logout').'</a></small></p>');
  824:     }
  825:     $r->print("</body></html>\n");
  826:     return OK;
  827: }
  828: 
  829: sub role_status {
  830:     my ($rolekey,$then,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
  831:     my @pwhere = ();
  832:     if (exists($ENV{$rolekey}) && $ENV{$rolekey} ne '') {
  833:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
  834:         unless (!defined($$role) || $$role eq '') {
  835:             $$where=join('.',@pwhere);
  836:             $$trolecode=$$role.'.'.$$where;
  837:             ($$tstart,$$tend)=split(/\./,$ENV{$rolekey});
  838:             $$tstatus='is';
  839:             if ($$tstart && $$tstart>$then) {
  840: 		$$tstatus='future';
  841: 		if ($$tstart<$now) { $$tstatus='will'; }
  842:             }
  843:             if ($$tend) {
  844:                 if ($$tend<$then) {
  845:                     $$tstatus='expired';
  846:                 } elsif ($$tend<$now) {
  847:                     $$tstatus='will_not';
  848:                 }
  849:             }
  850:         }
  851:     }
  852: }
  853: 
  854: sub dc_script {
  855:     my $response = (<<"END");
  856: function setDCchoice(caller) {
  857:     var dcname = "dc./"+caller+"/"
  858:     document.rolechoice.dcselected.value = caller
  859:     document.rolechoice.elements[3].name = dcname
  860:     document.rolechoice.submit()
  861: }
  862: END
  863:     return $response;
  864: }
  865:                                                                                  
  866: sub check_fordc {
  867:     my ($dcroles,$then) = @_;
  868:     my $numdc = 0;
  869:     if ($ENV{'user.adv'}) {
  870:         foreach my $envkey (sort keys %ENV) {
  871:             if ($envkey=~/^user\.role\.dc\.\/(\w+)\/$/) {
  872:                 my $dcdom = $1;
  873:                 my $livedc = 1;
  874:                 my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  875:                 if ($tstart && $tstart>$then) { $livedc = 0; }
  876:                 if ($tend   && $tend  <$then) { $livedc = 0; }
  877:                 if ($livedc) {
  878:                     $$dcroles{$dcdom} = $envkey;
  879:                     $numdc++;
  880:                 }
  881:             }
  882:         }
  883:     }
  884:     return $numdc;
  885: }
  886: 
  887: sub choosecourse_display {
  888:     my ($r,$dcdom,$then,$now) = @_;
  889:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($dcdom,'dom');
  890:     my $verify_script = <<"END";
  891: <script>
  892: function verifyCoursePick() {
  893:     var pickedCourse = document.roles.dccourse.value
  894:     var pickedDomain = document.roles.dcdomain.value
  895:     if (document.roles.dcdomain.value == document.roles.origdom.value) {
  896:         if (pickedCourse != '') {
  897:             var courseTarget = "cc./"+pickedDomain+"/"+pickedCourse
  898:             document.roles.elements[0].value = 1
  899:             document.roles.elements[0].name = courseTarget
  900:             document.roles.selectrole.value = 1
  901:             document.roles.submit()
  902:         }
  903:         else {
  904:             alert("You must use the 'Select Course' link to open a separate pick course window which you can use to select the course you wish to enter");
  905:         }
  906:     }
  907:     else {
  908:         alert("You can only use this screen to select courses in the current domain")
  909:     }
  910: }
  911: </script>
  912: END
  913:     my $courseform=&Apache::loncommon::selectcourse_link
  914:                      ('roles','dccourse','dcdomain','coursedesc');
  915:     $r->print($cb_jscript.$verify_script);
  916:     my $formaction='/adm/roles';
  917:     $formaction=~s/\/+/\//g;
  918:     my $crs;
  919:     &select_recent_courses($r,$dcdom,$then,$now);
  920:     $r->print('<br /><form name="roles" method="post" action="'.$formaction.'" />');
  921:     $r->print('<h3>'.&mt('Choose a course from domain').': '.$dcdom.'</h3><font size="+1">'.$courseform.'</font><br /><br />'.
  922:               '<input type="hidden" name="chosencourse" value=""/>'.
  923:               'Course Title: <input type="text" size="20" name="coursedesc" value=""><br />'.
  924:               'Course ID: <input type="text" size="15" name="dccourse" />'.
  925:               '<input type="hidden" name="dcdomain" value="'.$dcdom.'" />'.
  926:               '<input type="hidden" name="origdom" value="'.$dcdom.'" />'.
  927:               '<input type="hidden" name="selectrole" value="" />'.
  928:               '<br /><br /><input type="button" name="proceed" value="'.&mt('Enter this course').'" onClick="verifyCoursePick()" />'.
  929:               '</form>');
  930:     return;
  931: }
  932: 
  933: sub select_recent_courses {
  934:     my ($r,$dcdom,$then,$now)=@_;
  935:     my $advanced = $ENV{'user.adv'};
  936:     my $tryagain = $ENV{'form.tryagain'};
  937:     my %recent=&Apache::lonnet::dump(&recent_filename('cc_pickby_dc_'.$dcdom));
  938:     my $numrecent = 0;
  939:     my $roletext = '<h3>'.&mt('Enter a recently visited course').'</h3>'.
  940:                    '<form name="rolechoice" action="/adm/roles" method="post">'.
  941:                    '<table><tr><th>&nbsp;</th>'.
  942:                    '<th>'.&mt('User Role').'</th><th colspan=2>'.&mt('Extent').
  943:                    '</th><th>'.&mt('Start').'</th><th>'.&mt('End').'</th><th>'.
  944:                    &mt('Remarks and Calendar Announcements').'</th></tr>'."\n".
  945:                    '<tr bgcolor="#BBffBB">'.
  946:                    '<td align="center" colspan="7">'.
  947:                    &mt('Domain').' - '.&mt('Recent courses accessed in this domain').
  948:                     '</td></tr>'."\n";
  949:     foreach my $courseid (sort keys %recent) {
  950:         unless ($courseid =~/^error\:/) {
  951:             my ($dom,$crs) = split/_/,$courseid;
  952:             if ($dom eq $dcdom) {
  953:                 $numrecent ++;
  954:                 my $crskey = 'user.role.cc./'.$dom.'/'.$crs;
  955:                 my ($where,$trolecode,$role,$tstatus,$tend,$tstart,$tbg,$tfont,$tremark,$inrole,$tpstart,$tpend);
  956:                 my $button = 1;
  957:                 &role_status($crskey,$then,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
  958:                 if ($tstart) {
  959:                     $tpstart=&Apache::lonlocal::locallocaltime($tstart);
  960:                 }
  961:                 if ($tend) {
  962:                     $tpend=&Apache::lonlocal::locallocaltime($tend);
  963:                 }
  964:                 my $ttype = &mt('Course');
  965:                 my $twhere;
  966:                 if ($ENV{'course.'.$courseid.'.description'}) {
  967:                     $twhere=$ENV{'course.'.$courseid.'.description'};
  968:                 }
  969:                 my ($tdom,$trest,$tsection)=
  970:                     split(/\//,Apache::lonnet::declutter($where));
  971:                 if ($ENV{'request.role'} eq $trolecode) {
  972:                     $tstatus='selected';
  973:                 }
  974:                 if ($tstatus eq 'is') {
  975:                     $tbg='#77FF77';
  976:                     $tfont='#003300';
  977:                 } elsif ($tstatus eq 'future') {
  978:                     $tbg='#FFFF77';
  979:                     $button=0;
  980:                 } elsif ($tstatus eq 'will') {
  981:                     $tbg='#FFAA77';
  982:                     $tremark.=&mt('Active at next login. ');
  983:                 } elsif ($tstatus eq 'expired') {
  984:                     $tbg='#FF7777';
  985:                     $tfont='#330000';
  986:                     $button=0;
  987:                 } elsif ($tstatus eq 'will_not') {
  988:                     $tbg='#AAFF77';
  989:                     $tremark.=&mt('Expired after logout. ');
  990:                 } elsif ($tstatus eq 'selected') {
  991:                     $tbg='#11CC55';
  992:                     $tfont='#002200';
  993:                     $inrole=1;
  994:                     $tremark.=&mt('Currently selected. ');
  995:                 }
  996:                 my $trole=Apache::lonnet::plaintext($role);
  997:                 $roletext.='<tr bgcolor='.$tbg.'>';
  998:                 if (!$button) {
  999:                     $roletext.=('<td>&nbsp;</td>');
 1000:                 } elsif ($tstatus eq 'is') {
 1001:                     $roletext.=('<td><input type=submit value="'.
 1002:                               &mt('Select').'" name="'.
 1003:                               $trolecode.'"></td>');
 1004:                 } elsif ($tryagain) {
 1005:                     $roletext.=
 1006:                         '<td><input type=submit value="'.
 1007:                         &mt('Try Selecting Again').'" name="'.$trolecode.'"></td>';
 1008:                 } elsif ($advanced) {
 1009:                     $roletext.=
 1010:                         '<td><input type=submit value="'.
 1011:                         &mt('Re-Initialize').'" name="'.$trolecode.'"></td>';
 1012:                 } else {
 1013:                     $roletext.='<td>&nbsp;</td>';
 1014:                 }
 1015:                 $tremark.=&Apache::lonannounce::showday(time,1,
 1016:                          &Apache::lonannounce::readcalendar($tdom.'_'.$trest));
 1017:                                                                                   
 1018:                 $roletext.='<td><font color="'.$tfont.'">'.$trole.
 1019:                       '</font></td><td><font color="'.$tfont.'">'.$ttype.
 1020:                       '</font></td><td><font color="'.$tfont.'">'.$twhere.
 1021:                       '</font></td><td><font color="'.$tfont.'">'.$tpstart.
 1022:                       '</font></td><td><font color="'.$tfont.'">'.$tpend.
 1023:                       '</font></td><td><font color="'.$tfont.'">'.$tremark.
 1024:                       '&nbsp;</font></td></tr>'."\n";
 1025:             }
 1026:         }
 1027:     }
 1028:     if ($numrecent > 0) {
 1029:         $r->print($roletext.'</table><input type="hidden" '.
 1030:                   'name="selectrole" value="1" /></form>'."\n");
 1031:     }
 1032: }
 1033: 
 1034: sub recent_filename {
 1035:     my $area=shift;
 1036:     return 'nohist_recent_'.&Apache::lonnet::escape($area);
 1037: }
 1038: 
 1039: sub set_privileges {
 1040:     my ($dcdom,$pickedcourse) = @_;
 1041:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 1042:     my $role = 'cc';
 1043:     my $spec = $role.'.'.$area;
 1044:     my $userroles = &Apache::lonnet::set_arearole($role,$area,'','',$dcdom,$ENV{'user.name'});
 1045:     my %ccrole = ();
 1046:     &Apache::lonnet::standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
 1047:     $userroles .= &Apache::lonnet::set_userprivs(\$userroles,\%ccrole);                        my @newprivs = split/\n/,$userroles;
 1048:     my %newccroles = ();
 1049:     foreach (@newprivs) {
 1050:         my ($key,$val) = split/=/,$_;
 1051:         $newccroles{$key} = $val;
 1052:     }
 1053:     &Apache::lonnet::appenv(%newccroles);
 1054:     &Apache::lonnet::log($ENV{'user.domain'},
 1055:                          $ENV{'user.name'},
 1056:                          $ENV{'user.home'},
 1057:                         "Role ".$role);
 1058:                                                                                            
 1059:     &Apache::lonnet::appenv(
 1060:                           'request.role'        => $role,
 1061:                           'request.role.domain' => $dcdom,
 1062:                           'request.course.sec'  => '');
 1063:     my $tadv=0;
 1064:     if (&Apache::lonnet::allowed('adv') eq 'F') { $tadv=1; }
 1065:     &Apache::lonnet::appenv('request.role.adv'    => $tadv);
 1066: }
 1067: 
 1068: 1;
 1069: __END__
 1070: 
 1071: =head1 NAME
 1072: 
 1073: Apache::lonroles - User Roles Screen
 1074: 
 1075: =head1 SYNOPSIS
 1076: 
 1077: Invoked by /etc/httpd/conf/srm.conf:
 1078: 
 1079:  <Location /adm/roles>
 1080:  PerlAccessHandler       Apache::lonacc
 1081:  SetHandler perl-script
 1082:  PerlHandler Apache::lonroles
 1083:  ErrorDocument     403 /adm/login
 1084:  ErrorDocument	  500 /adm/errorhandler
 1085:  </Location>
 1086: 
 1087: =head1 OVERVIEW
 1088: 
 1089: =head2 Choosing Roles
 1090: 
 1091: C<lonroles> is a handler that allows a user to switch roles in
 1092: mid-session. LON-CAPA attempts to work with "No Role Specified", the
 1093: default role that a user has before selecting a role, as widely as
 1094: possible, but certain handlers for example need specification which
 1095: course they should act on, etc. Both in this scenario, and when the
 1096: handler determines via C<lonnet>'s C<&allowed> function that a certain
 1097: action is not allowed, C<lonroles> is used as error handler. This
 1098: allows the user to select another role which may have permission to do
 1099: what they were trying to do. C<lonroles> can also be accessed via the
 1100: B<CRS> button in the Remote Control. 
 1101: 
 1102: =begin latex
 1103: 
 1104: \begin{figure}
 1105: \begin{center}
 1106: \includegraphics[width=0.45\paperwidth,keepaspectratio]{Sample_Roles_Screen}
 1107:   \caption{\label{Sample_Roles_Screen}Sample Roles Screen} 
 1108: \end{center}
 1109: \end{figure}
 1110: 
 1111: =end latex
 1112: 
 1113: =head2 Role Initialization
 1114: 
 1115: 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.
 1116: 
 1117: =head1 INTRODUCTION
 1118: 
 1119: This module enables a user to select what role he wishes to
 1120: operate under (instructor, student, teaching assistant, course
 1121: coordinator, etc).  These roles are pre-established by the actions
 1122: of upper-level users.
 1123: 
 1124: This is part of the LearningOnline Network with CAPA project
 1125: described at http://www.lon-capa.org.
 1126: 
 1127: =head1 HANDLER SUBROUTINE
 1128: 
 1129: This routine is called by Apache and mod_perl.
 1130: 
 1131: =over 4
 1132: 
 1133: =item *
 1134: 
 1135: Roles Initialization (yes/no)
 1136: 
 1137: =item *
 1138: 
 1139: Get Error Message from Environment
 1140: 
 1141: =item *
 1142: 
 1143: Who is this?
 1144: 
 1145: =item *
 1146: 
 1147: Generate Page Output
 1148: 
 1149: =item *
 1150: 
 1151: Choice or no choice
 1152: 
 1153: =item *
 1154: 
 1155: Table
 1156: 
 1157: =item *
 1158: 
 1159: Privileges
 1160: 
 1161: =back
 1162: 
 1163: =cut

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