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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to manage course access keys 
                      3: #
1.13    ! www         4: # $Id: lonmanagekeys.pm,v 1.12 2003/09/21 21:40:06 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.3       www       194:         my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
1.11      www       195: 	if ($ENV{'form.listkeyscsv'}) {
1.3       www       196: #
                    197: # CSV Output
                    198: #
1.2       www       199: 	    $r->content_type('text/csv');
1.11      www       200:             $r->send_http_header;
1.3       www       201: #
                    202: # Do CSV
                    203: #
1.11      www       204: 	    &show_key_list($r,1,$ENV{'form.listcom'},
                    205:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
                    206: 
1.2       www       207: 	} else {
1.3       www       208: #
                    209: # Normal web stuff
                    210: #
1.12      www       211: 	    &Apache::loncommon::content_type($r,'text/html');
1.2       www       212: 	    $r->send_http_header;
                    213: 	    $r->print(&header());
1.3       www       214: 	
                    215: 	    $r->print(
                    216: 	    '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
                    217: # --- Actions
                    218: 	    if ($ENV{'form.toggle'}) {
                    219: 		$r->print(&togglekeyaccess(%cenv).'<br />');
                    220: 		%cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
                    221: 	    }
1.5       www       222:             my $batchnumber='';
1.3       www       223: 	    if ($ENV{'form.genkeys'}) {
1.5       www       224: 		(my $msg,$batchnumber)=
                    225: 		    &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
                    226:                 $r->print($msg.'<br />');
1.3       www       227: 	    }
1.5       www       228:             if ($ENV{'form.listkeys'}) {
1.11      www       229: 		&show_key_list($r,0,$ENV{'form.listcom'},
1.7       www       230:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
1.9       www       231:             }
                    232:             if ($ENV{'form.addcom'}) {
                    233: 		&addcom(%cenv);
1.5       www       234:             }
1.3       www       235: # --- Menu
1.12      www       236: 	    $r->print('<h3>'.&mt('Key Access').'</h3>');
1.3       www       237: 	    if ($cenv{'keyaccess'} eq 'yes') {
1.12      www       238: 		$r->print(&mt('Access to this course is key controlled.').
                    239: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
1.3       www       240: 		} else {
1.12      www       241: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
1.2       www       242: 	    }
1.5       www       243: 	    $r->print(<<ENDKEYMENU);
1.3       www       244: <hr /><h3>Generate New Keys</h3>
                    245: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
                    246: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
                    247: <input type="submit" name="genkeys" value="Generate Keys" />
1.5       www       248: <hr /><h3>List Keys</h3>
1.11      www       249: Comments/Remarks/Notes/User/Batch Number Filter:
1.5       www       250: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
1.7       www       251: <input type="checkbox" name="newonly" /> Unused keys only<br />
1.8       www       252: <input type="checkbox" name="checkonly" /> Used keys only<br />
1.11      www       253: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
                    254: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
1.5       www       255: ENDKEYMENU
1.2       www       256: 	    $r->print('</form></body></html>');
                    257: 	}
1.1       www       258:     } else {
1.2       www       259: 	# Start page no course id
1.13    ! www       260: 	&Apache::loncommon::content_type($r,'text/html');
1.2       www       261: 	$r->send_http_header;
                    262: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
                    263:         $r->print(
1.13    ! www       264:    &mt('Course ID').': <input input type="text" size="25" name="course" value="" />');
        !           265:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
1.2       www       266:                $ENV{'request.role.domain'},'domain'));
                    267:         $r->print(&Apache::loncommon::selectcourse_link(
                    268: 					        'keyform','course','domain'));
1.13    ! www       269:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
1.2       www       270: 	$r->print('</form></body></html>');
1.1       www       271:     }
                    272:     return OK;
                    273: }
                    274: 
                    275: ###################################################################
                    276: ###################################################################
                    277: 
                    278: 1;
                    279: __END__
                    280: 
                    281: 

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