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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to manage course access keys 
                      3: #
1.10    ! www         4: # $Id: lonmanagekeys.pm,v 1.9 2003/05/02 15:26:36 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.7       www        57:     my ($r,$comment,$newonly,$checkonly,%cenv)=@_;
                     58:     $comment=~s/\W/\./g;
1.6       www        59:     my %accesskeys=&Apache::lonnet::dump
                     60: 	('accesskeys',$cenv{'domain'},$cenv{'num'});
1.10    ! www        61:     $r->print(<<ENDTABLEHEADER);
        !            62: <script>
        !            63:     function copyallcom(tf) {
        !            64: 	for (i=0; i<tf.elements.length; i++) {
        !            65:             if  (tf.elements[i].name.indexOf('com_')==0) {
        !            66: 	      tf.elements[i].value+=tf.copyall.value;
        !            67:             }
        !            68:         }
        !            69: 
        !            70:     }
        !            71: </script>
        !            72: <h3>List of Keys/Enter New Comments</h3>
        !            73: <table border="2"><tr><th>Key</th><th>Checked Out</th>
        !            74: <th>Comments/Remarks/Notes</th>
        !            75: <th>Enter Additional Comments/Remarks/Notes<br />
        !            76: <input type="text" size="40" name="copyall" />
        !            77: <input type="button" value="Copy to All" onClick="copyallcom(this.form);" />
        !            78: </th></tr>
        !            79: ENDTABLEHEADER
1.6       www        80:     foreach (keys %accesskeys) {
1.7       www        81:         if ($_=~/^error\:/) {
                     82: 	    $r->print('<tr><td>No keys have been generated yet.</td></tr>');
                     83:         } elsif ($accesskeys{$_}=~/$comment/) {
                     84: 	    my ($checkout,$com)=split(/\s*\#\s*/,$accesskeys{$_});
                     85:             unless ($checkout) {
                     86: 		if ($checkonly) { next; }
                     87:             } else {
                     88: 		if ($newonly) { next; }
                     89:             }
                     90: 	   $r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
                     91:                      $checkout:'-').'</td><td>'.
1.8       www        92:                      join('<br />',split(/\s*\;\s*/,$com)).
1.9       www        93: 		     '</td><td><input type="text" size="40" name="com_'.$_.
1.10    ! www        94: 		     '" value="" /></td></tr>');
1.7       www        95:        }
1.6       www        96:     }
1.7       www        97:     $r->print('</table>');
1.9       www        98:     $r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
1.6       www        99:     return '';
                    100: #
                    101: # Junk below
                    102: #
                    103:     my ($mode,$linkto,$action,$statusmode,$classlist,$keylist);
1.1       www       104: #
                    105: # Just junk so that this compiles
                    106: #
                    107:     my ($username,$domain,$id,$name,$section,$status,@Sorted_Students);
                    108: #
                    109: # 
                    110: # 
1.2       www       111:     my $cid=$ENV{'form.cid'};
1.1       www       112:     #
                    113:     # Variables for excel output
                    114:     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
                    115:     #
                    116: 
                    117:     # Print out header 
                    118:     if ($mode eq 'view') {
                    119:     } elsif ($mode eq 'excel') {
                    120:         # Create the excel spreadsheet
                    121:         $excel_filename = '/prtspool/'.
                    122:             $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                    123:                 time.'_'.rand(1000000000).'.xls';
                    124:         $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
                    125:                                                        $excel_filename);
                    126:         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
                    127:         $excel_sheet = $excel_workbook->addworksheet('classlist');
                    128:         #
                    129:         my $description = 'Classlist for '.
                    130:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    131:         $excel_sheet->write($row++,0,$description);
                    132:         #
                    133:         $excel_sheet->write($row++,0,["username","domain","ID",
                    134:                                       "student name","section","status"]);
                    135:     }
                    136:     foreach my $student (@Sorted_Students) {
                    137:         if ($mode eq 'view') {
                    138:         } elsif ($mode eq 'csv') {
                    139:             # no need to bother with $linkto
                    140:             my @line = ();
                    141:             foreach ($username,$domain,$id,$name,$section) {
                    142:                 push @line,&Apache::loncommon::csv_translate($_);
                    143:             }
                    144:             if ($statusmode eq 'Any') {
                    145:                 push @line,&Apache::loncommon::csv_translate($status);
                    146:             }
                    147:             my $tmp = $";
                    148:             $" = '","';
                    149:             $r->print("\"@line\"\n");
                    150:             $" = $tmp;
                    151:         } elsif ($mode eq 'excel') {
                    152:             $excel_sheet->write($row++,0,[$username,$domain,$id,
                    153:                                           $name,$section,$status]);
                    154:         }
                    155:     }
                    156:     if ($mode eq 'view') {
                    157:     } elsif ($mode eq 'excel') {
                    158:         $excel_workbook->close();
                    159:         $r->print('<p><a href="'.$excel_filename.'">'.
1.2       www       160:                'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
                    161:     }
                    162: }
                    163: 
                    164: 
                    165: # ----------------------------------------------------------- Toggle Key Access
                    166: 
                    167: sub togglekeyaccess {
                    168:     my %cenv=@_;
                    169:     unless ($cenv{'domain'}) { return; }
                    170:     if ($cenv{'keyaccess'} eq 'yes') {
1.3       www       171:        return 'Removing key access: '.
1.2       www       172:        &Apache::lonnet::del('environment',['keyaccess'],
                    173: 			    $cenv{'domain'},$cenv{'num'});
                    174:    } else {
1.3       www       175:       return 'Establishing key access: '.
1.2       www       176:        &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
                    177: 			    $cenv{'domain'},$cenv{'num'});
1.1       www       178:     }
                    179: }
                    180: 
1.3       www       181: # --------------------------------------------------------------- Generate Keys
                    182: 
                    183: sub genkeys {
                    184:     my ($num,$comments,%cenv)=@_;
1.5       www       185:     unless ($comments) { $comments=''; }
                    186:     $comments=~s/\#/ /g;
                    187:     $comments=~s/\;/ /g;
1.3       www       188:     unless ($num) { return 'No number of keys given.'; }
                    189:     unless (($num=~/^\d+$/) && ($num>0)) { 
                    190: 	return 'Invalid number of keys given.'; 
                    191:     }
1.5       www       192:     my $batchnumber='BATCH_'.time().'_'.$$;
1.3       www       193:     return 'Generated '.&Apache::lonnet::generate_access_keys
1.5       www       194:     ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
                    195:     $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
1.3       www       196: }
                    197: 
1.9       www       198: # ---------------------------------------------------------------- Add comments
                    199: 
                    200: sub addcom {
                    201:     my %cenv=@_;
                    202:     my %newcomment=();
                    203:     undef %newcomment;
                    204:     foreach (keys %ENV) {
                    205: 	if ($_=~/^form\.com\_(.+)$/) {
                    206:             my $key=$1;
                    207: 	    my $comment=$ENV{$_};
                    208:             $comment=~s/^\s+//gs;
                    209:             if ($comment) {
                    210:                &Apache::lonnet::comment_access_key
                    211: 		   ($key,$cenv{'domain'},$cenv{'num'},$comment); 
                    212: 	   }
                    213: 	}
                    214:     }
                    215:     return '';
                    216: }
1.1       www       217: ###################################################################
                    218: ###################################################################
                    219: sub handler {
                    220:     my $r=shift;
                    221:     if ($r->header_only) {
                    222:         $r->content_type('text/html');
                    223:         $r->send_http_header;
                    224:         return OK;
                    225:     }
1.2       www       226:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    227: 					    ['state','cid']);
                    228:     if (($ENV{'form.domain'}) && ($ENV{'form.course'})) {
                    229: 	$ENV{'form.cid'}=$ENV{'form.domain'}.'_'.$ENV{'form.course'};
                    230:     }
1.1       www       231: 
                    232:     unless (&Apache::lonnet::allowed('mky',$ENV{'request.role.domain'})) {
                    233:         $ENV{'user.error.msg'}=
                    234:             "/adm/managekeys:mky:0:0:Cannot manage access keys";
                    235:         return HTTP_NOT_ACCEPTABLE; 
                    236:     }
1.2       www       237:     if ($ENV{'form.cid'}) {
1.3       www       238:         my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
1.2       www       239: 	if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
1.3       www       240: #
                    241: # CSV Output
                    242: #
1.2       www       243: 	    $r->content_type('text/csv');
1.3       www       244: #
                    245: # Do CSV
                    246: #
1.2       www       247: 	} else {
1.3       www       248: #
                    249: # Normal web stuff
                    250: #
1.2       www       251: 	    $r->content_type('text/html');
                    252: 	    $r->send_http_header;
                    253: 	    $r->print(&header());
1.3       www       254: 	
                    255: 	    $r->print(
                    256: 	    '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
                    257: # --- Actions
                    258: 	    if ($ENV{'form.toggle'}) {
                    259: 		$r->print(&togglekeyaccess(%cenv).'<br />');
                    260: 		%cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
                    261: 	    }
1.5       www       262:             my $batchnumber='';
1.3       www       263: 	    if ($ENV{'form.genkeys'}) {
1.5       www       264: 		(my $msg,$batchnumber)=
                    265: 		    &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
                    266:                 $r->print($msg.'<br />');
1.3       www       267: 	    }
1.5       www       268:             if ($ENV{'form.listkeys'}) {
1.7       www       269: 		&show_key_list($r,$ENV{'form.listcom'},
                    270:                           $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
1.9       www       271:             }
                    272:             if ($ENV{'form.addcom'}) {
                    273: 		&addcom(%cenv);
1.5       www       274:             }
1.3       www       275: # --- Menu
                    276: 	    $r->print('<h3>Key Access</h3>');
                    277: 	    if ($cenv{'keyaccess'} eq 'yes') {
1.5       www       278: 		$r->print('Access to this course is key controlled.<br /><input type="submit" name="toggle" value="Open Access" />')
1.3       www       279: 		} else {
1.5       www       280: 		    $r->print('Access to this course is open, no access keys.<br /><input type="submit" name="toggle" value="Control Access" />');
1.2       www       281: 	    }
1.5       www       282: 	    $r->print(<<ENDKEYMENU);
1.3       www       283: <hr /><h3>Generate New Keys</h3>
                    284: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
                    285: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
                    286: <input type="submit" name="genkeys" value="Generate Keys" />
1.5       www       287: <hr /><h3>List Keys</h3>
1.7       www       288: Comments/Remarks/Notes/User/Batch Number:
1.5       www       289: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
1.7       www       290: <input type="checkbox" name="newonly" /> Unused keys only<br />
1.8       www       291: <input type="checkbox" name="checkonly" /> Used keys only<br />
1.5       www       292: <input type="submit" name="listkeys" value="List Keys" />
                    293: ENDKEYMENU
1.2       www       294: 	    $r->print('</form></body></html>');
                    295: 	}
1.1       www       296:     } else {
1.2       www       297: 	# Start page no course id
                    298: 	$r->content_type('text/html');
                    299: 	$r->send_http_header;
                    300: 	$r->print(&header().&Apache::loncommon::coursebrowser_javascript());
                    301:         $r->print(
                    302:    'Course ID: <input input type="text" size="25" name="course" value="" />');
                    303:         $r->print('Domain: '.&Apache::loncommon::select_dom_form(
                    304:                $ENV{'request.role.domain'},'domain'));
                    305:         $r->print(&Apache::loncommon::selectcourse_link(
                    306: 					        'keyform','course','domain'));
                    307:         $r->print('<br /><input type="submit" value="Manage Access Keys" />');
                    308: 	$r->print('</form></body></html>');
1.1       www       309:     }
                    310:     return OK;
                    311: }
                    312: 
                    313: ###################################################################
                    314: ###################################################################
                    315: 
                    316: 1;
                    317: __END__
                    318: 
                    319: 

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