--- loncom/interface/Attic/londropadd.pm	2002/09/24 18:24:43	1.52
+++ loncom/interface/Attic/londropadd.pm	2002/11/13 19:16:34	1.61
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to drop and add students in courses 
 #
-# $Id: londropadd.pm,v 1.52 2002/09/24 18:24:43 matthew Exp $
+# $Id: londropadd.pm,v 1.61 2002/11/13 19:16:34 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,6 +41,7 @@ use Apache::lonnet();
 use Apache::loncommon();
 use Apache::lonhtmlcommon();
 use Apache::Constants qw(:common :http REDIRECT);
+use Spreadsheet::WriteExcel;
 
 ###############################################################
 ###############################################################
@@ -220,6 +221,7 @@ function verify_message (vf,founduname,f
 	alert('You need to specify the username field');
         return;
     }
+    // alert('current.radiovalue = '+current.radiovalue);
     if (current.radiovalue == null || current.radiovalue == 'nochange') {
         // They did not check any of the login radiobuttons.
         alert('You must choose an authentication type');
@@ -558,7 +560,7 @@ sub print_upload_manager_form {
 sub enroll_single_student {
     my $r=shift;
     $r->print('<h3>Enrolling Student</h3>');
-    $r->print('<p>Enrolling '.$ENV{'form.cuname'}." in domain ".
+    $r->print('<p>Enrolling '.$ENV{'form.cuname'}." \@ ".
               $ENV{'form.lcdomain'}.'</p>');
     if (($ENV{'form.cuname'})&&($ENV{'form.cuname'}!~/\W/)&&
         ($ENV{'form.lcdomain'})&&($ENV{'form.lcdomain'}!~/\W/)) {
@@ -594,17 +596,25 @@ sub enroll_single_student {
         my $home = &Apache::lonnet::homeserver($ENV{'form.cuname'},
                                                    $ENV{'form.lcdomain'});
         if ((($amode) && ($genpwd)) || ($home ne 'no_host')) {
+            # Clean out any old roles the student has in this class.
             &modifystudent($ENV{'form.lcdomain'},$ENV{'form.cuname'},
                            $ENV{'request.course.id'},$ENV{'form.csec'},
                             $desiredhost);
-          $r->print(&Apache::lonnet::modifystudent(
-                      $ENV{'form.lcdomain'},$ENV{'form.cuname'},
-                      $ENV{'form.cstid'},$amode,$genpwd,
- 	              $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
-                      $ENV{'form.clast'},$ENV{'form.cgen'},
-                      $ENV{'form.csec'},$ENV{'form.enddate'},
-                      $ENV{'form.startdate'},$ENV{'form.forceid'},
-                    $desiredhost));
+            my $login_result = &Apache::lonnet::modifystudent
+                ($ENV{'form.lcdomain'},$ENV{'form.cuname'},
+                 $ENV{'form.cstid'},$amode,$genpwd,
+                 $ENV{'form.cfirst'},$ENV{'form.cmiddle'},
+                 $ENV{'form.clast'},$ENV{'form.cgen'},
+                 $ENV{'form.csec'},$ENV{'form.enddate'},
+                 $ENV{'form.startdate'},$ENV{'form.forceid'},
+                 $desiredhost);
+            if ($login_result =~ /^ok/) {
+                $r->print($login_result);
+                $r->print("<p> If active, the new role will be available ".
+                          "when the student next logs in to LON-CAPA.</p>");
+            } else {
+                $r->print("unable to enroll: ".$login_result);
+            }
 	} else {
             $r->print('<p><font color="#ff0000">ERROR</font>&nbsp;'.
                       'Invalid login mode or password.  '.
@@ -726,113 +736,173 @@ END
     return;
 }
 
-# =================================================== get the current classlist
-sub get_current_classlist {
-    my $r = shift;
-    # Call DownloadClasslist
-    my $cid = $ENV{'request.course.id'};
-    my $c = $r->connection;
-    my $classlisthash = &Apache::loncoursedata::DownloadClasslist
-        ($cid,'Not downloaded',$c);
-    # Call ProcessClasslist
-    my %cache;
-    my @students = &Apache::loncoursedata::ProcessClasslist(\%cache,
-                                                            $classlisthash,
-                                                            $cid,$c);
-    return (\@students,\%cache);
-}
-
 # ========================================================= Menu Phase Two Drop
 sub print_drop_menu {
     my $r=shift;
     $r->print("<h3>Drop Students</h3>");
     my $cid=$ENV{'request.course.id'};
-    my ($student_array,$student_data)=&get_current_classlist($r);
-    if (! scalar(@$student_array)) {
+    my ($classlist,$keylist) = &Apache::loncoursedata::get_classlist();
+    if (! defined($classlist)) {
         $r->print("There are no students currently enrolled.\n");
         return;
     }
     # Print out the available choices
-    &show_drop_list($student_array,$student_data,$r);
+    &show_drop_list($r,$classlist,$keylist);
     return;
 }
 
 # ============================================== view classlist
 sub print_html_classlist {
     my $r=shift;
+    if (! exists($ENV{'form.sortby'})) {
+        $ENV{'form.sortby'} = 'username';
+    }
+    if ($ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
+        $ENV{'form.Status'} = 'Active';
+    }
+    my $status_select = &Apache::lonhtmlcommon::StatusOptions
+        ($ENV{'form.Status'},'studentform');
     $r->print(<<END);
+<input type="hidden" name="action" value="$ENV{'form.action'}" />
+<input type="hidden" name="state"  value="" />
 <p>
-<font size="+1">Current Classlist</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
-<font size="+1"><a href="/adm/dropadd?action=classlist&state=csv">CSV format</a></font>
-</p>
+<font size="+1">Current Classlist</font>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 END
+    if ($ENV{'form.action'} ne 'modifystudent') {
+        $r->print(<<END);
+<font size="+1">
+<a href="javascript:document.studentform.state.value='csv';document.studentform.submit();">CSV format</a>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="javascript:document.studentform.state.value='excel';document.studentform.submit();">Excel format</a>
+</font>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+Student Status:
+END
+    }
+    $r->print($status_select."</p>\n");
     my $cid=$ENV{'request.course.id'};
-    my ($student_array,$student_data)=&get_current_classlist($r);
-    if (! scalar(@$student_array)) {
+    my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
+    if (! defined($classlist)) {
         $r->print("There are no students currently enrolled.\n");
     } else {
         # Print out the available choices
         if ($ENV{'form.action'} eq 'modifystudent') {
-            &show_class_list($r,'view','modify','any',$student_array,$student_data);
+            &show_class_list($r,'view','modify','modifystudent',
+                             $ENV{'form.Status'},$classlist,$keylist);
         } else {
-            &show_class_list($r,'view','aboutme','any',$student_array,$student_data);
+            &show_class_list($r,'view','aboutme','classlist',
+                             $ENV{'form.Status'},$classlist,$keylist);
         }
     }
 }
 
 # ============================================== view classlist
-sub print_csv_classlist {
+sub print_formatted_classlist {
     my $r=shift;
+    my $mode = shift;
     my $cid=$ENV{'request.course.id'};
-    my ($student_array,$student_data)=&get_current_classlist($r);
-    if (! scalar(@$student_array)) {
+    my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
+    if (! defined($classlist)) {
         $r->print("There are no students currently enrolled.\n");
     } else {
-        &show_class_list($r,'csv','nolink','any',$student_array,$student_data);
+        &show_class_list($r,$mode,'nolink','csv',
+                         $ENV{'form.Status'},$classlist,$keylist);
     }
 }
 
 # =================================================== Show student list to drop
 sub show_class_list {
-    my ($r,$mode,$linkto,$statusmode,$students,$student_data)=@_;
+    my ($r,$mode,$linkto,$action,$statusmode,$classlist,$keylist)=@_;
     my $cid=$ENV{'request.course.id'};
+    #
+    # Variables for excel output
+    my ($excel_workbook, $excel_sheet, $excel_filename,$row);
+    #
+    my $sortby = $ENV{'form.sortby'};
+    if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
+        $sortby = 'username';
+    }
     # Print out header 
     if ($mode eq 'view') {
         if ($linkto eq 'aboutme') {
-            $r->print('Select a user name to view the users page.');
+            $r->print('Select a user name to view the users personal page.');
         } elsif ($linkto eq 'modify') {
             $r->print('Select a user name to modify the students information');
         }
         $r->print(<<END);
+
+<input type="hidden" name="sortby" value="$sortby" />
+<input type="hidden" name="sname"  value="" />
+<input type="hidden" name="sdom"   value="" />
 <p>
 <table border=2>
-<tr><th>username</th><th>domain</th><th>ID</th>
-    <th>student name</th><th>section</th></tr>
+<tr><th>
+       <a href="javascript:document.studentform.sortby.value='username';document.studentform.submit();">username</a>
+    </th><th>
+       <a href="javascript:document.studentform.sortby.value='domain';document.studentform.submit();">domain</a>
+    </th><th>
+       <a href="javascript:document.studentform.sortby.value='id';document.studentform.submit();">ID</a>
+    </th><th>
+       <a href="javascript:document.studentform.sortby.value='fullname';document.studentform.submit();">student name</a>
+    </th><th>
+       <a href="javascript:document.studentform.sortby.value='section';document.studentform.submit();">section</a>
+    </th>
+</tr>
 END
     } elsif ($mode eq 'csv') {
-        $r->print('"'.join('","',("username","domain","ID","student name",
-                                  "section")).'"'."\n");
-    }
-    foreach my $student (@$students) {
-        my $error;
-        if (exists($student_data->{$student.':error'})) {
-            $error = $student_data->{$student.':error'};
+        if($statusmode eq 'Expired') {
+            $r->print('"Students with expired roles"');
+        }
+        if ($statusmode eq 'Any') {
+            $r->print('"'.join('","',("username","domain","ID","student name",
+                                      "section","status")).'"'."\n");
+        } else {
+            $r->print('"'.join('","',("username","domain","ID","student name",
+                                      "section")).'"'."\n");
         }
-        if ($error) {
-            $r->print('<tr><td colspan="6">'.
-                      '<font color="#FF8888">Error</font>'.
-                      'Error retrieving data for '.
-                      join('@',split(/:/,$student)).
-                      ', '.$error.'</td></tr>'."\n");
-            next;
-        }
-        my $username = $student_data->{$student.':username'};
-        my $domain   = $student_data->{$student.':domain'};
-        my $section  = $student_data->{$student.':section'};
-        my $name     = $student_data->{$student.':fullname'};
-        my $status   = $student_data->{$student.':Status'};
-        my $id       = $student_data->{$student.':id'};
-        next if (($statusmode ne 'any') && ($status ne $statusmode));
+    } elsif ($mode eq 'excel') {
+        # Create the excel spreadsheet
+        $excel_filename = '/prtspool/'.
+            $ENV{'user.name'}.'_'.$ENV{'user.domain'}.'_'.
+                time.'_'.rand(1000000000).'.xls';
+        $excel_workbook = Spreadsheet::WriteExcel->new('/home/httpd'.
+                                                       $excel_filename);
+        $excel_workbook->set_tempdir('/home/httpd/perl/tmp');
+        $excel_sheet = $excel_workbook->addworksheet('classlist');
+        #
+        my $description = 'Classlist for '.
+            $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
+        $excel_sheet->write($row++,0,$description);
+        #
+        $excel_sheet->write($row++,0,["username","domain","ID",
+                                      "student name","section","status"]);
+    }
+    #
+    # Sort the students
+    my %index;
+    my $i;
+    foreach (@$keylist) {
+        $index{$_} = $i++;
+    }
+    my $index  = $index{$sortby};
+    my $second = $index{'username'};
+    my $third  = $index{'domain'};
+    my @Sorted_Students = sort {
+        lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
+            ||
+        lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
+            ||
+        lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
+        } (keys(%$classlist));
+    foreach my $student (@Sorted_Students) {
+        my $username = $classlist->{$student}->[$index{'username'}];
+        my $domain   = $classlist->{$student}->[$index{'domain'}];
+        my $section  = $classlist->{$student}->[$index{'section'}];
+        my $name     = $classlist->{$student}->[$index{'fullname'}];
+        my $id       = $classlist->{$student}->[$index{'id'}];
+        my $status   = $classlist->{$student}->[$index{'status'}];
+        next if (($statusmode ne 'Any') && ($status ne $statusmode));
         if ($mode eq 'view') {
             $r->print("<tr>\n    <td>\n        ");
             if ($linkto eq 'nothing') {
@@ -842,9 +912,13 @@ END
                                                              $username,
                                                              $domain));
             } elsif ($linkto eq 'modify') {
-                $r->print('<a href="/adm/dropadd?action=modifystudent'.
-                          '&state=selected'.'&sname='.$username.
-                          '&sdom='.$domain.'">'.$username."</a>\n");
+                $r->print('<a href="'.
+                          "javascript:document.studentform.sname.value='".
+                          $username.
+                          "';document.studentform.sdom.value='".$domain.
+                          "';document.studentform.state.value='selected".
+                          "';document.studentform.submit();".'">'.
+                          $username."</a>\n");
             }
             $r->print(<<"END");
     </td>
@@ -860,13 +934,25 @@ END
             foreach ($username,$domain,$id,$name,$section) {
                 push @line,&Apache::loncommon::csv_translate($_);
             }
+            if ($statusmode eq 'Any') {
+                push @line,&Apache::loncommon::csv_translate($status);
+            }
             my $tmp = $";
             $" = '","';
             $r->print("\"@line\"\n");
             $" = $tmp;
+        } elsif ($mode eq 'excel') {
+            $excel_sheet->write($row++,0,[$username,$domain,$id,
+                                          $name,$section,$status]);
         }
     }
-    $r->print('</table><br>') if ($mode eq 'view');
+    if ($mode eq 'view') {
+        $r->print('</table><br>');
+    } elsif ($mode eq 'excel') {
+        $excel_workbook->close();
+        $r->print('<p><a href="'.$excel_filename.'">'.
+                  'Your Excel spreadsheet</a> is ready for download.</p>'."\n");
+    }
 }
 
 
@@ -876,10 +962,10 @@ END
 sub print_modify_student_form {
     my $r = shift();
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-                                            ['sdom','sname']);
-    
-    my $sname = $ENV{'form.sname'};
-    my $sdom  = $ENV{'form.sdom'};
+                                            ['sdom','sname']);    
+    my $sname  = $ENV{'form.sname'};
+    my $sdom   = $ENV{'form.sdom'};
+    my $sortby = $ENV{'form.sortby'};
     # determine the students name information
     my %info=&Apache::lonnet::get('environment',
                                   ['firstname','middlename',
@@ -904,6 +990,11 @@ sub print_modify_student_form {
     my $enddateform = &Apache::lonhtmlcommon::date_setter('studentform',
                                                           'enddate',
                                                           $endtime);
+    #
+    if (! exists($ENV{'form.Status'}) || 
+        $ENV{'form.Status'} !~ /^(Any|Expired|Active)$/) {
+        $ENV{'form.Status'} = 'crap';
+    }
     # Make sure student is enrolled in course    
     $r->print(<<END);
 <p>
@@ -914,7 +1005,10 @@ Only domain coordinators can change a us
 <input type="hidden" name="slogin"  value="$sname"  />
 <input type="hidden" name="sdomain" value="$sdom" />
 <input type="hidden" name="action"  value="modifystudent" />
-<input type="hidden" name="state"  value="done" />
+<input type="hidden" name="state"   value="done" />
+<input type="hidden" name="sortby"  value="$sortby" />
+<input type="hidden" name="Status"  value="$ENV{'form.Status'}" />
+
 <h2>Modify Enrollment for $info{'firstname'} $info{'middlename'} 
 $info{'lastname'} $info{'generation'}, $sname\@$sdom</h2>
 <p>
@@ -930,6 +1024,10 @@ $info{'lastname'} $info{'generation'}, $
 </p><p>
 <b>Student ID</b>: <input type="text" name="id" value="$info{'id'}" size="12"/>
 </p><p>
+<input type="checkbox" name="forceid" > 
+Disable ID/Student Number Safeguard and Force Change of Conflicting IDs
+(only do if you know what you are doing)
+</p><p>
 <b>Section</b>: <input type="text" name="section" value="$section" size="4"/>
 </p><p>
 <table>
@@ -948,15 +1046,37 @@ END
 #
 sub modify_single_student {
     my $r = shift;
-    # make sure user can modify student data?
+    # Get the 'sortby' and 'Status' variables so the user goes back to their
+    # previous screen
+    my $sortby = $ENV{'form.sortby'};
+    my $status = $ENV{'form.Status'};
+    #
+    # We always need this information
+    my $slogin     = $ENV{'form.slogin'};
+    my $sdom       = $ENV{'form.sdomain'};
+    #
+    # Get the old data
+    my %old=&Apache::lonnet::get('environment',
+                                 ['firstname','middlename',
+                                  'lastname','generation','id'],
+                                 $sdom, $slogin);
+    $old{'section'} = &Apache::lonnet::getsection($sdom,$slogin,
+                                                  $ENV{'request.course.id'});
+    my ($tmp) = keys(%old);
+    if ($tmp =~ /^(con_lost|error|no_such_host)/i) {
+        $r->print("There was an error determining the environment values ".
+                  " for $slogin \@ $sdom.");
+        return;
+    }
+    undef $tmp;
+    #
+    # Get the new data
     my $firstname  = $ENV{'form.firstname'};
     my $middlename = $ENV{'form.middlename'};
     my $lastname   = $ENV{'form.lastname'};
     my $generation = $ENV{'form.generation'};
     my $section    = $ENV{'form.section'};
     my $courseid   = $ENV{'request.course.id'};
-    my $sname      = $ENV{'form.slogin'};
-    my $sdom       = $ENV{'form.sdomain'};
     my $sid        = $ENV{'form.id'};
     my $starttime = &Apache::lonhtmlcommon::get_date_from_form('startdate',
                                                                time);
@@ -964,38 +1084,76 @@ sub modify_single_student {
                                                                time);
     my $displayable_starttime = localtime($starttime);
     my $displayable_endtime   = localtime($endtime);
+    # 
+    # check for forceid override
+    if (($sid ne $old{'id'}) && (! exists($ENV{'form.forceid'}))) {
+        $r->print("<font color=\"ff0000\">You changed the students id ".
+                  " but did not disable the ID change safeguard.".
+                  "  The students id will not be changed.</font>");
+        $sid = $old{'id'};
+    }
+    #
     # talk to the user about what we are going to do
     $r->print(<<END);
-<h2>Modifying data for user $sname \@ $sdom </h2>
+    <h2>Modifying data for user $slogin \@ $sdom </h2>
 <h3>Student Information</h3>
-<table>
-<tr><td>First name  </td><td> $firstname  </td></tr>
-<tr><td>Middle name </td><td> $middlename </td></tr>
-<tr><td>Last name   </td><td> $lastname   </td></tr>
-<tr><td>Generation  </td><td> $generation </td></tr>
+<table rules="rows" border="1" cellpadding="3" >
+<tr>
+    <th> Field </th>
+    <th> Old Value </th>
+    <th> New Value </th>
+</tr>
+<tr>
+    <td> <b>First name</b> </td>
+    <td> $old{'firstname'} </td>
+    <td> $firstname </td>
+</tr><tr>
+    <td> <b>Middle name</b> </td>
+    <td> $old{'middlename'} </td>
+    <td> $middlename </td>
+</tr><tr>
+    <td> <b>Last name</b> </td>
+    <td> $old{'lastname'} </td>
+    <td> $lastname </td>
+</tr><tr>
+    <td> <b>Generation</b> </td>
+    <td> $old{'generation'} </td>
+    <td> $generation </td>
+</tr><tr>
+    <td> <b>ID</b> </td>
+    <td> $old{'id'} </td>
+    <td> $sid </td>
+</tr><tr>
+    <td> <b>Section</b> </td>
+    <td> $old{'section'} </td>
+    <td> $section</td>
+</tr>
 </table>
 <h3>Role Information</h3>
 <table>
-<tr><td>Section     </td><td> $section    </td></tr>
 <tr><td>Start Time  </td><td> $displayable_starttime </td></tr>
 <tr><td>End Time    </td><td> $displayable_endtime   </td></tr>
 </table>
 <p>
 END
+    #
     # Send request(s) to modify data
-    #   The '1' in the call to modifystudent is to force the students 
-    #   id to be changed.
     my $roleresults = &Apache::lonnet::modifystudent
-        ($sdom,$sname,$sid,undef,undef,$firstname,$middlename,$lastname,
-         $generation,$section,$endtime,$starttime,1);
-    if ($roleresults =~/refused/) {
+        ($sdom,$slogin,$sid,undef,undef,$firstname,$middlename,$lastname,
+         $generation,$section,$endtime,$starttime,$ENV{'form.forceid'});
+    if ($roleresults eq 'refused' ) {
         $r->print("Your request to change the role information for this ".
-                  "student was refused.");
+                  "student was refused.  You do not appear to have ".
+                  "sufficient authority to change student information.");
     } elsif ($roleresults !~ /ok/) {
         $r->print("An error occurred during the attempt to change the role".
                   " information for this student.  <br />".
                   "The error reported was ".
                   $roleresults);
+        &Apache::lonnet::logthis("londropadd:failed attempt to modify student".
+                                 " data for ".$slogin." \@ ".$sdom." by ".
+                                 $ENV{'user.name'}." \@ ".$ENV{'user.domain'}.
+                                 ":".$roleresults);
     } else { # everything is okay!
         $r->print("Student information updated successfully. <br />".
                   "The student must log out and log in again to see ".
@@ -1003,7 +1161,10 @@ END
     }
     $r->print(<<END);
 </p><p>
-<a href="/adm/dropadd?action=modifystudent">Modify another students data</a>
+<input type="hidden" name="action" value="modifystudent" />
+<input type="hidden" name="sortby" value="$sortby" />
+<input type="hidden" name="Status" value="$status" />
+<a href="javascript:document.studentform.submit();">Modify another students data</a>
 </body></html>
 END
     return;
@@ -1042,12 +1203,49 @@ sub get_enrollment_data {
     return ($start,$end,$section);
 }
 
-# =================================================== Show student list to drop
+#################################################
+#################################################
+
+=pod
+
+=item show_drop_list
+
+Display a list of students to drop
+Inputs: 
+
+=over 4
+
+=item $r, Apache request
+
+=item $classlist, hash pointer returned from loncoursedata::get_classlist();
+
+=item $keylist, array pointer returned from loncoursedata::get_classlist() 
+which describes the order elements are stored in the %$classlist values.
+
+=item $nosort, if true, sorting links are omitted.
+
+=back
+
+=cut
+
+#################################################
+#################################################
 sub show_drop_list {
-    my ($students,$student_data,$r)=@_;
+    my ($r,$classlist,$keylist,$nosort)=@_;
     my $cid=$ENV{'request.course.id'};
-    $r->print(<<'END');
-<input type="hidden" name="action" value="drop" />
+    if (! exists($ENV{'form.sortby'})) {
+        &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+                                                ['sortby']);
+    }
+    my $sortby = $ENV{'form.sortby'};
+    if ($sortby !~ /^(username|domain|section|fullname|id)$/) {
+        $sortby = 'username';
+    }
+    #
+    my $action = "drop";
+    $r->print(<<END);
+<input type="hidden" name="sortby" value="$sortby" />
+<input type="hidden" name="action" value="$action" />
 <input type="hidden" name="state"  value="done" />
 <script>
 function checkAll(field) {
@@ -1062,29 +1260,64 @@ function uncheckAll(field) {
 </script>
 <p>
 <input type="hidden" name="phase" value="four">
+END
+
+    if ($nosort) {
+        $r->print(<<END);
+<table border=2>
+<tr>
+    <th>&nbsp;</th>
+    <th>username</th>
+    <th>domain</th>
+    <th>ID</th>
+    <th>student name</th>
+    <th>section</th>
+</tr>
+END
+
+    } else  {
+        $r->print(<<END);
 <table border=2>
-<tr><th>&nbsp;</th><th>username</th><th>domain</th>
-    <th>ID</th><th>student name</th><th>section</th></tr>
+<tr><th>&nbsp;</th>
+    <th>
+       <a href="/adm/dropadd?action=$action&sortby=username">username</a>
+    </th><th>
+       <a href="/adm/dropadd?action=$action&sortby=domain">domain</a>
+    </th><th>
+       <a href="/adm/dropadd?action=$action&sortby=id">ID</a>
+    </th><th>
+       <a href="/adm/dropadd?action=$action&sortby=fullname">student name</a>
+    </th><th>
+       <a href="/adm/dropadd?action=$action&sortby=section">section</a>
+    </th>
+</tr>
 END
-    foreach my $student (@$students) {
+    }
+    #
+    # Sort the students
+    my %index;
+    my $i;
+    foreach (@$keylist) {
+        $index{$_} = $i++;
+    }
+    my $index  = $index{$sortby};
+    my $second = $index{'username'};
+    my $third  = $index{'domain'};
+    my @Sorted_Students = sort {
+        lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
+            ||
+        lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
+            ||
+        lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
+        } (keys(%$classlist));
+    foreach my $student (@Sorted_Students) {
         my $error;
-        if (exists($student_data->{$student.':error'})) {
-            $error = $student_data->{$student.':error'};
-        }
-        if ($error) {
-            $r->print('<tr><td colspan="6">'.
-                      '<font color="#FF8888">Error</font>'.
-                      'Error retrieving data for '.
-                      join('@',split(/:/,$student)).
-                      ', '.$error.'</td></tr>'."\n");
-            next;
-        }
-        my $username = $student_data->{$student.':username'};
-        my $domain   = $student_data->{$student.':domain'};
-        my $section  = $student_data->{$student.':section'};
-        my $name     = $student_data->{$student.':fullname'};
-        my $status   = $student_data->{$student.':Status'};
-        my $id       = $student_data->{$student.':id'};
+        my $username = $classlist->{$student}->[$index{'username'}];
+        my $domain   = $classlist->{$student}->[$index{'domain'}];
+        my $section  = $classlist->{$student}->[$index{'section'}];
+        my $name     = $classlist->{$student}->[$index{'fullname'}];
+        my $id       = $classlist->{$student}->[$index{'id'}];
+        my $status   = $classlist->{$student}->[$index{'status'}];
         next if ($status ne 'Active');
         #
         $r->print(<<"END");
@@ -1253,6 +1486,11 @@ sub upfile_drop_add {
                             }
                         }
                     }
+                    # Clean up whitespace
+                    foreach (\$domain,\$username,\$id,\$fname,\$mname,
+                             \$lname,\$gen,\$sec) {
+                        $$_ =~ s/(\s+$|^\s+)//g;
+                    }
                     if ($password) {
                         &modifystudent($domain,$username,$cid,$sec,
                                        $desiredhost);
@@ -1279,73 +1517,35 @@ sub upfile_drop_add {
                 }
             }
         } # end of foreach (@studentdata)
-        $r->print('<p>Processed Students: '.$count);
+        $r->print('<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>");
         #####################################
         #           Drop students           #
         #####################################
         if ($ENV{'form.fullup'} eq 'yes') {
             $r->print('<h3>Dropping Students</h3>');
             #  Get current classlist
-            my ($error,%currentlist)=&get_current_classlist($r);
-            if (defined($error)) {
-                $r->print('<pre>ERROR:$error</pre>');
-            }
-            if (defined(%currentlist)) {
-                # Drop the students
+            my ($classlist,$keylist)=&Apache::loncoursedata::get_classlist();
+            if (! defined($classlist)) {
+                $r->print("There are no students currently enrolled.\n");
+            } else {
+                # Remove the students we just added from the list of students.
                 foreach (@studentdata) {
                     my %entries=&Apache::loncommon::record_sep($_);
                     unless (($entries{$fields{'username'}} eq '') ||
                             (!defined($entries{$fields{'username'}}))) {
-                        delete($currentlist{$entries{$fields{'username'}}.
+                        delete($classlist->{$entries{$fields{'username'}}.
                                                 ':'.$domain});
                     }
                 }
-                # Print out list of dropped students
-                &show_drop_list($r,%currentlist);
-            } else {
-                $r->print("There are no students currently enrolled.\n");
+                # Print out list of dropped students.
+                &show_drop_list($r,$classlist,$keylist,'nosort');
             }
         }
     } # end of unless
 }
 
-###################################################################
-###################################################################
-
-=pod
-
-=item &drop_students
-
-Inputs: \@droplist, a pointer to an array of students to drop.
-Students should be in format of studentname:studentdomain
-
-Returns: $errors, a string describing any errors encountered.
-$successes, a string describing the successful dropping of students.
-
-=cut
-
-###################################################################
-###################################################################
-sub drop_students {
-    my @droplist = @{shift()};
-    my $courseid = $ENV{'request.course.id'};
-    my $successes = '';
-    my $errors = '';
-    foreach (@droplist) {
-        my ($sname,$sdom)=split(/:/,$_);
-        my $result = &drop_student($sname,$sdom,$courseid);
-        if ($result !~ /ok/) {
-            $errors .= "Error dropping $sname\@$sdom: $result\n";
-        } else {
-            $successes .= "Dropped $sname\@$sdom\n";
-        }
-    }
-    return ($errors,$successes);
-}
-###################################################################
-###################################################################
-
-
 # ================================================================== Phase four
 sub drop_student_list {
     my $r=shift;
@@ -1358,17 +1558,18 @@ sub drop_student_list {
     }
     foreach (@droplist) {
         my ($uname,$udom)=split(/\:/,$_);
+        # drop student
         my $result = &modifystudent($udom,$uname,$ENV{'request.course.id'});
         if ($result eq 'ok' || $result eq 'ok:') {
-            $r->print('Dropped '.$uname.' at '.$udom.'<br>');
+            $r->print('Dropped '.$uname.' @ '.$udom.'<br>');
+            $count++;
         } else {
-            $r->print('Error dropping '.$uname.' at '.$udom.': '.$result.
+            $r->print('Error dropping '.$uname.' @ '.$udom.': '.$result.
                       '<br />');
         }
-        $count++;
     }
     $r->print('<p><b>Dropped '.$count.' student(s).</b>');
-    $r->print('<p>Re-enrollment will re-activate data.');
+    $r->print('<p>Re-enrollment will re-activate data.') if ($count);
 }
 
 ###################################################################
@@ -1456,7 +1657,7 @@ sub handler {
         } elsif ($ENV{'form.state'} eq 'done') {
             &drop_student_list($r);
         } else {
-            &menu_phase_two_drop($r);
+            &print_drop_menu($r);
         }
     } elsif ($ENV{'form.action'} eq 'enrollstudent') {
         if (! exists($ENV{'form.state'})) {
@@ -1470,7 +1671,9 @@ sub handler {
         if (! exists($ENV{'form.state'})) {
             &print_html_classlist($r);
         } elsif ($ENV{'form.state'} eq 'csv') {
-            &print_csv_classlist($r);
+            &print_formatted_classlist($r,'csv');
+        } elsif ($ENV{'form.state'} eq 'excel') {
+            &print_formatted_classlist($r,'excel');
         } else {
             &print_html_classlist($r);
         }