Annotation of loncom/auth/lonroles.pm, revision 1.89

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

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