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

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

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