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

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

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