File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.55: download - view: text, annotated - select for diffs
Thu Sep 26 14:04:34 2002 UTC (21 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Bug 674 - remind the course coordinator the role will not become active
until the student next logs in to LON-CAPA.
Fixed idiocy in &handler regarding subroutine to call in bad 'drop' state.
It is always helpful to call subroutines that exist.

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

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