File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.34: download - view: text, annotated - select for diffs
Tue Apr 30 13:51:00 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Better reports to user if things go wrong.  No longer need to enter password
for a user who already exists.

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

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