File:
[LON-CAPA] /
loncom /
interface /
Attic /
londropadd.pm
Revision
1.68:
download - view:
text,
annotated -
select for diffs
Fri Jun 20 20:57:44 2003 UTC (21 years, 1 month ago) by
matthew
Branches:
MAIN
CVS tags:
HEAD
Bug 844:
All enrollment pages (classlist upload, enroll single student, and
modify single student) now use the new date_setter which lets you set the
date right in the page.
The date setters respect the course default start and end dates.
An instructor can set the dates he/she/it chooses as the default for future
enrollments.
Added a 'no ending date' checkbox so the person enrolling can hit it instead
of setting the date to Dec 31st, 1969, 7pm or whatever the unixtime start
date is for their time zone.
1: # The LearningOnline Network with CAPA
2: # Handler to drop and add students in courses
3: #
4: # $Id: londropadd.pm,v 1.68 2003/06/20 20:57:44 matthew 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: # (Handler to set parameters for assessments
29: #
30: # (Handler to resolve ambiguous file locations
31: #
32: # (TeX Content Handler
33: #
34: ###############################################################
35: ###############################################################
36:
37: package Apache::londropadd;
38:
39: use strict;
40: use Apache::lonnet();
41: use Apache::loncommon();
42: use Apache::lonhtmlcommon();
43: use Apache::Constants qw(:common :http REDIRECT);
44: use Spreadsheet::WriteExcel;
45:
46: ###############################################################
47: ###############################################################
48: sub header {
49: my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
50: return(<<ENDHEAD);
51: <html>
52: <head>
53: <title>LON-CAPA Enrollment Manager</title>
54: </head>
55: $bodytag
56: <form method="post" enctype="multipart/form-data"
57: action="/adm/dropadd" name="studentform">
58: ENDHEAD
59: }
60:
61: ###############################################################
62: ###############################################################
63: # Drop student from all sections of a course, except optional $csec
64: sub modifystudent {
65: my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
66: # if $csec is undefined, drop the student from all the courses matching
67: # this one. If $csec is defined, drop them from all other sections of
68: # this course and add them to section $csec
69: $courseid=~s/\_/\//g;
70: $courseid=~s/^(\w)/\/$1/;
71: my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
72: my ($tmp) = keys(%roles);
73: # Bail out if we were unable to get the students roles
74: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
75: # Go through the roles looking for enrollment in this course
76: my $result = '';
77: foreach my $course (keys(%roles)) {
78: if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
79: # We are in this course
80: my $section=$1;
81: $section='' if ($course eq $courseid.'_st');
82: if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
83: my (undef,$end,$start)=split(/\_/,$roles{$course});
84: my $now=time;
85: # if this is an active role
86: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
87: my $reply=&Apache::lonnet::modifystudent
88: ($udom,$unam,'','','','','','','',
89: $section,time,undef,undef,$desiredhost);
90: $result .= $reply.':';
91: }
92: }
93: }
94: }
95: if ($result eq '') {
96: $result = 'Unable to find section for this student';
97: } else {
98: $result =~ s/(ok:)+/ok/g;
99: }
100: return $result;
101: }
102:
103: ###############################################################
104: ###############################################################
105: # build a domain and server selection form
106: sub domain_form {
107: my ($defdom) = @_;
108: # Set up domain and server selection forms
109: #
110: # Get the domains
111: my @domains = &Apache::loncommon::get_domains();
112: # build up the menu information to be passed to
113: # &Apache::loncommon::linked_select_forms
114: my %select_menus;
115: foreach my $dom (@domains) {
116: # set up the text for this domain
117: $select_menus{$dom}->{'text'}= $dom;
118: # we want a choice of 'default' as the default in the second menu
119: $select_menus{$dom}->{'default'}= 'default';
120: $select_menus{$dom}->{'select2'}->{'default'} = 'default';
121: # Now build up the other items in the second menu
122: my %servers = &Apache::loncommon::get_library_servers($dom);
123: foreach my $server (keys(%servers)) {
124: $select_menus{$dom}->{'select2'}->{$server}
125: = "$server $servers{$server}";
126: }
127: }
128: my $result = &Apache::loncommon::linked_select_forms
129: ('studentform',' with home server ',$defdom,
130: 'lcdomain','lcserver',\%select_menus);
131: return $result;
132: }
133:
134: ###############################################################
135: ###############################################################
136: # Menu Phase One
137: sub print_main_menu {
138: my $r=shift;
139: $r->print(<<END);
140: <p>
141: <font size="+1">
142: <a href="/adm/dropadd?action=upload">Upload a course list</a>
143: </font>
144: </p><p>
145: <font size="+1">
146: <a href="/adm/dropadd?action=enrollstudent">Enroll a single student</a>
147: </font>
148: </p><p>
149: <font size="+1">
150: <a href="/adm/dropadd?action=modifystudent">Modify student data</a>
151: </font>
152: </p><p>
153: <font size="+1">
154: <a href="/adm/dropadd?action=classlist">View Classlist</a>
155: </font>
156: </p><p>
157: <font size="+1">
158: <a href="/adm/dropadd?action=drop">Drop Students</a>
159: </font>
160: </p>
161: END
162: }
163:
164: ###############################################################
165: ###############################################################
166: sub print_upload_manager_header {
167: my ($r,$datatoken,$distotal,$krbdefdom)=@_;
168: my $javascript;
169: if (! exists($ENV{'form.upfile_associate'})) {
170: $ENV{'form.upfile_associate'} = 'forward';
171: }
172: if ($ENV{'form.associate'} eq 'Reverse Association') {
173: if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
174: $ENV{'form.upfile_associate'} = 'reverse';
175: } else {
176: $ENV{'form.upfile_associate'} = 'forward';
177: }
178: }
179: if ($ENV{'form.upfile_associate'} eq 'reverse') {
180: $javascript=&upload_manager_javascript_reverse_associate();
181: } else {
182: $javascript=&upload_manager_javascript_forward_associate();
183: }
184: my $javascript_validations=&javascript_validations($krbdefdom);
185: $r->print(<<ENDPICK);
186: <h3>Uploading Class List</h3>
187: <hr>
188: <h3>Identify fields</h3>
189: Total number of records found in file: $distotal <hr />
190: Enter as many fields as you can. The system will inform you and bring you back
191: to this page if the data selected is insufficient to run your class.<hr />
192: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
193: <input type="hidden" name="action" value="upload" />
194: <input type="hidden" name="state" value="got_file" />
195: <input type="hidden" name="associate" value="" />
196: <input type="hidden" name="datatoken" value="$datatoken" />
197: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
198: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
199: <input type="hidden" name="upfile_associate"
200: value="$ENV{'form.upfile_associate'}" />
201: <hr />
202: <script type="text/javascript" language="Javascript">
203: $javascript
204: $javascript_validations
205: </script>
206: ENDPICK
207: }
208:
209: ###############################################################
210: ###############################################################
211: sub javascript_validations {
212: my ($krbdefdom)=@_;
213: my %param = ( formname => 'studentform',
214: kerb_def_dom => $krbdefdom );
215: my $authheader = &Apache::loncommon::authform_header(%param);
216: my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
217: return (<<ENDPICK);
218: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
219: var foundatype=0;
220: var message='';
221: if (founduname==0) {
222: alert('You need to specify the username field');
223: return;
224: }
225: // alert('current.radiovalue = '+current.radiovalue);
226: if (current.radiovalue == null || current.radiovalue == 'nochange') {
227: // They did not check any of the login radiobuttons.
228: alert('You must choose an authentication type');
229: return;
230: }
231: foundatype=1;
232: if (current.argfield == null || current.argfield == '') {
233: var alertmsg = '';
234: switch (current.value) {
235: case 'krb':
236: alertmsg = 'You need to specify the Kerberos domain';
237: break;
238: case 'loc':
239: case 'fsys':
240: alertmsg = 'You need to specify the initial password';
241: break;
242: case 'fsys':
243: alertmsg = '';
244: break;
245: default:
246: alertmsg = '';
247: }
248: if (alertmsg != '') {
249: alert(alertmsg);
250: return;
251: }
252: }
253:
254: if (foundname==0) { message='No name fields specified. '; }
255: if (foundid==0) { message+='No ID or student number field specified. '; }
256: if (foundsec==0) { message+='No section or group field specified. '; }
257: if (message!='') {
258: message+='Continue enrollment?';
259: if (confirm(message)) {
260: vf.state.value='enrolling';
261: vf.submit();
262: }
263: } else {
264: vf.state.value='enrolling';
265: vf.submit();
266: }
267: }
268:
269: $authheader
270: ENDPICK
271:
272: }
273:
274: ###############################################################
275: ###############################################################
276: sub upload_manager_javascript_forward_associate {
277: return(<<ENDPICK);
278: function verify(vf) {
279: var founduname=0;
280: var foundpwd=0;
281: var foundname=0;
282: var foundid=0;
283: var foundsec=0;
284: var tw;
285: for (i=0;i<=vf.nfields.value;i++) {
286: tw=eval('vf.f'+i+'.selectedIndex');
287: if (tw==1) { founduname=1; }
288: if ((tw>=2) && (tw<=6)) { foundname=1; }
289: if (tw==7) { foundid=1; }
290: if (tw==8) { foundsec=1; }
291: if (tw==9) { foundpwd=1; }
292: }
293: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
294: }
295:
296: //
297: // vf = this.form
298: // tf = column number
299: //
300: // values of nw
301: //
302: // 0 = none
303: // 1 = username
304: // 2 = names (lastname, firstnames)
305: // 3 = fname (firstname)
306: // 4 = mname (middlename)
307: // 5 = lname (lastname)
308: // 6 = gen (generation)
309: // 7 = id
310: // 8 = section
311: // 9 = ipwd (password)
312: //
313: function flip(vf,tf) {
314: var nw=eval('vf.f'+tf+'.selectedIndex');
315: var i;
316: // make sure no other columns are labeled the same as this one
317: for (i=0;i<=vf.nfields.value;i++) {
318: if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
319: eval('vf.f'+i+'.selectedIndex=0;')
320: }
321: }
322: // If we set this to 'lastname, firstnames', clear out all the ones
323: // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
324: if (nw==2) {
325: for (i=0;i<=vf.nfields.value;i++) {
326: if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
327: (eval('vf.f'+i+'.selectedIndex')<=6)) {
328: eval('vf.f'+i+'.selectedIndex=0;')
329: }
330: }
331: }
332: // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
333: // clear out any that are set to 'lastname, firstnames' (2)
334: if ((nw>=3) && (nw<=6)) {
335: for (i=0;i<=vf.nfields.value;i++) {
336: if (eval('vf.f'+i+'.selectedIndex')==2) {
337: eval('vf.f'+i+'.selectedIndex=0;')
338: }
339: }
340: }
341: // If we set the password, make the password form below correspond to
342: // the new value.
343: if (nw==9) {
344: changed_radio('int',document.studentform);
345: set_auth_radio_buttons('int',document.studentform);
346: vf.intarg.value='';
347: vf.krbarg.value='';
348: vf.locarg.value='';
349: }
350: }
351:
352: function clearpwd(vf) {
353: var i;
354: for (i=0;i<=vf.nfields.value;i++) {
355: if (eval('vf.f'+i+'.selectedIndex')==9) {
356: eval('vf.f'+i+'.selectedIndex=0;')
357: }
358: }
359: }
360:
361: ENDPICK
362: }
363:
364: ###############################################################
365: ###############################################################
366: sub upload_manager_javascript_reverse_associate {
367: return(<<ENDPICK);
368: function verify(vf) {
369: var founduname=0;
370: var foundpwd=0;
371: var foundname=0;
372: var foundid=0;
373: var foundsec=0;
374: var tw;
375: for (i=0;i<=vf.nfields.value;i++) {
376: tw=eval('vf.f'+i+'.selectedIndex');
377: if (i==0 && tw!=0) { founduname=1; }
378: if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
379: if (i==6 && tw!=0) { foundid=1; }
380: if (i==7 && tw!=0) { foundsec=1; }
381: if (i==8 && tw!=0) { foundpwd=1; }
382: }
383: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
384: }
385:
386: function flip(vf,tf) {
387: var nw=eval('vf.f'+tf+'.selectedIndex');
388: var i;
389: // picked the all one one name field, reset the other name ones to blank
390: if (tf==1 && nw!=0) {
391: for (i=2;i<=5;i++) {
392: eval('vf.f'+i+'.selectedIndex=0;')
393: }
394: }
395: //picked one of the piecewise name fields, reset the all in
396: //one field to blank
397: if ((tf>=2) && (tf<=5) && (nw!=0)) {
398: eval('vf.f1.selectedIndex=0;')
399: }
400: // intial password specified, pick internal authentication
401: if (tf==8 && nw!=0) {
402: changed_radio('int',document.studentform);
403: set_auth_radio_buttons('int',document.studentform);
404: vf.krbarg.value='';
405: vf.intarg.value='';
406: vf.locarg.value='';
407: }
408: }
409:
410: function clearpwd(vf) {
411: var i;
412: if (eval('vf.f8.selectedIndex')!=0) {
413: eval('vf.f8.selectedIndex=0;')
414: }
415: }
416: ENDPICK
417: }
418:
419: ###############################################################
420: ###############################################################
421: sub print_upload_manager_footer {
422: my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
423:
424: my ($krbdef,$krbdefdom) =
425: &Apache::loncommon::get_kerberos_defaults($defdom);
426: my %param = ( formname => 'document.studentform',
427: kerb_def_dom => $krbdefdom,
428: kerb_def_auth => $krbdef
429: );
430: my $krbform = &Apache::loncommon::authform_kerberos(%param);
431: my $intform = &Apache::loncommon::authform_internal(%param);
432: my $locform = &Apache::loncommon::authform_local(%param);
433: my $domform = &domain_form($defdom);
434: my $date_table = &date_setting_table();
435: $r->print(<<ENDPICK);
436: </table>
437: <input type=hidden name=nfields value=$i>
438: <input type=hidden name=keyfields value="$keyfields">
439: <h3>Login Type</h3>
440: <p>Note: this will not take effect if the user already exists</p>
441: <p>
442: $krbform
443: </p>
444: <p>
445: $intform
446: </p>
447: <p>
448: $locform
449: </p>
450: <h3>LON-CAPA Domain for Students</h3>
451: LON-CAPA domain: $domform <p>
452: <h3>Starting and Ending Dates</h3>
453: <p>
454: $date_table
455: </p>
456: <h3>Full Update</h3>
457: <input type=checkbox name=fullup value=yes> Full update
458: (also print list of users not enrolled anymore)<p>
459: <h3>ID/Student Number</h3>
460: <input type=checkbox name=forceid value=yes>
461: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
462: (only do if you know what you are doing)<p>
463: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
464: Note: for large courses, this operation may be time consuming.
465: ENDPICK
466: }
467:
468: # ======================================================= Menu Phase Two Upload
469: sub print_upload_manager_form {
470: my $r=shift;
471:
472: my $datatoken;
473: if (!$ENV{'form.datatoken'}) {
474: $datatoken=&Apache::loncommon::upfile_store($r);
475: } else {
476: $datatoken=$ENV{'form.datatoken'};
477: &Apache::loncommon::load_tmp_file($r);
478: }
479: my @records=&Apache::loncommon::upfile_record_sep();
480: my $total=$#records;
481: my $distotal=$total+1;
482: my $today=time;
483: my $halfyear=$today+15552000;
484: my $defdom=$r->dir_config('lonDefDomain');
485: my ($krbdef,$krbdefdom) =
486: &Apache::loncommon::get_kerberos_defaults($defdom);
487: &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
488: my $i;
489: my $keyfields;
490: if ($total>=0) {
491: my @d=(['username','Username'],
492: ['names','Last Name, First Names'],
493: ['fname','First Name'],
494: ['mname','Middle Names/Initials'],
495: ['lname','Last Name'],
496: ['gen','Generation'],
497: ['id','ID/Student Number'],
498: ['sec','Group/Section'],
499: ['ipwd','Initial Password']);
500: if ($ENV{'form.upfile_associate'} eq 'reverse') {
501: &Apache::loncommon::csv_print_samples($r,\@records);
502: $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
503: foreach (@d) { $keyfields.=$_->[0].','; }
504: chop($keyfields);
505: } else {
506: unshift(@d,['none','']);
507: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
508: my %sone=&Apache::loncommon::record_sep($records[0]);
509: $keyfields=join(',',sort(keys(%sone)));
510: }
511: }
512: &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
513: }
514:
515: # ======================================================= Enroll single student
516: sub enroll_single_student {
517: my $r=shift;
518: #
519: # We do the dates first because the action of making them the defaul
520: # in the course is entirely seperate from the action of enrolling the
521: # student. Also, a failure in setting the dates as default is not fatal
522: # to the process of enrolling / modifying a student.
523: my ($startdate,$enddate) = &get_dates_from_form();
524: if ($ENV{'form.makedatesdefault'}) {
525: $r->print(&make_dates_default($startdate,$enddate));
526: }
527:
528: $r->print('<h3>Enrolling Student</h3>');
529: $r->print('<p>Enrolling '.$ENV{'form.cuname'}." \@ ".
530: $ENV{'form.lcdomain'}.'</p>');
531: if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
532: ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
533: # Deal with home server selection
534: my $domain=$ENV{'form.lcdomain'};
535: my $desiredhost = $ENV{'form.lcserver'};
536: if (lc($desiredhost) eq 'default') {
537: $desiredhost = undef;
538: } else {
539: my %home_servers =&Apache::loncommon::get_library_servers($domain);
540: if (! exists($home_servers{$desiredhost})) {
541: $r->print('<font color="#ff0000">Error:</font>'.
542: 'Invalid home server specified');
543: return;
544: }
545: }
546: $r->print(" with server $desiredhost :") if (defined($desiredhost));
547: # End of home server selection logic
548: my $amode='';
549: my $genpwd='';
550: if ($ENV{'form.login'} eq 'krb') {
551: $amode='krb';
552: $amode.=$ENV{'form.krbver'};
553: $genpwd=$ENV{'form.krbarg'};
554: } elsif ($ENV{'form.login'} eq 'int') {
555: $amode='internal';
556: $genpwd=$ENV{'form.intarg'};
557: } elsif ($ENV{'form.login'} eq 'loc') {
558: $amode='localauth';
559: $genpwd=$ENV{'form.locarg'};
560: if (!$genpwd) { $genpwd=" "; }
561: }
562: my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
563: $ENV{'form.lcdomain'});
564: if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
565: # Clean out any old roles the student has in this class.
566: &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
567: $ENV{'request.course.id'},$ENV{'form.csec'},
568: $desiredhost);
569: my $login_result = &Apache::lonnet::modifystudent
570: ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
571: $ENV{'form.cstid'},$amode,$genpwd,
572: $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
573: $ENV{'form.clast'},$ENV{'form.cgen'},
574: $ENV{'form.csec'},$enddate,
575: $startdate,$ENV{'form.forceid'},
576: $desiredhost);
577: if ($login_result =~ /^ok/) {
578: $r->print($login_result);
579: $r->print("<p> If active, the new role will be available ".
580: "when the student next logs in to LON-CAPA.</p>");
581: } else {
582: $r->print("unable to enroll: ".$login_result);
583: }
584: } else {
585: $r->print('<p><font color="#ff0000">ERROR</font> '.
586: 'Invalid login mode or password. '.
587: 'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
588: }
589: } else {
590: $r->print('Invalid username or domain');
591: }
592: }
593:
594: sub setup_date_selectors {
595: my ($starttime,$endtime) = @_;
596: if (! defined($starttime)) {
597: $starttime = time;
598: if (exists($ENV{'course.'.$ENV{'request.course.id'}.
599: '.default_enrollment_start_date'})) {
600: $starttime = $ENV{'course.'.$ENV{'request.course.id'}.
601: '.default_enrollment_start_date'};
602: }
603: }
604: if (! defined($endtime)) {
605: $endtime = time+(6*30*24*60*60); # 6 months from now, approx
606: if (exists($ENV{'course.'.$ENV{'request.course.id'}.
607: '.default_enrollment_end_date'})) {
608: $endtime = $ENV{'course.'.$ENV{'request.course.id'}.
609: '.default_enrollment_end_date'};
610: }
611: }
612: my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
613: 'startdate',
614: $starttime);
615: my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
616: 'enddate',
617: $endtime);
618: return ($startdateform,$enddateform);
619: }
620:
621: sub get_dates_from_form {
622: my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
623: my $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate');
624: if ($ENV{'form.no_end_date'}) {
625: $enddate = 0;
626: }
627: return ($startdate,$enddate);
628: }
629:
630: sub date_setting_table {
631: my ($starttime,$endtime) = @_;
632: my ($startform,$endform)=&setup_date_selectors($starttime,$endtime);
633: my $dateDefault = '<nobr>'.
634: '<input type="checkbox" name="makedatesdefault" />'.
635: ' make these dates the default for future enrollment';
636: my $perpetual = '<nobr><input type="checkbox" name="no_end_date"';
637: if (defined($endtime) && $endtime == 0) {
638: $perpetual .= ' checked';
639: }
640: $perpetual.= ' />'.' no ending date</nobr>';
641: my $result = '';
642: $result .= "<table>\n";
643: $result .= '<tr><td align="right">Starting Date</td>'.
644: '<td>'.$startform.'</td>'.
645: '<td>'.$dateDefault.'</td>'."</tr>\n";
646: $result .= '<tr><td align="right">Ending Date</td>'.
647: '<td>'.$endform.'</td>'.
648: '<td>'.$perpetual.'</td>'."</tr>\n";
649: $result .= "</table>\n";
650: return $result;
651: }
652:
653: sub make_dates_default {
654: my ($startdate,$enddate) = @_;
655: my $result = '';
656: my $dom = $ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
657: my $crs = $ENV{'course.'.$ENV{'request.course.id'}.'.num'};
658: my $put_result = &Apache::lonnet::put('environment',
659: {'default_enrollment_start_date'=>$startdate,
660: 'default_enrollment_end_date' =>$enddate},$dom,$crs);
661: if ($put_result eq 'ok') {
662: $result .= "Set default start and end dates for course<br />";
663: } else {
664: $result .= "Unable to set default dates for course:".$put_result.
665: '<br />';
666: }
667: return $result;
668: }
669:
670: # ======================================================= Menu Phase Two Enroll
671: sub print_enroll_single_student_form {
672: my $r=shift;
673: $r->print("<h3>Enroll One Student</h3>");
674: my $today = time;
675: my $halfyear = $today+15552000;
676: my $defdom=$r->dir_config('lonDefDomain');
677: # Set up authentication forms
678: my ($krbdef,$krbdefdom) =
679: &Apache::loncommon::get_kerberos_defaults($defdom);
680: my $javascript_validations=&javascript_validations($krbdefdom);
681: my %param = ( formname => 'document.studentform',
682: kerb_def_dom => $krbdefdom,
683: kerb_def_auth => $krbdef
684: );
685: my $krbform = &Apache::loncommon::authform_kerberos(%param);
686: my $intform = &Apache::loncommon::authform_internal(%param);
687: my $locform = &Apache::loncommon::authform_local(%param);
688: # Set up domain selection form
689: my $domform = &domain_form($defdom);
690: my $date_table = &date_setting_table();
691: # Print it all out
692: $r->print(<<END);
693: <input type="hidden" name="action" value="enrollstudent">
694: <input type="hidden" name="state" value="done">
695:
696: <script type="text/javascript" language="Javascript">
697: function verify(vf) {
698: var founduname=0;
699: var foundpwd=0;
700: var foundname=0;
701: var foundid=0;
702: var foundsec=0;
703: var tw;
704: if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') &&
705: (typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
706: founduname=1;
707: }
708: if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
709: (typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
710: foundname=1;
711: }
712: if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
713: foundsec=1;
714: }
715: if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
716: foundid=1;
717: }
718: if (founduname==0) {
719: alert('You need to specify at least the username and domain fields');
720: return;
721: }
722: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
723: }
724:
725: $javascript_validations
726:
727: function clearpwd(vf) {
728: //nothing else needs clearing
729: }
730:
731: </script>
732: <h3>Personal Data</h3>
733: <table>
734: <tr><td>First Name:</td><td> <input type="text" name="cfirst" size="15"></td></tr>
735: <tr><td>Middle Name:</td><td> <input type="text" name="cmiddle" size="15"></td></tr>
736: <tr><td>Last Name: </td><td><input type="text" name="clast" size="15"></td></tr>
737: <tr><td>Generation: </td><td><input type="text" name="cgen" size="5"> </td></tr>
738: </table>
739:
740: <h3>Login Data</h3>
741: <p>Username: <input type="text" name="cuname" size="15"></p>
742: <p>Domain: $domform</p>
743: <p>Note: login settings below will not take effect if the user already exists
744: </p><p>
745: $krbform
746: </p><p>
747: $intform
748: </p><p>
749: $locform
750: </p><p>
751:
752: <h3>Course Data</h3>
753:
754: <p>Group/Section: <input type="text" name="csec" size="5" />
755: <p>
756: $date_table
757: </p>
758: <h3>ID/Student Number</h3>
759: <p>
760: ID/Student Number: <input type="text" name="cstid" size="10">
761: </p><p>
762: <input type="checkbox" name="forceid" value="yes">
763: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
764: (only do if you know what you are doing)
765: </p><p>
766: <input type="button" onClick="verify(this.form)" value="Enroll as student">
767: </p>
768: END
769: return;
770: }
771:
772: # ========================================================= Menu Phase Two Drop
773: sub print_drop_menu {
774: my $r=shift;
775: $r->print("<h3>Drop Students</h3>");
776: my $cid=$ENV{'request.course.id'};
777: my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
778: if (! defined($classlist)) {
779: $r->print("There are no students currently enrolled.\n");
780: return;
781: }
782: # Print out the available choices
783: &show_drop_list($r,$classlist,$keylist);
784: return;
785: }
786:
787: # ============================================== view classlist
788: sub print_html_classlist {
789: my $r=shift;
790: if (! exists($ENV{'form.sortby'})) {
791: $ENV{'form.sortby'} = 'username';
792: }
793: if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
794: $ENV{'form.Status'} = 'Active';
795: }
796: my $status_select = &Apache::lonhtmlcommon::StatusOptions
797: ($ENV{'form.Status'},'studentform');
798: $r->print(<<END);
799: <input type="hidden" name="action" value="$ENV{'form.action'}" />
800: <input type="hidden" name="state" value="" />
801: <p>
802: <font size="+1">Current Classlist</font>
803:
804: END
805: if ($ENV{'form.action'} ne 'modifystudent') {
806: $r->print(<<END);
807: <font size="+1">
808: <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
809:
810: <a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
811: </font>
812:
813: Student Status:
814: END
815: }
816: $r->print($status_select."</p>\n");
817: my $cid=$ENV{'request.course.id'};
818: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
819: if (! defined($classlist)) {
820: $r->print("There are no students currently enrolled.\n");
821: } else {
822: # Print out the available choices
823: if ($ENV{'form.action'} eq 'modifystudent') {
824: &show_class_list($r,'view','modify','modifystudent',
825: $ENV{'form.Status'},$classlist,$keylist);
826: } else {
827: &show_class_list($r,'view','aboutme','classlist',
828: $ENV{'form.Status'},$classlist,$keylist);
829: }
830: }
831: }
832:
833: # ============================================== view classlist
834: sub print_formatted_classlist {
835: my $r=shift;
836: my $mode = shift;
837: my $cid=$ENV{'request.course.id'};
838: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
839: if (! defined($classlist)) {
840: $r->print("There are no students currently enrolled.\n");
841: } else {
842: &show_class_list($r,$mode,'nolink','csv',
843: $ENV{'form.Status'},$classlist,$keylist);
844: }
845: }
846:
847: # =================================================== Show student list to drop
848: sub show_class_list {
849: my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
850: my $cid=$ENV{'request.course.id'};
851: #
852: # Variables for excel output
853: my ($excel_workbook, $excel_sheet, $excel_filename,$row);
854: #
855: my $sortby = $ENV{'form.sortby'};
856: if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
857: $sortby = 'username';
858: }
859: # Print out header
860: if ($mode eq 'view') {
861: if ($linkto eq 'aboutme') {
862: $r->print('Select a user name to view the users personal page.');
863: } elsif ($linkto eq 'modify') {
864: $r->print('Select a user name to modify the students information');
865: }
866: $r->print(<<END);
867:
868: <input type="hidden" name="sortby" value="$sortby" />
869: <input type="hidden" name="sname" value="" />
870: <input type="hidden" name="sdom" value="" />
871: <p>
872: <table border=2>
873: <tr><th>
874: <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
875: </th><th>
876: <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
877: </th><th>
878: <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
879: </th><th>
880: <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
881: </th><th>
882: <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
883: </th>
884: </tr>
885: END
886: } elsif ($mode eq 'csv') {
887: if($statusmode eq 'Expired') {
888: $r->print('"Students with expired roles"');
889: }
890: if ($statusmode eq 'Any') {
891: $r->print('"'.join('","',("username","domain","ID","student name",
892: "section","status")).'"'."\n");
893: } else {
894: $r->print('"'.join('","',("username","domain","ID","student name",
895: "section")).'"'."\n");
896: }
897: } elsif ($mode eq 'excel') {
898: # Create the excel spreadsheet
899: $excel_filename = '/prtspool/'.
900: $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
901: time.'_'.rand(1000000000).'.xls';
902: $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
903: $excel_filename);
904: $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
905: $excel_sheet = $excel_workbook->addworksheet('classlist');
906: #
907: my $description = 'Classlist for '.
908: $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
909: $excel_sheet->write($row++,0,$description);
910: #
911: $excel_sheet->write($row++,0,["username","domain","ID",
912: "student name","section","status"]);
913: }
914: #
915: # Sort the students
916: my %index;
917: my $i;
918: foreach (@$keylist) {
919: $index{$_} = $i++;
920: }
921: my $index = $index{$sortby};
922: my $second = $index{'username'};
923: my $third = $index{'domain'};
924: my @Sorted_Students = sort {
925: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
926: ||
927: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
928: ||
929: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
930: } (keys(%$classlist));
931: foreach my $student (@Sorted_Students) {
932: my $username = $classlist->{$student}->[$index{'username'}];
933: my $domain = $classlist->{$student}->[$index{'domain'}];
934: my $section = $classlist->{$student}->[$index{'section'}];
935: my $name = $classlist->{$student}->[$index{'fullname'}];
936: my $id = $classlist->{$student}->[$index{'id'}];
937: my $status = $classlist->{$student}->[$index{'status'}];
938: next if (($statusmode ne 'Any') && ($status ne $statusmode));
939: if ($mode eq 'view') {
940: $r->print("<tr>\n <td>\n ");
941: if ($linkto eq 'nothing') {
942: $r->print($username);
943: } elsif ($linkto eq 'aboutme') {
944: $r->print(&Apache::loncommon::aboutmewrapper($username,
945: $username,
946: $domain));
947: } elsif ($linkto eq 'modify') {
948: $r->print('<a href="'.
949: "javascript:document.studentform.sname.value='".
950: $username.
951: "';document.studentform.sdom.value='".$domain.
952: "';document.studentform.state.value='selected".
953: "';document.studentform.submit();".'">'.
954: $username."</a>\n");
955: }
956: $r->print(<<"END");
957: </td>
958: <td>$domain</td>
959: <td>$id</td>
960: <td>$name</td>
961: <td>$section</td>
962: </tr>
963: END
964: } elsif ($mode eq 'csv') {
965: # no need to bother with $linkto
966: my @line = ();
967: foreach ($username,$domain,$id,$name,$section) {
968: push @line,&Apache::loncommon::csv_translate($_);
969: }
970: if ($statusmode eq 'Any') {
971: push @line,&Apache::loncommon::csv_translate($status);
972: }
973: my $tmp = $";
974: $" = '","';
975: $r->print("\"@line\"\n");
976: $" = $tmp;
977: } elsif ($mode eq 'excel') {
978: $excel_sheet->write($row++,0,[$username,$domain,$id,
979: $name,$section,$status]);
980: }
981: }
982: if ($mode eq 'view') {
983: $r->print('</table><br>');
984: } elsif ($mode eq 'excel') {
985: $excel_workbook->close();
986: $r->print('<p><a href="'.$excel_filename.'">'.
987: 'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
988: }
989: }
990:
991:
992: #
993: # print out form for modification of a single students data
994: #
995: sub print_modify_student_form {
996: my $r = shift();
997: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
998: ['sdom','sname']);
999: my $sname = $ENV{'form.sname'};
1000: my $sdom = $ENV{'form.sdom'};
1001: my $sortby = $ENV{'form.sortby'};
1002: # determine the students name information
1003: my %info=&Apache::lonnet::get('environment',
1004: ['firstname','middlename',
1005: 'lastname','generation','id'],
1006: $sdom, $sname);
1007: my ($tmp) = keys(%info);
1008: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1009: $r->print('<font color="#ff0000" size="+2">Error</font>'.
1010: '<p>'.
1011: 'Unable to retrieve environment data for '.$sname.
1012: 'in domain '.$sdom.'</p><p>'.
1013: 'Please contact your LON-CAPA administrator '.
1014: 'regarding this situation.</p></body></html>');
1015: return;
1016: }
1017: # determine the students starting and ending times and section
1018: my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
1019: # Deal with date forms
1020: my $date_table = &date_setting_table($starttime,$endtime);
1021: #
1022: if (! exists($ENV{'form.Status'}) ||
1023: $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1024: $ENV{'form.Status'} = 'crap';
1025: }
1026: # Make sure student is enrolled in course
1027: $r->print(<<END);
1028: <p>
1029: <font size="+1">
1030: Only domain coordinators can change a users password.
1031: </font>
1032: </p>
1033: <input type="hidden" name="slogin" value="$sname" />
1034: <input type="hidden" name="sdomain" value="$sdom" />
1035: <input type="hidden" name="action" value="modifystudent" />
1036: <input type="hidden" name="state" value="done" />
1037: <input type="hidden" name="sortby" value="$sortby" />
1038: <input type="hidden" name="Status" value="$ENV{'form.Status'}" />
1039:
1040: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'}
1041: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
1042: <p>
1043: <b>Student Name</b>
1044: <table>
1045: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
1046: <tr><td>
1047: <input type="text" name="firstname" value="$info{'firstname'}" /></td><td>
1048: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
1049: <input type="text" name="lastname" value="$info{'lastname'}" /></td><td>
1050: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
1051: </table>
1052: </p><p>
1053: <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
1054: </p><p>
1055: <input type="checkbox" name="forceid" >
1056: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1057: (only do if you know what you are doing)
1058: </p><p>
1059: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
1060: </p>
1061: <p>$date_table</p>
1062: <input type="submit" value="Submit Modifications" />
1063: </body></html>
1064: END
1065: return;
1066: }
1067:
1068: #
1069: # modify a single students section
1070: #
1071: sub modify_single_student {
1072: my $r = shift;
1073: #
1074: # Do the date defaults first
1075: my ($starttime,$endtime) = &get_dates_from_form();
1076: if ($ENV{'form.makedatesdefault'}) {
1077: $r->print(&make_dates_default($starttime,$endtime));
1078: }
1079: # Get the 'sortby' and 'Status' variables so the user goes back to their
1080: # previous screen
1081: my $sortby = $ENV{'form.sortby'};
1082: my $status = $ENV{'form.Status'};
1083: #
1084: # We always need this information
1085: my $slogin = $ENV{'form.slogin'};
1086: my $sdom = $ENV{'form.sdomain'};
1087: #
1088: # Get the old data
1089: my %old=&Apache::lonnet::get('environment',
1090: ['firstname','middlename',
1091: 'lastname','generation','id'],
1092: $sdom, $slogin);
1093: $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
1094: $ENV{'request.course.id'});
1095: my ($tmp) = keys(%old);
1096: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
1097: $r->print("There was an error determining the environment values ".
1098: " for $slogin \@ $sdom.");
1099: return;
1100: }
1101: undef $tmp;
1102: #
1103: # Get the new data
1104: my $firstname = $ENV{'form.firstname'};
1105: my $middlename = $ENV{'form.middlename'};
1106: my $lastname = $ENV{'form.lastname'};
1107: my $generation = $ENV{'form.generation'};
1108: my $section = $ENV{'form.section'};
1109: my $courseid = $ENV{'request.course.id'};
1110: my $sid = $ENV{'form.id'};
1111: my $displayable_starttime = localtime($starttime);
1112: my $displayable_endtime = localtime($endtime);
1113: #
1114: # check for forceid override
1115: if ((defined($old{'id'})) && ($old{'id'} ne '') &&
1116: ($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
1117: $r->print("<font color=\"ff0000\">You changed the students id ".
1118: " but did not disable the ID change safeguard.".
1119: " The students id will not be changed.</font>");
1120: $sid = $old{'id'};
1121: }
1122: #
1123: # talk to the user about what we are going to do
1124: $r->print(<<END);
1125: <h2>Modifying data for user $slogin \@ $sdom </h2>
1126: <h3>Student Information</h3>
1127: <table rules="rows" border="1" cellpadding="3" >
1128: <tr>
1129: <th> Field </th>
1130: <th> Old Value </th>
1131: <th> New Value </th>
1132: </tr>
1133: <tr>
1134: <td> <b>First name</b> </td>
1135: <td> $old{'firstname'} </td>
1136: <td> $firstname </td>
1137: </tr><tr>
1138: <td> <b>Middle name</b> </td>
1139: <td> $old{'middlename'} </td>
1140: <td> $middlename </td>
1141: </tr><tr>
1142: <td> <b>Last name</b> </td>
1143: <td> $old{'lastname'} </td>
1144: <td> $lastname </td>
1145: </tr><tr>
1146: <td> <b>Generation</b> </td>
1147: <td> $old{'generation'} </td>
1148: <td> $generation </td>
1149: </tr><tr>
1150: <td> <b>ID</b> </td>
1151: <td> $old{'id'} </td>
1152: <td> $sid </td>
1153: </tr><tr>
1154: <td> <b>Section</b> </td>
1155: <td> $old{'section'} </td>
1156: <td> $section</td>
1157: </tr>
1158: </table>
1159: <h3>Role Information</h3>
1160: <table>
1161: <tr><td align="right"><b>Start Time:</b></td><td> $displayable_starttime </td></tr>
1162: <tr><td align="right"><b>End Time:</b></td><td> $displayable_endtime </td></tr>
1163: </table>
1164: <p>
1165: END
1166: #
1167: # Send request(s) to modify data (final undef is for 'desiredhost',
1168: # which is a moot point because the student already has an account.
1169: my $modify_section_results = &modifystudent($sdom,$slogin,
1170: $ENV{'request.course.id'},
1171: $section,undef);
1172: if ($modify_section_results !~ /^ok/) {
1173: $r->print("An error occured during the attempt to change the ".
1174: "section for this student.<br />");
1175: }
1176: my $roleresults = &Apache::lonnet::modifystudent
1177: ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
1178: $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
1179: if ($roleresults eq 'refused' ) {
1180: $r->print("Your request to change the role information for this ".
1181: "student was refused. You do not appear to have ".
1182: "sufficient authority to change student information.");
1183: } elsif ($roleresults !~ /ok/) {
1184: $r->print("An error occurred during the attempt to change the role".
1185: " information for this student. <br />".
1186: "The error reported was ".
1187: $roleresults);
1188: &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
1189: " data for ".$slogin." \@ ".$sdom." by ".
1190: $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
1191: ":".$roleresults);
1192: } else { # everything is okay!
1193: $r->print("Student information updated successfully. <br />".
1194: "The student must log out and log in again to see ".
1195: "these changes.");
1196: }
1197: $r->print(<<END);
1198: </p><p>
1199: <input type="hidden" name="action" value="modifystudent" />
1200: <input type="hidden" name="sortby" value="$sortby" />
1201: <input type="hidden" name="Status" value="$status" />
1202: <a href="javascript:document.studentform.submit();">Modify another students data</a>
1203: </body></html>
1204: END
1205: return;
1206: }
1207:
1208: sub get_enrollment_data {
1209: my ($sname,$sdomain) = @_;
1210: my $courseid = $ENV{'request.course.id'};
1211: $courseid =~ s:_:/:g;
1212: my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
1213: my ($tmp) = keys(%roles);
1214: # Bail out if we were unable to get the students roles
1215: return "666" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1216: # Go through the roles looking for enrollment in this course
1217: my ($end,$start) = (undef,undef);
1218: my $section = '';
1219: my $count = scalar(keys(%roles));
1220: while (my ($course,$role) = each(%roles)) {
1221: if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
1222: #
1223: # Get active role
1224: $section=$1;
1225: (undef,$end,$start)=split(/\_/,$role);
1226: my $now=time;
1227: my $notactive=0;
1228: if ($start) {
1229: if ($now<$start) { $notactive=1; }
1230: }
1231: if ($end) {
1232: if ($now>$end) { $notactive=1; }
1233: }
1234: unless ($notactive) { return ($start,$end,$section); }
1235: }
1236: }
1237: return ($start,$end,$section);
1238: }
1239:
1240: #################################################
1241: #################################################
1242:
1243: =pod
1244:
1245: =item show_drop_list
1246:
1247: Display a list of students to drop
1248: Inputs:
1249:
1250: =over 4
1251:
1252: =item $r, Apache request
1253:
1254: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
1255:
1256: =item $keylist, array pointer returned from loncoursedata::get_classlist()
1257: which describes the order elements are stored in the %$classlist values.
1258:
1259: =item $nosort, if true, sorting links are omitted.
1260:
1261: =back
1262:
1263: =cut
1264:
1265: #################################################
1266: #################################################
1267: sub show_drop_list {
1268: my ($r,$classlist,$keylist,$nosort)=@_;
1269: my $cid=$ENV{'request.course.id'};
1270: if (! exists($ENV{'form.sortby'})) {
1271: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1272: ['sortby']);
1273: }
1274: my $sortby = $ENV{'form.sortby'};
1275: if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
1276: $sortby = 'username';
1277: }
1278: #
1279: my $action = "drop";
1280: $r->print(<<END);
1281: <input type="hidden" name="sortby" value="$sortby" />
1282: <input type="hidden" name="action" value="$action" />
1283: <input type="hidden" name="state" value="done" />
1284: <script>
1285: function checkAll(field) {
1286: for (i = 0; i < field.length; i++)
1287: field[i].checked = true ;
1288: }
1289:
1290: function uncheckAll(field) {
1291: for (i = 0; i < field.length; i++)
1292: field[i].checked = false ;
1293: }
1294: </script>
1295: <p>
1296: <input type="hidden" name="phase" value="four">
1297: END
1298:
1299: if ($nosort) {
1300: $r->print(<<END);
1301: <table border=2>
1302: <tr>
1303: <th> </th>
1304: <th>username</th>
1305: <th>domain</th>
1306: <th>ID</th>
1307: <th>student name</th>
1308: <th>section</th>
1309: </tr>
1310: END
1311:
1312: } else {
1313: $r->print(<<END);
1314: <table border=2>
1315: <tr><th> </th>
1316: <th>
1317: <a href="/adm/dropadd?action=$action&sortby=username">username</a>
1318: </th><th>
1319: <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
1320: </th><th>
1321: <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
1322: </th><th>
1323: <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
1324: </th><th>
1325: <a href="/adm/dropadd?action=$action&sortby=section">section</a>
1326: </th>
1327: </tr>
1328: END
1329: }
1330: #
1331: # Sort the students
1332: my %index;
1333: my $i;
1334: foreach (@$keylist) {
1335: $index{$_} = $i++;
1336: }
1337: my $index = $index{$sortby};
1338: my $second = $index{'username'};
1339: my $third = $index{'domain'};
1340: my @Sorted_Students = sort {
1341: lc($classlist->{$a}->[$index]) cmp lc($classlist->{$b}->[$index])
1342: ||
1343: lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
1344: ||
1345: lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1346: } (keys(%$classlist));
1347: foreach my $student (@Sorted_Students) {
1348: my $error;
1349: my $username = $classlist->{$student}->[$index{'username'}];
1350: my $domain = $classlist->{$student}->[$index{'domain'}];
1351: my $section = $classlist->{$student}->[$index{'section'}];
1352: my $name = $classlist->{$student}->[$index{'fullname'}];
1353: my $id = $classlist->{$student}->[$index{'id'}];
1354: my $status = $classlist->{$student}->[$index{'status'}];
1355: next if ($status ne 'Active');
1356: #
1357: $r->print(<<"END");
1358: <tr>
1359: <td><input type="checkbox" name="droplist" value="$student"></td>
1360: <td>$username</td>
1361: <td>$domain</td>
1362: <td>$id</td>
1363: <td>$name</td>
1364: <td>$section</td>
1365: </tr>
1366: END
1367: }
1368: $r->print('</table><br>');
1369: $r->print(<<"END");
1370: </p><p>
1371: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)">
1372: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)">
1373: <p><input type=submit value="Drop Students"></p>
1374: END
1375: return;
1376: }
1377:
1378: #
1379: # Print out the initial form to get the courselist file
1380: #
1381: sub print_first_courselist_upload_form {
1382: my $r=shift;
1383: my $upfile_select=&Apache::loncommon::upfile_select_html();
1384: my $create_classlist_help =
1385: &Apache::loncommon::help_open_topic("Course_Create_Class_List",
1386: "How do I create a class list from a spreadsheet");
1387: my $create_csv_help =
1388: &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
1389: "How do I create a CSV file from a spreadsheet");
1390: $r->print(<<ENDUPFORM);
1391: <input type=hidden name=phase value=two>
1392: <h3>Upload a courselist</h3>
1393: $upfile_select
1394: <p>
1395: <input type=submit name="fileupload" value="Upload Courselist">
1396: <input type="hidden" name="action" value="upload" />
1397: <input type="hidden" name="state" value="got_file" />
1398: </p>
1399: $create_classlist_help <br />
1400: $create_csv_help
1401: </body></html>
1402: ENDUPFORM
1403: return;
1404: }
1405:
1406: # ================================================= Drop/Add from uploaded file
1407: sub upfile_drop_add {
1408: my $r=shift;
1409: &Apache::loncommon::load_tmp_file($r);
1410: my @studentdata=&Apache::loncommon::upfile_record_sep();
1411: my @keyfields = split(/\,/,$ENV{'form.keyfields'});
1412: my $cid = $ENV{'request.course.id'};
1413: my %fields=();
1414: for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1415: if ($ENV{'form.upfile_associate'} eq 'reverse') {
1416: if ($ENV{'form.f'.$i} ne 'none') {
1417: $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
1418: }
1419: } else {
1420: $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
1421: }
1422: }
1423: #
1424: my ($startdate,$enddate) = &get_dates_from_form();
1425: if ($ENV{'form.makedatesdefault'}) {
1426: $r->print(&make_dates_default($startdate,$enddate));
1427: }
1428: # Determine domain and desired host (home server)
1429: my $domain=$ENV{'form.lcdomain'};
1430: my $desiredhost = $ENV{'form.lcserver'};
1431: if (lc($desiredhost) eq 'default') {
1432: $desiredhost = undef;
1433: } else {
1434: my %home_servers = &Apache::loncommon::get_library_servers($domain);
1435: if (! exists($home_servers{$desiredhost})) {
1436: $r->print('<font color="#ff0000">Error:</font>'.
1437: 'Invalid home server specified');
1438: return;
1439: }
1440: }
1441: # Determine authentication mechanism
1442: my $amode = '';
1443: my $genpwd = '';
1444: if ($ENV{'form.login'} eq 'krb') {
1445: $amode='krb';
1446: $amode.=$ENV{'form.krbver'};
1447: $genpwd=$ENV{'form.krbarg'};
1448: } elsif ($ENV{'form.login'} eq 'int') {
1449: $amode='internal';
1450: if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
1451: $genpwd=$ENV{'form.intarg'};
1452: }
1453: } elsif ($ENV{'form.login'} eq 'loc') {
1454: $amode='localauth';
1455: if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
1456: $genpwd=$ENV{'form.locarg'};
1457: }
1458: }
1459: unless (($domain=~/\W/) || ($amode eq '')) {
1460: #######################################
1461: ## Enroll Students ##
1462: #######################################
1463: $r->print('<h3>Enrolling Students</h3>');
1464: my $count=0;
1465: my $flushc=0;
1466: my %student=();
1467: # Get new classlist
1468: foreach (@studentdata) {
1469: my %entries=&Apache::loncommon::record_sep($_);
1470: # Determine student name
1471: unless (($entries{$fields{'username'}} eq '') ||
1472: (!defined($entries{$fields{'username'}}))) {
1473: my ($fname, $mname, $lname,$gen) = ('','','','');
1474: if (defined($fields{'names'})) {
1475: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
1476: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1477: } else {
1478: if (defined($fields{'fname'})) {
1479: $fname=$entries{$fields{'fname'}};
1480: }
1481: if (defined($fields{'mname'})) {
1482: $mname=$entries{$fields{'mname'}};
1483: }
1484: if (defined($fields{'lname'})) {
1485: $lname=$entries{$fields{'lname'}};
1486: }
1487: if (defined($fields{'gen'})) {
1488: $gen=$entries{$fields{'gen'}};
1489: }
1490: }
1491: if ($entries{$fields{'username'}}=~/\W/) {
1492: $r->print('<p><b>Unacceptable username: '.
1493: $entries{$fields{'username'}}.' for user '.
1494: $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
1495: } else {
1496: # determine section number
1497: my $sec='';
1498: my $username=$entries{$fields{'username'}};
1499: if (defined($fields{'sec'})) {
1500: if (defined($entries{$fields{'sec'}})) {
1501: $sec=$entries{$fields{'sec'}};
1502: }
1503: }
1504: # determine student id number
1505: my $id='';
1506: if (defined($fields{'id'})) {
1507: if (defined($entries{$fields{'id'}})) {
1508: $id=$entries{$fields{'id'}};
1509: }
1510: $id=~tr/A-Z/a-z/;
1511: }
1512: # determine student password
1513: my $password='';
1514: if ($genpwd) {
1515: $password=$genpwd;
1516: } else {
1517: if (defined($fields{'ipwd'})) {
1518: if ($entries{$fields{'ipwd'}}) {
1519: $password=$entries{$fields{'ipwd'}};
1520: }
1521: }
1522: }
1523: # Clean up whitespace
1524: foreach (\$domain,\$username,\$id,\$fname,\$mname,
1525: \$lname,\$gen,\$sec) {
1526: $$_ =~ s/(\s+$|^\s+)//g;
1527: }
1528: if ($password) {
1529: &modifystudent($domain,$username,$cid,$sec,
1530: $desiredhost);
1531: my $reply=&Apache::lonnet::modifystudent
1532: ($domain,$username,$id,$amode,$password,
1533: $fname,$mname,$lname,$gen,$sec,$enddate,
1534: $startdate,$ENV{'form.forceid'},$desiredhost);
1535: if ($reply ne 'ok') {
1536: $r->print('<p><b>'.
1537: 'Error enrolling '.$username.': '.
1538: $reply.'</b></p>');
1539: } else {
1540: $count++; $flushc++;
1541: $student{$username}=1;
1542: $r->print('. ');
1543: if ($flushc>15) {
1544: $r->rflush;
1545: $flushc=0;
1546: }
1547: }
1548: } else {
1549: $r->print("<p><b>No password for $username</b><p>");
1550: }
1551: }
1552: }
1553: } # end of foreach (@studentdata)
1554: $r->print('<p>Processed Students: '.$count.'</p>');
1555: $r->print("<p>If active, the new role will be available when the ".
1556: "students next log in to LON-CAPA.</p>");
1557: #####################################
1558: # Drop students #
1559: #####################################
1560: if ($ENV{'form.fullup'} eq 'yes') {
1561: $r->print('<h3>Dropping Students</h3>');
1562: # Get current classlist
1563: my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
1564: if (! defined($classlist)) {
1565: $r->print("There are no students currently enrolled.\n");
1566: } else {
1567: # Remove the students we just added from the list of students.
1568: foreach (@studentdata) {
1569: my %entries=&Apache::loncommon::record_sep($_);
1570: unless (($entries{$fields{'username'}} eq '') ||
1571: (!defined($entries{$fields{'username'}}))) {
1572: delete($classlist->{$entries{$fields{'username'}}.
1573: ':'.$domain});
1574: }
1575: }
1576: # Print out list of dropped students.
1577: &show_drop_list($r,$classlist,$keylist,'nosort');
1578: }
1579: }
1580: } # end of unless
1581: }
1582:
1583: # ================================================================== Phase four
1584: sub drop_student_list {
1585: my $r=shift;
1586: my $count=0;
1587: my @droplist;
1588: if (ref($ENV{'form.droplist'})) {
1589: @droplist = @{$ENV{'form.droplist'}};
1590: } else {
1591: @droplist = ($ENV{'form.droplist'});
1592: }
1593: foreach (@droplist) {
1594: my ($uname,$udom)=split(/\:/,$_);
1595: # drop student
1596: my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
1597: if ($result eq 'ok' || $result eq 'ok:') {
1598: $r->print('Dropped '.$uname.' @ '.$udom.'<br>');
1599: $count++;
1600: } else {
1601: $r->print('Error dropping '.$uname.' @ '.$udom.': '.$result.
1602: '<br />');
1603: }
1604: }
1605: $r->print('<p><b>Dropped '.$count.' student(s).</b>');
1606: $r->print('<p>Re-enrollment will re-activate data.') if ($count);
1607: }
1608:
1609: ###################################################################
1610: ###################################################################
1611:
1612: =pod
1613:
1614: =item &handler
1615:
1616: The typical handler you see in all these modules. Takes $r, the
1617: http request, as an argument.
1618:
1619: The response to the request is governed by two form variables
1620:
1621: form.action form.state response
1622: ---------------------------------------------------
1623: undefined undefined print main menu
1624: upload undefined print courselist upload menu
1625: upload got_file deal with uploaded file,
1626: print the upload managing menu
1627: upload enrolling enroll students based on upload
1628: drop undefined print the classlist ready to drop
1629: drop done drop the selected students
1630: enrollstudent undefined print single student enroll menu
1631: enrollstudent enrolling enroll student
1632: classlist undefined print html classlist
1633: classlist csv print csv classlist
1634: modifystudent undefined print classlist to select student to modify
1635: modifystudent selected print modify student menu
1636: modifystudent done make modifications to student record
1637:
1638: =cut
1639:
1640: ###################################################################
1641: ###################################################################
1642: sub handler {
1643: my $r=shift;
1644: if ($r->header_only) {
1645: $r->content_type('text/html');
1646: $r->send_http_header;
1647: return OK;
1648: }
1649: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1650: ['action','state']);
1651: # Needs to be in a course
1652: if (! (($ENV{'request.course.fn'}) &&
1653: (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
1654: # Not in a course, or not allowed to modify parms
1655: $ENV{'user.error.msg'}=
1656: "/adm/dropadd:cst:0:0:Cannot drop or add students";
1657: return HTTP_NOT_ACCEPTABLE;
1658: }
1659: #
1660: # Only output the header information if they did not request csv format
1661: #
1662: if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
1663: $r->content_type('text/csv');
1664: } else {
1665: # Start page
1666: $r->content_type('text/html');
1667: $r->send_http_header;
1668: $r->print(&header());
1669: }
1670: #
1671: # Main switch on form.action and form.state, as appropriate
1672: if (! exists($ENV{'form.action'})) {
1673: &print_main_menu($r);
1674: } elsif ($ENV{'form.action'} eq 'upload') {
1675: if (! exists($ENV{'form.state'})) {
1676: &print_first_courselist_upload_form($r);
1677: } elsif ($ENV{'form.state'} eq 'got_file') {
1678: &print_upload_manager_form($r);
1679: } elsif ($ENV{'form.state'} eq 'enrolling') {
1680: if ($ENV{'form.datatoken'}) {
1681: &upfile_drop_add($r);
1682: } else {
1683: # Hmmm, this is an error
1684: }
1685: } else {
1686: &print_first_courselist_upload_form($r);
1687: }
1688: } elsif ($ENV{'form.action'} eq 'drop') {
1689: if (! exists($ENV{'form.state'})) {
1690: &print_drop_menu($r);
1691: } elsif ($ENV{'form.state'} eq 'done') {
1692: &drop_student_list($r);
1693: } else {
1694: &print_drop_menu($r);
1695: }
1696: } elsif ($ENV{'form.action'} eq 'enrollstudent') {
1697: if (! exists($ENV{'form.state'})) {
1698: &print_enroll_single_student_form($r);
1699: } elsif ($ENV{'form.state'} eq 'enrolling') {
1700: &enroll_single_student($r);
1701: } else {
1702: &print_enroll_single_student_form($r);
1703: }
1704: } elsif ($ENV{'form.action'} eq 'classlist') {
1705: if (! exists($ENV{'form.state'})) {
1706: &print_html_classlist($r);
1707: } elsif ($ENV{'form.state'} eq 'csv') {
1708: &print_formatted_classlist($r,'csv');
1709: } elsif ($ENV{'form.state'} eq 'excel') {
1710: &print_formatted_classlist($r,'excel');
1711: } else {
1712: &print_html_classlist($r);
1713: }
1714: } elsif ($ENV{'form.action'} eq 'modifystudent') {
1715: if (! exists($ENV{'form.state'})) {
1716: &print_html_classlist($r);
1717: } elsif ($ENV{'form.state'} eq 'selected') {
1718: &print_modify_student_form($r);
1719: } elsif ($ENV{'form.state'} eq 'done') {
1720: &modify_single_student($r);
1721: } else {
1722: &print_html_classlist($r);
1723: }
1724: } else {
1725: # We should not end up here, but I guess it is possible
1726: &Apache::lonnet::logthis("Undetermined state in londropadd.pm. ".
1727: "form.action = ".$ENV{'form.action'}.
1728: "Someone should fix this.");
1729: &print_main_menu($r);
1730: }
1731: #
1732: # Finish up
1733: if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
1734: $r->print("\n");
1735: } else {
1736: $r->print('</form></body></html>');
1737: }
1738: return OK;
1739: }
1740:
1741: ###################################################################
1742: ###################################################################
1743:
1744: 1;
1745: __END__
1746:
1747:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>