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

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

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