File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.131: download - view: text, annotated - select for diffs
Tue Nov 1 20:47:15 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <label>

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

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