1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
4: # $Id: lonrights.pm,v 1.23 2007/06/22 00:11:22 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: &Apache::loncommon::studentbrowser_javascript();
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 && $env{'form.store'}) {
70:
71: my @newrules;
72:
73: # read rules from form
74: foreach my $key (keys(%env)) {
75: next if ($key!~/^form\.effect\_(\d+)$/);
76: my $number=$1;
77: my %rulehash;
78: foreach my $action ('effect','type','domain','course','section','role') {
79: $rulehash{$action}=$env{'form.'.$action.'_'.$number};
80: }
81: if ($rulehash{'type'} !~ /^(user|course)$/) {
82: $rulehash{'type'} = 'course';
83: }
84: if ($rulehash{'type'} eq 'user') {
85: $rulehash{'section'}='';
86: $rulehash{'role'}='';
87: }
88: if ($rulehash{'role'} eq 'au') {
89: $rulehash{'course'}='';
90: $rulehash{'section'}='';
91: }
92: if ($rulehash{'role'} eq 'cc') {
93: $rulehash{'section'}='';
94: }
95: unless (($rulehash{'effect'} eq 'deny') ||
96: ($rulehash{'effect'} eq 'allow')) {
97: $rulehash{'effect'}='deny';
98: }
99: $rulehash{'domain'} =
100: &LONCAPA::clean_domain($rulehash{'domain'});
101: if ($rulehash{'type'} eq 'course') {
102: $rulehash{'course'} =
103: &LONCAPA::clean_courseid($rulehash{'course'});
104: } else {
105: $rulehash{'course'} =
106: &LONCAPA::clean_username($rulehash{'course'});
107: }
108: $rulehash{'section'}=~s/\W//g;
109: if (!$rulehash{'domain'}) {
110: $rulehash{'domain'}=$env{'user.domain'};
111: }
112: my $realm='';
113: if ($number) {
114: $realm=$rulehash{'domain'};
115: if ($rulehash{'course'}) {
116: $realm.='_'.$rulehash{'course'};
117: }
118: if ($rulehash{'section'}) {
119: $realm.='_'.$rulehash{'section'};
120: }
121: }
122: $newrules[$number]=$rulehash{'effect'}.':'.
123: $realm.':'.$rulehash{'role'}.':'.$rulehash{'type'};
124: }
125: # edit actions?
126: foreach my $key (keys(%env)) {
127: next if ($key!~/^form\.action\_(\d+)$/);
128: my $number=$1;
129: if ($env{$key} eq 'delete') { $newrules[$number]=''; }
130: if (($env{$key} eq 'moveup') && ($number>1)) {
131: my $buffer=$newrules[$number];
132: $newrules[$number]=$newrules[$number-1];
133: $newrules[$number-1]=$buffer;
134: }
135: if (($env{$key} eq 'movedown') && ($number<$#newrules)) {
136: my $buffer=$newrules[$number];
137: $newrules[$number]=$newrules[$number+1];
138: $newrules[$number+1]=$buffer;
139: }
140: if ($env{$key} eq 'insertabove') {
141: for (my $i=$#newrules;$i>=$number;$i--) {
142: $newrules[$i+1]=$newrules[$i];
143: }
144: $newrules[$number]='deny';
145: }
146: if ($env{$key} eq 'insertbelow') {
147: for (my $i=$#newrules;$i>$number;$i--) {
148: $newrules[$i+1]=$newrules[$i];
149: }
150: $newrules[$number+1]='deny';
151: }
152: }
153:
154: # store file
155: my $fh=Apache::File->new('>'.$fn);
156: foreach (my $i=0;$i<=$#newrules;$i++) {
157: if ($newrules[$i]) {
158: my ($effect,$realm,$role,$type)=split(/\:/,$newrules[$i]);
159: print $fh
160: "<accessrule effect='$effect' realm='$realm' role='$role' type='$type' />\n";
161: }
162: }
163: $fh->close;
164: }
165: # ============================================================ Read and display
166: unless ($constructmode) {
167: # =========================================== This is not in construction space
168: $contents=&Apache::lonnet::getfile($fn);
169: if ($contents==-1) { $contents=''; }
170: } else {
171: # =============================================== This is in construction space
172: if (-e $fn) {
173: my $fh=Apache::File->new($fn);
174: $contents=join('',<$fh>);
175: $fh->close();
176: }
177: $r->print('<form name="rules" method="post">');
178: }
179: unless ($contents=~/\<accessrule/s) {
180: $contents='<accessrule effect="deny" />';
181: }
182: my $parser=HTML::LCParser->new(\$contents);
183: my $token;
184: my $rulecounter=0;
185: my $colzero=&mt($constructmode?'Edit action':'Rule');
186: my %lt=&Apache::lonlocal::texthash('ef' => 'Effect',
187: 'do' => 'Domain',
188: 'co' => 'Course',
189: 'se' => 'Section',
190: 'ro' => 'Role');
191: # ---------------------------------------------------------- Start table output
192: $r->print(&Apache::loncommon::start_data_table().
193: &Apache::loncommon::start_data_table_header_row().
194: "<th>$colzero</th><th>$lt{'ef'}</th><th>Type</th><th>$lt{'do'}</th>".
195: "<th>$lt{'co'}</th><th>$lt{'se'}</th><th>$lt{'ro'}</th>".
196: &Apache::loncommon::end_data_table_header_row());
197: # --------------------------------------------------------------------- Default
198: # Fast forward to first rule
199: $token=$parser->get_token;
200: while ($token->[1] ne 'accessrule') { $token=$parser->get_token; }
201: # print default
202: $r->print(&Apache::loncommon::start_data_table_row().'<td align="right">');
203: if ($constructmode) {
204: $r->print(&Apache::loncommon::select_form('','action_0',
205: ('' => '',
206: 'insertbelow' => 'Insert rule below ')));
207:
208: } else {
209: $r->print(' ');
210: }
211: $r->print('</td><td>');
212: if ($constructmode) {
213: $r->print(&Apache::loncommon::select_form
214: ($token->[2]->{'effect'},'effect_0',
215: ('allow' => 'allow',
216: 'deny' => 'deny')));
217: } else {
218: $r->print($token->[2]->{'effect'});
219: }
220: $r->print('</td><td colspan="5">'.&mt('Default'));
221: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
222: $r->print(' - <span class="LC_error">'.&mt('Error! No default set.').
223: '</span>');
224: }
225: $r->print('</td>'.&Apache::loncommon::end_data_table_row());
226: # Additional roles
227: while ($token=$parser->get_token) {
228: if (($token->[0] eq 'S') && ($token->[1] eq 'accessrule')) {
229: $rulecounter++;
230: $r->print(&Apache::loncommon::start_data_table_row().
231: '<td align="right" rowspan="2">');
232: # insert, delete, etc
233: $r->print('<span class="LC_nobreak">'.$rulecounter.'. ');
234: if ($constructmode) {
235: $r->print(&Apache::loncommon::select_form(
236: '','action_'.$rulecounter,
237: ('' => '',
238: 'delete' => 'Delete this rule',
239: 'insertabove' => 'Insert rule above',
240: 'insertbelow' => 'Insert rule below ',
241: 'moveup' => 'Move rule up',
242: 'movedown' => 'Move rule down')));
243: }
244: $r->print('</span></td>'.
245: '<td rowspan="2">');
246: # effect
247: if ($constructmode) {
248: $r->print(&Apache::loncommon::select_form
249: ($token->[2]->{'effect'},
250: 'effect_'.$rulecounter,
251: ('allow' => 'allow',
252: 'deny' => 'deny')));
253: } else {
254: $r->print($token->[2]->{'effect'});
255: }
256:
257: # type
258: $r->print('</td><td rowspan="2">');
259: my $type = ($token->[2]{'type'} || 'course');
260: if ($constructmode) {
261: $r->print(&Apache::loncommon::select_form($type,
262: 'type_'.$rulecounter,
263: ('course' => 'Course',
264: 'user' => 'User')));
265: } else {
266: $r->print($type);
267: }
268:
269: # ---- realm
270: my $realm=$token->[2]->{'realm'};
271: my ($rdom,$rcourse,$rsec)=split(/[\/\_]/,$realm);
272: $rdom = &LONCAPA::clean_domain($rdom);
273: if ($type eq 'course') {
274: $rcourse = &LONCAPA::clean_courseid($rcourse);
275: } else {
276: $rcourse = &LONCAPA::clean_username($rcourse);
277: }
278:
279:
280: $r->print('</td><td>');
281: # realm domain
282: if ($constructmode) {
283: unless ($rdom) { $rdom=$env{'user.domain'}; }
284: $r->print(&Apache::loncommon::select_dom_form($rdom,
285: 'domain_'.$rulecounter));
286: } else {
287: $r->print($rdom);
288: }
289: $r->print('</td><td>');
290: # realm course
291: if ($constructmode) {
292: $r->print('<input type="text" size="25" name="course_'.
293: $rulecounter.'" value="'.$rcourse.'" />');
294: } else {
295: $r->print($rcourse);
296: }
297:
298: $r->print('</td><td rowspan="2">');
299: # realm section
300: if ($type eq 'course') {
301: if ($constructmode) {
302: $r->print('<input type="text" size="5" name="section_'.
303: $rulecounter.'" value="'.$rsec.'" />');
304: } else {
305: $r->print($rsec);
306: }
307: }
308:
309: $r->print('</td><td rowspan="2">');
310: # role
311: if ($type eq 'course') {
312: if ($constructmode) {
313: my %hash=('' => '');
314: foreach ('au','cc','in','ta','st') {
315: $hash{$_}=&Apache::lonnet::plaintext($_);
316: }
317: my $role=$token->[2]->{'role'};
318: unless ($role) { $role=''; }
319: $r->print(&Apache::loncommon::select_form(
320: $role,'role_'.$rulecounter,%hash));
321: } else {
322: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
323: }
324: }
325: # course selection link
326: $r->print('</td>'.
327: &Apache::loncommon::end_data_table_row().
328: &Apache::loncommon::continue_data_table_row().
329: '<td colspan="2" align="right">');
330: if ($type eq 'course') {
331: if ($rcourse) {
332: my %descript=
333: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
334: {'one_time' => 1});
335: $r->print($descript{'description'}.' ');
336: }
337: if ($constructmode) {
338: $r->print(&Apache::loncommon::selectcourse_link('rules',
339: 'course_'.$rulecounter,'domain_'.$rulecounter));
340: }
341: } else {
342: if ($rcourse) {
343: my $name = &Apache::loncommon::plainname($rcourse,$rdom);
344: $r->print($name.' ');
345: }
346: if ($constructmode) {
347: $r->print(&Apache::loncommon::selectstudent_link('rules','course_'.$rulecounter,'domain_'.$rulecounter));
348: }
349: }
350: # close row
351: $r->print('</td>'.&Apache::loncommon::end_data_table_row());
352: }
353: }
354: $r->print(&Apache::loncommon::end_data_table());
355: # ------------------------------------------------------------ End table output
356: if ($constructmode) {
357: $r->print('<input type="submit" name="store" value="'.&mt('Save').'" /></form>');
358: }
359: $r->print(&Apache::loncommon::end_page());
360: return OK;
361: }
362:
363:
364: 1;
365: __END__
366:
367:
368:
369:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>