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

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

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