--- loncom/interface/lonmanagekeys.pm 2003/04/23 01:55:52 1.3 +++ loncom/interface/lonmanagekeys.pm 2006/03/16 22:42:58 1.18 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler to manage course access keys # -# $Id: lonmanagekeys.pm,v 1.3 2003/04/23 01:55:52 www Exp $ +# $Id: lonmanagekeys.pm,v 1.18 2006/03/16 22:42:58 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,22 +31,19 @@ package Apache::lonmanagekeys; use strict; -use Apache::lonnet(); +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'); + my $start_page=&Apache::loncommon::start_page('Access Key Management'); return(< - -LON-CAPA Access Key Management - -$bodytag +$start_page
ENDHEAD @@ -54,65 +51,66 @@ ENDHEAD # =================================================== Show student list to drop sub show_key_list { - my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_; -# -# Just junk so that this compiles -# - my ($username,$domain,$id,$name,$section,$status,@Sorted_Students); -# -# -# - my $cid=$ENV{'form.cid'}; - # - # Variables for excel output - my ($excel_workbook, $excel_sheet, $excel_filename,$row); - # - - # Print out header - if ($mode eq 'view') { - } elsif ($mode eq 'excel') { - # Create the excel spreadsheet - $excel_filename = '/prtspool/'. - $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'. - time.'_'.rand(1000000000).'.xls'; - $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'. - $excel_filename); - $excel_workbook->set_tempdir('/home/httpd/perl/tmp'); - $excel_sheet = $excel_workbook->addworksheet('classlist'); - # - my $description = 'Classlist for '. - $ENV{'course.'.$ENV{'request.course.id'}.'.description'}; - $excel_sheet->write($row++,0,$description); - # - $excel_sheet->write($row++,0,["username","domain","ID", - "student name","section","status"]); - } - foreach my $student (@Sorted_Students) { - if ($mode eq 'view') { - } elsif ($mode eq 'csv') { - # no need to bother with $linkto - my @line = (); - foreach ($username,$domain,$id,$name,$section) { - push @line,&Apache::loncommon::csv_translate($_); + 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; iprint("\"@line\"\n"); - $" = $tmp; - } elsif ($mode eq 'excel') { - $excel_sheet->write($row++,0,[$username,$domain,$id, - $name,$section,$status]); } + } - if ($mode eq 'view') { - } elsif ($mode eq 'excel') { - $excel_workbook->close(); - $r->print('

'. - 'Your Excel spreadsheet is ready for download.

'."\n"); + +

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 OutComments/Remarks/NotesEnter Additional Comments/Remarks/Notes
+ + +
No keys have been generated yet.
".$_.''.($checkout? + $checkout:'-').''. + join('
',split(/\s*\;\s*/,$com)). + '
'); + $r->print('
'); + } + return ''; } @@ -136,91 +134,151 @@ sub togglekeyaccess { 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{'number'},$comments).' 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) { - $r->content_type('text/html'); + &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'}; + 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'}= + 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'}); - if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) { + if ($env{'form.cid'}) { + my %cenv=&Apache::lonnet::coursedescription($env{'form.cid'}); + my $keytype=''; + if ($cenv{'url'} eq '/res/') { + ($cenv{'domain'},$cenv{'num'})=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 # - $r->content_type('text/html'); + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; $r->print(&header()); $r->print( - ''); + ''); # --- Actions - if ($ENV{'form.toggle'}) { + if ($env{'form.toggle'}) { $r->print(&togglekeyaccess(%cenv).'
'); - %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'}); + %cenv=&Apache::lonnet::coursedescription($env{'form.cid'}); } - if ($ENV{'form.genkeys'}) { - $r->print( - &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv).'
'); + 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 - $r->print('

Key Access

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

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

'); } - $r->print(<print(<

Generate New Keys

Number of keys to be generated:
Comments/Remarks/Notes:
-ENDKEYGEN - $r->print(''); +

List Keys

+Comments/Remarks/Notes/User/Batch Number Filter: +
+ Unused keys only
+ Used keys only
+ + +ENDKEYMENU + $r->print(''.&Apache::loncommon::end_page()); } } else { # Start page no course id - $r->content_type('text/html'); + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; $r->print(&header().&Apache::loncommon::coursebrowser_javascript()); $r->print( - 'Course ID: '); - $r->print('Domain: '.&Apache::loncommon::select_dom_form( - $ENV{'request.role.domain'},'domain')); + &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(''); + $r->print('
'); + $r->print(''.&Apache::loncommon::end_page()); } return OK; }