File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.150: download - view: text, annotated - select for diffs
Tue May 30 17:15:00 2006 UTC (18 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #4370: better warning if there are errors during course initialization,
e.g., a map included more than once.

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

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