File:  [LON-CAPA] / loncom / publisher / lonrights.pm
Revision 1.12: download - view: text, annotated - select for diffs
Tue Jan 20 21:10:03 2004 UTC (20 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- only show 'Select Course' link when in construct mode

    1: # The LearningOnline Network with CAPA
    2: # Handler to show and edit custom distribution rights
    3: #
    4: # $Id: lonrights.pm,v 1.12 2004/01/20 21:10:03 albertel 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: 
   31: package Apache::lonrights;
   32: 
   33: use strict;
   34: use Apache::Constants qw(:common :http);
   35: use Apache::lonnet();
   36: use Apache::loncommon();
   37: use HTML::LCParser;
   38: use Apache::File;
   39: use Apache::lonlocal;
   40: 
   41: sub handler {
   42: 
   43:   my $r=shift;
   44:   &Apache::loncommon::content_type($r,'text/html');
   45:   $r->send_http_header;
   46: 
   47:   $r->print(
   48:      '<html><head><title>LON-CAPA Custom Distribution Rights</title>'.
   49:    &Apache::loncommon::coursebrowser_javascript().'</head>');
   50: 
   51:   $r->print(&Apache::loncommon::bodytag('Custom Distribution Rights'));
   52:   $r->rflush();
   53: 
   54:   my $uri=$r->uri;
   55:   my $fn=&Apache::lonnet::filelocation('',$uri);
   56:   my $contents='';
   57:   my $constructmode=($uri=~/^\/\~/);
   58: 
   59: # ============================================================ Modify and store
   60:   if ($constructmode) {
   61:       if ($ENV{'form.store'}) {
   62: 	  my @newrules=();
   63:           undef @newrules;
   64: # read rules from form
   65:           foreach (keys %ENV) {
   66: 	      if ($_=~/^form\.effect\_(\d+)$/) {
   67: 		  my $number=$1;
   68:                   my %rulehash=();
   69:                   foreach ('effect','domain','course','section','role') {
   70: 		      $rulehash{$_}=$ENV{'form.'.$_.'_'.$number};
   71:                   }
   72:                   if ($rulehash{'role'} eq 'au') {
   73: 		      $rulehash{'course'}='';
   74:                       $rulehash{'section'}='';
   75:                   }
   76:                   if ($rulehash{'role'} eq 'cc') {
   77:                       $rulehash{'section'}='';
   78:                   }
   79:                   unless (($rulehash{'effect'} eq 'deny') ||
   80: 		          ($rulehash{'effect'} eq 'allow')) {
   81: 		      $rulehash{'effect'}='deny';
   82:                   }
   83:                   $rulehash{'domain'}=~s/\W//g;
   84:                   $rulehash{'course'}=~s/\W//g;
   85:                   $rulehash{'section'}=~s/\W//g;
   86:                   unless ($rulehash{'domain'}) { 
   87:                      $rulehash{'domain'}=$ENV{'user.domain'}; 
   88:                   }
   89:                   my $realm='';
   90:                   if ($number) {
   91:                      $realm=$rulehash{'domain'};
   92:                      if ($rulehash{'course'}) {
   93: 			 $realm.='_'.$rulehash{'course'};
   94:                      }
   95:                      if ($rulehash{'section'}) {
   96: 			 $realm.='_'.$rulehash{'section'};
   97:                      }
   98: 		 }
   99: 		  $newrules[$number]=$rulehash{'effect'}.':'.
  100: 		                     $realm.':'.$rulehash{'role'};
  101:               }
  102:           }
  103: # edit actions?
  104:           foreach (keys %ENV) {
  105: 	      if ($_=~/^form\.action\_(\d+)$/) {
  106:                   my $number=$1;
  107: 		  if ($ENV{$_} eq 'delete') { $newrules[$number]=''; }
  108:                   if (($ENV{$_} eq 'moveup') && ($number>2)) {
  109: 		      my $buffer=$newrules[$number];
  110:                       $newrules[$number]=$newrules[$number-1];
  111:                       $newrules[$number-1]=$buffer;
  112:                   }
  113:                   if (($ENV{$_} eq 'movedown') && ($number<$#newrules)) {
  114: 		      my $buffer=$newrules[$number];
  115:                       $newrules[$number]=$newrules[$number+1];
  116:                       $newrules[$number+1]=$buffer;
  117:                   }
  118:                   if ($ENV{$_} eq 'insertabove') {
  119: 		      for (my $i=$#newrules;$i>=$number;$i--) {
  120: 			  $newrules[$i+1]=$newrules[$i];
  121:                       }
  122:                       $newrules[$number]='deny';
  123:                   }
  124:                   if ($ENV{$_} eq 'insertbelow') {
  125:  		      for (my $i=$#newrules;$i>$number;$i--) {
  126: 			  $newrules[$i+1]=$newrules[$i];
  127:                       }
  128:                       $newrules[$number+1]='deny';
  129:                  }
  130: 	      }
  131:           }
  132: 
  133: # store file
  134:           my $fh=Apache::File->new('>'.$fn);
  135:           foreach (my $i=0;$i<=$#newrules;$i++) {
  136:               if ($newrules[$i]) {
  137: 	         my ($effect,$realm,$role)=split(/\:/,$newrules[$i]);
  138:                  print $fh
  139: 	       "<accessrule effect='$effect' realm='$realm' role='$role' />\n";
  140: 	     }
  141:           }
  142:           $fh->close;
  143:       }
  144:   }
  145: # ============================================================ Read and display
  146:   unless ($constructmode) { 
  147: # =========================================== This is not in construction space
  148:       $contents=&Apache::lonnet::getfile($fn);
  149:       if ($contents==-1) { $contents=''; }
  150:   } else {
  151: # =============================================== This is in construction space
  152:       if (-e $fn) {
  153: 	  my $fh=Apache::File->new($fn);
  154:           $contents=join('',<$fh>);
  155:           $fh->close();
  156:       }
  157:       $r->print('<form name="rules" method="post">');
  158:   }
  159:   unless ($contents=~/\<accessrule/s) {
  160:       $contents='<accessrule effect="deny" />';
  161:   }
  162:   my $parser=HTML::LCParser->new(\$contents);
  163:   my $token;
  164:   my $rulecounter=0;
  165:   my $colzero=&mt($constructmode?'Edit action':'Rule');
  166:   my %lt=&Apache::lonlocal::texthash('ef' => 'Effect',
  167: 				     'do' => 'Domain',
  168: 				     'co' => 'Course',
  169: 				     'se' => 'Section/Group',
  170: 				     'ro' => 'Role');
  171: # ---------------------------------------------------------- Start table output
  172:   $r->print(<<ENDSTARTTABLE);
  173: <table border="2">
  174:     <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
  175: <th>$lt{'se'}</th><th>$lt{'ro'}</th></tr>
  176: ENDSTARTTABLE
  177: # --------------------------------------------------------------------- Default
  178: # Fast forward to first rule
  179:   $token=$parser->get_token;
  180:   while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
  181: # print default
  182:   $r->print('<tr><td align="right">');
  183:   if ($constructmode) {
  184:      $r->print(&Apache::loncommon::select_form('','action_0',
  185:                                   ('' => '',
  186: 				   'insertbelow' => 'Insert rule below    ')));
  187:                                                 
  188:   } else {
  189:       $r->print('&nbsp;');
  190:   }
  191:   $r->print('</td><td>');
  192:   if ($constructmode) {
  193:       $r->print(&Apache::loncommon::select_form
  194:                                  ($token->[2]->{'effect'},'effect_0',
  195:                                    ('allow' => 'allow',
  196:                                     'deny'  => 'deny')));
  197:   } else {
  198:       $r->print($token->[2]->{'effect'});
  199:   }
  200:   $r->print('</td><td colspan="4">Default');
  201:   if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
  202:       $r->print(' - <font color="red">'.&mt('Error! No default set.').
  203: 		'</font>');
  204:   }
  205:   $r->print('</td></tr>');
  206: # Additional roles
  207:   while ($token=$parser->get_token) {
  208:       if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
  209:           $rulecounter++;
  210: 	  $r->print('<tr><td align="right" rowspan="2">');
  211: # insert, delete, etc
  212: 	  $r->print($rulecounter.'.&nbsp;');
  213:           if ($constructmode) {
  214:              $r->print(&Apache::loncommon::select_form(
  215:                     '','action_'.$rulecounter,
  216:                     ('' => '', 
  217:                      'delete' => 'Delete this rule',
  218:                      'insertabove' => 'Insert rule above',
  219:                      'insertbelow' => 'Insert rule below    ',
  220:                      'moveup'      => 'Move rule up',
  221:                      'movedown'    => 'Move rule down')));
  222: 	  }
  223:           $r->print('</td><td rowspan="2">');
  224: # effect
  225:           if ($constructmode) {
  226:              $r->print(&Apache::loncommon::select_form
  227:                                  ($token->[2]->{'effect'},
  228:                                   'effect_'.$rulecounter,
  229:                                    ('allow' => 'allow',
  230:                                     'deny'  => 'deny')));
  231:           } else {
  232:              $r->print($token->[2]->{'effect'});
  233:           }
  234: 	  $r->print('</td><td>');
  235: # ---- realm
  236:           my $realm=$token->[2]->{'realm'};
  237:           $realm=~s/^\W//;
  238:           my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
  239: # realm domain
  240:           if ($constructmode) {
  241:               unless ($rdom) { $rdom=$ENV{'user.domain'}; }
  242:               $r->print(&Apache::loncommon::select_dom_form($rdom,
  243:                                                       'domain_'.$rulecounter));
  244:           } else {
  245:               $r->print($rdom);
  246:           }
  247:           $r->print('</td><td>');
  248: # realm course
  249:           if ($constructmode) {
  250:              $r->print('<input input type="text" size="25" name="course_'.
  251:                        $rulecounter.'" value="'.$rcourse.'" />');
  252:           } else {
  253:               $r->print($rcourse);
  254:           }
  255: 
  256:           $r->print('</td><td>');
  257: # realm section
  258:           if ($constructmode) {
  259:              $r->print('<input input type="text" size="5" name="section_'.
  260:                        $rulecounter.'" value="'.$rsec.'" />');
  261:           } else {
  262:               $r->print($rsec);
  263:           }
  264: 
  265:           $r->print('</td><td rowspan="2">');
  266: # role
  267:           if ($constructmode) {
  268: 	      my %hash=('' => '');
  269:               foreach ('au','cc','in','ta','st') { 
  270:                  $hash{$_}=&Apache::lonnet::plaintext($_); 
  271:               }
  272:               my $role=$token->[2]->{'role'};
  273:               unless ($role) { $role=''; }
  274:               $r->print(&Apache::loncommon::select_form(
  275:                 $role,'role_'.$rulecounter,%hash));
  276:           } else {
  277:               $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
  278:           }
  279: # course selection link
  280:           $r->print('</td></tr><tr><td colspan="3" align="right">');
  281:           if ($rcourse) {
  282: 	      my %descript=
  283:                  &Apache::lonnet::coursedescription($rdom.'_'.$rcourse);
  284:               $r->print($descript{'description'}.'&nbsp;&nbsp;&nbsp;');
  285:           }
  286: 	  if ($constructmode) {
  287: 	      $r->print(&Apache::loncommon::selectcourse_link('rules',
  288: 		        'course_'.$rulecounter,'domain_'.$rulecounter));
  289: 	  }
  290: # close row
  291:           $r->print('</td></tr>');
  292:      }                                       
  293:   }
  294:   $r->print('</table>');
  295: # ------------------------------------------------------------ End table output
  296:   if ($constructmode) { 
  297:      $r->print('<input type="submit" name="store" value="'.&mt('Store').'" /></form>'); 
  298:   }
  299:   $r->print('</body></html>');
  300:   return OK;  
  301: }
  302: 
  303: 
  304: 1;
  305: __END__
  306: 
  307: 
  308: 
  309: 

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