File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.88: download - view: text, annotated - select for diffs
Thu Nov 6 20:04:06 2003 UTC (20 years, 8 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
First stab a localization of londropadd.pm.
Note: Using parameters in localization.  Let me know if this is okay.
Also needed to add comments to newphrases.txt.  Again, let me know.

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

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