File:  [LON-CAPA] / loncom / interface / lonmanagekeys.pm
Revision 1.13: download - view: text, annotated - select for diffs
Tue Sep 23 00:26:10 2003 UTC (20 years, 7 months ago) by www
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Internationalizing.

    1: # The LearningOnline Network with CAPA
    2: # Handler to manage course access keys 
    3: #
    4: # $Id: lonmanagekeys.pm,v 1.13 2003/09/23 00:26:10 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: 	if ($ENV{'form.listkeyscsv'}) {
  196: #
  197: # CSV Output
  198: #
  199: 	    $r->content_type('text/csv');
  200:             $r->send_http_header;
  201: #
  202: # Do CSV
  203: #
  204: 	    &show_key_list($r,1,$ENV{'form.listcom'},
  205:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
  206: 
  207: 	} else {
  208: #
  209: # Normal web stuff
  210: #
  211: 	    &Apache::loncommon::content_type($r,'text/html');
  212: 	    $r->send_http_header;
  213: 	    $r->print(&header());
  214: 	
  215: 	    $r->print(
  216: 	    '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
  217: # --- Actions
  218: 	    if ($ENV{'form.toggle'}) {
  219: 		$r->print(&togglekeyaccess(%cenv).'<br />');
  220: 		%cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
  221: 	    }
  222:             my $batchnumber='';
  223: 	    if ($ENV{'form.genkeys'}) {
  224: 		(my $msg,$batchnumber)=
  225: 		    &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
  226:                 $r->print($msg.'<br />');
  227: 	    }
  228:             if ($ENV{'form.listkeys'}) {
  229: 		&show_key_list($r,0,$ENV{'form.listcom'},
  230:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
  231:             }
  232:             if ($ENV{'form.addcom'}) {
  233: 		&addcom(%cenv);
  234:             }
  235: # --- Menu
  236: 	    $r->print('<h3>'.&mt('Key Access').'</h3>');
  237: 	    if ($cenv{'keyaccess'} eq 'yes') {
  238: 		$r->print(&mt('Access to this course is key controlled.').
  239: '<br /><input type="submit" name="toggle" value="'.&mt('Open Access').'" />')
  240: 		} else {
  241: 		    $r->print(&mt('Access to this course is open, no access keys').'<br /><input type="submit" name="toggle" value="'.&mt('Control Access').'" />');
  242: 	    }
  243: 	    $r->print(<<ENDKEYMENU);
  244: <hr /><h3>Generate New Keys</h3>
  245: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
  246: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
  247: <input type="submit" name="genkeys" value="Generate Keys" />
  248: <hr /><h3>List Keys</h3>
  249: Comments/Remarks/Notes/User/Batch Number Filter:
  250: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
  251: <input type="checkbox" name="newonly" /> Unused keys only<br />
  252: <input type="checkbox" name="checkonly" /> Used keys only<br />
  253: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
  254: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
  255: ENDKEYMENU
  256: 	    $r->print('</form></body></html>');
  257: 	}
  258:     } else {
  259: 	# Start page no course id
  260: 	&Apache::loncommon::content_type($r,'text/html');
  261: 	$r->send_http_header;
  262: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
  263:         $r->print(
  264:    &mt('Course ID').': <input input type="text" size="25" name="course" value="" />');
  265:         $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form(
  266:                $ENV{'request.role.domain'},'domain'));
  267:         $r->print(&Apache::loncommon::selectcourse_link(
  268: 					        'keyform','course','domain'));
  269:         $r->print('<br /><input type="submit" value="'.&mt('Manage Access Keys').'" />');
  270: 	$r->print('</form></body></html>');
  271:     }
  272:     return OK;
  273: }
  274: 
  275: ###################################################################
  276: ###################################################################
  277: 
  278: 1;
  279: __END__
  280: 
  281: 

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