File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.32: download - view: text, annotated - select for diffs
Fri Apr 26 19:10:54 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added 'check all' and 'uncheck all' to drop screen.

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

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