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

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

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