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