File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.30: download - view: text, annotated - select for diffs
Thu Apr 25 19:21:34 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Remove debugging flag.

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # $Id: londropadd.pm,v 1.30 2002/04/25 19:21:34 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 (current.radiovalue == null || current.radiovalue == 'nochange') {
  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 (current.argfield == null || current.argfield == '') {
  176:         var alertmsg = '';
  177:         switch (current.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:     # Set up domain selection form
  386:     my $domform = &Apache::loncommon::select_dom_form($defdom,'lcdomain');    
  387:     $r->print(<<ENDPICK);
  388: </table>
  389: <input type=hidden name=nfields value=$i>
  390: <input type=hidden name=keyfields value="$keyfields">
  391: <h3>Login Type</h3>
  392: <p>Note: this will not take effect if the user already exists</p>
  393: <p>
  394: $krbform
  395: </p>
  396: <p>
  397: $intform
  398: </p>
  399: <p>
  400: $locform
  401: </p>
  402: <h3>LON-CAPA Domain for Students</h3>
  403: LON-CAPA domain: $domform <p>
  404: <h3>Starting and Ending Dates</h3>
  405: <input type="hidden" value=''          name="pres_value"  >
  406: <input type="hidden" value=''          name="pres_type"   >
  407: <input type="hidden" value=''          name="pres_marker" >
  408: <input type="hidden" value='$today'    name="startdate"   >
  409: <input type="hidden" value='$halfyear' name="enddate"     >
  410: <a 
  411:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  412: >Set Starting Date</a><p>
  413: 
  414: <a 
  415:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  416: >Set Ending Date</a><p>
  417: <h3>Full Update</h3>
  418: <input type=checkbox name=fullup value=yes> Full update 
  419: (also print list of users not enrolled anymore)<p>
  420: <h3>ID/Student Number</h3>
  421: <input type=checkbox name=forceid value=yes> 
  422: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  423: (only do if you know what you are doing)<p>
  424: <input type="button" onClick="verify(this.form)" value="Update Courselist"><br>
  425: Note: for large courses, this operation might be time consuming.
  426: ENDPICK
  427: }
  428: 
  429: # ======================================================= Menu Phase Two Upload
  430: sub menu_phase_two_upload {
  431:     my $r=shift;
  432: 
  433:     my $datatoken;
  434:     if (!$ENV{'form.datatoken'}) {
  435:       $datatoken=&Apache::loncommon::upfile_store($r);
  436:     } else {
  437:       $datatoken=$ENV{'form.datatoken'};
  438:       &Apache::loncommon::load_tmp_file($r);
  439:     }
  440:     my @records=&Apache::loncommon::upfile_record_sep();
  441:     my $total=$#records;
  442:     my $distotal=$total+1;
  443:     $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  444:     my $krbdefdom=$1;
  445:     $krbdefdom=~tr/a-z/A-Z/;
  446:     my $today=time;
  447:     my $halfyear=$today+15552000;
  448:     my $defdom=$r->dir_config('lonDefDomain');
  449:     &phase_two_header($r,$datatoken,$distotal,$krbdefdom);
  450:     my $i;
  451:     my $keyfields;
  452:     if ($total>=0) {
  453: 	my @d=(['username','Username'],['names','Last Name, First Names'],
  454: 	       ['fname','First Name'],['mname','Middle Names/Initials'],
  455: 	       ['lname','Last Name'],['gen','Generation'],
  456: 	       ['id','ID/Student Number'],['sec','Group/Section'],
  457: 	       ['ipwd','Initial Password']);
  458: 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
  459: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  460: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
  461: 	    foreach (@d) { $keyfields.=$_->[0].','; }
  462: 	    chop($keyfields);
  463: 	} else {
  464: 	    unshift(@d,['none','']);
  465: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,\@d);
  466: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
  467: 	    $keyfields=join(',',sort(keys(%sone)));
  468: 	}
  469:     }
  470:     &phase_two_end($r,$i,$keyfields,$defdom,$today,$halfyear);
  471: }
  472: 
  473: # ======================================================= Enroll single student
  474: sub enroll_single_student {
  475:     my $r=shift;
  476:     $r->print('<h3>Enrolling Student</h3>');
  477:     $r->print($ENV{'form.cuname'}." in domain ".$ENV{'form.cdomain'}.": ");
  478:     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
  479:         ($ENV{'form.cdomain'})&&($ENV{'form.cdomain'}!~/\W/)) {
  480: 	my $amode='';
  481:         my $genpwd='';
  482:         if ($ENV{'form.login'} eq 'krb') {
  483:            $amode='krb4';
  484:            $genpwd=$ENV{'form.krbarg'};
  485:         } elsif ($ENV{'form.login'} eq 'int') {
  486:            $amode='internal';
  487:            $genpwd=$ENV{'form.intarg'};
  488:         }  elsif ($ENV{'form.login'} eq 'loc') {
  489: 	    $amode='localauth';
  490: 	    $genpwd=$ENV{'form.locarg'};
  491: 	    if (!$genpwd) { $genpwd=" "; }
  492: 	}
  493:         if (($amode) && ($genpwd)) {
  494:             &modifystudent($ENV{'form.cdomain'},$ENV{'form.cuname'},
  495:                            $ENV{'request.course.id'},$ENV{'form.csec'});
  496:           $r->print(&Apache::lonnet::modifystudent(
  497:                       $ENV{'form.cdomain'},$ENV{'form.cuname'},
  498:                       $ENV{'form.cstid'},$amode,$genpwd,
  499:  	              $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
  500:                       $ENV{'form.clast'},$ENV{'form.cgen'},
  501:                       $ENV{'form.csec'},$ENV{'form.enddate'},
  502:                       $ENV{'form.startdate'},$ENV{'form.forceid'}));
  503: 	} else {
  504:            $r->print('Invalid login mode or password');    
  505:         }          
  506:     } else {
  507:         $r->print('Invalid username or domain');
  508:     }    
  509: }
  510: 
  511: # ======================================================= Menu Phase Two Enroll
  512: sub menu_phase_two_enroll {
  513:     my $r=shift;
  514:     my ($krbdefdom) = $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  515:     $krbdefdom=~tr/a-z/A-Z/;
  516:     my $today    = time;
  517:     my $halfyear = $today+15552000;
  518:     my $defdom=$r->dir_config('lonDefDomain');
  519:     my $javascript_validations=&javascript_validations($krbdefdom);
  520:     # Set up authentication forms
  521:     my %param = ( formname => 'document.studentform');
  522:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
  523:     my $intform = &Apache::loncommon::authform_internal(%param);
  524:     my $locform = &Apache::loncommon::authform_local(%param);
  525:     # Set up domain selection form
  526:     my $domform = &Apache::loncommon::select_dom_form($defdom,'cdomain');    
  527:     # Print it all out
  528:     $r->print(<<ENDSENROLL);
  529: <script type="text/javascript" language="Javascript">
  530: function verify(vf) {
  531:     var founduname=0;
  532:     var foundpwd=0;
  533:     var foundname=0;
  534:     var foundid=0;
  535:     var foundsec=0;
  536:     var tw;
  537:     if ((typeof(vf.cuname.value) !="undefined") && (vf.cuname.value!='') && 
  538: 	(typeof(vf.cdomain.value)!="undefined") && (vf.cdomain.value!='')) {
  539:         founduname=1;
  540:     }
  541:     if ((typeof(vf.cfirst.value)!="undefined") && (vf.cfirst.value!='') &&
  542: 	(typeof(vf.clast.value) !="undefined") && (vf.clast.value!='')) {
  543:         foundname=1;
  544:     }
  545:     if ((typeof(vf.csec.value)!="undefined") && (vf.csec.value!='')) {
  546:         foundsec=1;
  547:     }
  548:     if ((typeof(vf.cstid.value)!="undefined") && (vf.cstid.value!='')) {
  549: 	foundid=1;
  550:     }
  551:     if (founduname==0) {
  552: 	alert('You need to specify at least the username and domain fields');
  553:         return;
  554:     }
  555:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
  556: }
  557: 
  558: $javascript_validations
  559: 
  560: function clearpwd(vf) {
  561:     //nothing else needs clearing
  562: }
  563: 
  564: </script>
  565: <h3>Personal Data</h3>
  566: First Name:  <input type="text" name="cfirst"  size="15"><br>
  567: Middle Name: <input type="text" name="cmiddle" size="15"><br>
  568: Last Name:   <input type="text" name="clast"   size="15"><br>
  569: Generation:  <input type="text" name="cgen"    size="5"> 
  570: 
  571: <p>ID/Student Number: <input type="text" name="cstid" size="10"></p>
  572: 
  573: <p>Group/Section: <input type=text name=csec size=5></p>
  574: 
  575: <h3>Login Data</h3>
  576: <p>Username: <input type="text" name="cuname"  size="15"></p>
  577: <p>Domain:   $domform</p>
  578: <p>Note: login settings below  will not take effect if the user already exists
  579: </p><p>
  580: $krbform
  581: </p><p>
  582: $intform
  583: </p><p>
  584: $locform
  585: </p><p>
  586: <h3>Starting and Ending Dates</h3>
  587: <input type="hidden" value='' name="pres_value">
  588: <input type="hidden" value='' name="pres_type">
  589: <input type="hidden" value='' name="pres_marker">
  590: <input type="hidden" value='$today' name=startdate>
  591: <input type="hidden" value='$halfyear' name=enddate>
  592: </p><p>
  593: <a 
  594:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  595: >Set Starting Date</a>
  596: </p><p>
  597: <a 
  598:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  599: >Set Ending Date</a>
  600: </p><p>
  601: <h3>ID/Student Number</h3>
  602: <input type="checkbox" name="forceid" value="yes"> 
  603: Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
  604: (only do if you know what you are doing)<p>
  605: <input type="button" onClick="verify(this.form)" value="Enroll as student"><br>
  606: <input type="hidden" name="phase" value="five">
  607: </p>
  608: ENDSENROLL
  609: }
  610: 
  611: # =================================================== get the current classlist
  612: sub get_current_classlist {
  613:     my ($domain,$identifier) = @_;
  614:     # domain is the domain the class is being run in
  615:     # identifier is the internal, unique identifier for the class.
  616:     my %currentlist=();
  617:     my $now=time;
  618:     my %results=&Apache::lonnet::dump('classlist',$domain,$identifier);
  619:     my ($tmp) = keys(%results);
  620:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  621:         foreach my $student (keys(%results)) {
  622:             # Extract the start and end dates
  623:             my ($end,$start)=split(/\:/,$results{$student});
  624:             # If the class isn't over, put it in the list
  625:             unless (($end) && ($now>$end)) { 
  626:                 $currentlist{$student}=1;
  627:             }
  628:         }
  629:         return (undef,%currentlist);
  630:     } else {
  631:         return ($tmp,undef);
  632:     }
  633: }
  634: 
  635: # ========================================================= Menu Phase Two Drop
  636: sub menu_phase_two_drop {
  637:     my $r=shift;
  638:     my $cid=$ENV{'request.course.id'};
  639:     my ($error,%currentlist)=&get_current_classlist($ENV{'course.'.$cid.'.domain'},
  640:                                            $ENV{'course.'.$cid.'.num'});
  641:     if (defined($error)) {
  642:         $r->print('<pre>ERROR:$error</pre>');
  643:     }
  644:     if (!defined(%currentlist)) { 
  645:         $r->print("There are no students currently enrolled.\n");
  646:     } else {
  647:         # Print out the available choices
  648:         &show_drop_list($r,%currentlist);
  649:     }
  650: }
  651: 
  652: # =================================================== Show student list to drop
  653: sub show_drop_list {
  654:     my ($r,%currentlist)=@_;
  655:     my $cid=$ENV{'request.course.id'};
  656:     $r->print(<<'END');
  657: <input type="hidden" name="phase" value="four">
  658: <table border=2>
  659: <tr><th>&nbsp;</th><th>username</th><th>domain</th>
  660: <th>ID</th><th>student name</th><th>generation</th>
  661: <th>section</th></tr>
  662: END
  663:     foreach (sort keys %currentlist) {
  664:         my ($sname,$sdom)=split(/\:/,$_);
  665:         my %reply=&Apache::lonnet::idrget($sdom,$sname);
  666:         my $ssec=&Apache::lonnet::usection($sdom,$sname,$cid);
  667:         my %info=&Apache::lonnet::get('environment',
  668:                                       ['firstname','middlename',
  669:                                        'lastname','generation'],
  670:                                       $sdom, $sname);
  671:         my ($tmp) = keys(%info);
  672:         if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
  673:             $r->print('<tr><td colspan="7"><font color="red">'.
  674:                       'Internal error: unable to get environment '.
  675:                       'for '.$sname.' in domain '.$sdom.'</font></td></tr>');
  676:         } else {
  677:             $r->print(<<"END");
  678: <tr>
  679:     <td><input type="checkbox" name="droplist" value="$_"></td>
  680:     <td>$sname</td>
  681:     <td>$sdom</td>
  682:     <td>$reply{$sname}</td>
  683:     <td>$info{'lastname'}, $info{'firstname'} $info{'middlename'}</td>
  684:     <td>$info{'generation'}</td>
  685:     <td>$ssec</td>
  686: </tr>
  687: END
  688:         }
  689:     }
  690:     $r->print('</table><br>');
  691:     $r->print('<input type=submit value="Drop Students">');
  692: }
  693: 
  694: # ================================================= Drop/Add from uploaded file
  695: sub upfile_drop_add {
  696:     my $r=shift;
  697:     &Apache::loncommon::load_tmp_file($r);
  698:     my @studentdata=&Apache::loncommon::upfile_record_sep();
  699:     my @keyfields = split(/\,/,$ENV{'form.keyfields'});
  700:     my $cid = $ENV{'request.course.id'};
  701:     my %fields=();
  702:     for (my $i=0; $i<=$ENV{'form.nfields'}; $i++) {
  703:         if ($ENV{'form.upfile_associate'} eq 'reverse') {
  704:             if ($ENV{'form.f'.$i} ne 'none') {
  705:                 $fields{$keyfields[$i]}=$ENV{'form.f'.$i};
  706:             }
  707:         } else {
  708:             $fields{$ENV{'form.f'.$i}}=$keyfields[$i];
  709:         }
  710:     }
  711:     #
  712:     my $startdate = $ENV{'form.startdate'};
  713:     my $enddate   = $ENV{'form.enddate'};
  714:     if ($startdate=~/\D/) { $startdate=''; }
  715:     if ($enddate=~/\D/)   { $enddate=''; }
  716:     #
  717:     my $domain=$ENV{'form.lcdomain'};
  718:     # Determine authentication mechanism
  719:     my $amode  = '';
  720:     my $genpwd = '';
  721:     if ($ENV{'form.login'} eq 'krb') {
  722:         $amode='krb4';
  723:         $genpwd=$ENV{'form.krbarg'};
  724:     } elsif ($ENV{'form.login'} eq 'int') {
  725:         $amode='internal';
  726:         if ((defined($ENV{'form.intarg'})) && ($ENV{'form.intarg'})) {
  727:             $genpwd=$ENV{'form.intarg'};
  728:         }
  729:     } elsif ($ENV{'form.login'} eq 'loc') {
  730:         $amode='localauth';
  731:         if ((defined($ENV{'form.locarg'})) && ($ENV{'form.locarg'})) {
  732:             $genpwd=$ENV{'form.locarg'};
  733:         }
  734:     }
  735:     unless (($domain=~/\W/) || ($amode eq '')) {
  736:         #######################################
  737:         ##         Enroll Students           ##
  738:         #######################################
  739:         $r->print('<h3>Enrolling Students</h3>');
  740:         my $count=0;
  741:         my $flushc=0;
  742:         my %student=();
  743:         # Get new classlist
  744:         foreach (@studentdata) {
  745:             my %entries=&Apache::loncommon::record_sep($_);
  746:             # Determine student name
  747:             unless (($entries{$fields{'username'}} eq '') ||
  748:                     (!defined($entries{$fields{'username'}}))) {
  749:                 my ($fname, $mname, $lname,$gen) = ('','','','');
  750:                 if (defined($fields{'names'})) {
  751:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
  752:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
  753:                 } else {
  754:                     if (defined($fields{'fname'})) {
  755:                         $fname=$entries{$fields{'fname'}};
  756:                     }
  757:                     if (defined($fields{'mname'})) {
  758:                         $mname=$entries{$fields{'mname'}};
  759:                     }
  760:                     if (defined($fields{'lname'})) {
  761:                         $lname=$entries{$fields{'lname'}};
  762:                     }
  763:                     if (defined($fields{'gen'})) {
  764:                         $gen=$entries{$fields{'gen'}};
  765:                     }
  766:                 }
  767:                 if ($entries{$fields{'username'}}=~/\W/) {
  768:                     $r->print('<p><b>Unacceptable username: '.
  769:                               $entries{$fields{'username'}}.' for user '.
  770:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
  771:                 } else {
  772:                     # determine section number
  773:                     my $sec='';
  774:                     my $username=$entries{$fields{'username'}};
  775:                     if (defined($fields{'sec'})) {
  776:                         if (defined($entries{$fields{'sec'}})) {
  777:                             $sec=$entries{$fields{'sec'}};
  778:                         }
  779:                     }
  780:                     # determine student id number
  781:                     my $id='';
  782:                     if (defined($fields{'id'})) {
  783:                         if (defined($entries{$fields{'id'}})) {
  784:                             $id=$entries{$fields{'id'}};
  785:                         }
  786:                         $id=~tr/A-Z/a-z/;
  787:                     }
  788:                     # determine student password
  789:                     my $password='';
  790:                     if ($genpwd) { 
  791:                         $password=$genpwd; 
  792:                     } else {
  793:                         if (defined($fields{'ipwd'})) {
  794:                             if ($entries{$fields{'ipwd'}}) {
  795:                                 $password=$entries{$fields{'ipwd'}};
  796:                             }
  797:                         }
  798:                     }
  799:                     if ($password) {
  800:                         &modifystudent($domain,$username,$cid,$sec);
  801:                         my $reply=&Apache::lonnet::modifystudent
  802:                             ($domain,$username,$id,$amode,$password,
  803:                              $fname,$mname,$lname,$gen,$sec,$enddate,
  804:                              $startdate,$ENV{'form.forceid'});
  805:                         if ($reply ne 'ok') {
  806:                             $r->print('<p><b>'.
  807:                                       'Error enrolling '.$username.': '.
  808:                                       $reply.'</b></p>');
  809:          		} else {
  810:                             $count++; $flushc++;
  811:                             $student{$username}=1;
  812:                             $r->print('. ');
  813:                             if ($flushc>15) {
  814: 				$r->rflush;
  815:                                 $flushc=0;
  816:                             }
  817:                         }
  818:                     } else {
  819:                         $r->print("<p><b>No password for $username</b><p>");
  820:                     }
  821:                 }
  822:             }
  823:         } # end of foreach (@studentdata)
  824:         $r->print('<p>Processed Students: '.$count);
  825:         #####################################
  826:         #           Drop students           #
  827:         #####################################
  828:         if ($ENV{'form.fullup'} eq 'yes') {
  829:             $r->print('<h3>Dropping Students</h3>');
  830:             #  Get current classlist
  831:             my ($error,%currentlist)=&get_current_classlist
  832:                 ($ENV{'course.'.$cid.'.domain'},
  833:                  $ENV{'course.'.$cid.'.num'});
  834:             if (defined($error)) {
  835:                 $r->print('<pre>ERROR:$error</pre>');
  836:             }
  837:             if (defined(%currentlist)) {
  838:                 # Drop the students
  839:                 foreach (@studentdata) {
  840:                     my %entries=&Apache::loncommon::record_sep($_);
  841:                     unless (($entries{$fields{'username'}} eq '') ||
  842:                             (!defined($entries{$fields{'username'}}))) {
  843:                         delete($currentlist{$entries{$fields{'username'}}.
  844:                                                 ':'.$domain});
  845:                     }
  846:                 }
  847:                 # Print out list of dropped students
  848:                 &show_drop_list($r,%currentlist);
  849:             } else {
  850:                 $r->print("There are no students currently enrolled.\n");
  851:             }
  852:         }
  853:     } # end of unless
  854: }
  855: 
  856: # ================================================================== Phase four
  857: sub drop_student_list {
  858:     my $r=shift;
  859:     my $count=0;
  860:     foreach (@{$ENV{'form.droplist'}}) {
  861:         my ($uname,$udom)=split(/\:/,$_);
  862:         &modifystudent($udom,$uname,$ENV{'request.course.id'});
  863:         $r->print('Dropped '.$uname.' at '.$udom.'<br>');
  864:         $count++;
  865:     }
  866:     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
  867:     $r->print('<p>Re-enrollment will re-activate data.');
  868: }
  869: 
  870: # ================================================================ Main Handler
  871: sub handler {
  872:     my $r=shift;
  873:     if ($r->header_only) {
  874:         $r->content_type('text/html');
  875:         $r->send_http_header;
  876:         return OK;
  877:     }
  878:     #  Needs to be in a course
  879:     if (($ENV{'request.course.fn'}) && 
  880:         (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'}))) {
  881:         # Start page
  882:         $r->content_type('text/html');
  883:         $r->send_http_header;
  884:         $r->print(&header());
  885:         # Phase one, initial screen
  886:         unless ($ENV{'form.phase'}) {
  887:             &menu_phase_one($r);
  888:         }
  889:         # Phase two
  890:         if ($ENV{'form.associate'} eq 'Reverse Association') {
  891:             $ENV{'form.phase'} = 'two';
  892:             if ( $ENV{'form.upfile_associate'} ne 'reverse' ) {
  893:                 $ENV{'form.upfile_associate'} = 'reverse';
  894:             } else {
  895:                 $ENV{'form.upfile_associate'} = 'forward';
  896:             }
  897:         }
  898:         if ($ENV{'form.phase'} eq 'two') {
  899:             if ($ENV{'form.fileupload'}) {
  900:                 &menu_phase_two_upload($r);
  901:             } elsif ($ENV{'form.enroll'}) {
  902:                 &menu_phase_two_enroll($r);
  903:             } elsif ($ENV{'form.drop'}) {
  904:                 &menu_phase_two_drop($r);
  905:             }
  906:         }
  907:         # Phase three
  908:         if ($ENV{'form.phase'} eq 'three') {
  909:             if ($ENV{'form.datatoken'}) {
  910:                 &upfile_drop_add($r);
  911:             }
  912:         }
  913:         # Phase four
  914:         if ($ENV{'form.phase'} eq 'four') {
  915:             &drop_student_list($r);
  916:         }
  917:         # Phase five
  918:         if ($ENV{'form.phase'} eq 'five') {
  919:             &enroll_single_student($r);
  920:         }
  921:          # End
  922:         $r->print('</form></body></html>');
  923:     } else {
  924:         # Not in a course, or not allowed to modify parms
  925:         $ENV{'user.error.msg'}=
  926:             "/adm/dropadd:cst:0:0:Cannot drop or add students";
  927:         return HTTP_NOT_ACCEPTABLE; 
  928:     }
  929:     return OK;
  930: }
  931: 
  932: 1;
  933: __END__
  934: 

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