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

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

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