File:
[LON-CAPA] /
loncom /
interface /
lonmanagekeys.pm
Revision
1.11:
download - view:
text,
annotated -
select for diffs
Fri May 2 19:00:29 2003 UTC (21 years, 3 months ago) by
www
Branches:
MAIN
CVS tags:
version_1_0_3,
version_1_0_2,
version_1_0_1,
version_1_0_0,
version_0_99_5,
version_0_99_4,
version_0_99_3,
version_0_99_2,
version_0_99_1,
version_0_99_0,
conference_2003,
HEAD
Looks like this works now.
1: # The LearningOnline Network with CAPA
2: # Handler to manage course access keys
3: #
4: # $Id: lonmanagekeys.pm,v 1.11 2003/05/02 19:00:29 www Exp $
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"
51: action="/adm/managekeys" name="keyform">
52: ENDHEAD
53: }
54:
55: # =================================================== Show student list to drop
56: sub show_key_list {
57: my ($r,$csvlist,$comment,$newonly,$checkonly,%cenv)=@_;
58: $comment=~s/\W/\./g;
59: my %accesskeys=&Apache::lonnet::dump
60: ('accesskeys',$cenv{'domain'},$cenv{'num'});
61: unless ($csvlist) {
62: $r->print(<<ENDTABLEHEADER);
63: <script>
64: function copyallcom(tf) {
65: for (i=0; i<tf.elements.length; i++) {
66: if (tf.elements[i].name.indexOf('com_')==0) {
67: tf.elements[i].value+=tf.copyall.value;
68: }
69: }
70:
71: }
72: </script>
73: <h3>List of Keys/Enter New Comments</h3>
74: <table border="2"><tr><th>Key</th><th>Checked Out</th>
75: <th>Comments/Remarks/Notes</th>
76: <th>Enter Additional Comments/Remarks/Notes<br />
77: <input type="text" size="40" name="copyall" />
78: <input type="button" value="Copy to All" onClick="copyallcom(this.form);" />
79: </th></tr>
80: ENDTABLEHEADER
81: }
82: foreach (keys %accesskeys) {
83: if ($_=~/^error\:/) {
84: $r->print('<tr><td>No keys have been generated yet.</td></tr>');
85: } elsif ($accesskeys{$_}=~/$comment/) {
86: my ($checkout,$com)=split(/\s*\#\s*/,$accesskeys{$_});
87: unless ($checkout) {
88: if ($checkonly) { next; }
89: } else {
90: if ($newonly) { next; }
91: }
92: unless ($csvlist) {
93: $r->print("\n<tr><td><tt>".$_.'</tt></td><td>'.($checkout?
94: $checkout:'-').'</td><td>'.
95: join('<br />',split(/\s*\;\s*/,$com)).
96: '</td><td><input type="text" size="40" name="com_'.$_.
97: '" value="" /></td></tr>');
98: } else {
99: my @line = ();
100: push @line,&Apache::loncommon::csv_translate($_);
101: push @line,&Apache::loncommon::csv_translate($checkout);
102: foreach (split(/\s*\;\s*/,$com)) {
103: push @line,&Apache::loncommon::csv_translate($_);
104: }
105: my $tmp = $";
106: $" = '","';
107: $r->print("\"@line\"\n");
108: $" = $tmp;
109: }
110: }
111: }
112: unless ($csvlist) {
113: $r->print('</table>');
114: $r->print('<input type="submit" name="addcom" value="Add Above Comments to Keys" /><hr />');
115: }
116: return '';
117: }
118:
119:
120: # ----------------------------------------------------------- Toggle Key Access
121:
122: sub togglekeyaccess {
123: my %cenv=@_;
124: unless ($cenv{'domain'}) { return; }
125: if ($cenv{'keyaccess'} eq 'yes') {
126: return 'Removing key access: '.
127: &Apache::lonnet::del('environment',['keyaccess'],
128: $cenv{'domain'},$cenv{'num'});
129: } else {
130: return 'Establishing key access: '.
131: &Apache::lonnet::put('environment',{'keyaccess' => 'yes'},
132: $cenv{'domain'},$cenv{'num'});
133: }
134: }
135:
136: # --------------------------------------------------------------- Generate Keys
137:
138: sub genkeys {
139: my ($num,$comments,%cenv)=@_;
140: unless ($comments) { $comments=''; }
141: $comments=~s/\#/ /g;
142: $comments=~s/\;/ /g;
143: unless ($num) { return 'No number of keys given.'; }
144: unless (($num=~/^\d+$/) && ($num>0)) {
145: return 'Invalid number of keys given.';
146: }
147: my $batchnumber='BATCH_'.time().'_'.$$;
148: return 'Generated '.&Apache::lonnet::generate_access_keys
149: ($num,$cenv{'domain'},$cenv{'num'},$batchnumber.'; '.$comments).' of '.
150: $num.' access keys (Batch Number: '.$batchnumber.')',$batchnumber;
151: }
152:
153: # ---------------------------------------------------------------- Add comments
154:
155: sub addcom {
156: my %cenv=@_;
157: my %newcomment=();
158: undef %newcomment;
159: foreach (keys %ENV) {
160: if ($_=~/^form\.com\_(.+)$/) {
161: my $key=$1;
162: my $comment=$ENV{$_};
163: $comment=~s/^\s+//gs;
164: if ($comment) {
165: &Apache::lonnet::comment_access_key
166: ($key,$cenv{'domain'},$cenv{'num'},$comment);
167: }
168: }
169: }
170: return '';
171: }
172: ###################################################################
173: ###################################################################
174: sub handler {
175: my $r=shift;
176: if ($r->header_only) {
177: $r->content_type('text/html');
178: $r->send_http_header;
179: return OK;
180: }
181: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
182: ['state','cid']);
183: if (($ENV{'form.domain'}) && ($ENV{'form.course'})) {
184: $ENV{'form.cid'}=$ENV{'form.domain'}.'_'.$ENV{'form.course'};
185: }
186:
187: unless (&Apache::lonnet::allowed('mky',$ENV{'request.role.domain'})) {
188: $ENV{'user.error.msg'}=
189: "/adm/managekeys:mky:0:0:Cannot manage access keys";
190: return HTTP_NOT_ACCEPTABLE;
191: }
192: if ($ENV{'form.cid'}) {
193: my %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
194: if ($ENV{'form.listkeyscsv'}) {
195: #
196: # CSV Output
197: #
198: $r->content_type('text/csv');
199: $r->send_http_header;
200: #
201: # Do CSV
202: #
203: &show_key_list($r,1,$ENV{'form.listcom'},
204: $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
205:
206: } else {
207: #
208: # Normal web stuff
209: #
210: $r->content_type('text/html');
211: $r->send_http_header;
212: $r->print(&header());
213:
214: $r->print(
215: '<input type="hidden" name="cid" value="'.$ENV{'form.cid'}.'" />');
216: # --- Actions
217: if ($ENV{'form.toggle'}) {
218: $r->print(&togglekeyaccess(%cenv).'<br />');
219: %cenv=&Apache::lonnet::coursedescription($ENV{'form.cid'});
220: }
221: my $batchnumber='';
222: if ($ENV{'form.genkeys'}) {
223: (my $msg,$batchnumber)=
224: &genkeys($ENV{'form.num'},$ENV{'form.comments'},%cenv);
225: $r->print($msg.'<br />');
226: }
227: if ($ENV{'form.listkeys'}) {
228: &show_key_list($r,0,$ENV{'form.listcom'},
229: $ENV{'form.newonly'},$ENV{'form.checkonly'},%cenv);
230: }
231: if ($ENV{'form.addcom'}) {
232: &addcom(%cenv);
233: }
234: # --- Menu
235: $r->print('<h3>Key Access</h3>');
236: if ($cenv{'keyaccess'} eq 'yes') {
237: $r->print('Access to this course is key controlled.<br /><input type="submit" name="toggle" value="Open Access" />')
238: } else {
239: $r->print('Access to this course is open, no access keys.<br /><input type="submit" name="toggle" value="Control Access" />');
240: }
241: $r->print(<<ENDKEYMENU);
242: <hr /><h3>Generate New Keys</h3>
243: Number of keys to be generated: <input type="text" name="num" size="6" /><br />
244: Comments/Remarks/Notes: <input type="text" name="comments" size="30" /><br />
245: <input type="submit" name="genkeys" value="Generate Keys" />
246: <hr /><h3>List Keys</h3>
247: Comments/Remarks/Notes/User/Batch Number Filter:
248: <input type="text" name="listcom" size="30" value="$batchnumber" /><br />
249: <input type="checkbox" name="newonly" /> Unused keys only<br />
250: <input type="checkbox" name="checkonly" /> Used keys only<br />
251: <input type="submit" name="listkeys" value="List Keys/Add Comments" />
252: <input type="submit" name="listkeyscsv" value="CSV List of Keys" />
253: ENDKEYMENU
254: $r->print('</form></body></html>');
255: }
256: } else {
257: # Start page no course id
258: $r->content_type('text/html');
259: $r->send_http_header;
260: $r->print(&header().&Apache::loncommon::coursebrowser_javascript());
261: $r->print(
262: 'Course ID: <input input type="text" size="25" name="course" value="" />');
263: $r->print('Domain: '.&Apache::loncommon::select_dom_form(
264: $ENV{'request.role.domain'},'domain'));
265: $r->print(&Apache::loncommon::selectcourse_link(
266: 'keyform','course','domain'));
267: $r->print('<br /><input type="submit" value="Manage Access Keys" />');
268: $r->print('</form></body></html>');
269: }
270: return OK;
271: }
272:
273: ###################################################################
274: ###################################################################
275:
276: 1;
277: __END__
278:
279:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>