File:  [LON-CAPA] / loncom / interface / lonmanagekeys.pm
Revision 1.15: download - view: text, annotated - select for diffs
Sun May 9 00:45:01 2004 UTC (19 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, HEAD
Bug #2970: Key authority other than course possible

    1: # The LearningOnline Network with CAPA
    2: # Handler to manage course access keys 
    3: #
    4: # $Id: lonmanagekeys.pm,v 1.15 2004/05/09 00:45:01 www 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 $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"  
   52:       action="/adm/managekeys" name="keyform">
   53: ENDHEAD
   54: }
   55: 
   56: # =================================================== Show student list to drop
   57: sub show_key_list {
   58:     my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_;
   59:     $comment=~s/\W/\./g;
   60:     my %accesskeys=&Apache::lonnet::dump
   61: 	('accesskeys',$cenv{'domain'},$cenv{'num'});
   62:     unless ($csvlist) {
   63: 	$r->print(<<ENDTABLEHEADER);
   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
   82:     }
   83:     foreach (keys %accesskeys) {
   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:             }
   93:             unless ($csvlist) {
   94: 		$r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
   95:                      $checkout:'-').'</td><td>'.
   96:                      join('<br />',split(/\s*\;\s*/,$com)).
   97: 		     '</td><td><input type="text" size="40" name="com_'.$_.
   98: 		     '" value="" /></td></tr>');
   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: 	    }
  111:        }
  112:     }
  113:     unless ($csvlist) {
  114: 	$r->print('</table>');
  115: 	$r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
  116:     }
  117:     return '';
  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') {
  127:        return 'Removing key access: '.
  128:        &Apache::lonnet::del('environment',['keyaccess'],
  129: 			    $cenv{'domain'},$cenv{'num'});
  130:    } else {
  131:       return 'Establishing key access: '.
  132:        &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
  133: 			    $cenv{'domain'},$cenv{'num'});
  134:     }
  135: }
  136: 
  137: # --------------------------------------------------------------- Generate Keys
  138: 
  139: sub genkeys {
  140:     my ($num,$comments,%cenv)=@_;
  141:     unless ($comments) { $comments=''; }
  142:     $comments=~s/\#/ /g;
  143:     $comments=~s/\;/ /g;
  144:     unless ($num) { return 'No number of keys given.'; }
  145:     unless (($num=~/^\d+$/) && ($num>0)) { 
  146: 	return 'Invalid number of keys given.'; 
  147:     }
  148:     my $batchnumber='BATCH_'.time().'_'.$$;
  149:     return 'Generated '.&Apache::lonnet::generate_access_keys
  150:     ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
  151:     $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
  152: }
  153: 
  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: }
  173: ###################################################################
  174: ###################################################################
  175: sub handler {
  176:     my $r=shift;
  177:     if ($r->header_only) {
  178:         &Apache::loncommon::content_type($r,'text/html');
  179:         $r->send_http_header;
  180:         return OK;
  181:     }
  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:     }
  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:     }
  193:     if ($ENV{'form.cid'}) {
  194: 	my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
  195: 	my $keytype='';
  196: 	if ($cenv{'url'} eq '/res/') {
  197: 	    ($cenv{'domain'},$cenv{'num'})=split(/\_/,$ENV{'form.cid'});
  198: 	    $keytype='auth';
  199: 	} elsif ($cenv{'keyauth'}) {
  200: 	    ($cenv{'num'},$cenv{'domain'})=split(/\W/,$cenv{'keyauth'});
  201: 	    $keytype='auth';
  202: 	} else {
  203: 	    $keytype='course';
  204: 	}
  205:   	if ($ENV{'form.listkeyscsv'}) {
  206: #
  207: # CSV Output
  208: #
  209: 	    $r->content_type('text/csv');
  210:             $r->send_http_header;
  211: #
  212: # Do CSV
  213: #
  214: 	    &show_key_list($r,1,$ENV{'form.listcom'},
  215:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
  216: 
  217: 	} else {
  218: #
  219: # Normal web stuff
  220: #
  221: 	    &Apache::loncommon::content_type($r,'text/html');
  222: 	    $r->send_http_header;
  223: 	    $r->print(&header());
  224: 	
  225: 	    $r->print(
  226: 	    '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
  227: # --- Actions
  228: 	    if ($ENV{'form.toggle'}) {
  229: 		$r->print(&togglekeyaccess(%cenv).'<br />');
  230: 		%cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
  231: 	    }
  232:             my $batchnumber='';
  233: 	    if ($ENV{'form.genkeys'}) {
  234: 		(my $msg,$batchnumber)=
  235: 		    &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
  236:                 $r->print($msg.'<br />');
  237: 	    }
  238:             if ($ENV{'form.listkeys'}) {
  239: 		&show_key_list($r,0,$ENV{'form.listcom'},
  240:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
  241:             }
  242:             if ($ENV{'form.addcom'}) {
  243: 		&addcom(%cenv);
  244:             }
  245: # --- Menu
  246: 	    if ($keytype eq 'course') {
  247: 		$r->print('<h3>'.&mt('Key Access').'</h3>');
  248: 		if ($cenv{'keyaccess'} eq 'yes') {
  249: 		    $r->print(&mt('Access to this course is key controlled.').
  250: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
  251: 		} else {
  252: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
  253: 		}
  254: 	    } else {
  255: 		$r->print('<h3>'.&mt('Key Authority').
  256: 			  ' <tt>'.$cenv{'num'}.'@'.$cenv{'domain'}.'</tt></h3>');
  257: 	    }
  258: 	    $r->print(<<ENDKEYMENU);
  259: <hr /><h3>Generate New Keys</h3>
  260: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
  261: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
  262: <input type="submit" name="genkeys" value="Generate Keys" />
  263: <hr /><h3>List Keys</h3>
  264: Comments/Remarks/Notes/User/Batch Number Filter:
  265: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
  266: <input type="checkbox" name="newonly" /> Unused keys only<br />
  267: <input type="checkbox" name="checkonly" /> Used keys only<br />
  268: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
  269: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
  270: ENDKEYMENU
  271: 	    $r->print('</form></body></html>');
  272: 	}
  273:     } else {
  274: 	# Start page no course id
  275: 	&Apache::loncommon::content_type($r,'text/html');
  276: 	$r->send_http_header;
  277: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
  278:         $r->print(
  279:    &mt('Course ID of Key Authority').': <input input type="text" size="25" name="course" value="" />');
  280:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
  281:                $ENV{'request.role.domain'},'domain'));
  282:         $r->print(&Apache::loncommon::selectcourse_link(
  283: 					        'keyform','course','domain'));
  284:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
  285: 	$r->print('</form></body></html>');
  286:     }
  287:     return OK;
  288: }
  289: 
  290: ###################################################################
  291: ###################################################################
  292: 
  293: 1;
  294: __END__
  295: 
  296: 

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