File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.87: download - view: text, annotated - select for diffs
Wed Nov 5 20:55:59 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fixed bug which made it impossible to revoke the role of student for a
student who had previously had a section but later did not have a section.
Some better error reporting was added as well (returning '666' on error
doesn't seem like a bright idea now).

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # $Id: londropadd.pm,v 1.87 2003/11/05 20:55:59 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:     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 (defined($csec) && $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:     #
  759:     my $user_data_html = '';
  760:     my $javascript_validations = '';
  761:     if ($new_user) {
  762:         my $defdom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
  763:         # Set up authentication forms
  764:         my ($krbdef,$krbdefdom) =
  765:             &Apache::loncommon::get_kerberos_defaults($domain);
  766:         $javascript_validations=&javascript_validations($krbdefdom);
  767:         my %param = ( formname => 'document.studentform',
  768:                       kerb_def_dom => $krbdefdom,
  769:                       kerb_def_auth => $krbdef
  770:                       );
  771:         my $krbform = &Apache::loncommon::authform_kerberos(%param);
  772:         my $intform = &Apache::loncommon::authform_internal(%param);
  773:         my $locform = &Apache::loncommon::authform_local(%param);
  774:         #
  775:         # Set up domain selection form
  776:         my $homeserver_form = '';
  777:         my %servers = &Apache::loncommon::get_library_servers($domain);
  778:         $homeserver_form = '<select name="lcserver" size="1">'."\n".
  779:             '<option value="default" selected>default</option>'."\n";
  780:         while (my ($servername,$serverdescription) = each (%servers)) {
  781:             $homeserver_form .= '<option value="'.$servername.'">'.
  782:                 $serverdescription."</option>\n";
  783:         }
  784:         $homeserver_form .= "</select>\n";
  785:         #
  786:         #
  787:         $user_data_html = <<END;
  788: <h3>User Data for $username\@$domain</h3>
  789: <table>
  790: <tr><th>First Name:</th>
  791:     <td><input type="text" name="cfirst"  size="15"></td></tr>
  792: <tr><th>Middle Name:</th>
  793:     <td><input type="text" name="cmiddle" size="15"></td></tr>
  794: <tr><th>Last Name:</th>
  795:     <td><input type="text" name="clast"   size="15"></td></tr>
  796: <tr><th>Generation:</th>
  797:     <td><input type="text" name="cgen"    size="5"> </td></tr>
  798: <tr><th>Home Server:</th>
  799:     <td>$homeserver_form</td></tr>
  800: </table>
  801: <h3>Password</h3>
  802: Please select an authentication mechanism
  803: <table>
  804: <p>
  805: $krbform
  806: <br />
  807: $intform
  808: <br />
  809: $locform
  810: </p>
  811: END
  812:     } else {
  813:         # User already exists.  Do not worry about authentication
  814:         my %uenv = &Apache::lonnet::dump('environment',$domain,$username);
  815:         $javascript_validations = &javascript_validations_without_auth();
  816:         $user_data_html = <<END;
  817: <h3>User Data for $username\@$domain</h3>
  818: <input type="hidden" name="lcserver" value="default" />
  819: <table>
  820: <tr><th>First Name:</th>
  821:     <td>
  822:     <input type="text" name="cfirst" value="$uenv{'firstname'}" size="15" />
  823:     </td></tr>
  824: <tr><th>Middle Name:</th>
  825:     <td>
  826:     <input type="text" name="cmiddle" value="$uenv{'middlename'}" size="15" />
  827:     </td></tr>
  828: <tr><th>Last Name:</th>
  829:     <td>
  830:     <input type="text" name="clast"value="$uenv{'lastname'}" size="15" />
  831:     </td></tr>
  832: <tr><th>Generation:</th>
  833:     <td>
  834:     <input type="text" name="cgen" value="$uenv{'generation'}" size="5" />
  835:     </td></tr>
  836: </table>
  837: END
  838:     }
  839:     my $date_table = &date_setting_table();
  840:         # Print it all out
  841:     $r->print(<<END);
  842: <input type="hidden" name="action" value="enrollstudent" />
  843: <input type="hidden" name="state"  value="done" />
  844: <input type="hidden" name="cuname" value="$username" />
  845: <input type="hidden" name="lcdomain" value="$domain" />
  846: <script type="text/javascript" language="Javascript">
  847: function verify(vf) {
  848:     var founduname=0;
  849:     var foundpwd=0;
  850:     var foundname=0;
  851:     var foundid=0;
  852:     var foundsec=0;
  853:     var tw;
  854:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
  855: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
  856:         founduname=1;
  857:     }
  858:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
  859: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
  860:         foundname=1;
  861:     }
  862:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
  863:         foundsec=1;
  864:     }
  865:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
  866: 	foundid=1;
  867:     }
  868:     if (founduname==0) {
  869: 	alert('You need to specify at least the username and domain fields');
  870:         return;
  871:     }
  872:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  873: }
  874: 
  875: $javascript_validations
  876: 
  877: function clearpwd(vf) {
  878:     //nothing else needs clearing
  879: }
  880: 
  881: </script>
  882: 
  883: $user_data_html
  884: 
  885: <h3>Course Data</h3>
  886: 
  887: <p>Group/Section: <input type="text" name="csec" size="5" />
  888: <p>
  889: $date_table
  890: </p>
  891: <h3>ID/Student Number</h3>
  892: <p>
  893: ID/Student Number: <input type="text" name="cstid" size="10">
  894: </p><p>
  895: <input type="checkbox" name="forceid" value="yes"> 
  896: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  897: (only do if you know what you are doing)
  898: </p><p>
  899: <input type="button" onClick="verify(this.form)" value="Enroll as student">
  900: </p>
  901: END
  902:     return;
  903: }
  904: 
  905: # ========================================================= Menu Phase Two Drop
  906: sub print_drop_menu {
  907:     my $r=shift;
  908:     $r->print("<h3>Drop Students</h3>");
  909:     my $cid=$ENV{'request.course.id'};
  910:     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
  911:     if (! defined($classlist)) {
  912:         $r->print("There are no students currently enrolled.\n");
  913:         return;
  914:     }
  915:     # Print out the available choices
  916:     &show_drop_list($r,$classlist,$keylist);
  917:     return;
  918: }
  919: 
  920: # ============================================== view classlist
  921: sub print_html_classlist {
  922:     my $r=shift;
  923:     if (! exists($ENV{'form.sortby'})) {
  924:         $ENV{'form.sortby'} = 'username';
  925:     }
  926:     if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
  927:         $ENV{'form.Status'} = 'Active';
  928:     }
  929:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
  930:         ($ENV{'form.Status'},'studentform');
  931:     $r->print(<<END);
  932: <input type="hidden" name="action" value="$ENV{'form.action'}" />
  933: <input type="hidden" name="state"  value="" />
  934: <p>
  935: <font size="+1">Current Class List</font>
  936: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  937: END
  938:     if ($ENV{'form.action'} ne 'modifystudent') {
  939:         $r->print(<<END);
  940: <font size="+1">
  941: <a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
  942: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  943: <a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
  944: </font>
  945: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  946: Student Status:
  947: END
  948:     }
  949:     $r->print($status_select."</p>\n");
  950:     my $cid=$ENV{'request.course.id'};
  951:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
  952:     if (! defined($classlist)) {
  953:         $r->print("There are no students currently enrolled.\n");
  954:     } else {
  955:         # Print out the available choices
  956:         if ($ENV{'form.action'} eq 'modifystudent') {
  957:             &show_class_list($r,'view','modify','modifystudent',
  958:                              $ENV{'form.Status'},$classlist,$keylist);
  959:         } else {
  960:             &show_class_list($r,'view','aboutme','classlist',
  961:                              $ENV{'form.Status'},$classlist,$keylist);
  962:         }
  963:     }
  964: }
  965: 
  966: # ============================================== view classlist
  967: sub print_formatted_classlist {
  968:     my $r=shift;
  969:     my $mode = shift;
  970:     my $cid=$ENV{'request.course.id'};
  971:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
  972:     if (! defined($classlist)) {
  973:         $r->print("There are no students currently enrolled.\n");
  974:     } else {
  975:         &show_class_list($r,$mode,'nolink','csv',
  976:                          $ENV{'form.Status'},$classlist,$keylist);
  977:     }
  978: }
  979: 
  980: # =================================================== Show student list to drop
  981: sub show_class_list {
  982:     my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
  983:     my $cid=$ENV{'request.course.id'};
  984:     #
  985:     # Variables for excel output
  986:     my ($excel_workbook, $excel_sheet, $excel_filename,$row);
  987:     #
  988:     my $sortby = $ENV{'form.sortby'};
  989:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
  990:         $sortby = 'username';
  991:     }
  992:     # Print out header 
  993:     if ($mode eq 'view') {
  994:         if ($linkto eq 'aboutme') {
  995:             $r->print('Select a user name to view the users personal page.');
  996:         } elsif ($linkto eq 'modify') {
  997:             $r->print('Select a user name to modify the students information');
  998:         }
  999:         $r->print(<<END);
 1000: 
 1001: <input type="hidden" name="sortby" value="$sortby" />
 1002: <input type="hidden" name="sname"  value="" />
 1003: <input type="hidden" name="sdom"   value="" />
 1004: <p>
 1005: <table border=2>
 1006: <tr><th>
 1007:        <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
 1008:     </th><th>
 1009:        <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
 1010:     </th><th>
 1011:        <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
 1012:     </th><th>
 1013:        <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
 1014:     </th><th>
 1015:        <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
 1016:     </th>
 1017: </tr>
 1018: END
 1019:     } elsif ($mode eq 'csv') {
 1020:         if($statusmode eq 'Expired') {
 1021:             $r->print('"Students with expired roles"');
 1022:         }
 1023:         if ($statusmode eq 'Any') {
 1024:             $r->print('"'.join('","',("username","domain","ID","student name",
 1025:                                       "section","status")).'"'."\n");
 1026:         } else {
 1027:             $r->print('"'.join('","',("username","domain","ID","student name",
 1028:                                       "section")).'"'."\n");
 1029:         }
 1030:     } elsif ($mode eq 'excel') {
 1031:         # Create the excel spreadsheet
 1032:         $excel_filename = '/prtspool/'.
 1033:             $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
 1034:                 time.'_'.rand(1000000000).'.xls';
 1035:         $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
 1036:                                                        $excel_filename);
 1037:         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
 1038:         $excel_sheet = $excel_workbook->addworksheet('classlist');
 1039:         #
 1040:         my $description = 'Class List for '.
 1041:             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1042:         $excel_sheet->write($row++,0,$description);
 1043:         #
 1044:         $excel_sheet->write($row++,0,["username","domain","ID",
 1045:                                       "student name","section","status"]);
 1046:     }
 1047:     #
 1048:     # Sort the students
 1049:     my %index;
 1050:     my $i;
 1051:     foreach (@$keylist) {
 1052:         $index{$_} = $i++;
 1053:     }
 1054:     my $index  = $index{$sortby};
 1055:     my $second = $index{'username'};
 1056:     my $third  = $index{'domain'};
 1057:     my @Sorted_Students = sort {
 1058:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
 1059:             ||
 1060:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
 1061:             ||
 1062:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
 1063:         } (keys(%$classlist));
 1064:     foreach my $student (@Sorted_Students) {
 1065:         my $username = $classlist->{$student}->[$index{'username'}];
 1066:         my $domain   = $classlist->{$student}->[$index{'domain'}];
 1067:         my $section  = $classlist->{$student}->[$index{'section'}];
 1068:         my $name     = $classlist->{$student}->[$index{'fullname'}];
 1069:         my $id       = $classlist->{$student}->[$index{'id'}];
 1070:         my $status   = $classlist->{$student}->[$index{'status'}];
 1071:         next if (($statusmode ne 'Any') && ($status ne $statusmode));
 1072:         if ($mode eq 'view') {
 1073:             $r->print("<tr>\n    <td>\n        ");
 1074:             if ($linkto eq 'nothing') {
 1075:                 $r->print($username);
 1076:             } elsif ($linkto eq 'aboutme') {
 1077:                 $r->print(&Apache::loncommon::aboutmewrapper($username,
 1078:                                                              $username,
 1079:                                                              $domain));
 1080:             } elsif ($linkto eq 'modify') {
 1081:                 $r->print('<a href="'.
 1082:                           "javascript:document.studentform.sname.value='".
 1083:                           $username.
 1084:                           "';document.studentform.sdom.value='".$domain.
 1085:                           "';document.studentform.state.value='selected".
 1086:                           "';document.studentform.submit();".'">'.
 1087:                           $username."</a>\n");
 1088:             }
 1089:             $r->print(<<"END");
 1090:     </td>
 1091:     <td>$domain</td>
 1092:     <td>$id</td>
 1093:     <td>$name</td>
 1094:     <td>$section</td>
 1095: </tr>
 1096: END
 1097:         } elsif ($mode eq 'csv') {
 1098:             # no need to bother with $linkto
 1099:             my @line = ();
 1100:             foreach ($username,$domain,$id,$name,$section) {
 1101:                 push @line,&Apache::loncommon::csv_translate($_);
 1102:             }
 1103:             if ($statusmode eq 'Any') {
 1104:                 push @line,&Apache::loncommon::csv_translate($status);
 1105:             }
 1106:             my $tmp = $";
 1107:             $" = '","';
 1108:             $r->print("\"@line\"\n");
 1109:             $" = $tmp;
 1110:         } elsif ($mode eq 'excel') {
 1111:             $excel_sheet->write($row++,0,[$username,$domain,$id,
 1112:                                           $name,$section,$status]);
 1113:         }
 1114:     }
 1115:     if ($mode eq 'view') {
 1116:         $r->print('</table><br>');
 1117:     } elsif ($mode eq 'excel') {
 1118:         $excel_workbook->close();
 1119:         $r->print('<p><a href="'.$excel_filename.'">'.
 1120:                   'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
 1121:     }
 1122: }
 1123: 
 1124: 
 1125: #
 1126: # print out form for modification of a single students data
 1127: #
 1128: sub print_modify_student_form {
 1129:     my $r = shift();
 1130:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1131:                                             ['sdom','sname']);    
 1132:     my $sname  = $ENV{'form.sname'};
 1133:     my $sdom   = $ENV{'form.sdom'};
 1134:     my $sortby = $ENV{'form.sortby'};
 1135:     # determine the students name information
 1136:     my %info=&Apache::lonnet::get('environment',
 1137:                                   ['firstname','middlename',
 1138:                                    'lastname','generation','id'],
 1139:                                   $sdom, $sname);
 1140:     my ($tmp) = keys(%info);
 1141:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
 1142:         $r->print('<font color="#ff0000" size="+2">Error</font>'.
 1143:                   '<p>'.
 1144:                   'Unable to retrieve environment data for '.$sname.
 1145:                   'in domain '.$sdom.'</p><p>'.
 1146:                   'Please contact your LON-CAPA administrator '.
 1147:                   'regarding this situation.</p></body></html>');
 1148:         return;
 1149:     }
 1150:     # determine the students starting and ending times and section
 1151:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
 1152:     if ($starttime =~ /^error/) {
 1153:         $r->print('<h2>Error</h2>');
 1154:         $r->print('<p>'.$starttime.'</p>');
 1155:         return;
 1156:     }
 1157:     # Deal with date forms
 1158:     my $date_table = &date_setting_table($starttime,$endtime);
 1159:     #
 1160:     if (! exists($ENV{'form.Status'}) || 
 1161:         $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
 1162:         $ENV{'form.Status'} = 'crap';
 1163:     }
 1164:     # Make sure student is enrolled in course    
 1165:     $r->print(<<END);
 1166: <p>
 1167: <font size="+1">
 1168: Only domain coordinators can change a users password.
 1169: </font>
 1170: </p>
 1171: <input type="hidden" name="slogin"  value="$sname"  />
 1172: <input type="hidden" name="sdomain" value="$sdom" />
 1173: <input type="hidden" name="action"  value="modifystudent" />
 1174: <input type="hidden" name="state"   value="done" />
 1175: <input type="hidden" name="sortby"  value="$sortby" />
 1176: <input type="hidden" name="Status"  value="$ENV{'form.Status'}" />
 1177: 
 1178: <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
 1179: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
 1180: <p>
 1181: <b>Student Name</b>
 1182: <table>
 1183: <tr><th>First</th><th>Middle</th><th>Last</th><th>Generation</th></tr>
 1184: <tr><td>
 1185: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
 1186: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
 1187: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
 1188: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
 1189: </table>
 1190: </p><p>
 1191: <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
 1192: </p><p>
 1193: <input type="checkbox" name="forceid" > 
 1194: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
 1195: (only do if you know what you are doing)
 1196: </p><p>
 1197: <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
 1198: </p>
 1199: <p>$date_table</p>
 1200: <input type="submit" value="Submit Modifications" />
 1201: </body></html>
 1202: END
 1203:     return;
 1204: }
 1205: 
 1206: #
 1207: # modify a single students section 
 1208: #
 1209: sub modify_single_student {
 1210:     my $r = shift;
 1211:     #
 1212:     # Remove non alphanumeric values from the section
 1213:     $ENV{'form.section'} =~ s/\W//g;
 1214:     #
 1215:     # Do the date defaults first
 1216:     my ($starttime,$endtime) = &get_dates_from_form();
 1217:     if ($ENV{'form.makedatesdefault'}) {
 1218:         $r->print(&make_dates_default($starttime,$endtime));
 1219:     }
 1220:     # Get the 'sortby' and 'Status' variables so the user goes back to their
 1221:     # previous screen
 1222:     my $sortby = $ENV{'form.sortby'};
 1223:     my $status = $ENV{'form.Status'};
 1224:     #
 1225:     # We always need this information
 1226:     my $slogin     = $ENV{'form.slogin'};
 1227:     my $sdom       = $ENV{'form.sdomain'};
 1228:     #
 1229:     # Get the old data
 1230:     my %old=&Apache::lonnet::get('environment',
 1231:                                  ['firstname','middlename',
 1232:                                   'lastname','generation','id'],
 1233:                                  $sdom, $slogin);
 1234:     $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
 1235:                                                   $ENV{'request.course.id'});
 1236:     my ($tmp) = keys(%old);
 1237:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
 1238:         $r->print("There was an error determining the environment values ".
 1239:                   " for $slogin \@ $sdom.");
 1240:         return;
 1241:     }
 1242:     undef $tmp;
 1243:     #
 1244:     # Get the new data
 1245:     my $firstname  = $ENV{'form.firstname'};
 1246:     my $middlename = $ENV{'form.middlename'};
 1247:     my $lastname   = $ENV{'form.lastname'};
 1248:     my $generation = $ENV{'form.generation'};
 1249:     my $section    = $ENV{'form.section'};
 1250:     my $courseid   = $ENV{'request.course.id'};
 1251:     my $sid        = $ENV{'form.id'};
 1252:     my $displayable_starttime = localtime($starttime);
 1253:     my $displayable_endtime   = localtime($endtime);
 1254:     # 
 1255:     # check for forceid override
 1256:     if ((defined($old{'id'})) && ($old{'id'} ne '') && 
 1257:         ($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
 1258:         $r->print("<font color=\"ff0000\">You changed the students id ".
 1259:                   " but did not disable the ID change safeguard.".
 1260:                   "  The students id will not be changed.</font>");
 1261:         $sid = $old{'id'};
 1262:     }
 1263:     #
 1264:     # talk to the user about what we are going to do
 1265:     $r->print(<<END);
 1266:     <h2>Modifying data for user $slogin \@ $sdom </h2>
 1267: <h3>Student Information</h3>
 1268: <table rules="rows" border="1" cellpadding="3" >
 1269: <tr>
 1270:     <th> Field </th>
 1271:     <th> Old Value </th>
 1272:     <th> New Value </th>
 1273: </tr>
 1274: <tr>
 1275:     <td> <b>First name</b> </td>
 1276:     <td> $old{'firstname'} </td>
 1277:     <td> $firstname </td>
 1278: </tr><tr>
 1279:     <td> <b>Middle name</b> </td>
 1280:     <td> $old{'middlename'} </td>
 1281:     <td> $middlename </td>
 1282: </tr><tr>
 1283:     <td> <b>Last name</b> </td>
 1284:     <td> $old{'lastname'} </td>
 1285:     <td> $lastname </td>
 1286: </tr><tr>
 1287:     <td> <b>Generation</b> </td>
 1288:     <td> $old{'generation'} </td>
 1289:     <td> $generation </td>
 1290: </tr><tr>
 1291:     <td> <b>ID</b> </td>
 1292:     <td> $old{'id'} </td>
 1293:     <td> $sid </td>
 1294: </tr><tr>
 1295:     <td> <b>Section</b> </td>
 1296:     <td> $old{'section'} </td>
 1297:     <td> $section</td>
 1298: </tr>
 1299: </table>
 1300: <h3>Role Information</h3>
 1301: <table>
 1302: <tr><td align="right"><b>Start Time:</b></td><td> $displayable_starttime </td></tr>
 1303: <tr><td align="right"><b>End Time:</b></td><td> $displayable_endtime   </td></tr>
 1304: </table>
 1305: <p>
 1306: END
 1307:     #
 1308:     # Send request(s) to modify data (final undef is for 'desiredhost',
 1309:     # which is a moot point because the student already has an account.
 1310:     my $modify_section_results = &modifystudent($sdom,$slogin,
 1311:                                                 $ENV{'request.course.id'},
 1312:                                                 $section,undef);
 1313:     if ($modify_section_results !~ /^ok/) {
 1314:         $r->print("An error occured during the attempt to change the ".
 1315:                   "section for this student.<br />");
 1316:     }
 1317:     my $roleresults = &Apache::lonnet::modifystudent
 1318:         ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
 1319:          $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
 1320:     if ($roleresults eq 'refused' ) {
 1321:         $r->print("Your request to change the role information for this ".
 1322:                   "student was refused.  You do not appear to have ".
 1323:                   "sufficient authority to change student information.");
 1324:     } elsif ($roleresults !~ /ok/) {
 1325:         $r->print("An error occurred during the attempt to change the role".
 1326:                   " information for this student.  <br />".
 1327:                   "The error reported was ".
 1328:                   $roleresults);
 1329:         &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
 1330:                                  " data for ".$slogin." \@ ".$sdom." by ".
 1331:                                  $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
 1332:                                  ":".$roleresults);
 1333:     } else { # everything is okay!
 1334:         $r->print("Student information updated successfully. <br />".
 1335:                   "The student must log out and log in again to see ".
 1336:                   "these changes.");
 1337:     }
 1338:     $r->print(<<END);
 1339: </p><p>
 1340: <input type="hidden" name="action" value="modifystudent" />
 1341: <input type="hidden" name="sortby" value="$sortby" />
 1342: <input type="hidden" name="Status" value="$status" />
 1343: <a href="javascript:document.studentform.submit();">Modify another students data</a>
 1344: </body></html>
 1345: END
 1346:     return;
 1347: }
 1348: 
 1349: sub get_enrollment_data {
 1350:     my ($sname,$sdomain) = @_;
 1351:     my $courseid = $ENV{'request.course.id'};
 1352:     $courseid =~ s:_:/:g;
 1353:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
 1354:     my ($tmp) = keys(%roles);
 1355:     # Bail out if we were unable to get the students roles
 1356:     return ('error'.$tmp) if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 1357:     # Go through the roles looking for enrollment in this course
 1358:     my ($end,$start) = (undef,undef);
 1359:     my $section = '';
 1360:     my $count = scalar(keys(%roles));
 1361:     while (my ($course,$role) = each(%roles)) {
 1362:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
 1363:             #
 1364:             # Get active role
 1365:             $section=$1;
 1366:             (undef,$end,$start)=split(/\_/,$role);
 1367:             my $now=time;
 1368:             my $notactive=0;
 1369:             if ($start) {
 1370:                 if ($now<$start) { $notactive=1; }
 1371:             }
 1372:             if ($end) {
 1373:                 if ($now>$end) { $notactive=1; }
 1374:             } 
 1375:             unless ($notactive) { return ($start,$end,$section); }
 1376:         }
 1377:     }
 1378:     return ($start,$end,$section);
 1379: }
 1380: 
 1381: #################################################
 1382: #################################################
 1383: 
 1384: =pod
 1385: 
 1386: =item show_drop_list
 1387: 
 1388: Display a list of students to drop
 1389: Inputs: 
 1390: 
 1391: =over 4
 1392: 
 1393: =item $r, Apache request
 1394: 
 1395: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
 1396: 
 1397: =item $keylist, array pointer returned from loncoursedata::get_classlist() 
 1398: which describes the order elements are stored in the %$classlist values.
 1399: 
 1400: =item $nosort, if true, sorting links are omitted.
 1401: 
 1402: =back
 1403: 
 1404: =cut
 1405: 
 1406: #################################################
 1407: #################################################
 1408: sub show_drop_list {
 1409:     my ($r,$classlist,$keylist,$nosort)=@_;
 1410:     my $cid=$ENV{'request.course.id'};
 1411:     if (! exists($ENV{'form.sortby'})) {
 1412:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1413:                                                 ['sortby']);
 1414:     }
 1415:     my $sortby = $ENV{'form.sortby'};
 1416:     if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
 1417:         $sortby = 'username';
 1418:     }
 1419:     #
 1420:     my $action = "drop";
 1421:     $r->print(<<END);
 1422: <input type="hidden" name="sortby" value="$sortby" />
 1423: <input type="hidden" name="action" value="$action" />
 1424: <input type="hidden" name="state"  value="done" />
 1425: <script>
 1426: function checkAll(field) {
 1427:     for (i = 0; i < field.length; i++)
 1428:         field[i].checked = true ;
 1429: }
 1430: 
 1431: function uncheckAll(field) {
 1432:     for (i = 0; i < field.length; i++)
 1433:         field[i].checked = false ;
 1434: }
 1435: </script>
 1436: <p>
 1437: <input type="hidden" name="phase" value="four">
 1438: END
 1439: 
 1440:     if ($nosort) {
 1441:         $r->print(<<END);
 1442: <table border=2>
 1443: <tr>
 1444:     <th>&nbsp;</th>
 1445:     <th>username</th>
 1446:     <th>domain</th>
 1447:     <th>ID</th>
 1448:     <th>student name</th>
 1449:     <th>section</th>
 1450: </tr>
 1451: END
 1452: 
 1453:     } else  {
 1454:         $r->print(<<END);
 1455: <table border=2>
 1456: <tr><th>&nbsp;</th>
 1457:     <th>
 1458:        <a href="/adm/dropadd?action=$action&sortby=username">username</a>
 1459:     </th><th>
 1460:        <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
 1461:     </th><th>
 1462:        <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
 1463:     </th><th>
 1464:        <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
 1465:     </th><th>
 1466:        <a href="/adm/dropadd?action=$action&sortby=section">section</a>
 1467:     </th>
 1468: </tr>
 1469: END
 1470:     }
 1471:     #
 1472:     # Sort the students
 1473:     my %index;
 1474:     my $i;
 1475:     foreach (@$keylist) {
 1476:         $index{$_} = $i++;
 1477:     }
 1478:     my $index  = $index{$sortby};
 1479:     my $second = $index{'username'};
 1480:     my $third  = $index{'domain'};
 1481:     my @Sorted_Students = sort {
 1482:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
 1483:             ||
 1484:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
 1485:             ||
 1486:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
 1487:         } (keys(%$classlist));
 1488:     foreach my $student (@Sorted_Students) {
 1489:         my $error;
 1490:         my $username = $classlist->{$student}->[$index{'username'}];
 1491:         my $domain   = $classlist->{$student}->[$index{'domain'}];
 1492:         my $section  = $classlist->{$student}->[$index{'section'}];
 1493:         my $name     = $classlist->{$student}->[$index{'fullname'}];
 1494:         my $id       = $classlist->{$student}->[$index{'id'}];
 1495:         my $status   = $classlist->{$student}->[$index{'status'}];
 1496:         next if ($status ne 'Active');
 1497:         #
 1498:         $r->print(<<"END");
 1499: <tr>
 1500:     <td><input type="checkbox" name="droplist" value="$student"></td>
 1501:     <td>$username</td>
 1502:     <td>$domain</td>
 1503:     <td>$id</td>
 1504:     <td>$name</td>
 1505:     <td>$section</td>
 1506: </tr>
 1507: END
 1508:     }
 1509:     $r->print('</table><br>');
 1510:     $r->print(<<"END");
 1511: </p><p>
 1512: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
 1513: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
 1514: <p><input type=submit value="Drop Students"></p>
 1515: END
 1516:     return;
 1517: }
 1518: 
 1519: #
 1520: # Print out the initial form to get the courselist file
 1521: #
 1522: sub print_first_courselist_upload_form {
 1523:     my $r=shift;
 1524:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1525:     my $create_classlist_help = 
 1526: 	&Apache::loncommon::help_open_topic("Course_Create_Class_List",
 1527:            "How do I create a class list from a spreadsheet");
 1528:     my $create_csv_help =
 1529: 	&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
 1530:            "How do I create a CSV file from a spreadsheet");
 1531:     $r->print(<<ENDUPFORM);
 1532: <input type=hidden name=phase value=two>
 1533: <h3>Upload a courselist</h3>
 1534: $upfile_select
 1535: <p>
 1536: <input type=submit name="fileupload" value="Upload Courselist">
 1537: <input type="hidden" name="action" value="upload" />
 1538: <input type="hidden" name="state"  value="got_file" />
 1539: <input type="checkbox" name="noFirstLine" /> Ignore First Line<br />
 1540: </p>
 1541: $create_classlist_help <br />
 1542: $create_csv_help
 1543: </body></html>
 1544: ENDUPFORM
 1545:     return;
 1546: }
 1547: 
 1548: # ================================================= Drop/Add from uploaded file
 1549: sub upfile_drop_add {
 1550:     my $r=shift;
 1551:     &Apache::loncommon::load_tmp_file($r);
 1552:     my @studentdata=&Apache::loncommon::upfile_record_sep();
 1553:     if($ENV{'form.noFirstLine'}){shift(@studentdata);}
 1554:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
 1555:     my $cid = $ENV{'request.course.id'};
 1556:     my %fields=();
 1557:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
 1558:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
 1559:             if ($ENV{'form.f'.$i} ne 'none') {
 1560:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
 1561:             }
 1562:         } else {
 1563:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
 1564:         }
 1565:     }
 1566:     #
 1567:     my ($startdate,$enddate) = &get_dates_from_form();
 1568:     if ($ENV{'form.makedatesdefault'}) {
 1569:         $r->print(&make_dates_default($startdate,$enddate));
 1570:     }
 1571:     # Determine domain and desired host (home server)
 1572:     my $domain=$ENV{'form.lcdomain'};
 1573:     my $desiredhost = $ENV{'form.lcserver'};
 1574:     if (lc($desiredhost) eq 'default') {
 1575:         $desiredhost = undef;
 1576:     } else {
 1577:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
 1578:         if (! exists($home_servers{$desiredhost})) {
 1579:             $r->print('<font color="#ff0000">Error:</font>'.
 1580:                       'Invalid home server specified');
 1581:             return;
 1582:         }
 1583:     }
 1584:     # Determine authentication mechanism
 1585:     my $amode  = '';
 1586:     my $genpwd = '';
 1587:     if ($ENV{'form.login'} eq 'krb') {
 1588:         $amode='krb';
 1589: 	$amode.=$ENV{'form.krbver'};
 1590:         $genpwd=$ENV{'form.krbarg'};
 1591:     } elsif ($ENV{'form.login'} eq 'int') {
 1592:         $amode='internal';
 1593:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
 1594:             $genpwd=$ENV{'form.intarg'};
 1595:         }
 1596:     } elsif ($ENV{'form.login'} eq 'loc') {
 1597:         $amode='localauth';
 1598:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
 1599:             $genpwd=$ENV{'form.locarg'};
 1600:         }
 1601:     }
 1602:     if ($amode =~ /^krb/) {
 1603:         if (! defined($genpwd) || $genpwd eq '') {
 1604:             $r->print('<font color="red" size="+1">'.
 1605:                       'Unable to enroll students:'.'</font>  '.
 1606:                       'No Kerberos domain was specified.</p>');
 1607:             $amode = ''; # This causes the loop below to be skipped
 1608:         }
 1609:     }
 1610:     unless (($domain=~/\W/) || ($amode eq '')) {
 1611:         #######################################
 1612:         ##         Enroll Students           ##
 1613:         #######################################
 1614:         $r->print('<h3>Enrolling Students</h3>'."\n".'<p>');
 1615:         my $count=0;
 1616:         my $flushc=0;
 1617:         my %student=();
 1618:         # Get new classlist
 1619:         foreach (@studentdata) {
 1620:             my %entries=&Apache::loncommon::record_sep($_);
 1621:             # Determine student name
 1622:             unless (($entries{$fields{'username'}} eq '') ||
 1623:                     (!defined($entries{$fields{'username'}}))) {
 1624:                 my ($fname, $mname, $lname,$gen) = ('','','','');
 1625:                 if (defined($fields{'names'})) {
 1626:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
 1627:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
 1628:                 } else {
 1629:                     if (defined($fields{'fname'})) {
 1630:                         $fname=$entries{$fields{'fname'}};
 1631:                     }
 1632:                     if (defined($fields{'mname'})) {
 1633:                         $mname=$entries{$fields{'mname'}};
 1634:                     }
 1635:                     if (defined($fields{'lname'})) {
 1636:                         $lname=$entries{$fields{'lname'}};
 1637:                     }
 1638:                     if (defined($fields{'gen'})) {
 1639:                         $gen=$entries{$fields{'gen'}};
 1640:                     }
 1641:                 }
 1642:                 if ($entries{$fields{'username'}}=~/\W/) {
 1643:                     $r->print('<br /><b>Unacceptable username: '.
 1644:                               $entries{$fields{'username'}}.' for user '.
 1645:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b>');
 1646:                 } else {
 1647:                     # determine section number
 1648:                     my $sec='';
 1649:                     my $username=$entries{$fields{'username'}};
 1650:                     if (defined($fields{'sec'})) {
 1651:                         if (defined($entries{$fields{'sec'}})) {
 1652:                             $sec=$entries{$fields{'sec'}};
 1653:                         }
 1654:                     }
 1655:                     # remove non alphanumeric values from section
 1656:                     $sec =~ s/\W//g;
 1657:                     # determine student id number
 1658:                     my $id='';
 1659:                     if (defined($fields{'id'})) {
 1660:                         if (defined($entries{$fields{'id'}})) {
 1661:                             $id=$entries{$fields{'id'}};
 1662:                         }
 1663:                         $id=~tr/A-Z/a-z/;
 1664:                     }
 1665:                     # determine email address
 1666:                     my $email='';
 1667:                     if (defined($fields{'email'})) {
 1668:                         if (defined($entries{$fields{'email'}})) {
 1669:                             $email=$entries{$fields{'email'}};
 1670:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
 1671:                         }
 1672:                     }
 1673:                     # determine student password
 1674:                     my $password='';
 1675:                     if ($genpwd) { 
 1676:                         $password=$genpwd; 
 1677:                     } else {
 1678:                         if (defined($fields{'ipwd'})) {
 1679:                             if ($entries{$fields{'ipwd'}}) {
 1680:                                 $password=$entries{$fields{'ipwd'}};
 1681:                             }
 1682:                         }
 1683:                     }
 1684:                     # Clean up whitespace
 1685:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
 1686:                              \$lname,\$gen,\$sec) {
 1687:                         $$_ =~ s/(\s+$|^\s+)//g;
 1688:                     }
 1689:                     if ($password || $ENV{'form.login'} eq 'loc') {
 1690:                         &modifystudent($domain,$username,$cid,$sec,
 1691:                                        $desiredhost);
 1692:                         my $reply=&Apache::lonnet::modifystudent
 1693:                             ($domain,$username,$id,$amode,$password,
 1694:                              $fname,$mname,$lname,$gen,$sec,$enddate,
 1695:                              $startdate,$ENV{'form.forceid'},$desiredhost,
 1696:                              $email);
 1697:                         if ($reply ne 'ok') {
 1698:                             $reply =~ s/^error://;
 1699:                             $r->print('<br /><b>'.$username.'</b>:'.
 1700:                                       '  Unable to enroll: '.$reply);
 1701:          		} else {
 1702:                             $count++; $flushc++;
 1703:                             $student{$username}=1;
 1704:                             $r->print('. ');
 1705:                             if ($flushc>15) {
 1706: 				$r->rflush;
 1707:                                 $flushc=0;
 1708:                             }
 1709:                         }
 1710:                     } else {
 1711:                         $r->print('<br /><b>'.$username.'</b>:'.
 1712:                            '  Unable to enroll: No password specified.');
 1713:                     }
 1714:                 }
 1715:             }
 1716:         } # end of foreach (@studentdata)
 1717:         $r->print('</p><p>Processed Students: '.$count.'</p>');
 1718:         $r->print("<p>If active, the new role will be available when the ".
 1719:                   "students next log in to LON-CAPA.</p>");
 1720:         #####################################
 1721:         #           Drop students           #
 1722:         #####################################
 1723:         if ($ENV{'form.fullup'} eq 'yes') {
 1724:             $r->print('<h3>Dropping Students</h3>');
 1725:             #  Get current classlist
 1726:             my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
 1727:             if (! defined($classlist)) {
 1728:                 $r->print("There are no students currently enrolled.\n");
 1729:             } else {
 1730:                 # Remove the students we just added from the list of students.
 1731:                 foreach (@studentdata) {
 1732:                     my %entries=&Apache::loncommon::record_sep($_);
 1733:                     unless (($entries{$fields{'username'}} eq '') ||
 1734:                             (!defined($entries{$fields{'username'}}))) {
 1735:                         delete($classlist->{$entries{$fields{'username'}}.
 1736:                                                 ':'.$domain});
 1737:                     }
 1738:                 }
 1739:                 # Print out list of dropped students.
 1740:                 &show_drop_list($r,$classlist,$keylist,'nosort');
 1741:             }
 1742:         }
 1743:     } # end of unless
 1744: }
 1745: 
 1746: # ================================================================== Phase four
 1747: sub drop_student_list {
 1748:     my $r=shift;
 1749:     my $count=0;
 1750:     my @droplist;
 1751:     if (ref($ENV{'form.droplist'})) {
 1752:         @droplist = @{$ENV{'form.droplist'}};
 1753:     } else {
 1754:         @droplist = ($ENV{'form.droplist'});
 1755:     }
 1756:     foreach (@droplist) {
 1757:         my ($uname,$udom)=split(/\:/,$_);
 1758:         # drop student
 1759:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
 1760:         if ($result eq 'ok' || $result eq 'ok:') {
 1761:             $r->print('Dropped '.$uname.' @ '.$udom.'<br>');
 1762:             $count++;
 1763:         } else {
 1764:             $r->print('Error dropping '.$uname.' @ '.$udom.': '.$result.
 1765:                       '<br />');
 1766:         }
 1767:     }
 1768:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
 1769:     $r->print('<p>Re-enrollment will re-activate data.') if ($count);
 1770: }
 1771: 
 1772: ###################################################################
 1773: ###################################################################
 1774: 
 1775: =pod
 1776: 
 1777: =item &handler
 1778: 
 1779: The typical handler you see in all these modules.  Takes $r, the
 1780: http request, as an argument.  
 1781: 
 1782: The response to the request is governed by two form variables
 1783: 
 1784:  form.action      form.state     response
 1785:  ---------------------------------------------------
 1786:  undefined        undefined      print main menu
 1787:  upload           undefined      print courselist upload menu
 1788:  upload           got_file       deal with uploaded file,
 1789:                                  print the upload managing menu
 1790:  upload           enrolling      enroll students based on upload
 1791:  drop             undefined      print the classlist ready to drop
 1792:  drop             done           drop the selected students
 1793:  enrollstudent    undefined      print student username domain form
 1794:  enrollstudent    gotusername    print single student enroll menu
 1795:  enrollstudent    enrolling      enroll student
 1796:  classlist        undefined      print html classlist
 1797:  classlist        csv            print csv classlist
 1798:  modifystudent    undefined      print classlist to select student to modify
 1799:  modifystudent    selected       print modify student menu
 1800:  modifystudent    done           make modifications to student record
 1801: 
 1802: =cut
 1803: 
 1804: ###################################################################
 1805: ###################################################################
 1806: sub handler {
 1807:     my $r=shift;
 1808:     if ($r->header_only) {
 1809:         &Apache::loncommon::content_type($r,'text/html');
 1810:         $r->send_http_header;
 1811:         return OK;
 1812:     }
 1813:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1814:                                             ['action','state']);
 1815:     #  Needs to be in a course
 1816:     if (! (($ENV{'request.course.fn'}) &&
 1817:           (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'})))) {
 1818:         # Not in a course, or not allowed to modify parms
 1819:         $ENV{'user.error.msg'}=
 1820:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
 1821:         return HTTP_NOT_ACCEPTABLE; 
 1822:     }
 1823:     #
 1824:     # Only output the header information if they did not request csv format
 1825:     #
 1826:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
 1827:         $r->content_type('text/csv');
 1828:     } else {
 1829:         # Start page
 1830:         &Apache::loncommon::content_type($r,'text/html');
 1831:         $r->send_http_header;
 1832:         $r->print(&header());
 1833:     }
 1834:     #
 1835:     # Main switch on form.action and form.state, as appropriate
 1836:     if (! exists($ENV{'form.action'})) {
 1837:         &print_main_menu($r);
 1838:     } elsif ($ENV{'form.action'} eq 'upload') {
 1839:         if (! exists($ENV{'form.state'})) {
 1840:             &print_first_courselist_upload_form($r);            
 1841:         } elsif ($ENV{'form.state'} eq 'got_file') {
 1842:             &print_upload_manager_form($r);
 1843:         } elsif ($ENV{'form.state'} eq 'enrolling') {
 1844:             if ($ENV{'form.datatoken'}) {
 1845:                 &upfile_drop_add($r);
 1846:             } else {
 1847:                 # Hmmm, this is an error
 1848:             }
 1849:         } else {
 1850:             &print_first_courselist_upload_form($r);            
 1851:         }
 1852:     } elsif ($ENV{'form.action'} eq 'drop') {
 1853:         if (! exists($ENV{'form.state'})) {
 1854:             &print_drop_menu($r);
 1855:         } elsif ($ENV{'form.state'} eq 'done') {
 1856:             &drop_student_list($r);
 1857:         } else {
 1858:             &print_drop_menu($r);
 1859:         }
 1860:     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
 1861:         if (! exists($ENV{'form.state'})) {
 1862:             &get_student_username_domain_form($r);
 1863:         } elsif ($ENV{'form.state'} eq 'gotusername') {
 1864:             &print_enroll_single_student_form($r);
 1865:         } elsif ($ENV{'form.state'} eq 'enrolling') {
 1866:             &enroll_single_student($r);
 1867:         } else {
 1868:             &get_student_username_domain_form($r);
 1869:         }
 1870:     } elsif ($ENV{'form.action'} eq 'classlist') {
 1871:         if (! exists($ENV{'form.state'})) {
 1872:             &print_html_classlist($r);
 1873:         } elsif ($ENV{'form.state'} eq 'csv') {
 1874:             &print_formatted_classlist($r,'csv');
 1875:         } elsif ($ENV{'form.state'} eq 'excel') {
 1876:             &print_formatted_classlist($r,'excel');
 1877:         } else {
 1878:             &print_html_classlist($r);
 1879:         }
 1880:     } elsif ($ENV{'form.action'} eq 'modifystudent') {
 1881:         if (! exists($ENV{'form.state'})) {
 1882:             &print_html_classlist($r);
 1883:         } elsif ($ENV{'form.state'} eq 'selected') {
 1884:             &print_modify_student_form($r);
 1885:         } elsif ($ENV{'form.state'} eq 'done') {
 1886:             &modify_single_student($r);
 1887:         } else {
 1888:             &print_html_classlist($r);
 1889:         }        
 1890:     } else {
 1891:         # We should not end up here, but I guess it is possible
 1892:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
 1893:                                  "form.action = ".$ENV{'form.action'}.
 1894:                                  "Someone should fix this.");
 1895:         &print_main_menu($r);
 1896:     }
 1897:     #
 1898:     # Finish up
 1899:     if (exists($ENV{'form.state'}) && ($ENV{'form.state'} eq 'csv')) {
 1900:         $r->print("\n");
 1901:     } else {
 1902:         $r->print('</form></body></html>');
 1903:     }
 1904:     return OK;
 1905: }
 1906: 
 1907: ###################################################################
 1908: ###################################################################
 1909: 
 1910: 1;
 1911: __END__
 1912: 
 1913: 

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