Annotation of loncom/publisher/lonrights.pm, revision 1.25
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to show and edit custom distribution rights
3: #
1.25 ! bisitz 4: # $Id: lonrights.pm,v 1.24 2007/07/13 18:35:28 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.23 albertel 58: my $js = &Apache::loncommon::coursebrowser_javascript().
59: &Apache::loncommon::studentbrowser_javascript();
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
1.23 albertel 69: if ($constructmode && $env{'form.store'}) {
70:
71: my @newrules;
72:
1.5 www 73: # read rules from form
1.23 albertel 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='';
1.24 albertel 113: my $separator = ($rulehash{'type'} eq 'course') ? '_' : '/';
1.23 albertel 114: if ($number) {
115: $realm=$rulehash{'domain'};
116: if ($rulehash{'course'}) {
1.24 albertel 117: $realm.=$separator.$rulehash{'course'};
1.23 albertel 118: }
119: if ($rulehash{'section'}) {
1.24 albertel 120: $realm.=$separator.$rulehash{'section'};
1.23 albertel 121: }
122: }
123: $newrules[$number]=$rulehash{'effect'}.':'.
124: $realm.':'.$rulehash{'role'}.':'.$rulehash{'type'};
125: }
1.5 www 126: # edit actions?
1.23 albertel 127: foreach my $key (keys(%env)) {
128: next if ($key!~/^form\.action\_(\d+)$/);
129: my $number=$1;
1.24 albertel 130: if ($env{$key} eq 'delete') { splice(@newrules,$number,1); }
1.23 albertel 131: if (($env{$key} eq 'moveup') && ($number>1)) {
1.24 albertel 132: @newrules[$number-1,$number] = @newrules[$number,$number-1];
1.23 albertel 133: }
134: if (($env{$key} eq 'movedown') && ($number<$#newrules)) {
1.24 albertel 135: @newrules[$number+1,$number] = @newrules[$number,$number+1];
1.23 albertel 136: }
137: if ($env{$key} eq 'insertabove') {
1.24 albertel 138: splice(@newrules,$number,0,'deny');
1.23 albertel 139: }
140: if ($env{$key} eq 'insertbelow') {
1.24 albertel 141: splice(@newrules,$number+1,0,'deny');
1.23 albertel 142: }
143: }
144:
1.5 www 145: # store file
1.23 albertel 146: my $fh=Apache::File->new('>'.$fn);
147: foreach (my $i=0;$i<=$#newrules;$i++) {
148: if ($newrules[$i]) {
149: my ($effect,$realm,$role,$type)=split(/\:/,$newrules[$i]);
150: print $fh
151: "<accessrule effect='$effect' realm='$realm' role='$role' type='$type' />\n";
152: }
1.5 www 153: }
1.23 albertel 154: $fh->close;
1.5 www 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.25 ! bisitz 177: my %lt=&Apache::lonlocal::texthash(
! 178: 'ef' => 'Effect',
! 179: 'ty' => 'Type',
1.11 albertel 180: 'do' => 'Domain',
1.24 albertel 181: 'co' => 'Course / User',
1.20 raeburn 182: 'se' => 'Section',
1.15 www 183: 'ro' => 'Role');
1.3 www 184: # ---------------------------------------------------------- Start table output
1.23 albertel 185: $r->print(&Apache::loncommon::start_data_table().
186: &Apache::loncommon::start_data_table_header_row().
1.25 ! bisitz 187: "<th>$colzero</th><th>$lt{'ef'}</th><th>$lt{'ty'}</th><th>$lt{'do'}</th>".
1.23 albertel 188: "<th>$lt{'co'}</th><th>$lt{'se'}</th><th>$lt{'ro'}</th>".
189: &Apache::loncommon::end_data_table_header_row());
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.23 albertel 195: $r->print(&Apache::loncommon::start_data_table_row().'<td align="right">');
1.6 www 196: if ($constructmode) {
197: $r->print(&Apache::loncommon::select_form('','action_0',
1.7 www 198: ('' => '',
1.25 ! bisitz 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.23 albertel 213: $r->print('</td><td colspan="5">'.&mt('Default'));
1.3 www 214: if (($token->[2]->{'realm'}) || ($token->[2]->{'role'})) {
1.23 albertel 215: $r->print(' - <span class="LC_error">'.&mt('Error! No default set.').
216: '</span>');
1.3 www 217: }
1.23 albertel 218: $r->print('</td>'.&Apache::loncommon::end_data_table_row());
1.3 www 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.23 albertel 223: $r->print(&Apache::loncommon::start_data_table_row().
224: '<td align="right" rowspan="2">');
1.4 www 225: # insert, delete, etc
1.23 albertel 226: $r->print('<span class="LC_nobreak">'.$rulecounter.'. ');
1.4 www 227: if ($constructmode) {
228: $r->print(&Apache::loncommon::select_form(
229: '','action_'.$rulecounter,
230: ('' => '',
231: 'delete' => 'Delete this rule',
232: 'insertabove' => 'Insert rule above',
1.25 ! bisitz 233: 'insertbelow' => 'Insert rule below',
1.4 www 234: 'moveup' => 'Move rule up',
235: 'movedown' => 'Move rule down')));
236: }
1.23 albertel 237: $r->print('</span></td>'.
238: '<td rowspan="2">');
1.3 www 239: # effect
240: if ($constructmode) {
241: $r->print(&Apache::loncommon::select_form
242: ($token->[2]->{'effect'},
243: 'effect_'.$rulecounter,
244: ('allow' => 'allow',
245: 'deny' => 'deny')));
246: } else {
247: $r->print($token->[2]->{'effect'});
248: }
1.23 albertel 249:
250: # type
251: $r->print('</td><td rowspan="2">');
252: my $type = ($token->[2]{'type'} || 'course');
253: if ($constructmode) {
254: $r->print(&Apache::loncommon::select_form($type,
255: 'type_'.$rulecounter,
256: ('course' => 'Course',
257: 'user' => 'User')));
258: } else {
259: $r->print($type);
260: }
261:
1.4 www 262: # ---- realm
1.3 www 263: my $realm=$token->[2]->{'realm'};
1.24 albertel 264: my ($rdom,$rcourse,$rsec);
1.23 albertel 265: if ($type eq 'course') {
1.24 albertel 266: ($rdom,$rcourse,$rsec) = split(m{[/_]},$realm,3);
1.23 albertel 267: $rcourse = &LONCAPA::clean_courseid($rcourse);
268: } else {
1.24 albertel 269: ($rdom,$rcourse,$rsec) = split(m{/},$realm,3);
1.23 albertel 270: $rcourse = &LONCAPA::clean_username($rcourse);
271: }
272: $r->print('</td><td>');
1.7 www 273: # realm domain
1.3 www 274: if ($constructmode) {
1.17 albertel 275: unless ($rdom) { $rdom=$env{'user.domain'}; }
1.3 www 276: $r->print(&Apache::loncommon::select_dom_form($rdom,
277: 'domain_'.$rulecounter));
278: } else {
1.4 www 279: $r->print($rdom);
280: }
281: $r->print('</td><td>');
282: # realm course
283: if ($constructmode) {
1.23 albertel 284: $r->print('<input type="text" size="25" name="course_'.
1.4 www 285: $rulecounter.'" value="'.$rcourse.'" />');
286: } else {
287: $r->print($rcourse);
1.3 www 288: }
1.4 www 289:
1.23 albertel 290: $r->print('</td><td rowspan="2">');
1.4 www 291: # realm section
1.23 albertel 292: if ($type eq 'course') {
293: if ($constructmode) {
294: $r->print('<input type="text" size="5" name="section_'.
295: $rulecounter.'" value="'.$rsec.'" />');
296: } else {
297: $r->print($rsec);
298: }
299: }
1.4 www 300:
1.8 www 301: $r->print('</td><td rowspan="2">');
1.3 www 302: # role
1.23 albertel 303: if ($type eq 'course') {
304: if ($constructmode) {
305: my %hash=('' => '');
306: foreach ('au','cc','in','ta','st') {
307: $hash{$_}=&Apache::lonnet::plaintext($_);
308: }
309: my $role=$token->[2]->{'role'};
310: unless ($role) { $role=''; }
311: $r->print(&Apache::loncommon::select_form(
312: $role,'role_'.$rulecounter,%hash));
313: } else {
314: $r->print(&Apache::lonnet::plaintext($token->[2]->{'role'}));
315: }
316: }
1.8 www 317: # course selection link
1.23 albertel 318: $r->print('</td>'.
319: &Apache::loncommon::end_data_table_row().
320: &Apache::loncommon::continue_data_table_row().
321: '<td colspan="2" align="right">');
322: if ($type eq 'course') {
323: if ($rcourse) {
324: my %descript=
325: &Apache::lonnet::coursedescription($rdom.'_'.$rcourse,
326: {'one_time' => 1});
327: $r->print($descript{'description'}.' ');
328: }
329: if ($constructmode) {
330: $r->print(&Apache::loncommon::selectcourse_link('rules',
331: 'course_'.$rulecounter,'domain_'.$rulecounter));
332: }
333: } else {
334: if ($rcourse) {
335: my $name = &Apache::loncommon::plainname($rcourse,$rdom);
336: $r->print($name.' ');
337: }
338: if ($constructmode) {
339: $r->print(&Apache::loncommon::selectstudent_link('rules','course_'.$rulecounter,'domain_'.$rulecounter));
340: }
1.12 albertel 341: }
1.3 www 342: # close row
1.23 albertel 343: $r->print('</td>'.&Apache::loncommon::end_data_table_row());
1.2 www 344: }
1.3 www 345: }
1.23 albertel 346: $r->print(&Apache::loncommon::end_data_table());
1.3 www 347: # ------------------------------------------------------------ End table output
348: if ($constructmode) {
1.22 albertel 349: $r->print('<input type="submit" name="store" value="'.&mt('Save').'" /></form>');
1.2 www 350: }
1.18 albertel 351: $r->print(&Apache::loncommon::end_page());
1.1 www 352: return OK;
353: }
354:
355:
356: 1;
357: __END__
358:
359:
360:
361:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>