Diff for /loncom/publisher/lonrights.pm between versions 1.4 and 1.22

version 1.4, 2003/03/20 19:20:31 version 1.22, 2007/05/02 01:34:23
Line 32  package Apache::lonrights; Line 32  package Apache::lonrights;
   
 use strict;  use strict;
 use Apache::Constants qw(:common :http);  use Apache::Constants qw(:common :http);
 use Apache::lonnet();  use Apache::lonnet;
 use Apache::loncommon();  use Apache::loncommon();
 use HTML::LCParser;  use HTML::LCParser;
 use Apache::File;  use Apache::File;
   use Apache::lonlocal;
   
 sub handler {  sub handler {
   
   my $r=shift;    my $r=shift;
   $r->content_type('text/html');    my $target = $env{'form.grade_target'};
     if ($target eq 'meta') {
         &Apache::loncommon::content_type($r,'text/html');
         $r->send_http_header;
         $env{'request.uri'}=$r->uri;
         my $file = &Apache::lonnet::filelocation("",$r->uri);
         my $content=&Apache::lonnet::getfile($file);
         my $result=&Apache::lonxml::xmlparse(undef,'meta',$content);
         $r->print($result);
         return OK;
     }
     &Apache::loncommon::content_type($r,'text/html');
   $r->send_http_header;    $r->send_http_header;
   
   $r->print(    my $js = &Apache::loncommon::coursebrowser_javascript();
      '<html><head><title>LON-CAPA Custom Distribution Rights</title></head>');  
   
   $r->print(&Apache::loncommon::bodytag('Custom Distribution Rights'));    $r->print(&Apache::loncommon::start_page('Custom Distribution Rights',$js));
     $r->rflush();
   
   my $uri=$r->uri;    my $uri=$r->uri;
   my $fn=&Apache::lonnet::filelocation('',$uri);    my $fn=&Apache::lonnet::filelocation('',$uri);
   my $contents='';    my $contents='';
   my $constructmode=($uri=~/^\/\~/);    my $constructmode=($uri=~/^\/\~/);
   
   # ============================================================ Modify and store
     if ($constructmode) {
         if ($env{'form.store'}) {
     my @newrules=();
             undef @newrules;
   # read rules from form
             foreach (keys %env) {
         if ($_=~/^form\.effect\_(\d+)$/) {
     my $number=$1;
                     my %rulehash=();
                     foreach ('effect','domain','course','section','role') {
         $rulehash{$_}=$env{'form.'.$_.'_'.$number};
                     }
                     if ($rulehash{'role'} eq 'au') {
         $rulehash{'course'}='';
                         $rulehash{'section'}='';
                     }
                     if ($rulehash{'role'} eq 'cc') {
                         $rulehash{'section'}='';
                     }
                     unless (($rulehash{'effect'} eq 'deny') ||
             ($rulehash{'effect'} eq 'allow')) {
         $rulehash{'effect'}='deny';
                     }
                     $rulehash{'domain'} = &LONCAPA::clean_domain($rulehash{'domain'});
     $rulehash{'course'} = &LONCAPA::clean_courseid($rulehash{'course'});
                     $rulehash{'section'}=~s/\W//g;
                     unless ($rulehash{'domain'}) { 
                        $rulehash{'domain'}=$env{'user.domain'}; 
                     }
                     my $realm='';
                     if ($number) {
                        $realm=$rulehash{'domain'};
                        if ($rulehash{'course'}) {
    $realm.='_'.$rulehash{'course'};
                        }
                        if ($rulehash{'section'}) {
    $realm.='_'.$rulehash{'section'};
                        }
    }
     $newrules[$number]=$rulehash{'effect'}.':'.
                        $realm.':'.$rulehash{'role'};
                 }
             }
   # edit actions?
             foreach (keys %env) {
         if ($_=~/^form\.action\_(\d+)$/) {
                     my $number=$1;
     if ($env{$_} eq 'delete') { $newrules[$number]=''; }
                     if (($env{$_} eq 'moveup') && ($number>1)) {
         my $buffer=$newrules[$number];
                         $newrules[$number]=$newrules[$number-1];
                         $newrules[$number-1]=$buffer;
                     }
                     if (($env{$_} eq 'movedown') && ($number<$#newrules)) {
         my $buffer=$newrules[$number];
                         $newrules[$number]=$newrules[$number+1];
                         $newrules[$number+1]=$buffer;
                     }
                     if ($env{$_} eq 'insertabove') {
         for (my $i=$#newrules;$i>=$number;$i--) {
     $newrules[$i+1]=$newrules[$i];
                         }
                         $newrules[$number]='deny';
                     }
                     if ($env{$_} eq 'insertbelow') {
          for (my $i=$#newrules;$i>$number;$i--) {
     $newrules[$i+1]=$newrules[$i];
                         }
                         $newrules[$number+1]='deny';
                    }
         }
             }
   
   # store file
             my $fh=Apache::File->new('>'.$fn);
             foreach (my $i=0;$i<=$#newrules;$i++) {
                 if ($newrules[$i]) {
            my ($effect,$realm,$role)=split(/\:/,$newrules[$i]);
                    print $fh
          "<accessrule effect='$effect' realm='$realm' role='$role' />\n";
        }
             }
             $fh->close;
         }
     }
   # ============================================================ Read and display
   unless ($constructmode) {     unless ($constructmode) { 
 # =========================================== This is not in construction space  # =========================================== This is not in construction space
       $contents=&Apache::lonnet::getfile($fn);        $contents=&Apache::lonnet::getfile($fn);
Line 63  sub handler { Line 163  sub handler {
           $contents=join('',<$fh>);            $contents=join('',<$fh>);
           $fh->close();            $fh->close();
       }        }
       $r->print('<form method="post">');        $r->print('<form name="rules" method="post">');
     }
     unless ($contents=~/\<accessrule/s) {
         $contents='<accessrule effect="deny" />';
   }    }
   my $parser=HTML::LCParser->new(\$contents);    my $parser=HTML::LCParser->new(\$contents);
   my $token;    my $token;
   my $rulecounter=0;    my $rulecounter=0;
   my $colzero=($constructmode?'Edit action':'Rule');    my $colzero=&mt($constructmode?'Edit action':'Rule');
     my %lt=&Apache::lonlocal::texthash('ef' => 'Effect',
        'do' => 'Domain',
        'co' => 'Course',
        'se' => 'Section',
        'ro' => 'Role');
 # ---------------------------------------------------------- Start table output  # ---------------------------------------------------------- Start table output
   $r->print(<<ENDSTARTTABLE);    $r->print(<<ENDSTARTTABLE);
 <table border="2">  <table border="2">
     <tr><th>$colzero</th><th>Effect</th><th>Domain</th><th>Course</th>      <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
 <th>Section</th><th>Role</th></tr>  <th>$lt{'se'}</th><th>$lt{'ro'}</th></tr>
 ENDSTARTTABLE  ENDSTARTTABLE
 # --------------------------------------------------------------------- Default  # --------------------------------------------------------------------- Default
 # Fast forward to first rule  # Fast forward to first rule
   $token=$parser->get_token;    $token=$parser->get_token;
   while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }    while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
 # print default  # print default
   $r->print('<tr><td>&nbsp;</td><td>');    $r->print('<tr><td align="right">');
     if ($constructmode) {
        $r->print(&Apache::loncommon::select_form('','action_0',
                                     ('' => '',
      'insertbelow' => 'Insert rule below    ')));
                                                   
     } else {
         $r->print('&nbsp;');
     }
     $r->print('</td><td>');
   if ($constructmode) {    if ($constructmode) {
       $r->print(&Apache::loncommon::select_form        $r->print(&Apache::loncommon::select_form
                                  ($token->[2]->{'effect'},'effect_0',                                   ($token->[2]->{'effect'},'effect_0',
Line 91  ENDSTARTTABLE Line 208  ENDSTARTTABLE
   }    }
   $r->print('</td><td colspan="4">Default');    $r->print('</td><td colspan="4">Default');
   if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {    if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
       $r->print(' - <font color="red">Error! No default set.</font>');        $r->print(' - <font color="red">'.&mt('Error! No default set.').
    '</font>');
   }    }
   $r->print('</td></tr>');    $r->print('</td></tr>');
 # Additional roles  # Additional roles
   while ($token=$parser->get_token) {    while ($token=$parser->get_token) {
       if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {        if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
           $rulecounter++;            $rulecounter++;
   $r->print('<tr><td>');    $r->print('<tr><td align="right" rowspan="2">');
 # insert, delete, etc  # insert, delete, etc
   $r->print($rulecounter.'.&nbsp;');    $r->print($rulecounter.'.&nbsp;');
           if ($constructmode) {            if ($constructmode) {
Line 107  ENDSTARTTABLE Line 225  ENDSTARTTABLE
                     ('' => '',                       ('' => '', 
                      'delete' => 'Delete this rule',                       'delete' => 'Delete this rule',
                      'insertabove' => 'Insert rule above',                       'insertabove' => 'Insert rule above',
                      'insertbelow' => 'Insert rule below',                       'insertbelow' => 'Insert rule below    ',
                      'moveup'      => 'Move rule up',                       'moveup'      => 'Move rule up',
                      'movedown'    => 'Move rule down')));                       'movedown'    => 'Move rule down')));
   }    }
           $r->print('</td><td>');            $r->print('</td><td rowspan="2">');
 # effect  # effect
           if ($constructmode) {            if ($constructmode) {
              $r->print(&Apache::loncommon::select_form               $r->print(&Apache::loncommon::select_form
Line 125  ENDSTARTTABLE Line 243  ENDSTARTTABLE
   $r->print('</td><td>');    $r->print('</td><td>');
 # ---- realm  # ---- realm
           my $realm=$token->[2]->{'realm'};            my $realm=$token->[2]->{'realm'};
           $realm=~s/^\W//;  
           my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);            my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
 # realm role    $rdom = &LONCAPA::clean_domain($rdom);
     $rcourse = &LONCAPA::clean_courseid($rcourse);
   # realm domain
           if ($constructmode) {            if ($constructmode) {
                 unless ($rdom) { $rdom=$env{'user.domain'}; }
               $r->print(&Apache::loncommon::select_dom_form($rdom,                $r->print(&Apache::loncommon::select_dom_form($rdom,
                                                       'domain_'.$rulecounter));                                                        'domain_'.$rulecounter));
           } else {            } else {
Line 152  ENDSTARTTABLE Line 272  ENDSTARTTABLE
               $r->print($rsec);                $r->print($rsec);
           }            }
   
           $r->print('</td><td>');            $r->print('</td><td rowspan="2">');
 # role  # role
           if ($constructmode) {            if ($constructmode) {
       my %hash=('' => '');        my %hash=('' => '');
Line 166  ENDSTARTTABLE Line 286  ENDSTARTTABLE
           } else {            } else {
               $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));                $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
           }            }
   # course selection link
             $r->print('</td></tr><tr><td colspan="3" align="right">');
             if ($rcourse) {
         my %descript=
                    &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
       {'one_time' => 1});
                 $r->print($descript{'description'}.'&nbsp;&nbsp;&nbsp;');
             }
     if ($constructmode) {
         $r->print(&Apache::loncommon::selectcourse_link('rules',
           'course_'.$rulecounter,'domain_'.$rulecounter));
     }
 # close row  # close row
           $r->print('</td></tr>');            $r->print('</td></tr>');
      }                                              }                                       
Line 173  ENDSTARTTABLE Line 305  ENDSTARTTABLE
   $r->print('</table>');    $r->print('</table>');
 # ------------------------------------------------------------ End table output  # ------------------------------------------------------------ End table output
   if ($constructmode) {     if ($constructmode) { 
      $r->print('<input type="submit" value="Store" /></form>');        $r->print('<input type="submit" name="store" value="'.&mt('Save').'" /></form>'); 
   }    }
   $r->print('</body></html>');    $r->print(&Apache::loncommon::end_page());
   return OK;      return OK;  
 }  }
   

Removed from v.1.4  
changed lines
  Added in v.1.22


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