--- loncom/interface/lonmanagekeys.pm 2003/04/12 15:57:30 1.1 +++ loncom/interface/lonmanagekeys.pm 2003/04/23 20:29:17 1.5 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler to manage course access keys # -# $Id: lonmanagekeys.pm,v 1.1 2003/04/12 15:57:30 www Exp $ +# $Id: lonmanagekeys.pm,v 1.5 2003/04/23 20:29:17 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -48,7 +48,7 @@ sub header { $bodytag
+ action="/adm/managekeys" name="keyform"> ENDHEAD } @@ -62,7 +62,7 @@ sub show_key_list { # # # - my $cid=$ENV{'request.course.id'}; + my $cid=$ENV{'form.cid'}; # # Variables for excel output my ($excel_workbook, $excel_sheet, $excel_filename,$row); @@ -111,10 +111,44 @@ sub show_key_list { } elsif ($mode eq 'excel') { $excel_workbook->close(); $r->print('

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

'."\n"); + 'Your Excel spreadsheet is ready for download.

'."\n"); } } + +# ----------------------------------------------------------- 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; +} + ################################################################### ################################################################### sub handler { @@ -124,32 +158,83 @@ sub handler { $r->send_http_header; return OK; } - &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['state']); + &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; } - # - # Only output the header information if they did not request csv format - # - if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) { - $r->content_type('text/csv'); - } else { - # Start page - $r->content_type('text/html'); - $r->send_http_header; - $r->print(&header()); - } -# -# do stuff here. -# - - if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) { - $r->print("\n"); + if ($ENV{'form.cid'}) { + my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'}); + if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) { +# +# CSV Output +# + $r->content_type('text/csv'); +# +# Do CSV +# + } else { +# +# Normal web stuff +# + $r->content_type('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($ENV{'form.listcom'},%cenv); + } +# --- Menu + $r->print('

Key Access

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

Generate New Keys

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

List Keys

+Comments/Remarks/Notes/Batch Number: +
+ +ENDKEYMENU + $r->print(''); + } } else { - $r->print(''); + # Start page no course id + $r->content_type('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')); + $r->print(&Apache::loncommon::selectcourse_link( + 'keyform','course','domain')); + $r->print('
'); + $r->print(''); } return OK; }