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

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

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