Annotation of loncom/publisher/lonrights.pm, revision 1.27

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to show and edit custom distribution rights
                      3: #
1.27    ! bisitz      4: # $Id: lonrights.pm,v 1.26 2009/05/26 11:21:47 bisitz Exp $
1.1       www         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);
1.17      albertel   35: use Apache::lonnet;
1.1       www        36: use Apache::loncommon();
1.2       www        37: use HTML::LCParser;
                     38: use Apache::File;
1.10      www        39: use Apache::lonlocal;
1.1       www        40: 
                     41: sub handler {
                     42: 
                     43:   my $r=shift;
1.17      albertel   44:   my $target = $env{'form.grade_target'};
1.13      albertel   45:   if ($target eq 'meta') {
                     46:       &Apache::loncommon::content_type($r,'text/html');
                     47:       $r->send_http_header;
1.17      albertel   48:       $env{'request.uri'}=$r->uri;
1.13      albertel   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:   }
1.10      www        55:   &Apache::loncommon::content_type($r,'text/html');
1.1       www        56:   $r->send_http_header;
                     57: 
1.26      bisitz     58:   # Breadcrumbs
                     59:   &Apache::lonhtmlcommon::clear_breadcrumbs();
                     60:   &Apache::lonhtmlcommon::add_breadcrumb({
                     61:       'text'  => 'Construction Space',
1.27    ! bisitz     62:       'href'  => &Apache::loncommon::authorspace(),
1.26      bisitz     63:       });
                     64:   &Apache::lonhtmlcommon::add_breadcrumb({
                     65:       'text'  => 'Custom Rights Editor',
                     66:       'title' => 'Custom Distribution Rights Editor',
                     67:       'href'  => '',});
                     68: 
1.23      albertel   69:   my $js = &Apache::loncommon::coursebrowser_javascript().
                     70:       &Apache::loncommon::studentbrowser_javascript();
1.26      bisitz     71:   $r->print(&Apache::loncommon::start_page('Custom Distribution Rights',$js)
                     72:            .&Apache::lonhtmlcommon::breadcrumbs()
                     73:            .&Apache::loncommon::head_subbox(
                     74:                 &Apache::loncommon::CSTR_pageheader())
                     75:   );
1.5       www        76:   $r->rflush();
1.1       www        77: 
1.2       www        78:   my $uri=$r->uri;
                     79:   my $fn=&Apache::lonnet::filelocation('',$uri);
                     80:   my $contents='';
                     81:   my $constructmode=($uri=~/^\/\~/);
1.5       www        82: 
                     83: # ============================================================ Modify and store
1.23      albertel   84:   if ($constructmode && $env{'form.store'}) {
                     85:       
                     86:       my @newrules;
                     87: 
1.5       www        88: # read rules from form
1.23      albertel   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='';
1.24      albertel  128: 	  my $separator = ($rulehash{'type'} eq 'course') ? '_' : '/';
1.23      albertel  129: 	  if ($number) {
                    130: 	      $realm=$rulehash{'domain'};
                    131: 	      if ($rulehash{'course'}) {
1.24      albertel  132: 		  $realm.=$separator.$rulehash{'course'};
1.23      albertel  133: 	      }
                    134: 	      if ($rulehash{'section'}) {
1.24      albertel  135: 		  $realm.=$separator.$rulehash{'section'};
1.23      albertel  136: 	      }
                    137: 	  }
                    138: 	  $newrules[$number]=$rulehash{'effect'}.':'.
                    139: 	      $realm.':'.$rulehash{'role'}.':'.$rulehash{'type'};
                    140:       }
1.5       www       141: # edit actions?
1.23      albertel  142:       foreach my $key (keys(%env)) {
                    143: 	  next if ($key!~/^form\.action\_(\d+)$/);
                    144: 	  my $number=$1;
1.24      albertel  145: 	  if ($env{$key} eq 'delete') { splice(@newrules,$number,1); }
1.23      albertel  146: 	  if (($env{$key} eq 'moveup') && ($number>1)) {
1.24      albertel  147: 	      @newrules[$number-1,$number] = @newrules[$number,$number-1];
1.23      albertel  148: 	  }
                    149: 	  if (($env{$key} eq 'movedown') && ($number<$#newrules)) {
1.24      albertel  150: 	      @newrules[$number+1,$number] = @newrules[$number,$number+1];
1.23      albertel  151: 	  }
                    152: 	  if ($env{$key} eq 'insertabove') {
1.24      albertel  153: 	      splice(@newrules,$number,0,'deny');
1.23      albertel  154: 	  }
                    155: 	  if ($env{$key} eq 'insertbelow') {
1.24      albertel  156: 	      splice(@newrules,$number+1,0,'deny');
1.23      albertel  157: 	  }
                    158:       }
                    159:       
1.5       www       160: # store file
1.23      albertel  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: 	  }
1.5       www       168:       }
1.23      albertel  169:       $fh->close;
1.5       www       170:   }
                    171: # ============================================================ Read and display
1.2       www       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:       }
1.8       www       183:       $r->print('<form name="rules" method="post">');
1.2       www       184:   }
1.5       www       185:   unless ($contents=~/\<accessrule/s) {
                    186:       $contents='<accessrule effect="deny" />';
                    187:   }
1.2       www       188:   my $parser=HTML::LCParser->new(\$contents);
                    189:   my $token;
1.3       www       190:   my $rulecounter=0;
1.10      www       191:   my $colzero=&mt($constructmode?'Edit action':'Rule');
1.25      bisitz    192:   my %lt=&Apache::lonlocal::texthash(
                    193: 				     'ef' => 'Effect',
                    194: 				     'ty' => 'Type',
1.11      albertel  195: 				     'do' => 'Domain',
1.24      albertel  196: 				     'co' => 'Course / User',
1.20      raeburn   197: 				     'se' => 'Section',
1.15      www       198: 				     'ro' => 'Role');
1.3       www       199: # ---------------------------------------------------------- Start table output
1.23      albertel  200:   $r->print(&Apache::loncommon::start_data_table().
                    201: 	    &Apache::loncommon::start_data_table_header_row().
1.25      bisitz    202: 	    "<th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'ty'}</th><th>$lt{'do'}</th>".
1.23      albertel  203: 	    "<th>$lt{'co'}</th><th>$lt{'se'}</th><th>$lt{'ro'}</th>".
                    204: 	    &Apache::loncommon::end_data_table_header_row());
1.3       www       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
1.23      albertel  210:   $r->print(&Apache::loncommon::start_data_table_row().'<td align="right">');
1.6       www       211:   if ($constructmode) {
                    212:      $r->print(&Apache::loncommon::select_form('','action_0',
1.7       www       213:                                   ('' => '',
1.25      bisitz    214: 				   'insertbelow' => 'Insert rule below')));
1.6       www       215:                                                 
                    216:   } else {
                    217:       $r->print('&nbsp;');
                    218:   }
                    219:   $r->print('</td><td>');
1.3       www       220:   if ($constructmode) {
                    221:       $r->print(&Apache::loncommon::select_form
                    222:                                  ($token->[2]->{'effect'},'effect_0',
                    223:                                    ('allow' => 'allow',
                    224:                                     'deny'  => 'deny')));
                    225:   } else {
                    226:       $r->print($token->[2]->{'effect'});
                    227:   }
1.23      albertel  228:   $r->print('</td><td colspan="5">'.&mt('Default'));
1.3       www       229:   if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
1.23      albertel  230:       $r->print(' - <span class="LC_error">'.&mt('Error! No default set.').
                    231: 		'</span>');
1.3       www       232:   }
1.23      albertel  233:   $r->print('</td>'.&Apache::loncommon::end_data_table_row());
1.3       www       234: # Additional roles
1.2       www       235:   while ($token=$parser->get_token) {
                    236:       if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
1.3       www       237:           $rulecounter++;
1.23      albertel  238: 	  $r->print(&Apache::loncommon::start_data_table_row().
                    239: 		     '<td align="right" rowspan="2">');
1.4       www       240: # insert, delete, etc
1.23      albertel  241: 	  $r->print('<span class="LC_nobreak">'.$rulecounter.'. ');
1.4       www       242:           if ($constructmode) {
                    243:              $r->print(&Apache::loncommon::select_form(
                    244:                     '','action_'.$rulecounter,
                    245:                     ('' => '', 
                    246:                      'delete' => 'Delete this rule',
                    247:                      'insertabove' => 'Insert rule above',
1.25      bisitz    248:                      'insertbelow' => 'Insert rule below',
1.4       www       249:                      'moveup'      => 'Move rule up',
                    250:                      'movedown'    => 'Move rule down')));
                    251: 	  }
1.23      albertel  252:           $r->print('</span></td>'.
                    253: 		    '<td rowspan="2">');
1.3       www       254: # effect
                    255:           if ($constructmode) {
                    256:              $r->print(&Apache::loncommon::select_form
                    257:                                  ($token->[2]->{'effect'},
                    258:                                   'effect_'.$rulecounter,
                    259:                                    ('allow' => 'allow',
                    260:                                     'deny'  => 'deny')));
                    261:           } else {
                    262:              $r->print($token->[2]->{'effect'});
                    263:           }
1.23      albertel  264: 
                    265: # type
                    266: 	  $r->print('</td><td rowspan="2">');
                    267: 	  my $type = ($token->[2]{'type'} || 'course');
                    268:           if ($constructmode) {
                    269:               $r->print(&Apache::loncommon::select_form($type,
                    270: 							'type_'.$rulecounter,
                    271: 							('course' => 'Course',
                    272: 							 'user'   => 'User')));
                    273:           } else {
                    274:               $r->print($type);
                    275:           }
                    276: 
1.4       www       277: # ---- realm
1.3       www       278:           my $realm=$token->[2]->{'realm'};
1.24      albertel  279:           my ($rdom,$rcourse,$rsec);
1.23      albertel  280: 	  if ($type eq 'course') {
1.24      albertel  281: 	      ($rdom,$rcourse,$rsec) = split(m{[/_]},$realm,3);
1.23      albertel  282: 	      $rcourse = &LONCAPA::clean_courseid($rcourse);
                    283: 	  } else {
1.24      albertel  284: 	      ($rdom,$rcourse,$rsec) = split(m{/},$realm,3);
1.23      albertel  285: 	      $rcourse = &LONCAPA::clean_username($rcourse);
                    286: 	  }
                    287:           $r->print('</td><td>');
1.7       www       288: # realm domain
1.3       www       289:           if ($constructmode) {
1.17      albertel  290:               unless ($rdom) { $rdom=$env{'user.domain'}; }
1.3       www       291:               $r->print(&Apache::loncommon::select_dom_form($rdom,
                    292:                                                       'domain_'.$rulecounter));
                    293:           } else {
1.4       www       294:               $r->print($rdom);
                    295:           }
                    296:           $r->print('</td><td>');
                    297: # realm course
                    298:           if ($constructmode) {
1.23      albertel  299:              $r->print('<input type="text" size="25" name="course_'.
1.4       www       300:                        $rulecounter.'" value="'.$rcourse.'" />');
                    301:           } else {
                    302:               $r->print($rcourse);
1.3       www       303:           }
1.4       www       304: 
1.23      albertel  305:           $r->print('</td><td rowspan="2">');
1.4       www       306: # realm section
1.23      albertel  307: 	  if ($type eq 'course') {
                    308: 	      if ($constructmode) {
                    309: 		  $r->print('<input type="text" size="5" name="section_'.
                    310: 			    $rulecounter.'" value="'.$rsec.'" />');
                    311: 	      } else {
                    312: 		  $r->print($rsec);
                    313: 	      }
                    314: 	  }
1.4       www       315: 
1.8       www       316:           $r->print('</td><td rowspan="2">');
1.3       www       317: # role
1.23      albertel  318: 	  if ($type eq 'course') {
                    319: 	      if ($constructmode) {
                    320: 		  my %hash=('' => '');
                    321: 		  foreach ('au','cc','in','ta','st') { 
                    322: 		      $hash{$_}=&Apache::lonnet::plaintext($_); 
                    323: 		  }
                    324: 		  my $role=$token->[2]->{'role'};
                    325: 		  unless ($role) { $role=''; }
                    326: 		  $r->print(&Apache::loncommon::select_form(
                    327: 							    $role,'role_'.$rulecounter,%hash));
                    328: 	      } else {
                    329: 		  $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
                    330: 	      }
                    331: 	  }
1.8       www       332: # course selection link
1.23      albertel  333:           $r->print('</td>'.
                    334: 		    &Apache::loncommon::end_data_table_row().
                    335: 		    &Apache::loncommon::continue_data_table_row().
                    336: 		    '<td colspan="2" align="right">');
                    337: 	  if ($type eq 'course') {
                    338: 	      if ($rcourse) {
                    339: 		  my %descript=
                    340: 		      &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
                    341: 							 {'one_time' => 1});
                    342: 		  $r->print($descript{'description'}.'&nbsp;&nbsp;&nbsp;');
                    343: 	      }
                    344: 	      if ($constructmode) {
                    345: 		  $r->print(&Apache::loncommon::selectcourse_link('rules',
                    346: 								  'course_'.$rulecounter,'domain_'.$rulecounter));
                    347: 	      }
                    348: 	  } else {
                    349: 	      if ($rcourse) {
                    350: 		  my $name = &Apache::loncommon::plainname($rcourse,$rdom);
                    351: 		  $r->print($name.'&nbsp;&nbsp;&nbsp;');
                    352: 	      }
                    353: 	      if ($constructmode) {
                    354: 		  $r->print(&Apache::loncommon::selectstudent_link('rules','course_'.$rulecounter,'domain_'.$rulecounter));
                    355: 	      }
1.12      albertel  356: 	  }
1.3       www       357: # close row
1.23      albertel  358: 	  $r->print('</td>'.&Apache::loncommon::end_data_table_row());
1.2       www       359:      }                                       
1.3       www       360:   }
1.23      albertel  361:   $r->print(&Apache::loncommon::end_data_table());
1.3       www       362: # ------------------------------------------------------------ End table output
                    363:   if ($constructmode) { 
1.22      albertel  364:      $r->print('<input type="submit" name="store" value="'.&mt('Save').'" /></form>'); 
1.2       www       365:   }
1.18      albertel  366:   $r->print(&Apache::loncommon::end_page());
1.1       www       367:   return OK;  
                    368: }
                    369: 
                    370: 
                    371: 1;
                    372: __END__
                    373: 
                    374: 
                    375: 
                    376: 

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