Annotation of loncom/interface/londropadd.pm, revision 1.27

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>