File:  [LON-CAPA] / loncom / interface / lonmanagekeys.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Apr 7 06:56:23 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, HEAD
- ENV -> env

    1: # The LearningOnline Network with CAPA
    2: # Handler to manage course access keys 
    3: #
    4: # $Id: lonmanagekeys.pm,v 1.17 2005/04/07 06:56:23 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 $html=&Apache::lonxml::xmlbegin();
   45:     my $bodytag=&Apache::loncommon::bodytag('Access Key Management');
   46:     return(<<ENDHEAD);
   47: $html
   48: <head>
   49: <title>LON-CAPA Access Key Management</title>
   50: </head>
   51: $bodytag
   52: <form method="post" enctype="multipart/form-data"  
   53:       action="/adm/managekeys" name="keyform">
   54: ENDHEAD
   55: }
   56: 
   57: # =================================================== Show student list to drop
   58: sub show_key_list {
   59:     my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_;
   60:     $comment=~s/\W/\./g;
   61:     my %accesskeys=&Apache::lonnet::dump
   62: 	('accesskeys',$cenv{'domain'},$cenv{'num'});
   63:     unless ($csvlist) {
   64: 	$r->print(<<ENDTABLEHEADER);
   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
   83:     }
   84:     foreach (keys %accesskeys) {
   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:             }
   94:             unless ($csvlist) {
   95: 		$r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
   96:                      $checkout:'-').'</td><td>'.
   97:                      join('<br />',split(/\s*\;\s*/,$com)).
   98: 		     '</td><td><input type="text" size="40" name="com_'.$_.
   99: 		     '" value="" /></td></tr>');
  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: 	    }
  112:        }
  113:     }
  114:     unless ($csvlist) {
  115: 	$r->print('</table>');
  116: 	$r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
  117:     }
  118:     return '';
  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') {
  128:        return 'Removing key access: '.
  129:        &Apache::lonnet::del('environment',['keyaccess'],
  130: 			    $cenv{'domain'},$cenv{'num'});
  131:    } else {
  132:       return 'Establishing key access: '.
  133:        &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
  134: 			    $cenv{'domain'},$cenv{'num'});
  135:     }
  136: }
  137: 
  138: # --------------------------------------------------------------- Generate Keys
  139: 
  140: sub genkeys {
  141:     my ($num,$comments,%cenv)=@_;
  142:     unless ($comments) { $comments=''; }
  143:     $comments=~s/\#/ /g;
  144:     $comments=~s/\;/ /g;
  145:     unless ($num) { return 'No number of keys given.'; }
  146:     unless (($num=~/^\d+$/) && ($num>0)) { 
  147: 	return 'Invalid number of keys given.'; 
  148:     }
  149:     my $batchnumber='BATCH_'.time().'_'.$$;
  150:     return 'Generated '.&Apache::lonnet::generate_access_keys
  151:     ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
  152:     $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
  153: }
  154: 
  155: # ---------------------------------------------------------------- Add comments
  156: 
  157: sub addcom {
  158:     my %cenv=@_;
  159:     my %newcomment=();
  160:     undef %newcomment;
  161:     foreach (keys %env) {
  162: 	if ($_=~/^form\.com\_(.+)$/) {
  163:             my $key=$1;
  164: 	    my $comment=$env{$_};
  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: }
  174: ###################################################################
  175: ###################################################################
  176: sub handler {
  177:     my $r=shift;
  178:     if ($r->header_only) {
  179:         &Apache::loncommon::content_type($r,'text/html');
  180:         $r->send_http_header;
  181:         return OK;
  182:     }
  183:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  184: 					    ['state','cid']);
  185:     if (($env{'form.domain'}) && ($env{'form.course'})) {
  186: 	$env{'form.cid'}=$env{'form.domain'}.'_'.$env{'form.course'};
  187:     }
  188: 
  189:     unless (&Apache::lonnet::allowed('mky',$env{'request.role.domain'})) {
  190:         $env{'user.error.msg'}=
  191:             "/adm/managekeys:mky:0:0:Cannot manage access keys";
  192:         return HTTP_NOT_ACCEPTABLE; 
  193:     }
  194:     if ($env{'form.cid'}) {
  195: 	my %cenv=&Apache::lonnet::coursedescription($env{'form.cid'});
  196: 	my $keytype='';
  197: 	if ($cenv{'url'} eq '/res/') {
  198: 	    ($cenv{'domain'},$cenv{'num'})=split(/\_/,$env{'form.cid'});
  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: 	}
  206:   	if ($env{'form.listkeyscsv'}) {
  207: #
  208: # CSV Output
  209: #
  210: 	    $r->content_type('text/csv');
  211:             $r->send_http_header;
  212: #
  213: # Do CSV
  214: #
  215: 	    &show_key_list($r,1,$env{'form.listcom'},
  216:                           $env{'form.newonly'},$env{'form.checkonly'},%cenv);
  217: 
  218: 	} else {
  219: #
  220: # Normal web stuff
  221: #
  222: 	    &Apache::loncommon::content_type($r,'text/html');
  223: 	    $r->send_http_header;
  224: 	    $r->print(&header());
  225: 	
  226: 	    $r->print(
  227: 	    '<input type="hidden" name="cid" value="'.$env{'form.cid'}.'" />');
  228: # --- Actions
  229: 	    if ($env{'form.toggle'}) {
  230: 		$r->print(&togglekeyaccess(%cenv).'<br />');
  231: 		%cenv=&Apache::lonnet::coursedescription($env{'form.cid'});
  232: 	    }
  233:             my $batchnumber='';
  234: 	    if ($env{'form.genkeys'}) {
  235: 		(my $msg,$batchnumber)=
  236: 		    &genkeys($env{'form.num'},$env{'form.comments'},%cenv);
  237:                 $r->print($msg.'<br />');
  238: 	    }
  239:             if ($env{'form.listkeys'}) {
  240: 		&show_key_list($r,0,$env{'form.listcom'},
  241:                           $env{'form.newonly'},$env{'form.checkonly'},%cenv);
  242:             }
  243:             if ($env{'form.addcom'}) {
  244: 		&addcom(%cenv);
  245:             }
  246: # --- Menu
  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.').
  251: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
  252: 		} else {
  253: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
  254: 		}
  255: 	    } else {
  256: 		$r->print('<h3>'.&mt('Key Authority').
  257: 			  ' <tt>'.$cenv{'num'}.'@'.$cenv{'domain'}.'</tt></h3>');
  258: 	    }
  259: 	    $r->print(<<ENDKEYMENU);
  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" />
  264: <hr /><h3>List Keys</h3>
  265: Comments/Remarks/Notes/User/Batch Number Filter:
  266: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
  267: <input type="checkbox" name="newonly" /> Unused keys only<br />
  268: <input type="checkbox" name="checkonly" /> Used keys only<br />
  269: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
  270: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
  271: ENDKEYMENU
  272: 	    $r->print('</form></body></html>');
  273: 	}
  274:     } else {
  275: 	# Start page no course id
  276: 	&Apache::loncommon::content_type($r,'text/html');
  277: 	$r->send_http_header;
  278: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
  279:         $r->print(
  280:    &mt('Course ID of Key Authority').': <input input type="text" size="25" name="course" value="" />');
  281:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
  282:                $env{'request.role.domain'},'domain'));
  283:         $r->print(&Apache::loncommon::selectcourse_link(
  284: 					        'keyform','course','domain'));
  285:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
  286: 	$r->print('</form></body></html>');
  287:     }
  288:     return OK;
  289: }
  290: 
  291: ###################################################################
  292: ###################################################################
  293: 
  294: 1;
  295: __END__
  296: 
  297: 

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