File:  [LON-CAPA] / loncom / publisher / lonrights.pm
Revision 1.25.2.1: download - view: text, annotated - select for diffs
Wed Jan 27 16:18:17 2010 UTC (14 years, 3 months ago) by raeburn
Branches: version_2_9_X
CVS tags: version_2_9_1, version_2_9_0, version_2_8_99_1
Diff to branchpoint 1.25: preferred, unified
- Backport 1.29.

    1: # The LearningOnline Network with CAPA
    2: # Handler to show and edit custom distribution rights
    3: #
    4: # $Id: lonrights.pm,v 1.25.2.1 2010/01/27 16:18:17 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: 
   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:       &Apache::loncommon::studentbrowser_javascript();
   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 && $env{'form.store'}) {
   70:       
   71:       my @newrules;
   72: 
   73: # read rules from form
   74:       foreach my $key (keys(%env)) {
   75: 	  next if ($key!~/^form\.effect\_(\d+)$/);
   76: 	  my $number=$1;
   77: 	  my %rulehash;
   78: 	  foreach my $action ('effect','type','domain','course','section','role') {
   79: 	      $rulehash{$action}=$env{'form.'.$action.'_'.$number};
   80: 	  }
   81: 	  if ($rulehash{'type'} !~ /^(user|course)$/) {
   82: 	      $rulehash{'type'} = 'course';
   83: 	  }
   84: 	  if ($rulehash{'type'} eq 'user') {
   85: 	      $rulehash{'section'}='';
   86: 	      $rulehash{'role'}='';
   87: 	  }
   88: 	  if ($rulehash{'role'} eq 'au') {
   89: 	      $rulehash{'course'}='';
   90: 	      $rulehash{'section'}='';
   91: 	  }
   92: 	  if ($rulehash{'role'} eq 'cc') {
   93: 	      $rulehash{'section'}='';
   94: 	  }
   95: 	  unless (($rulehash{'effect'} eq 'deny') ||
   96: 		  ($rulehash{'effect'} eq 'allow')) {
   97: 	      $rulehash{'effect'}='deny';
   98: 	  }
   99: 	  $rulehash{'domain'} = 
  100: 	      &LONCAPA::clean_domain($rulehash{'domain'});
  101: 	  if ($rulehash{'type'} eq 'course') {
  102: 	      $rulehash{'course'} = 
  103: 		  &LONCAPA::clean_courseid($rulehash{'course'});
  104: 	  } else {
  105: 	      $rulehash{'course'} = 
  106: 		  &LONCAPA::clean_username($rulehash{'course'});
  107: 	  }
  108: 	  $rulehash{'section'}=~s/\W//g;
  109: 	  if (!$rulehash{'domain'}) { 
  110: 	      $rulehash{'domain'}=$env{'user.domain'}; 
  111: 	  }
  112: 	  my $realm='';
  113: 	  my $separator = ($rulehash{'type'} eq 'course') ? '_' : '/';
  114: 	  if ($number) {
  115: 	      $realm=$rulehash{'domain'};
  116: 	      if ($rulehash{'course'}) {
  117: 		  $realm.=$separator.$rulehash{'course'};
  118: 	      }
  119: 	      if ($rulehash{'section'}) {
  120: 		  $realm.=$separator.$rulehash{'section'};
  121: 	      }
  122: 	  }
  123: 	  $newrules[$number]=$rulehash{'effect'}.':'.
  124: 	      $realm.':'.$rulehash{'role'}.':'.$rulehash{'type'};
  125:       }
  126: # edit actions?
  127:       foreach my $key (keys(%env)) {
  128: 	  next if ($key!~/^form\.action\_(\d+)$/);
  129: 	  my $number=$1;
  130: 	  if ($env{$key} eq 'delete') { splice(@newrules,$number,1); }
  131: 	  if (($env{$key} eq 'moveup') && ($number>1)) {
  132: 	      @newrules[$number-1,$number] = @newrules[$number,$number-1];
  133: 	  }
  134: 	  if (($env{$key} eq 'movedown') && ($number<$#newrules)) {
  135: 	      @newrules[$number+1,$number] = @newrules[$number,$number+1];
  136: 	  }
  137: 	  if ($env{$key} eq 'insertabove') {
  138: 	      splice(@newrules,$number,0,'deny');
  139: 	  }
  140: 	  if ($env{$key} eq 'insertbelow') {
  141: 	      splice(@newrules,$number+1,0,'deny');
  142: 	  }
  143:       }
  144:       
  145: # store file
  146:       my $fh=Apache::File->new('>'.$fn);
  147:       foreach (my $i=0;$i<=$#newrules;$i++) {
  148: 	  if ($newrules[$i]) {
  149: 	      my ($effect,$realm,$role,$type)=split(/\:/,$newrules[$i]);
  150: 	      print $fh
  151: 		  "<accessrule effect='$effect' realm='$realm' role='$role' type='$type' />\n";
  152: 	  }
  153:       }
  154:       $fh->close;
  155:   }
  156: # ============================================================ Read and display
  157:   unless ($constructmode) { 
  158: # =========================================== This is not in construction space
  159:       $contents=&Apache::lonnet::getfile($fn);
  160:       if ($contents==-1) { $contents=''; }
  161:   } else {
  162: # =============================================== This is in construction space
  163:       if (-e $fn) {
  164: 	  my $fh=Apache::File->new($fn);
  165:           $contents=join('',<$fh>);
  166:           $fh->close();
  167:       }
  168:       $r->print('<form name="rules" method="post">');
  169:   }
  170:   unless ($contents=~/\<accessrule/s) {
  171:       $contents='<accessrule effect="deny" />';
  172:   }
  173:   my $parser=HTML::LCParser->new(\$contents);
  174:   my $token;
  175:   my $rulecounter=0;
  176:   my $colzero=&mt($constructmode?'Edit action':'Rule');
  177:   my %lt=&Apache::lonlocal::texthash(
  178: 				     'ef' => 'Effect',
  179: 				     'ty' => 'Type',
  180: 				     'do' => 'Domain',
  181: 				     'co' => 'Course / User',
  182: 				     'se' => 'Section',
  183: 				     'ro' => 'Role');
  184: # ---------------------------------------------------------- Start table output
  185:   $r->print(&Apache::loncommon::start_data_table().
  186: 	    &Apache::loncommon::start_data_table_header_row().
  187: 	    "<th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'ty'}</th><th>$lt{'do'}</th>".
  188: 	    "<th>$lt{'co'}</th><th>$lt{'se'}</th><th>$lt{'ro'}</th>".
  189: 	    &Apache::loncommon::end_data_table_header_row());
  190: # --------------------------------------------------------------------- Default
  191: # Fast forward to first rule
  192:   $token=$parser->get_token;
  193:   while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
  194: # print default
  195:   $r->print(&Apache::loncommon::start_data_table_row().'<td align="right">');
  196:   if ($constructmode) {
  197:      $r->print(&Apache::loncommon::select_form('','action_0',
  198:                                   ('' => '',
  199:                                    'insertbelow' => &mt('Insert rule below'))));
  200:                                                 
  201:   } else {
  202:       $r->print('&nbsp;');
  203:   }
  204:   $r->print('</td><td>');
  205:   if ($constructmode) {
  206:       $r->print(&Apache::loncommon::select_form
  207:                                  ($token->[2]->{'effect'},'effect_0',
  208:                                   &Apache::lonlocal::texthash
  209:                                    ('allow' => 'allow',
  210:                                     'deny'  => 'deny')));
  211:   } else {
  212:       $r->print($token->[2]->{'effect'});
  213:   }
  214:   $r->print('</td><td colspan="5">'.&mt('Default'));
  215:   if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
  216:       $r->print(' - <span class="LC_error">'.&mt('Error! No default set.').
  217: 		'</span>');
  218:   }
  219:   $r->print('</td>'.&Apache::loncommon::end_data_table_row());
  220: # Additional roles
  221:   while ($token=$parser->get_token) {
  222:       if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
  223:           $rulecounter++;
  224: 	  $r->print(&Apache::loncommon::start_data_table_row().
  225: 		     '<td align="right" rowspan="2">');
  226: # insert, delete, etc
  227: 	  $r->print('<span class="LC_nobreak">'.$rulecounter.'. ');
  228:           if ($constructmode) {
  229:              $r->print(&Apache::loncommon::select_form(
  230:                     '','action_'.$rulecounter,
  231:                     &Apache::lonlocal::texthash
  232:                     ('' => '', 
  233:                      'delete' => 'Delete this rule',
  234:                      'insertabove' => 'Insert rule above',
  235:                      'insertbelow' => 'Insert rule below',
  236:                      'moveup'      => 'Move rule up',
  237:                      'movedown'    => 'Move rule down')));
  238: 	  }
  239:           $r->print('</span></td>'.
  240: 		    '<td rowspan="2">');
  241: # effect
  242:           if ($constructmode) {
  243:              $r->print(&Apache::loncommon::select_form
  244:                                  ($token->[2]->{'effect'},
  245:                                   'effect_'.$rulecounter,
  246:                                   &Apache::lonlocal::texthash
  247:                                    ('allow' => 'allow',
  248:                                     'deny'  => 'deny')));
  249:           } else {
  250:              $r->print($token->[2]->{'effect'});
  251:           }
  252: 
  253: # type
  254: 	  $r->print('</td><td rowspan="2">');
  255: 	  my $type = ($token->[2]{'type'} || 'course');
  256:           if ($constructmode) {
  257:               $r->print(&Apache::loncommon::select_form($type,
  258: 							'type_'.$rulecounter,
  259:                             &Apache::lonlocal::texthash
  260: 							('course' => 'Course',
  261: 							 'user'   => 'User')));
  262:           } else {
  263:               $r->print($type);
  264:           }
  265: 
  266: # ---- realm
  267:           my $realm=$token->[2]->{'realm'};
  268:           my ($rdom,$rcourse,$rsec);
  269: 	  if ($type eq 'course') {
  270: 	      ($rdom,$rcourse,$rsec) = split(m{[/_]},$realm,3);
  271: 	      $rcourse = &LONCAPA::clean_courseid($rcourse);
  272: 	  } else {
  273: 	      ($rdom,$rcourse,$rsec) = split(m{/},$realm,3);
  274: 	      $rcourse = &LONCAPA::clean_username($rcourse);
  275: 	  }
  276:           $r->print('</td><td>');
  277: # realm domain
  278:           if ($constructmode) {
  279:               unless ($rdom) { $rdom=$env{'user.domain'}; }
  280:               $r->print(&Apache::loncommon::select_dom_form($rdom,
  281:                                                       'domain_'.$rulecounter));
  282:           } else {
  283:               $r->print($rdom);
  284:           }
  285:           $r->print('</td><td>');
  286: # realm course
  287:           if ($constructmode) {
  288:              $r->print('<input type="text" size="25" name="course_'.
  289:                        $rulecounter.'" value="'.$rcourse.'" />');
  290:           } else {
  291:               $r->print($rcourse);
  292:           }
  293: 
  294:           $r->print('</td><td rowspan="2">');
  295: # realm section
  296: 	  if ($type eq 'course') {
  297: 	      if ($constructmode) {
  298: 		  $r->print('<input type="text" size="5" name="section_'.
  299: 			    $rulecounter.'" value="'.$rsec.'" />');
  300: 	      } else {
  301: 		  $r->print($rsec);
  302: 	      }
  303: 	  }
  304: 
  305:           $r->print('</td><td rowspan="2">');
  306: # role
  307: 	  if ($type eq 'course') {
  308: 	      if ($constructmode) {
  309: 		  my %hash=('' => '');
  310: 		  foreach ('au','cc','in','ta','st') { 
  311: 		      $hash{$_}=&Apache::lonnet::plaintext($_); 
  312: 		  }
  313: 		  my $role=$token->[2]->{'role'};
  314: 		  unless ($role) { $role=''; }
  315: 		  $r->print(&Apache::loncommon::select_form(
  316: 							    $role,'role_'.$rulecounter,%hash));
  317: 	      } else {
  318: 		  $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
  319: 	      }
  320: 	  }
  321: # course selection link
  322:           $r->print('</td>'.
  323: 		    &Apache::loncommon::end_data_table_row().
  324: 		    &Apache::loncommon::continue_data_table_row().
  325: 		    '<td colspan="2" align="right">');
  326: 	  if ($type eq 'course') {
  327: 	      if ($rcourse) {
  328: 		  my %descript=
  329: 		      &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
  330: 							 {'one_time' => 1});
  331: 		  $r->print($descript{'description'}.'&nbsp;&nbsp;&nbsp;');
  332: 	      }
  333: 	      if ($constructmode) {
  334: 		  $r->print(&Apache::loncommon::selectcourse_link('rules',
  335: 								  'course_'.$rulecounter,'domain_'.$rulecounter));
  336: 	      }
  337: 	  } else {
  338: 	      if ($rcourse) {
  339: 		  my $name = &Apache::loncommon::plainname($rcourse,$rdom);
  340: 		  $r->print($name.'&nbsp;&nbsp;&nbsp;');
  341: 	      }
  342: 	      if ($constructmode) {
  343: 		  $r->print(&Apache::loncommon::selectstudent_link('rules','course_'.$rulecounter,'domain_'.$rulecounter));
  344: 	      }
  345: 	  }
  346: # close row
  347: 	  $r->print('</td>'.&Apache::loncommon::end_data_table_row());
  348:      }                                       
  349:   }
  350:   $r->print(&Apache::loncommon::end_data_table());
  351: # ------------------------------------------------------------ End table output
  352:   if ($constructmode) { 
  353:      $r->print('<input type="submit" name="store" value="'.&mt('Save').'" /></form>'); 
  354:   }
  355:   $r->print(&Apache::loncommon::end_page());
  356:   return OK;  
  357: }
  358: 
  359: 
  360: 1;
  361: __END__
  362: 
  363: 
  364: 
  365: 

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