--- loncom/interface/lonselstudent.pm	2006/05/23 21:41:26	1.7
+++ loncom/interface/lonselstudent.pm	2009/01/15 18:23:27	1.14
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # lonselstudent.pm : Reusable subs for student selection.
 #
-# $Id: lonselstudent.pm,v 1.7 2006/05/23 21:41:26 foxr Exp $
+# $Id: lonselstudent.pm,v 1.14 2009/01/15 18:23:27 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -28,8 +28,9 @@
 
 package Apache::lonselstudent;
 use     Apache::lonnet;
-use     Apache::loncoursedata;
-use     HTML::Entities;
+use     Apache::lonlocal;
+use     Apache::loncoursedata();
+use     HTML::Entities();
 
 #
 #  Utility function used when rendering <student> tags.
@@ -110,12 +111,10 @@ sub get_people_in_class {
 
     my $classlist = &Apache::loncoursedata::get_classlist();
     my @keys = keys(%{$classlist});
-    # Sort by: Section, name
+    # Sort by: fullname, username
     @keys = sort {
-        if ($classlist->{$a}->[$section] ne $classlist->{$b}->[$section]) {
-            return $classlist->{$a}->[$section] cmp $classlist->{$b}->[$section];
-        }
-        return $classlist->{$a}->[$fullname] cmp $classlist->{$b}->[$fullname];
+	lc($classlist->{$a}[$fullname]) cmp lc($classlist->{$b}[$fullname]) ||
+	lc($a) cmp lc($b)
     } (@keys);
  
 
@@ -179,25 +178,30 @@ sub get_people_in_class {
 #                     This should be true for the first call for a page
 #                     and false for all other calls... only matters if
 #                     multiselect is true.
+#     $context      - If email, do not include <br /><hr /> tags at the end
+#                     of the data table. 
 #  Returns:
 #     HTML  text to add to the rendering of the helper.
 #
 sub render_student_list {
     my ($students, $formname, $formprefix, $defaultusers,
-	$multiselect, $resultname, $javascript) = @_;
+	$multiselect, $resultname, $javascript, $context) = @_;
 
     my $result = "";
 
+    # no students so no output
+    return if (!@$students);
+
     if ($javascript && $multiselect) {
         $result .= <<SCRIPT;
 <script type="text/javascript">
 // <!--
 
-    function findElement(name) {
+    function findElement(name,formname) {
 	var i;
 	var ele;
-	for(i =0; i < document.forms.$formname.elements.length; i++) {
-	    ele = document.forms.$formname.elements[i];
+	for(i =0; i < formname.elements.length; i++) {
+	    ele = formname.elements[i];
 	    if(ele.name == name) {
 		return ele;
 	    }
@@ -228,31 +232,31 @@ sub render_student_list {
 	}
     }
 
-    function setAllStudents(value, which) {
+    function setAllStudents(value, which, formname) {
 	var i;
 	var ele;
-	for (i =0; i < document.forms.$formname.elements.length; i++) {
-	    ele = document.forms.$formname.elements[i];
+	for (i =0; i < formname.elements.length; i++) {
+	    ele = formname.elements[i];
 	    if(isStudent(ele) && rightSubForm(ele, which)) {
 		ele.checked=value;
 	    }
 	}
     }
-    function setAllCoursePersonnel(value, which) {
+    function setAllCoursePersonnel(value, which, formname) {
 	var i;
 	var ele;
-	for (i =0; i < document.forms.$formname.elements.length; i++) {
-	    ele = document.forms.$formname.elements[i];
+	for (i =0; i < formname.elements.length; i++) {
+	    ele = formname.elements[i];
 	    if(!isStudent(ele) && rightSubForm(ele, which)) {
 		ele.checked = value;
 	    }
 	}
     }
-    function setSection(which, value, subform) {
+    function setSection(which, value, subform, formname) {
 	var i;
 	var ele;
-	for (i =0; i < document.forms.$formname.elements.length; i++) {
-	    ele = document.forms.$formname.elements[i];
+	for (i =0; i < formname.elements.length; i++) {
+	    ele = formname.elements[i];
 	    if (ele.value.indexOf(':') != -1) {
 		if ((section(ele) == which) && rightSubForm(ele, subform)) {
 		    ele.checked = value;
@@ -261,34 +265,34 @@ sub render_student_list {
 	}
     }
 
-    function setCheckboxes(listbox, which, value) {
+    function setCheckboxes(listbox, which, value, formname) {
 	var k;
 	var elem;
 	var what;
-        elem = findElement(listbox);
+        elem = findElement(listbox, formname);
 	if (elem != null) {
 	    for (k = 0; k < elem.length; k++) {
 		if (elem.options[k].selected) {
-		    what = elem.options[k].text;
-		    if (what == 'All Students') {
-			setAllStudents(value, which);
-		    } else if (what == 'All Course Personnel') {
-			setAllCoursePersonnel(value, which);
-		    } else if (what == 'No Section') {
-			setSection('',value, which);
+		    what = elem.options[k].value;
+		    if (what == 'allstudents') {
+			setAllStudents(value, which, formname);
+		    } else if (what == 'allpersonnel') {
+			setAllCoursePersonnel(value, which, formname);
+		    } else if (what == 'nosection') {
+			setSection('',value, which, formname);
 		    } else {
-			setSection(what, value, which);
+			setSection(what, value, which, formname);
 		    }
 		}
 	    }
 	}
     }
-    function selectSections(listbox, which) {
-	setCheckboxes(listbox, which, true);
+    function selectSections(listbox, which, formname) {
+	setCheckboxes(listbox, which, true, formname);
 
     }
-    function unselectSections(listbox, which) {
-	setCheckboxes(listbox, which, false);
+    function unselectSections(listbox, which, formname) {
+	setCheckboxes(listbox, which, false, formname);
     }
 
 // -->
@@ -319,21 +323,21 @@ SCRIPT
 	if ($size > 5) { 
 	    $size = 5; 
 	}
-	$result .= '<select multiple name="'.$formprefix
+	$result .= '<select multiple="multiple" name="'.$formprefix
 	    .'.chosensections" size="'.$size.'">'."\n";
-	$result .= '<option name="allstudents">All Students</option>';
-	$result .= '<option name="allpersonnel">All Course Personnel</option>';
-	$result .= '<option name="nosection">No Section</option>';
+	$result .= '<option value="allstudents">'.&mt('All Students').'</option>';
+	$result .= '<option value="allpersonnel">'.&mt('All Course Personnel').'</option>';
+	$result .= '<option value="nosection">'.&mt('No Section').'</option>';
 	$result .= "\n";
 	foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
 	    $result .= '<option name="'.$sec.'">'.$sec.'</option>'."\n";
 	}
-	$result .= '</td><td valign="top">';
-	$result .= '<input type="button" name="'.$formprefix.'.select" value="Select" onclick='
-	    ."'selectSections(\"$formprefix.chosensections\", \"$formprefix\")'".' /></td>';
+	$result .= '</select></td><td valign="top">';
+	$result .= '<input type="button" name="'.$formprefix.'.select" value="'.&mt('Select').'" onclick='
+	    ."'selectSections(\"$formprefix.chosensections\", \"$formprefix\", document.forms.$formname)'".' /></td>';
 	$result .= '<td valign="top"><input type="button" name="'.$formprefix
-	    .'.unselect" value="Unselect"  onclick='.
-	    "'unselectSections(\"$formprefix.chosensections\", \"$formprefix\")' ".' /></td></tr></table>';
+	    .'.unselect" value="'.&mt('Unselect').'"  onclick='.
+	    "'unselectSections(\"$formprefix.chosensections\", \"$formprefix\", document.forms.$formname)' ".' /></td></tr></table>';
     }
 
     #  Now we list the students, but the form element type
@@ -343,11 +347,11 @@ SCRIPT
 
     $result .= &Apache::loncommon::start_data_table();
     $result .= &Apache::loncommon::start_data_table_header_row();
-    $result .= '<th></th><th>Name</th>'."\n";
-    $result .= '    <th>Section</th>'."\n";
-    $result .= '    <th>Status</th>'."\n";
-    $result .= '    <th>Role</th>'."\n";
-    $result .= '    <th>Username : Domain</th>'."\n";
+    $result .= '<th></th><th>'.&mt('Name').'</th>'."\n";
+    $result .= '    <th>'.&mt('Section').'</th>'."\n";
+    $result .= '    <th>'.&mt('Status').'</th>'."\n";
+    $result .= '    <th>'.&mt('Role').'</th>'."\n";
+    $result .= '    <th>'.&mt('Username : Domain').'</th>'."\n";
     $result .= &Apache::loncommon::end_data_table_header_row();
 
     my $input_type;
@@ -361,7 +365,7 @@ SCRIPT
     for my $student (@$students) {
 	$result .= &Apache::loncommon::start_data_table_row().
 	    '<td><input type="'.$input_type.'"  name="'.
-	    $resultname.".forminput".'"';
+	    $resultname."_forminput".'"';
 	my $user    = $student->[0];
 
 	# Figure out which students are checked by default...
@@ -394,8 +398,10 @@ SCRIPT
 	        . '</td>'.&Apache::loncommon::end_data_table_row().
 		"\n";
     }
-    $result .= &Apache::loncommon::end_data_table().
-	" <br /> <hr />\n";
+    $result .= &Apache::loncommon::end_data_table();
+    if ($context ne 'email') {
+	$result .= "<br /> <hr />\n";
+    }
 
     return $result;
 }