File:  [LON-CAPA] / loncom / interface / Attic / londropadd.pm
Revision 1.9: download - view: text, annotated - select for diffs
Mon Jan 15 14:51:18 2001 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Mac and PC linebreak, formfeed, etc, compatible (hopefully)

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

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