File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.27: download - view: text, annotated - select for diffs
Wed Apr 17 13:55:35 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Fixed bug in modifystudent introduced in last commit, changed return value
for get_current_classlist so that we detect when the classlist is empty
instead of assuming it is an error.

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

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