File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.6: download - view: text, annotated - select for diffs
Thu Dec 28 19:56:31 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Seems to do the enrollment correct

    1: # The LearningOnline Network with CAPA
    2: # Handler to drop and add students in courses 
    3: #
    4: # (Handler to set parameters for assessments
    5: #
    6: # (Handler to resolve ambiguous file locations
    7: #
    8: # (TeX Content Handler
    9: #
   10: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   11: #
   12: # 10/11,10/12,10/16 Gerd Kortemeyer)
   13: #
   14: # 11/20,11/21,11/22,11/23,11/24,11/25,11/27,11/28,
   15: # 12/08,12/12 Gerd Kortemeyer)
   16: #
   17: # 12/26,12/27,12/28 Gerd Kortemeyer
   18: 
   19: package Apache::londropadd;
   20: 
   21: use strict;
   22: use Apache::lonnet;
   23: use Apache::Constants qw(:common :http REDIRECT);
   24: 
   25: 
   26: # ================================================================ Main Handler
   27: 
   28: sub handler {
   29:    my $r=shift;
   30: 
   31:    if ($r->header_only) {
   32:       $r->content_type('text/html');
   33:       $r->send_http_header;
   34:       return OK;
   35:    }
   36: 
   37: # ----------------------------------------------------- Needs to be in a course
   38: 
   39:    if (($ENV{'request.course.fn'}) && 
   40:        (&Apache::lonnet::allowed('cst',$ENV{'request.course.id'}))) {
   41: 
   42: # ------------------------------------------------------------------ Start page
   43:       $r->content_type('text/html');
   44:       $r->send_http_header;
   45:       $r->print(<<ENDHEAD);
   46: <html>
   47: <head>
   48: <title>LON-CAPA Student Drop/Add</title>
   49: </head>
   50: <body bgcolor="#FFFFFF">
   51: <img align=right src=/adm/lonIcons/lonlogos.gif>
   52: <h1>Drop/Add Students</h1>
   53: <form method="post" enctype="multipart/form-data"
   54: action="/adm/dropadd" name="studentform">
   55: <h2>Course: $ENV{'course.'.$ENV{'request.course.id'}.'.description'}</h2>
   56: ENDHEAD
   57: # --------------------------------------------------- Phase one, initial screen
   58:     unless ($ENV{'form.phase'}) {
   59: 	$r->print(<<ENDUPFORM);
   60: <input type=hidden name=phase value=two>
   61: <hr>
   62: <h3>Upload a courselist</h3>
   63: <input type=file name=upfile size=50>
   64: <br>Type: <select name=upfiletype>
   65: <option value=csv>CSV (comma separated values, spreadsheet)</option>
   66: <option value=space>Space separated</option>
   67: <option value=tab>Tabulator separated</option>
   68: <option value=xml>HTML/XML</option>
   69: </select>
   70: <p><input type=submit name=fileupload value="Upload Courselist">
   71: <hr>
   72: <h3>Enroll a single student</h3>
   73: <p><input type=submit name=enroll value="Enroll Student">
   74: <hr>
   75: <h3>Drop a student</h3>
   76: <p><input type=submit name=drop value="Drop Student">
   77: ENDUPFORM
   78:       }
   79: # ------------------------------------------------------------------- Phase two
   80:       if ($ENV{'form.phase'} eq 'two') {
   81: 	  if ($ENV{'form.fileupload'}) {
   82: 	      my $datatoken=$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
   83: 		  '_enroll_'.$ENV{'request.course.id'}.'_'.time.'_'.$$;
   84: 	    {
   85:                 my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
   86:                                          '/tmp/'.$datatoken.'.tmp');
   87: 		print $fh $ENV{'form.upfile'};
   88: 	    }
   89:              my $separator='';
   90:              my $remove='';
   91:              if ($ENV{'form.upfiletype'} eq 'csv') {
   92: 		 $separator='\"\,\s*\"';
   93:                  $remove='"';
   94:              } elsif ($ENV{'form.upfiletype'} eq 'space') {
   95:                  $separator='\s+';
   96:              } elsif ($ENV{'form.upfiletype'} eq 'tab') {
   97:                  $separator='\t+';
   98:              } elsif ($ENV{'form.upfiletype'} eq 'xml') {
   99:              }
  100:              my @lines=split(/\n/,$ENV{'form.upfile'});
  101:              my $total=$#lines;
  102:              $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
  103: 	     my $krbdefdom=$1;
  104:              $krbdefdom=~tr/a-z/A-Z/;
  105:              my $today=time;
  106:              my $halfyear=$today+15552000;
  107:              my $defdom=$r->dir_config('lonDefDomain');
  108: 	     $r->print(<<ENDPICK);
  109: <input type=hidden name=phase value=three>
  110: <input type=hidden name=datatoken value="$datatoken">
  111: <input type=hidden name=upfiletype value=$ENV{'form.upfiletype'}>
  112: <hr>
  113: <h3>Identify fields</h3>
  114: Total number of records found in file: $total
  115: <script>
  116: function verify(vf) {
  117:     var founduname=0;
  118:     var foundpwd=0;
  119:     var foundname=0;
  120:     var foundid=0;
  121:     var foundsec=0;
  122:     var foundatype=0;
  123:     var tw;
  124:     var message='';
  125:     for (i=0;i<=vf.nfields.value;i++) {
  126:         tw=eval('vf.f'+i+'.selectedIndex');
  127:         if (tw==1) { founduname=1; }
  128:         if ((tw>=2) && (tw<=6)) { foundname=1; }
  129:         if (tw==7) { foundid=1; }
  130:         if (tw==8) { foundsec=1; } 
  131:         if (tw==9) { foundpwd=1; }
  132:     }
  133:     if (founduname==0) {
  134: 	alert('You need to specify at least the username field');
  135:         return;
  136:     }
  137:     if (vf.login[0].checked) {
  138: 	foundatype=1;
  139:         if (vf.krbdom.value=='') {
  140: 	    alert('You need to specify the Kerberos domain');
  141:             return;
  142:         }
  143:     }
  144:     if (vf.login[1].checked) {
  145: 	foundatype=1;
  146:         if ((vf.intpwd.value=='') && (foundpwd==0)) {
  147: 	    alert('You need to specify the initial password');
  148:             return;
  149:         }
  150:     }
  151:     if (foundatype==0) {
  152: 	alert('You need to set the login type');
  153:         return;
  154:     }
  155:     if (foundname==0) { message='No name fields specified. '; }
  156:     if (foundid==0) { message+='No ID or student number field specified. '; }
  157:     if (foundsec==0) { message+='No section or group field specified. '; }
  158:     if (vf.startdate.value=='') {
  159: 	message+='No starting date set. ';
  160:     }
  161:     if (vf.enddate.value=='') {
  162:         message+='No ending date set. ';
  163:     }
  164:     if ((vf.enddate.value!='') && (vf.startdate.value!='')) {
  165:        if (vf.enddate.value<vf.startdate.value) {
  166:           alert('Ending date is before starting date');
  167:           return;
  168:        }
  169:     }
  170:     if (message!='') {
  171:        message+='Continue enrollment?';
  172:        if (confirm(message)) {
  173: 	  pclose();
  174: 	  vf.submit();
  175:        }
  176:     } else {
  177:       pclose();
  178:       vf.submit();
  179:     }   
  180: }
  181: 
  182: function flip(vf,tf) {
  183:    var nw=eval('vf.f'+tf+'.selectedIndex');
  184:    var i;
  185:    for (i=0;i<=vf.nfields.value;i++) {
  186:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
  187:           eval('vf.f'+i+'.selectedIndex=0;')      
  188:       } 
  189:    }
  190:    if (nw==2) {
  191:       for (i=0;i<=vf.nfields.value;i++) {
  192:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
  193:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
  194:              eval('vf.f'+i+'.selectedIndex=0;')
  195:          }
  196:       }      
  197:    }
  198:    if ((nw>=3) && (nw<=6)) {
  199:       for (i=0;i<=vf.nfields.value;i++) {
  200:          if (eval('vf.f'+i+'.selectedIndex')==2) {
  201:              eval('vf.f'+i+'.selectedIndex=0;')
  202:          }
  203:       }      
  204:    }
  205:    if (nw==9) {
  206:        vf.login[1].checked=true;
  207:        vf.intpwd.value='';
  208:        vf.krbdom.value='';
  209:    }
  210: 
  211: }
  212: 
  213: function clearpwd(vf) {
  214:     var i;
  215:     for (i=0;i<=vf.nfields.value;i++) {
  216:         if (eval('vf.f'+i+'.selectedIndex')==9) {
  217:             eval('vf.f'+i+'.selectedIndex=0;')
  218:         }
  219:     }      
  220: }
  221: 
  222: function setkrb(vf) {
  223:     if (vf.krbdom.value!='') {
  224:        clearpwd(vf);
  225:        vf.login[0].checked=true;
  226:        vf.krbdom.value=vf.krbdom.value.toUpperCase();
  227:        vf.intpwd.value='';
  228:    }
  229: }
  230: 
  231: function setint(vf) {
  232:     if (vf.intpwd.value!='') {
  233:        clearpwd(vf);
  234:        vf.login[1].checked=true;
  235:        vf.krbdom.value='';
  236:    }
  237: }
  238: 
  239: function clickkrb(vf) {
  240:     vf.krbdom.value='$krbdefdom';
  241:     clearpwd(vf);
  242:     vf.intpwd.value='';
  243: }
  244: 
  245: function clickint(vf) {
  246:     vf.krbdom.value='';
  247: }
  248: 
  249:     function pclose() {
  250:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
  251:                  "height=350,width=350,scrollbars=no,menubar=no");
  252:         parmwin.close();
  253:     }
  254: 
  255:     function pjump(type,dis,value,marker,ret,call) {
  256:         parmwin=window.open("/adm/rat/parameter.html?type="+escape(type)
  257:                  +"&value="+escape(value)+"&marker="+escape(marker)
  258:                  +"&return="+escape(ret)
  259:                  +"&call="+escape(call)+"&name="+escape(dis),"LONCAPAparms",
  260:                  "height=350,width=350,scrollbars=no,menubar=no");
  261: 
  262:     }
  263: 
  264:     function dateset() {
  265:         if (document.studentform.pres_marker.value=='end') {
  266:            document.studentform.enddate.value=
  267: 	       document.studentform.pres_value.value;
  268:         }
  269:         if (document.studentform.pres_marker.value=='start') {
  270:            document.studentform.startdate.value=
  271: 	       document.studentform.pres_value.value;
  272:         }
  273:         pclose();
  274:     }
  275: 
  276: </script>
  277: <table border=2><tr><th>Field</th><th>Samples</th></tr>
  278: ENDPICK
  279:              my @sone; my @stwo; my @sthree; my $nfields=0;
  280:              if ($#lines>=0) {
  281: 		$lines[0]=~s/^$remove//;
  282:                 $lines[0]=~s/$remove$//;
  283:                 @sone=split(/$separator/,$lines[0]);
  284:                 $nfields=$#sone;
  285:                 if ($#lines>=1) {
  286:         	   $lines[1]=~s/^$remove//;
  287:                    $lines[1]=~s/$remove$//;
  288:                    @stwo=split(/$separator/,$lines[1]);
  289:                    $nfields=$#stwo;
  290: 	        }
  291:                 if ($#lines>=2) {
  292: 		   $lines[2]=~s/^$remove//;
  293:                    $lines[2]=~s/$remove$//;
  294:                    @sthree=split(/$separator/,$lines[2]);
  295:                    $nfields=$#sthree;
  296: 	        }
  297:                 my $i;
  298:                 for ($i=0;$i<=$nfields;$i++) {
  299:                    $r->print('<tr><td><select name=f'.$i.
  300:                        ' onChange="flip(this.form,'.$i.');">');
  301:                    map {
  302:                       my ($value,$display)=split(/\:/,$_);
  303:                       $r->print('<option value='.$value.'>'.$display.
  304:                               '</option>');
  305:                    } ('none: ','username:Username',
  306:                       'names:Last Name, First Names',
  307:                       'fname:First Name','mname:Middle Names/Initials',
  308:                       'lname:Last Name','gen:Generation',
  309:                       'id:ID/Student Number','sec:Group/Section',
  310:                       'ipwd:Initial Password');
  311:                    $r->print('</select></td><td>');
  312:                    if (defined($sone[$i])) { 
  313:                       $r->print($sone[$i]."</br>\n"); 
  314:                    }
  315: 	           if (defined($stwo[$i])) { 
  316:                       $r->print($stwo[$i]."</br>\n"); 
  317:                    }
  318: 	           if (defined($sthree[$i])) { 
  319:                       $r->print($sthree[$i]."</br>\n"); 
  320:                    }
  321:                    $r->print('</td></tr>');
  322: 	       }
  323: 	     }
  324:              $r->print(<<ENDPICK);
  325: </table>
  326: <input type=hidden name=nfields value=$nfields>
  327: <h3>Login Type</h3>
  328: <input type=radio name=login value=krb onClick="clickkrb(this.form);">
  329: Kerberos authenticated with domain
  330: <input type=text size=10 name=krbdom onChange="setkrb(this.form);"><p>
  331: <input type=radio name=login value=int onClick="clickint(this.form);"> 
  332: Internally authenticated (with initial password 
  333: <input type=text size=10 name=intpwd onChange="setint(this.form);">)
  334: <h3>LON-CAPA Domain for Students</h3>
  335: LON-CAPA domain: <input type=text size=10 value=$defdom name=lcdomain><p>
  336: <h3>Starting and Ending Dates</h3>
  337: <input type="hidden" value='' name="pres_value">
  338: <input type="hidden" value='' name="pres_type">
  339: <input type="hidden" value='' name="pres_marker">
  340: <input type="hidden" value='$today' name=startdate>
  341: <input type="hidden" value='$halfyear' name=enddate>
  342: <a 
  343:  href="javascript:pjump('date_start','Enrollment Starting Date',document.studentform.startdate.value,'start','studentform.pres','dateset');"
  344: >Set Starting Date</a><p>
  345: 
  346: <a 
  347:  href="javascript:pjump('date_end','Enrollment Ending Date',document.studentform.enddate.value,'end','studentform.pres','dateset');"
  348: >Set Ending Date</a><p>
  349: <h3>Full Update</h3>
  350: <input type=checkbox name=fullup value=yes> Full update 
  351: (also dropping students)<p>
  352: <input type=button onClick="verify(this.form)" value="Update Courselist"><br>
  353: Note: for large courses, this operation might be time consuming.
  354: ENDPICK
  355:          } elsif ($ENV{'form.enroll'}) {
  356:          } elsif ($ENV{'form.drop'}) {
  357:          }
  358:       }
  359: # ----------------------------------------------------------------- Phase three
  360:       if ($ENV{'form.phase'} eq 'three') {
  361: 	  if ($ENV{'form.datatoken'}) {
  362:              my $separator='';
  363:              my $remove='';
  364:              if ($ENV{'form.upfiletype'} eq 'csv') {
  365: 		 $separator='\"\,\s*\"';
  366:                  $remove='"';
  367:              } elsif ($ENV{'form.upfiletype'} eq 'space') {
  368:                  $separator='\s+';
  369:              } elsif ($ENV{'form.upfiletype'} eq 'tab') {
  370:                  $separator='\t+';
  371:              } elsif ($ENV{'form.upfiletype'} eq 'xml') {
  372:              }
  373:              my %fields=();
  374:              for (my $i=0;$i<=$ENV{'form.nfields'};$i++) {
  375:                  $fields{$ENV{'form.f'.$i}}=$i;
  376:              }
  377:              my $startdate=$ENV{'form.startdate'};
  378:              my $enddate=$ENV{'form.enddate'};
  379:              if ($startdate=~/\D/) { $startdate=''; }
  380:              if ($enddate=~/\D/) { $enddate=''; }
  381:              my $domain=$ENV{'form.lcdomain'};
  382:              my $amode='';
  383:              my $genpwd='';
  384:              if ($ENV{'form.login'} eq 'krb') {
  385:                  $amode='krb4';
  386:                  $genpwd=$ENV{'form.krbdom'};
  387:              } elsif ($ENV{'form.login'} eq 'int') {
  388:                  $amode='internal';
  389:                  if ((defined($ENV{'form.intpwd'})) && ($ENV{'form.intpwd'})) {
  390: 		     $genpwd=$ENV{'form.intpwd'};
  391:                  }
  392:              }
  393:              unless (($domain=~/\W/) || ($amode eq '')) {
  394:               $r->print('<h3>Enrolling Students</h3>');
  395:               my $count=1;
  396: # ----------------------------------------------------------- Get new classlist
  397:               my @studentdata=();
  398:              {
  399:                 my $fh;
  400:                 if ($fh=Apache::File->new($r->dir_config('lonDaemons').
  401: 		              '/tmp/'.$ENV{'form.datatoken'}.'.tmp')) {
  402: 		    @studentdata=<$fh>;
  403:                 }
  404: 	     }
  405: # --------------------------------------------------------- Enroll new students
  406: 	      map {
  407:                 my $line=$_;
  408:                 chomp($line);
  409:                 $line=~s/^$remove//;
  410:                 $line=~s/$remove$//;
  411: 	        my @entries=split(/$separator/,$line);
  412:                 unless (($entries[$fields{'username'}] eq '') ||
  413:                         (!defined($entries[$fields{'username'}]))) {
  414:                   my $fname=''; my $mname=''; my $lname=''; my $gen='';
  415:                   if (defined($fields{'names'})) {
  416: 		      ($lname,$fname,$mname)=
  417: 	            ($entries[$fields{'names'}]=~/([^\,]+)\,\s*(\w+)\s*(.*)$/);
  418:                   } else {
  419:                       if (defined($fields{'fname'})) {
  420: 		         $fname=$entries[$fields{'fname'}];
  421: 		      }
  422:                       if (defined($fields{'mname'})) {
  423: 		         $mname=$entries[$fields{'mname'}];
  424: 		      }
  425:                       if (defined($fields{'lname'})) {
  426: 		         $lname=$entries[$fields{'lname'}];
  427: 		      }
  428:                       if (defined($fields{'gen'})) {
  429: 		         $gen=$entries[$fields{'gen'}];
  430: 		      }
  431:                   }
  432:                   if ($entries[$fields{'username'}]=~/\W/) {
  433:                      $r->print('<p><b>Unacceptable username: '.
  434:                               $entries[$fields{'username'}].' for user '.
  435:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
  436: 		  } else {
  437:                       my $sec='';
  438:                       my $username=$entries[$fields{'username'}];
  439:                       if (defined($fields{'sec'})) {
  440:                          if (defined($entries[$fields{'sec'}])) {
  441: 			     $sec=$entries[$fields{'sec'}];
  442:                          }
  443:                       }
  444:                       my $id='';
  445:                       if (defined($fields{'id'})) {
  446:                          if (defined($entries[$fields{'id'}])) {
  447: 			     $id=$entries[$fields{'id'}];
  448:                          }
  449:                          $id=~tr/A-Z/a-z/;
  450:                       }
  451:                       my $password='';
  452:                       if ($genpwd) { 
  453:                          $password=$genpwd; 
  454: 		      } else {
  455:                          if (defined($fields{'ipwd'})) {
  456: 			     if ($entries[$fields{'ipwd'}]) {
  457: 				 $password=$entries[$fields{'ipwd'}];
  458:                              }
  459:                          }
  460:                       }
  461:                       if ($password) { 
  462:                          my $reply=&Apache::lonnet::modifystudent(
  463:                           $domain,$username,$id,$amode,$password,
  464: 			   $fname,$mname,$lname,$gen,$sec,$enddate,$startdate);
  465:                          unless ($reply eq 'ok') {
  466:                             $r->print(
  467:                              "<p><b>Error enrolling $username: $reply</b><p>");
  468: 			} else {
  469:                             $r->print('. ');
  470:                         }
  471:                      } else {
  472:                             $r->print(
  473:                              "<p><b>No password for $username</b><p>");
  474:                      }
  475:                   }
  476:                  }                 
  477:               } @studentdata;
  478: # --------------------------------------------------------------- Drop students
  479:               if ($ENV{'form.fullup'} eq 'yes') {
  480: 		 $r->print('<h3>Dropping Students</h3>');
  481: # ------------------------------------------------------- Get current classlist
  482:                  my $cid=$ENV{'request.course.id'};
  483:                  my $classlst=&Apache::lonnet::reply
  484:                  ('dump:'.$ENV{'course.'.$cid.'.domain'}.':'.
  485: 	                  $ENV{'course.'.$cid.'.num'}.':classlist',
  486: 	                  $ENV{'course.'.$cid.'.home'});
  487:                  my %currentlist=();
  488:                  my $now=time;
  489:                  unless ($classlst=~/^error\:/) {
  490: 	         } else {
  491:                      $r->print(
  492:                   '<font color=red><h3>Could not access classlist: '.$classlst.
  493:                   '</h3></font>');
  494:                  }
  495: 	     }
  496: # ------------------------------------------------------------------------ Done
  497:           }
  498:         }
  499:       }
  500: # ------------------------------------------------------------------------- End
  501:       $r->print('</form></body></html>');
  502:    } else {
  503: # ----------------------------- Not in a course, or not allowed to modify parms
  504:       $ENV{'user.error.msg'}=
  505:         "/adm/dropadd:cst:0:0:Cannot drop or add students";
  506:       return HTTP_NOT_ACCEPTABLE; 
  507:    }
  508:    return OK;
  509: }
  510: 
  511: 1;
  512: __END__
  513: 
  514: 
  515: 
  516: 

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