Annotation of loncom/publisher/lonrights.pm, revision 1.13
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
1.13 ! albertel 4: # $Id: lonrights.pm,v 1.12 2004/01/20 21:10:03 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',
181: 'ro' => 'Role');
1.3 www 182: # ---------------------------------------------------------- Start table output
1.4 www 183: $r->print(<<ENDSTARTTABLE);
184: <table border="2">
1.10 www 185: <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
186: <th>$lt{'se'}</th><th>$lt{'ro'}</th></tr>
1.4 www 187: ENDSTARTTABLE
1.3 www 188: # --------------------------------------------------------------------- Default
189: # Fast forward to first rule
190: $token=$parser->get_token;
191: while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
192: # print default
1.7 www 193: $r->print('<tr><td align="right">');
1.6 www 194: if ($constructmode) {
195: $r->print(&Apache::loncommon::select_form('','action_0',
1.7 www 196: ('' => '',
197: 'insertbelow' => 'Insert rule below ')));
1.6 www 198:
199: } else {
200: $r->print(' ');
201: }
202: $r->print('</td><td>');
1.3 www 203: if ($constructmode) {
204: $r->print(&Apache::loncommon::select_form
205: ($token->[2]->{'effect'},'effect_0',
206: ('allow' => 'allow',
207: 'deny' => 'deny')));
208: } else {
209: $r->print($token->[2]->{'effect'});
210: }
1.4 www 211: $r->print('</td><td colspan="4">Default');
1.3 www 212: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
1.10 www 213: $r->print(' - <font color="red">'.&mt('Error! No default set.').
214: '</font>');
1.3 www 215: }
216: $r->print('</td></tr>');
217: # Additional roles
1.2 www 218: while ($token=$parser->get_token) {
219: if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
1.3 www 220: $rulecounter++;
1.8 www 221: $r->print('<tr><td align="right" rowspan="2">');
1.4 www 222: # insert, delete, etc
223: $r->print($rulecounter.'. ');
224: if ($constructmode) {
225: $r->print(&Apache::loncommon::select_form(
226: '','action_'.$rulecounter,
227: ('' => '',
228: 'delete' => 'Delete this rule',
229: 'insertabove' => 'Insert rule above',
1.7 www 230: 'insertbelow' => 'Insert rule below ',
1.4 www 231: 'moveup' => 'Move rule up',
232: 'movedown' => 'Move rule down')));
233: }
1.8 www 234: $r->print('</td><td rowspan="2">');
1.3 www 235: # effect
236: if ($constructmode) {
237: $r->print(&Apache::loncommon::select_form
238: ($token->[2]->{'effect'},
239: 'effect_'.$rulecounter,
240: ('allow' => 'allow',
241: 'deny' => 'deny')));
242: } else {
243: $r->print($token->[2]->{'effect'});
244: }
245: $r->print('</td><td>');
1.4 www 246: # ---- realm
1.3 www 247: my $realm=$token->[2]->{'realm'};
248: $realm=~s/^\W//;
1.4 www 249: my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
1.7 www 250: # realm domain
1.3 www 251: if ($constructmode) {
1.7 www 252: unless ($rdom) { $rdom=$ENV{'user.domain'}; }
1.3 www 253: $r->print(&Apache::loncommon::select_dom_form($rdom,
254: 'domain_'.$rulecounter));
255: } else {
1.4 www 256: $r->print($rdom);
257: }
258: $r->print('</td><td>');
259: # realm course
260: if ($constructmode) {
261: $r->print('<input input type="text" size="25" name="course_'.
262: $rulecounter.'" value="'.$rcourse.'" />');
263: } else {
264: $r->print($rcourse);
1.3 www 265: }
1.4 www 266:
1.9 www 267: $r->print('</td><td>');
1.4 www 268: # realm section
269: if ($constructmode) {
270: $r->print('<input input type="text" size="5" name="section_'.
271: $rulecounter.'" value="'.$rsec.'" />');
272: } else {
273: $r->print($rsec);
274: }
275:
1.8 www 276: $r->print('</td><td rowspan="2">');
1.3 www 277: # role
278: if ($constructmode) {
1.4 www 279: my %hash=('' => '');
280: foreach ('au','cc','in','ta','st') {
281: $hash{$_}=&Apache::lonnet::plaintext($_);
282: }
283: my $role=$token->[2]->{'role'};
284: unless ($role) { $role=''; }
285: $r->print(&Apache::loncommon::select_form(
286: $role,'role_'.$rulecounter,%hash));
1.3 www 287: } else {
1.4 www 288: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
1.3 www 289: }
1.8 www 290: # course selection link
1.9 www 291: $r->print('</td></tr><tr><td colspan="3" align="right">');
1.8 www 292: if ($rcourse) {
293: my %descript=
294: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse);
295: $r->print($descript{'description'}.' ');
296: }
1.12 albertel 297: if ($constructmode) {
298: $r->print(&Apache::loncommon::selectcourse_link('rules',
299: 'course_'.$rulecounter,'domain_'.$rulecounter));
300: }
1.3 www 301: # close row
302: $r->print('</td></tr>');
1.2 www 303: }
1.3 www 304: }
305: $r->print('</table>');
306: # ------------------------------------------------------------ End table output
307: if ($constructmode) {
1.10 www 308: $r->print('<input type="submit" name="store" value="'.&mt('Store').'" /></form>');
1.2 www 309: }
1.1 www 310: $r->print('</body></html>');
311: return OK;
312: }
313:
314:
315: 1;
316: __END__
317:
318:
319:
320:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>