File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.86: download - view: text, annotated - select for diffs
Mon Sep 29 16:09:20 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Various small internationalization bugfixes.

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

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