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

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