File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.35: download - view: text, annotated - select for diffs
Tue Apr 30 15:24:16 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fixed bug which prevented dropping of single students.
Fixed bug which prevented dropping of students whose section begins with
whitespace.

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # $Id: londropadd.pm,v 1.35 2002/04/30 15:24:16 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: # YEAR=2000
   35: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   36: #
   37: # 10/11,10/12,10/16 Gerd Kortemeyer)
   38: #
   39: # 11/20,11/21,11/22,11/23,11/24,11/25,11/27,11/28,
   40: # 12/08,12/12 Gerd Kortemeyer)
   41: #
   42: # 12/26,12/27,12/28,
   43: # YEAR=2001
   44: # 01/01/01,01/15,02/10,02/13,02/14,02/22 Gerd Kortemeyer
   45: # 8/6 Scott Harrison
   46: # Guy Albertelli
   47: # 9/25 Gerd Kortemeyer
   48: # 12/19 Guy Albertelli
   49: # YEAR=2002
   50: # 1/4 Gerd Kortemeyer
   51: 
   52: package Apache::londropadd;
   53: 
   54: use strict;
   55: use Apache::lonnet();
   56: use Apache::loncommon();
   57: use Apache::Constants qw(:common :http REDIRECT);
   58: 
   59: # ================================================================ Print header
   60: 
   61: sub header {
   62:     return(<<ENDHEAD);
   63: <html>
   64: <head>
   65: <title>LON-CAPA Student Drop/Add</title>
   66: </head>
   67: <body bgcolor="#FFFFFF">
   68: <img align=right src=/adm/lonIcons/lonlogos.gif>
   69: <h1>Drop/Add Students</h1>
   70: <form method="post" enctype="multipart/form-data"
   71: action="/adm/dropadd" name="studentform">
   72: <h2>Course: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}</h2>
   73: ENDHEAD
   74: }
   75: 
   76: # =========== Drop student from all sections of a course, except optional $csec
   77: sub modifystudent {
   78:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
   79:     # if $csec is undefined, drop the student from all the courses matching
   80:     # this one.  If $csec is defined, drop them from all other sections of 
   81:     # this course and add them to section $csec
   82:     $courseid=~s/\_/\//g;
   83:     $courseid=~s/^(\w)/\/$1/;
   84:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
   85:     my ($tmp) = keys(%roles);
   86:     # Bail out if we were unable to get the students roles
   87:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
   88:     # Go through the roles looking for enrollment in this course
   89:     my $result = '';
   90:     foreach my $course (keys(%roles)) {
   91:         if ($course=~/^$courseid(?:\/)*(?:\s+)*(\w+)*\_st$/) {
   92:             # We are in this course
   93:             my $section=$1;
   94:             $section='' if ($course eq $courseid.'_st');
   95:             if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
   96:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
   97:                 my $now=time;
   98:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
   99:                     my $reply=&Apache::lonnet::modifystudent
  100:                         ($udom,$unam,'','','','','','','',
  101:                          $section,time,undef,undef,$desiredhost);
  102:                     $result .= $reply.':';
  103:                 }
  104:             }
  105:         }
  106:     }
  107:     if ($result eq '') {
  108:         $result eq 'Unable to find section for this student';
  109:     } elsif ($result =~ /^(ok:)+$/) {
  110:         $result eq 'ok';
  111:     }
  112:     return $result;
  113: }
  114: 
  115: # ============ build a domain and server selection form
  116: sub domain_form {
  117:     my ($defdom) = @_;
  118:     # Set up domain and server selection forms
  119:     #
  120:     # Get the domains
  121:     my @domains = &Apache::loncommon::get_domains();
  122:     # build up the menu information to be passed to 
  123:     # &Apache::loncommon::linked_select_forms
  124:     my %select_menus;
  125:     foreach my $dom (@domains) {
  126:         # set up the text for this domain
  127:         $select_menus{$dom}->{'text'}= $dom;
  128:         # we want a choice of 'default' as the default in the second menu
  129:         $select_menus{$dom}->{'default'}= 'default';
  130:         $select_menus{$dom}->{'select2'}->{'default'} = 'default';
  131:         # Now build up the other items in the second menu
  132:         my %servers = &Apache::loncommon::get_home_servers($dom);
  133:         foreach my $server (keys(%servers)) {
  134:             $select_menus{$dom}->{'select2'}->{$server} 
  135:                                             = "$server $servers{$server}";
  136:         }
  137:     }
  138:     my $result  = &Apache::loncommon::linked_select_forms
  139:         ('studentform',' with home server ',$defdom,
  140:          'lcdomain','lcserver',\%select_menus);
  141:     return $result;
  142: }
  143: 
  144: # ============================================================== Menu Phase One
  145: sub menu_phase_one {
  146:     my $r=shift;
  147:     my $upfile_select=&Apache::loncommon::upfile_select_html();
  148:     $r->print(<<ENDUPFORM);
  149: <input type=hidden name=phase value=two>
  150: <hr>
  151: <h3>Upload a courselist</h3>
  152: $upfile_select
  153: <p><input type=submit name=fileupload value="Upload Courselist">
  154: <hr>
  155: <h3>Enroll a single student</h3>
  156: <p><input type=submit name=enroll value="Enroll Student">
  157: <hr>
  158: <h3>Drop students</h3>
  159: <p><input type=submit name=drop value="Selection List">
  160: ENDUPFORM
  161: }
  162: 
  163: sub phase_two_header {
  164:     my ($r,$datatoken,$distotal,$krbdefdom)=@_;
  165:     my $javascript;
  166:     if ($ENV{'form.upfile_associate'} eq 'reverse') {
  167: 	$javascript=&phase_two_javascript_reverse_associate();
  168:     } else {
  169: 	$javascript=&phase_two_javascript_forward_associate();
  170:     }
  171:     my $javascript_validations=&javascript_validations($krbdefdom);
  172:     $r->print(<<ENDPICK);
  173: <hr>
  174: <h3>Identify fields</h3>
  175: Total number of records found in file: $distotal <hr />
  176: Enter as many fields as you can. The system will inform you and bring you back
  177: to this page if the data selected is insufficient to run your class.<hr />
  178: <input type="submit" name="associate"  value="Reverse Association" />
  179: <input type="hidden" name="phase"      value="three" />
  180: <input type="hidden" name="datatoken"  value="$datatoken" />
  181: <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
  182: <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
  183: <input type="hidden" name="upfile_associate" 
  184:                                        value="$ENV{'form.upfile_associate'}" />
  185: <hr />
  186: <script type="text/javascript" language="Javascript">
  187: $javascript
  188: $javascript_validations
  189: </script>
  190: ENDPICK
  191: }
  192: 
  193: sub javascript_validations {
  194:     my ($krbdefdom)=@_;
  195:     my %param = ( formname => 'studentform',
  196:                   kerb_def_dom => $krbdefdom );
  197:     my $authheader = &Apache::loncommon::authform_header(%param);
  198:     return (<<ENDPICK);
  199: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
  200:     var foundatype=0;
  201:     var message='';
  202:     if (founduname==0) {
  203: 	alert('You need to specify the username field');
  204:         return;
  205:     }
  206:     if (current.radiovalue == null || current.radiovalue == 'nochange') {
  207:         // They did not check any of the login radiobuttons.
  208:         alert('You must choose an authentication type');
  209:         return;
  210:     }
  211:     foundatype=1;
  212:     if (current.argfield == null || current.argfield == '') {
  213:         var alertmsg = '';
  214:         switch (current.value) {
  215:             case 'krb': 
  216:                 alertmsg = 'You need to specify the Kerberos domain';
  217:                 break;
  218:             case 'loc':
  219:             case 'fsys':
  220:                 alertmsg = 'You need to specify the initial password';
  221:                 break;
  222:             case 'fsys':
  223:                 alertmsg = '';
  224:                 break;
  225:             default: 
  226:                 alertmsg = '';
  227:         }
  228:         if (alertmsg != '') {
  229:             alert(alertmsg);
  230:             return;
  231:         }
  232:     }
  233: 
  234:     if (foundname==0) { message='No name fields specified. '; }
  235:     if (foundid==0) { message+='No ID or student number field specified. '; }
  236:     if (foundsec==0) { message+='No section or group field specified. '; }
  237:     if (vf.startdate.value=='') {
  238: 	message+='No starting date set. ';
  239:     }
  240:     if (vf.enddate.value=='') {
  241:         message+='No ending date set. ';
  242:     }
  243:     if ((vf.enddate.value!='') && (vf.startdate.value!='')) {
  244:        if (Math.round(vf.enddate.value)<Math.round(vf.startdate.value)) {
  245:           alert('Ending date is before starting date');
  246:           return;
  247:        }
  248:     }
  249:     if (message!='') {
  250:        message+='Continue enrollment?';
  251:        if (confirm(message)) {
  252: 	  pclose();
  253: 	  vf.submit();
  254:        }
  255:     } else {
  256:       pclose();
  257:       vf.submit();
  258:     }
  259: }
  260: 
  261: 
  262:     function pclose() {
  263:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  264:                  "height=350,width=350,scrollbars=no,menubar=no");
  265:         parmwin.close();
  266:     }
  267: 
  268:     function pjump(type,dis,value,marker,ret,call) {
  269:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  270:                  +"&value="+escape(value)+"&marker="+escape(marker)
  271:                  +"&return="+escape(ret)
  272:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
  273:                  "height=350,width=350,scrollbars=no,menubar=no");
  274: 
  275:     }
  276: 
  277:     function dateset() {
  278:         if (document.studentform.pres_marker.value=='end') {
  279:            document.studentform.enddate.value=
  280: 	       document.studentform.pres_value.value;
  281:         }
  282:         if (document.studentform.pres_marker.value=='start') {
  283:            document.studentform.startdate.value=
  284: 	       document.studentform.pres_value.value;
  285:         }
  286:         pclose();
  287:     }
  288: 
  289: $authheader
  290: ENDPICK
  291: 
  292: }
  293: 
  294: sub phase_two_javascript_forward_associate {
  295:     return(<<ENDPICK);
  296: function verify(vf) {
  297:     var founduname=0;
  298:     var foundpwd=0;
  299:     var foundname=0;
  300:     var foundid=0;
  301:     var foundsec=0;
  302:     var tw;
  303:     for (i=0;i<=vf.nfields.value;i++) {
  304:         tw=eval('vf.f'+i+'.selectedIndex');
  305:         if (tw==1) { founduname=1; }
  306:         if ((tw>=2) && (tw<=6)) { foundname=1; }
  307:         if (tw==7) { foundid=1; }
  308:         if (tw==8) { foundsec=1; }
  309:         if (tw==9) { foundpwd=1; }
  310:     }
  311:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  312: }
  313: 
  314: function flip(vf,tf) {
  315:    var nw=eval('vf.f'+tf+'.selectedIndex');
  316:    var i;
  317:    for (i=0;i<=vf.nfields.value;i++) {
  318:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
  319:           eval('vf.f'+i+'.selectedIndex=0;')
  320:       }
  321:    }
  322:    if (tf==1 && nw!=0) {
  323:       for (i=2;i<=5;i++) {
  324:          eval('vf.f'+i+'.selectedIndex=0;')
  325:       }
  326:    }
  327:    if (nw==2) {
  328:       for (i=0;i<=vf.nfields.value;i++) {
  329:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
  330:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
  331:              eval('vf.f'+i+'.selectedIndex=0;')
  332:          }
  333:       }
  334:    }
  335:    if ((nw>=3) && (nw<=6)) {
  336:       for (i=0;i<=vf.nfields.value;i++) {
  337:          if (eval('vf.f'+i+'.selectedIndex')==2) {
  338:              eval('vf.f'+i+'.selectedIndex=0;')
  339:          }
  340:       }
  341:    }
  342:    if (nw==9) {
  343:        changed_radio('int',document.studentform);
  344:        set_auth_radio_buttons('int',document.studentform);
  345:        vf.intarg.value='';
  346:        vf.krbarg.value='';
  347:        vf.locarg.value='';
  348:    }
  349: }
  350: 
  351: function clearpwd(vf) {
  352:     var i;
  353:     for (i=0;i<=vf.nfields.value;i++) {
  354:         if (eval('vf.f'+i+'.selectedIndex')==9) {
  355:             eval('vf.f'+i+'.selectedIndex=0;')
  356:         }
  357:     }
  358: }
  359: 
  360: ENDPICK
  361: }
  362: 
  363: sub phase_two_javascript_reverse_associate {
  364:     return(<<ENDPICK);
  365: function verify(vf) {
  366:     var founduname=0;
  367:     var foundpwd=0;
  368:     var foundname=0;
  369:     var foundid=0;
  370:     var foundsec=0;
  371:     var tw;
  372:     for (i=0;i<=vf.nfields.value;i++) {
  373:         tw=eval('vf.f'+i+'.selectedIndex');
  374:         if (i==0 && tw!=0) { founduname=1; }
  375:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
  376:         if (i==6 && tw!=0) { foundid=1; }
  377:         if (i==7 && tw!=0) { foundsec=1; }
  378:         if (i==8 && tw!=0) { foundpwd=1; }
  379:     }
  380:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  381: }
  382: 
  383: function flip(vf,tf) {
  384:    var nw=eval('vf.f'+tf+'.selectedIndex');
  385:    var i;
  386:    // picked the all one one name field, reset the other name ones to blank
  387:    if (tf==1 && nw!=0) {
  388:       for (i=2;i<=5;i++) {
  389:          eval('vf.f'+i+'.selectedIndex=0;')
  390:       }
  391:    }
  392:    //picked one of the piecewise name fields, reset the all in
  393:    //one field to blank
  394:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
  395:       eval('vf.f1.selectedIndex=0;')
  396:    }
  397:    // intial password specified, pick internal authentication
  398:    if (tf==8 && nw!=0) {
  399:        changed_radio('int',document.studentform);
  400:        set_auth_radio_buttons('int',document.studentform);
  401:        vf.krbarg.value='';
  402:        vf.intarg.value='';
  403:        vf.locarg.value='';
  404:    }
  405: }
  406: 
  407: function clearpwd(vf) {
  408:     var i;
  409:     if (eval('vf.f8.selectedIndex')!=0) {
  410:         eval('vf.f8.selectedIndex=0;')
  411:     }
  412: }
  413: ENDPICK
  414: }
  415: 
  416: sub phase_two_end {
  417:     my ($r,$i,$keyfields,$defdom,$today,$halfyear)=@_;
  418:     my %param = ( formname => 'document.studentform');
  419:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  420:     my $intform = &Apache::loncommon::authform_internal(%param);
  421:     my $locform = &Apache::loncommon::authform_local(%param);
  422:     my $domform = &domain_form($defdom);
  423:     $r->print(<<ENDPICK);
  424: </table>
  425: <input type=hidden name=nfields value=$i>
  426: <input type=hidden name=keyfields value="$keyfields">
  427: <h3>Login Type</h3>
  428: <p>Note: this will not take effect if the user already exists</p>
  429: <p>
  430: $krbform
  431: </p>
  432: <p>
  433: $intform
  434: </p>
  435: <p>
  436: $locform
  437: </p>
  438: <h3>LON-CAPA Domain for Students</h3>
  439: LON-CAPA domain: $domform <p>
  440: <h3>Starting and Ending Dates</h3>
  441: <input type="hidden" value=''          name="pres_value"  >
  442: <input type="hidden" value=''          name="pres_type"   >
  443: <input type="hidden" value=''          name="pres_marker" >
  444: <input type="hidden" value='$today'    name="startdate"   >
  445: <input type="hidden" value='$halfyear' name="enddate"     >
  446: <a 
  447:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  448: >Set Starting Date</a><p>
  449: 
  450: <a 
  451:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  452: >Set Ending Date</a><p>
  453: <h3>Full Update</h3>
  454: <input type=checkbox name=fullup value=yes> Full update 
  455: (also print list of users not enrolled anymore)<p>
  456: <h3>ID/Student Number</h3>
  457: <input type=checkbox name=forceid value=yes> 
  458: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  459: (only do if you know what you are doing)<p>
  460: <input type="button" onClick="verify(this.form)" value="Update Courselist"><br>
  461: Note: for large courses, this operation might be time consuming.
  462: ENDPICK
  463: }
  464: 
  465: # ======================================================= Menu Phase Two Upload
  466: sub menu_phase_two_upload {
  467:     my $r=shift;
  468: 
  469:     my $datatoken;
  470:     if (!$ENV{'form.datatoken'}) {
  471:       $datatoken=&Apache::loncommon::upfile_store($r);
  472:     } else {
  473:       $datatoken=$ENV{'form.datatoken'};
  474:       &Apache::loncommon::load_tmp_file($r);
  475:     }
  476:     my @records=&Apache::loncommon::upfile_record_sep();
  477:     my $total=$#records;
  478:     my $distotal=$total+1;
  479:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  480:     my $krbdefdom=$1;
  481:     $krbdefdom=~tr/a-z/A-Z/;
  482:     my $today=time;
  483:     my $halfyear=$today+15552000;
  484:     my $defdom=$r->dir_config('lonDefDomain');
  485:     &phase_two_header($r,$datatoken,$distotal,$krbdefdom);
  486:     my $i;
  487:     my $keyfields;
  488:     if ($total>=0) {
  489: 	my @d=(['username','Username'],['names','Last Name, First Names'],
  490: 	       ['fname','First Name'],['mname','Middle Names/Initials'],
  491: 	       ['lname','Last Name'],['gen','Generation'],
  492: 	       ['id','ID/Student Number'],['sec','Group/Section'],
  493: 	       ['ipwd','Initial Password']);
  494: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
  495: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  496: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
  497: 	    foreach (@d) { $keyfields.=$_->[0].','; }
  498: 	    chop($keyfields);
  499: 	} else {
  500: 	    unshift(@d,['none','']);
  501: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
  502: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
  503: 	    $keyfields=join(',',sort(keys(%sone)));
  504: 	}
  505:     }
  506:     &phase_two_end($r,$i,$keyfields,$defdom,$today,$halfyear);
  507: }
  508: 
  509: # ======================================================= Enroll single student
  510: sub enroll_single_student {
  511:     my $r=shift;
  512:     $r->print('<h3>Enrolling Student</h3>');
  513:     $r->print('<p>Enrolling '.$ENV{'form.cuname'}." in domain ".
  514:               $ENV{'form.lcdomain'}.'</p>');
  515:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
  516:         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
  517:         # Deal with home server selection
  518:         my $domain=$ENV{'form.lcdomain'};
  519:         my $desiredhost = $ENV{'form.lcserver'};
  520:         if (lc($desiredhost) eq 'default') {
  521:             $desiredhost = undef;
  522:         } else {
  523:             my %home_servers = &Apache::loncommon::get_home_servers($domain);
  524:             if (! exists($home_servers{$desiredhost})) {
  525:                 $r->print('<font color="#ff0000">Error:</font>'.
  526:                           'Invalid home server specified');
  527:                 return;
  528:             }
  529:         }
  530:         $r->print(" with server $desiredhost :") if (defined($desiredhost));
  531:         # End of home server selection logic
  532: 	my $amode='';
  533:         my $genpwd='';
  534:         if ($ENV{'form.login'} eq 'krb') {
  535:            $amode='krb4';
  536:            $genpwd=$ENV{'form.krbarg'};
  537:         } elsif ($ENV{'form.login'} eq 'int') {
  538:            $amode='internal';
  539:            $genpwd=$ENV{'form.intarg'};
  540:         }  elsif ($ENV{'form.login'} eq 'loc') {
  541: 	    $amode='localauth';
  542: 	    $genpwd=$ENV{'form.locarg'};
  543: 	    if (!$genpwd) { $genpwd=" "; }
  544: 	}
  545:         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
  546:                                                    $ENV{'form.lcdomain'});
  547:         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
  548:             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
  549:                            $ENV{'request.course.id'},$ENV{'form.csec'},
  550:                             $desiredhost);
  551:           $r->print(&Apache::lonnet::modifystudent(
  552:                       $ENV{'form.lcdomain'},$ENV{'form.cuname'},
  553:                       $ENV{'form.cstid'},$amode,$genpwd,
  554:  	              $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
  555:                       $ENV{'form.clast'},$ENV{'form.cgen'},
  556:                       $ENV{'form.csec'},$ENV{'form.enddate'},
  557:                       $ENV{'form.startdate'},$ENV{'form.forceid'},
  558:                     $desiredhost));
  559: 	} else {
  560:             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
  561:                       'Invalid login mode or password.  '.
  562:                       'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
  563:         }          
  564:     } else {
  565:         $r->print('Invalid username or domain');
  566:     }    
  567: }
  568: 
  569: # ======================================================= Menu Phase Two Enroll
  570: sub menu_phase_two_enroll {
  571:     my $r=shift;
  572:     my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  573:     $krbdefdom=~tr/a-z/A-Z/;
  574:     my $today    = time;
  575:     my $halfyear = $today+15552000;
  576:     my $defdom=$r->dir_config('lonDefDomain');
  577:     my $javascript_validations=&javascript_validations($krbdefdom);
  578:     # Set up authentication forms
  579:     my %param = ( formname => 'document.studentform');
  580:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  581:     my $intform = &Apache::loncommon::authform_internal(%param);
  582:     my $locform = &Apache::loncommon::authform_local(%param);
  583:     # Set up domain selection form
  584:     my $domform = &domain_form($defdom);
  585:     # Print it all out
  586:     $r->print(<<ENDSENROLL);
  587: <script type="text/javascript" language="Javascript">
  588: function verify(vf) {
  589:     var founduname=0;
  590:     var foundpwd=0;
  591:     var foundname=0;
  592:     var foundid=0;
  593:     var foundsec=0;
  594:     var tw;
  595:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
  596: 	(typeof(vf.lcdomain.value)!="undefined") && (vf.lcdomain.value!='')) {
  597:         founduname=1;
  598:     }
  599:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
  600: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
  601:         foundname=1;
  602:     }
  603:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
  604:         foundsec=1;
  605:     }
  606:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
  607: 	foundid=1;
  608:     }
  609:     if (founduname==0) {
  610: 	alert('You need to specify at least the username and domain fields');
  611:         return;
  612:     }
  613:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  614: }
  615: 
  616: $javascript_validations
  617: 
  618: function clearpwd(vf) {
  619:     //nothing else needs clearing
  620: }
  621: 
  622: </script>
  623: <h3>Personal Data</h3>
  624: First Name:  <input type="text" name="cfirst"  size="15"><br>
  625: Middle Name: <input type="text" name="cmiddle" size="15"><br>
  626: Last Name:   <input type="text" name="clast"   size="15"><br>
  627: Generation:  <input type="text" name="cgen"    size="5"> 
  628: 
  629: <p>ID/Student Number: <input type="text" name="cstid" size="10"></p>
  630: 
  631: <p>Group/Section: <input type=text name=csec size=5></p>
  632: 
  633: <h3>Login Data</h3>
  634: <p>Username: <input type="text" name="cuname"  size="15"></p>
  635: <p>Domain:   $domform</p>
  636: <p>Note: login settings below  will not take effect if the user already exists
  637: </p><p>
  638: $krbform
  639: </p><p>
  640: $intform
  641: </p><p>
  642: $locform
  643: </p><p>
  644: <h3>Starting and Ending Dates</h3>
  645: <input type="hidden" value='' name="pres_value">
  646: <input type="hidden" value='' name="pres_type">
  647: <input type="hidden" value='' name="pres_marker">
  648: <input type="hidden" value='$today' name=startdate>
  649: <input type="hidden" value='$halfyear' name=enddate>
  650: </p><p>
  651: <a 
  652:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  653: >Set Starting Date</a>
  654: </p><p>
  655: <a 
  656:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  657: >Set Ending Date</a>
  658: </p><p>
  659: <h3>ID/Student Number</h3>
  660: <input type="checkbox" name="forceid" value="yes"> 
  661: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  662: (only do if you know what you are doing)<p>
  663: <input type="button" onClick="verify(this.form)" value="Enroll as student"><br>
  664: <input type="hidden" name="phase" value="five">
  665: </p>
  666: ENDSENROLL
  667: }
  668: 
  669: # =================================================== get the current classlist
  670: sub get_current_classlist {
  671:     my ($domain,$identifier) = @_;
  672:     # domain is the domain the class is being run in
  673:     # identifier is the internal, unique identifier for the class.
  674:     my %currentlist=();
  675:     my $now=time;
  676:     my %results=&Apache::lonnet::dump('classlist',$domain,$identifier);
  677:     my ($tmp) = keys(%results);
  678:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  679:         foreach my $student (keys(%results)) {
  680:             # Extract the start and end dates
  681:             my ($end,$start)=split(/\:/,$results{$student});
  682:             # If the class isn't over, put it in the list
  683:             unless (($end) && ($now>$end)) { 
  684:                 $currentlist{$student}=1;
  685:             }
  686:         }
  687:         return (undef,%currentlist);
  688:     } else {
  689:         return ($tmp,undef);
  690:     }
  691: }
  692: 
  693: # ========================================================= Menu Phase Two Drop
  694: sub menu_phase_two_drop {
  695:     my $r=shift;
  696:     my $cid=$ENV{'request.course.id'};
  697:     my ($error,%currentlist)=&get_current_classlist($ENV{'course.'.$cid.'.domain'},
  698:                                            $ENV{'course.'.$cid.'.num'});
  699:     if (defined($error)) {
  700:         $r->print('<pre>ERROR:$error</pre>');
  701:     }
  702:     if (!defined(%currentlist)) { 
  703:         $r->print("There are no students currently enrolled.\n");
  704:     } else {
  705:         # Print out the available choices
  706:         &show_drop_list($r,%currentlist);
  707:     }
  708: }
  709: 
  710: # =================================================== Show student list to drop
  711: sub show_drop_list {
  712:     my ($r,%currentlist)=@_;
  713:     my $cid=$ENV{'request.course.id'};
  714:     $r->print(<<'END');
  715: <script>
  716: function checkAll(field)
  717: {
  718:     for (i = 0; i < field.length; i++)
  719:         field[i].checked = true ;
  720: }
  721: 
  722: function uncheckAll(field)
  723: {
  724:     for (i = 0; i < field.length; i++)
  725:         field[i].checked = false ;
  726: }
  727: </script>
  728: <p>
  729: <input type="hidden" name="phase" value="four">
  730: <table border=2>
  731: <tr><th>&nbsp;</th><th>username</th><th>domain</th>
  732: <th>ID</th><th>student name</th><th>generation</th>
  733: <th>section</th></tr>
  734: END
  735:     foreach (sort keys %currentlist) {
  736:         my ($sname,$sdom)=split(/\:/,$_);
  737:         my %reply=&Apache::lonnet::idrget($sdom,$sname);
  738:         my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
  739:         my %info=&Apache::lonnet::get('environment',
  740:                                       ['firstname','middlename',
  741:                                        'lastname','generation'],
  742:                                       $sdom, $sname);
  743:         my ($tmp) = keys(%info);
  744:         if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
  745:             $r->print('<tr><td colspan="7"><font color="red">'.
  746:                       'Internal error: unable to get environment '.
  747:                       'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
  748:         } else {
  749:             $r->print(<<"END");
  750: <tr>
  751:     <td><input type="checkbox" name="droplist" value="$_"></td>
  752:     <td>$sname</td>
  753:     <td>$sdom</td>
  754:     <td>$reply{$sname}</td>
  755:     <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
  756:     <td>$info{'generation'}</td>
  757:     <td>$ssec</td>
  758: </tr>
  759: END
  760:         }
  761:     }
  762:     $r->print('</table><br>');
  763:     $r->print(<<"END");
  764: </p><p>
  765: <input type="button" value="check all" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
  766: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.studentform.droplist)"> 
  767: <p><input type=submit value="Drop Students"></p>
  768: END
  769: }
  770: 
  771: # ================================================= Drop/Add from uploaded file
  772: sub upfile_drop_add {
  773:     my $r=shift;
  774:     &Apache::loncommon::load_tmp_file($r);
  775:     my @studentdata=&Apache::loncommon::upfile_record_sep();
  776:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
  777:     my $cid = $ENV{'request.course.id'};
  778:     my %fields=();
  779:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
  780:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
  781:             if ($ENV{'form.f'.$i} ne 'none') {
  782:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
  783:             }
  784:         } else {
  785:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
  786:         }
  787:     }
  788:     #
  789:     my $startdate = $ENV{'form.startdate'};
  790:     my $enddate   = $ENV{'form.enddate'};
  791:     if ($startdate=~/\D/) { $startdate=''; }
  792:     if ($enddate=~/\D/)   { $enddate=''; }
  793:     # Determine domain and desired host (home server)
  794:     my $domain=$ENV{'form.lcdomain'};
  795:     my $desiredhost = $ENV{'form.lcserver'};
  796:     if (lc($desiredhost) eq 'default') {
  797:         $desiredhost = undef;
  798:     } else {
  799:         my %home_servers = &Apache::loncommon::get_home_servers($domain);
  800:         if (! exists($home_servers{$desiredhost})) {
  801:             $r->print('<font color="#ff0000">Error:</font>'.
  802:                       'Invalid home server specified');
  803:             return;
  804:         }
  805:     }
  806:     # Determine authentication mechanism
  807:     my $amode  = '';
  808:     my $genpwd = '';
  809:     if ($ENV{'form.login'} eq 'krb') {
  810:         $amode='krb4';
  811:         $genpwd=$ENV{'form.krbarg'};
  812:     } elsif ($ENV{'form.login'} eq 'int') {
  813:         $amode='internal';
  814:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
  815:             $genpwd=$ENV{'form.intarg'};
  816:         }
  817:     } elsif ($ENV{'form.login'} eq 'loc') {
  818:         $amode='localauth';
  819:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
  820:             $genpwd=$ENV{'form.locarg'};
  821:         }
  822:     }
  823:     unless (($domain=~/\W/) || ($amode eq '')) {
  824:         #######################################
  825:         ##         Enroll Students           ##
  826:         #######################################
  827:         $r->print('<h3>Enrolling Students</h3>');
  828:         my $count=0;
  829:         my $flushc=0;
  830:         my %student=();
  831:         # Get new classlist
  832:         foreach (@studentdata) {
  833:             my %entries=&Apache::loncommon::record_sep($_);
  834:             # Determine student name
  835:             unless (($entries{$fields{'username'}} eq '') ||
  836:                     (!defined($entries{$fields{'username'}}))) {
  837:                 my ($fname, $mname, $lname,$gen) = ('','','','');
  838:                 if (defined($fields{'names'})) {
  839:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
  840:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
  841:                 } else {
  842:                     if (defined($fields{'fname'})) {
  843:                         $fname=$entries{$fields{'fname'}};
  844:                     }
  845:                     if (defined($fields{'mname'})) {
  846:                         $mname=$entries{$fields{'mname'}};
  847:                     }
  848:                     if (defined($fields{'lname'})) {
  849:                         $lname=$entries{$fields{'lname'}};
  850:                     }
  851:                     if (defined($fields{'gen'})) {
  852:                         $gen=$entries{$fields{'gen'}};
  853:                     }
  854:                 }
  855:                 if ($entries{$fields{'username'}}=~/\W/) {
  856:                     $r->print('<p><b>Unacceptable username: '.
  857:                               $entries{$fields{'username'}}.' for user '.
  858:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
  859:                 } else {
  860:                     # determine section number
  861:                     my $sec='';
  862:                     my $username=$entries{$fields{'username'}};
  863:                     if (defined($fields{'sec'})) {
  864:                         if (defined($entries{$fields{'sec'}})) {
  865:                             $sec=$entries{$fields{'sec'}};
  866:                         }
  867:                     }
  868:                     # determine student id number
  869:                     my $id='';
  870:                     if (defined($fields{'id'})) {
  871:                         if (defined($entries{$fields{'id'}})) {
  872:                             $id=$entries{$fields{'id'}};
  873:                         }
  874:                         $id=~tr/A-Z/a-z/;
  875:                     }
  876:                     # determine student password
  877:                     my $password='';
  878:                     if ($genpwd) { 
  879:                         $password=$genpwd; 
  880:                     } else {
  881:                         if (defined($fields{'ipwd'})) {
  882:                             if ($entries{$fields{'ipwd'}}) {
  883:                                 $password=$entries{$fields{'ipwd'}};
  884:                             }
  885:                         }
  886:                     }
  887:                     if ($password) {
  888:                         &modifystudent($domain,$username,$cid,$sec,
  889:                                        $desiredhost);
  890:                         my $reply=&Apache::lonnet::modifystudent
  891:                             ($domain,$username,$id,$amode,$password,
  892:                              $fname,$mname,$lname,$gen,$sec,$enddate,
  893:                              $startdate,$ENV{'form.forceid'},$desiredhost);
  894:                         if ($reply ne 'ok') {
  895:                             $r->print('<p><b>'.
  896:                                       'Error enrolling '.$username.': '.
  897:                                       $reply.'</b></p>');
  898:          		} else {
  899:                             $count++; $flushc++;
  900:                             $student{$username}=1;
  901:                             $r->print('. ');
  902:                             if ($flushc>15) {
  903: 				$r->rflush;
  904:                                 $flushc=0;
  905:                             }
  906:                         }
  907:                     } else {
  908:                         $r->print("<p><b>No password for $username</b><p>");
  909:                     }
  910:                 }
  911:             }
  912:         } # end of foreach (@studentdata)
  913:         $r->print('<p>Processed Students: '.$count);
  914:         #####################################
  915:         #           Drop students           #
  916:         #####################################
  917:         if ($ENV{'form.fullup'} eq 'yes') {
  918:             $r->print('<h3>Dropping Students</h3>');
  919:             #  Get current classlist
  920:             my ($error,%currentlist)=&get_current_classlist
  921:                 ($ENV{'course.'.$cid.'.domain'},
  922:                  $ENV{'course.'.$cid.'.num'});
  923:             if (defined($error)) {
  924:                 $r->print('<pre>ERROR:$error</pre>');
  925:             }
  926:             if (defined(%currentlist)) {
  927:                 # Drop the students
  928:                 foreach (@studentdata) {
  929:                     my %entries=&Apache::loncommon::record_sep($_);
  930:                     unless (($entries{$fields{'username'}} eq '') ||
  931:                             (!defined($entries{$fields{'username'}}))) {
  932:                         delete($currentlist{$entries{$fields{'username'}}.
  933:                                                 ':'.$domain});
  934:                     }
  935:                 }
  936:                 # Print out list of dropped students
  937:                 &show_drop_list($r,%currentlist);
  938:             } else {
  939:                 $r->print("There are no students currently enrolled.\n");
  940:             }
  941:         }
  942:     } # end of unless
  943: }
  944: 
  945: # ================================================================== Phase four
  946: sub drop_student_list {
  947:     my $r=shift;
  948:     my $count=0;
  949:     my @droplist;
  950:     if (ref($ENV{'form.droplist'})) {
  951:         @droplist = @{$ENV{'form.droplist'}};
  952:     } else {
  953:         @droplist = ($ENV{'form.droplist'});
  954:     }
  955:     foreach (@droplist) {
  956:         my ($uname,$udom)=split(/\:/,$_);
  957:         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
  958:         if ($result eq 'ok') {
  959:             $r->print('Dropped '.$uname.' at '.$udom.'<br>');
  960:         } else {
  961:             $r->print('Error dropping '.$uname.' at '.$udom.': '.$result.
  962:                       '<br />');
  963:         }
  964:         $count++;
  965:     }
  966:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
  967:     $r->print('<p>Re-enrollment will re-activate data.');
  968: }
  969: 
  970: # ================================================================ Main Handler
  971: sub handler {
  972:     my $r=shift;
  973:     if ($r->header_only) {
  974:         $r->content_type('text/html');
  975:         $r->send_http_header;
  976:         return OK;
  977:     }
  978:     #  Needs to be in a course
  979:     if (($ENV{'request.course.fn'}) && 
  980:         (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'}))) {
  981:         # Start page
  982:         $r->content_type('text/html');
  983:         $r->send_http_header;
  984:         $r->print(&header());
  985:         # Phase one, initial screen
  986:         unless ($ENV{'form.phase'}) {
  987:             &menu_phase_one($r);
  988:         }
  989:         # Phase two
  990:         if ($ENV{'form.associate'} eq 'Reverse Association') {
  991:             $ENV{'form.phase'} = 'two';
  992:             if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
  993:                 $ENV{'form.upfile_associate'} = 'reverse';
  994:             } else {
  995:                 $ENV{'form.upfile_associate'} = 'forward';
  996:             }
  997:         }
  998:         if ($ENV{'form.phase'} eq 'two') {
  999:             if ($ENV{'form.fileupload'}) {
 1000:                 &menu_phase_two_upload($r);
 1001:             } elsif ($ENV{'form.enroll'}) {
 1002:                 &menu_phase_two_enroll($r);
 1003:             } elsif ($ENV{'form.drop'}) {
 1004:                 &menu_phase_two_drop($r);
 1005:             }
 1006:         }
 1007:         # Phase three
 1008:         if ($ENV{'form.phase'} eq 'three') {
 1009:             if ($ENV{'form.datatoken'}) {
 1010:                 &upfile_drop_add($r);
 1011:             }
 1012:         }
 1013:         # Phase four
 1014:         if ($ENV{'form.phase'} eq 'four') {
 1015:             &drop_student_list($r);
 1016:         }
 1017:         # Phase five
 1018:         if ($ENV{'form.phase'} eq 'five') {
 1019:             &enroll_single_student($r);
 1020:         }
 1021:          # End
 1022:         $r->print('</form></body></html>');
 1023:     } else {
 1024:         # Not in a course, or not allowed to modify parms
 1025:         $ENV{'user.error.msg'}=
 1026:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
 1027:         return HTTP_NOT_ACCEPTABLE; 
 1028:     }
 1029:     return OK;
 1030: }
 1031: 
 1032: 1;
 1033: __END__
 1034: 

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