Annotation of loncom/publisher/lonrights.pm, revision 1.14
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
1.14 ! taceyjo1 4: # $Id: lonrights.pm,v 1.13 2004/01/20 23:03:18 albertel 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);
35: use Apache::lonnet();
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.13 albertel 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: }
1.10 www 55: &Apache::loncommon::content_type($r,'text/html');
1.1 www 56: $r->send_http_header;
57:
1.3 www 58: $r->print(
1.8 www 59: '<html><head><title>LON-CAPA Custom Distribution Rights</title>'.
60: &Apache::loncommon::coursebrowser_javascript().'</head>');
1.1 www 61:
62: $r->print(&Apache::loncommon::bodytag('Custom Distribution Rights'));
1.5 www 63: $r->rflush();
1.1 www 64:
1.2 www 65: my $uri=$r->uri;
66: my $fn=&Apache::lonnet::filelocation('',$uri);
67: my $contents='';
68: my $constructmode=($uri=~/^\/\~/);
1.5 www 69:
70: # ============================================================ Modify and store
71: if ($constructmode) {
72: if ($ENV{'form.store'}) {
73: my @newrules=();
74: undef @newrules;
75: # read rules from form
76: foreach (keys %ENV) {
77: if ($_=~/^form\.effect\_(\d+)$/) {
78: my $number=$1;
79: my %rulehash=();
80: foreach ('effect','domain','course','section','role') {
81: $rulehash{$_}=$ENV{'form.'.$_.'_'.$number};
82: }
83: if ($rulehash{'role'} eq 'au') {
84: $rulehash{'course'}='';
85: $rulehash{'section'}='';
86: }
87: if ($rulehash{'role'} eq 'cc') {
88: $rulehash{'section'}='';
89: }
90: unless (($rulehash{'effect'} eq 'deny') ||
91: ($rulehash{'effect'} eq 'allow')) {
92: $rulehash{'effect'}='deny';
93: }
94: $rulehash{'domain'}=~s/\W//g;
95: $rulehash{'course'}=~s/\W//g;
96: $rulehash{'section'}=~s/\W//g;
97: unless ($rulehash{'domain'}) {
98: $rulehash{'domain'}=$ENV{'user.domain'};
99: }
100: my $realm='';
101: if ($number) {
102: $realm=$rulehash{'domain'};
103: if ($rulehash{'course'}) {
104: $realm.='_'.$rulehash{'course'};
105: }
106: if ($rulehash{'section'}) {
107: $realm.='_'.$rulehash{'section'};
108: }
109: }
110: $newrules[$number]=$rulehash{'effect'}.':'.
111: $realm.':'.$rulehash{'role'};
112: }
113: }
114: # edit actions?
1.6 www 115: foreach (keys %ENV) {
116: if ($_=~/^form\.action\_(\d+)$/) {
117: my $number=$1;
118: if ($ENV{$_} eq 'delete') { $newrules[$number]=''; }
119: if (($ENV{$_} eq 'moveup') && ($number>2)) {
120: my $buffer=$newrules[$number];
121: $newrules[$number]=$newrules[$number-1];
122: $newrules[$number-1]=$buffer;
123: }
124: if (($ENV{$_} eq 'movedown') && ($number<$#newrules)) {
125: my $buffer=$newrules[$number];
126: $newrules[$number]=$newrules[$number+1];
127: $newrules[$number+1]=$buffer;
128: }
129: if ($ENV{$_} eq 'insertabove') {
130: for (my $i=$#newrules;$i>=$number;$i--) {
131: $newrules[$i+1]=$newrules[$i];
132: }
133: $newrules[$number]='deny';
134: }
135: if ($ENV{$_} eq 'insertbelow') {
136: for (my $i=$#newrules;$i>$number;$i--) {
137: $newrules[$i+1]=$newrules[$i];
138: }
139: $newrules[$number+1]='deny';
140: }
141: }
142: }
1.5 www 143:
144: # store file
145: my $fh=Apache::File->new('>'.$fn);
146: foreach (my $i=0;$i<=$#newrules;$i++) {
147: if ($newrules[$i]) {
148: my ($effect,$realm,$role)=split(/\:/,$newrules[$i]);
149: print $fh
150: "<accessrule effect='$effect' realm='$realm' role='$role' />\n";
151: }
152: }
153: $fh->close;
154: }
155: }
156: # ============================================================ Read and display
1.2 www 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: }
1.8 www 168: $r->print('<form name="rules" method="post">');
1.2 www 169: }
1.5 www 170: unless ($contents=~/\<accessrule/s) {
171: $contents='<accessrule effect="deny" />';
172: }
1.2 www 173: my $parser=HTML::LCParser->new(\$contents);
174: my $token;
1.3 www 175: my $rulecounter=0;
1.10 www 176: my $colzero=&mt($constructmode?'Edit action':'Rule');
1.11 albertel 177: my %lt=&Apache::lonlocal::texthash('ef' => 'Effect',
178: 'do' => 'Domain',
179: 'co' => 'Course',
180: 'se' => 'Section/Group',
1.14 ! taceyjo1 181: 'ro' => 'Role',
! 182: 'ua' => 'Use Access',
! 183: 'sa' => 'Source Access');
1.3 www 184: # ---------------------------------------------------------- Start table output
1.4 www 185: $r->print(<<ENDSTARTTABLE);
186: <table border="2">
1.10 www 187: <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
1.14 ! taceyjo1 188: <th>$lt{'se'}</th><th>$lt{'ro'}</th><th>$lt{'ua'}</th><th>$lt{'sa'}</th></tr>
1.4 www 189: ENDSTARTTABLE
1.3 www 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
1.7 www 195: $r->print('<tr><td align="right">');
1.6 www 196: if ($constructmode) {
197: $r->print(&Apache::loncommon::select_form('','action_0',
1.7 www 198: ('' => '',
199: 'insertbelow' => 'Insert rule below ')));
1.6 www 200:
201: } else {
202: $r->print(' ');
203: }
204: $r->print('</td><td>');
1.3 www 205: if ($constructmode) {
206: $r->print(&Apache::loncommon::select_form
207: ($token->[2]->{'effect'},'effect_0',
208: ('allow' => 'allow',
209: 'deny' => 'deny')));
210: } else {
211: $r->print($token->[2]->{'effect'});
212: }
1.14 ! taceyjo1 213: $r->print('</td><td colspan="6">Default');
1.3 www 214: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
1.10 www 215: $r->print(' - <font color="red">'.&mt('Error! No default set.').
216: '</font>');
1.3 www 217: }
218: $r->print('</td></tr>');
219: # Additional roles
1.2 www 220: while ($token=$parser->get_token) {
221: if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
1.3 www 222: $rulecounter++;
1.8 www 223: $r->print('<tr><td align="right" rowspan="2">');
1.4 www 224: # insert, delete, etc
225: $r->print($rulecounter.'. ');
226: if ($constructmode) {
227: $r->print(&Apache::loncommon::select_form(
228: '','action_'.$rulecounter,
229: ('' => '',
230: 'delete' => 'Delete this rule',
231: 'insertabove' => 'Insert rule above',
1.7 www 232: 'insertbelow' => 'Insert rule below ',
1.4 www 233: 'moveup' => 'Move rule up',
234: 'movedown' => 'Move rule down')));
235: }
1.8 www 236: $r->print('</td><td rowspan="2">');
1.3 www 237: # effect
238: if ($constructmode) {
239: $r->print(&Apache::loncommon::select_form
240: ($token->[2]->{'effect'},
241: 'effect_'.$rulecounter,
242: ('allow' => 'allow',
243: 'deny' => 'deny')));
244: } else {
245: $r->print($token->[2]->{'effect'});
246: }
247: $r->print('</td><td>');
1.4 www 248: # ---- realm
1.3 www 249: my $realm=$token->[2]->{'realm'};
250: $realm=~s/^\W//;
1.4 www 251: my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
1.7 www 252: # realm domain
1.3 www 253: if ($constructmode) {
1.7 www 254: unless ($rdom) { $rdom=$ENV{'user.domain'}; }
1.3 www 255: $r->print(&Apache::loncommon::select_dom_form($rdom,
256: 'domain_'.$rulecounter));
257: } else {
1.4 www 258: $r->print($rdom);
259: }
260: $r->print('</td><td>');
261: # realm course
262: if ($constructmode) {
263: $r->print('<input input type="text" size="25" name="course_'.
264: $rulecounter.'" value="'.$rcourse.'" />');
265: } else {
266: $r->print($rcourse);
1.3 www 267: }
1.4 www 268:
1.9 www 269: $r->print('</td><td>');
1.4 www 270: # realm section
271: if ($constructmode) {
272: $r->print('<input input type="text" size="5" name="section_'.
273: $rulecounter.'" value="'.$rsec.'" />');
274: } else {
275: $r->print($rsec);
276: }
277:
1.8 www 278: $r->print('</td><td rowspan="2">');
1.3 www 279: # role
280: if ($constructmode) {
1.4 www 281: my %hash=('' => '');
282: foreach ('au','cc','in','ta','st') {
283: $hash{$_}=&Apache::lonnet::plaintext($_);
284: }
285: my $role=$token->[2]->{'role'};
286: unless ($role) { $role=''; }
287: $r->print(&Apache::loncommon::select_form(
288: $role,'role_'.$rulecounter,%hash));
1.3 www 289: } else {
1.4 www 290: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
1.3 www 291: }
1.14 ! taceyjo1 292: # use access
! 293: # if ($constructmode) {
! 294: #
! 295: # } else {
! 296: # $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
! 297: # }
1.8 www 298: # course selection link
1.9 www 299: $r->print('</td></tr><tr><td colspan="3" align="right">');
1.8 www 300: if ($rcourse) {
301: my %descript=
302: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse);
303: $r->print($descript{'description'}.' ');
304: }
1.12 albertel 305: if ($constructmode) {
306: $r->print(&Apache::loncommon::selectcourse_link('rules',
307: 'course_'.$rulecounter,'domain_'.$rulecounter));
308: }
1.3 www 309: # close row
310: $r->print('</td></tr>');
1.2 www 311: }
1.3 www 312: }
313: $r->print('</table>');
314: # ------------------------------------------------------------ End table output
315: if ($constructmode) {
1.10 www 316: $r->print('<input type="submit" name="store" value="'.&mt('Store').'" /></form>');
1.2 www 317: }
1.1 www 318: $r->print('</body></html>');
319: return OK;
320: }
321:
322:
323: 1;
324: __END__
325:
326:
327:
328:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>