--- loncom/interface/Attic/londropadd.pm	2003/06/23 14:38:53	1.69
+++ loncom/interface/Attic/londropadd.pm	2003/08/14 14:16:42	1.81
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to drop and add students in courses 
 #
-# $Id: londropadd.pm,v 1.69 2003/06/23 14:38:53 matthew Exp $
+# $Id: londropadd.pm,v 1.81 2003/08/14 14:16:42 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -79,13 +79,16 @@ sub modifystudent {
             # We are in this course
             my $section=$1;
             $section='' if ($course eq $courseid.'_st');
-            if ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
+            if ($section eq $csec) {
+                $result .= 'ok:';
+            } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                 my $now=time;
                 # if this is an active role 
                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     my $reply=&Apache::lonnet::modifystudent
-                        ($udom,$unam,'','','','','','','',
+                        # dom  name  id mode pass     f     m     l     g
+                        ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
                          $section,time,undef,undef,$desiredhost);
                     $result .= $reply.':';
                 }
@@ -151,7 +154,7 @@ sub print_main_menu {
 </font>
 </p><p>
 <font size="+1">
-    <a href="/adm/dropadd?action=classlist">View Classlist</a>
+    <a href="/adm/dropadd?action=classlist">View Class List</a>
 </font>
 </p><p>
 <font size="+1">
@@ -195,6 +198,7 @@ to this page if the data selected is ins
 <input type="hidden" name="associate"  value="" />
 <input type="hidden" name="datatoken"  value="$datatoken" />
 <input type="hidden" name="fileupload" value="$ENV{'form.fileupload'}" />
+<input type="hidden" name="noFirstLine" value="$ENV{'form.noFirstLine'}" />
 <input type="hidden" name="upfiletype" value="$ENV{'form.upfiletype'}" />
 <input type="hidden" name="upfile_associate" 
                                        value="$ENV{'form.upfile_associate'}" />
@@ -215,7 +219,7 @@ sub javascript_validations {
     my $authheader = &Apache::loncommon::authform_header(%param);
     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
     return (<<ENDPICK);
-function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec) {
+function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
     var foundatype=0;
     var message='';
     if (founduname==0) {
@@ -251,11 +255,12 @@ function verify_message (vf,founduname,f
         }
     }
 
-    if (foundname==0) { message='No name fields specified. '; }
-    if (foundid==0) { message+='No ID or student number field specified. '; }
-    if (foundsec==0) { message+='No section or group field specified. '; }
+    if (foundname==0) { message='name fields'; }
+    if (foundid==0) { if (message!='') { message+=', '; } message+='student number field'; }
+    if (foundsec==0) {  if (message!='') { message+=', '; } message+='section or group field'; }
+    if (foundemail==0) {  if (message!='') { message+=', '; } message+='email address field'; }
     if (message!='') {
-       message+='Continue enrollment?';
+       message='Not specified (optional): '+message+'.  Continue enrollment?';
        if (confirm(message)) {
           vf.state.value='enrolling';
 	  vf.submit();
@@ -271,6 +276,36 @@ ENDPICK
 
 }
 
+sub javascript_validations_without_auth {
+    my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
+    return (<<ENDPICK);
+function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
+    var foundatype=0;
+    var message='';
+    if (founduname==0) {
+	alert('You need to specify the username field');
+        return;
+    }
+    if (foundname==0) { message='name fields'; }
+    if (foundid==0) { if (message!='') { message+=', '; } message+='student number field'; }
+    if (foundsec==0) {  if (message!='') { message+=', '; } message+='section or group field'; }
+    if (foundemail==0) {  if (message!='') { message+=', '; } message+='email address field'; }
+    if (message!='') {
+       message='Not specified (optional): '+message+'.  Continue enrollment?';
+       if (confirm(message)) {
+          vf.state.value='enrolling';
+	  vf.submit();
+       }
+    } else {
+      vf.state.value='enrolling';
+      vf.submit();
+    }
+}
+
+ENDPICK
+
+}
+
 ###############################################################
 ###############################################################
 sub upload_manager_javascript_forward_associate {
@@ -281,6 +316,7 @@ function verify(vf) {
     var foundname=0;
     var foundid=0;
     var foundsec=0;
+    var foundemail=0;
     var tw;
     for (i=0;i<=vf.nfields.value;i++) {
         tw=eval('vf.f'+i+'.selectedIndex');
@@ -289,8 +325,9 @@ function verify(vf) {
         if (tw==7) { foundid=1; }
         if (tw==8) { foundsec=1; }
         if (tw==9) { foundpwd=1; }
+        if (tw==10) { foundemail=1; }
     }
-    verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec);
+    verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail);
 }
 
 //
@@ -309,7 +346,8 @@ function verify(vf) {
 // 7 = id
 // 8 = section
 // 9 = ipwd  (password)
-//
+// 10 = email address
+
 function flip(vf,tf) {
    var nw=eval('vf.f'+tf+'.selectedIndex');
    var i;
@@ -487,6 +525,7 @@ sub print_upload_manager_form {
     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom);
     my $i;
     my $keyfields;
+    my $dummy;
     if ($total>=0) {
 	my @d=(['username','Username'],
                ['names','Last Name, First Names'],
@@ -496,7 +535,12 @@ sub print_upload_manager_form {
                ['gen','Generation'],
 	       ['id','ID/Student Number'],
                ['sec','Group/Section'],
-	       ['ipwd','Initial Password']);
+	       ['ipwd','Initial Password'],
+               ['email','EMail Address']);
+	if($ENV{'form.noFirstLine'}){
+	    ($dummy,@records)=@records;
+	}
+
 	if ($ENV{'form.upfile_associate'} eq 'reverse') {	
 	    &Apache::loncommon::csv_print_samples($r,\@records);
 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,\@d);
@@ -515,6 +559,8 @@ sub print_upload_manager_form {
 # ======================================================= Enroll single student
 sub enroll_single_student {
     my $r=shift;
+    # Remove non alphanumeric values from section
+    $ENV{'form.csec'}=~s/\W//g;
     #
     # We do the dates first because the action of making them the defaul
     # in the course is entirely seperate from the action of enrolling the
@@ -582,10 +628,14 @@ sub enroll_single_student {
                 $r->print("unable to enroll: ".$login_result);
             }
 	} else {
-            $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
-                      'Invalid login mode or password.  '.
-                      'Unable to enroll '.$ENV{'form.cuname'}.'.</p>');
-        }          
+            $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;');
+            if ($amode =~ /^krb/) {
+                $r->print('Missing Kerberos domain information.  ');
+            } else {
+                $r->print('Invalid login mode or password.  ');
+            }
+            $r->print('<b>Unable to enroll '.$ENV{'form.cuname'}.'.</b></p>');
+        }
     } else {
         $r->print('Invalid username or domain');
     }    
@@ -670,32 +720,132 @@ sub make_dates_default {
     return $result;
 }
 
-# ======================================================= Menu Phase Two Enroll
+##
+## Single student enrollment routines (some of them)
+##
+sub get_student_username_domain_form {
+    my $r = shift;
+    my $domform = &Apache::loncommon::select_dom_form
+        ($r->dir_config('lonDefDomain'),'cudomain',0);
+    $r->print(<<END);
+<input type="hidden" name="action" value="enrollstudent" />
+<input type="hidden" name="state"  value="gotusername" />
+<h3>Enroll One Student</h3>
+<table>
+<tr><th>Username:</th>
+    <td><input type="text" name="cuname"  size="15" /></td></tr>
+<tr><th>Domain:</th>
+    <td>$domform</td></tr>
+<tr><th>&nbsp;</th>
+    <td>
+    <input type="submit" name="Begin Enrollment" value="Begin Enrollment" />
+    </td></tr>
+</table>
+END
+    return;
+}
+
 sub print_enroll_single_student_form {
     my $r=shift;
     $r->print("<h3>Enroll One Student</h3>");
-    my $today    = time;
-    my $halfyear = $today+15552000;
-    my $defdom=$r->dir_config('lonDefDomain');
-    # Set up authentication forms
-    my ($krbdef,$krbdefdom) =
-        &Apache::loncommon::get_kerberos_defaults($defdom);
-    my $javascript_validations=&javascript_validations($krbdefdom);
-    my %param = ( formname => 'document.studentform',
-                  kerb_def_dom => $krbdefdom,
-                  kerb_def_auth => $krbdef
-                  );
-    my $krbform = &Apache::loncommon::authform_kerberos(%param);
-    my $intform = &Apache::loncommon::authform_internal(%param);
-    my $locform = &Apache::loncommon::authform_local(%param);
-    # Set up domain selection form
-    my $domform = &domain_form($defdom);
+    #
+    my $username = $ENV{'form.cuname'};
+    my $domain   = $ENV{'form.cudomain'};
+    my $home = &Apache::lonnet::homeserver($username,$domain);
+    # $new_user flags whether we are creating a new user or using an old one
+    my $new_user = 1;
+    if ($home ne 'no_host') {
+        $new_user = 0;
+    }
+    &Apache::lonnet::logthis('home = '.$home);
+    #
+    my $user_data_html = '';
+    my $javascript_validations = '';
+    if ($new_user) {
+        my $defdom=$r->dir_config('lonDefDomain');
+        # Set up authentication forms
+        my ($krbdef,$krbdefdom) =
+            &Apache::loncommon::get_kerberos_defaults($domain);
+        $javascript_validations=&javascript_validations($krbdefdom);
+        my %param = ( formname => 'document.studentform',
+                      kerb_def_dom => $krbdefdom,
+                      kerb_def_auth => $krbdef
+                      );
+        my $krbform = &Apache::loncommon::authform_kerberos(%param);
+        my $intform = &Apache::loncommon::authform_internal(%param);
+        my $locform = &Apache::loncommon::authform_local(%param);
+        #
+        # Set up domain selection form
+        my $homeserver_form = '';
+        my %servers = &Apache::loncommon::get_library_servers($domain);
+        $homeserver_form = '<select name="lcserver" size="1">'."\n".
+            '<option value="default" selected>default</option>'."\n";
+        while (my ($servername,$serverdescription) = each (%servers)) {
+            $homeserver_form .= '<option value="'.$servername.'">'.
+                $serverdescription."</option>\n";
+        }
+        $homeserver_form .= "</select>\n";
+        #
+        #
+        $user_data_html = <<END;
+<h3>User Data for $username\@$domain</h3>
+<table>
+<tr><th>First Name:</th>
+    <td><input type="text" name="cfirst"  size="15"></td></tr>
+<tr><th>Middle Name:</th>
+    <td><input type="text" name="cmiddle" size="15"></td></tr>
+<tr><th>Last Name:</th>
+    <td><input type="text" name="clast"   size="15"></td></tr>
+<tr><th>Generation:</th>
+    <td><input type="text" name="cgen"    size="5"> </td></tr>
+<tr><th>Home Server:</th>
+    <td>$homeserver_form</td></tr>
+</table>
+<h3>Password</h3>
+Please select an authentication mechanism
+<table>
+<p>
+$krbform
+<br />
+$intform
+<br />
+$locform
+</p>
+END
+    } else {
+        # User already exists.  Do not worry about authentication
+        my %uenv = &Apache::lonnet::dump('environment',$domain,$username);
+        $javascript_validations = &javascript_validations_without_auth();
+        $user_data_html = <<END;
+<h3>User Data for $username\@$domain</h3>
+<input type="hidden" name="lcserver" value="default" />
+<table>
+<tr><th>First Name:</th>
+    <td>
+    <input type="text" name="cfirst" value="$uenv{'firstname'}" size="15" />
+    </td></tr>
+<tr><th>Middle Name:</th>
+    <td>
+    <input type="text" name="cmiddle" value="$uenv{'middlename'}" size="15" />
+    </td></tr>
+<tr><th>Last Name:</th>
+    <td>
+    <input type="text" name="clast"value="$uenv{'lastname'}" size="15" />
+    </td></tr>
+<tr><th>Generation:</th>
+    <td>
+    <input type="text" name="cgen" value="$uenv{'generation'}" size="5" />
+    </td></tr>
+</table>
+END
+    }
     my $date_table = &date_setting_table();
-    # Print it all out
+        # Print it all out
     $r->print(<<END);
-<input type="hidden" name="action" value="enrollstudent">
-<input type="hidden" name="state"  value="done">
-
+<input type="hidden" name="action" value="enrollstudent" />
+<input type="hidden" name="state"  value="done" />
+<input type="hidden" name="cuname" value="$username" />
+<input type="hidden" name="lcdomain" value="$domain" />
 <script type="text/javascript" language="Javascript">
 function verify(vf) {
     var founduname=0;
@@ -732,25 +882,8 @@ function clearpwd(vf) {
 }
 
 </script>
-<h3>Personal Data</h3>
-<table>
-<tr><td>First Name:</td><td> <input type="text" name="cfirst"  size="15"></td></tr>
-<tr><td>Middle Name:</td><td> <input type="text" name="cmiddle" size="15"></td></tr>
-<tr><td>Last Name: </td><td><input type="text" name="clast"   size="15"></td></tr>
-<tr><td>Generation: </td><td><input type="text" name="cgen"    size="5"> </td></tr>
-</table>
 
-<h3>Login Data</h3>
-<p>Username: <input type="text" name="cuname"  size="15"></p>
-<p>Domain:   $domform</p>
-<p>Note: login settings below  will not take effect if the user already exists
-</p><p>
-$krbform
-</p><p>
-$intform
-</p><p>
-$locform
-</p><p>
+$user_data_html
 
 <h3>Course Data</h3>
 
@@ -802,7 +935,7 @@ sub print_html_classlist {
 <input type="hidden" name="action" value="$ENV{'form.action'}" />
 <input type="hidden" name="state"  value="" />
 <p>
-<font size="+1">Current Classlist</font>
+<font size="+1">Current Class List</font>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 END
     if ($ENV{'form.action'} ne 'modifystudent') {
@@ -907,7 +1040,7 @@ END
         $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
         $excel_sheet = $excel_workbook->addworksheet('classlist');
         #
-        my $description = 'Classlist for '.
+        my $description = 'Class List for '.
             $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
         $excel_sheet->write($row++,0,$description);
         #
@@ -1074,6 +1207,9 @@ END
 sub modify_single_student {
     my $r = shift;
     #
+    # Remove non alphanumeric values from the section
+    $ENV{'form.section'} =~ s/\W//g;
+    #
     # Do the date defaults first
     my ($starttime,$endtime) = &get_dates_from_form();
     if ($ENV{'form.makedatesdefault'}) {
@@ -1398,6 +1534,7 @@ $upfile_select
 <input type=submit name="fileupload" value="Upload Courselist">
 <input type="hidden" name="action" value="upload" />
 <input type="hidden" name="state"  value="got_file" />
+<input type="checkbox" name="noFirstLine" /> Ignore First Line<br />
 </p>
 $create_classlist_help <br />
 $create_csv_help
@@ -1459,11 +1596,19 @@ sub upfile_drop_add {
             $genpwd=$ENV{'form.locarg'};
         }
     }
+    if ($amode =~ /^krb/) {
+        if (! defined($genpwd) || $genpwd eq '') {
+            $r->print('<font color="red" size="+1">'.
+                      'Unable to enroll students:'.'</font>  '.
+                      'No Kerberos domain was specified.</p>');
+            $amode = ''; # This causes the loop below to be skipped
+        }
+    }
     unless (($domain=~/\W/) || ($amode eq '')) {
         #######################################
         ##         Enroll Students           ##
         #######################################
-        $r->print('<h3>Enrolling Students</h3>');
+        $r->print('<h3>Enrolling Students</h3>'."\n".'<p>');
         my $count=0;
         my $flushc=0;
         my %student=();
@@ -1492,9 +1637,9 @@ sub upfile_drop_add {
                     }
                 }
                 if ($entries{$fields{'username'}}=~/\W/) {
-                    $r->print('<p><b>Unacceptable username: '.
+                    $r->print('<br /><b>Unacceptable username: '.
                               $entries{$fields{'username'}}.' for user '.
-                              $fname.' '.$mname.' '.$lname.' '.$gen.'</b><p>');
+                              $fname.' '.$mname.' '.$lname.' '.$gen.'</b>');
                 } else {
                     # determine section number
                     my $sec='';
@@ -1504,6 +1649,8 @@ sub upfile_drop_add {
                             $sec=$entries{$fields{'sec'}};
                         }
                     }
+                    # remove non alphanumeric values from section
+                    $sec =~ s/\W//g;
                     # determine student id number
                     my $id='';
                     if (defined($fields{'id'})) {
@@ -1512,6 +1659,14 @@ sub upfile_drop_add {
                         }
                         $id=~tr/A-Z/a-z/;
                     }
+                    # determine email address
+                    my $email='';
+                    if (defined($fields{'email'})) {
+                        if (defined($entries{$fields{'email'}})) {
+                            $email=$entries{$fields{'email'}};
+                            unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
+                        }
+                    }
                     # determine student password
                     my $password='';
                     if ($genpwd) { 
@@ -1534,11 +1689,12 @@ sub upfile_drop_add {
                         my $reply=&Apache::lonnet::modifystudent
                             ($domain,$username,$id,$amode,$password,
                              $fname,$mname,$lname,$gen,$sec,$enddate,
-                             $startdate,$ENV{'form.forceid'},$desiredhost);
+                             $startdate,$ENV{'form.forceid'},$desiredhost,
+                             $email);
                         if ($reply ne 'ok') {
-                            $r->print('<p><b>'.
-                                      'Error enrolling '.$username.': '.
-                                      $reply.'</b></p>');
+                            $reply =~ s/^error://;
+                            $r->print('<br /><b>'.$username.'</b>:'.
+                                      '  Unable to enroll: '.$reply);
          		} else {
                             $count++; $flushc++;
                             $student{$username}=1;
@@ -1549,12 +1705,13 @@ sub upfile_drop_add {
                             }
                         }
                     } else {
-                        $r->print("<p><b>No password for $username</b><p>");
+                        $r->print('<br /><b>'.$username.'</b>:'.
+                           '  Unable to enroll: No password specified.');
                     }
                 }
             }
         } # end of foreach (@studentdata)
-        $r->print('<p>Processed Students: '.$count.'</p>');
+        $r->print('</p><p>Processed Students: '.$count.'</p>');
         $r->print("<p>If active, the new role will be available when the ".
                   "students next log in to LON-CAPA.</p>");
         #####################################
@@ -1630,7 +1787,8 @@ The response to the request is governed
  upload           enrolling      enroll students based on upload
  drop             undefined      print the classlist ready to drop
  drop             done           drop the selected students
- enrollstudent    undefined      print single student enroll menu
+ enrollstudent    undefined      print student username domain form
+ enrollstudent    gotusername    print single student enroll menu
  enrollstudent    enrolling      enroll student
  classlist        undefined      print html classlist
  classlist        csv            print csv classlist
@@ -1698,11 +1856,13 @@ sub handler {
         }
     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
         if (! exists($ENV{'form.state'})) {
+            &get_student_username_domain_form($r);
+        } elsif ($ENV{'form.state'} eq 'gotusername') {
             &print_enroll_single_student_form($r);
         } elsif ($ENV{'form.state'} eq 'enrolling') {
             &enroll_single_student($r);
         } else {
-            &print_enroll_single_student_form($r);
+            &get_student_username_domain_form($r);
         }
     } elsif ($ENV{'form.action'} eq 'classlist') {
         if (! exists($ENV{'form.state'})) {