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