File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.76: download - view: text, annotated - select for diffs
Mon Jul 21 17:54:43 2003 UTC (20 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- bug 1911, Classlist is really Class List

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

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