[LON-CAPA-cvs] cvs: loncom /interface lonprintout.pm

foxr lon-capa-cvs@mail.lon-capa.org
Mon, 13 Dec 2004 23:28:46 -0000


foxr		Mon Dec 13 18:28:46 2004 EDT

  Modified files:              
    /loncom/interface	lonprintout.pm 
  Log:
  Enhancement request 3618:
    Added the ability to sort printout by student last name across all
  sections being printed.  This helps people who give multi-section exams e.g.
  
  
  
Index: loncom/interface/lonprintout.pm
diff -u loncom/interface/lonprintout.pm:1.340 loncom/interface/lonprintout.pm:1.341
--- loncom/interface/lonprintout.pm:1.340	Mon Dec 13 17:24:03 2004
+++ loncom/interface/lonprintout.pm	Mon Dec 13 18:28:43 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Printout
 #
-# $Id: lonprintout.pm,v 1.340 2004/12/13 22:24:03 foxr Exp $
+# $Id: lonprintout.pm,v 1.341 2004/12/13 23:28:43 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,6 +45,64 @@
 my $LaTeXwidth = 0;
 
 
+#   Compare two students by name.  The students are in the form
+#   returned by the helper:
+#      user:domain:section:last,   first:status
+#   This is a helper function for the perl sort built-in  therefore:
+# Implicit Inputs:
+#    $a     - The first element to compare (global)
+#    $b     - The second element to compare (global)
+# Returns:
+#   -1   - $a < $b
+#    0   - $a == $b
+#   +1   - $a > $b
+#   Note that the initial comparison is done on the last names with the
+#   first names only used to break the tie.
+#
+#
+sub compare_names {
+    #  First split the names up into the primary fields.
+
+    my ($u1, $d1, $s1, $n1, $stat1) = split(/:/, $a);
+    my ($u2, $d2, $s2, $n2, $stat2) = split(/:/, $b);
+
+    # Now split the last name and first name of each n:
+    #
+
+    my ($l1,$f1) = split(/,/, $n1);
+    my ($l2,$f2) = split(/,/, $n2);
+
+    # We don't bother to remove the leading/trailing whitespace from the
+    # firstname, unless the last names compare identical.
+
+    if($l1 lt $l2) {
+	return -1;
+    }
+    if($l1 gt $l2) {
+	return  1;
+    }
+
+    # Break the tie on the first name, but there are leading (possibly trailing
+    # whitespaces to get rid of first 
+    #
+    $f1 =~ s/^\s+//;		# Remove leading...
+    $f1 =~ s/\s+$//;		# Trailing spaces from first 1...
+    
+    $f2 =~ s/^\s+//;
+    $f2 =~ s/\s+$//;		# And the same for first 2...
+
+    if($f1 lt $f2) {
+	return -1;
+    }
+    if($f1 gt $f2) {
+	return 1;
+    }
+    
+    #  Must be the same name.
+
+    return 0;
+}
+
 sub latex_header_footer_remove {
     my $text = shift;
     $text =~ s/\\end{document}//;
@@ -1057,6 +1115,16 @@
 	     $type='resources';
 	 }
 	 my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
+	 #   The normal sort order is by section then by students within the
+	 #   section. If the helper var student_sort is 1, then the user has elected
+	 #   to override this and output the students by name.
+	 #    Each element of the students array is of the form:
+	 #       username:domain:section:last, first:status
+	 #    
+	 #   
+	 if ($helper->{'VARS'}->{'student_sort'}    eq 1) {
+	     @students = sort compare_names  @students;
+	 }
          if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq '0' ||
 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'all' ) {
 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'}=$#students+1;