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

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

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