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

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

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