File:  [LON-CAPA] / loncom / publisher / lonrights.pm
Revision 1.19: download - view: text, annotated - select for diffs
Wed Apr 26 14:53:48 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- don't cache the coursedescription data to the env cache for one time use data

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

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