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

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

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