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