File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.108: download - view: text, annotated - select for diffs
Fri Nov 12 15:33:32 2004 UTC (19 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
DC selection of CC roles.  Intermediate page eliminated.  Ordering of roles reverted to original order as per v 1.103.  Recent courses visited by DC now displayed at top of roles page. "Select Course" link for DC to go to open pickcourse window now displayed on main roles page.

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

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