File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.90: download - view: text, annotated - select for diffs
Fri Nov 7 21:04:37 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: version_1_0_99, HEAD
Lots of localization of enrollment forms and authentications forms.
Minor comment/POD changes to loncommon.pm in addition to localization.

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

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