# The LearningOnline Network with CAPA # Handler to manage course access keys # # $Id: lonmanagekeys.pm,v 1.14 2004/05/08 02:07:16 www Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # ############################################################### ############################################################### package Apache::lonmanagekeys; use strict; use Apache::lonnet(); use Apache::loncommon(); use Apache::lonhtmlcommon(); use Apache::Constants qw(:common :http REDIRECT); use Spreadsheet::WriteExcel; use Apache::lonlocal; ############################################################### ############################################################### sub header { my $bodytag=&Apache::loncommon::bodytag('Access Key Management'); return(< LON-CAPA Access Key Management $bodytag
ENDHEAD } # =================================================== Show student list to drop sub show_key_list { my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_; $comment=~s/\W/\./g; my %accesskeys=&Apache::lonnet::dump ('accesskeys',$cenv{'domain'},$cenv{'num'}); unless ($csvlist) { $r->print(< function copyallcom(tf) { for (i=0; i

List of Keys/Enter New Comments

ENDTABLEHEADER } foreach (keys %accesskeys) { if ($_=~/^error\:/) { $r->print(''); } elsif ($accesskeys{$_}=~/$comment/) { my ($checkout,$com)=split(/\s*\#\s*/,$accesskeys{$_}); unless ($checkout) { if ($checkonly) { next; } } else { if ($newonly) { next; } } unless ($csvlist) { $r->print("\n'); } else { my @line = (); push @line,&Apache::loncommon::csv_translate($_); push @line,&Apache::loncommon::csv_translate($checkout); foreach (split(/\s*\;\s*/,$com)) { push @line,&Apache::loncommon::csv_translate($_); } my $tmp = $"; $" = '","'; $r->print("\"@line\"\n"); $" = $tmp; } } } unless ($csvlist) { $r->print('
KeyChecked Out Comments/Remarks/Notes Enter Additional Comments/Remarks/Notes
No keys have been generated yet.
".$_.''.($checkout? $checkout:'-').''. join('
',split(/\s*\;\s*/,$com)). '
'); $r->print('
'); } return ''; } # ----------------------------------------------------------- Toggle Key Access sub togglekeyaccess { my %cenv=@_; unless ($cenv{'domain'}) { return; } if ($cenv{'keyaccess'} eq 'yes') { return 'Removing key access: '. &Apache::lonnet::del('environment',['keyaccess'], $cenv{'domain'},$cenv{'num'}); } else { return 'Establishing key access: '. &Apache::lonnet::put('environment',{'keyaccess' => 'yes'}, $cenv{'domain'},$cenv{'num'}); } } # --------------------------------------------------------------- Generate Keys sub genkeys { my ($num,$comments,%cenv)=@_; unless ($comments) { $comments=''; } $comments=~s/\#/ /g; $comments=~s/\;/ /g; unless ($num) { return 'No number of keys given.'; } unless (($num=~/^\d+$/) && ($num>0)) { return 'Invalid number of keys given.'; } my $batchnumber='BATCH_'.time().'_'.$$; return 'Generated '.&Apache::lonnet::generate_access_keys ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '. $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber; } # ---------------------------------------------------------------- Add comments sub addcom { my %cenv=@_; my %newcomment=(); undef %newcomment; foreach (keys %ENV) { if ($_=~/^form\.com\_(.+)$/) { my $key=$1; my $comment=$ENV{$_}; $comment=~s/^\s+//gs; if ($comment) { &Apache::lonnet::comment_access_key ($key,$cenv{'domain'},$cenv{'num'},$comment); } } } return ''; } ################################################################### ################################################################### sub handler { my $r=shift; if ($r->header_only) { &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK; } &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['state','cid']); if (($ENV{'form.domain'}) && ($ENV{'form.course'})) { $ENV{'form.cid'}=$ENV{'form.domain'}.'_'.$ENV{'form.course'}; } unless (&Apache::lonnet::allowed('mky',$ENV{'request.role.domain'})) { $ENV{'user.error.msg'}= "/adm/managekeys:mky:0:0:Cannot manage access keys"; return HTTP_NOT_ACCEPTABLE; } if ($ENV{'form.cid'}) { my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'}); my $keytype=''; if ($cenv{'url'} eq '/res/') { ($cenv{'num'},$cenv{'domain'})=split(/\_/,$ENV{'form.cid'}); $keytype='auth'; } elsif ($cenv{'keyauth'}) { ($cenv{'num'},$cenv{'domain'})=split(/\W/,$cenv{'keyauth'}); $keytype='auth'; } else { $keytype='course'; } if ($ENV{'form.listkeyscsv'}) { # # CSV Output # $r->content_type('text/csv'); $r->send_http_header; # # Do CSV # &show_key_list($r,1,$ENV{'form.listcom'}, $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv); } else { # # Normal web stuff # &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; $r->print(&header()); $r->print( ''); # --- Actions if ($ENV{'form.toggle'}) { $r->print(&togglekeyaccess(%cenv).'
'); %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'}); } my $batchnumber=''; if ($ENV{'form.genkeys'}) { (my $msg,$batchnumber)= &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv); $r->print($msg.'
'); } if ($ENV{'form.listkeys'}) { &show_key_list($r,0,$ENV{'form.listcom'}, $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv); } if ($ENV{'form.addcom'}) { &addcom(%cenv); } # --- Menu if ($keytype eq 'course') { $r->print('

'.&mt('Key Access').'

'); if ($cenv{'keyaccess'} eq 'yes') { $r->print(&mt('Access to this course is key controlled.'). '
') } else { $r->print(&mt('Access to this course is open, no access keys').'
'); } } else { $r->print('

'.&mt('Key Authority'). ' '.$cenv{'num'}.'@'.$cenv{'domain'}.'

'); } $r->print(<

Generate New Keys

Number of keys to be generated:
Comments/Remarks/Notes:

List Keys

Comments/Remarks/Notes/User/Batch Number Filter:
Unused keys only
Used keys only
ENDKEYMENU $r->print(''); } } else { # Start page no course id &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; $r->print(&header().&Apache::loncommon::coursebrowser_javascript()); $r->print( &mt('Course ID of Key Authority').': '); $r->print(&mt('Domain').': '.&Apache::loncommon::select_dom_form( $ENV{'request.role.domain'},'domain')); $r->print(&Apache::loncommon::selectcourse_link( 'keyform','course','domain')); $r->print('
'); $r->print(''); } return OK; } ################################################################### ################################################################### 1; __END__