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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to drop and add students in courses 
                      3: #
1.28    ! matthew     4: # $Id: londropadd.pm,v 1.27 2002/04/17 13:55:35 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.28    ! matthew   169:     if (vf.login.value == null) {
        !           170:         // They did not check any of the login radiobuttons.
        !           171:         alert('You must choose an authentication type');
        !           172:         return;
        !           173:     }
        !           174:     foundatype=1;
        !           175:     if (vf.login.argfield.value == '') {
        !           176:         var alertmsg = '';
        !           177:         switch (vf.login.value) {
        !           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.23      albertel  385:     $r->print(<<ENDPICK);
1.3       www       386: </table>
1.10      www       387: <input type=hidden name=nfields value=$i>
                    388: <input type=hidden name=keyfields value="$keyfields">
1.3       www       389: <h3>Login Type</h3>
1.15      albertel  390: <p>Note: this will not take effect if the user already exists</p>
                    391: <p>
1.28    ! matthew   392: $krbform
1.15      albertel  393: </p>
                    394: <p>
1.28    ! matthew   395: $intform
1.15      albertel  396: </p>
                    397: <p>
1.28    ! matthew   398: $locform
1.15      albertel  399: </p>
1.5       www       400: <h3>LON-CAPA Domain for Students</h3>
1.4       www       401: LON-CAPA domain: <input type=text size=10 value=$defdom name=lcdomain><p>
1.5       www       402: <h3>Starting and Ending Dates</h3>
1.26      matthew   403: <input type="hidden" value=''          name="pres_value"  >
                    404: <input type="hidden" value=''          name="pres_type"   >
                    405: <input type="hidden" value=''          name="pres_marker" >
                    406: <input type="hidden" value='$today'    name="startdate"   >
                    407: <input type="hidden" value='$halfyear' name="enddate"     >
1.4       www       408: <a 
                    409:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
                    410: >Set Starting Date</a><p>
                    411: 
                    412: <a 
                    413:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
                    414: >Set Ending Date</a><p>
1.5       www       415: <h3>Full Update</h3>
                    416: <input type=checkbox name=fullup value=yes> Full update 
1.11      www       417: (also print list of users not enrolled anymore)<p>
1.18      www       418: <h3>ID/Student Number</h3>
                    419: <input type=checkbox name=forceid value=yes> 
                    420: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.19      www       421: (only do if you know what you are doing)<p>
1.26      matthew   422: <input type="button" onClick="verify(this.form)" value="Update Courselist"><br>
1.6       www       423: Note: for large courses, this operation might be time consuming.
1.3       www       424: ENDPICK
1.23      albertel  425: }
1.24      albertel  426: 
1.23      albertel  427: # ======================================================= Menu Phase Two Upload
                    428: sub menu_phase_two_upload {
                    429:     my $r=shift;
1.26      matthew   430: 
1.24      albertel  431:     my $datatoken;
                    432:     if (!$ENV{'form.datatoken'}) {
1.26      matthew   433:       $datatoken=&Apache::loncommon::upfile_store($r);
1.24      albertel  434:     } else {
1.26      matthew   435:       $datatoken=$ENV{'form.datatoken'};
                    436:       &Apache::loncommon::load_tmp_file($r);
1.24      albertel  437:     }
                    438:     my @records=&Apache::loncommon::upfile_record_sep();
1.23      albertel  439:     my $total=$#records;
                    440:     my $distotal=$total+1;
                    441:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                    442:     my $krbdefdom=$1;
                    443:     $krbdefdom=~tr/a-z/A-Z/;
                    444:     my $today=time;
                    445:     my $halfyear=$today+15552000;
                    446:     my $defdom=$r->dir_config('lonDefDomain');
                    447:     &phase_two_header($r,$datatoken,$distotal,$krbdefdom);
1.24      albertel  448:     my $i;
                    449:     my $keyfields;
1.23      albertel  450:     if ($total>=0) {
1.24      albertel  451: 	my @d=(['username','Username'],['names','Last Name, First Names'],
                    452: 	       ['fname','First Name'],['mname','Middle Names/Initials'],
                    453: 	       ['lname','Last Name'],['gen','Generation'],
                    454: 	       ['id','ID/Student Number'],['sec','Group/Section'],
                    455: 	       ['ipwd','Initial Password']);
                    456: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    457: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                    458: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
                    459: 	    foreach (@d) { $keyfields.=$_->[0].','; }
                    460: 	    chop($keyfields);
                    461: 	} else {
                    462: 	    unshift(@d,['none','']);
                    463: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
                    464: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                    465: 	    $keyfields=join(',',sort(keys(%sone)));
1.23      albertel  466: 	}
                    467:     }
                    468:     &phase_two_end($r,$i,$keyfields,$defdom,$today,$halfyear);
1.10      www       469: }
                    470: 
1.12      www       471: # ======================================================= Enroll single student
                    472: sub enroll_single_student {
                    473:     my $r=shift;
                    474:     $r->print('<h3>Enrolling Student</h3>');
                    475:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
                    476:         ($ENV{'form.cdomain'})&&($ENV{'form.cdomain'}!~/\W/)) {
                    477: 	my $amode='';
                    478:         my $genpwd='';
                    479:         if ($ENV{'form.login'} eq 'krb') {
1.26      matthew   480:            $amode='krb4';
1.28    ! matthew   481:            $genpwd=$ENV{'form.krbarg'};
1.12      www       482:         } elsif ($ENV{'form.login'} eq 'int') {
1.26      matthew   483:            $amode='internal';
1.28    ! matthew   484:            $genpwd=$ENV{'form.intarg'};
1.15      albertel  485:         }  elsif ($ENV{'form.login'} eq 'loc') {
                    486: 	    $amode='localauth';
                    487: 	    $genpwd=$ENV{'form.locarg'};
                    488: 	    if (!$genpwd) { $genpwd=" "; }
                    489: 	}
1.12      www       490:         if (($amode) && ($genpwd)) {
1.26      matthew   491:             &modifystudent($ENV{'form.cdomain'},$ENV{'form.cuname'},
                    492:                            $ENV{'request.course.id'},$ENV{'form.csec'});
                    493:           $r->print(&Apache::lonnet::modifystudent(
                    494:                       $ENV{'form.cdomain'},$ENV{'form.cuname'},
                    495:                       $ENV{'form.cstid'},$amode,$genpwd,
                    496:  	              $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
                    497:                       $ENV{'form.clast'},$ENV{'form.cgen'},
                    498:                       $ENV{'form.csec'},$ENV{'form.enddate'},
                    499:                       $ENV{'form.startdate'},$ENV{'form.forceid'}));
1.12      www       500: 	} else {
1.26      matthew   501:            $r->print('Invalid login mode or password');    
1.12      www       502:         }          
                    503:     } else {
                    504:         $r->print('Invalid username or domain');
1.26      matthew   505:     }    
1.12      www       506: }
                    507: 
1.10      www       508: # ======================================================= Menu Phase Two Enroll
                    509: sub menu_phase_two_enroll {
                    510:     my $r=shift;
1.26      matthew   511:     my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
1.11      www       512:     $krbdefdom=~tr/a-z/A-Z/;
1.26      matthew   513:     my $today    = time;
                    514:     my $halfyear = $today+15552000;
1.11      www       515:     my $defdom=$r->dir_config('lonDefDomain');
1.24      albertel  516:     my $javascript_validations=&javascript_validations($krbdefdom);
1.28    ! matthew   517:     # Set up authentication forms
        !           518:     my %param = ( formname => 'document.studentform');
        !           519:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
        !           520:     my $intform = &Apache::loncommon::authform_internal(%param);
        !           521:     my $locform = &Apache::loncommon::authform_local(%param);
        !           522:     # Print it all out
1.11      www       523:     $r->print(<<ENDSENROLL);
1.28    ! matthew   524: <script type="text/javascript" language="Javascript">
1.12      www       525: function verify(vf) {
                    526:     var founduname=0;
                    527:     var foundpwd=0;
                    528:     var foundname=0;
                    529:     var foundid=0;
                    530:     var foundsec=0;
                    531:     var tw;
1.26      matthew   532:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
1.14      harris41  533: 	(typeof(vf.cdomain.value)!="undefined") && (vf.cdomain.value!='')) {
1.12      www       534:         founduname=1;
                    535:     }
1.14      harris41  536:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
1.26      matthew   537: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
1.12      www       538:         foundname=1;
                    539:     }
1.14      harris41  540:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
1.12      www       541:         foundsec=1;
                    542:     }
1.14      harris41  543:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
1.12      www       544: 	foundid=1;
                    545:     }
                    546:     if (founduname==0) {
                    547: 	alert('You need to specify at least the username and domain fields');
                    548:         return;
                    549:     }
1.24      albertel  550:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
1.12      www       551: }
                    552: 
1.24      albertel  553: $javascript_validations
1.12      www       554: 
1.24      albertel  555: function clearpwd(vf) {
                    556:     //nothing else needs clearing
1.15      albertel  557: }
                    558: 
1.12      www       559: </script>
1.11      www       560: <h3>Personal Data</h3>
1.26      matthew   561: First Name:  <input type="text" name="cfirst"  size="15"><br>
                    562: Middle Name: <input type="text" name="cmiddle" size="15"><br>
                    563: Last Name:   <input type="text" name="clast"   size="15"><br>
                    564: Generation:  <input type="text" name="cgen"    size="5"> 
1.11      www       565: 
1.26      matthew   566: <p>ID/Student Number: <input type="text" name="cstid" size="10"></p>
1.11      www       567: 
1.26      matthew   568: <p>Group/Section: <input type=text name=csec size=5></p>
1.11      www       569: 
1.12      www       570: <h3>Login Data</h3>
1.26      matthew   571: <p>Username: <input type="text" name="cuname"  size="15"></p>
                    572: <p>Domain:   <input type="text" name="cdomain" size="10" value="$defdom"></p>
                    573: <p>Note: login settings below  will not take effect if the user already exists
                    574: </p><p>
1.28    ! matthew   575: $krbform
1.26      matthew   576: </p><p>
1.28    ! matthew   577: $intform
1.26      matthew   578: </p><p>
1.28    ! matthew   579: $locform
1.26      matthew   580: </p><p>
1.11      www       581: <h3>Starting and Ending Dates</h3>
                    582: <input type="hidden" value='' name="pres_value">
                    583: <input type="hidden" value='' name="pres_type">
                    584: <input type="hidden" value='' name="pres_marker">
                    585: <input type="hidden" value='$today' name=startdate>
                    586: <input type="hidden" value='$halfyear' name=enddate>
1.26      matthew   587: </p><p>
1.11      www       588: <a 
                    589:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
1.26      matthew   590: >Set Starting Date</a>
                    591: </p><p>
1.11      www       592: <a 
                    593:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
1.26      matthew   594: >Set Ending Date</a>
                    595: </p><p>
1.18      www       596: <h3>ID/Student Number</h3>
1.26      matthew   597: <input type="checkbox" name="forceid" value="yes"> 
1.18      www       598: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.19      www       599: (only do if you know what you are doing)<p>
1.26      matthew   600: <input type="button" onClick="verify(this.form)" value="Enroll as student"><br>
                    601: <input type="hidden" name="phase" value="five">
                    602: </p>
1.11      www       603: ENDSENROLL
1.10      www       604: }
                    605: 
1.26      matthew   606: # =================================================== get the current classlist
                    607: sub get_current_classlist {
                    608:     my ($domain,$identifier) = @_;
                    609:     # domain is the domain the class is being run in
                    610:     # identifier is the internal, unique identifier for the class.
                    611:     my %currentlist=();
                    612:     my $now=time;
                    613:     my %results=&Apache::lonnet::dump('classlist',$domain,$identifier);
                    614:     my ($tmp) = keys(%results);
                    615:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    616:         foreach my $student (keys(%results)) {
                    617:             # Extract the start and end dates
                    618:             my ($end,$start)=split(/\:/,$results{$student});
                    619:             # If the class isn't over, put it in the list
                    620:             unless (($end) && ($now>$end)) { 
                    621:                 $currentlist{$student}=1;
                    622:             }
                    623:         }
1.27      matthew   624:         return (undef,%currentlist);
1.26      matthew   625:     } else {
1.27      matthew   626:         return ($tmp,undef);
1.26      matthew   627:     }
                    628: }
                    629: 
1.10      www       630: # ========================================================= Menu Phase Two Drop
                    631: sub menu_phase_two_drop {
                    632:     my $r=shift;
1.11      www       633:     my $cid=$ENV{'request.course.id'};
1.27      matthew   634:     my ($error,%currentlist)=&get_current_classlist($ENV{'course.'.$cid.'.domain'},
1.26      matthew   635:                                            $ENV{'course.'.$cid.'.num'});
1.27      matthew   636:     if (defined($error)) {
                    637:         $r->print('<pre>ERROR:$error</pre>');
                    638:     }
1.26      matthew   639:     if (!defined(%currentlist)) { 
1.27      matthew   640:         $r->print("There are no students currently enrolled.\n");
1.26      matthew   641:     } else {
                    642:         # Print out the available choices
1.25      matthew   643:         &show_drop_list($r,%currentlist);
                    644:     }
1.11      www       645: }
                    646: 
                    647: # =================================================== Show student list to drop
                    648: sub show_drop_list {
                    649:     my ($r,%currentlist)=@_;
                    650:     my $cid=$ENV{'request.course.id'};
1.26      matthew   651:     $r->print(<<'END');
                    652: <input type="hidden" name="phase" value="four">
                    653: <table border=2>
                    654: <tr><th>&nbsp;</th><th>username</th><th>domain</th>
                    655: <th>ID</th><th>student name</th><th>generation</th>
                    656: <th>section</th></tr>
                    657: END
1.25      matthew   658:     foreach (sort keys %currentlist) {
                    659:         my ($sname,$sdom)=split(/\:/,$_);
                    660:         my %reply=&Apache::lonnet::idrget($sdom,$sname);
                    661:         my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
1.26      matthew   662:         my %info=&Apache::lonnet::get('environment',
                    663:                                       ['firstname','middlename',
                    664:                                        'lastname','generation'],
                    665:                                       $sdom, $sname);
                    666:         my ($tmp) = keys(%info);
                    667:         if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                    668:             $r->print('<tr><td colspan="7"><font color="red">'.
                    669:                       'Internal error: unable to get environment '.
                    670:                       'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
                    671:         } else {
                    672:             $r->print(<<"END");
                    673: <tr>
                    674:     <td><input type="checkbox" name="droplist" value="$_"></td>
                    675:     <td>$sname</td>
                    676:     <td>$sdom</td>
                    677:     <td>$reply{$sname}</td>
                    678:     <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
                    679:     <td>$info{'generation'}</td>
                    680:     <td>$ssec</td>
                    681: </tr>
                    682: END
                    683:         }
1.25      matthew   684:     }
                    685:     $r->print('</table><br>');
                    686:     $r->print('<input type=submit value="Drop Students">');
1.10      www       687: }
                    688: 
                    689: # ================================================= Drop/Add from uploaded file
                    690: sub upfile_drop_add {
                    691:     my $r=shift;
1.24      albertel  692:     &Apache::loncommon::load_tmp_file($r);
                    693:     my @studentdata=&Apache::loncommon::upfile_record_sep();
1.26      matthew   694:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                    695:     my $cid = $ENV{'request.course.id'};
1.25      matthew   696:     my %fields=();
1.26      matthew   697:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1.25      matthew   698:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
                    699:             if ($ENV{'form.f'.$i} ne 'none') {
                    700:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                    701:             }
                    702:         } else {
                    703:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                    704:         }
                    705:     }
1.26      matthew   706:     #
                    707:     my $startdate = $ENV{'form.startdate'};
                    708:     my $enddate   = $ENV{'form.enddate'};
1.25      matthew   709:     if ($startdate=~/\D/) { $startdate=''; }
1.26      matthew   710:     if ($enddate=~/\D/)   { $enddate=''; }
                    711:     #
1.25      matthew   712:     my $domain=$ENV{'form.lcdomain'};
1.26      matthew   713:     # Determine authentication mechanism
                    714:     my $amode  = '';
                    715:     my $genpwd = '';
1.25      matthew   716:     if ($ENV{'form.login'} eq 'krb') {
                    717:         $amode='krb4';
1.28    ! matthew   718:         $genpwd=$ENV{'form.krbarg'};
1.25      matthew   719:     } elsif ($ENV{'form.login'} eq 'int') {
                    720:         $amode='internal';
1.28    ! matthew   721:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
        !           722:             $genpwd=$ENV{'form.intarg'};
1.25      matthew   723:         }
                    724:     } elsif ($ENV{'form.login'} eq 'loc') {
                    725:         $amode='localauth';
                    726:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
                    727:             $genpwd=$ENV{'form.locarg'};
                    728:         }
                    729:     }
                    730:     unless (($domain=~/\W/) || ($amode eq '')) {
1.26      matthew   731:         #######################################
                    732:         ##         Enroll Students           ##
                    733:         #######################################
1.25      matthew   734:         $r->print('<h3>Enrolling Students</h3>');
                    735:         my $count=0;
                    736:         my $flushc=0;
                    737:         my %student=();
1.26      matthew   738:         # Get new classlist
1.25      matthew   739:         foreach (@studentdata) {
                    740:             my %entries=&Apache::loncommon::record_sep($_);
1.26      matthew   741:             # Determine student name
1.25      matthew   742:             unless (($entries{$fields{'username'}} eq '') ||
                    743:                     (!defined($entries{$fields{'username'}}))) {
1.26      matthew   744:                 my ($fname, $mname, $lname,$gen) = ('','','','');
1.25      matthew   745:                 if (defined($fields{'names'})) {
1.26      matthew   746:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                    747:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1.25      matthew   748:                 } else {
                    749:                     if (defined($fields{'fname'})) {
                    750:                         $fname=$entries{$fields{'fname'}};
                    751:                     }
                    752:                     if (defined($fields{'mname'})) {
                    753:                         $mname=$entries{$fields{'mname'}};
                    754:                     }
                    755:                     if (defined($fields{'lname'})) {
                    756:                         $lname=$entries{$fields{'lname'}};
                    757:                     }
                    758:                     if (defined($fields{'gen'})) {
                    759:                         $gen=$entries{$fields{'gen'}};
                    760:                     }
                    761:                 }
                    762:                 if ($entries{$fields{'username'}}=~/\W/) {
                    763:                     $r->print('<p><b>Unacceptable username: '.
1.10      www       764:                               $entries{$fields{'username'}}.' for user '.
1.4       www       765:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
1.25      matthew   766:                 } else {
1.26      matthew   767:                     # determine section number
1.25      matthew   768:                     my $sec='';
                    769:                     my $username=$entries{$fields{'username'}};
                    770:                     if (defined($fields{'sec'})) {
                    771:                         if (defined($entries{$fields{'sec'}})) {
                    772:                             $sec=$entries{$fields{'sec'}};
                    773:                         }
                    774:                     }
1.26      matthew   775:                     # determine student id number
1.25      matthew   776:                     my $id='';
                    777:                     if (defined($fields{'id'})) {
                    778:                         if (defined($entries{$fields{'id'}})) {
                    779:                             $id=$entries{$fields{'id'}};
                    780:                         }
                    781:                         $id=~tr/A-Z/a-z/;
                    782:                     }
1.26      matthew   783:                     # determine student password
1.25      matthew   784:                     my $password='';
                    785:                     if ($genpwd) { 
                    786:                         $password=$genpwd; 
                    787:                     } else {
                    788:                         if (defined($fields{'ipwd'})) {
                    789:                             if ($entries{$fields{'ipwd'}}) {
                    790:                                 $password=$entries{$fields{'ipwd'}};
                    791:                             }
                    792:                         }
                    793:                     }
                    794:                     if ($password) {
1.26      matthew   795:                         &modifystudent($domain,$username,$cid,$sec);
1.25      matthew   796:                         my $reply=&Apache::lonnet::modifystudent
                    797:                             ($domain,$username,$id,$amode,$password,
                    798:                              $fname,$mname,$lname,$gen,$sec,$enddate,
                    799:                              $startdate,$ENV{'form.forceid'});
1.26      matthew   800:                         if ($reply ne 'ok') {
                    801:                             $r->print('<p><b>'.
                    802:                                       'Error enrolling '.$username.': '.
                    803:                                       $reply.'</b></p>');
1.10      www       804:          		} else {
1.7       www       805:                             $count++; $flushc++;
                    806:                             $student{$username}=1;
1.6       www       807:                             $r->print('. ');
1.7       www       808:                             if ($flushc>15) {
                    809: 				$r->rflush;
                    810:                                 $flushc=0;
                    811:                             }
1.6       www       812:                         }
1.25      matthew   813:                     } else {
                    814:                         $r->print("<p><b>No password for $username</b><p>");
                    815:                     }
                    816:                 }
1.26      matthew   817:             }
                    818:         } # end of foreach (@studentdata)
1.25      matthew   819:         $r->print('<p>Processed Students: '.$count);
1.26      matthew   820:         #####################################
                    821:         #           Drop students           #
                    822:         #####################################
1.25      matthew   823:         if ($ENV{'form.fullup'} eq 'yes') {
                    824:             $r->print('<h3>Dropping Students</h3>');
1.26      matthew   825:             #  Get current classlist
1.27      matthew   826:             my ($error,%currentlist)=&get_current_classlist
1.26      matthew   827:                 ($ENV{'course.'.$cid.'.domain'},
                    828:                  $ENV{'course.'.$cid.'.num'});
1.27      matthew   829:             if (defined($error)) {
                    830:                 $r->print('<pre>ERROR:$error</pre>');
                    831:             }
1.26      matthew   832:             if (defined(%currentlist)) {
                    833:                 # Drop the students
1.25      matthew   834:                 foreach (@studentdata) {
                    835:                     my %entries=&Apache::loncommon::record_sep($_);
                    836:                     unless (($entries{$fields{'username'}} eq '') ||
                    837:                             (!defined($entries{$fields{'username'}}))) {
1.26      matthew   838:                         delete($currentlist{$entries{$fields{'username'}}.
                    839:                                                 ':'.$domain});
1.25      matthew   840:                     }
                    841:                 }
1.26      matthew   842:                 # Print out list of dropped students
1.25      matthew   843:                 &show_drop_list($r,%currentlist);
                    844:             } else {
1.27      matthew   845:                 $r->print("There are no students currently enrolled.\n");
1.25      matthew   846:             }
                    847:         }
1.26      matthew   848:     } # end of unless
1.10      www       849: }
                    850: 
1.11      www       851: # ================================================================== Phase four
                    852: sub drop_student_list {
                    853:     my $r=shift;
                    854:     my $count=0;
1.26      matthew   855:     foreach (@{$ENV{'form.droplist'}}) {
                    856:         my ($uname,$udom)=split(/\:/,$_);
                    857:         &modifystudent($udom,$uname,$ENV{'request.course.id'});
                    858:         $r->print('Dropped '.$uname.' at '.$udom.'<br>');
                    859:         $count++;
1.20      harris41  860:     }
1.11      www       861:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
                    862:     $r->print('<p>Re-enrollment will re-activate data.');
                    863: }
                    864: 
1.10      www       865: # ================================================================ Main Handler
                    866: sub handler {
1.26      matthew   867:     my $r=shift;
                    868:     $Apache::lonxml::debug=1;
                    869:     if ($r->header_only) {
                    870:         $r->content_type('text/html');
                    871:         $r->send_http_header;
                    872:         return OK;
                    873:     }
                    874:     #  Needs to be in a course
                    875:     if (($ENV{'request.course.fn'}) && 
                    876:         (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'}))) {
                    877:         # Start page
                    878:         $r->content_type('text/html');
                    879:         $r->send_http_header;
1.27      matthew   880:         $r->print(&header());
1.26      matthew   881:         # Phase one, initial screen
                    882:         unless ($ENV{'form.phase'}) {
                    883:             &menu_phase_one($r);
                    884:         }
                    885:         # Phase two
                    886:         if ($ENV{'form.associate'} eq 'Reverse Association') {
                    887:             $ENV{'form.phase'} = 'two';
                    888:             if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    889:                 $ENV{'form.upfile_associate'} = 'reverse';
                    890:             } else {
                    891:                 $ENV{'form.upfile_associate'} = 'forward';
                    892:             }
                    893:         }
                    894:         if ($ENV{'form.phase'} eq 'two') {
                    895:             if ($ENV{'form.fileupload'}) {
                    896:                 &menu_phase_two_upload($r);
                    897:             } elsif ($ENV{'form.enroll'}) {
                    898:                 &menu_phase_two_enroll($r);
                    899:             } elsif ($ENV{'form.drop'}) {
                    900:                 &menu_phase_two_drop($r);
                    901:             }
                    902:         }
                    903:         # Phase three
                    904:         if ($ENV{'form.phase'} eq 'three') {
                    905:             if ($ENV{'form.datatoken'}) {
                    906:                 &upfile_drop_add($r);
                    907:             }
                    908:         }
                    909:         # Phase four
                    910:         if ($ENV{'form.phase'} eq 'four') {
                    911:             &drop_student_list($r);
                    912:         }
                    913:         # Phase five
                    914:         if ($ENV{'form.phase'} eq 'five') {
                    915:             &enroll_single_student($r);
                    916:         }
                    917:          # End
                    918:         $r->print('</form></body></html>');
                    919:     } else {
                    920:         # Not in a course, or not allowed to modify parms
                    921:         $ENV{'user.error.msg'}=
                    922:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
                    923:         return HTTP_NOT_ACCEPTABLE; 
                    924:     }
                    925:     return OK;
1.1       www       926: }
                    927: 
                    928: 1;
                    929: __END__
                    930: 

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