Annotation of loncom/interface/lonmanagekeys.pm, revision 1.14

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to manage course access keys 
                      3: #
1.14    ! www         4: # $Id: lonmanagekeys.pm,v 1.13 2003/09/23 00:26:10 www 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::lonmanagekeys;
                     32: 
                     33: use strict;
                     34: use Apache::lonnet();
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon();
                     37: use Apache::Constants qw(:common :http REDIRECT);
                     38: use Spreadsheet::WriteExcel;
1.12      www        39: use Apache::lonlocal;
1.1       www        40: 
                     41: ###############################################################
                     42: ###############################################################
                     43: sub header {
                     44:     my $bodytag=&Apache::loncommon::bodytag('Access Key Management');
                     45:     return(<<ENDHEAD);
                     46: <html>
                     47: <head>
                     48: <title>LON-CAPA Access Key Management</title>
                     49: </head>
                     50: $bodytag
                     51: <form method="post" enctype="multipart/form-data"  
1.2       www        52:       action="/adm/managekeys" name="keyform">
1.1       www        53: ENDHEAD
                     54: }
                     55: 
                     56: # =================================================== Show student list to drop
                     57: sub show_key_list {
1.11      www        58:     my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_;
1.7       www        59:     $comment=~s/\W/\./g;
1.6       www        60:     my %accesskeys=&Apache::lonnet::dump
                     61: 	('accesskeys',$cenv{'domain'},$cenv{'num'});
1.11      www        62:     unless ($csvlist) {
                     63: 	$r->print(<<ENDTABLEHEADER);
1.10      www        64: <script>
                     65:     function copyallcom(tf) {
                     66: 	for (i=0; i<tf.elements.length; i++) {
                     67:             if  (tf.elements[i].name.indexOf('com_')==0) {
                     68: 	      tf.elements[i].value+=tf.copyall.value;
                     69:             }
                     70:         }
                     71: 
                     72:     }
                     73: </script>
                     74: <h3>List of Keys/Enter New Comments</h3>
                     75: <table border="2"><tr><th>Key</th><th>Checked Out</th>
                     76: <th>Comments/Remarks/Notes</th>
                     77: <th>Enter Additional Comments/Remarks/Notes<br />
                     78: <input type="text" size="40" name="copyall" />
                     79: <input type="button" value="Copy to All" onClick="copyallcom(this.form);" />
                     80: </th></tr>
                     81: ENDTABLEHEADER
1.11      www        82:     }
1.6       www        83:     foreach (keys %accesskeys) {
1.7       www        84:         if ($_=~/^error\:/) {
                     85: 	    $r->print('<tr><td>No keys have been generated yet.</td></tr>');
                     86:         } elsif ($accesskeys{$_}=~/$comment/) {
                     87: 	    my ($checkout,$com)=split(/\s*\#\s*/,$accesskeys{$_});
                     88:             unless ($checkout) {
                     89: 		if ($checkonly) { next; }
                     90:             } else {
                     91: 		if ($newonly) { next; }
                     92:             }
1.11      www        93:             unless ($csvlist) {
                     94: 		$r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
1.7       www        95:                      $checkout:'-').'</td><td>'.
1.8       www        96:                      join('<br />',split(/\s*\;\s*/,$com)).
1.9       www        97: 		     '</td><td><input type="text" size="40" name="com_'.$_.
1.10      www        98: 		     '" value="" /></td></tr>');
1.11      www        99: 	    } else {
                    100: 		my @line = ();
                    101: 		push @line,&Apache::loncommon::csv_translate($_);
                    102: 		push @line,&Apache::loncommon::csv_translate($checkout);
                    103: 		foreach (split(/\s*\;\s*/,$com)) {
                    104:  		   push @line,&Apache::loncommon::csv_translate($_);
                    105: 		}
                    106: 		my $tmp = $";
                    107: 		$" = '","';
                    108: 		$r->print("\"@line\"\n");
                    109: 		$" = $tmp;
                    110: 	    }
1.7       www       111:        }
1.6       www       112:     }
1.11      www       113:     unless ($csvlist) {
                    114: 	$r->print('</table>');
                    115: 	$r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
                    116:     }
1.6       www       117:     return '';
1.2       www       118: }
                    119: 
                    120: 
                    121: # ----------------------------------------------------------- Toggle Key Access
                    122: 
                    123: sub togglekeyaccess {
                    124:     my %cenv=@_;
                    125:     unless ($cenv{'domain'}) { return; }
                    126:     if ($cenv{'keyaccess'} eq 'yes') {
1.3       www       127:        return 'Removing key access: '.
1.2       www       128:        &Apache::lonnet::del('environment',['keyaccess'],
                    129: 			    $cenv{'domain'},$cenv{'num'});
                    130:    } else {
1.3       www       131:       return 'Establishing key access: '.
1.2       www       132:        &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
                    133: 			    $cenv{'domain'},$cenv{'num'});
1.1       www       134:     }
                    135: }
                    136: 
1.3       www       137: # --------------------------------------------------------------- Generate Keys
                    138: 
                    139: sub genkeys {
                    140:     my ($num,$comments,%cenv)=@_;
1.5       www       141:     unless ($comments) { $comments=''; }
                    142:     $comments=~s/\#/ /g;
                    143:     $comments=~s/\;/ /g;
1.3       www       144:     unless ($num) { return 'No number of keys given.'; }
                    145:     unless (($num=~/^\d+$/) && ($num>0)) { 
                    146: 	return 'Invalid number of keys given.'; 
                    147:     }
1.5       www       148:     my $batchnumber='BATCH_'.time().'_'.$$;
1.3       www       149:     return 'Generated '.&Apache::lonnet::generate_access_keys
1.5       www       150:     ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
                    151:     $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
1.3       www       152: }
                    153: 
1.9       www       154: # ---------------------------------------------------------------- Add comments
                    155: 
                    156: sub addcom {
                    157:     my %cenv=@_;
                    158:     my %newcomment=();
                    159:     undef %newcomment;
                    160:     foreach (keys %ENV) {
                    161: 	if ($_=~/^form\.com\_(.+)$/) {
                    162:             my $key=$1;
                    163: 	    my $comment=$ENV{$_};
                    164:             $comment=~s/^\s+//gs;
                    165:             if ($comment) {
                    166:                &Apache::lonnet::comment_access_key
                    167: 		   ($key,$cenv{'domain'},$cenv{'num'},$comment); 
                    168: 	   }
                    169: 	}
                    170:     }
                    171:     return '';
                    172: }
1.1       www       173: ###################################################################
                    174: ###################################################################
                    175: sub handler {
                    176:     my $r=shift;
                    177:     if ($r->header_only) {
1.12      www       178:         &Apache::loncommon::content_type($r,'text/html');
1.1       www       179:         $r->send_http_header;
                    180:         return OK;
                    181:     }
1.2       www       182:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    183: 					    ['state','cid']);
                    184:     if (($ENV{'form.domain'}) && ($ENV{'form.course'})) {
                    185: 	$ENV{'form.cid'}=$ENV{'form.domain'}.'_'.$ENV{'form.course'};
                    186:     }
1.1       www       187: 
                    188:     unless (&Apache::lonnet::allowed('mky',$ENV{'request.role.domain'})) {
                    189:         $ENV{'user.error.msg'}=
                    190:             "/adm/managekeys:mky:0:0:Cannot manage access keys";
                    191:         return HTTP_NOT_ACCEPTABLE; 
                    192:     }
1.2       www       193:     if ($ENV{'form.cid'}) {
1.14    ! www       194: 	my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
        !           195: 	my $keytype='';
        !           196: 	if ($cenv{'url'} eq '/res/') {
        !           197: 	    ($cenv{'num'},$cenv{'domain'})=split(/\_/,$ENV{'form.cid'});
        !           198: 	    $keytype='auth';
        !           199: 	} elsif ($cenv{'keyauth'}) {
        !           200: 	    ($cenv{'num'},$cenv{'domain'})=split(/\W/,$cenv{'keyauth'});
        !           201: 	    $keytype='auth';
        !           202: 	} else {
        !           203: 	    $keytype='course';
        !           204: 	}
        !           205:   	if ($ENV{'form.listkeyscsv'}) {
1.3       www       206: #
                    207: # CSV Output
                    208: #
1.2       www       209: 	    $r->content_type('text/csv');
1.11      www       210:             $r->send_http_header;
1.3       www       211: #
                    212: # Do CSV
                    213: #
1.11      www       214: 	    &show_key_list($r,1,$ENV{'form.listcom'},
                    215:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
                    216: 
1.2       www       217: 	} else {
1.3       www       218: #
                    219: # Normal web stuff
                    220: #
1.12      www       221: 	    &Apache::loncommon::content_type($r,'text/html');
1.2       www       222: 	    $r->send_http_header;
                    223: 	    $r->print(&header());
1.3       www       224: 	
                    225: 	    $r->print(
                    226: 	    '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
                    227: # --- Actions
                    228: 	    if ($ENV{'form.toggle'}) {
                    229: 		$r->print(&togglekeyaccess(%cenv).'<br />');
                    230: 		%cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
                    231: 	    }
1.5       www       232:             my $batchnumber='';
1.3       www       233: 	    if ($ENV{'form.genkeys'}) {
1.5       www       234: 		(my $msg,$batchnumber)=
                    235: 		    &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
                    236:                 $r->print($msg.'<br />');
1.3       www       237: 	    }
1.5       www       238:             if ($ENV{'form.listkeys'}) {
1.11      www       239: 		&show_key_list($r,0,$ENV{'form.listcom'},
1.7       www       240:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
1.9       www       241:             }
                    242:             if ($ENV{'form.addcom'}) {
                    243: 		&addcom(%cenv);
1.5       www       244:             }
1.3       www       245: # --- Menu
1.14    ! www       246: 	    if ($keytype eq 'course') {
        !           247: 		$r->print('<h3>'.&mt('Key Access').'</h3>');
        !           248: 		if ($cenv{'keyaccess'} eq 'yes') {
        !           249: 		    $r->print(&mt('Access to this course is key controlled.').
1.12      www       250: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
1.3       www       251: 		} else {
1.12      www       252: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
1.14    ! www       253: 		}
        !           254: 	    } else {
        !           255: 		$r->print('<h3>'.&mt('Key Authority').
        !           256: 			  ' <tt>'.$cenv{'num'}.'@'.$cenv{'domain'}.'</tt></h3>');
1.2       www       257: 	    }
1.5       www       258: 	    $r->print(<<ENDKEYMENU);
1.3       www       259: <hr /><h3>Generate New Keys</h3>
                    260: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
                    261: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
                    262: <input type="submit" name="genkeys" value="Generate Keys" />
1.5       www       263: <hr /><h3>List Keys</h3>
1.11      www       264: Comments/Remarks/Notes/User/Batch Number Filter:
1.5       www       265: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
1.7       www       266: <input type="checkbox" name="newonly" /> Unused keys only<br />
1.8       www       267: <input type="checkbox" name="checkonly" /> Used keys only<br />
1.11      www       268: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
                    269: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
1.5       www       270: ENDKEYMENU
1.2       www       271: 	    $r->print('</form></body></html>');
                    272: 	}
1.1       www       273:     } else {
1.2       www       274: 	# Start page no course id
1.13      www       275: 	&Apache::loncommon::content_type($r,'text/html');
1.2       www       276: 	$r->send_http_header;
                    277: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
                    278:         $r->print(
1.14    ! www       279:    &mt('Course ID of Key Authority').': <input input type="text" size="25" name="course" value="" />');
1.13      www       280:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
1.2       www       281:                $ENV{'request.role.domain'},'domain'));
                    282:         $r->print(&Apache::loncommon::selectcourse_link(
                    283: 					        'keyform','course','domain'));
1.13      www       284:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
1.2       www       285: 	$r->print('</form></body></html>');
1.1       www       286:     }
                    287:     return OK;
                    288: }
                    289: 
                    290: ###################################################################
                    291: ###################################################################
                    292: 
                    293: 1;
                    294: __END__
                    295: 
                    296: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>