File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.138: download - view: text, annotated - select for diffs
Tue Mar 21 18:34:23 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start_page

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # $Id: londropadd.pm,v 1.138 2006/03/21 18:34:23 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: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: package Apache::londropadd;
   33: 
   34: use strict;
   35: use Apache::lonnet;
   36: use Apache::loncommon();
   37: use Apache::lonhtmlcommon();
   38: use Apache::Constants qw(:common :http REDIRECT);
   39: use Spreadsheet::WriteExcel;
   40: use Apache::lonstathelpers();
   41: use Apache::lonlocal;
   42: 
   43: ###############################################################
   44: ###############################################################
   45: sub header {
   46:     my $start_page=&Apache::loncommon::start_page('Enrollment Manager');
   47:     return(<<ENDHEAD);
   48: $start_page
   49: <form method="post" enctype="multipart/form-data"  
   50:       action="/adm/dropadd" name="studentform">
   51: ENDHEAD
   52: }
   53: 
   54: ###############################################################
   55: ###############################################################
   56: # Drop student from all sections of a course, except optional $csec
   57: sub modifystudent {
   58:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
   59:     # if $csec is undefined, drop the student from all the courses matching
   60:     # this one.  If $csec is defined, drop them from all other sections of 
   61:     # this course and add them to section $csec
   62:     $courseid=~s/\_/\//g;
   63:     $courseid=~s/^(\w)/\/$1/;
   64:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
   65:     my ($tmp) = keys(%roles);
   66:     # Bail out if we were unable to get the students roles
   67:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
   68:     # Go through the roles looking for enrollment in this course
   69:     my $result = '';
   70:     foreach my $course (keys(%roles)) {
   71:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
   72:             # We are in this course
   73:             my $section=$1;
   74:             $section='' if ($course eq $courseid.'_st');
   75:             if (defined($csec) && $section eq $csec) {
   76:                 $result .= 'ok:';
   77:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
   78:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
   79:                 my $now=time;
   80:                 # if this is an active role 
   81:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
   82:                     my $reply=&Apache::lonnet::modifystudent
   83:                         # dom  name  id mode pass     f     m     l     g
   84:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
   85:                          $section,time,undef,undef,$desiredhost);
   86:                     $result .= $reply.':';
   87:                 }
   88:             }
   89:         }
   90:     }
   91:     if ($result eq '') {
   92:         $result = 'Unable to find section for this student';
   93:     } else {
   94:         $result =~ s/(ok:)+/ok/g;
   95:     }
   96:     return $result;
   97: }
   98: 
   99: ###############################################################
  100: ###############################################################
  101: # build a domain and server selection form
  102: sub domain_form {
  103:     my ($defdom) = @_;
  104:     # Set up domain and server selection forms
  105:     #
  106:     # Get the domains
  107:     my @domains = &Apache::loncommon::get_domains();
  108:     # build up the menu information to be passed to 
  109:     # &Apache::loncommon::linked_select_forms
  110:     my %select_menus;
  111:     foreach my $dom (@domains) {
  112:         # set up the text for this domain
  113:         $select_menus{$dom}->{'text'}= $dom;
  114:         # we want a choice of 'default' as the default in the second menu
  115:         $select_menus{$dom}->{'default'}= 'default';
  116:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
  117:         # Now build up the other items in the second menu
  118:         my %servers = &Apache::loncommon::get_library_servers($dom);
  119:         foreach my $server (keys(%servers)) {
  120:             $select_menus{$dom}->{'select2'}->{$server} 
  121:                                             = "$server $servers{$server}";
  122:         }
  123:     }
  124:     my $result  = &Apache::loncommon::linked_select_forms
  125:         ('studentform',' with home server ',$defdom,
  126:          'lcdomain','lcserver',\%select_menus);
  127:     return $result;
  128: }
  129: 
  130: ###############################################################
  131: ###############################################################
  132: #  Menu Phase One
  133: sub print_main_menu {
  134:     my ($r,$enrl_permission,$view_permission,$grp_manage_permission,
  135:         $grp_view_permission)=@_;
  136:     #
  137:     my ($cdom,$cnum) = split/_/,$env{'request.course.id'};
  138:     my @menu = 
  139:         ( 
  140:           { text => 'Upload a class list', 
  141:             help => 'Course_Create_Class_List',
  142:             action => 'upload',
  143:             permission => $enrl_permission,
  144:             },
  145:           { text => 'Enroll a single student', 
  146:             help => 'Course_Add_Student',
  147:             action => 'enrollstudent',
  148:             permission => $enrl_permission,
  149:             },
  150:           { text => 'Modify student data', 
  151:             help => 'Course_Modify_Student_Data',
  152:             action => 'modifystudent',
  153:             permission => $enrl_permission,
  154:             },
  155:           { text => 'View Class List', 
  156:             help => 'Course_View_Class_List',
  157:             action => 'classlist',
  158:             permission => $view_permission,
  159:             },
  160:           { text => 'Drop Students', 
  161:             help => 'Course_Drop_Student',
  162:             action => 'drop',
  163:             permission => $enrl_permission,
  164:             },
  165:           { text => 'Automated Enrollment Manager', 
  166:             permission => &Apache::lonnet::auto_run($cnum,$cdom),
  167:             url  => '/adm/populate',
  168:             },
  169:           { text => 'Create a new group',
  170:             help => 'Course_Create_Group',
  171:             permission => $grp_manage_permission,
  172:             url => '/adm/coursegroups?refpage=enrl&action=create',
  173:             },
  174:           { text => 'Modify an existing group',
  175:             help => 'Course_Modify_Group',
  176:             permission => $grp_manage_permission,
  177:             url => '/adm/coursegroups?refpage=enrl&action=modify',
  178:             },
  179:           { text => 'Delete an existing group',
  180:             help => 'Course_Delete_Group',
  181:             permission => $grp_manage_permission,
  182:             url => '/adm/coursegroups?refpage=enrl&action=delete',
  183:             },
  184:           { text => 'Enter an existing group',
  185:             help => 'Course_Display_Group',
  186:             permission => $grp_view_permission,
  187:             url => '/adm/coursegroups?refpage=enrl&action=view',
  188:             },
  189:           );
  190:     my $menu_html = '';
  191:     foreach my $menu_item (@menu) {
  192:         next if (! $menu_item->{'permission'});
  193:         $menu_html.='<p>';
  194:         $menu_html.='<font size="+1">';
  195:         if (exists($menu_item->{'url'})) {
  196:             $menu_html.=qq{<a href="$menu_item->{'url'}">};
  197:         } else {
  198:             $menu_html.=
  199:                 qq{<a href="/adm/dropadd?action=$menu_item->{'action'}">};
  200:         }
  201:         $menu_html.= &mt($menu_item->{'text'}).'</a></font>';
  202:         if (exists($menu_item->{'help'})) {
  203:             $menu_html.=
  204:                 &Apache::loncommon::help_open_topic($menu_item->{'help'});
  205:         }
  206:         $menu_html.='</p>'.$/;
  207:     }
  208:     $r->print($menu_html);
  209:     return;
  210: }
  211: 
  212: ###############################################################
  213: ###############################################################
  214: sub hidden_input {
  215:     my ($name,$value) = @_;
  216:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
  217: }
  218: 
  219: sub print_upload_manager_header {
  220:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
  221:     my $javascript;
  222:     #
  223:     if (! exists($env{'form.upfile_associate'})) {
  224:         $env{'form.upfile_associate'} = 'forward';
  225:     }
  226:     if ($env{'form.associate'} eq 'Reverse Association') {
  227:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
  228:             $env{'form.upfile_associate'} = 'reverse';
  229:         } else {
  230:             $env{'form.upfile_associate'} = 'forward';
  231:         }
  232:     }
  233:     if ($env{'form.upfile_associate'} eq 'reverse') {
  234: 	$javascript=&upload_manager_javascript_reverse_associate();
  235:     } else {
  236: 	$javascript=&upload_manager_javascript_forward_associate();
  237:     }
  238:     #
  239:     # Deal with restored settings
  240:     my $password_choice = '';
  241:     if (exists($env{'form.ipwd_choice'}) &&
  242:         $env{'form.ipwd_choice'} ne '') {
  243:         # If a column was specified for password, assume it is for an
  244:         # internal password.  This is a bug waiting to be filed (could be
  245:         # local or krb auth instead of internal) but I do not have the 
  246:         # time to mess around with this now.
  247:         $password_choice = 'int';        
  248:     }
  249:     #
  250:     my $javascript_validations=&javascript_validations('auth',$krbdefdom,
  251:                                     $password_choice);
  252:     my $checked=(($env{'form.noFirstLine'})?' checked="1"':'');
  253:     $r->print('<h3>'.&mt('Uploading Class List')."</h3>\n".
  254:               "<hr>\n".
  255:               '<h3>'.&mt('Identify fields')."</h3>\n");
  256:     $r->print("<p>\n".
  257:               &mt('Total number of records found in file: [_1].',$distotal).
  258:               "\n".
  259:               "</p><hr>\n");
  260:     $r->print(&mt('Enter as many fields as you can. The system will inform you and bring you back to this page if the data selected is insufficient to enroll students in your class.')."<hr>\n");
  261:     $r->print(&hidden_input('action','upload').
  262:               &hidden_input('state','got_file').
  263:               &hidden_input('associate','').
  264:               &hidden_input('datatoken',$datatoken).
  265:               &hidden_input('fileupload',$env{'form.fileupload'}).
  266:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
  267:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
  268:     $r->print('<input type="button" value="Reverse Association" '.
  269:               'name="'.&mt('Reverse Association').'" '.
  270:               'onClick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
  271:     $r->print('<label><input type="checkbox" name="noFirstLine" $checked />'.
  272:               &mt('Ignore First Line').'</label>');
  273:     $r->print("<hr />\n".
  274:               '<script type="text/javascript" language="Javascript">'."\n".
  275:               $javascript."\n".$javascript_validations.'</script>');
  276: }
  277: 
  278: ###############################################################
  279: ###############################################################
  280: sub javascript_validations {
  281:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield)=@_;
  282:     my $authheader;
  283:     if ($mode eq 'auth') {
  284:         my %param = ( formname => 'studentform',
  285:                       kerb_def_dom => $krbdefdom,
  286:                       curr_authtype => $curr_authtype);
  287:         $authheader = &Apache::loncommon::authform_header(%param);
  288:     } elsif ($mode eq 'createcourse') {
  289:         my %param = ( formname => 'ccrs',
  290:                   kerb_def_dom => $krbdefdom,
  291:                       curr_authtype => $curr_authtype );
  292:         $authheader = &Apache::loncommon::authform_header(%param);
  293:     } elsif ($mode eq 'modifycourse') {
  294:         my %param = ( formname => 'cmod',
  295:                   kerb_def_dom => $krbdefdom,
  296:                   mode => 'modifycourse',
  297:                   curr_authtype => $curr_authtype,
  298:                   curr_autharg => $curr_authfield );
  299:         $authheader = &Apache::loncommon::authform_header(%param);
  300:     }
  301: 
  302:     
  303:     my %alert = &Apache::lonlocal::texthash
  304:         (username => 'You need to specify the username field.',
  305:          authen   => 'You must choose an authentication type.',
  306:          krb      => 'You need to specify the Kerberos domain.',
  307:          ipass    => 'You need to specify the initial password.',
  308:          name     => 'The optional name field was not specified.',
  309:          snum     => 'The optional student number field was not specified.',
  310:          section  => 'The optional section or group field was not specified.', 
  311:          email    => 'The optional email address field was not specified.',
  312:          continue => 'Continue enrollment?',
  313:          );
  314:     
  315: #    my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  316:     my $function_name =(<<END);
  317: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
  318: END
  319:     my $auth_checks;
  320:     if ($mode eq 'createcourse') {
  321:         $auth_checks .= (<<END);
  322:     if (vf.autoadds[0].checked == true) {
  323:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
  324:             alert('$alert{'authen'}');
  325:             return;
  326:         }
  327:     }
  328: END
  329:     } else {
  330:         $auth_checks .= (<<END);
  331:     var foundatype=0;
  332:     if (founduname==0) {
  333: 	alert('$alert{'username'}');
  334:         return;
  335:     }
  336:     // alert('current.radiovalue = '+current.radiovalue);
  337:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
  338:         // They did not check any of the login radiobuttons.
  339:         alert('$alert{'authen'}');
  340:         return;
  341:     }
  342: END
  343:     }
  344:     if ($mode eq 'createcourse') {
  345:         $auth_checks .= "
  346:     if ( (vf.autoadds[0].checked == true) &&
  347:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
  348: ";
  349:     } elsif ($mode eq 'modifycourse') {
  350:         $auth_checks .= " 
  351:     if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
  352: ";
  353:     }
  354:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
  355:         $auth_checks .= (<<END);
  356:         var alertmsg = '';
  357:         switch (current.radiovalue) {
  358:             case 'krb':
  359:                 alertmsg = '$alert{'krb'}';
  360:                 break;
  361:             default:
  362:                 alertmsg = '';
  363:         }
  364:         if (alertmsg != '') {
  365:             alert(alertmsg);
  366:             return;
  367:         }
  368:     }
  369: END
  370:     } else {
  371:         $auth_checks .= (<<END);
  372:     foundatype=1;
  373:     if (current.argfield == null || current.argfield == '') {
  374:         var alertmsg = '';
  375:         switch (current.value) {
  376:             case 'krb': 
  377:                 alertmsg = '$alert{'krb'}';
  378:                 break;
  379:             case 'loc':
  380:             case 'fsys':
  381:                 alertmsg = '$alert{'ipass'}';
  382:                 break;
  383:             case 'fsys':
  384:                 alertmsg = '';
  385:                 break;
  386:             default: 
  387:                 alertmsg = '';
  388:         }
  389:         if (alertmsg != '') {
  390:             alert(alertmsg);
  391:             return;
  392:         }
  393:     }
  394: END
  395:     }
  396:     my $optional_checks = '';
  397:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
  398:         $optional_checks = (<<END);
  399:     vf.submit();
  400: }
  401: END
  402:     } else {
  403:         $optional_checks = (<<END);
  404:     var message='';
  405:     if (foundname==0) { 
  406:         message='$alert{'name'}';
  407:     }
  408:     if (foundid==0) { 
  409:         if (message!='') { 
  410:             message+='\\n'; 
  411:         }
  412:         message+='$alert{'snum'}';
  413:     }
  414:     if (foundsec==0) {
  415:         if (message!='') {
  416:             message+='\\n';
  417:         } 
  418:         message+='$alert{'section'}';
  419:     }
  420:     if (foundemail==0) {
  421:         if (message!='') {
  422:             message+='\\n';
  423:         }
  424:         message+='$alert{'email'}';
  425:     }
  426:     if (message!='') {
  427:         message+= '\\n$alert{'continue'}';
  428:         if (confirm(message)) {
  429:             vf.state.value='enrolling';
  430:             vf.submit();
  431:         }
  432:     } else {
  433:         vf.state.value='enrolling';
  434:         vf.submit();
  435:     }
  436: }
  437: END
  438:     }
  439:     my $result = $function_name;
  440:     if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse')  ) {
  441:         $result .= $auth_checks;
  442:     }
  443:     $result .= $optional_checks;
  444:     if ( ($mode eq 'auth') || ($mode eq 'createcourse') || ($mode eq 'modifycourse')  ) {
  445:         $result .= $authheader;
  446:     }
  447:     return $result;
  448: }
  449: 
  450: ###############################################################
  451: ###############################################################
  452: sub upload_manager_javascript_forward_associate {
  453:     return(<<ENDPICK);
  454: function verify(vf) {
  455:     var founduname=0;
  456:     var foundpwd=0;
  457:     var foundname=0;
  458:     var foundid=0;
  459:     var foundsec=0;
  460:     var foundemail=0;
  461:     var tw;
  462:     for (i=0;i<=vf.nfields.value;i++) {
  463:         tw=eval('vf.f'+i+'.selectedIndex');
  464:         if (tw==1) { founduname=1; }
  465:         if ((tw>=2) && (tw<=6)) { foundname=1; }
  466:         if (tw==7) { foundid=1; }
  467:         if (tw==8) { foundsec=1; }
  468:         if (tw==9) { foundpwd=1; }
  469:         if (tw==10) { foundemail=1; }
  470:     }
  471:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail);
  472: }
  473: 
  474: //
  475: // vf = this.form
  476: // tf = column number
  477: //
  478: // values of nw
  479: //
  480: // 0 = none
  481: // 1 = username
  482: // 2 = names (lastname, firstnames)
  483: // 3 = fname (firstname)
  484: // 4 = mname (middlename)
  485: // 5 = lname (lastname)
  486: // 6 = gen   (generation)
  487: // 7 = id
  488: // 8 = section
  489: // 9 = ipwd  (password)
  490: // 10 = email address
  491: 
  492: function flip(vf,tf) {
  493:    var nw=eval('vf.f'+tf+'.selectedIndex');
  494:    var i;
  495:    // make sure no other columns are labeled the same as this one
  496:    for (i=0;i<=vf.nfields.value;i++) {
  497:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
  498:           eval('vf.f'+i+'.selectedIndex=0;')
  499:       }
  500:    }
  501:    // If we set this to 'lastname, firstnames', clear out all the ones
  502:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
  503:    if (nw==2) {
  504:       for (i=0;i<=vf.nfields.value;i++) {
  505:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
  506:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
  507:              eval('vf.f'+i+'.selectedIndex=0;')
  508:          }
  509:       }
  510:    }
  511:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
  512:    // clear out any that are set to 'lastname, firstnames' (2)
  513:    if ((nw>=3) && (nw<=6)) {
  514:       for (i=0;i<=vf.nfields.value;i++) {
  515:          if (eval('vf.f'+i+'.selectedIndex')==2) {
  516:              eval('vf.f'+i+'.selectedIndex=0;')
  517:          }
  518:       }
  519:    }
  520:    // If we set the password, make the password form below correspond to 
  521:    // the new value.
  522:    if (nw==9) {
  523:        changed_radio('int',document.studentform);
  524:        set_auth_radio_buttons('int',document.studentform);
  525:        vf.intarg.value='';
  526:        vf.krbarg.value='';
  527:        vf.locarg.value='';
  528:    }
  529: }
  530: 
  531: function clearpwd(vf) {
  532:     var i;
  533:     for (i=0;i<=vf.nfields.value;i++) {
  534:         if (eval('vf.f'+i+'.selectedIndex')==9) {
  535:             eval('vf.f'+i+'.selectedIndex=0;')
  536:         }
  537:     }
  538: }
  539: 
  540: ENDPICK
  541: }
  542: 
  543: ###############################################################
  544: ###############################################################
  545: sub upload_manager_javascript_reverse_associate {
  546:     return(<<ENDPICK);
  547: function verify(vf) {
  548:     var founduname=0;
  549:     var foundpwd=0;
  550:     var foundname=0;
  551:     var foundid=0;
  552:     var foundsec=0;
  553:     var tw;
  554:     for (i=0;i<=vf.nfields.value;i++) {
  555:         tw=eval('vf.f'+i+'.selectedIndex');
  556:         if (i==0 && tw!=0) { founduname=1; }
  557:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
  558:         if (i==6 && tw!=0) { foundid=1; }
  559:         if (i==7 && tw!=0) { foundsec=1; }
  560:         if (i==8 && tw!=0) { foundpwd=1; }
  561:     }
  562:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  563: }
  564: 
  565: function flip(vf,tf) {
  566:    var nw=eval('vf.f'+tf+'.selectedIndex');
  567:    var i;
  568:    // picked the all one one name field, reset the other name ones to blank
  569:    if (tf==1 && nw!=0) {
  570:       for (i=2;i<=5;i++) {
  571:          eval('vf.f'+i+'.selectedIndex=0;')
  572:       }
  573:    }
  574:    //picked one of the piecewise name fields, reset the all in
  575:    //one field to blank
  576:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
  577:       eval('vf.f1.selectedIndex=0;')
  578:    }
  579:    // intial password specified, pick internal authentication
  580:    if (tf==8 && nw!=0) {
  581:        changed_radio('int',document.studentform);
  582:        set_auth_radio_buttons('int',document.studentform);
  583:        vf.krbarg.value='';
  584:        vf.intarg.value='';
  585:        vf.locarg.value='';
  586:    }
  587: }
  588: 
  589: function clearpwd(vf) {
  590:     var i;
  591:     if (eval('vf.f8.selectedIndex')!=0) {
  592:         eval('vf.f8.selectedIndex=0;')
  593:     }
  594: }
  595: ENDPICK
  596: }
  597: 
  598: ###############################################################
  599: ###############################################################
  600: sub print_upload_manager_footer {
  601:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
  602: 
  603:     my ($krbdef,$krbdefdom) =
  604:         &Apache::loncommon::get_kerberos_defaults($defdom);
  605:     my %param = ( formname => 'document.studentform',
  606:                   kerb_def_dom => $krbdefdom,
  607:                   kerb_def_auth => $krbdef
  608:                   );
  609:     if (exists($env{'form.ipwd_choice'}) &&
  610:         defined($env{'form.ipwd_choice'}) &&
  611:         $env{'form.ipwd_choice'} ne '') {
  612:         $param{'curr_authtype'} = 'int';
  613:     }
  614:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  615:     my $intform = &Apache::loncommon::authform_internal(%param);
  616:     my $locform = &Apache::loncommon::authform_local(%param);
  617:     my $domform = &domain_form($defdom);
  618:     my $date_table = &date_setting_table();
  619:     my $Str = "</table>\n";
  620:     $Str .= &hidden_input('nfields',$i);
  621:     $Str .= &hidden_input('keyfields',$keyfields);
  622:     $Str .= '<h3>'.&mt('Login Type')."</h3>\n";
  623:     $Str .= "<p>\n".
  624:         &mt('Note: this will not take effect if the user already exists').
  625: 	&Apache::loncommon::help_open_topic('Auth_Options').
  626:         "</p><p>\n";
  627:     $Str .= $krbform."\n</p><p>\n".
  628:         $intform."\n</p><p>\n".
  629:         $locform."\n</p>\n";
  630:     $Str .= '<h3>'.&mt('LON-CAPA Domain for Students')."</h3>\n";
  631:     $Str .= "<p>\n".&mt('LON-CAPA domain: [_1]',$domform)."\n</p>\n";
  632:     $Str .= "<h3>".&mt('Starting and Ending Dates')."</h3>\n";
  633:     $Str .= "<p>\n".$date_table."</p>\n";
  634:     $Str .= "<h3>".&mt('Full Update')."</h3>\n";
  635:     $Str .= '<label><input type="checkbox" name="fullup" value="yes">'.
  636:         ' '.&mt('Full update (also print list of users not enrolled anymore)').
  637:         "</label></p>\n";
  638:     $Str .= "<h3>".&mt('Student Number')."</h3>\n";
  639:     $Str .= "<p>\n".'<label><input type="checkbox" name="forceid" value="yes">';
  640:     $Str .= &mt('Disable ID/Student Number Safeguard and Force Change '.
  641:                 'of Conflicting IDs (only do if you know what you are doing)').
  642:                 "</label>\n</p><p>\n";
  643:     $Str .= '<input type="button" onClick="javascript:verify(this.form)" '.
  644:         'value="Update Class List" />'."<br />\n";
  645:     $Str .= &mt('Note: for large courses, this operation may be time '.
  646:                 'consuming');
  647:     $r->print($Str);
  648:     return;
  649: }
  650: 
  651: ###############################################################
  652: ###############################################################
  653: sub print_upload_manager_form {
  654:     my $r=shift;
  655: 
  656:     my $firstLine;
  657:     my $datatoken;
  658:     if (!$env{'form.datatoken'}) {
  659:         $datatoken=&Apache::loncommon::upfile_store($r);
  660:     } else {
  661:         $datatoken=$env{'form.datatoken'};
  662:         &Apache::loncommon::load_tmp_file($r);
  663:     }
  664:     my @records=&Apache::loncommon::upfile_record_sep();
  665:     if($env{'form.noFirstLine'}){
  666:         $firstLine=shift(@records);
  667:     }
  668:     my $total=$#records;
  669:     my $distotal=$total+1;
  670:     my $today=time;
  671:     my $halfyear=$today+15552000;
  672:     #
  673:     # Restore memorized settings
  674:     &Apache::loncommon::restore_course_settings
  675:         ('enrollment_upload',{ 'username_choice' => 'scalar', # column settings
  676:                                'names_choice' => 'scalar',
  677:                                'fname_choice' => 'scalar',
  678:                                'mname_choice' => 'scalar',
  679:                                'lname_choice' => 'scalar',
  680:                                'gen_choice' => 'scalar',
  681:                                'id_choice' => 'scalar',
  682:                                'sec_choice' => 'scalar',
  683:                                'ipwd_choice' => 'scalar',
  684:                                'email_choice' => 'scalar',
  685:                            });
  686:     #
  687:     # Determine kerberos parameters as appropriate
  688:     my $defdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  689:     my ($krbdef,$krbdefdom) =
  690:         &Apache::loncommon::get_kerberos_defaults($defdom);
  691:     #
  692:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
  693:     my $i;
  694:     my $keyfields;
  695:     if ($total>=0) {
  696:         my @field=
  697:             (['username',&mt('Username'),     $env{'form.username_choice'}],
  698:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
  699:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
  700:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
  701:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
  702:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
  703:              ['id',   &mt('ID/Student Number'),$env{'form.id_choice'}],
  704:              ['sec',  &mt('Group/Section'),   $env{'form.sec_choice'}],
  705:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
  706:              ['email',&mt('EMail Address'),   $env{'form.email_choice'}]);
  707: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
  708: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  709: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
  710:                                                           \@field);
  711: 	    foreach (@field) { 
  712:                 $keyfields.=$_->[0].','; 
  713:             }
  714: 	    chop($keyfields);
  715: 	} else {
  716: 	    unshift(@field,['none','']);
  717: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
  718:                                                             \@field);
  719: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
  720: 	    $keyfields=join(',',sort(keys(%sone)));
  721: 	}
  722:     }
  723:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear);
  724: }
  725: 
  726: ###############################################################
  727: ###############################################################
  728: sub enroll_single_student {
  729:     my $r=shift;
  730:     # Remove non alphanumeric values from section
  731:     $env{'form.csec'}=~s/\W//g;
  732:     #
  733:     # We do the dates first because the action of making them the defaul
  734:     # in the course is entirely separate from the action of enrolling the
  735:     # student.  Also, a failure in setting the dates as default is not fatal
  736:     # to the process of enrolling / modifying a student.
  737:     my ($startdate,$enddate) = &get_dates_from_form();
  738:     if ($env{'form.makedatesdefault'}) {
  739:         $r->print(&make_dates_default($startdate,$enddate));
  740:     }
  741: 
  742:     $r->print('<h3>'.&mt('Enrolling Student').'</h3>');
  743:     $r->print('<p>'.&mt('Enrolling').' '.$env{'form.cuname'}." \@ ".
  744:               $env{'form.lcdomain'}.'</p>');
  745:     if (($env{'form.cuname'})&&($env{'form.cuname'}!~/\W/)&&
  746:         ($env{'form.lcdomain'})&&($env{'form.lcdomain'}!~/\W/)) {
  747:         # Deal with home server selection
  748:         my $domain=$env{'form.lcdomain'};
  749:         my $desiredhost = $env{'form.lcserver'};
  750:         if (lc($desiredhost) eq 'default') {
  751:             $desiredhost = undef;
  752:         } else {
  753:             my %home_servers =&Apache::loncommon::get_library_servers($domain);
  754:             if (! exists($home_servers{$desiredhost})) {
  755:                 $r->print('<font color="#ff0000">'.&mt('Error').':</font>'.
  756:                           &mt('Invalid home server specified'));
  757:                 return;
  758:             }
  759:         }
  760:         $r->print(" ".&mt('with server')." $desiredhost :") if (defined($desiredhost));
  761:         # End of home server selection logic
  762: 	my $amode='';
  763:         my $genpwd='';
  764:         if ($env{'form.login'} eq 'krb') {
  765:            $amode='krb';
  766: 	   $amode.=$env{'form.krbver'};
  767:            $genpwd=$env{'form.krbarg'};
  768:         } elsif ($env{'form.login'} eq 'int') {
  769:            $amode='internal';
  770:            $genpwd=$env{'form.intarg'};
  771:         }  elsif ($env{'form.login'} eq 'loc') {
  772: 	    $amode='localauth';
  773: 	    $genpwd=$env{'form.locarg'};
  774: 	    if (!$genpwd) { $genpwd=" "; }
  775: 	}
  776:         my $home = &Apache::lonnet::homeserver($env{'form.cuname'},
  777:                                                    $env{'form.lcdomain'});
  778:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
  779:             # Clean out any old roles the student has in this class.
  780:             &modifystudent($env{'form.lcdomain'},$env{'form.cuname'},
  781:                            $env{'request.course.id'},$env{'form.csec'},
  782:                             $desiredhost);
  783:             my $login_result = &Apache::lonnet::modifystudent
  784:                 ($env{'form.lcdomain'},$env{'form.cuname'},
  785:                  $env{'form.cstid'},$amode,$genpwd,
  786:                  $env{'form.cfirst'},$env{'form.cmiddle'},
  787:                  $env{'form.clast'},$env{'form.cgen'},
  788:                  $env{'form.csec'},$enddate,
  789:                  $startdate,$env{'form.forceid'},
  790:                  $desiredhost,$env{'form.emailaddress'});
  791:             if ($login_result =~ /^ok/) {
  792:                 $r->print($login_result);
  793:                 $r->print("<p> ".&mt('If active, the new role will be available when the student next logs in to LON-CAPA.')."</p>");
  794:             } else {
  795:                 $r->print(&mt('unable to enroll').": ".$login_result);
  796:             }
  797: 	} else {
  798:             $r->print('<p><font color="#ff0000">'.&mt('ERROR').'</font>&nbsp;');
  799:             if ($amode =~ /^krb/) {
  800:                 $r->print(&mt('Missing Kerberos domain information.').'  ');
  801:             } else {
  802:                 $r->print(&mt('Invalid login mode or password.').'  ');
  803:             }
  804:             $r->print('<b>'.&mt('Unable to enroll').' '.$env{'form.cuname'}.'.</b></p>');
  805:         }
  806:     } else {
  807:         $r->print(&mt('Invalid username or domain'));
  808:     }    
  809: }
  810: 
  811: sub setup_date_selectors {
  812:     my ($starttime,$endtime,$mode) = @_;
  813:     if (! defined($starttime)) {
  814:         $starttime = time;
  815:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
  816:             if (exists($env{'course.'.$env{'request.course.id'}.
  817:                             '.default_enrollment_start_date'})) {
  818:                 $starttime = $env{'course.'.$env{'request.course.id'}.
  819:                                   '.default_enrollment_start_date'};
  820:             }
  821:         }
  822:     }
  823:     if (! defined($endtime)) {
  824:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
  825:         unless ($mode eq 'createcourse') {
  826:             if (exists($env{'course.'.$env{'request.course.id'}.
  827:                             '.default_enrollment_end_date'})) {
  828:                 $endtime = $env{'course.'.$env{'request.course.id'}.
  829:                                 '.default_enrollment_end_date'};
  830:             }
  831:         }
  832:     }
  833:     my $startdateform = &Apache::lonhtmlcommon::date_setter('studentform',
  834:                                                             'startdate',
  835:                                                             $starttime);
  836:     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
  837:                                                           'enddate',
  838:                                                           $endtime);
  839:     if ($mode eq 'create_enrolldates') {
  840:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
  841:                                                             'startenroll',
  842:                                                             $starttime);
  843:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
  844:                                                           'endenroll',
  845:                                                           $endtime);
  846:     }
  847:     if ($mode eq 'create_defaultdates') {
  848:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
  849:                                                             'startaccess',
  850:                                                             $starttime);
  851:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
  852:                                                           'endaccess',
  853:                                                           $endtime);
  854:     }
  855:     return ($startdateform,$enddateform);
  856: }
  857: 
  858: sub get_dates_from_form {
  859:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
  860:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
  861:     if ($env{'form.no_end_date'}) {
  862:         $enddate = 0;
  863:     }
  864:     return ($startdate,$enddate);
  865: }
  866: 
  867: sub date_setting_table {
  868:     my ($starttime,$endtime,$mode) = @_;
  869:     my ($startform,$endform)=&setup_date_selectors($starttime,$endtime,$mode);
  870:     my $dateDefault = '<nobr>'.
  871:         '<label><input type="checkbox" name="makedatesdefault" /> '.
  872:         &mt('make these dates the default for future enrollment').'</label>';
  873:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
  874:         $dateDefault = '&nbsp;';
  875:     }
  876:     my $perpetual = '<nobr><label><input type="checkbox" name="no_end_date"';
  877:     if (defined($endtime) && $endtime == 0) {
  878:         $perpetual .= ' checked';
  879:     }
  880:     $perpetual.= ' /> '.&mt('no ending date').'</label></nobr>';
  881:     if ($mode eq 'create_enrolldates') {
  882:         $perpetual = '&nbsp;';
  883:     }
  884:     my $result = '';
  885:     $result .= "<table>\n";
  886:     $result .= '<tr><td align="right">'.&mt('Starting Date').'</td>'.
  887:         '<td>'.$startform.'</td>'.
  888:         '<td>'.$dateDefault.'</td>'."</tr>\n";
  889:     $result .= '<tr><td align="right">'.&mt('Ending Date').'</td>'.
  890:         '<td>'.$endform.'</td>'.
  891:         '<td>'.$perpetual.'</td>'."</tr>\n";
  892:     $result .= "</table>\n";
  893:     return $result;
  894: }
  895: 
  896: sub make_dates_default {
  897:     my ($startdate,$enddate) = @_;
  898:     my $result = '';
  899:     my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  900:     my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
  901:     my $put_result = &Apache::lonnet::put('environment',
  902:             {'default_enrollment_start_date'=>$startdate,
  903:              'default_enrollment_end_date'  =>$enddate},$dom,$crs);
  904:     if ($put_result eq 'ok') {
  905:         $result .= "Set default start and end dates for course<br />";
  906:         #
  907:         # Refresh the course environment
  908:         &Apache::lonnet::coursedescription($env{'request.course.id'});
  909:     } else {
  910:         $result .= &mt('Unable to set default dates for course').":".$put_result.
  911:             '<br />';
  912:     }
  913:     return $result;
  914: }
  915: 
  916: ##
  917: ## Single student enrollment routines (some of them)
  918: ##
  919: sub get_student_username_domain_form {
  920:     my $r = shift;
  921:     my $domform = &Apache::loncommon::select_dom_form
  922:         ($env{'course.'.$env{'request.course.id'}.'.domain'},'cudomain',0);
  923:     my %lt=&Apache::lonlocal::texthash(
  924: 		    'eos'  => "Enroll One Student",
  925: 		    'usr'  => "Username",
  926:                     'dom'  => "Domain",
  927:                     'been' => "Begin Enrollment",
  928: 				       );
  929:     $r->print(<<END);
  930: <input type="hidden" name="action" value="enrollstudent" />
  931: <input type="hidden" name="state"  value="gotusername" />
  932: <h3>$lt{'eos'}</h3>
  933: <table>
  934: <tr><th>$lt{'usr'}:</th>
  935:     <td><input type="text" name="cuname"  size="15" /></td></tr>
  936: <tr><th>$lt{'dom'}:</th>
  937:     <td>$domform</td></tr>
  938: <tr><th>&nbsp;</th>
  939:     <td>
  940:     <input type="submit" name="Begin Enrollment" value="$lt{'been'}" />
  941:     </td></tr>
  942: </table>
  943: <script type="text/javascript">
  944: // the if prevents the script error if the browser can not handle this
  945: if ( document.studentform.cuname ) { document.studentform.cuname.focus(); }
  946: </script>
  947: END
  948:     return;
  949: }
  950: 
  951: sub print_enroll_single_student_form {
  952:     my $r=shift;
  953:     $r->print("<h3>".&mt('Enroll One Student')."</h3>");
  954:     #
  955:     my $username = $env{'form.cuname'};
  956:     my $domain   = $env{'form.cudomain'};
  957:     $username=~s/\W//gs;
  958:     $domain=~s/\W//gs;
  959:     my $home = &Apache::lonnet::homeserver($username,$domain);
  960:     # $new_user flags whether we are creating a new user or using an old one
  961:     my $new_user = 1;
  962:     if ($home ne 'no_host') {
  963:         $new_user = 0;
  964:     }
  965:     #
  966:     my $user_data_html = '';
  967:     my $javascript_validations = '';
  968:     if ($new_user) {
  969:         my $defdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  970:         # Set up authentication forms
  971:         my ($krbdef,$krbdefdom) =
  972:             &Apache::loncommon::get_kerberos_defaults($domain);
  973:         $javascript_validations=&javascript_validations('auth',$krbdefdom);
  974:         my %param = ( formname => 'document.studentform',
  975:                       kerb_def_dom => $krbdefdom,
  976:                       kerb_def_auth => $krbdef
  977:                       );
  978:         my $krbform = &Apache::loncommon::authform_kerberos(%param);
  979:         my $intform = &Apache::loncommon::authform_internal(%param);
  980:         my $locform = &Apache::loncommon::authform_local(%param);
  981:         #
  982:         # Set up domain selection form
  983:         my $homeserver_form = '';
  984:         my %servers = &Apache::loncommon::get_library_servers($domain);
  985:         $homeserver_form = '<select name="lcserver" size="1">'."\n".
  986:             '<option value="default" selected>default</option>'."\n";
  987:         while (my ($servername,$serverdescription) = each (%servers)) {
  988:             $homeserver_form .= '<option value="'.$servername.'">'.
  989:                 $serverdescription."</option>\n";
  990:         }
  991:         $homeserver_form .= "</select>\n";
  992:         #
  993:         #
  994: 	my %lt=&Apache::lonlocal::texthash(
  995: 		       'udf'  => "User Data for",
  996:                        'fn'   => "First Name",
  997:                        'mn'   => "Middle Name",
  998:                        'ln'   => "Last Name",
  999:                        'gen'  => "Generation",
 1000:                        'hs'   => "Home Server",
 1001:                        'pswd' => "Password",
 1002: 		       'psam' => "Please select an authentication mechanism",
 1003:                        'mail' => "Email Address"
 1004: 					   );
 1005: 	my $authhelp=&Apache::loncommon::help_open_topic('Auth_Options');
 1006:         $user_data_html = <<END;
 1007: <h3>$lt{'udf'} $username\@$domain</h3>
 1008: <table>
 1009: <tr><th>$lt{'fn'}:</th>
 1010:     <td><input type="text" name="cfirst"  size="15"></td></tr>
 1011: <tr><th>$lt{'mn'}:</th>
 1012:     <td><input type="text" name="cmiddle" size="15"></td></tr>
 1013: <tr><th>$lt{'ln'}:</th>
 1014:     <td><input type="text" name="clast"   size="15"></td></tr>
 1015: <tr><th>$lt{'gen'}:</th>
 1016:     <td><input type="text" name="cgen"    size="5"> </td></tr>
 1017: <tr><th>$lt{'hs'}:</th>
 1018:     <td>$homeserver_form</td></tr>
 1019: <tr><th>$lt{'mail'}:</th>
 1020:     <td><input type="text" name="emailaddress" size="20" /></td></tr>
 1021: </table>
 1022: <h3>$lt{'pswd'}</h3>
 1023: $lt{'psam'}$authhelp
 1024: <table>
 1025: <p>
 1026: $krbform
 1027: <br />
 1028: $intform
 1029: <br />
 1030: $locform
 1031: </p>
 1032: END
 1033:     } else {
 1034:         # User already exists.  Do not worry about authentication
 1035:         my %uenv = &Apache::lonnet::dump('environment',$domain,$username);
 1036:         $javascript_validations = &javascript_validations('noauth');
 1037: 	my %lt=&Apache::lonlocal::texthash(
 1038: 		       'udf'  => "User Data for",
 1039:                        'fn'   => "First Name",
 1040:                        'mn'   => "Middle Name",
 1041:                        'ln'   => "Last Name",
 1042:                        'gen'  => "Generation",
 1043:                        'mail' => "Email Address",
 1044: 					   );
 1045:         $user_data_html = <<END;
 1046: <h3>$lt{'udf'} $username\@$domain</h3>
 1047: <input type="hidden" name="lcserver" value="default" />
 1048: <table>
 1049: <tr><th>$lt{'fn'}:</th>
 1050:     <td>
 1051:     <input type="text" name="cfirst" value="$uenv{'firstname'}" size="15" />
 1052:     </td></tr>
 1053: <tr><th>$lt{'mn'}:</th>
 1054:     <td>
 1055:     <input type="text" name="cmiddle" value="$uenv{'middlename'}" size="15" />
 1056:     </td></tr>
 1057: <tr><th>$lt{'ln'}:</th>
 1058:     <td>
 1059:     <input type="text" name="clast"value="$uenv{'lastname'}" size="15" />
 1060:     </td></tr>
 1061: <tr><th>$lt{'gen'}:</th>
 1062:     <td>
 1063:     <input type="text" name="cgen" value="$uenv{'generation'}" size="5" />
 1064:     </td></tr>
 1065: <tr><th>$lt{'mail'}:</th>
 1066:     <td>
 1067:     <input type="text" name="emailaddress" value="$uenv{'permanentemail'}" size="20" />
 1068:     </td></tr>
 1069: </table>
 1070: END
 1071:     }
 1072:     my $date_table = &date_setting_table();
 1073:         # Print it all out
 1074:     my %lt=&Apache::lonlocal::texthash(
 1075: 		   'cd'   => "Course Data",
 1076:                    'gs'   => "Group/Section",
 1077:                    'idsn' => "ID/Student Number",
 1078:                    'disn' => "Disable ID/Student Number Safeguard and Force Change of Conflicting IDs (only do if you know what you are doing)",
 1079:                    'eas'  => "Enroll as student",
 1080: 				       );
 1081:     $r->print(<<END);
 1082: <input type="hidden" name="action" value="enrollstudent" />
 1083: <input type="hidden" name="state"  value="done" />
 1084: <input type="hidden" name="cuname" value="$username" />
 1085: <input type="hidden" name="lcdomain" value="$domain" />
 1086: <script type="text/javascript" language="Javascript">
 1087: function verify(vf) {
 1088:     var founduname=0;
 1089:     var foundpwd=0;
 1090:     var foundname=0;
 1091:     var foundid=0;
 1092:     var foundsec=0;
 1093:     var tw;
 1094:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
 1095: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
 1096:         founduname=1;
 1097:     }
 1098:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
 1099: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
 1100:         foundname=1;
 1101:     }
 1102:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
 1103:         foundsec=1;
 1104:     }
 1105:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
 1106: 	foundid=1;
 1107:     }
 1108:     if (founduname==0) {
 1109: 	alert('You need to specify at least the username and domain fields');
 1110:         return;
 1111:     }
 1112:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
 1113: }
 1114: 
 1115: $javascript_validations
 1116: 
 1117: function clearpwd(vf) {
 1118:     //nothing else needs clearing
 1119: }
 1120: 
 1121: </script>
 1122: 
 1123: $user_data_html
 1124: 
 1125: <h3>$lt{'cd'}</h3>
 1126: 
 1127: <p>$lt{'gs'}: <input type="text" name="csec" size="5" />
 1128: <p>
 1129: $date_table
 1130: </p>
 1131: <h3>$lt{'idsn'}</h3>
 1132: <p>
 1133: $lt{'idsn'}: <input type="text" name="cstid" size="10">
 1134: </p><p>
 1135: <label>
 1136: <input type="checkbox" name="forceid" value="yes"> 
 1137: $lt{'disn'}
 1138: </label>
 1139: </p><p>
 1140: <input type="button" onClick="verify(this.form)" value="$lt{'eas'}">
 1141: </p>
 1142: END
 1143:     return;
 1144: }
 1145: 
 1146: # ========================================================= Menu Phase Two Drop
 1147: sub print_drop_menu {
 1148:     my $r=shift;
 1149:     $r->print("<h3>".&mt('Drop Students')."</h3>");
 1150:     my $cid=$env{'request.course.id'};
 1151:     my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
 1152:     if (! defined($classlist)) {
 1153:         $r->print(&mt('There are no students currently enrolled.')."\n");
 1154:         return;
 1155:     }
 1156:     # Print out the available choices
 1157:     &show_drop_list($r,$classlist,$keylist);
 1158:     return;
 1159: }
 1160: 
 1161: # ============================================== view classlist
 1162: sub print_html_classlist {
 1163:     my ($r,$mode) = @_;
 1164:     if (! exists($env{'form.sortby'})) {
 1165:         $env{'form.sortby'} = 'username';
 1166:     }
 1167:     if ($env{'form.Status'} !~ /^(Any|Expired|Active)$/) {
 1168:         $env{'form.Status'} = 'Active';
 1169:     }
 1170:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
 1171:         ($env{'form.Status'});
 1172:     my $cid=$env{'request.course.id'};
 1173:     my $cdom=$env{'course.'.$cid.'.domain'};
 1174:     my $cnum=$env{'course.'.$cid.'.num'};
 1175:     #
 1176:     # List course personnel
 1177:     my %coursepersonnel=&Apache::lonnet::get_course_adv_roles($cdom.'/'.$cnum);
 1178:     #
 1179:     if (! defined($env{'form.output'}) ||
 1180:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
 1181:         $env{'form.output'} = 'html';
 1182:     }
 1183:     #
 1184:     $r->print('<br /><table border="2">');
 1185:     foreach my $role (sort keys %coursepersonnel) {
 1186:         next if ($role =~ /^\s*$/);
 1187: 	$r->print('<tr><td>'.$role.'</td><td>');
 1188:         foreach my $user (split(',',$coursepersonnel{$role})) {
 1189: 	    my ($puname,$pudom)=split(':',$user);
 1190: 	    $r->print(' '.&Apache::loncommon::aboutmewrapper(
 1191:                                     &Apache::loncommon::plainname($puname,
 1192:                                                                   $pudom),
 1193:                                                              $puname,$pudom));
 1194: 	}
 1195:         $r->print('</td></tr>');
 1196:     }
 1197:     $r->print('</table>');
 1198:     #
 1199:     # Interface output
 1200:     $r->print('<input type="hidden" name="action" value="'.
 1201:               $env{'form.action'}.'" />');
 1202:     $r->print("<p>\n");
 1203:     if ($env{'form.action'} ne 'modifystudent') {
 1204: 	my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
 1205:                                            'excel' => "Excel",
 1206:                                            'html'  => 'HTML');
 1207:         my $output_selector = '<select size="1" name="output" >';
 1208:         foreach my $outputformat ('html','csv','excel') {
 1209:             my $option = '<option value="'.$outputformat.'" ';
 1210:             if ($outputformat eq $env{'form.output'}) {
 1211:                 $option .= 'selected ';
 1212:             }
 1213:             $option .='>'.$lt{$outputformat}.'</option>';
 1214:             $output_selector .= "\n".$option;
 1215:         }
 1216:         $output_selector .= '</select>';
 1217:         $r->print(&mt('Output Format: [_1]',$output_selector).('&nbsp;'x3));
 1218:     }
 1219:     $r->print(&mt('Student Status: [_1]',$status_select)."\n");
 1220:     $r->print('<input type="submit" value="'.&mt('Update Display').'" />'.
 1221:               "\n</p>\n");
 1222:     #
 1223:     # Print the classlist
 1224:     $r->print('<h2>'.&mt('Current Class List').'</h2>');
 1225:     my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
 1226:     if (! defined($classlist)) {
 1227:         $r->print(&mt('There are no students currently enrolled.')."\n");
 1228:     } else {
 1229:         # Print out the available choices
 1230:         if ($env{'form.action'} eq 'modifystudent') {
 1231:             &show_class_list($r,'view','modify',
 1232:                              $env{'form.Status'},$classlist,$keylist);
 1233:         } else {
 1234:             &show_class_list($r,$env{'form.output'},'aboutme',
 1235:                              $env{'form.Status'},$classlist,$keylist);
 1236:         }
 1237:     }
 1238: }
 1239: 
 1240: # =================================================== Show student list to drop
 1241: sub show_class_list {
 1242:     my ($r,$mode,$linkto,$statusmode,$classlist,$keylist)=@_;
 1243:     my $cid=$env{'request.course.id'};
 1244:     #
 1245:     # Variables for excel output
 1246:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
 1247:     #
 1248:     # Variables for csv output
 1249:     my ($CSVfile,$CSVfilename);
 1250:     #
 1251:     my $sortby = $env{'form.sortby'};
 1252:     if ($sortby !~ /^(username|domain|section|fullname|id|start|end|type)$/) {
 1253:         $sortby = 'username';
 1254:     }
 1255:     if (! exists($env{'form.displayphotos'})) {
 1256:         $env{'form.displayphotos'} = 'off';
 1257:     }
 1258:     my $displayphotos = $env{'form.displayphotos'};
 1259: 
 1260:     # Print out header 
 1261:     unless ($mode eq 'autoenroll') {
 1262:         $r->print(<<END);
 1263: <input type="hidden" name="state" value="$env{'form.state'}" />
 1264: END
 1265:     }
 1266:     $r->print(<<END);
 1267: <input type="hidden" name="sortby" value="$sortby" />
 1268: <input type="hidden" name="displayphotos" value="$displayphotos" />
 1269: END
 1270:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll') {
 1271:         if ($linkto eq 'aboutme') {
 1272:             $r->print(&mt('Select a user name to view the users personal page.'));
 1273:         } elsif ($linkto eq 'modify') {
 1274:             $r->print(&mt('Select a user name to modify the students information'));
 1275:         }
 1276: 	my %lt=&Apache::lonlocal::texthash(
 1277:                                            'usrn'   => "username",
 1278:                                            'dom'    => "domain",
 1279:                                            'sn'     => "student name",
 1280:                                            'sec'    => "section",
 1281:                                            'start'  => "start date",
 1282:                                            'end'    => "end date",
 1283:                                            'type'   => "enroll type/action",
 1284:                                            'photo'  => "photo",
 1285: 					   );
 1286:         unless ($mode eq 'autoenroll') {
 1287:             $r->print(<<END);
 1288: <input type="hidden" name="sname"  value="" />
 1289: <input type="hidden" name="sdom"   value="" />
 1290: END
 1291:         }
 1292:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 1293:             $r->print('
 1294: <script type="text/javascript">
 1295: function photowindow(photolink) {
 1296:     var title = "Photo_Viewer";
 1297:     var options = "scrollbars=1,resizable=1,menubar=0";
 1298:     options += ",width=240,height=240";
 1299:     stdeditbrowser = open(photolink,title,options,"1");
 1300:     stdeditbrowser.focus();
 1301: }
 1302: </script>
 1303:            ');
 1304:         }
 1305:         $r->print("
 1306: <p>
 1307: <table border=2>
 1308: <tr>
 1309:         ");
 1310:         if ($mode eq 'autoenroll') {
 1311:             $r->print("
 1312:  <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
 1313:             ");
 1314:         } else {
 1315:             $r->print("
 1316: <th>Count</th>
 1317:             ");
 1318:         }
 1319:         $r->print(<<END);
 1320:     <th>
 1321:        <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">$lt{'usrn'}</a>
 1322:     </th><th>
 1323:        <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">$lt{'dom'}</a>
 1324:     </th><th>
 1325:        <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
 1326:     </th><th>
 1327:        <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">$lt{'sn'}</a>
 1328:     </th><th>
 1329:        <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">$lt{'sec'}</a>
 1330:     </th><th>
 1331:        <a href="javascript:document.studentform.sortby.value='start';document.studentform.submit();">$lt{'start'}</a>
 1332:     </th><th>
 1333:        <a href="javascript:document.studentform.sortby.value='end';document.studentform.submit();">$lt{'end'}</a>
 1334:     </th>
 1335: END
 1336:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 1337:             my %photo_options = &Apache::lonlocal::texthash(
 1338:                                                             'on' => 'Show',
 1339:                                                             'off' => 'Hide',
 1340:                                                             );
 1341:             my $photochg = 'on';
 1342:             if ($displayphotos eq 'on') {
 1343:                 $photochg = 'off';
 1344:             }
 1345:             $r->print('    <th>'."\n".'     '. 
 1346:             '<a href="javascript:document.studentform.displayphotos.value='.
 1347:                       "'".$photochg."'".';document.studentform.submit();">'.
 1348:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
 1349:                       '    </th>'."\n");
 1350:         }
 1351:         $r->print("  </tr>\n");
 1352:     } elsif ($mode eq 'csv') {
 1353: 	#
 1354: 	# Open a file
 1355: 	$CSVfilename = '/prtspool/'.
 1356: 	    $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
 1357:             time.'_'.rand(1000000000).'.csv';
 1358: 	unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
 1359: 	    $r->log_error("Couldn't open $CSVfilename for output $!");
 1360: 	    $r->print("Problems occured in writing the csv file.  ".
 1361: 		      "This error has been logged.  ".
 1362: 		      "Please alert your LON-CAPA administrator.");
 1363: 	    $CSVfile = undef;
 1364: 	}
 1365: 	#
 1366: 	# Write headers and data to file
 1367:         if($statusmode eq 'Expired') {
 1368:             print $CSVfile '"'.&mt('Students with expired roles').'"'."\n";
 1369:         }
 1370:         if ($statusmode eq 'Any') {
 1371:             print $CSVfile '"'.join('","',map {
 1372: 		&Apache::loncommon::csv_translate(&mt($_))
 1373:                 } ("username","domain","ID","student name",
 1374:                    "section","start date","end date","status")).'"'."\n";
 1375:         } else {
 1376:             print $CSVfile '"'.join('","',map {
 1377: 		&Apache::loncommon::csv_translate(&mt($_))
 1378:                 } ("username","domain","ID","student name",
 1379:                    "section","start date","end date")).'"'."\n";
 1380:         }
 1381:     } elsif ($mode eq 'excel') {
 1382:         # Create the excel spreadsheet
 1383:         ($excel_workbook,$excel_filename,$format) = 
 1384:             &Apache::loncommon::create_workbook($r);
 1385:         return if (! defined($excel_workbook));
 1386:         $excel_sheet = $excel_workbook->addworksheet('classlist');
 1387:         #
 1388:         my $description = 'Class List for '.
 1389:             $env{'course.'.$env{'request.course.id'}.'.description'};
 1390:         $excel_sheet->write($row++,0,$description,$format->{'h1'});
 1391:         #
 1392:         $excel_sheet->write($row++,0,["username","domain","ID",
 1393:                                       "student name","section",
 1394:                                       "start date","end date","status"],
 1395:                             $format->{'bold'});
 1396:     }
 1397:     #
 1398:     # Sort the students
 1399:     my %index;
 1400:     my $i;
 1401:     foreach (@$keylist) {
 1402:         $index{$_} = $i++;
 1403:     }
 1404:     my $index  = $index{$sortby};
 1405:     my $second = $index{'username'};
 1406:     my $third  = $index{'domain'};
 1407:     my @Sorted_Students = sort {
 1408:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
 1409:             ||
 1410:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
 1411:             ||
 1412:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
 1413:         } (keys(%$classlist));
 1414:     my $studentcount = 0;
 1415:     my $autocount = 0;
 1416:     my $manualcount = 0;
 1417:     my $unlockcount = 0;
 1418:     my $lockcount = 0;
 1419:     foreach my $student (@Sorted_Students) {
 1420:         my $sdata = $classlist->{$student};
 1421:         my $username = $sdata->[$index{'username'}];
 1422:         my $domain   = $sdata->[$index{'domain'}];
 1423:         my $section  = $sdata->[$index{'section'}];
 1424:         my $name     = $sdata->[$index{'fullname'}];
 1425:         my $id       = $sdata->[$index{'id'}];
 1426:         my $status   = $sdata->[$index{'status'}];
 1427:         my $start    = $sdata->[$index{'start'}];
 1428:         my $end      = $sdata->[$index{'end'}];
 1429:         my $type     = $sdata->[$index{'type'}];
 1430:         next if (($statusmode ne 'Any') && ($status ne $statusmode));
 1431:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
 1432:             if (! defined($start) || $start == 0) {
 1433:                 $start = &mt('none');
 1434:             } else {
 1435:                 $start = &Apache::lonlocal::locallocaltime($start);
 1436:             }
 1437:             if (! defined($end) || $end == 0) {
 1438:                 $end = &mt('none');
 1439:             } else {
 1440:                 $end = &Apache::lonlocal::locallocaltime($end);
 1441:             }
 1442:             $r->print("<tr>\n    ");
 1443:             if ($mode eq 'autoenroll') {
 1444:                 my $lockedtype = $sdata->[$index{'lockedtype'}];
 1445:                 $studentcount++;
 1446:                 my $cellentry;
 1447:                 if ($type eq 'auto') {
 1448:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$username.':'.$domain.'" />&nbsp;Change</label>';
 1449:                     $autocount ++;
 1450:                 } else {
 1451:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><nobr><label><input type="checkbox" name="chgmanual" value="'.$username.':'.$domain.'" />&nbsp;Change</label></nobr></td></tr><tr><td><nobr>';
 1452:                     $manualcount ++;
 1453:                     if ($lockedtype) {
 1454:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$username.':'.$domain.'" />&nbsp;'.&mt('Unlock').'</label>';
 1455:                         $unlockcount ++;
 1456:                     } else {
 1457:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$username.':'.$domain.'" />&nbsp;'.&mt('Lock').'</label>';
 1458:                         $lockcount ++;
 1459:                     }
 1460:                     $cellentry .= '</nobr></td></tr></table>';
 1461:                 }
 1462:                 $r->print("<td>$cellentry<td>\n    ");
 1463:             } else {
 1464:                 $r->print("<td>".(++$studentcount)."</td><td>\n    ");
 1465:             }
 1466:             if ($linkto eq 'nothing') {
 1467:                 $r->print($username);
 1468:             } elsif ($linkto eq 'aboutme') {
 1469:                 $r->print(&Apache::loncommon::aboutmewrapper($username,
 1470:                                                              $username,
 1471:                                                              $domain));
 1472:             } elsif ($linkto eq 'modify') {
 1473:                 $r->print('<a href="'.
 1474:                           "javascript:document.studentform.sname.value='".
 1475:                           $username.
 1476:                           "';document.studentform.sdom.value='".$domain.
 1477:                           "';document.studentform.state.value='selected".
 1478:                           "';document.studentform.submit();".'">'.
 1479:                           $username."</a>\n");
 1480:             }
 1481:             $r->print(<<"END");
 1482:     </td>
 1483:     <td>$domain</td>
 1484:     <td>$id</td>
 1485:     <td>$name</td>
 1486:     <td>$section</td>
 1487:     <td>$start</td>
 1488:     <td>$end</td>
 1489: END
 1490:             if ($env{'course.'.$env{'request.course.id'}.
 1491: 			 '.internal.showphoto'}) {
 1492:                 if ($displayphotos eq 'on') {
 1493:                     my $imgurl = 
 1494: 			&Apache::lonnet::retrievestudentphoto($domain,
 1495: 							      $username,'gif',
 1496: 							      'thumbnail');
 1497:                 
 1498:                     $r->print('    <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($domain,$username,'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
 1499:                 } else {
 1500:                     $r->print('    <td>&nbsp;</td>  ');
 1501:                 }
 1502:             }
 1503:             $r->print('  </tr>  ');
 1504:         } elsif ($mode eq 'csv') {
 1505:             next if (! defined($CSVfile));
 1506:             # no need to bother with $linkto
 1507:             if (! defined($start) || $start == 0) {
 1508:                 $start = &mt('none');
 1509:             } else {
 1510:                 $start = &Apache::lonlocal::locallocaltime($start);
 1511:             }
 1512:             if (! defined($end) || $end == 0) {
 1513:                 $end = &mt('none');
 1514:             } else {
 1515:                 $end = &Apache::lonlocal::locallocaltime($end);
 1516:             }
 1517:             my @line = ();
 1518:             foreach ($username,$domain,$id,$name,$section,$start,$end) {
 1519:                 push @line,&Apache::loncommon::csv_translate($_);
 1520:             }
 1521:             if ($statusmode eq 'Any') {
 1522:                 push @line,&Apache::loncommon::csv_translate($status);
 1523:             }
 1524:             print $CSVfile '"'.join('","',@line).'"'."\n";
 1525:         } elsif ($mode eq 'excel') {
 1526:             $excel_sheet->write($row,0,[$username,$domain,$id,
 1527:                                           $name,$section]);
 1528:             my $col = 5;
 1529:             foreach my $time ($start,$end) {
 1530:                 if (defined($time) && $time != 0) {
 1531:                     $excel_sheet->write($row,$col++,
 1532:                                    &Apache::lonstathelpers::calc_serial($time),
 1533:                                     $format->{'date'});
 1534:                 } else {
 1535:                     $excel_sheet->write($row,$col++,'none');
 1536:                 }                    
 1537:             }
 1538:             $excel_sheet->write($row,$col++,$status);
 1539:             $row++;
 1540:         }
 1541:     }
 1542:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
 1543:         $r->print('</table><br>');
 1544:     } elsif ($mode eq 'excel') {
 1545:         $excel_workbook->close();
 1546:         $r->print('<p><a href="'.$excel_filename.'">'.
 1547:                   &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
 1548:     } elsif ($mode eq 'csv') {
 1549:         close($CSVfile);
 1550:         $r->print('<a href="'.$CSVfilename.'">'.
 1551:                   &mt('Your CSV file').'</a> is ready for download.'.
 1552:                   "\n");
 1553:         $r->rflush();
 1554:     }
 1555:     if ($mode eq 'autoenroll') {
 1556:         return ($studentcount,$autocount,$manualcount,$lockcount,$unlockcount);
 1557:     }
 1558:     return;
 1559: }
 1560: 
 1561: 
 1562: #
 1563: # print out form for modification of a single students data
 1564: #
 1565: sub print_modify_student_form {
 1566:     my $r = shift();
 1567:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1568:                                             ['sdom','sname']);    
 1569:     my $sname  = $env{'form.sname'};
 1570:     my $sdom   = $env{'form.sdom'};
 1571:     my $sortby = $env{'form.sortby'};
 1572:     # determine the students name information
 1573:     my %info=&Apache::lonnet::get('environment',
 1574:                                   ['firstname','middlename',
 1575:                                    'lastname','generation','id'],
 1576:                                   $sdom, $sname);
 1577:     my ($tmp) = keys(%info);
 1578:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
 1579:         $r->print('<font color="#ff0000" size="+2">'.&mt('Error').'</font>'.
 1580:                   '<p>'.
 1581:                   &mt('Unable to retrieve environment data for').' '.$sname.
 1582:                   &mt('in domain').' '.$sdom.'</p><p>'.
 1583:                   &mt('Please contact your LON-CAPA administrator regarding this situation.').'</p>'.&Apache::loncommon::end_page());
 1584:         return;
 1585:     }
 1586:     # determine the students starting and ending times and section
 1587:     my ($starttime,$endtime,$section) = &get_enrollment_data($sname,$sdom);
 1588:     if ($starttime =~ /^error/) {
 1589:         $r->print('<h2>'&mt('Error').'</h2>');
 1590:         $r->print('<p>'.$starttime.'</p>');
 1591:         return;
 1592:     }
 1593:     #
 1594:     # Deal with date forms
 1595:     my $current_date_description = '';
 1596:     my $textdate = '';
 1597: 
 1598:     if (! defined($starttime) || $starttime == 0) {
 1599:         $current_date_description = &mt('Current Starting Date: not set').
 1600:             '<br />';
 1601:     } else {
 1602:         $current_date_description = 
 1603:             &mt('Current Starting Date: [_1]',
 1604:                 &Apache::lonlocal::locallocaltime($starttime)).'<br />';
 1605:     }
 1606:     if (! defined($endtime) || $endtime == 0) {
 1607:         $current_date_description.= &mt('Current Ending Date: not set').
 1608:             '<br />';
 1609:     } else {
 1610:         $current_date_description.= 
 1611:             &mt('Current Ending Date: [_1]',
 1612:                 &Apache::lonlocal::locallocaltime($endtime)).'<br />';
 1613: 
 1614:     }
 1615:     my $date_table = &date_setting_table($starttime,$endtime);
 1616:     #
 1617:     if (! exists($env{'form.Status'}) || 
 1618:         $env{'form.Status'} !~ /^(Any|Expired|Active)$/) {
 1619:         $env{'form.Status'} = 'crap';
 1620:     }
 1621:     # Make sure student is enrolled in course
 1622:     my %lt=&Apache::lonlocal::texthash(
 1623: 	           'mef'   => "Modify Enrollment for",
 1624:                    'odcc'  => "Only domain coordinators can change a users password.",
 1625:                    'sn'    => "Student Name",
 1626:                    'fn'    => "First",
 1627:                    'mn'    => "Middle",
 1628:                    'ln'    => "Last",
 1629:                    'gen'   => "Generation",
 1630:                    'sid'   => "Student ID",
 1631:                    'disn'  => "Disable ID/Student Number Safeguard and Force Change of Conflicting IDs (only do if you know what you are doing)",
 1632:                    'sec'   => "Section",
 1633:                    'sm'    => "Submit Modifications",
 1634: 				       );
 1635:     $r->print(<<END);
 1636: <p>
 1637: <font size="+1">
 1638: $lt{'odcc'}
 1639: </font>
 1640: </p>
 1641: <input type="hidden" name="slogin"  value="$sname"  />
 1642: <input type="hidden" name="sdomain" value="$sdom" />
 1643: <input type="hidden" name="action"  value="modifystudent" />
 1644: <input type="hidden" name="state"   value="done" />
 1645: <input type="hidden" name="sortby"  value="$sortby" />
 1646: <input type="hidden" name="Status"  value="$env{'form.Status'}" />
 1647: <h2>$lt{'mef'} $info{'firstname'} $info{'middlename'} 
 1648: $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
 1649: <p>
 1650: <b>$lt{'sn'}</b>
 1651: <table>
 1652: <tr><th>$lt{'fn'}</th><th>$lt{'mn'}</th><th>$lt{'ln'}</th><th>$lt{'gen'}</th></tr>
 1653: <tr><td>
 1654: <input type="text" name="firstname"  value="$info{'firstname'}"  /></td><td>
 1655: <input type="text" name="middlename" value="$info{'middlename'}" /></td><td>
 1656: <input type="text" name="lastname"   value="$info{'lastname'}"   /></td><td>
 1657: <input type="text" name="generation" value="$info{'generation'}" /></td></tr>
 1658: </table>
 1659: </p><p>
 1660: <b>$lt{'sid'}</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
 1661: </p><p>
 1662: <label>
 1663: <input type="checkbox" name="forceid" > 
 1664: $lt{'disn'}
 1665: </label>
 1666: </p><p>
 1667: <b>$lt{'sec'}</b>: <input type="text" name="section" value="$section" size="14"/>
 1668: </p>
 1669: <p>$current_date_description</p>
 1670: <p>$date_table</p>
 1671: <input type="submit" value="$lt{'sm'}" />
 1672: END
 1673:     $r->print(&Apache::loncommon::end_page());
 1674:     return;
 1675: }
 1676: 
 1677: #
 1678: # modify a single students section 
 1679: #
 1680: sub modify_single_student {
 1681:     my ($r) = @_;
 1682:     #
 1683:     # Remove non alphanumeric values from the section
 1684:     $env{'form.section'} =~ s/\W//g;
 1685:     #
 1686:     # Do the date defaults first
 1687:     my ($starttime,$endtime) = &get_dates_from_form();
 1688:     if ($env{'form.makedatesdefault'}) {
 1689:         $r->print(&make_dates_default($starttime,$endtime));
 1690:     }
 1691:     # Get the 'sortby' and 'Status' variables so the user goes back to their
 1692:     # previous screen
 1693:     my $sortby = $env{'form.sortby'};
 1694:     my $status = $env{'form.Status'};
 1695:     #
 1696:     # We always need this information
 1697:     my $slogin     = $env{'form.slogin'};
 1698:     my $sdom       = $env{'form.sdomain'};
 1699:     #
 1700:     # Get the old data
 1701:     my %old=&Apache::lonnet::get('environment',
 1702:                                  ['firstname','middlename',
 1703:                                   'lastname','generation','id'],
 1704:                                  $sdom, $slogin);
 1705:     $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
 1706:                                                   $env{'request.course.id'});
 1707:     my ($tmp) = keys(%old);
 1708:     if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
 1709:         $r->print(&mt('There was an error determining the environment values for')." $slogin \@ $sdom.");
 1710:         return;
 1711:     }
 1712:     undef $tmp;
 1713:     #
 1714:     # Get the new data
 1715:     my $firstname  = $env{'form.firstname'};
 1716:     my $middlename = $env{'form.middlename'};
 1717:     my $lastname   = $env{'form.lastname'};
 1718:     my $generation = $env{'form.generation'};
 1719:     my $section    = $env{'form.section'};
 1720:     my $courseid   = $env{'request.course.id'};
 1721:     my $sid        = $env{'form.id'};
 1722:     my $displayable_starttime = localtime($starttime);
 1723:     my $displayable_endtime   = localtime($endtime);
 1724:     # 
 1725:     # check for forceid override
 1726:     if ((defined($old{'id'})) && ($old{'id'} ne '') && 
 1727:         ($sid ne $old{'id'}) && (! exists($env{'form.forceid'}))) {
 1728:         $r->print("<font color=\"ff0000\">".&mt('You changed the students id but did not disable the ID change safeguard. The students id will not be changed.')."</font>");
 1729:         $sid = $old{'id'};
 1730:     }
 1731:     #
 1732:     # talk to the user about what we are going to do
 1733:     my %lt=&Apache::lonlocal::texthash(
 1734: 	           'mdu'   => "Modifying data for user",
 1735:                    'si'    => "Student Information",
 1736:                    'fd'    => "Field",
 1737:                    'ov'    => "Old Value",
 1738:                    'nv'    => "New Value",
 1739:                    'fn'    => "First name",
 1740:                    'mn'    => "Middle name",
 1741:                    'ln'    => "Last name",
 1742:                    'gen'   => "Generation",
 1743:                    'sec'   => "Section",
 1744:                    'ri'    => "Role Information",
 1745:                    'st'    => "Start Time",
 1746:                    'et'    => "End Time",
 1747: 				       );
 1748:     $r->print(<<END);
 1749:     <h2>$lt{'mdu'} $slogin \@ $sdom </h2>
 1750: <h3>$lt{'si'}</h3>
 1751: <table rules="rows" border="1" cellpadding="3" >
 1752: <tr>
 1753:     <th> $lt{'fd'} </th>
 1754:     <th> $lt{'ov'} </th>
 1755:     <th> $lt{'nv'} </th>
 1756: </tr>
 1757: <tr>
 1758:     <td> <b>$lt{'fn'}</b> </td>
 1759:     <td> $old{'firstname'} </td>
 1760:     <td> $firstname </td>
 1761: </tr><tr>
 1762:     <td> <b>$lt{'mn'}</b> </td>
 1763:     <td> $old{'middlename'} </td>
 1764:     <td> $middlename </td>
 1765: </tr><tr>
 1766:     <td> <b>$lt{'ln'}</b> </td>
 1767:     <td> $old{'lastname'} </td>
 1768:     <td> $lastname </td>
 1769: </tr><tr>
 1770:     <td> <b>$lt{'gen'}</b> </td>
 1771:     <td> $old{'generation'} </td>
 1772:     <td> $generation </td>
 1773: </tr><tr>
 1774:     <td> <b>ID</b> </td>
 1775:     <td> $old{'id'} </td>
 1776:     <td> $sid </td>
 1777: </tr><tr>
 1778:     <td> <b>$lt{'sec'}</b> </td>
 1779:     <td> $old{'section'} </td>
 1780:     <td> $section</td>
 1781: </tr>
 1782: </table>
 1783: <h3>$lt{'ri'}</h3>
 1784: <table>
 1785: <tr><td align="right"><b>$lt{'st'}:</b></td><td> $displayable_starttime </td></tr>
 1786: <tr><td align="right"><b>$lt{'et'}:</b></td><td> $displayable_endtime   </td></tr>
 1787: </table>
 1788: <p>
 1789: END
 1790:     #
 1791:     # Send request(s) to modify data (final undef is for 'desiredhost',
 1792:     # which is a moot point because the student already has an account.
 1793:     my $modify_section_results = &modifystudent($sdom,$slogin,
 1794:                                                 $env{'request.course.id'},
 1795:                                                 $section,undef);
 1796:     if ($modify_section_results !~ /^ok/) {
 1797:         $r->print(&mt('An error occured during the attempt to change the section for this student.')."<br />");
 1798:     }
 1799:     my $roleresults = &Apache::lonnet::modifystudent
 1800:         ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
 1801:          $generation,$section,$endtime,$starttime,$env{'form.forceid'});
 1802:     if ($roleresults eq 'refused' ) {
 1803:         $r->print(&mt('Your request to change the role information for this student was refused. You do not appear to have sufficient authority to change student information.'));
 1804:     } elsif ($roleresults !~ /ok/) {
 1805:         $r->print(&mt('An error occurred during the attempt to change the role information for this student.')."  <br />".
 1806:                   &mt('The error reported was')." ".
 1807:                   $roleresults);
 1808:         &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
 1809:                                  " data for ".$slogin." \@ ".$sdom." by ".
 1810:                                  $env{'user.name'}." \@ ".$env{'user.domain'}.
 1811:                                  ":".$roleresults);
 1812:     } else { # everything is okay!
 1813:         $r->print(&mt('Student information updated successfully.')." <br />".
 1814:                   &mt('The student must log out and log in again to see these changes.'));
 1815:     }
 1816:     my $Masd=&mt('Modify another students data');
 1817:     $r->print(<<END);
 1818: </p><p>
 1819: <input type="hidden" name="action" value="modifystudent" />
 1820: <input type="hidden" name="sortby" value="$sortby" />
 1821: <input type="hidden" name="Status" value="$status" />
 1822: <a href="javascript:document.studentform.submit();">$Masd</a>
 1823: END
 1824:     $r->print(&Apache::loncommon::end_page());
 1825:     return;
 1826: }
 1827: 
 1828: sub get_enrollment_data {
 1829:     my ($sname,$sdomain) = @_;
 1830:     my $courseid = $env{'request.course.id'};
 1831:     $courseid =~ s:_:/:g;
 1832:     my %roles = &Apache::lonnet::dump('roles',$sdomain,$sname);
 1833:     my ($tmp) = keys(%roles);
 1834:     # Bail out if we were unable to get the students roles
 1835:     return ('error'.$tmp) if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 1836:     # Go through the roles looking for enrollment in this course
 1837:     my ($end,$start) = (undef,undef);
 1838:     my $section = '';
 1839:     my $count = scalar(keys(%roles));
 1840:     while (my ($course,$role) = each(%roles)) {
 1841:         if ($course=~ /^\/$courseid\/*\s*(\w+)*_st$/ ) {
 1842:             #
 1843:             # Get active role
 1844:             $section=$1;
 1845:             (undef,$end,$start)=split(/\_/,$role);
 1846:             my $now=time;
 1847:             my $notactive=0;
 1848:             if ($start) {
 1849:                 if ($now<$start) { $notactive=1; }
 1850:             }
 1851:             if ($end) {
 1852:                 if ($now>$end) { $notactive=1; }
 1853:             } 
 1854:             unless ($notactive) { return ($start,$end,$section); }
 1855:         }
 1856:     }
 1857:     return ($start,$end,$section);
 1858: }
 1859: 
 1860: #################################################
 1861: #################################################
 1862: 
 1863: =pod
 1864: 
 1865: =item show_drop_list
 1866: 
 1867: Display a list of students to drop
 1868: Inputs: 
 1869: 
 1870: =over 4
 1871: 
 1872: =item $r, Apache request
 1873: 
 1874: =item $classlist, hash pointer returned from loncoursedata::get_classlist();
 1875: 
 1876: =item $keylist, array pointer returned from loncoursedata::get_classlist() 
 1877: which describes the order elements are stored in the %$classlist values.
 1878: 
 1879: =item $nosort, if true, sorting links are omitted.
 1880: 
 1881: =back
 1882: 
 1883: =cut
 1884: 
 1885: #################################################
 1886: #################################################
 1887: sub show_drop_list {
 1888:     my ($r,$classlist,$keylist,$nosort)=@_;
 1889:     my $cid=$env{'request.course.id'};
 1890:     if (! exists($env{'form.sortby'})) {
 1891:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1892:                                                 ['sortby']);
 1893:     }
 1894:     my $sortby = $env{'form.sortby'};
 1895:     if ($sortby !~ /^(username|domain|section|fullname|id|start|end)$/) {
 1896:         $sortby = 'username';
 1897:     }
 1898:     #
 1899:     my $action = "drop";
 1900:     $r->print(<<END);
 1901: <input type="hidden" name="sortby" value="$sortby" />
 1902: <input type="hidden" name="action" value="$action" />
 1903: <input type="hidden" name="state"  value="done" />
 1904: <script>
 1905: function checkAll(field) {
 1906:     for (i = 0; i < field.length; i++)
 1907:         field[i].checked = true ;
 1908: }
 1909: 
 1910: function uncheckAll(field) {
 1911:     for (i = 0; i < field.length; i++)
 1912:         field[i].checked = false ;
 1913: }
 1914: </script>
 1915: <p>
 1916: <input type="hidden" name="phase" value="four">
 1917: END
 1918: 
 1919: my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
 1920:                                    'dom'    => "domain",
 1921:                                    'sn'     => "student name",
 1922:                                    'sec'    => "section",
 1923:                                    'start'  => "start date",
 1924:                                    'end'    => "end date",
 1925:                                    );
 1926:     if ($nosort) {
 1927:         $r->print(<<END);
 1928: <table border=2>
 1929: <tr>
 1930:     <th>&nbsp;</th>
 1931:     <th>$lt{'usrn'}</th>
 1932:     <th>$lt{'dom'}</th>
 1933:     <th>ID</th>
 1934:     <th>$lt{'sn'}</th>
 1935:     <th>$lt{'sec'}</th>
 1936:     <th>$lt{'start'}</th>
 1937:     <th>$lt{'end'}</th>
 1938: </tr>
 1939: END
 1940: 
 1941:     } else  {
 1942:         $r->print(<<END);
 1943: <table border=2>
 1944: <tr><th>&nbsp;</th>
 1945:     <th>
 1946:        <a href="/adm/dropadd?action=$action&sortby=username">$lt{'usrn'}</a>
 1947:     </th><th>
 1948:        <a href="/adm/dropadd?action=$action&sortby=domain">$lt{'dom'}</a>
 1949:     </th><th>
 1950:        <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
 1951:     </th><th>
 1952:        <a href="/adm/dropadd?action=$action&sortby=fullname">$lt{'sn'}</a>
 1953:     </th><th>
 1954:        <a href="/adm/dropadd?action=$action&sortby=section">$lt{'sec'}</a>
 1955:     </th><th>
 1956:        <a href="/adm/dropadd?action=$action&sortby=start">$lt{'start'}</a>
 1957:     </th><th>
 1958:        <a href="/adm/dropadd?action=$action&sortby=end">$lt{'end'}</a>
 1959:     </th>
 1960: </tr>
 1961: END
 1962:     }
 1963:     #
 1964:     # Sort the students
 1965:     my %index;
 1966:     my $i;
 1967:     foreach (@$keylist) {
 1968:         $index{$_} = $i++;
 1969:     }
 1970:     my $index  = $index{$sortby};
 1971:     my $second = $index{'username'};
 1972:     my $third  = $index{'domain'};
 1973:     my @Sorted_Students = sort {
 1974:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
 1975:             ||
 1976:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
 1977:             ||
 1978:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
 1979:         } (keys(%$classlist));
 1980:     foreach my $student (@Sorted_Students) {
 1981:         my $error;
 1982:         my $sdata = $classlist->{$student};
 1983:         my $username = $sdata->[$index{'username'}];
 1984:         my $domain   = $sdata->[$index{'domain'}];
 1985:         my $section  = $sdata->[$index{'section'}];
 1986:         my $name     = $sdata->[$index{'fullname'}];
 1987:         my $id       = $sdata->[$index{'id'}];
 1988:         my $start    = $sdata->[$index{'start'}];
 1989:         my $end      = $sdata->[$index{'end'}];
 1990:         if (! defined($start) || $start == 0) {
 1991:             $start = &mt('none');
 1992:         } else {
 1993:             $start = &Apache::lonlocal::locallocaltime($start);
 1994:         }
 1995:         if (! defined($end) || $end == 0) {
 1996:             $end = &mt('none');
 1997:         } else {
 1998:             $end = &Apache::lonlocal::locallocaltime($end);
 1999:         }
 2000:         my $status   = $sdata->[$index{'status'}];
 2001:         next if ($status ne 'Active');
 2002:         #
 2003:         $r->print(<<"END");
 2004: <tr>
 2005:     <td><input type="checkbox" name="droplist" value="$student"></td>
 2006:     <td>$username</td>
 2007:     <td>$domain</td>
 2008:     <td>$id</td>
 2009:     <td>$name</td>
 2010:     <td>$section</td>
 2011:     <td>$start</td>
 2012:     <td>$end</td>
 2013: </tr>
 2014: END
 2015:     }
 2016:     $r->print('</table><br>');
 2017:     %lt=&Apache::lonlocal::texthash(
 2018: 	               'dp'   => "Drop Students",
 2019:                        'ca'   => "check all",
 2020:                        'ua'   => "uncheck all",
 2021: 				       );
 2022:     $r->print(<<"END");
 2023: </p><p>
 2024: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
 2025: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
 2026: <p><input type=submit value="$lt{'dp'}"></p>
 2027: END
 2028:     return;
 2029: }
 2030: 
 2031: #
 2032: # Print out the initial form to get the courselist file
 2033: #
 2034: sub print_first_courselist_upload_form {
 2035:     my $r=shift;
 2036:     my $str;
 2037:     $str  = '<input type="hidden" name="phase" value="two">';
 2038:     $str .= '<input type="hidden" name="action" value="upload" />';
 2039:     $str .= '<input type="hidden"   name="state"  value="got_file" />';
 2040:     $str .= "<h3>".&mt('Upload a class list')."</h3>\n";
 2041:     $str .= &Apache::loncommon::upfile_select_html();
 2042:     $str .= "<p>\n";
 2043:     $str .= '<input type="submit" name="fileupload" value="'.
 2044:         &mt('Upload class list').'">'."\n";
 2045:     $str .= '<label><input type="checkbox" name="noFirstLine" /> '.
 2046:         &mt('Ignore First Line')."</label></p>\n";
 2047:     $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
 2048:                          &mt("How do I create a class list from a spreadsheet")).
 2049:                              "<br />\n";
 2050:     $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
 2051:                            &mt("How do I create a CSV file from a spreadsheet")).
 2052:                                "<br />\n";
 2053:     $str .= &Apache::loncommon::end_page();
 2054:     $r->print($str);
 2055:     return;
 2056: }
 2057: 
 2058: # ================================================= Drop/Add from uploaded file
 2059: sub upfile_drop_add {
 2060:     my $r=shift;
 2061:     &Apache::loncommon::load_tmp_file($r);
 2062:     my @studentdata=&Apache::loncommon::upfile_record_sep();
 2063:     if($env{'form.noFirstLine'}){shift(@studentdata);}
 2064:     my @keyfields = split(/\,/,$env{'form.keyfields'});
 2065:     my $cid = $env{'request.course.id'};
 2066:     my %fields=();
 2067:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
 2068:         if ($env{'form.upfile_associate'} eq 'reverse') {
 2069:             if ($env{'form.f'.$i} ne 'none') {
 2070:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
 2071:             }
 2072:         } else {
 2073:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
 2074:         }
 2075:     }
 2076:     #
 2077:     # Store the field choices away
 2078:     foreach my $field (qw/username names 
 2079:                        fname mname lname gen id sec ipwd email/) {
 2080:         $env{'form.'.$field.'_choice'}=$fields{$field};
 2081:     }
 2082:     &Apache::loncommon::store_course_settings('enrollment_upload',
 2083:                                               { 'username_choice' => 'scalar',
 2084:                                                 'names_choice' => 'scalar',
 2085:                                                 'fname_choice' => 'scalar',
 2086:                                                 'mname_choice' => 'scalar',
 2087:                                                 'lname_choice' => 'scalar',
 2088:                                                 'gen_choice' => 'scalar',
 2089:                                                 'id_choice' => 'scalar',
 2090:                                                 'sec_choice' => 'scalar',
 2091:                                                 'ipwd_choice' => 'scalar',
 2092:                                                 'email_choice' => 'scalar' });
 2093: 
 2094:     #
 2095:     my ($startdate,$enddate) = &get_dates_from_form();
 2096:     if ($env{'form.makedatesdefault'}) {
 2097:         $r->print(&make_dates_default($startdate,$enddate));
 2098:     }
 2099:     # Determine domain and desired host (home server)
 2100:     my $domain=$env{'form.lcdomain'};
 2101:     my $desiredhost = $env{'form.lcserver'};
 2102:     if (lc($desiredhost) eq 'default') {
 2103:         $desiredhost = undef;
 2104:     } else {
 2105:         my %home_servers = &Apache::loncommon::get_library_servers($domain);
 2106:         if (! exists($home_servers{$desiredhost})) {
 2107:             $r->print('<font color="#ff0000">'.&mt('Error').'</font>'.
 2108:                       &mt('Invalid home server specified'));
 2109:             $r->print(&Apache::loncommon::end_page());
 2110:             return;
 2111:         }
 2112:     }
 2113:     # Determine authentication mechanism
 2114:     my $amode  = '';
 2115:     my $genpwd = '';
 2116:     if ($env{'form.login'} eq 'krb') {
 2117:         $amode='krb';
 2118: 	$amode.=$env{'form.krbver'};
 2119:         $genpwd=$env{'form.krbarg'};
 2120:     } elsif ($env{'form.login'} eq 'int') {
 2121:         $amode='internal';
 2122:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
 2123:             $genpwd=$env{'form.intarg'};
 2124:         }
 2125:     } elsif ($env{'form.login'} eq 'loc') {
 2126:         $amode='localauth';
 2127:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
 2128:             $genpwd=$env{'form.locarg'};
 2129:         }
 2130:     }
 2131:     if ($amode =~ /^krb/) {
 2132:         if (! defined($genpwd) || $genpwd eq '') {
 2133:             $r->print('<font color="red" size="+1">'.
 2134:                       &mt('Unable to enroll students').'</font>  '.
 2135:                       &mt('No Kerberos domain was specified.').'</p>');
 2136:             $amode = ''; # This causes the loop below to be skipped
 2137:         }
 2138:     }
 2139:     unless (($domain=~/\W/) || ($amode eq '')) {
 2140:         #######################################
 2141:         ##         Enroll Students           ##
 2142:         #######################################
 2143:         $r->print('<h3>'.&mt('Enrolling Students')."</h3>\n<p>\n");
 2144:         my $count=0;
 2145:         my $flushc=0;
 2146:         my %student=();
 2147:         # Get new classlist
 2148:         foreach (@studentdata) {
 2149:             my %entries=&Apache::loncommon::record_sep($_);
 2150:             # Determine student name
 2151:             unless (($entries{$fields{'username'}} eq '') ||
 2152:                     (!defined($entries{$fields{'username'}}))) {
 2153:                 my ($fname, $mname, $lname,$gen) = ('','','','');
 2154:                 if (defined($fields{'names'})) {
 2155:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
 2156:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
 2157:                 } else {
 2158:                     if (defined($fields{'fname'})) {
 2159:                         $fname=$entries{$fields{'fname'}};
 2160:                     }
 2161:                     if (defined($fields{'mname'})) {
 2162:                         $mname=$entries{$fields{'mname'}};
 2163:                     }
 2164:                     if (defined($fields{'lname'})) {
 2165:                         $lname=$entries{$fields{'lname'}};
 2166:                     }
 2167:                     if (defined($fields{'gen'})) {
 2168:                         $gen=$entries{$fields{'gen'}};
 2169:                     }
 2170:                 }
 2171:                 if ($entries{$fields{'username'}}=~/\W/) {
 2172:                     $r->print('<br />'.
 2173:       &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
 2174:           $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
 2175:                               '</b>');
 2176:                 } else {
 2177:                     # determine section number
 2178:                     my $sec='';
 2179:                     my $username=$entries{$fields{'username'}};
 2180:                     if (defined($fields{'sec'})) {
 2181:                         if (defined($entries{$fields{'sec'}})) {
 2182:                             $sec=$entries{$fields{'sec'}};
 2183:                         }
 2184:                     }
 2185:                     # remove non alphanumeric values from section
 2186:                     $sec =~ s/\W//g;
 2187:                     # determine student id number
 2188:                     my $id='';
 2189:                     if (defined($fields{'id'})) {
 2190:                         if (defined($entries{$fields{'id'}})) {
 2191:                             $id=$entries{$fields{'id'}};
 2192:                         }
 2193:                         $id=~tr/A-Z/a-z/;
 2194:                     }
 2195:                     # determine email address
 2196:                     my $email='';
 2197:                     if (defined($fields{'email'})) {
 2198:                         if (defined($entries{$fields{'email'}})) {
 2199:                             $email=$entries{$fields{'email'}};
 2200:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
 2201:                         }
 2202:                     }
 2203:                     # determine student password
 2204:                     my $password='';
 2205:                     if ($genpwd) { 
 2206:                         $password=$genpwd; 
 2207:                     } else {
 2208:                         if (defined($fields{'ipwd'})) {
 2209:                             if ($entries{$fields{'ipwd'}}) {
 2210:                                 $password=$entries{$fields{'ipwd'}};
 2211:                             }
 2212:                         }
 2213:                     }
 2214:                     # Clean up whitespace
 2215:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
 2216:                              \$lname,\$gen,\$sec) {
 2217:                         $$_ =~ s/(\s+$|^\s+)//g;
 2218:                     }
 2219:                     if ($password || $env{'form.login'} eq 'loc') {
 2220:                         &modifystudent($domain,$username,$cid,$sec,
 2221:                                        $desiredhost);
 2222:                         my $reply=&Apache::lonnet::modifystudent
 2223:                             ($domain,$username,$id,$amode,$password,
 2224:                              $fname,$mname,$lname,$gen,$sec,$enddate,
 2225:                              $startdate,$env{'form.forceid'},$desiredhost,
 2226:                              $email);
 2227:                         if ($reply ne 'ok') {
 2228:                             $reply =~ s/^error://;
 2229:                             $r->print('<br />'.
 2230:                 &mt('<b>[_1]</b>:  Unable to enroll: [_2]',$username,$reply));
 2231:          		} else {
 2232:                             $count++; $flushc++;
 2233:                             $student{$username}=1;
 2234:                             $r->print('. ');
 2235:                             if ($flushc>15) {
 2236: 				$r->rflush;
 2237:                                 $flushc=0;
 2238:                             }
 2239:                         }
 2240:                     } else {
 2241:                         $r->print('<br />'.
 2242:       &mt('<b>[_1]</b>: Unable to enroll.  No password specified.',$username)
 2243:                                   );
 2244:                     }
 2245:                 }
 2246:             }
 2247:         } # end of foreach (@studentdata)
 2248:         $r->print("</p>\n<p>\n".&mt('Processed [_1] student(s).',$count).
 2249:                   "</p>\n");
 2250:         $r->print("<p>\n".
 2251:                   &mt('If active, the new role will be available when the '.
 2252:                   'students next log in to LON-CAPA.')."</p>\n");
 2253:         #####################################
 2254:         #           Drop students           #
 2255:         #####################################
 2256:         if ($env{'form.fullup'} eq 'yes') {
 2257:             $r->print('<h3>'.&mt('Dropping Students')."</h3>\n");
 2258:             #  Get current classlist
 2259:             my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
 2260:             if (! defined($classlist)) {
 2261:                 $r->print(&mt('There are no students currently enrolled.').
 2262:                           "\n");
 2263:             } else {
 2264:                 # Remove the students we just added from the list of students.
 2265:                 foreach (@studentdata) {
 2266:                     my %entries=&Apache::loncommon::record_sep($_);
 2267:                     unless (($entries{$fields{'username'}} eq '') ||
 2268:                             (!defined($entries{$fields{'username'}}))) {
 2269:                         delete($classlist->{$entries{$fields{'username'}}.
 2270:                                                 ':'.$domain});
 2271:                     }
 2272:                 }
 2273:                 # Print out list of dropped students.
 2274:                 &show_drop_list($r,$classlist,$keylist,'nosort');
 2275:             }
 2276:         }
 2277:     } # end of unless
 2278: }
 2279: 
 2280: # ================================================================== Phase four
 2281: sub drop_student_list {
 2282:     my $r=shift;
 2283:     my $count=0;
 2284:     my @droplist = &Apache::loncommon::get_env_multiple('form.droplist');
 2285:     foreach (@droplist) {
 2286:         my ($uname,$udom)=split(/\:/,$_);
 2287:         # drop student
 2288:         my $result = &modifystudent($udom,$uname,$env{'request.course.id'});
 2289:         if ($result eq 'ok' || $result eq 'ok:') {
 2290:             $r->print(&mt('Dropped [_1]',$uname.'@'.$udom).'<br>');
 2291:             $count++;
 2292:         } else {
 2293:             $r->print(
 2294:           &mt('Error dropping [_1]:[_2]',$uname.'@'.$udom,$result).
 2295:                       '<br />');
 2296:         }
 2297:     }
 2298:     $r->print('<p><b>'.&mt('Dropped [_1] student(s).',$count).'</b></p>');
 2299:     $r->print('<p>'.&mt('Re-enrollment will re-activate data.')) if ($count);
 2300: }
 2301: 
 2302: ###################################################################
 2303: ###################################################################
 2304: 
 2305: =pod
 2306: 
 2307: =item &handler
 2308: 
 2309: The typical handler you see in all these modules.  Takes $r, the
 2310: http request, as an argument.  
 2311: 
 2312: The response to the request is governed by two form variables
 2313: 
 2314:  form.action      form.state     response
 2315:  ---------------------------------------------------
 2316:  undefined        undefined      print main menu
 2317:  upload           undefined      print courselist upload menu
 2318:  upload           got_file       deal with uploaded file,
 2319:                                  print the upload managing menu
 2320:  upload           enrolling      enroll students based on upload
 2321:  drop             undefined      print the classlist ready to drop
 2322:  drop             done           drop the selected students
 2323:  enrollstudent    undefined      print student username domain form
 2324:  enrollstudent    gotusername    print single student enroll menu
 2325:  enrollstudent    enrolling      enroll student
 2326:  classlist        undefined      print html classlist
 2327:  classlist        csv            print csv classlist
 2328:  modifystudent    undefined      print classlist to select student to modify
 2329:  modifystudent    selected       print modify student menu
 2330:  modifystudent    done           make modifications to student record
 2331: 
 2332: =cut
 2333: 
 2334: ###################################################################
 2335: ###################################################################
 2336: sub handler {
 2337:     my $r=shift;
 2338:     if ($r->header_only) {
 2339:         &Apache::loncommon::content_type($r,'text/html');
 2340:         $r->send_http_header;
 2341:         return OK;
 2342:     }
 2343:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 2344:                                             ['action','state']);
 2345: 
 2346:     &Apache::lonhtmlcommon::clear_breadcrumbs();
 2347:     &Apache::lonhtmlcommon::add_breadcrumb
 2348:         ({href=>"/adm/dropadd",
 2349:           text=>"Enrollment Manager",
 2350:           faq=>9,bug=>'Instructor Interface',});
 2351:     #  Needs to be in a course
 2352:     if (! ($env{'request.course.fn'})) {
 2353:         # Not in a course
 2354:         $env{'user.error.msg'}=
 2355:             "/adm/dropadd:cst:0:0:Cannot manage or view course groups, ".
 2356:                                   "or drop or add students";
 2357:         return HTTP_NOT_ACCEPTABLE; 
 2358:     }
 2359:     #
 2360:     my $view_permission = 
 2361:         &Apache::lonnet::allowed('vcl',$env{'request.course.id'});
 2362:     my $enrl_permission = 
 2363:         &Apache::lonnet::allowed('cst',$env{'request.course.id'});
 2364: 
 2365:     my $grp_view_permission =
 2366:         &Apache::lonnet::allowed('vcg',$env{'request.course.id'});
 2367:     my $grp_manage_permission =
 2368:         &Apache::lonnet::allowed('mdg',$env{'request.course.id'});
 2369: 
 2370: 
 2371:     if (! $grp_view_permission && ! $grp_manage_permission && 
 2372:                                   ! $view_permission && ! $enrl_permission) {
 2373:         $env{'user.error.msg'}=
 2374:      "/adm/coursegroups:cst:0:0:Cannot manage or view course groups, ".
 2375:                                  "or drop or add students";
 2376:         return HTTP_NOT_ACCEPTABLE;
 2377:     }
 2378: 
 2379:     #
 2380:     # Only output the header information if they did not request csv format
 2381:     #
 2382:     # Start page
 2383:     &Apache::loncommon::content_type($r,'text/html');
 2384:     $r->send_http_header;
 2385:     $r->print(&header());
 2386:     #
 2387:     # Main switch on form.action and form.state, as appropriate
 2388:     if (! exists($env{'form.action'})) {
 2389:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2390:                   (undef,'Enrollment Manager'));
 2391:         &print_main_menu($r,$enrl_permission,$view_permission,$grp_manage_permission,
 2392:                          $grp_view_permission);
 2393:     } elsif ($env{'form.action'} eq 'upload' && $enrl_permission) {
 2394:         &Apache::lonhtmlcommon::add_breadcrumb
 2395:             ({href=>'/adm/dropadd?action=upload&state=',
 2396:               text=>"Upload Classlist"});
 2397:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2398:                   (undef,'Upload Classlist','Course_Create_Class_List'));
 2399:         if (! exists($env{'form.state'})) {
 2400:             &print_first_courselist_upload_form($r);            
 2401:         } elsif ($env{'form.state'} eq 'got_file') {
 2402:             &print_upload_manager_form($r);
 2403:         } elsif ($env{'form.state'} eq 'enrolling') {
 2404:             if ($env{'form.datatoken'}) {
 2405:                 &upfile_drop_add($r);
 2406:             } else {
 2407:                 # Hmmm, this is an error
 2408:             }
 2409:         } else {
 2410:             &print_first_courselist_upload_form($r);            
 2411:         }
 2412:     } elsif ($env{'form.action'} eq 'drop' && $enrl_permission) {
 2413:         &Apache::lonhtmlcommon::add_breadcrumb
 2414:             ({href=>'/adm/dropadd?action=drop',
 2415:               text=>"Drop Students"});
 2416:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2417:                   (undef,'Drop Students','Course_Drop_Student'));
 2418:         if (! exists($env{'form.state'})) {
 2419:             &print_drop_menu($r);
 2420:         } elsif ($env{'form.state'} eq 'done') {
 2421:             &drop_student_list($r);
 2422:         } else {
 2423:             &print_drop_menu($r);
 2424:         }
 2425:     } elsif ($env{'form.action'} eq 'enrollstudent' && $enrl_permission) {
 2426:         &Apache::lonhtmlcommon::add_breadcrumb
 2427:             ({href=>'/adm/dropadd?action=enrollstudent',
 2428:               text=>"Enroll Student"});
 2429:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2430:                   (undef,'Enroll Student','Course_Add_Student'));
 2431:         if (! exists($env{'form.state'})) {
 2432:             &get_student_username_domain_form($r);
 2433:         } elsif ($env{'form.state'} eq 'gotusername') {
 2434:             &print_enroll_single_student_form($r);
 2435:         } elsif ($env{'form.state'} eq 'enrolling') {
 2436:             &enroll_single_student($r);
 2437:         } else {
 2438:             &get_student_username_domain_form($r);
 2439:         }
 2440:     } elsif ($env{'form.action'} eq 'classlist' && $view_permission) {
 2441:         &Apache::lonhtmlcommon::add_breadcrumb
 2442:             ({href=>'/adm/dropadd?action=classlist',
 2443:               text=>"View Classlist"});
 2444:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2445:                   (undef,'View Classlist','Course_View_Class_List'));
 2446:         if (! exists($env{'form.state'})) {
 2447:             &print_html_classlist($r,undef);
 2448:         } elsif ($env{'form.state'} eq 'csv') {
 2449:             &print_html_classlist($r,'csv');
 2450:         } elsif ($env{'form.state'} eq 'excel') {
 2451:             &print_html_classlist($r,'excel');
 2452:         } else {
 2453:             &print_html_classlist($r,undef);
 2454:         }
 2455:     } elsif ($env{'form.action'} eq 'modifystudent' && $enrl_permission) {
 2456:         &Apache::lonhtmlcommon::add_breadcrumb
 2457:             ({href=>'/adm/dropadd?action=modifystudent',
 2458:               text=>"Modify Student Data"});
 2459:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2460:                   (undef,'Modify Student Data','Course_Modify_Student_Data'));
 2461:         if (! exists($env{'form.state'})) {
 2462:             &print_html_classlist($r);
 2463:         } elsif ($env{'form.state'} eq 'selected') {
 2464:             &print_modify_student_form($r);
 2465:         } elsif ($env{'form.state'} eq 'done') {
 2466:             &modify_single_student($r);
 2467:         } else {
 2468:             &print_html_classlist($r);
 2469:         }        
 2470:     } else {
 2471:         # We should not end up here, but I guess it is possible
 2472:         &Apache::lonnet::logthis("Undetermined state in londropadd.pm.  ".
 2473:                                  "form.action = ".$env{'form.action'}.
 2474:                                  "Someone should fix this.");
 2475:         $r->print(&Apache::lonhtmlcommon::breadcrumbs
 2476:                   (undef,'Enrollment Manager'));
 2477:         &print_main_menu($r,$enrl_permission,$view_permission);
 2478:     }
 2479:     #
 2480:     # Finish up
 2481:     $r->print('</form>'.&Apache::loncommon::end_page());
 2482:     return OK;
 2483: }
 2484: 
 2485: ###################################################################
 2486: ###################################################################
 2487: 
 2488: 1;
 2489: __END__
 2490: 
 2491: 

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