File:  [LON-CAPA] / loncom / auth / lonroles.pm
Revision 1.16: download - view: text, annotated - select for diffs
Sat Oct 28 19:23:21 2000 UTC (23 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Got rid of special coursedescription, now part of session environment

    1: # The LearningOnline Network with CAPA
    2: # User Roles Screen
    3: # (Directory Indexer
    4: # (Login Screen
    5: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14 Gerd Kortemeyer)
    6: # 11/23 Gerd Kortemeyer)
    7: # 1/14,03/06,06/01,07/22,07/24,07/25,
    8: # 09/04,09/06,09/28,09/29,09/30,10/2,10/5,10/26,10/28 Gerd Kortemeyer
    9: #
   10: package Apache::lonroles;
   11: 
   12: use strict;
   13: use Apache::lonnet();
   14: use Apache::lonuserstate();
   15: use Apache::Constants qw(:common);
   16: use Apache::File();
   17: 
   18: sub handler {
   19: 
   20:     my $r = shift;
   21: 
   22:     my $now=time;
   23:     my $then=$ENV{'user.login.time'};
   24:     my $envkey;
   25: 
   26: 
   27: # ================================================================== Roles Init
   28: 
   29:     if ($ENV{'form.selectrole'}) {
   30:        &Apache::lonnet::appenv("request.course.id"  => '',
   31:                                "request.course.fn"  => '',
   32:                                "request.course.uri" => '',
   33:                                "request.course.sec" => '',
   34:                                "request.role" => 'cm'); 
   35:         foreach $envkey (keys %ENV) {
   36:          if ($envkey=~/^user\.role\./) {
   37: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
   38:             my $where=join('.',@pwhere);
   39:             my $trolecode=$role.'.'.$where;
   40:             if ($ENV{'form.'.$trolecode}) {
   41:                my ($tstart,$tend)=split(/\./,$ENV{$envkey});
   42:                my $tstatus='is';
   43:                if ($tstart) {
   44:       		  if ($tstart>$then) { 
   45:                      $tstatus='future';
   46:                   }
   47:                }
   48:                if ($tend) {
   49:                   if ($tend<$then) { $tstatus='expired'; }
   50:                   if ($tend>$now) { $tstatus='will_not'; }
   51:                }
   52:                if ($tstatus eq 'is') {
   53:                    $where=~s/^\///;
   54:                    my ($cdom,$cnum,$csec)=split(/\//,$where);
   55:                    &Apache::lonnet::appenv('request.role' => $trolecode,
   56:                                            'request.course.sec' => $csec);
   57:                    if ($cnum) {
   58: 		      &Apache::lonuserstate::readmap($cdom.'/'.$cnum);
   59:                       if ($ENV{'form.orgurl'}) {
   60:                          $r->internal_redirect($ENV{'form.orgurl'});
   61:                          return OK;
   62: 		      }
   63:                    }
   64:                }
   65:             } 
   66: 	  }
   67:         }
   68:     }
   69:         
   70: 
   71: # =============================================================== No Roles Init
   72: 
   73:     $r->content_type('text/html');
   74:     $r->send_http_header;
   75:     return OK if $r->header_only;
   76: 
   77:     $r->print(<<ENDHEADER);
   78: <html>
   79: <head>
   80: <title>LON-CAPA User Roles</title>
   81: </head><body bgcolor="#FFFFFF">
   82: ENDHEADER
   83: 
   84: # ------------------------------------------ Get Error Message from Environment
   85: 
   86:     my ($fn,$priv,$nochoose,$error,$msg)=split(/:/,$ENV{'user.error.msg'});
   87:     if ($ENV{'user.error.msg'}) {
   88:        $r->log_reason(
   89:      "$msg for $ENV{'user.name'} domain $ENV{'user.domain'} access $priv",$fn);
   90:     }
   91: 
   92: # ---------------------------------------------------------------- Who is this?
   93: 
   94:     my $advanced=0;
   95:     foreach $envkey (keys %ENV) {
   96:         if ($envkey=~/^user\.role\./) {
   97: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
   98:             if ($role ne 'st') { $advanced=1; }
   99:         }
  100:     }
  101: 
  102: # -------------------------------------------------------- Generate Page Output
  103: # --------------------------------------------------------------- Error Header?
  104:     if ($error) {
  105: 	$r->print("<h1>LON-CAPA Access Control</h1>");
  106:         $r->print("<hr><pre>Access  : ".
  107:                   Apache::lonnet::plaintext($priv)."\n");
  108:         $r->print("Resource: $fn\n");
  109:         $r->print("Action  : $msg\n</pre><hr>");
  110:     } else {
  111:         $r->print("<h1>LON-CAPA User Roles</h1>");
  112:     }
  113: # -------------------------------------------------------- Choice or no choice?
  114:     if ($nochoose) {
  115:         if ($advanced) {
  116: 	   $r->print("<h2>Assigned User Roles</h2>\n");
  117:         } else {
  118:            $r->print("<h2>Sorry ...</h2>\nThis resource might be part of");
  119:            if ($ENV{'request.course.id'}) {
  120: 	       $r->print(' another');
  121:            } else {
  122:                $r->print(' a certain');
  123:            } 
  124:            $r->print(' course.</body></html>');
  125:            return OK;
  126:         } 
  127:     } else {
  128:         if ($advanced) {
  129:            $r->print("<h2>Select a User Role</h2>\n");
  130:         } else {
  131: 	   $r->print("<h2>Enter a Course</h2>\n");
  132:         }
  133:         $r->print('<form method=post action="'.(($fn)?$fn:$r->uri).'">');
  134:         $r->print('<input type=hidden name=orgurl value="'.$fn.'">');
  135:         $r->print('<input type=hidden name=selectrole value=1>');
  136:     }
  137: # ----------------------------------------------------------------------- Table
  138:     $r->print('<table><tr>');
  139:     unless ($nochoose) { $r->print('<th>&nbsp;</th>'); }
  140:     if ($advanced) {
  141:        $r->print('<th>User Role</th><th colspan=2>Extent</th>'.
  142:                  '<th>Start</th><th>End</th><th>Remark</th></tr>'."\n");
  143:     } else {
  144: 	$r->print('<th>Course</th></tr>'."\n");
  145:     }
  146: 
  147:     foreach $envkey (sort keys %ENV) {
  148:         if ($envkey=~/^user\.role\./) {
  149: 	    my ($dum1,$dum2,$role,@pwhere)=split(/\./,$envkey);
  150:             my $where=join('.',@pwhere);
  151:             my $trolecode=$role.'.'.$where;
  152:             my ($tstart,$tend)=split(/\./,$ENV{$envkey});
  153:             my $tremark='';
  154:             my $tstatus='is';
  155:             my $tpstart='&nbsp;';
  156:             my $tpend='&nbsp;';
  157:             if ($tstart) {
  158: 		if ($tstart>$then) { 
  159:                    $tstatus='future';
  160:                    if ($tstart<$now) { $tstatus='will'; }
  161:                 }
  162:                 $tpstart=localtime($tstart);
  163:             }
  164:             if ($tend) {
  165:                 if ($tend<$then) { $tstatus='expired'; }
  166:                 if ($tend>$now) { $tstatus='will_not'; }
  167:                 $tpend=localtime($tend);
  168:             }
  169:             if ($ENV{'request.role'} eq $trolecode) {
  170: 		$tstatus='selected';
  171:             }
  172:             my $tbg;
  173:             if ($tstatus eq 'is') {
  174: 		$tbg='#77FF77';
  175:             } elsif ($tstatus eq 'future') {
  176:                 $tbg='#FFFF77';
  177:             } elsif ($tstatus eq 'will') {
  178:                 $tbg='#FFAA77';
  179:                 $tremark.='Active at next login. ';
  180:             } elsif ($tstatus eq 'expired') {
  181:                 $tbg='#FF7777';
  182: 	    } elsif ($tstatus eq 'will_not') {
  183:                 $tbg='#AAFF77';
  184:                 $tremark.='Expired after logout. ';
  185:             } elsif ($tstatus eq 'selected') {
  186:                 $tbg='#33FF33';
  187:                 $tremark.='Currently selected. ';
  188:             }
  189:             my $trole;
  190:             if ($role =~ /^cr\//) {
  191: 	       my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$role);
  192:                $tremark.='<br>Defined by '.$rauthor.' at '.$rdomain.'.';
  193:                $trole=$rrole;
  194: 	    } else {
  195:                $trole=Apache::lonnet::plaintext($role);
  196:             }
  197:             my $ttype;
  198:             my $twhere;
  199:             my ($tdom,$trest,$tsection)=
  200:                split(/\//,Apache::lonnet::declutter($where));
  201:             if ($trest) {
  202: 		$ttype='Course';
  203:                 if ($tsection) {
  204:                    $ttype.='<br>Section/Group: '.$tsection;
  205:                 }     
  206:                 my $tcourseid=$tdom.'_'.$trest;
  207:                 if ($ENV{'course.'.$tcourseid.'.description'}) {
  208: 		    $twhere=$ENV{'course.'.$tcourseid.'.description'};
  209:                 } else {
  210:                     my %newhash=Apache::lonnet::coursedescription($tcourseid);
  211:                     if (%newhash) {
  212: 			$twhere=$newhash{'description'};
  213:                     } else {
  214:                         $twhere='Currently not available';
  215:                         $ENV{'course.'.$tcourseid.'.description'}=$twhere;
  216:                     }
  217:                 }
  218:             } elsif ($tdom) {
  219:                 $ttype='Domain';
  220:                 $twhere=$tdom;
  221:             } else {
  222:                 $ttype='System';
  223:                 $twhere='system wide';
  224:             }
  225:                
  226:             $r->print('<tr bgcolor='.$tbg.'>');
  227:             unless ($nochoose) {
  228: 		if ($tstatus eq 'is') {
  229:                     $r->print('<td><input type=submit value=Select name="'.
  230:                               $trolecode.'"></td>');
  231:                 } else {
  232:                     $r->print('<td>&nbsp;</td>');
  233:                 }
  234:             }
  235:             $r->print('<td>'.$trole.'</td><td>'.
  236: 		      $ttype.'</td><td>'.$twhere.'</td><td>'.$tpstart.
  237:                       '</td><td>'.$tpend.
  238:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  239:         }
  240:     }
  241:     my $tremark='';
  242:     if ($ENV{'request.role'} eq 'cm') {
  243: 	$r->print('<tr bgcolor="#33FF33">');
  244:         $tremark='Currently selected.';
  245:     } else {
  246:         $r->print('<tr bgcolor="#77FF77">');
  247:     }
  248:     unless ($nochoose) {
  249:        if ($ENV{'request.role'} ne 'cm') {
  250:           $r->print('<td><input type=submit value=Select name="cm"></td>');
  251:        } else {
  252:           $r->print('<td>&nbsp;</td>');
  253:        }
  254:     }
  255:     $r->print('<td colspan=5>No role specified'.
  256:                       '</td><td>'.$tremark.'&nbsp;</td></tr>'."\n");
  257: 
  258:     $r->print('</table>');
  259:     unless ($nochoose) {
  260: 	$r->print("</form>\n");
  261:     }
  262: # ------------------------------------------------------------ Priviledges Info
  263:   if ($advanced) {
  264:     $r->print('<hr><h2>Current Priviledges</h2>');
  265: 
  266:     foreach $envkey (sort keys %ENV) {
  267:         if ($envkey=~/^user\.priv\.$ENV{'request.role'}\./) {
  268:             my $where=$envkey;
  269:             $where=~s/^user\.priv\.$ENV{'request.role'}\.//;
  270:             my $ttype;
  271:             my $twhere;
  272:             my ($tdom,$trest,$tsec)=
  273:                split(/\//,Apache::lonnet::declutter($where));
  274:             if ($trest) {
  275: 		$ttype='Course';
  276:                 $twhere=$ENV{'course.'.$tdom.'_'.$trest.'.description'};
  277:                 if ($tsec) {
  278: 		    $twhere.=' (Section/Group: '.$tsec.')';
  279:                 }
  280:             } elsif ($tdom) {
  281:                 $ttype='Domain';
  282:                 $twhere=$tdom;
  283:             } else {
  284:                 $ttype='System';
  285:                 $twhere='/';
  286:             }
  287:             $r->print("\n<h3>".$ttype.': '.$twhere.'</h3><ul>');
  288:             map {
  289:               if ($_) {
  290: 		  my ($prv,$restr)=split(/\&/,$_);
  291:                   my $trestr='';
  292:                   if ($restr ne 'F') {
  293:                       my $i;
  294:                       $trestr.=' (';
  295:                       for ($i=0;$i<length($restr);$i++) {
  296: 		         $trestr.=
  297:                            Apache::lonnet::plaintext(substr($restr,$i,1));
  298:                          if ($i<length($restr)-1) { $trestr.=', '; }
  299: 		      }
  300:                       $trestr.=')';
  301:                   }
  302:                   $r->print('<li>'.Apache::lonnet::plaintext($prv).$trestr.
  303:                             '</li>');
  304: 	      }
  305:             } sort split(/:/,$ENV{$envkey});
  306:             $r->print('</ul>');
  307:         }
  308:     }
  309:   }
  310: # -------------------------------------------------------------- Debug - remove
  311: 
  312:     $->print("<hr><h1>Debugging</h1><hr>\n");
  313:     
  314:     foreach $envkey (sort keys %ENV) {
  315: 	$r->print("$envkey ---- $ENV{$envkey}<br>");
  316:     }
  317: 
  318: # ------------------------------------------------------------------- End Debug
  319: 
  320:     $r->print("</body></html>\n");
  321:     return OK;
  322: } 
  323: 
  324: 1;
  325: __END__

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