File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.40: download - view: text, annotated - select for diffs
Fri May 3 20:38:13 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: stable_2002_april, HEAD
Added course list viewing capability.  BUG 205.
Rewrote web page header information.

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

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