File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.173: download - view: text, annotated - select for diffs
Mon Oct 22 22:16:38 2007 UTC (16 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
Bug 2593 csv file upload by DC to change passwords
- DCs can change authentication, add a domain or course role for uploaded users, and/or change user information (names, ID, e-mail address etc.)
- Authors can add co-authors
- Course Coordinators can add course roles.
- Some subroutines in londropdd.pm moved to lonuserutils.pm.  Some routines in loncreateuser.pm moved to lonuserutils.pm
- &process_pick() moved from lonroles.pm to lonpickcourse.pm
- Work in progress.  Ultimate goal is to provide user listing functionality in different contexts and eliminate separate ENRL button.

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

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