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

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

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