1: # The LearningOnline Network with CAPA
2: # Handler to drop and add students in courses
3: #
4: # $Id: londropadd.pm,v 1.45.2.1 2002/08/22 21:22:30 albertel 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: # YEAR=2000
35: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
36: #
37: # 10/11,10/12,10/16 Gerd Kortemeyer)
38: #
39: # 11/20,11/21,11/22,11/23,11/24,11/25,11/27,11/28,
40: # 12/08,12/12 Gerd Kortemeyer)
41: #
42: # 12/26,12/27,12/28,
43: # YEAR=2001
44: # 01/01/01,01/15,02/10,02/13,02/14,02/22 Gerd Kortemeyer
45: # 8/6 Scott Harrison
46: # Guy Albertelli
47: # 9/25 Gerd Kortemeyer
48: # 12/19 Guy Albertelli
49: # YEAR=2002
50: # 1/4 Gerd Kortemeyer
51:
52: package Apache::londropadd;
53:
54: use strict;
55: use Apache::lonnet();
56: use Apache::loncommon();
57: use Apache::Constants qw(:common :http REDIRECT);
58:
59: # ================================================================ Print header
60:
61: sub header {
62: return(<<ENDHEAD);
63: <html>
64: <head>
65: <title>LON-CAPA Enrollment Manager</title>
66: </head>
67: <body bgcolor="#FFFFFF">
68: <img align=right src=/adm/lonIcons/lonlogos.gif>
69: <h1>$ENV{'course.'.$ENV{'request.course.id'}.'.description'}</h1>
70: <h2>Enrollment Manager</h2>
71: <form method="post" enctype="multipart/form-data"
72: action="/adm/dropadd" name="studentform">
73: ENDHEAD
74: }
75:
76: # =========== Drop student from all sections of a course, except optional $csec
77: sub modifystudent {
78: my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
79: # if $csec is undefined, drop the student from all the courses matching
80: # this one. If $csec is defined, drop them from all other sections of
81: # this course and add them to section $csec
82: $courseid=~s/\_/\//g;
83: $courseid=~s/^(\w)/\/$1/;
84: my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
85: my ($tmp) = keys(%roles);
86: # Bail out if we were unable to get the students roles
87: return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
88: # Go through the roles looking for enrollment in this course
89: my $result = '';
90: foreach my $course (keys(%roles)) {
91: if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
92: # We are in this course
93: my $section=$1;
94: $section='' if ($course eq $courseid.'_st');
95: if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
96: my (undef,$end,$start)=split(/\_/,$roles{$course});
97: my $now=time;
98: if (!($start && ($now<$start)) || !($end && ($now>$end))) {
99: my $reply=&Apache::lonnet::modifystudent
100: ($udom,$unam,'','','','','','','',
101: $section,time,undef,undef,$desiredhost);
102: $result .= $reply.':';
103: }
104: }
105: }
106: }
107: if ($result eq '') {
108: $result eq 'Unable to find section for this student';
109: } else {
110: $result =~ s/(ok:)+/ok/g;
111: }
112: return $result;
113: }
114:
115: # ============ build a domain and server selection form
116: sub domain_form {
117: my ($defdom) = @_;
118: # Set up domain and server selection forms
119: #
120: # Get the domains
121: my @domains = &Apache::loncommon::get_domains();
122: # build up the menu information to be passed to
123: # &Apache::loncommon::linked_select_forms
124: my %select_menus;
125: foreach my $dom (@domains) {
126: # set up the text for this domain
127: $select_menus{$dom}->{'text'}= $dom;
128: # we want a choice of 'default' as the default in the second menu
129: $select_menus{$dom}->{'default'}= 'default';
130: $select_menus{$dom}->{'select2'}->{'default'} = 'default';
131: # Now build up the other items in the second menu
132: my %servers = &Apache::loncommon::get_library_servers($dom);
133: foreach my $server (keys(%servers)) {
134: $select_menus{$dom}->{'select2'}->{$server}
135: = "$server $servers{$server}";
136: }
137: }
138: my $result = &Apache::loncommon::linked_select_forms
139: ('studentform',' with home server ',$defdom,
140: 'lcdomain','lcserver',\%select_menus);
141: return $result;
142: }
143:
144: # ============================================================== Menu Phase One
145: sub menu_phase_one {
146: my $r=shift;
147: my $upfile_select=&Apache::loncommon::upfile_select_html();
148: my $create_classlist_help =
149: &Apache::loncommon::help_open_topic("Course_Create_Class_List",
150: "How do I create a class list from a spreadsheet");
151: my $create_csv_help =
152: &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
153: "How do I create a CSV file from a spreadsheet");
154: $r->print(<<ENDUPFORM);
155: <input type=hidden name=phase value=two>
156: <hr>
157: <h3>Upload a courselist</h3>
158: $upfile_select
159: <p><input type=submit name="fileupload" value="Upload Courselist">
160: <br />
161: $create_classlist_help <br />
162: $create_csv_help
163:
164: <hr />
165: <h3>Enroll a single student</h3>
166: <p><input type=submit name="enroll" value="Enroll Student"></p>
167: <hr />
168: <h3>Classlist</h3>
169: <p><input type=submit name="view" value="View Class List">
170: <input type=submit name="viewcsv" value="Comma Separated Class List"></p>
171: <hr />
172: <h3>Drop students</h3>
173: <p><input type=submit name="drop" value="Selection List"></p>
174: ENDUPFORM
175: }
176:
177: sub phase_two_header {
178: my ($r,$datatoken,$distotal,$krbdefdom)=@_;
179: my $javascript;
180: if ($ENV{'form.upfile_associate'} eq 'reverse') {
181: $javascript=&phase_two_javascript_reverse_associate();
182: } else {
183: $javascript=&phase_two_javascript_forward_associate();
184: }
185: my $javascript_validations=&javascript_validations($krbdefdom);
186: $r->print(<<ENDPICK);
187: <h3>Uploading Class List</h3>
188: <hr>
189: <h3>Identify fields</h3>
190: Total number of records found in file: $distotal <hr />
191: Enter as many fields as you can. The system will inform you and bring you back
192: to this page if the data selected is insufficient to run your class.<hr />
193: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
194: <input type="hidden" name="associate" value="" />
195: <input type="hidden" name="phase" value="three" />
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: sub javascript_validations {
210: my ($krbdefdom)=@_;
211: my %param = ( formname => 'studentform',
212: kerb_def_dom => $krbdefdom );
213: my $authheader = &Apache::loncommon::authform_header(%param);
214: return (<<ENDPICK);
215: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
216: var foundatype=0;
217: var message='';
218: if (founduname==0) {
219: alert('You need to specify the username field');
220: return;
221: }
222: if (current.radiovalue == null || current.radiovalue == 'nochange') {
223: // They did not check any of the login radiobuttons.
224: alert('You must choose an authentication type');
225: return;
226: }
227: foundatype=1;
228: if (current.argfield == null || current.argfield == '') {
229: var alertmsg = '';
230: switch (current.value) {
231: case 'krb':
232: alertmsg = 'You need to specify the Kerberos domain';
233: break;
234: case 'loc':
235: case 'fsys':
236: alertmsg = 'You need to specify the initial password';
237: break;
238: case 'fsys':
239: alertmsg = '';
240: break;
241: default:
242: alertmsg = '';
243: }
244: if (alertmsg != '') {
245: alert(alertmsg);
246: return;
247: }
248: }
249:
250: if (foundname==0) { message='No name fields specified. '; }
251: if (foundid==0) { message+='No ID or student number field specified. '; }
252: if (foundsec==0) { message+='No section or group field specified. '; }
253: if (vf.startdate.value=='') {
254: message+='No starting date set. ';
255: }
256: if (vf.enddate.value=='') {
257: message+='No ending date set. ';
258: }
259: if ((vf.enddate.value!='') && (vf.startdate.value!='')) {
260: if (Math.round(vf.enddate.value)<Math.round(vf.startdate.value)) {
261: alert('Ending date is before starting date');
262: return;
263: }
264: }
265: if (message!='') {
266: message+='Continue enrollment?';
267: if (confirm(message)) {
268: pclose();
269: vf.submit();
270: }
271: } else {
272: pclose();
273: vf.submit();
274: }
275: }
276:
277:
278: function pclose() {
279: parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
280: "height=350,width=350,scrollbars=no,menubar=no");
281: parmwin.close();
282: }
283:
284: function pjump(type,dis,value,marker,ret,call) {
285: parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
286: +"&value="+escape(value)+"&marker="+escape(marker)
287: +"&return="+escape(ret)
288: +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
289: "height=350,width=350,scrollbars=no,menubar=no");
290:
291: }
292:
293: function dateset() {
294: if (document.studentform.pres_marker.value=='end') {
295: document.studentform.enddate.value=
296: document.studentform.pres_value.value;
297: }
298: if (document.studentform.pres_marker.value=='start') {
299: document.studentform.startdate.value=
300: document.studentform.pres_value.value;
301: }
302: pclose();
303: }
304:
305: $authheader
306: ENDPICK
307:
308: }
309:
310: sub phase_two_javascript_forward_associate {
311: return(<<ENDPICK);
312: function verify(vf) {
313: var founduname=0;
314: var foundpwd=0;
315: var foundname=0;
316: var foundid=0;
317: var foundsec=0;
318: var tw;
319: for (i=0;i<=vf.nfields.value;i++) {
320: tw=eval('vf.f'+i+'.selectedIndex');
321: if (tw==1) { founduname=1; }
322: if ((tw>=2) && (tw<=6)) { foundname=1; }
323: if (tw==7) { foundid=1; }
324: if (tw==8) { foundsec=1; }
325: if (tw==9) { foundpwd=1; }
326: }
327: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
328: }
329:
330: function flip(vf,tf) {
331: var nw=eval('vf.f'+tf+'.selectedIndex');
332: var i;
333: for (i=0;i<=vf.nfields.value;i++) {
334: if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
335: eval('vf.f'+i+'.selectedIndex=0;')
336: }
337: }
338: if (tf==1 && nw!=0) {
339: for (i=2;i<=5;i++) {
340: eval('vf.f'+i+'.selectedIndex=0;')
341: }
342: }
343: if (nw==2) {
344: for (i=0;i<=vf.nfields.value;i++) {
345: if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
346: (eval('vf.f'+i+'.selectedIndex')<=6)) {
347: eval('vf.f'+i+'.selectedIndex=0;')
348: }
349: }
350: }
351: if ((nw>=3) && (nw<=6)) {
352: for (i=0;i<=vf.nfields.value;i++) {
353: if (eval('vf.f'+i+'.selectedIndex')==2) {
354: eval('vf.f'+i+'.selectedIndex=0;')
355: }
356: }
357: }
358: if (nw==9) {
359: changed_radio('int',document.studentform);
360: set_auth_radio_buttons('int',document.studentform);
361: vf.intarg.value='';
362: vf.krbarg.value='';
363: vf.locarg.value='';
364: }
365: }
366:
367: function clearpwd(vf) {
368: var i;
369: for (i=0;i<=vf.nfields.value;i++) {
370: if (eval('vf.f'+i+'.selectedIndex')==9) {
371: eval('vf.f'+i+'.selectedIndex=0;')
372: }
373: }
374: }
375:
376: ENDPICK
377: }
378:
379: sub phase_two_javascript_reverse_associate {
380: return(<<ENDPICK);
381: function verify(vf) {
382: var founduname=0;
383: var foundpwd=0;
384: var foundname=0;
385: var foundid=0;
386: var foundsec=0;
387: var tw;
388: for (i=0;i<=vf.nfields.value;i++) {
389: tw=eval('vf.f'+i+'.selectedIndex');
390: if (i==0 && tw!=0) { founduname=1; }
391: if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
392: if (i==6 && tw!=0) { foundid=1; }
393: if (i==7 && tw!=0) { foundsec=1; }
394: if (i==8 && tw!=0) { foundpwd=1; }
395: }
396: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
397: }
398:
399: function flip(vf,tf) {
400: var nw=eval('vf.f'+tf+'.selectedIndex');
401: var i;
402: // picked the all one one name field, reset the other name ones to blank
403: if (tf==1 && nw!=0) {
404: for (i=2;i<=5;i++) {
405: eval('vf.f'+i+'.selectedIndex=0;')
406: }
407: }
408: //picked one of the piecewise name fields, reset the all in
409: //one field to blank
410: if ((tf>=2) && (tf<=5) && (nw!=0)) {
411: eval('vf.f1.selectedIndex=0;')
412: }
413: // intial password specified, pick internal authentication
414: if (tf==8 && nw!=0) {
415: changed_radio('int',document.studentform);
416: set_auth_radio_buttons('int',document.studentform);
417: vf.krbarg.value='';
418: vf.intarg.value='';
419: vf.locarg.value='';
420: }
421: }
422:
423: function clearpwd(vf) {
424: var i;
425: if (eval('vf.f8.selectedIndex')!=0) {
426: eval('vf.f8.selectedIndex=0;')
427: }
428: }
429: ENDPICK
430: }
431:
432: sub phase_two_end {
433: my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
434: my %param = ( formname => 'document.studentform');
435: my $krbform = &Apache::loncommon::authform_kerberos(%param);
436: my $intform = &Apache::loncommon::authform_internal(%param);
437: my $locform = &Apache::loncommon::authform_local(%param);
438: my $domform = &domain_form($defdom);
439: $r->print(<<ENDPICK);
440: </table>
441: <input type=hidden name=nfields value=$i>
442: <input type=hidden name=keyfields value="$keyfields">
443: <h3>Login Type</h3>
444: <p>Note: this will not take effect if the user already exists</p>
445: <p>
446: $krbform
447: </p>
448: <p>
449: $intform
450: </p>
451: <p>
452: $locform
453: </p>
454: <h3>LON-CAPA Domain for Students</h3>
455: LON-CAPA domain: $domform <p>
456: <h3>Starting and Ending Dates</h3>
457: <input type="hidden" value='' name="pres_value" >
458: <input type="hidden" value='' name="pres_type" >
459: <input type="hidden" value='' name="pres_marker" >
460: <input type="hidden" value='$today' name="startdate" >
461: <input type="hidden" value='$halfyear' name="enddate" >
462: <a
463: href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
464: >Set Starting Date</a><p>
465:
466: <a
467: href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
468: >Set Ending Date</a><p>
469: <h3>Full Update</h3>
470: <input type=checkbox name=fullup value=yes> Full update
471: (also print list of users not enrolled anymore)<p>
472: <h3>ID/Student Number</h3>
473: <input type=checkbox name=forceid value=yes>
474: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
475: (only do if you know what you are doing)<p>
476: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
477: Note: for large courses, this operation might be time consuming.
478: ENDPICK
479: }
480:
481: # ======================================================= Menu Phase Two Upload
482: sub menu_phase_two_upload {
483: my $r=shift;
484:
485: my $datatoken;
486: if (!$ENV{'form.datatoken'}) {
487: $datatoken=&Apache::loncommon::upfile_store($r);
488: } else {
489: $datatoken=$ENV{'form.datatoken'};
490: &Apache::loncommon::load_tmp_file($r);
491: }
492: my @records=&Apache::loncommon::upfile_record_sep();
493: my $total=$#records;
494: my $distotal=$total+1;
495: $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
496: my $krbdefdom=$1;
497: $krbdefdom=~tr/a-z/A-Z/;
498: my $today=time;
499: my $halfyear=$today+15552000;
500: my $defdom=$r->dir_config('lonDefDomain');
501: &phase_two_header($r,$datatoken,$distotal,$krbdefdom);
502: my $i;
503: my $keyfields;
504: if ($total>=0) {
505: my @d=(['username','Username'],['names','Last Name, First Names'],
506: ['fname','First Name'],['mname','Middle Names/Initials'],
507: ['lname','Last Name'],['gen','Generation'],
508: ['id','ID/Student Number'],['sec','Group/Section'],
509: ['ipwd','Initial Password']);
510: if ($ENV{'form.upfile_associate'} eq 'reverse') {
511: &Apache::loncommon::csv_print_samples($r,\@records);
512: $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
513: foreach (@d) { $keyfields.=$_->[0].','; }
514: chop($keyfields);
515: } else {
516: unshift(@d,['none','']);
517: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
518: my %sone=&Apache::loncommon::record_sep($records[0]);
519: $keyfields=join(',',sort(keys(%sone)));
520: }
521: }
522: &phase_two_end($r,$i,$keyfields,$defdom,$today,$halfyear);
523: }
524:
525: # ======================================================= Enroll single student
526: sub enroll_single_student {
527: my $r=shift;
528: $r->print('<h3>Enrolling Student</h3>');
529: $r->print('<p>Enrolling '.$ENV{'form.cuname'}." in domain ".
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: &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
566: $ENV{'request.course.id'},$ENV{'form.csec'},
567: $desiredhost);
568: $r->print(&Apache::lonnet::modifystudent(
569: $ENV{'form.lcdomain'},$ENV{'form.cuname'},
570: $ENV{'form.cstid'},$amode,$genpwd,
571: $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
572: $ENV{'form.clast'},$ENV{'form.cgen'},
573: $ENV{'form.csec'},$ENV{'form.enddate'},
574: $ENV{'form.startdate'},$ENV{'form.forceid'},
575: $desiredhost));
576: } else {
577: $r->print('<p><font color="#ff0000">ERROR</font> '.
578: 'Invalid login mode or password. '.
579: 'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
580: }
581: } else {
582: $r->print('Invalid username or domain');
583: }
584: }
585:
586: # ======================================================= Menu Phase Two Enroll
587: sub menu_phase_two_enroll {
588: my $r=shift;
589: $r->print("<h3>Enroll One Student</h3>");
590: my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
591: $krbdefdom=~tr/a-z/A-Z/;
592: my $today = time;
593: my $halfyear = $today+15552000;
594: my $defdom=$r->dir_config('lonDefDomain');
595: my $javascript_validations=&javascript_validations($krbdefdom);
596: # Set up authentication forms
597: my %param = ( formname => 'document.studentform');
598: my $krbform = &Apache::loncommon::authform_kerberos(%param);
599: my $intform = &Apache::loncommon::authform_internal(%param);
600: my $locform = &Apache::loncommon::authform_local(%param);
601: # Set up domain selection form
602: my $domform = &domain_form($defdom);
603: # Print it all out
604: $r->print(<<ENDSENROLL);
605: <script type="text/javascript" language="Javascript">
606: function verify(vf) {
607: var founduname=0;
608: var foundpwd=0;
609: var foundname=0;
610: var foundid=0;
611: var foundsec=0;
612: var tw;
613: if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') &&
614: (typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
615: founduname=1;
616: }
617: if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
618: (typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
619: foundname=1;
620: }
621: if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
622: foundsec=1;
623: }
624: if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
625: foundid=1;
626: }
627: if (founduname==0) {
628: alert('You need to specify at least the username and domain fields');
629: return;
630: }
631: verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
632: }
633:
634: $javascript_validations
635:
636: function clearpwd(vf) {
637: //nothing else needs clearing
638: }
639:
640: </script>
641: <h3>Personal Data</h3>
642: First Name: <input type="text" name="cfirst" size="15"><br>
643: Middle Name: <input type="text" name="cmiddle" size="15"><br>
644: Last Name: <input type="text" name="clast" size="15"><br>
645: Generation: <input type="text" name="cgen" size="5">
646:
647: <p>ID/Student Number: <input type="text" name="cstid" size="10"></p>
648:
649: <p>Group/Section: <input type=text name=csec size=5></p>
650:
651: <h3>Login Data</h3>
652: <p>Username: <input type="text" name="cuname" size="15"></p>
653: <p>Domain: $domform</p>
654: <p>Note: login settings below will not take effect if the user already exists
655: </p><p>
656: $krbform
657: </p><p>
658: $intform
659: </p><p>
660: $locform
661: </p><p>
662: <h3>Starting and Ending Dates</h3>
663: <input type="hidden" value='' name="pres_value">
664: <input type="hidden" value='' name="pres_type">
665: <input type="hidden" value='' name="pres_marker">
666: <input type="hidden" value='$today' name=startdate>
667: <input type="hidden" value='$halfyear' name=enddate>
668: </p><p>
669: <a
670: href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
671: >Set Starting Date</a>
672: </p><p>
673: <a
674: href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
675: >Set Ending Date</a>
676: </p><p>
677: <h3>ID/Student Number</h3>
678: <input type="checkbox" name="forceid" value="yes">
679: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
680: (only do if you know what you are doing)<p>
681: <input type="button" onClick="verify(this.form)" value="Enroll as student"><br>
682: <input type="hidden" name="phase" value="five">
683: </p>
684: ENDSENROLL
685: }
686:
687: # =================================================== get the current classlist
688: sub get_current_classlist {
689: my ($domain,$identifier) = @_;
690: # domain is the domain the class is being run in
691: # identifier is the internal, unique identifier for the class.
692: my %currentlist=();
693: my $now=time;
694: my %results=&Apache::lonnet::dump('classlist',$domain,$identifier);
695: my ($tmp) = keys(%results);
696: if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
697: foreach my $student (keys(%results)) {
698: # Extract the start and end dates
699: my ($end,$start)=split(/\:/,$results{$student});
700: # If the class isn't over, put it in the list
701: unless (($end) && ($now>$end)) {
702: $currentlist{$student}=1;
703: }
704: }
705: return (undef,%currentlist);
706: } else {
707: $tmp =~ s/^error://;
708: return ($tmp,undef);
709: }
710: }
711:
712: # ========================================================= Menu Phase Two Drop
713: sub menu_phase_two_drop {
714: my $r=shift;
715: $r->print("<h3>Drop Students</h3>");
716: my $cid=$ENV{'request.course.id'};
717: my ($error,%currentlist)=&get_current_classlist
718: ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
719: if (defined($error)) {
720: if ($error =~ /^No such file or directory/) {
721: $r->print("There are no students currently enrolled.\n");
722: } else {
723: $r->print("<pre>ERROR:$error</pre>");
724: }
725: } elsif (!defined(%currentlist)) {
726: $r->print("There are no students currently enrolled.\n");
727: } else {
728: # Print out the available choices
729: &show_drop_list($r,%currentlist);
730: }
731: }
732:
733: # ============================================== view classlist
734: sub menu_phase_two_view {
735: my $r=shift;
736: $r->print("<h3>Current Classlist</h3>");
737: my $cid=$ENV{'request.course.id'};
738: my ($error,%currentlist)=&get_current_classlist
739: ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
740: if (defined($error)) {
741: if ($error =~ /^No such file or directory/) {
742: $r->print("There are no students currently enrolled.\n");
743: } else {
744: $r->print("<pre>ERROR:$error</pre>");
745: }
746: } elsif (!defined(%currentlist)) {
747: $r->print("There are no students currently enrolled.\n");
748: } else {
749: # Print out the available choices
750: &show_class_list($r,'view',%currentlist);
751: }
752: }
753:
754: # ============================================== view classlist
755: sub menu_phase_two_viewcsv {
756: my $r=shift;
757: my $cid=$ENV{'request.course.id'};
758: my ($error,%currentlist)=&get_current_classlist
759: ($ENV{'course.'.$cid.'.domain'},$ENV{'course.'.$cid.'.num'});
760: if (defined($error)) {
761: if ($error =~ /^No such file or directory/) {
762: $r->print("There are no students currently enrolled.\n");
763: } else {
764: $r->print("<pre>ERROR:$error</pre>");
765: }
766: } elsif (!defined(%currentlist)) {
767: $r->print("There are no students currently enrolled.\n");
768: } else {
769: &show_class_list($r,'csv',%currentlist);
770: }
771: }
772:
773: # =================================================== Show student list to drop
774: sub show_class_list {
775: my ($r,$mode,%currentlist)=@_;
776: my $cid=$ENV{'request.course.id'};
777: # Print out header
778: if ($mode eq 'view') {
779: $r->print(<<END);
780: <p>
781: <table border=2>
782: <tr><th>username</th><th>domain</th><th>ID</th>
783: <th>student name</th><th>generation</th><th>section</th></tr>
784: END
785: } elsif ($mode eq 'csv') {
786: $r->print(<<END);
787: username,domain,ID,last name,first name,middle name,generation,section
788: END
789: }
790: foreach (sort keys %currentlist) {
791: my ($sname,$sdom)=split(/\:/,$_);
792: my %reply=&Apache::lonnet::idrget($sdom,$sname);
793: my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
794: my %info=&Apache::lonnet::get('environment',
795: ['firstname','middlename',
796: 'lastname','generation'],
797: $sdom, $sname);
798: my ($tmp) = keys(%info);
799: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
800: $r->print( ($mode eq 'view' ?
801: '<tr><td colspan="6"><font color="red">' :'').
802: 'Internal error: unable to get environment '.
803: 'for '.$sname.' in domain '.$sdom.
804: ( $mode eq 'view' ?'</font></td></tr>' :''));
805: } else {
806: if ($mode eq 'view') {
807: $r->print(<<"END");
808: <tr>
809: <td>$sname</td>
810: <td>$sdom</td>
811: <td>$reply{$sname}</td>
812: <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
813: <td>$info{'generation'}</td>
814: <td>$ssec</td>
815: </tr>
816: END
817: } elsif ($mode eq 'csv') {
818: my @line = ();
819: foreach ($sname,$sdom,$reply{$sname},
820: $info{'lastname'},$info{'firstname'},
821: $info{'middlename'},$info{'generation'},$ssec) {
822: push @line,&Apache::loncommon::csv_translate($_);
823: }
824: my $tmp = $";
825: $" = '","';
826: $r->print("\"@line\"\n");
827: $" = $tmp;
828: }
829: }
830: }
831: $r->print('</table><br>') if ($mode eq 'view');
832: }
833:
834: # =================================================== Show student list to drop
835: sub show_drop_list {
836: my ($r,%currentlist)=@_;
837: my $cid=$ENV{'request.course.id'};
838: $r->print(<<'END');
839: <script>
840: function checkAll(field)
841: {
842: for (i = 0; i < field.length; i++)
843: field[i].checked = true ;
844: }
845:
846: function uncheckAll(field)
847: {
848: for (i = 0; i < field.length; i++)
849: field[i].checked = false ;
850: }
851: </script>
852: <p>
853: <input type="hidden" name="phase" value="four">
854: <table border=2>
855: <tr><th> </th><th>username</th><th>domain</th>
856: <th>ID</th><th>student name</th><th>generation</th>
857: <th>section</th></tr>
858: END
859: foreach (sort keys %currentlist) {
860: my ($sname,$sdom)=split(/\:/,$_);
861: my %reply=&Apache::lonnet::idrget($sdom,$sname);
862: my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
863: my %info=&Apache::lonnet::get('environment',
864: ['firstname','middlename',
865: 'lastname','generation'],
866: $sdom, $sname);
867: my ($tmp) = keys(%info);
868: if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
869: $r->print('<tr><td colspan="7"><font color="red">'.
870: 'Internal error: unable to get environment '.
871: 'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
872: } else {
873: $r->print(<<"END");
874: <tr>
875: <td><input type="checkbox" name="droplist" value="$_"></td>
876: <td>$sname</td>
877: <td>$sdom</td>
878: <td>$reply{$sname}</td>
879: <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
880: <td>$info{'generation'}</td>
881: <td>$ssec</td>
882: </tr>
883: END
884: }
885: }
886: $r->print('</table><br>');
887: $r->print(<<"END");
888: </p><p>
889: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)">
890: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)">
891: <p><input type=submit value="Drop Students"></p>
892: END
893: }
894:
895: # ================================================= Drop/Add from uploaded file
896: sub upfile_drop_add {
897: my $r=shift;
898: &Apache::loncommon::load_tmp_file($r);
899: my @studentdata=&Apache::loncommon::upfile_record_sep();
900: my @keyfields = split(/\,/,$ENV{'form.keyfields'});
901: my $cid = $ENV{'request.course.id'};
902: my %fields=();
903: for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
904: if ($ENV{'form.upfile_associate'} eq 'reverse') {
905: if ($ENV{'form.f'.$i} ne 'none') {
906: $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
907: }
908: } else {
909: $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
910: }
911: }
912: #
913: my $startdate = $ENV{'form.startdate'};
914: my $enddate = $ENV{'form.enddate'};
915: if ($startdate=~/\D/) { $startdate=''; }
916: if ($enddate=~/\D/) { $enddate=''; }
917: # Determine domain and desired host (home server)
918: my $domain=$ENV{'form.lcdomain'};
919: my $desiredhost = $ENV{'form.lcserver'};
920: if (lc($desiredhost) eq 'default') {
921: $desiredhost = undef;
922: } else {
923: my %home_servers = &Apache::loncommon::get_library_servers($domain);
924: if (! exists($home_servers{$desiredhost})) {
925: $r->print('<font color="#ff0000">Error:</font>'.
926: 'Invalid home server specified');
927: return;
928: }
929: }
930: # Determine authentication mechanism
931: my $amode = '';
932: my $genpwd = '';
933: if ($ENV{'form.login'} eq 'krb') {
934: $amode='krb';
935: $amode.=$ENV{'form.krbver'};
936: $genpwd=$ENV{'form.krbarg'};
937: } elsif ($ENV{'form.login'} eq 'int') {
938: $amode='internal';
939: if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
940: $genpwd=$ENV{'form.intarg'};
941: }
942: } elsif ($ENV{'form.login'} eq 'loc') {
943: $amode='localauth';
944: if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
945: $genpwd=$ENV{'form.locarg'};
946: }
947: }
948: unless (($domain=~/\W/) || ($amode eq '')) {
949: #######################################
950: ## Enroll Students ##
951: #######################################
952: $r->print('<h3>Enrolling Students</h3>');
953: my $count=0;
954: my $flushc=0;
955: my %student=();
956: # Get new classlist
957: foreach (@studentdata) {
958: my %entries=&Apache::loncommon::record_sep($_);
959: # Determine student name
960: unless (($entries{$fields{'username'}} eq '') ||
961: (!defined($entries{$fields{'username'}}))) {
962: my ($fname, $mname, $lname,$gen) = ('','','','');
963: if (defined($fields{'names'})) {
964: ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
965: /([^\,]+)\,\s*(\w+)\s*(.*)$/);
966: } else {
967: if (defined($fields{'fname'})) {
968: $fname=$entries{$fields{'fname'}};
969: }
970: if (defined($fields{'mname'})) {
971: $mname=$entries{$fields{'mname'}};
972: }
973: if (defined($fields{'lname'})) {
974: $lname=$entries{$fields{'lname'}};
975: }
976: if (defined($fields{'gen'})) {
977: $gen=$entries{$fields{'gen'}};
978: }
979: }
980: if ($entries{$fields{'username'}}=~/\W/) {
981: $r->print('<p><b>Unacceptable username: '.
982: $entries{$fields{'username'}}.' for user '.
983: $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
984: } else {
985: # determine section number
986: my $sec='';
987: my $username=$entries{$fields{'username'}};
988: if (defined($fields{'sec'})) {
989: if (defined($entries{$fields{'sec'}})) {
990: $sec=$entries{$fields{'sec'}};
991: }
992: }
993: # determine student id number
994: my $id='';
995: if (defined($fields{'id'})) {
996: if (defined($entries{$fields{'id'}})) {
997: $id=$entries{$fields{'id'}};
998: }
999: $id=~tr/A-Z/a-z/;
1000: }
1001: # determine student password
1002: my $password='';
1003: if ($genpwd) {
1004: $password=$genpwd;
1005: } else {
1006: if (defined($fields{'ipwd'})) {
1007: if ($entries{$fields{'ipwd'}}) {
1008: $password=$entries{$fields{'ipwd'}};
1009: }
1010: }
1011: }
1012: if ($password) {
1013: &modifystudent($domain,$username,$cid,$sec,
1014: $desiredhost);
1015: my $reply=&Apache::lonnet::modifystudent
1016: ($domain,$username,$id,$amode,$password,
1017: $fname,$mname,$lname,$gen,$sec,$enddate,
1018: $startdate,$ENV{'form.forceid'},$desiredhost);
1019: if ($reply ne 'ok') {
1020: $r->print('<p><b>'.
1021: 'Error enrolling '.$username.': '.
1022: $reply.'</b></p>');
1023: } else {
1024: $count++; $flushc++;
1025: $student{$username}=1;
1026: $r->print('. ');
1027: if ($flushc>15) {
1028: $r->rflush;
1029: $flushc=0;
1030: }
1031: }
1032: } else {
1033: $r->print("<p><b>No password for $username</b><p>");
1034: }
1035: }
1036: }
1037: } # end of foreach (@studentdata)
1038: $r->print('<p>Processed Students: '.$count);
1039: #####################################
1040: # Drop students #
1041: #####################################
1042: if ($ENV{'form.fullup'} eq 'yes') {
1043: $r->print('<h3>Dropping Students</h3>');
1044: # Get current classlist
1045: my ($error,%currentlist)=&get_current_classlist
1046: ($ENV{'course.'.$cid.'.domain'},
1047: $ENV{'course.'.$cid.'.num'});
1048: if (defined($error)) {
1049: $r->print('<pre>ERROR:$error</pre>');
1050: }
1051: if (defined(%currentlist)) {
1052: # Drop the students
1053: foreach (@studentdata) {
1054: my %entries=&Apache::loncommon::record_sep($_);
1055: unless (($entries{$fields{'username'}} eq '') ||
1056: (!defined($entries{$fields{'username'}}))) {
1057: delete($currentlist{$entries{$fields{'username'}}.
1058: ':'.$domain});
1059: }
1060: }
1061: # Print out list of dropped students
1062: &show_drop_list($r,%currentlist);
1063: } else {
1064: $r->print("There are no students currently enrolled.\n");
1065: }
1066: }
1067: } # end of unless
1068: }
1069:
1070: # ================================================================== Phase four
1071: sub drop_student_list {
1072: my $r=shift;
1073: my $count=0;
1074: my @droplist;
1075: if (ref($ENV{'form.droplist'})) {
1076: @droplist = @{$ENV{'form.droplist'}};
1077: } else {
1078: @droplist = ($ENV{'form.droplist'});
1079: }
1080: foreach (@droplist) {
1081: my ($uname,$udom)=split(/\:/,$_);
1082: my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
1083: if ($result eq 'ok' || $result eq 'ok:') {
1084: $r->print('Dropped '.$uname.' at '.$udom.'<br>');
1085: } else {
1086: $r->print('Error dropping '.$uname.' at '.$udom.': '.$result.
1087: '<br />');
1088: }
1089: $count++;
1090: }
1091: $r->print('<p><b>Dropped '.$count.' student(s).</b>');
1092: $r->print('<p>Re-enrollment will re-activate data.');
1093: }
1094:
1095: # ================================================================ Main Handler
1096: sub handler {
1097: my $r=shift;
1098: if ($r->header_only) {
1099: $r->content_type('text/html');
1100: $r->send_http_header;
1101: return OK;
1102: }
1103: # Needs to be in a course
1104: if (($ENV{'request.course.fn'}) &&
1105: (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'}))) {
1106: # Start page
1107: $r->content_type('text/html') if (! exists($ENV{'form.viewcsv'}));
1108: $r->send_http_header;
1109: $r->print(&header()) if (! exists($ENV{'form.viewcsv'}));
1110: # Phase one, initial screen
1111: unless ($ENV{'form.phase'}) {
1112: &menu_phase_one($r);
1113: }
1114: # Phase two
1115: if ($ENV{'form.associate'} eq 'Reverse Association') {
1116: $ENV{'form.phase'} = 'two';
1117: if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
1118: $ENV{'form.upfile_associate'} = 'reverse';
1119: } else {
1120: $ENV{'form.upfile_associate'} = 'forward';
1121: }
1122: }
1123: if ($ENV{'form.phase'} eq 'two') {
1124: if ($ENV{'form.fileupload'}) {
1125: &menu_phase_two_upload($r);
1126: } elsif ($ENV{'form.enroll'}) {
1127: &menu_phase_two_enroll($r);
1128: } elsif ($ENV{'form.drop'}) {
1129: &menu_phase_two_drop($r);
1130: } elsif ($ENV{'form.view'}) {
1131: &menu_phase_two_view($r);
1132: } elsif ($ENV{'form.viewcsv'}) {
1133: &menu_phase_two_viewcsv($r);
1134: }
1135: }
1136: # Phase three
1137: if ($ENV{'form.phase'} eq 'three') {
1138: if ($ENV{'form.datatoken'}) {
1139: &upfile_drop_add($r);
1140: }
1141: }
1142: # Phase four
1143: if ($ENV{'form.phase'} eq 'four') {
1144: &drop_student_list($r);
1145: }
1146: # Phase five
1147: if ($ENV{'form.phase'} eq 'five') {
1148: &enroll_single_student($r);
1149: }
1150: # End
1151: $r->print('</form></body></html>') if (! exists($ENV{'form.viewcsv'}));
1152: } else {
1153: # Not in a course, or not allowed to modify parms
1154: $ENV{'user.error.msg'}=
1155: "/adm/dropadd:cst:0:0:Cannot drop or add students";
1156: return HTTP_NOT_ACCEPTABLE;
1157: }
1158: return OK;
1159: }
1160:
1161: 1;
1162: __END__
1163:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>