File:  [LON-CAPA] / loncom / publisher / lonrights.pm
Revision 1.36: download - view: text, annotated - select for diffs
Thu Jul 13 22:21:26 2023 UTC (9 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Breadcrumbs text when displaying rights file in Course Authoring Space.
- Editing of default.rights file at top level of Course Authoring Space
  disabled.

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

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