1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
4: # $Id: lonrights.pm,v 1.21 2006/12/20 22:41:08 albertel 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 $js = &Apache::loncommon::coursebrowser_javascript();
59:
60: $r->print(&Apache::loncommon::start_page('Custom Distribution Rights',$js));
61: $r->rflush();
62:
63: my $uri=$r->uri;
64: my $fn=&Apache::lonnet::filelocation('',$uri);
65: my $contents='';
66: my $constructmode=($uri=~/^\/\~/);
67:
68: # ============================================================ Modify and store
69: if ($constructmode) {
70: if ($env{'form.store'}) {
71: my @newrules=();
72: undef @newrules;
73: # read rules from form
74: foreach (keys %env) {
75: if ($_=~/^form\.effect\_(\d+)$/) {
76: my $number=$1;
77: my %rulehash=();
78: foreach ('effect','domain','course','section','role') {
79: $rulehash{$_}=$env{'form.'.$_.'_'.$number};
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'} = &LONCAPA::clean_domain($rulehash{'domain'});
93: $rulehash{'course'} = &LONCAPA::clean_courseid($rulehash{'course'});
94: $rulehash{'section'}=~s/\W//g;
95: unless ($rulehash{'domain'}) {
96: $rulehash{'domain'}=$env{'user.domain'};
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?
113: foreach (keys %env) {
114: if ($_=~/^form\.action\_(\d+)$/) {
115: my $number=$1;
116: if ($env{$_} eq 'delete') { $newrules[$number]=''; }
117: if (($env{$_} eq 'moveup') && ($number>1)) {
118: my $buffer=$newrules[$number];
119: $newrules[$number]=$newrules[$number-1];
120: $newrules[$number-1]=$buffer;
121: }
122: if (($env{$_} eq 'movedown') && ($number<$#newrules)) {
123: my $buffer=$newrules[$number];
124: $newrules[$number]=$newrules[$number+1];
125: $newrules[$number+1]=$buffer;
126: }
127: if ($env{$_} eq 'insertabove') {
128: for (my $i=$#newrules;$i>=$number;$i--) {
129: $newrules[$i+1]=$newrules[$i];
130: }
131: $newrules[$number]='deny';
132: }
133: if ($env{$_} eq 'insertbelow') {
134: for (my $i=$#newrules;$i>$number;$i--) {
135: $newrules[$i+1]=$newrules[$i];
136: }
137: $newrules[$number+1]='deny';
138: }
139: }
140: }
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
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: }
166: $r->print('<form name="rules" method="post">');
167: }
168: unless ($contents=~/\<accessrule/s) {
169: $contents='<accessrule effect="deny" />';
170: }
171: my $parser=HTML::LCParser->new(\$contents);
172: my $token;
173: my $rulecounter=0;
174: my $colzero=&mt($constructmode?'Edit action':'Rule');
175: my %lt=&Apache::lonlocal::texthash('ef' => 'Effect',
176: 'do' => 'Domain',
177: 'co' => 'Course',
178: 'se' => 'Section',
179: 'ro' => 'Role');
180: # ---------------------------------------------------------- Start table output
181: $r->print(<<ENDSTARTTABLE);
182: <table border="2">
183: <tr><th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'do'}</th><th>$lt{'co'}</th>
184: <th>$lt{'se'}</th><th>$lt{'ro'}</th></tr>
185: ENDSTARTTABLE
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
191: $r->print('<tr><td align="right">');
192: if ($constructmode) {
193: $r->print(&Apache::loncommon::select_form('','action_0',
194: ('' => '',
195: 'insertbelow' => 'Insert rule below ')));
196:
197: } else {
198: $r->print(' ');
199: }
200: $r->print('</td><td>');
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: }
209: $r->print('</td><td colspan="4">Default');
210: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
211: $r->print(' - <font color="red">'.&mt('Error! No default set.').
212: '</font>');
213: }
214: $r->print('</td></tr>');
215: # Additional roles
216: while ($token=$parser->get_token) {
217: if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
218: $rulecounter++;
219: $r->print('<tr><td align="right" rowspan="2">');
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',
228: 'insertbelow' => 'Insert rule below ',
229: 'moveup' => 'Move rule up',
230: 'movedown' => 'Move rule down')));
231: }
232: $r->print('</td><td rowspan="2">');
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>');
244: # ---- realm
245: my $realm=$token->[2]->{'realm'};
246: my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
247: $rdom = &LONCAPA::clean_domain($rdom);
248: $rcourse = &LONCAPA::clean_courseid($rcourse);
249: # realm domain
250: if ($constructmode) {
251: unless ($rdom) { $rdom=$env{'user.domain'}; }
252: $r->print(&Apache::loncommon::select_dom_form($rdom,
253: 'domain_'.$rulecounter));
254: } else {
255: $r->print($rdom);
256: }
257: $r->print('</td><td>');
258: # realm course
259: if ($constructmode) {
260: $r->print('<input input type="text" size="25" name="course_'.
261: $rulecounter.'" value="'.$rcourse.'" />');
262: } else {
263: $r->print($rcourse);
264: }
265:
266: $r->print('</td><td>');
267: # realm section
268: if ($constructmode) {
269: $r->print('<input input type="text" size="5" name="section_'.
270: $rulecounter.'" value="'.$rsec.'" />');
271: } else {
272: $r->print($rsec);
273: }
274:
275: $r->print('</td><td rowspan="2">');
276: # role
277: if ($constructmode) {
278: my %hash=('' => '');
279: foreach ('au','cc','in','ta','st') {
280: $hash{$_}=&Apache::lonnet::plaintext($_);
281: }
282: my $role=$token->[2]->{'role'};
283: unless ($role) { $role=''; }
284: $r->print(&Apache::loncommon::select_form(
285: $role,'role_'.$rulecounter,%hash));
286: } else {
287: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
288: }
289: # course selection link
290: $r->print('</td></tr><tr><td colspan="3" align="right">');
291: if ($rcourse) {
292: my %descript=
293: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
294: {'one_time' => 1});
295: $r->print($descript{'description'}.' ');
296: }
297: if ($constructmode) {
298: $r->print(&Apache::loncommon::selectcourse_link('rules',
299: 'course_'.$rulecounter,'domain_'.$rulecounter));
300: }
301: # close row
302: $r->print('</td></tr>');
303: }
304: }
305: $r->print('</table>');
306: # ------------------------------------------------------------ End table output
307: if ($constructmode) {
308: $r->print('<input type="submit" name="store" value="'.&mt('Store').'" /></form>');
309: }
310: $r->print(&Apache::loncommon::end_page());
311: return OK;
312: }
313:
314:
315: 1;
316: __END__
317:
318:
319:
320:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>