Annotation of loncom/interface/londropadd.pm, revision 1.5

1.1       www         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: #
1.5     ! www        17: # 12/26,12/27,12/28 Gerd Kortemeyer
1.1       www        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
1.2       www        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'}) {
1.4       www        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: 	    }
1.2       www        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'});
1.4       www       101:              my $total=$#lines;
1.3       www       102:              $ENV{'SERVER_NAME'}=~/(\w+\.\w+)$/;
                    103: 	     my $krbdefdom=$1;
                    104:              $krbdefdom=~tr/a-z/A-Z/;
1.4       www       105:              my $today=time;
                    106:              my $halfyear=$today+15552000;
                    107:              my $defdom=$r->dir_config('lonDefDomain');
1.2       www       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>
1.4       www       114: Total number of records found in file: $total
1.3       www       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. '; }
1.4       www       158:     if (vf.startdate.value=='') {
                    159: 	message+='No starting date set. ';
1.3       www       160:     }
1.4       www       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:     }   
1.3       www       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: 
1.4       www       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:     }
1.3       www       275: 
                    276: </script>
1.2       www       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++) {
1.3       www       299:                    $r->print('<tr><td><select name=f'.$i.
                    300:                        ' onChange="flip(this.form,'.$i.');">');
1.2       www       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',
1.3       www       309:                       'id:ID/Student Number','sec:Group/Section',
                    310:                       'ipwd:Initial Password');
1.2       www       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: 	     }
1.3       www       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 
1.5     ! www       333: <input type=text size=10 name=intpwd onChange="setint(this.form);">)
        !           334: <h3>LON-CAPA Domain for Students</h3>
1.4       www       335: LON-CAPA domain: <input type=text size=10 value=$defdom name=lcdomain><p>
1.5     ! www       336: <h3>Starting and Ending Dates</h3>
1.4       www       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>
1.5     ! www       349: <h3>Full Update</h3>
        !           350: <input type=checkbox name=fullup value=yes> Full update 
        !           351: (also dropping students)<p>
1.3       www       352: <input type=button onClick="verify(this.form)" value="Submit Selection">
                    353: ENDPICK
1.2       www       354:          } elsif ($ENV{'form.enroll'}) {
                    355:          } elsif ($ENV{'form.drop'}) {
                    356:          }
                    357:       }
                    358: # ----------------------------------------------------------------- Phase three
                    359:       if ($ENV{'form.phase'} eq 'three') {
                    360: 	  if ($ENV{'form.datatoken'}) {
                    361:              my $separator='';
                    362:              my $remove='';
                    363:              if ($ENV{'form.upfiletype'} eq 'csv') {
                    364: 		 $separator='\"\,\s*\"';
                    365:                  $remove='"';
                    366:              } elsif ($ENV{'form.upfiletype'} eq 'space') {
                    367:                  $separator='\s+';
                    368:              } elsif ($ENV{'form.upfiletype'} eq 'tab') {
                    369:                  $separator='\t+';
                    370:              } elsif ($ENV{'form.upfiletype'} eq 'xml') {
                    371:              }
1.4       www       372:              my %fields=();
                    373:              for (my $i=0;$i<=$ENV{'form.nfields'};$i++) {
                    374:                  $fields{$ENV{'form.f'.$i}}=$i;
                    375:              }
                    376:              my $startdate=$ENV{'form.startdate'};
                    377:              my $enddate=$ENV{'form.enddate'};
                    378:              if ($startdate=~/\D/) { $startdate=''; }
                    379:              if ($enddate=~/\D/) { $enddate=''; }
1.5     ! www       380:              my $domain=$ENV{'form.lcdomain'};
        !           381:              my $amode='';
        !           382:              my $genpwd='';
        !           383:              if ($ENV{'form.login'} eq 'krb') {
        !           384:                  $amode='krb4';
        !           385:                  $genpwd=$ENV{'form.krbdom'};
        !           386:              } elsif ($ENV{'form.login'} eq 'int') {
        !           387:                  $amode='internal';
        !           388:                  if ((defined($ENV{'form.intpwd'})) && ($ENV{'form.intpwd'})) {
        !           389: 		     $genpwd=$ENV{'form.intpwd'};
        !           390:                  }
        !           391:              }
        !           392:              unless (($domain=~/\W/) || ($amode eq '')) {
        !           393:               $r->print('<h3>Enrolling Students</h3>');
        !           394:               my $count=1;
        !           395: # ----------------------------------------------------------- Get new classlist
        !           396:               my @studentdata=();
1.4       www       397:              {
                    398:                 my $fh;
                    399:                 if ($fh=Apache::File->new($r->dir_config('lonDaemons').
                    400: 		              '/tmp/'.$ENV{'form.datatoken'}.'.tmp')) {
                    401: 		    @studentdata=<$fh>;
                    402:                 }
                    403: 	     }
1.5     ! www       404: # --------------------------------------------------------- Enroll new students
1.2       www       405: 	      map {
1.4       www       406:                 my $line=$_;
                    407:                 chomp($line);
                    408:                 $line=~s/^$remove//;
                    409:                 $line=~s/$remove$//;
                    410: 	        my @entries=split(/$separator/,$line);
                    411:                 unless (($entries[$fields{'username'}] eq '') ||
                    412:                         (!defined($entries[$fields{'username'}]))) {
                    413:                   my $fname=''; my $mname=''; my $lname=''; my $gen='';
                    414:                   if (defined($fields{'names'})) {
                    415: 		      ($lname,$fname,$mname)=
                    416: 	            ($entries[$fields{'names'}]=~/([^\,]+)\,\s*(\w+)\s*(.*)$/);
                    417:                   } else {
                    418:                       if (defined($fields{'fname'})) {
                    419: 		         $fname=$entries[$fields{'fname'}];
                    420: 		      }
                    421:                       if (defined($fields{'mname'})) {
                    422: 		         $mname=$entries[$fields{'mname'}];
                    423: 		      }
                    424:                       if (defined($fields{'lname'})) {
                    425: 		         $lname=$entries[$fields{'lname'}];
                    426: 		      }
                    427:                       if (defined($fields{'gen'})) {
                    428: 		         $gen=$entries[$fields{'gen'}];
                    429: 		      }
                    430:                   }
                    431:                   if ($entries[$fields{'username'}]=~/\W/) {
                    432:                      $r->print('<p><b>Unacceptable username: '.
                    433:                               $entries[$fields{'username'}].' for user '.
                    434:                               $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
1.5     ! www       435: 		  } else {
        !           436:                       my $sec='';
        !           437:                       my $username=$entries[$fields{'username'}];
        !           438:                       if (defined($fields{'sec'})) {
        !           439:                          if (defined($entries[$fields{'sec'}])) {
        !           440: 			     $sec=$entries[$fields{'sec'}];
        !           441:                          }
        !           442:                       }
        !           443:                       my $id='';
        !           444:                       if (defined($fields{'id'})) {
        !           445:                          if (defined($entries[$fields{'id'}])) {
        !           446: 			     $id=$entries[$fields{'id'}];
        !           447:                          }
        !           448:                          $id=~tr/A-Z/a-z/;
        !           449:                       }
        !           450:                       my $password='';
        !           451:                       if ($genpwd) { 
        !           452:                          $password=$genpwd; 
        !           453: 		      } else {
        !           454:                          if (defined($fields{'ipwd'})) {
        !           455: 			     if ($entries[$fields{'ipwd'}]) {
        !           456: 				 $password=$entries[$fields{'ipwd'}];
        !           457:                              }
        !           458:                          }
        !           459:                       }
        !           460:                       if ($password) { 
        !           461:                          my $reply=&Apache::lonnet::modifystudent(
        !           462:                           $domain,$username,$id,$amode,$password,
        !           463: 			   $fname,$mname,$lname,$gen,$sec,$enddate,$startdate);
        !           464:                          unless ($reply eq 'ok') {
        !           465:                             $r->print(
        !           466:                              "<p><b>Error enrolling $username: $reply</b><p>");
        !           467: 			 }
        !           468:                      } else {
        !           469:                             $r->print(
        !           470:                              "<p><b>No password for $username</b><p>");
        !           471:                      }
1.4       www       472:                   }
                    473:                  }                 
                    474:               } @studentdata;
1.5     ! www       475: # --------------------------------------------------------------- Drop students
        !           476:               if ($ENV{'form.fullup'} eq 'yes') {
        !           477: 		 $r->print('<h3>Dropping Students</h3>');
        !           478: # ------------------------------------------------------- Get current classlist
        !           479:                  my $cid=$ENV{'request.course.id'};
        !           480:                  my $classlst=&Apache::lonnet::reply
        !           481:                  ('dump:'.$ENV{'course.'.$cid.'.domain'}.':'.
        !           482: 	                  $ENV{'course.'.$cid.'.num'}.':classlist',
        !           483: 	                  $ENV{'course.'.$cid.'.home'});
        !           484:                  my %currentlist=();
        !           485:                  my $now=time;
        !           486:                  unless ($classlst=~/^error\:/) {
        !           487: 	         } else {
        !           488:                      $r->print(
        !           489:                   '<font color=red><h3>Could not access classlist: '.$classlst.
        !           490:                   '</h3></font>');
        !           491:                  }
        !           492: 	     }
        !           493: # ------------------------------------------------------------------------ Done
1.2       www       494:           }
1.5     ! www       495:         }
1.2       www       496:       }
                    497: # ------------------------------------------------------------------------- End
1.1       www       498:       $r->print('</form></body></html>');
                    499:    } else {
                    500: # ----------------------------- Not in a course, or not allowed to modify parms
                    501:       $ENV{'user.error.msg'}=
                    502:         "/adm/dropadd:cst:0:0:Cannot drop or add students";
                    503:       return HTTP_NOT_ACCEPTABLE; 
                    504:    }
                    505:    return OK;
                    506: }
                    507: 
                    508: 1;
                    509: __END__
                    510: 
                    511: 
                    512: 
                    513: 

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