File:  [LON-CAPA] / loncom / interface / lonmanagekeys.pm
Revision 1.22: download - view: text, annotated - select for diffs
Wed Jun 20 23:15:15 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_8_X, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_99_0, HEAD, GCI_1
- add in labels to check boxes

    1: # The LearningOnline Network with CAPA
    2: # Handler to manage course access keys 
    3: #
    4: # $Id: lonmanagekeys.pm,v 1.22 2007/06/20 23:15:15 albertel Exp $
    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;
   39: use Apache::lonlocal;
   40: 
   41: ###############################################################
   42: ###############################################################
   43: sub header {
   44:     my $start_page=&Apache::loncommon::start_page('Access Key Management');
   45:     return(<<ENDHEAD);
   46: $start_page
   47: <form method="post" enctype="multipart/form-data"  
   48:       action="/adm/managekeys" name="keyform">
   49: ENDHEAD
   50: }
   51: 
   52: # =================================================== Show student list to drop
   53: sub show_key_list {
   54:     my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_;
   55:     $comment=~s/\W/\./g;
   56:     my %accesskeys=&Apache::lonnet::dump
   57: 	('accesskeys',$cenv{'domain'},$cenv{'num'});
   58:     unless ($csvlist) {
   59: 	$r->print(<<ENDTABLEHEADER);
   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
   78:     }
   79:     foreach (keys %accesskeys) {
   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:             }
   89:             unless ($csvlist) {
   90: 		$r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
   91:                      $checkout:'-').'</td><td>'.
   92:                      join('<br />',split(/\s*\;\s*/,$com)).
   93: 		     '</td><td><input type="text" size="40" name="com_'.$_.
   94: 		     '" value="" /></td></tr>');
   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: 	    }
  107:        }
  108:     }
  109:     unless ($csvlist) {
  110: 	$r->print('</table>');
  111: 	$r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
  112:     }
  113:     return '';
  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') {
  123:        return 'Removing key access: '.
  124:        &Apache::lonnet::del('environment',['keyaccess'],
  125: 			    $cenv{'domain'},$cenv{'num'});
  126:    } else {
  127:       return 'Establishing key access: '.
  128:        &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
  129: 			    $cenv{'domain'},$cenv{'num'});
  130:     }
  131: }
  132: 
  133: # --------------------------------------------------------------- Generate Keys
  134: 
  135: sub genkeys {
  136:     my ($num,$comments,%cenv)=@_;
  137:     unless ($comments) { $comments=''; }
  138:     $comments=~s/\#/ /g;
  139:     $comments=~s/\;/ /g;
  140:     unless ($num) { return 'No number of keys given.'; }
  141:     unless (($num=~/^\d+$/) && ($num>0)) { 
  142: 	return 'Invalid number of keys given.'; 
  143:     }
  144:     my $batchnumber='BATCH_'.time().'_'.$$;
  145:     return 'Generated '.&Apache::lonnet::generate_access_keys
  146:     ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
  147:     $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
  148: }
  149: 
  150: # ---------------------------------------------------------------- Add comments
  151: 
  152: sub addcom {
  153:     my %cenv=@_;
  154:     my %newcomment=();
  155:     undef %newcomment;
  156:     foreach (keys %env) {
  157: 	if ($_=~/^form\.com\_(.+)$/) {
  158:             my $key=$1;
  159: 	    my $comment=$env{$_};
  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: }
  169: ###################################################################
  170: ###################################################################
  171: sub handler {
  172:     my $r=shift;
  173:     if ($r->header_only) {
  174:         &Apache::loncommon::content_type($r,'text/html');
  175:         $r->send_http_header;
  176:         return OK;
  177:     }
  178:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  179: 					    ['state','cid']);
  180:     if (($env{'form.domain'}) && ($env{'form.course'})) {
  181: 	$env{'form.cid'}=$env{'form.domain'}.'_'.$env{'form.course'};
  182:     }
  183: 
  184:     unless (&Apache::lonnet::allowed('mky',$env{'request.role.domain'})) {
  185:         $env{'user.error.msg'}=
  186:             "/adm/managekeys:mky:0:0:Cannot manage access keys";
  187:         return HTTP_NOT_ACCEPTABLE; 
  188:     }
  189:     if ($env{'form.cid'}) {
  190: 	my %cenv=&Apache::lonnet::coursedescription($env{'form.cid'});
  191: 	my $keytype='';
  192: 	if ($cenv{'url'} eq '/res/') {
  193: 	    ($cenv{'domain'},$cenv{'num'})=split(/\_/,$env{'form.cid'});
  194: 	    $keytype='auth';
  195: 	} elsif ($cenv{'keyauth'}) {
  196: 	    ($cenv{'num'},$cenv{'domain'})=split(/:/,$cenv{'keyauth'});
  197: 	    $keytype='auth';
  198: 	} else {
  199: 	    $keytype='course';
  200: 	}
  201:   	if ($env{'form.listkeyscsv'}) {
  202: #
  203: # CSV Output
  204: #
  205: 	    $r->content_type('text/csv');
  206:             $r->send_http_header;
  207: #
  208: # Do CSV
  209: #
  210: 	    &show_key_list($r,1,$env{'form.listcom'},
  211:                           $env{'form.newonly'},$env{'form.checkonly'},%cenv);
  212: 
  213: 	} else {
  214: #
  215: # Normal web stuff
  216: #
  217: 	    &Apache::loncommon::content_type($r,'text/html');
  218: 	    $r->send_http_header;
  219: 	    $r->print(&header());
  220: 	
  221: 	    $r->print(
  222: 	    '<input type="hidden" name="cid" value="'.$env{'form.cid'}.'" />');
  223: # --- Actions
  224: 	    if ($env{'form.toggle'}) {
  225: 		$r->print(&togglekeyaccess(%cenv).'<br />');
  226: 		%cenv=&Apache::lonnet::coursedescription($env{'form.cid'},
  227: 							 {'freshen_cache'=> 1});
  228: 	    }
  229:             my $batchnumber='';
  230: 	    if ($env{'form.genkeys'}) {
  231: 		(my $msg,$batchnumber)=
  232: 		    &genkeys($env{'form.num'},$env{'form.comments'},%cenv);
  233:                 $r->print($msg.'<br />');
  234: 	    }
  235:             if ($env{'form.listkeys'}) {
  236: 		&show_key_list($r,0,$env{'form.listcom'},
  237:                           $env{'form.newonly'},$env{'form.checkonly'},%cenv);
  238:             }
  239:             if ($env{'form.addcom'}) {
  240: 		&addcom(%cenv);
  241:             }
  242: # --- Menu
  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.').
  247: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
  248: 		} else {
  249: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
  250: 		}
  251: 	    } else {
  252: 		$r->print('<h3>'.&mt('Key Authority').
  253: 			  ' <tt>'.$cenv{'num'}.'@'.$cenv{'domain'}.'</tt></h3>');
  254: 	    }
  255: 	    $r->print(<<ENDKEYMENU);
  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" />
  260: <hr /><h3>List Keys</h3>
  261: Comments/Remarks/Notes/User/Batch Number Filter:
  262: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
  263: <label><input type="checkbox" name="newonly" /> Unused keys only</label><br />
  264: <label><input type="checkbox" name="checkonly" /> Used keys only</label><br />
  265: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
  266: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
  267: ENDKEYMENU
  268: 	    $r->print('</form>'.&Apache::loncommon::end_page());
  269: 	}
  270:     } else {
  271: 	# Start page no course id
  272: 	&Apache::loncommon::content_type($r,'text/html');
  273: 	$r->send_http_header;
  274: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
  275:         $r->print(
  276:    &mt('Course ID of Key Authority').': <input input type="text" size="25" name="course" value="" />');
  277:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
  278:                $env{'request.role.domain'},'domain'));
  279:         $r->print(&Apache::loncommon::selectcourse_link(
  280: 					        'keyform','course','domain',
  281:                                                 undef,undef,undef,'Course'));
  282:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
  283: 	$r->print('</form>'.&Apache::loncommon::end_page());
  284:     }
  285:     return OK;
  286: }
  287: 
  288: ###################################################################
  289: ###################################################################
  290: 
  291: 1;
  292: __END__
  293: 
  294: 

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