File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.39: download - view: text, annotated - select for diffs
Fri May 3 17:45:07 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fixes BUG 330.  If the enrollment database file does not exist we take this
to mean the course does not have any students enrolled.

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

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