File:  [LON-CAPA] / loncom / publisher / lonrights.pm
Revision 1.33: download - view: text, annotated - select for diffs
Mon Nov 14 00:20:31 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- &authorspace() routine now takes an argument: $uri
  so "Construction Space" breadcrumb points at appropriate author space
  for resource/directory being viewed or acted om.
  - current role could be different or ("no role" - i.e., cm), so is only
    used to determine authorspace when $uri is unavailable.

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

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