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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to drop and add students in courses 
                      3: #
1.62    ! matthew     4: # $Id: londropadd.pm,v 1.61 2002/11/13 19:16:34 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.50      matthew    34: ###############################################################
                     35: ###############################################################
1.1       www        36: 
                     37: package Apache::londropadd;
                     38: 
                     39: use strict;
1.24      albertel   40: use Apache::lonnet();
                     41: use Apache::loncommon();
1.50      matthew    42: use Apache::lonhtmlcommon();
1.1       www        43: use Apache::Constants qw(:common :http REDIRECT);
1.60      matthew    44: use Spreadsheet::WriteExcel;
1.1       www        45: 
1.50      matthew    46: ###############################################################
                     47: ###############################################################
1.10      www        48: sub header {
1.46      www        49:     my $bodytag=&Apache::loncommon::bodytag('Enrollment Manager');
1.27      matthew    50:     return(<<ENDHEAD);
1.1       www        51: <html>
                     52: <head>
1.40      matthew    53: <title>LON-CAPA Enrollment Manager</title>
1.1       www        54: </head>
1.46      www        55: $bodytag
1.40      matthew    56: <form method="post" enctype="multipart/form-data"  
                     57:       action="/adm/dropadd" name="studentform">
1.1       www        58: ENDHEAD
1.10      www        59: }
                     60: 
1.50      matthew    61: ###############################################################
                     62: ###############################################################
                     63: # Drop student from all sections of a course, except optional $csec
1.26      matthew    64: sub modifystudent {
1.33      matthew    65:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
1.26      matthew    66:     # if $csec is undefined, drop the student from all the courses matching
                     67:     # this one.  If $csec is defined, drop them from all other sections of 
                     68:     # this course and add them to section $csec
1.25      matthew    69:     $courseid=~s/\_/\//g;
                     70:     $courseid=~s/^(\w)/\/$1/;
1.26      matthew    71:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     72:     my ($tmp) = keys(%roles);
                     73:     # Bail out if we were unable to get the students roles
1.35      matthew    74:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
1.26      matthew    75:     # Go through the roles looking for enrollment in this course
1.35      matthew    76:     my $result = '';
1.26      matthew    77:     foreach my $course (keys(%roles)) {
1.35      matthew    78:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
1.26      matthew    79:             # We are in this course
1.25      matthew    80:             my $section=$1;
1.26      matthew    81:             $section='' if ($course eq $courseid.'_st');
1.35      matthew    82:             if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
1.27      matthew    83:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
1.25      matthew    84:                 my $now=time;
1.50      matthew    85:                 # if this is an active role 
1.27      matthew    86:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
1.25      matthew    87:                     my $reply=&Apache::lonnet::modifystudent
1.33      matthew    88:                         ($udom,$unam,'','','','','','','',
                     89:                          $section,time,undef,undef,$desiredhost);
1.35      matthew    90:                     $result .= $reply.':';
1.25      matthew    91:                 }
1.10      www        92:             }
                     93:         }
1.20      harris41   94:     }
1.35      matthew    95:     if ($result eq '') {
1.62    ! matthew    96:         $result = 'Unable to find section for this student';
1.37      matthew    97:     } else {
                     98:         $result =~ s/(ok:)+/ok/g;
1.35      matthew    99:     }
                    100:     return $result;
1.10      www       101: }
                    102: 
1.50      matthew   103: ###############################################################
                    104: ###############################################################
                    105: # build a domain and server selection form
1.31      matthew   106: sub domain_form {
                    107:     my ($defdom) = @_;
                    108:     # Set up domain and server selection forms
                    109:     #
                    110:     # Get the domains
                    111:     my @domains = &Apache::loncommon::get_domains();
                    112:     # build up the menu information to be passed to 
                    113:     # &Apache::loncommon::linked_select_forms
                    114:     my %select_menus;
                    115:     foreach my $dom (@domains) {
                    116:         # set up the text for this domain
                    117:         $select_menus{$dom}->{'text'}= $dom;
                    118:         # we want a choice of 'default' as the default in the second menu
                    119:         $select_menus{$dom}->{'default'}= 'default';
                    120:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
                    121:         # Now build up the other items in the second menu
1.45      matthew   122:         my %servers = &Apache::loncommon::get_library_servers($dom);
1.31      matthew   123:         foreach my $server (keys(%servers)) {
                    124:             $select_menus{$dom}->{'select2'}->{$server} 
                    125:                                             = "$server $servers{$server}";
                    126:         }
                    127:     }
                    128:     my $result  = &Apache::loncommon::linked_select_forms
                    129:         ('studentform',' with home server ',$defdom,
                    130:          'lcdomain','lcserver',\%select_menus);
                    131:     return $result;
                    132: }
                    133: 
1.50      matthew   134: ###############################################################
                    135: ###############################################################
                    136: #  Menu Phase One
                    137: sub print_main_menu {
1.10      www       138:     my $r=shift;
1.48      matthew   139:     $r->print(<<END);
                    140: <p>
1.50      matthew   141: <font size="+1">
                    142:     <a href="/adm/dropadd?action=upload">Upload a course list</a>
                    143: </font>
                    144: </p><p>
                    145: <font size="+1">
                    146:     <a href="/adm/dropadd?action=enrollstudent">Enroll a single student</a>
                    147: </font>
1.48      matthew   148: </p><p>
1.50      matthew   149: <font size="+1">
                    150:     <a href="/adm/dropadd?action=modifystudent">Modify student data</a>
                    151: </font>
1.48      matthew   152: </p><p>
1.50      matthew   153: <font size="+1">
                    154:     <a href="/adm/dropadd?action=classlist">View Classlist</a>
                    155: </font>
1.48      matthew   156: </p><p>
1.50      matthew   157: <font size="+1">
                    158:     <a href="/adm/dropadd?action=drop">Drop Students</a>
                    159: </font>
1.48      matthew   160: </p>
                    161: END
1.10      www       162: }
                    163: 
1.50      matthew   164: ###############################################################
                    165: ###############################################################
                    166: sub print_upload_manager_header {
1.23      albertel  167:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
1.24      albertel  168:     my $javascript;
1.50      matthew   169:     if (! exists($ENV{'form.upfile_associate'})) {
                    170:         $ENV{'form.upfile_associate'} = 'forward';
                    171:     }
                    172:     if ($ENV{'form.associate'} eq 'Reverse Association') {
                    173:         if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
                    174:             $ENV{'form.upfile_associate'} = 'reverse';
                    175:         } else {
                    176:             $ENV{'form.upfile_associate'} = 'forward';
                    177:         }
                    178:     }
1.24      albertel  179:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
1.50      matthew   180: 	$javascript=&upload_manager_javascript_reverse_associate();
1.24      albertel  181:     } else {
1.50      matthew   182: 	$javascript=&upload_manager_javascript_forward_associate();
1.24      albertel  183:     }
                    184:     my $javascript_validations=&javascript_validations($krbdefdom);
1.10      www       185:     $r->print(<<ENDPICK);
1.40      matthew   186: <h3>Uploading Class List</h3>
1.2       www       187: <hr>
                    188: <h3>Identify fields</h3>
1.22      albertel  189: Total number of records found in file: $distotal <hr />
                    190: Enter as many fields as you can. The system will inform you and bring you back
                    191: to this page if the data selected is insufficient to run your class.<hr />
1.36      albertel  192: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1.50      matthew   193: <input type="hidden" name="action"     value="upload" />
                    194: <input type="hidden" name="state"      value="got_file" />
1.36      albertel  195: <input type="hidden" name="associate"  value="" />
1.26      matthew   196: <input type="hidden" name="datatoken"  value="$datatoken" />
1.24      albertel  197: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
                    198: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
1.26      matthew   199: <input type="hidden" name="upfile_associate" 
                    200:                                        value="$ENV{'form.upfile_associate'}" />
1.24      albertel  201: <hr />
1.28      matthew   202: <script type="text/javascript" language="Javascript">
1.24      albertel  203: $javascript
                    204: $javascript_validations
                    205: </script>
                    206: ENDPICK
                    207: }
                    208: 
1.50      matthew   209: ###############################################################
                    210: ###############################################################
1.24      albertel  211: sub javascript_validations {
                    212:     my ($krbdefdom)=@_;
1.28      matthew   213:     my %param = ( formname => 'studentform',
                    214:                   kerb_def_dom => $krbdefdom );
                    215:     my $authheader = &Apache::loncommon::authform_header(%param);
1.24      albertel  216:     return (<<ENDPICK);
                    217: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
1.3       www       218:     var foundatype=0;
                    219:     var message='';
                    220:     if (founduname==0) {
1.28      matthew   221: 	alert('You need to specify the username field');
1.3       www       222:         return;
                    223:     }
1.61      matthew   224:     // alert('current.radiovalue = '+current.radiovalue);
1.29      matthew   225:     if (current.radiovalue == null || current.radiovalue == 'nochange') {
1.28      matthew   226:         // They did not check any of the login radiobuttons.
                    227:         alert('You must choose an authentication type');
                    228:         return;
                    229:     }
                    230:     foundatype=1;
1.29      matthew   231:     if (current.argfield == null || current.argfield == '') {
1.28      matthew   232:         var alertmsg = '';
1.29      matthew   233:         switch (current.value) {
1.28      matthew   234:             case 'krb': 
                    235:                 alertmsg = 'You need to specify the Kerberos domain';
                    236:                 break;
                    237:             case 'loc':
                    238:             case 'fsys':
                    239:                 alertmsg = 'You need to specify the initial password';
                    240:                 break;
                    241:             case 'fsys':
                    242:                 alertmsg = '';
                    243:                 break;
                    244:             default: 
                    245:                 alertmsg = '';
1.3       www       246:         }
1.28      matthew   247:         if (alertmsg != '') {
                    248:             alert(alertmsg);
1.3       www       249:             return;
                    250:         }
                    251:     }
1.28      matthew   252: 
1.3       www       253:     if (foundname==0) { message='No name fields specified. '; }
                    254:     if (foundid==0) { message+='No ID or student number field specified. '; }
                    255:     if (foundsec==0) { message+='No section or group field specified. '; }
1.4       www       256:     if (vf.startdate.value=='') {
                    257: 	message+='No starting date set. ';
1.3       www       258:     }
1.4       www       259:     if (vf.enddate.value=='') {
                    260:         message+='No ending date set. ';
                    261:     }
                    262:     if ((vf.enddate.value!='') && (vf.startdate.value!='')) {
1.10      www       263:        if (Math.round(vf.enddate.value)<Math.round(vf.startdate.value)) {
1.4       www       264:           alert('Ending date is before starting date');
                    265:           return;
                    266:        }
                    267:     }
                    268:     if (message!='') {
                    269:        message+='Continue enrollment?';
                    270:        if (confirm(message)) {
                    271: 	  pclose();
1.50      matthew   272:           vf.state.value='enrolling';
1.4       www       273: 	  vf.submit();
                    274:        }
                    275:     } else {
                    276:       pclose();
1.50      matthew   277:       vf.state.value='enrolling';
1.4       www       278:       vf.submit();
1.24      albertel  279:     }
1.3       www       280: }
                    281: 
                    282: 
1.4       www       283:     function pclose() {
                    284:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    285:                  "height=350,width=350,scrollbars=no,menubar=no");
                    286:         parmwin.close();
                    287:     }
                    288: 
                    289:     function pjump(type,dis,value,marker,ret,call) {
                    290:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
                    291:                  +"&value="+escape(value)+"&marker="+escape(marker)
                    292:                  +"&return="+escape(ret)
                    293:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
                    294:                  "height=350,width=350,scrollbars=no,menubar=no");
                    295: 
                    296:     }
                    297: 
                    298:     function dateset() {
                    299:         if (document.studentform.pres_marker.value=='end') {
                    300:            document.studentform.enddate.value=
                    301: 	       document.studentform.pres_value.value;
                    302:         }
                    303:         if (document.studentform.pres_marker.value=='start') {
                    304:            document.studentform.startdate.value=
                    305: 	       document.studentform.pres_value.value;
                    306:         }
                    307:         pclose();
                    308:     }
1.3       www       309: 
1.28      matthew   310: $authheader
1.24      albertel  311: ENDPICK
1.28      matthew   312: 
1.24      albertel  313: }
                    314: 
1.50      matthew   315: ###############################################################
                    316: ###############################################################
                    317: sub upload_manager_javascript_forward_associate {
1.24      albertel  318:     return(<<ENDPICK);
                    319: function verify(vf) {
                    320:     var founduname=0;
                    321:     var foundpwd=0;
                    322:     var foundname=0;
                    323:     var foundid=0;
                    324:     var foundsec=0;
                    325:     var tw;
                    326:     for (i=0;i<=vf.nfields.value;i++) {
                    327:         tw=eval('vf.f'+i+'.selectedIndex');
                    328:         if (tw==1) { founduname=1; }
                    329:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    330:         if (tw==7) { foundid=1; }
                    331:         if (tw==8) { foundsec=1; }
                    332:         if (tw==9) { foundpwd=1; }
                    333:     }
                    334:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
                    335: }
                    336: 
1.49      matthew   337: //
                    338: // vf = this.form
                    339: // tf = column number
                    340: //
                    341: // values of nw
                    342: //
                    343: // 0 = none
                    344: // 1 = username
                    345: // 2 = names (lastname, firstnames)
                    346: // 3 = fname (firstname)
                    347: // 4 = mname (middlename)
                    348: // 5 = lname (lastname)
                    349: // 6 = gen   (generation)
                    350: // 7 = id
                    351: // 8 = section
                    352: // 9 = ipwd  (password)
                    353: //
1.24      albertel  354: function flip(vf,tf) {
                    355:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    356:    var i;
1.49      matthew   357:    // make sure no other columns are labeled the same as this one
1.24      albertel  358:    for (i=0;i<=vf.nfields.value;i++) {
                    359:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    360:           eval('vf.f'+i+'.selectedIndex=0;')
                    361:       }
                    362:    }
1.49      matthew   363:    // If we set this to 'lastname, firstnames', clear out all the ones
                    364:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
1.24      albertel  365:    if (nw==2) {
                    366:       for (i=0;i<=vf.nfields.value;i++) {
                    367:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    368:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    369:              eval('vf.f'+i+'.selectedIndex=0;')
                    370:          }
                    371:       }
                    372:    }
1.49      matthew   373:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    374:    // clear out any that are set to 'lastname, firstnames' (2)
1.24      albertel  375:    if ((nw>=3) && (nw<=6)) {
                    376:       for (i=0;i<=vf.nfields.value;i++) {
                    377:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    378:              eval('vf.f'+i+'.selectedIndex=0;')
                    379:          }
                    380:       }
                    381:    }
1.49      matthew   382:    // If we set the password, make the password form below correspond to 
                    383:    // the new value.
1.24      albertel  384:    if (nw==9) {
1.28      matthew   385:        changed_radio('int',document.studentform);
                    386:        set_auth_radio_buttons('int',document.studentform);
                    387:        vf.intarg.value='';
                    388:        vf.krbarg.value='';
1.24      albertel  389:        vf.locarg.value='';
                    390:    }
                    391: }
                    392: 
                    393: function clearpwd(vf) {
                    394:     var i;
                    395:     for (i=0;i<=vf.nfields.value;i++) {
                    396:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    397:             eval('vf.f'+i+'.selectedIndex=0;')
                    398:         }
                    399:     }
                    400: }
                    401: 
                    402: ENDPICK
                    403: }
                    404: 
1.50      matthew   405: ###############################################################
                    406: ###############################################################
                    407: sub upload_manager_javascript_reverse_associate {
1.24      albertel  408:     return(<<ENDPICK);
                    409: function verify(vf) {
                    410:     var founduname=0;
                    411:     var foundpwd=0;
                    412:     var foundname=0;
                    413:     var foundid=0;
                    414:     var foundsec=0;
                    415:     var tw;
                    416:     for (i=0;i<=vf.nfields.value;i++) {
                    417:         tw=eval('vf.f'+i+'.selectedIndex');
                    418:         if (i==0 && tw!=0) { founduname=1; }
                    419:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    420:         if (i==6 && tw!=0) { foundid=1; }
                    421:         if (i==7 && tw!=0) { foundsec=1; }
                    422:         if (i==8 && tw!=0) { foundpwd=1; }
                    423:     }
                    424:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
                    425: }
                    426: 
                    427: function flip(vf,tf) {
                    428:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    429:    var i;
                    430:    // picked the all one one name field, reset the other name ones to blank
                    431:    if (tf==1 && nw!=0) {
                    432:       for (i=2;i<=5;i++) {
                    433:          eval('vf.f'+i+'.selectedIndex=0;')
                    434:       }
                    435:    }
                    436:    //picked one of the piecewise name fields, reset the all in
                    437:    //one field to blank
                    438:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    439:       eval('vf.f1.selectedIndex=0;')
                    440:    }
                    441:    // intial password specified, pick internal authentication
                    442:    if (tf==8 && nw!=0) {
1.28      matthew   443:        changed_radio('int',document.studentform);
                    444:        set_auth_radio_buttons('int',document.studentform);
                    445:        vf.krbarg.value='';
                    446:        vf.intarg.value='';
1.24      albertel  447:        vf.locarg.value='';
                    448:    }
                    449: }
                    450: 
                    451: function clearpwd(vf) {
                    452:     var i;
                    453:     if (eval('vf.f8.selectedIndex')!=0) {
                    454:         eval('vf.f8.selectedIndex=0;')
                    455:     }
                    456: }
1.2       www       457: ENDPICK
1.23      albertel  458: }
1.10      www       459: 
1.50      matthew   460: ###############################################################
                    461: ###############################################################
                    462: sub print_upload_manager_footer {
1.23      albertel  463:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
1.28      matthew   464:     my %param = ( formname => 'document.studentform');
                    465:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    466:     my $intform = &Apache::loncommon::authform_internal(%param);
                    467:     my $locform = &Apache::loncommon::authform_local(%param);
1.31      matthew   468:     my $domform = &domain_form($defdom);
1.23      albertel  469:     $r->print(<<ENDPICK);
1.3       www       470: </table>
1.10      www       471: <input type=hidden name=nfields value=$i>
                    472: <input type=hidden name=keyfields value="$keyfields">
1.3       www       473: <h3>Login Type</h3>
1.15      albertel  474: <p>Note: this will not take effect if the user already exists</p>
                    475: <p>
1.28      matthew   476: $krbform
1.15      albertel  477: </p>
                    478: <p>
1.28      matthew   479: $intform
1.15      albertel  480: </p>
                    481: <p>
1.28      matthew   482: $locform
1.15      albertel  483: </p>
1.5       www       484: <h3>LON-CAPA Domain for Students</h3>
1.29      matthew   485: LON-CAPA domain: $domform <p>
1.5       www       486: <h3>Starting and Ending Dates</h3>
1.26      matthew   487: <input type="hidden" value=''          name="pres_value"  >
                    488: <input type="hidden" value=''          name="pres_type"   >
                    489: <input type="hidden" value=''          name="pres_marker" >
                    490: <input type="hidden" value='$today'    name="startdate"   >
                    491: <input type="hidden" value='$halfyear' name="enddate"     >
1.4       www       492: <a 
                    493:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
                    494: >Set Starting Date</a><p>
                    495: 
                    496: <a 
                    497:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
                    498: >Set Ending Date</a><p>
1.5       www       499: <h3>Full Update</h3>
                    500: <input type=checkbox name=fullup value=yes> Full update 
1.11      www       501: (also print list of users not enrolled anymore)<p>
1.18      www       502: <h3>ID/Student Number</h3>
                    503: <input type=checkbox name=forceid value=yes> 
                    504: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.19      www       505: (only do if you know what you are doing)<p>
1.36      albertel  506: <input type="button" onClick="javascript:verify(this.form)" value="Update Courselist" /><br />
1.50      matthew   507: Note: for large courses, this operation may be time consuming.
1.3       www       508: ENDPICK
1.23      albertel  509: }
1.24      albertel  510: 
1.23      albertel  511: # ======================================================= Menu Phase Two Upload
1.50      matthew   512: sub print_upload_manager_form {
1.23      albertel  513:     my $r=shift;
1.26      matthew   514: 
1.24      albertel  515:     my $datatoken;
                    516:     if (!$ENV{'form.datatoken'}) {
1.26      matthew   517:       $datatoken=&Apache::loncommon::upfile_store($r);
1.24      albertel  518:     } else {
1.26      matthew   519:       $datatoken=$ENV{'form.datatoken'};
                    520:       &Apache::loncommon::load_tmp_file($r);
1.24      albertel  521:     }
                    522:     my @records=&Apache::loncommon::upfile_record_sep();
1.23      albertel  523:     my $total=$#records;
                    524:     my $distotal=$total+1;
                    525:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                    526:     my $krbdefdom=$1;
                    527:     $krbdefdom=~tr/a-z/A-Z/;
                    528:     my $today=time;
                    529:     my $halfyear=$today+15552000;
                    530:     my $defdom=$r->dir_config('lonDefDomain');
1.50      matthew   531:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
1.24      albertel  532:     my $i;
                    533:     my $keyfields;
1.23      albertel  534:     if ($total>=0) {
1.50      matthew   535: 	my @d=(['username','Username'],
                    536:                ['names','Last Name, First Names'],
                    537: 	       ['fname','First Name'],
                    538:                ['mname','Middle Names/Initials'],
                    539: 	       ['lname','Last Name'],
                    540:                ['gen','Generation'],
                    541: 	       ['id','ID/Student Number'],
                    542:                ['sec','Group/Section'],
1.24      albertel  543: 	       ['ipwd','Initial Password']);
                    544: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
                    545: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                    546: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
                    547: 	    foreach (@d) { $keyfields.=$_->[0].','; }
                    548: 	    chop($keyfields);
                    549: 	} else {
                    550: 	    unshift(@d,['none','']);
                    551: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
                    552: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                    553: 	    $keyfields=join(',',sort(keys(%sone)));
1.23      albertel  554: 	}
                    555:     }
1.50      matthew   556:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
1.10      www       557: }
                    558: 
1.12      www       559: # ======================================================= Enroll single student
                    560: sub enroll_single_student {
                    561:     my $r=shift;
                    562:     $r->print('<h3>Enrolling Student</h3>');
1.54      matthew   563:     $r->print('<p>Enrolling '.$ENV{'form.cuname'}." \@ ".
1.34      matthew   564:               $ENV{'form.lcdomain'}.'</p>');
1.12      www       565:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
1.31      matthew   566:         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
                    567:         # Deal with home server selection
                    568:         my $domain=$ENV{'form.lcdomain'};
                    569:         my $desiredhost = $ENV{'form.lcserver'};
                    570:         if (lc($desiredhost) eq 'default') {
                    571:             $desiredhost = undef;
                    572:         } else {
1.45      matthew   573:             my %home_servers =&Apache::loncommon::get_library_servers($domain);
1.31      matthew   574:             if (! exists($home_servers{$desiredhost})) {
                    575:                 $r->print('<font color="#ff0000">Error:</font>'.
                    576:                           'Invalid home server specified');
                    577:                 return;
                    578:             }
                    579:         }
1.34      matthew   580:         $r->print(" with server $desiredhost :") if (defined($desiredhost));
1.31      matthew   581:         # End of home server selection logic
1.12      www       582: 	my $amode='';
                    583:         my $genpwd='';
                    584:         if ($ENV{'form.login'} eq 'krb') {
1.47      albertel  585:            $amode='krb';
                    586: 	   $amode.=$ENV{'form.krbver'};
1.28      matthew   587:            $genpwd=$ENV{'form.krbarg'};
1.12      www       588:         } elsif ($ENV{'form.login'} eq 'int') {
1.26      matthew   589:            $amode='internal';
1.28      matthew   590:            $genpwd=$ENV{'form.intarg'};
1.15      albertel  591:         }  elsif ($ENV{'form.login'} eq 'loc') {
                    592: 	    $amode='localauth';
                    593: 	    $genpwd=$ENV{'form.locarg'};
                    594: 	    if (!$genpwd) { $genpwd=" "; }
                    595: 	}
1.34      matthew   596:         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
                    597:                                                    $ENV{'form.lcdomain'});
                    598:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
1.55      matthew   599:             # Clean out any old roles the student has in this class.
1.33      matthew   600:             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    601:                            $ENV{'request.course.id'},$ENV{'form.csec'},
                    602:                             $desiredhost);
1.55      matthew   603:             my $login_result = &Apache::lonnet::modifystudent
                    604:                 ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                    605:                  $ENV{'form.cstid'},$amode,$genpwd,
                    606:                  $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
                    607:                  $ENV{'form.clast'},$ENV{'form.cgen'},
                    608:                  $ENV{'form.csec'},$ENV{'form.enddate'},
                    609:                  $ENV{'form.startdate'},$ENV{'form.forceid'},
                    610:                  $desiredhost);
                    611:             if ($login_result =~ /^ok/) {
                    612:                 $r->print($login_result);
                    613:                 $r->print("<p> If active, the new role will be available ".
                    614:                           "when the student next logs in to LON-CAPA.</p>");
                    615:             } else {
                    616:                 $r->print("unable to enroll: ".$login_result);
                    617:             }
1.12      www       618: 	} else {
1.34      matthew   619:             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
                    620:                       'Invalid login mode or password.  '.
                    621:                       'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
1.12      www       622:         }          
                    623:     } else {
                    624:         $r->print('Invalid username or domain');
1.26      matthew   625:     }    
1.12      www       626: }
                    627: 
1.10      www       628: # ======================================================= Menu Phase Two Enroll
1.50      matthew   629: sub print_enroll_single_student_form {
1.10      www       630:     my $r=shift;
1.40      matthew   631:     $r->print("<h3>Enroll One Student</h3>");
1.26      matthew   632:     my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
1.11      www       633:     $krbdefdom=~tr/a-z/A-Z/;
1.26      matthew   634:     my $today    = time;
                    635:     my $halfyear = $today+15552000;
1.11      www       636:     my $defdom=$r->dir_config('lonDefDomain');
1.24      albertel  637:     my $javascript_validations=&javascript_validations($krbdefdom);
1.28      matthew   638:     # Set up authentication forms
                    639:     my %param = ( formname => 'document.studentform');
                    640:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    641:     my $intform = &Apache::loncommon::authform_internal(%param);
                    642:     my $locform = &Apache::loncommon::authform_local(%param);
1.29      matthew   643:     # Set up domain selection form
1.31      matthew   644:     my $domform = &domain_form($defdom);
1.28      matthew   645:     # Print it all out
1.50      matthew   646:     $r->print(<<END);
                    647: <input type="hidden" name="action" value="enrollstudent">
                    648: <input type="hidden" name="state"  value="done">
                    649: 
1.28      matthew   650: <script type="text/javascript" language="Javascript">
1.12      www       651: function verify(vf) {
                    652:     var founduname=0;
                    653:     var foundpwd=0;
                    654:     var foundname=0;
                    655:     var foundid=0;
                    656:     var foundsec=0;
                    657:     var tw;
1.26      matthew   658:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
1.31      matthew   659: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
1.12      www       660:         founduname=1;
                    661:     }
1.14      harris41  662:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
1.26      matthew   663: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
1.12      www       664:         foundname=1;
                    665:     }
1.14      harris41  666:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
1.12      www       667:         foundsec=1;
                    668:     }
1.14      harris41  669:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
1.12      www       670: 	foundid=1;
                    671:     }
                    672:     if (founduname==0) {
                    673: 	alert('You need to specify at least the username and domain fields');
                    674:         return;
                    675:     }
1.24      albertel  676:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
1.12      www       677: }
                    678: 
1.24      albertel  679: $javascript_validations
1.12      www       680: 
1.24      albertel  681: function clearpwd(vf) {
                    682:     //nothing else needs clearing
1.15      albertel  683: }
                    684: 
1.12      www       685: </script>
1.11      www       686: <h3>Personal Data</h3>
1.50      matthew   687: <table>
                    688: <tr><td>First Name:</td><td> <input type="text" name="cfirst"  size="15"></td></tr>
                    689: <tr><td>Middle Name:</td><td> <input type="text" name="cmiddle" size="15"></td></tr>
                    690: <tr><td>Last Name: </td><td><input type="text" name="clast"   size="15"></td></tr>
                    691: <tr><td>Generation: </td><td><input type="text" name="cgen"    size="5"> </td></tr>
                    692: </table>
1.11      www       693: 
1.12      www       694: <h3>Login Data</h3>
1.26      matthew   695: <p>Username: <input type="text" name="cuname"  size="15"></p>
1.29      matthew   696: <p>Domain:   $domform</p>
1.26      matthew   697: <p>Note: login settings below  will not take effect if the user already exists
                    698: </p><p>
1.28      matthew   699: $krbform
1.26      matthew   700: </p><p>
1.28      matthew   701: $intform
1.26      matthew   702: </p><p>
1.28      matthew   703: $locform
1.26      matthew   704: </p><p>
1.50      matthew   705: 
                    706: <h3>Course Data</h3>
                    707: 
                    708: <p>Group/Section: <input type="text" name="csec" size="5" />
                    709: <p>
                    710: <!-- Date setting form elements -->
                    711: <input type="hidden" name="pres_value"  value='' />
                    712: <input type="hidden" name="pres_type"   value='' />
                    713: <input type="hidden" name="pres_marker" value='' />
                    714: <input type="hidden" name="startdate"   value='$today'    />
                    715: <input type="hidden" name="enddate"     value='$halfyear' />
1.26      matthew   716: </p><p>
1.11      www       717: <a 
                    718:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
1.26      matthew   719: >Set Starting Date</a>
                    720: </p><p>
1.11      www       721: <a 
                    722:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
1.26      matthew   723: >Set Ending Date</a>
1.50      matthew   724: </p>
                    725: <h3>ID/Student Number</h3>
                    726: <p>
                    727: ID/Student Number: <input type="text" name="cstid" size="10">
1.26      matthew   728: </p><p>
                    729: <input type="checkbox" name="forceid" value="yes"> 
1.18      www       730: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
1.50      matthew   731: (only do if you know what you are doing)
                    732: </p><p>
                    733: <input type="button" onClick="verify(this.form)" value="Enroll as student">
1.26      matthew   734: </p>
1.50      matthew   735: END
                    736:     return;
1.10      www       737: }
                    738: 
                    739: # ========================================================= Menu Phase Two Drop
1.51      matthew   740: sub print_drop_menu {
1.10      www       741:     my $r=shift;
1.40      matthew   742:     $r->print("<h3>Drop Students</h3>");
1.11      www       743:     my $cid=$ENV{'request.course.id'};
1.56      matthew   744:     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
                    745:     if (! defined($classlist)) {
1.27      matthew   746:         $r->print("There are no students currently enrolled.\n");
1.51      matthew   747:         return;
1.25      matthew   748:     }
1.51      matthew   749:     # Print out the available choices
1.56      matthew   750:     &show_drop_list($r,$classlist,$keylist);
1.51      matthew   751:     return;
1.11      www       752: }
                    753: 
1.40      matthew   754: # ============================================== view classlist
1.50      matthew   755: sub print_html_classlist {
1.40      matthew   756:     my $r=shift;
1.57      matthew   757:     if (! exists($ENV{'form.sortby'})) {
                    758:         $ENV{'form.sortby'} = 'username';
                    759:     }
1.59      matthew   760:     if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
1.57      matthew   761:         $ENV{'form.Status'} = 'Active';
                    762:     }
                    763:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
                    764:         ($ENV{'form.Status'},'studentform');
1.48      matthew   765:     $r->print(<<END);
1.58      matthew   766: <input type="hidden" name="action" value="$ENV{'form.action'}" />
1.57      matthew   767: <input type="hidden" name="state"  value="" />
1.50      matthew   768: <p>
1.57      matthew   769: <font size="+1">Current Classlist</font>
                    770: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.59      matthew   771: END
                    772:     if ($ENV{'form.action'} ne 'modifystudent') {
                    773:         $r->print(<<END);
1.57      matthew   774: <font size="+1">
                    775: <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
1.61      matthew   776: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.60      matthew   777: <a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
1.57      matthew   778: </font>
                    779: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
1.61      matthew   780: Student Status:
1.48      matthew   781: END
1.59      matthew   782:     }
                    783:     $r->print($status_select."</p>\n");
1.40      matthew   784:     my $cid=$ENV{'request.course.id'};
1.56      matthew   785:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    786:     if (! defined($classlist)) {
1.40      matthew   787:         $r->print("There are no students currently enrolled.\n");
                    788:     } else {
                    789:         # Print out the available choices
1.50      matthew   790:         if ($ENV{'form.action'} eq 'modifystudent') {
1.53      matthew   791:             &show_class_list($r,'view','modify','modifystudent',
1.57      matthew   792:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   793:         } else {
1.53      matthew   794:             &show_class_list($r,'view','aboutme','classlist',
1.57      matthew   795:                              $ENV{'form.Status'},$classlist,$keylist);
1.50      matthew   796:         }
1.41      matthew   797:     }
                    798: }
                    799: 
                    800: # ============================================== view classlist
1.60      matthew   801: sub print_formatted_classlist {
1.41      matthew   802:     my $r=shift;
1.60      matthew   803:     my $mode = shift;
1.41      matthew   804:     my $cid=$ENV{'request.course.id'};
1.56      matthew   805:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                    806:     if (! defined($classlist)) {
1.41      matthew   807:         $r->print("There are no students currently enrolled.\n");
                    808:     } else {
1.60      matthew   809:         &show_class_list($r,$mode,'nolink','csv',
1.57      matthew   810:                          $ENV{'form.Status'},$classlist,$keylist);
1.40      matthew   811:     }
                    812: }
                    813: 
                    814: # =================================================== Show student list to drop
                    815: sub show_class_list {
1.56      matthew   816:     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
1.40      matthew   817:     my $cid=$ENV{'request.course.id'};
1.60      matthew   818:     #
                    819:     # Variables for excel output
                    820:     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
                    821:     #
1.53      matthew   822:     my $sortby = $ENV{'form.sortby'};
                    823:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                    824:         $sortby = 'username';
                    825:     }
1.42      matthew   826:     # Print out header 
1.41      matthew   827:     if ($mode eq 'view') {
1.50      matthew   828:         if ($linkto eq 'aboutme') {
1.56      matthew   829:             $r->print('Select a user name to view the users personal page.');
1.50      matthew   830:         } elsif ($linkto eq 'modify') {
                    831:             $r->print('Select a user name to modify the students information');
                    832:         }
1.41      matthew   833:         $r->print(<<END);
1.59      matthew   834: 
1.53      matthew   835: <input type="hidden" name="sortby" value="$sortby" />
1.59      matthew   836: <input type="hidden" name="sname"  value="" />
                    837: <input type="hidden" name="sdom"   value="" />
1.40      matthew   838: <p>
                    839: <table border=2>
1.53      matthew   840: <tr><th>
1.57      matthew   841:        <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
1.53      matthew   842:     </th><th>
1.57      matthew   843:        <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
1.53      matthew   844:     </th><th>
1.57      matthew   845:        <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
1.53      matthew   846:     </th><th>
1.57      matthew   847:        <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
1.53      matthew   848:     </th><th>
1.57      matthew   849:        <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
1.53      matthew   850:     </th>
                    851: </tr>
1.40      matthew   852: END
1.41      matthew   853:     } elsif ($mode eq 'csv') {
1.58      matthew   854:         if($statusmode eq 'Expired') {
                    855:             $r->print('"Students with expired roles"');
                    856:         }
                    857:         if ($statusmode eq 'Any') {
                    858:             $r->print('"'.join('","',("username","domain","ID","student name",
                    859:                                       "section","status")).'"'."\n");
                    860:         } else {
                    861:             $r->print('"'.join('","',("username","domain","ID","student name",
                    862:                                       "section")).'"'."\n");
                    863:         }
1.60      matthew   864:     } elsif ($mode eq 'excel') {
                    865:         # Create the excel spreadsheet
                    866:         $excel_filename = '/prtspool/'.
                    867:             $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
                    868:                 time.'_'.rand(1000000000).'.xls';
                    869:         $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
                    870:                                                        $excel_filename);
                    871:         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
                    872:         $excel_sheet = $excel_workbook->addworksheet('classlist');
                    873:         #
                    874:         my $description = 'Classlist for '.
                    875:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    876:         $excel_sheet->write($row++,0,$description);
                    877:         #
                    878:         $excel_sheet->write($row++,0,["username","domain","ID",
                    879:                                       "student name","section","status"]);
1.41      matthew   880:     }
1.56      matthew   881:     #
                    882:     # Sort the students
                    883:     my %index;
                    884:     my $i;
                    885:     foreach (@$keylist) {
                    886:         $index{$_} = $i++;
                    887:     }
                    888:     my $index  = $index{$sortby};
                    889:     my $second = $index{'username'};
                    890:     my $third  = $index{'domain'};
1.53      matthew   891:     my @Sorted_Students = sort {
1.56      matthew   892:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                    893:             ||
                    894:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                    895:             ||
                    896:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                    897:         } (keys(%$classlist));
1.53      matthew   898:     foreach my $student (@Sorted_Students) {
1.56      matthew   899:         my $username = $classlist->{$student}->[$index{'username'}];
                    900:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                    901:         my $section  = $classlist->{$student}->[$index{'section'}];
                    902:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                    903:         my $id       = $classlist->{$student}->[$index{'id'}];
                    904:         my $status   = $classlist->{$student}->[$index{'status'}];
1.57      matthew   905:         next if (($statusmode ne 'Any') && ($status ne $statusmode));
1.51      matthew   906:         if ($mode eq 'view') {
                    907:             $r->print("<tr>\n    <td>\n        ");
                    908:             if ($linkto eq 'nothing') {
                    909:                 $r->print($username);
                    910:             } elsif ($linkto eq 'aboutme') {
                    911:                 $r->print(&Apache::loncommon::aboutmewrapper($username,
                    912:                                                              $username,
                    913:                                                              $domain));
                    914:             } elsif ($linkto eq 'modify') {
1.59      matthew   915:                 $r->print('<a href="'.
                    916:                           "javascript:document.studentform.sname.value='".
                    917:                           $username.
                    918:                           "';document.studentform.sdom.value='".$domain.
                    919:                           "';document.studentform.state.value='selected".
                    920:                           "';document.studentform.submit();".'">'.
1.53      matthew   921:                           $username."</a>\n");
1.50      matthew   922:             }
1.51      matthew   923:             $r->print(<<"END");
1.50      matthew   924:     </td>
1.51      matthew   925:     <td>$domain</td>
                    926:     <td>$id</td>
                    927:     <td>$name</td>
                    928:     <td>$section</td>
1.40      matthew   929: </tr>
                    930: END
1.51      matthew   931:         } elsif ($mode eq 'csv') {
                    932:             # no need to bother with $linkto
                    933:             my @line = ();
                    934:             foreach ($username,$domain,$id,$name,$section) {
                    935:                 push @line,&Apache::loncommon::csv_translate($_);
1.58      matthew   936:             }
                    937:             if ($statusmode eq 'Any') {
                    938:                 push @line,&Apache::loncommon::csv_translate($status);
1.41      matthew   939:             }
1.51      matthew   940:             my $tmp = $";
                    941:             $" = '","';
                    942:             $r->print("\"@line\"\n");
                    943:             $" = $tmp;
1.60      matthew   944:         } elsif ($mode eq 'excel') {
                    945:             $excel_sheet->write($row++,0,[$username,$domain,$id,
                    946:                                           $name,$section,$status]);
1.40      matthew   947:         }
                    948:     }
1.60      matthew   949:     if ($mode eq 'view') {
                    950:         $r->print('</table><br>');
                    951:     } elsif ($mode eq 'excel') {
                    952:         $excel_workbook->close();
                    953:         $r->print('<p><a href="'.$excel_filename.'">'.
                    954:                   'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
                    955:     }
1.40      matthew   956: }
                    957: 
1.50      matthew   958: 
                    959: #
                    960: # print out form for modification of a single students data
                    961: #
                    962: sub print_modify_student_form {
                    963:     my $r = shift();
                    964:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.59      matthew   965:                                             ['sdom','sname']);    
1.53      matthew   966:     my $sname  = $ENV{'form.sname'};
                    967:     my $sdom   = $ENV{'form.sdom'};
                    968:     my $sortby = $ENV{'form.sortby'};
1.50      matthew   969:     # determine the students name information
                    970:     my %info=&Apache::lonnet::get('environment',
                    971:                                   ['firstname','middlename',
1.52      matthew   972:                                    'lastname','generation','id'],
1.50      matthew   973:                                   $sdom, $sname);
                    974:     my ($tmp) = keys(%info);
                    975:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                    976:         $r->print('<font color="#ff0000" size="+2">Error</font>'.
                    977:                   '<p>'.
                    978:                   'Unable to retrieve environment data for '.$sname.
                    979:                   'in domain '.$sdom.'</p><p>'.
                    980:                   'Please contact your LON-CAPA administrator '.
                    981:                   'regarding this situation.</p></body></html>');
                    982:         return;
                    983:     }
                    984:     # determine the students starting and ending times and section
                    985:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
                    986:     # Deal with date forms
                    987:     my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    988:                                                             'startdate',
                    989:                                                             $starttime);
                    990:     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
                    991:                                                           'enddate',
                    992:                                                           $endtime);
1.59      matthew   993:     #
                    994:     if (! exists($ENV{'form.Status'}) || 
                    995:         $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
                    996:         $ENV{'form.Status'} = 'crap';
                    997:     }
1.50      matthew   998:     # Make sure student is enrolled in course    
                    999:     $r->print(<<END);
1.52      matthew  1000: <p>
                   1001: <font size="+1">
                   1002: Only domain coordinators can change a users password.
                   1003: </font>
                   1004: </p>
1.50      matthew  1005: <input type="hidden" name="slogin"  value="$sname"  />
                   1006: <input type="hidden" name="sdomain" value="$sdom" />
                   1007: <input type="hidden" name="action"  value="modifystudent" />
1.53      matthew  1008: <input type="hidden" name="state"   value="done" />
                   1009: <input type="hidden" name="sortby"  value="$sortby" />
1.59      matthew  1010: <input type="hidden" name="Status"  value="$ENV{'form.Status'}" />
                   1011: 
1.50      matthew  1012: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
                   1013: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
                   1014: <p>
                   1015: <b>Student Name</b>
                   1016: <table>
                   1017: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
                   1018: <tr><td>
                   1019: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
                   1020: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
                   1021: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
                   1022: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
                   1023: </table>
                   1024: </p><p>
1.52      matthew  1025: <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
                   1026: </p><p>
1.53      matthew  1027: <input type="checkbox" name="forceid" > 
                   1028: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
                   1029: (only do if you know what you are doing)
                   1030: </p><p>
1.50      matthew  1031: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
                   1032: </p><p>
                   1033: <table>
                   1034: <tr><td align="right"><b>Starting Date:</b></td><td>$startdateform</td></tr>
                   1035: <tr><td align="right"><b>Ending Date:</b></td><td>$enddateform</td></tr>
                   1036: </table>
                   1037: </p>
                   1038: <input type="submit" value="Submit Modifications" />
                   1039: </body></html>
                   1040: END
                   1041:     return;
                   1042: }
                   1043: 
                   1044: #
                   1045: # modify a single students section 
                   1046: #
                   1047: sub modify_single_student {
                   1048:     my $r = shift;
1.59      matthew  1049:     # Get the 'sortby' and 'Status' variables so the user goes back to their
                   1050:     # previous screen
1.53      matthew  1051:     my $sortby = $ENV{'form.sortby'};
1.59      matthew  1052:     my $status = $ENV{'form.Status'};
1.53      matthew  1053:     #
                   1054:     # We always need this information
                   1055:     my $slogin     = $ENV{'form.slogin'};
                   1056:     my $sdom       = $ENV{'form.sdomain'};
                   1057:     #
                   1058:     # Get the old data
                   1059:     my %old=&Apache::lonnet::get('environment',
                   1060:                                  ['firstname','middlename',
                   1061:                                   'lastname','generation','id'],
                   1062:                                  $sdom, $slogin);
1.59      matthew  1063:     $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
                   1064:                                                   $ENV{'request.course.id'});
1.53      matthew  1065:     my ($tmp) = keys(%old);
                   1066:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
                   1067:         $r->print("There was an error determining the environment values ".
                   1068:                   " for $slogin \@ $sdom.");
                   1069:         return;
                   1070:     }
                   1071:     undef $tmp;
                   1072:     #
                   1073:     # Get the new data
1.50      matthew  1074:     my $firstname  = $ENV{'form.firstname'};
                   1075:     my $middlename = $ENV{'form.middlename'};
                   1076:     my $lastname   = $ENV{'form.lastname'};
                   1077:     my $generation = $ENV{'form.generation'};
                   1078:     my $section    = $ENV{'form.section'};
                   1079:     my $courseid   = $ENV{'request.course.id'};
1.52      matthew  1080:     my $sid        = $ENV{'form.id'};
1.50      matthew  1081:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form('startdate',
                   1082:                                                                time);
                   1083:     my $endtime   = &Apache::lonhtmlcommon::get_date_from_form('enddate',
                   1084:                                                                time);
                   1085:     my $displayable_starttime = localtime($starttime);
                   1086:     my $displayable_endtime   = localtime($endtime);
1.53      matthew  1087:     # 
                   1088:     # check for forceid override
                   1089:     if (($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
                   1090:         $r->print("<font color=\"ff0000\">You changed the students id ".
                   1091:                   " but did not disable the ID change safeguard.".
                   1092:                   "  The students id will not be changed.</font>");
                   1093:         $sid = $old{'id'};
                   1094:     }
                   1095:     #
1.50      matthew  1096:     # talk to the user about what we are going to do
                   1097:     $r->print(<<END);
1.53      matthew  1098:     <h2>Modifying data for user $slogin \@ $sdom </h2>
1.50      matthew  1099: <h3>Student Information</h3>
1.53      matthew  1100: <table rules="rows" border="1" cellpadding="3" >
                   1101: <tr>
                   1102:     <th> Field </th>
                   1103:     <th> Old Value </th>
                   1104:     <th> New Value </th>
                   1105: </tr>
                   1106: <tr>
                   1107:     <td> <b>First name</b> </td>
                   1108:     <td> $old{'firstname'} </td>
                   1109:     <td> $firstname </td>
                   1110: </tr><tr>
                   1111:     <td> <b>Middle name</b> </td>
                   1112:     <td> $old{'middlename'} </td>
                   1113:     <td> $middlename </td>
                   1114: </tr><tr>
                   1115:     <td> <b>Last name</b> </td>
                   1116:     <td> $old{'lastname'} </td>
                   1117:     <td> $lastname </td>
                   1118: </tr><tr>
                   1119:     <td> <b>Generation</b> </td>
                   1120:     <td> $old{'generation'} </td>
                   1121:     <td> $generation </td>
                   1122: </tr><tr>
                   1123:     <td> <b>ID</b> </td>
                   1124:     <td> $old{'id'} </td>
                   1125:     <td> $sid </td>
1.59      matthew  1126: </tr><tr>
                   1127:     <td> <b>Section</b> </td>
                   1128:     <td> $old{'section'} </td>
                   1129:     <td> $section</td>
1.53      matthew  1130: </tr>
1.50      matthew  1131: </table>
                   1132: <h3>Role Information</h3>
                   1133: <table>
                   1134: <tr><td>Start Time  </td><td> $displayable_starttime </td></tr>
                   1135: <tr><td>End Time    </td><td> $displayable_endtime   </td></tr>
                   1136: </table>
1.52      matthew  1137: <p>
1.50      matthew  1138: END
1.53      matthew  1139:     #
1.52      matthew  1140:     # Send request(s) to modify data
                   1141:     my $roleresults = &Apache::lonnet::modifystudent
1.53      matthew  1142:         ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
                   1143:          $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
                   1144:     if ($roleresults eq 'refused' ) {
1.50      matthew  1145:         $r->print("Your request to change the role information for this ".
1.53      matthew  1146:                   "student was refused.  You do not appear to have ".
                   1147:                   "sufficient authority to change student information.");
1.50      matthew  1148:     } elsif ($roleresults !~ /ok/) {
                   1149:         $r->print("An error occurred during the attempt to change the role".
1.52      matthew  1150:                   " information for this student.  <br />".
                   1151:                   "The error reported was ".
1.50      matthew  1152:                   $roleresults);
1.53      matthew  1153:         &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
                   1154:                                  " data for ".$slogin." \@ ".$sdom." by ".
                   1155:                                  $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
                   1156:                                  ":".$roleresults);
1.50      matthew  1157:     } else { # everything is okay!
1.52      matthew  1158:         $r->print("Student information updated successfully. <br />".
                   1159:                   "The student must log out and log in again to see ".
                   1160:                   "these changes.");
1.50      matthew  1161:     }
                   1162:     $r->print(<<END);
1.52      matthew  1163: </p><p>
1.59      matthew  1164: <input type="hidden" name="action" value="modifystudent" />
                   1165: <input type="hidden" name="sortby" value="$sortby" />
                   1166: <input type="hidden" name="Status" value="$status" />
                   1167: <a href="javascript:document.studentform.submit();">Modify another students data</a>
1.50      matthew  1168: </body></html>
                   1169: END
                   1170:     return;
                   1171: }
                   1172: 
                   1173: sub get_enrollment_data {
                   1174:     my ($sname,$sdomain) = @_;
                   1175:     my $courseid = $ENV{'request.course.id'};
                   1176:     $courseid =~ s:_:/:g;
                   1177:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
                   1178:     my ($tmp) = keys(%roles);
                   1179:     # Bail out if we were unable to get the students roles
                   1180:     return "666" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   1181:     # Go through the roles looking for enrollment in this course
                   1182:     my ($end,$start) = (undef,undef);
                   1183:     my $section = '';
                   1184:     my $count = scalar(keys(%roles));
                   1185:     while (my ($course,$role) = each(%roles)) {
                   1186:         &Apache::lonnet::logthis('course = '.$course.' role = '.$role);
                   1187:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
                   1188:             #
                   1189:             # Get active role
                   1190:             $section=$1;
                   1191:             (undef,$end,$start)=split(/\_/,$role);
                   1192:             my $now=time;
                   1193:             my $notactive=0;
                   1194:             if ($start) {
                   1195:                 if ($now<$start) { $notactive=1; }
                   1196:             }
                   1197:             if ($end) {
                   1198:                 if ($now>$end) { $notactive=1; }
                   1199:             } 
                   1200:             unless ($notactive) { return ($start,$end,$section); }
                   1201:         }
                   1202:     }
                   1203:     return ($start,$end,$section);
                   1204: }
                   1205: 
1.56      matthew  1206: #################################################
                   1207: #################################################
                   1208: 
                   1209: =pod
                   1210: 
                   1211: =item show_drop_list
                   1212: 
                   1213: Display a list of students to drop
                   1214: Inputs: 
                   1215: 
                   1216: =over 4
                   1217: 
                   1218: =item $r, Apache request
                   1219: 
                   1220: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
                   1221: 
                   1222: =item $keylist, array pointer returned from loncoursedata::get_classlist() 
                   1223: which describes the order elements are stored in the %$classlist values.
                   1224: 
                   1225: =item $nosort, if true, sorting links are omitted.
                   1226: 
                   1227: =back
                   1228: 
                   1229: =cut
                   1230: 
                   1231: #################################################
                   1232: #################################################
1.11      www      1233: sub show_drop_list {
1.56      matthew  1234:     my ($r,$classlist,$keylist,$nosort)=@_;
1.11      www      1235:     my $cid=$ENV{'request.course.id'};
1.59      matthew  1236:     if (! exists($ENV{'form.sortby'})) {
                   1237:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1238:                                                 ['sortby']);
                   1239:     }
1.54      matthew  1240:     my $sortby = $ENV{'form.sortby'};
                   1241:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
                   1242:         $sortby = 'username';
                   1243:     }
1.56      matthew  1244:     #
1.54      matthew  1245:     my $action = "drop";
                   1246:     $r->print(<<END);
                   1247: <input type="hidden" name="sortby" value="$sortby" />
                   1248: <input type="hidden" name="action" value="$action" />
1.50      matthew  1249: <input type="hidden" name="state"  value="done" />
1.32      matthew  1250: <script>
1.51      matthew  1251: function checkAll(field) {
1.32      matthew  1252:     for (i = 0; i < field.length; i++)
                   1253:         field[i].checked = true ;
                   1254: }
                   1255: 
1.51      matthew  1256: function uncheckAll(field) {
1.32      matthew  1257:     for (i = 0; i < field.length; i++)
                   1258:         field[i].checked = false ;
                   1259: }
                   1260: </script>
                   1261: <p>
1.26      matthew  1262: <input type="hidden" name="phase" value="four">
1.56      matthew  1263: END
                   1264: 
                   1265:     if ($nosort) {
                   1266:         $r->print(<<END);
                   1267: <table border=2>
                   1268: <tr>
                   1269:     <th>&nbsp;</th>
                   1270:     <th>username</th>
                   1271:     <th>domain</th>
                   1272:     <th>ID</th>
                   1273:     <th>student name</th>
                   1274:     <th>section</th>
                   1275: </tr>
                   1276: END
                   1277: 
                   1278:     } else  {
                   1279:         $r->print(<<END);
1.26      matthew  1280: <table border=2>
1.54      matthew  1281: <tr><th>&nbsp;</th>
                   1282:     <th>
                   1283:        <a href="/adm/dropadd?action=$action&sortby=username">username</a>
                   1284:     </th><th>
                   1285:        <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
                   1286:     </th><th>
                   1287:        <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
                   1288:     </th><th>
                   1289:        <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
                   1290:     </th><th>
                   1291:        <a href="/adm/dropadd?action=$action&sortby=section">section</a>
                   1292:     </th>
                   1293: </tr>
1.26      matthew  1294: END
1.56      matthew  1295:     }
                   1296:     #
                   1297:     # Sort the students
                   1298:     my %index;
                   1299:     my $i;
                   1300:     foreach (@$keylist) {
                   1301:         $index{$_} = $i++;
                   1302:     }
                   1303:     my $index  = $index{$sortby};
                   1304:     my $second = $index{'username'};
                   1305:     my $third  = $index{'domain'};
1.54      matthew  1306:     my @Sorted_Students = sort {
1.56      matthew  1307:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   1308:             ||
                   1309:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   1310:             ||
                   1311:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
                   1312:         } (keys(%$classlist));
1.54      matthew  1313:     foreach my $student (@Sorted_Students) {
1.52      matthew  1314:         my $error;
1.56      matthew  1315:         my $username = $classlist->{$student}->[$index{'username'}];
                   1316:         my $domain   = $classlist->{$student}->[$index{'domain'}];
                   1317:         my $section  = $classlist->{$student}->[$index{'section'}];
                   1318:         my $name     = $classlist->{$student}->[$index{'fullname'}];
                   1319:         my $id       = $classlist->{$student}->[$index{'id'}];
                   1320:         my $status   = $classlist->{$student}->[$index{'status'}];
1.51      matthew  1321:         next if ($status ne 'Active');
                   1322:         #
                   1323:         $r->print(<<"END");
1.26      matthew  1324: <tr>
1.51      matthew  1325:     <td><input type="checkbox" name="droplist" value="$student"></td>
                   1326:     <td>$username</td>
                   1327:     <td>$domain</td>
                   1328:     <td>$id</td>
                   1329:     <td>$name</td>
                   1330:     <td>$section</td>
1.26      matthew  1331: </tr>
                   1332: END
1.25      matthew  1333:     }
                   1334:     $r->print('</table><br>');
1.32      matthew  1335:     $r->print(<<"END");
                   1336: </p><p>
                   1337: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
                   1338: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
                   1339: <p><input type=submit value="Drop Students"></p>
                   1340: END
1.51      matthew  1341:     return;
1.10      www      1342: }
                   1343: 
1.48      matthew  1344: #
                   1345: # Print out the initial form to get the courselist file
                   1346: #
                   1347: sub print_first_courselist_upload_form {
                   1348:     my $r=shift;
                   1349:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1350:     my $create_classlist_help = 
                   1351: 	&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   1352:            "How do I create a class list from a spreadsheet");
                   1353:     my $create_csv_help =
                   1354: 	&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   1355:            "How do I create a CSV file from a spreadsheet");
                   1356:     $r->print(<<ENDUPFORM);
                   1357: <input type=hidden name=phase value=two>
                   1358: <h3>Upload a courselist</h3>
                   1359: $upfile_select
1.50      matthew  1360: <p>
                   1361: <input type=submit name="fileupload" value="Upload Courselist">
                   1362: <input type="hidden" name="action" value="upload" />
                   1363: <input type="hidden" name="state"  value="got_file" />
                   1364: </p>
1.48      matthew  1365: $create_classlist_help <br />
                   1366: $create_csv_help
                   1367: </body></html>
                   1368: ENDUPFORM
                   1369:     return;
                   1370: }
                   1371: 
1.10      www      1372: # ================================================= Drop/Add from uploaded file
                   1373: sub upfile_drop_add {
                   1374:     my $r=shift;
1.24      albertel 1375:     &Apache::loncommon::load_tmp_file($r);
                   1376:     my @studentdata=&Apache::loncommon::upfile_record_sep();
1.26      matthew  1377:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
                   1378:     my $cid = $ENV{'request.course.id'};
1.25      matthew  1379:     my %fields=();
1.26      matthew  1380:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
1.25      matthew  1381:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
                   1382:             if ($ENV{'form.f'.$i} ne 'none') {
                   1383:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
                   1384:             }
                   1385:         } else {
                   1386:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
                   1387:         }
                   1388:     }
1.26      matthew  1389:     #
                   1390:     my $startdate = $ENV{'form.startdate'};
                   1391:     my $enddate   = $ENV{'form.enddate'};
1.25      matthew  1392:     if ($startdate=~/\D/) { $startdate=''; }
1.26      matthew  1393:     if ($enddate=~/\D/)   { $enddate=''; }
1.31      matthew  1394:     # Determine domain and desired host (home server)
1.25      matthew  1395:     my $domain=$ENV{'form.lcdomain'};
1.31      matthew  1396:     my $desiredhost = $ENV{'form.lcserver'};
                   1397:     if (lc($desiredhost) eq 'default') {
                   1398:         $desiredhost = undef;
                   1399:     } else {
1.45      matthew  1400:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
1.31      matthew  1401:         if (! exists($home_servers{$desiredhost})) {
                   1402:             $r->print('<font color="#ff0000">Error:</font>'.
                   1403:                       'Invalid home server specified');
                   1404:             return;
                   1405:         }
                   1406:     }
1.26      matthew  1407:     # Determine authentication mechanism
                   1408:     my $amode  = '';
                   1409:     my $genpwd = '';
1.25      matthew  1410:     if ($ENV{'form.login'} eq 'krb') {
1.47      albertel 1411:         $amode='krb';
                   1412: 	$amode.=$ENV{'form.krbver'};
1.28      matthew  1413:         $genpwd=$ENV{'form.krbarg'};
1.25      matthew  1414:     } elsif ($ENV{'form.login'} eq 'int') {
                   1415:         $amode='internal';
1.28      matthew  1416:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
                   1417:             $genpwd=$ENV{'form.intarg'};
1.25      matthew  1418:         }
                   1419:     } elsif ($ENV{'form.login'} eq 'loc') {
                   1420:         $amode='localauth';
                   1421:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
                   1422:             $genpwd=$ENV{'form.locarg'};
                   1423:         }
                   1424:     }
                   1425:     unless (($domain=~/\W/) || ($amode eq '')) {
1.26      matthew  1426:         #######################################
                   1427:         ##         Enroll Students           ##
                   1428:         #######################################
1.25      matthew  1429:         $r->print('<h3>Enrolling Students</h3>');
                   1430:         my $count=0;
                   1431:         my $flushc=0;
                   1432:         my %student=();
1.26      matthew  1433:         # Get new classlist
1.25      matthew  1434:         foreach (@studentdata) {
                   1435:             my %entries=&Apache::loncommon::record_sep($_);
1.26      matthew  1436:             # Determine student name
1.25      matthew  1437:             unless (($entries{$fields{'username'}} eq '') ||
                   1438:                     (!defined($entries{$fields{'username'}}))) {
1.26      matthew  1439:                 my ($fname, $mname, $lname,$gen) = ('','','','');
1.25      matthew  1440:                 if (defined($fields{'names'})) {
1.26      matthew  1441:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   1442:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
1.25      matthew  1443:                 } else {
                   1444:                     if (defined($fields{'fname'})) {
                   1445:                         $fname=$entries{$fields{'fname'}};
                   1446:                     }
                   1447:                     if (defined($fields{'mname'})) {
                   1448:                         $mname=$entries{$fields{'mname'}};
                   1449:                     }
                   1450:                     if (defined($fields{'lname'})) {
                   1451:                         $lname=$entries{$fields{'lname'}};
                   1452:                     }
                   1453:                     if (defined($fields{'gen'})) {
                   1454:                         $gen=$entries{$fields{'gen'}};
                   1455:                     }
                   1456:                 }
                   1457:                 if ($entries{$fields{'username'}}=~/\W/) {
                   1458:                     $r->print('<p><b>Unacceptable username: '.
1.10      www      1459:                               $entries{$fields{'username'}}.' for user '.
1.4       www      1460:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
1.25      matthew  1461:                 } else {
1.26      matthew  1462:                     # determine section number
1.25      matthew  1463:                     my $sec='';
                   1464:                     my $username=$entries{$fields{'username'}};
                   1465:                     if (defined($fields{'sec'})) {
                   1466:                         if (defined($entries{$fields{'sec'}})) {
                   1467:                             $sec=$entries{$fields{'sec'}};
                   1468:                         }
                   1469:                     }
1.26      matthew  1470:                     # determine student id number
1.25      matthew  1471:                     my $id='';
                   1472:                     if (defined($fields{'id'})) {
                   1473:                         if (defined($entries{$fields{'id'}})) {
                   1474:                             $id=$entries{$fields{'id'}};
                   1475:                         }
                   1476:                         $id=~tr/A-Z/a-z/;
                   1477:                     }
1.26      matthew  1478:                     # determine student password
1.25      matthew  1479:                     my $password='';
                   1480:                     if ($genpwd) { 
                   1481:                         $password=$genpwd; 
                   1482:                     } else {
                   1483:                         if (defined($fields{'ipwd'})) {
                   1484:                             if ($entries{$fields{'ipwd'}}) {
                   1485:                                 $password=$entries{$fields{'ipwd'}};
                   1486:                             }
                   1487:                         }
                   1488:                     }
1.56      matthew  1489:                     # Clean up whitespace
                   1490:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
                   1491:                              \$lname,\$gen,\$sec) {
                   1492:                         $$_ =~ s/(\s+$|^\s+)//g;
                   1493:                     }
1.25      matthew  1494:                     if ($password) {
1.33      matthew  1495:                         &modifystudent($domain,$username,$cid,$sec,
                   1496:                                        $desiredhost);
1.25      matthew  1497:                         my $reply=&Apache::lonnet::modifystudent
                   1498:                             ($domain,$username,$id,$amode,$password,
                   1499:                              $fname,$mname,$lname,$gen,$sec,$enddate,
1.31      matthew  1500:                              $startdate,$ENV{'form.forceid'},$desiredhost);
1.26      matthew  1501:                         if ($reply ne 'ok') {
                   1502:                             $r->print('<p><b>'.
                   1503:                                       'Error enrolling '.$username.': '.
                   1504:                                       $reply.'</b></p>');
1.10      www      1505:          		} else {
1.7       www      1506:                             $count++; $flushc++;
                   1507:                             $student{$username}=1;
1.6       www      1508:                             $r->print('. ');
1.7       www      1509:                             if ($flushc>15) {
                   1510: 				$r->rflush;
                   1511:                                 $flushc=0;
                   1512:                             }
1.6       www      1513:                         }
1.25      matthew  1514:                     } else {
                   1515:                         $r->print("<p><b>No password for $username</b><p>");
                   1516:                     }
                   1517:                 }
1.26      matthew  1518:             }
                   1519:         } # end of foreach (@studentdata)
1.55      matthew  1520:         $r->print('<p>Processed Students: '.$count.'</p>');
                   1521:         $r->print("<p>If active, the new role will be available when the ".
                   1522:                   "students next log in to LON-CAPA.</p>");
1.26      matthew  1523:         #####################################
                   1524:         #           Drop students           #
                   1525:         #####################################
1.25      matthew  1526:         if ($ENV{'form.fullup'} eq 'yes') {
                   1527:             $r->print('<h3>Dropping Students</h3>');
1.26      matthew  1528:             #  Get current classlist
1.56      matthew  1529:             my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
                   1530:             if (! defined($classlist)) {
                   1531:                 $r->print("There are no students currently enrolled.\n");
                   1532:             } else {
                   1533:                 # Remove the students we just added from the list of students.
1.25      matthew  1534:                 foreach (@studentdata) {
                   1535:                     my %entries=&Apache::loncommon::record_sep($_);
                   1536:                     unless (($entries{$fields{'username'}} eq '') ||
                   1537:                             (!defined($entries{$fields{'username'}}))) {
1.56      matthew  1538:                         delete($classlist->{$entries{$fields{'username'}}.
1.26      matthew  1539:                                                 ':'.$domain});
1.25      matthew  1540:                     }
                   1541:                 }
1.56      matthew  1542:                 # Print out list of dropped students.
                   1543:                 &show_drop_list($r,$classlist,$keylist,'nosort');
1.25      matthew  1544:             }
                   1545:         }
1.26      matthew  1546:     } # end of unless
1.10      www      1547: }
                   1548: 
1.11      www      1549: # ================================================================== Phase four
                   1550: sub drop_student_list {
                   1551:     my $r=shift;
                   1552:     my $count=0;
1.35      matthew  1553:     my @droplist;
                   1554:     if (ref($ENV{'form.droplist'})) {
                   1555:         @droplist = @{$ENV{'form.droplist'}};
                   1556:     } else {
                   1557:         @droplist = ($ENV{'form.droplist'});
                   1558:     }
                   1559:     foreach (@droplist) {
1.26      matthew  1560:         my ($uname,$udom)=split(/\:/,$_);
1.56      matthew  1561:         # drop student
1.35      matthew  1562:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
1.37      matthew  1563:         if ($result eq 'ok' || $result eq 'ok:') {
1.54      matthew  1564:             $r->print('Dropped '.$uname.' @ '.$udom.'<br>');
1.59      matthew  1565:             $count++;
1.35      matthew  1566:         } else {
1.54      matthew  1567:             $r->print('Error dropping '.$uname.' @ '.$udom.': '.$result.
1.35      matthew  1568:                       '<br />');
                   1569:         }
1.20      harris41 1570:     }
1.11      www      1571:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
1.59      matthew  1572:     $r->print('<p>Re-enrollment will re-activate data.') if ($count);
1.11      www      1573: }
                   1574: 
1.50      matthew  1575: ###################################################################
                   1576: ###################################################################
                   1577: 
                   1578: =pod
                   1579: 
                   1580: =item &handler
                   1581: 
                   1582: The typical handler you see in all these modules.  Takes $r, the
                   1583: http request, as an argument.  
                   1584: 
                   1585: The response to the request is governed by two form variables
                   1586: 
                   1587:  form.action      form.state     response
                   1588:  ---------------------------------------------------
                   1589:  undefined        undefined      print main menu
                   1590:  upload           undefined      print courselist upload menu
                   1591:  upload           got_file       deal with uploaded file,
                   1592:                                  print the upload managing menu
                   1593:  upload           enrolling      enroll students based on upload
                   1594:  drop             undefined      print the classlist ready to drop
                   1595:  drop             done           drop the selected students
                   1596:  enrollstudent    undefined      print single student enroll menu
                   1597:  enrollstudent    enrolling      enroll student
                   1598:  classlist        undefined      print html classlist
                   1599:  classlist        csv            print csv classlist
                   1600:  modifystudent    undefined      print classlist to select student to modify
                   1601:  modifystudent    selected       print modify student menu
                   1602:  modifystudent    done           make modifications to student record
                   1603: 
                   1604: =cut
                   1605: 
                   1606: ###################################################################
                   1607: ###################################################################
1.10      www      1608: sub handler {
1.26      matthew  1609:     my $r=shift;
                   1610:     if ($r->header_only) {
                   1611:         $r->content_type('text/html');
                   1612:         $r->send_http_header;
                   1613:         return OK;
                   1614:     }
1.48      matthew  1615:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.50      matthew  1616:                                             ['action','state']);
1.26      matthew  1617:     #  Needs to be in a course
1.50      matthew  1618:     if (! (($ENV{'request.course.fn'}) &&
                   1619:           (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
                   1620:         # Not in a course, or not allowed to modify parms
                   1621:         $ENV{'user.error.msg'}=
                   1622:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
                   1623:         return HTTP_NOT_ACCEPTABLE; 
                   1624:     }
                   1625:     #
                   1626:     # Only output the header information if they did not request csv format
                   1627:     #
                   1628:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1629:         $r->content_type('text/csv');
                   1630:     } else {
1.26      matthew  1631:         # Start page
1.50      matthew  1632:         $r->content_type('text/html');
1.26      matthew  1633:         $r->send_http_header;
1.50      matthew  1634:         $r->print(&header());
                   1635:     }
                   1636:     #
                   1637:     # Main switch on form.action and form.state, as appropriate
                   1638:     if (! exists($ENV{'form.action'})) {
                   1639:         &print_main_menu($r);
                   1640:     } elsif ($ENV{'form.action'} eq 'upload') {
                   1641:         if (! exists($ENV{'form.state'})) {
                   1642:             &print_first_courselist_upload_form($r);            
                   1643:         } elsif ($ENV{'form.state'} eq 'got_file') {
                   1644:             &print_upload_manager_form($r);
                   1645:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1646:             if ($ENV{'form.datatoken'}) {
                   1647:                 &upfile_drop_add($r);
1.50      matthew  1648:             } else {
                   1649:                 # Hmmm, this is an error
1.26      matthew  1650:             }
1.50      matthew  1651:         } else {
                   1652:             &print_first_courselist_upload_form($r);            
1.26      matthew  1653:         }
1.50      matthew  1654:     } elsif ($ENV{'form.action'} eq 'drop') {
                   1655:         if (! exists($ENV{'form.state'})) {
1.51      matthew  1656:             &print_drop_menu($r);
1.50      matthew  1657:         } elsif ($ENV{'form.state'} eq 'done') {
1.26      matthew  1658:             &drop_student_list($r);
1.50      matthew  1659:         } else {
1.55      matthew  1660:             &print_drop_menu($r);
1.26      matthew  1661:         }
1.50      matthew  1662:     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
                   1663:         if (! exists($ENV{'form.state'})) {
                   1664:             &print_enroll_single_student_form($r);
                   1665:         } elsif ($ENV{'form.state'} eq 'enrolling') {
1.26      matthew  1666:             &enroll_single_student($r);
1.50      matthew  1667:         } else {
                   1668:             &print_enroll_single_student_form($r);
1.26      matthew  1669:         }
1.50      matthew  1670:     } elsif ($ENV{'form.action'} eq 'classlist') {
                   1671:         if (! exists($ENV{'form.state'})) {
                   1672:             &print_html_classlist($r);
                   1673:         } elsif ($ENV{'form.state'} eq 'csv') {
1.60      matthew  1674:             &print_formatted_classlist($r,'csv');
                   1675:         } elsif ($ENV{'form.state'} eq 'excel') {
                   1676:             &print_formatted_classlist($r,'excel');
1.50      matthew  1677:         } else {
                   1678:             &print_html_classlist($r);
                   1679:         }
                   1680:     } elsif ($ENV{'form.action'} eq 'modifystudent') {
                   1681:         if (! exists($ENV{'form.state'})) {
                   1682:             &print_html_classlist($r);
                   1683:         } elsif ($ENV{'form.state'} eq 'selected') {
                   1684:             &print_modify_student_form($r);
                   1685:         } elsif ($ENV{'form.state'} eq 'done') {
                   1686:             &modify_single_student($r);
                   1687:         } else {
                   1688:             &print_html_classlist($r);
                   1689:         }        
                   1690:     } else {
                   1691:         # We should not end up here, but I guess it is possible
                   1692:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
                   1693:                                  "form.action = ".$ENV{'form.action'}.
                   1694:                                  "Someone should fix this.");
                   1695:         &print_main_menu($r);
                   1696:     }
                   1697:     #
                   1698:     # Finish up
                   1699:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
                   1700:         $r->print("\n");
1.26      matthew  1701:     } else {
1.50      matthew  1702:         $r->print('</form></body></html>');
1.26      matthew  1703:     }
                   1704:     return OK;
1.1       www      1705: }
                   1706: 
1.50      matthew  1707: ###################################################################
                   1708: ###################################################################
                   1709: 
1.1       www      1710: 1;
                   1711: __END__
1.50      matthew  1712: 
1.1       www      1713: 

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