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

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

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