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

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

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