File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.28: download - view: text, annotated - select for diffs
Mon Apr 22 15:26:46 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
New javascript to handle authentication forms.

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

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