Annotation of loncom/interface/lonselstudent.pm, revision 1.10

1.1       foxr        1: # The LearningOnline Network with CAPA
                      2: # lonselstudent.pm : Reusable subs for student selection.
                      3: #
1.10    ! albertel    4: # $Id: lonselstudent.pm,v 1.9 2006/07/17 16:26:08 raeburn Exp $
1.1       foxr        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: #
                     28: 
                     29: package Apache::lonselstudent;
                     30: use     Apache::lonnet;
1.10    ! albertel   31: use     Apache::lonlocal;
        !            32: use     Apache::loncoursedata();
        !            33: use     HTML::Entities();
1.1       foxr       34: 
                     35: #
                     36: #  Utility function used when rendering <student> tags.
                     37: #  This function produces a list references to four
                     38: #  arrays:
                     39: #    (\@course_personel, \@current_members, \@expired_members, \@future_members)
1.7       foxr       40: #
                     41: #
                     42: # Parameters;
                     43: #
                     44: #  restrict           - Optional.. if present and defined should be a section name.
                     45: #                       The *_members arrays will then only contain people
                     46: #                       in that section
                     47: #
1.1       foxr       48: # Where:
                     49: #    course_personnel - Each element of this array is itself a reference to an array
                     50: #                      containing information about a member of the course staff.
                     51: #    current_members  - Each element of this array is itself a reference to an
                     52: #                       array that contains information about current students in
                     53: #                       the course.
                     54: #    expired_members  - Each element of this array is itself a reference to an
                     55: #                       array that contains information about students whose 
                     56: #                       status has expired.
                     57: #    future_members   - Each element of this arrya is itself a reference to an
                     58: #                       array that contains information about students who will
                     59: #                       become active at a future date.
                     60: #
                     61: #  Course personnel elements include:
                     62: #       [0]    Last, First of the user.
                     63: #       [1]    Role held by the user.
                     64: #       [2]    Empty.
                     65: #       [3]    Empty
                     66: #       [4]    username:domain of the user.
                     67: #
                     68: #  Student member array elements are:
                     69: #       [0]    Last, First of the user.
                     70: #       [1]    Status of the user one of ("Active", "Future", or "Expired')
                     71: #              depending on which array the user was put in.
                     72: #       [2]    Section the student is in.
                     73: #       [3]    Role of the member (student).
                     74: #       [4]    username:domain of the user.
                     75: #
                     76: sub get_people_in_class {
1.7       foxr       77:     my ($section_restriction) = @_;
1.1       foxr       78:     my %coursepersonnel = &Apache::lonnet::get_course_adv_roles();
                     79:     #
                     80:     #  Enumerate the course_personnel.
                     81:     #
                     82:     my @course_personnel;
1.5       albertel   83:     for my $role (sort(keys(%coursepersonnel))) {
                     84: 	# extract the names so we can sort them
                     85: 	my @people;
                     86: 	for my $person (split(/,/, $coursepersonnel{$role})) {
                     87: 	    my ($uname,$domain) = split(/:/, $person);
                     88: 	    push(@people, [&Apache::loncommon::plainname($uname,$domain),
                     89: 			   $uname,$domain]);
                     90: 	}
                     91: 	@people = sort { $a->[0] cmp $b->[0] } (@people);
1.1       foxr       92: 	    
1.5       albertel   93: 	for my $person (@people) {
                     94: 	    push(@course_personnel, [join(':', $person->[1],$person->[2]), 
                     95: 				     $person->[0], '', '', $role]);
1.1       foxr       96: 	}
                     97:     }
                     98:     #  Students must be split into the three categories:
                     99: 
                    100:     my @current_members;
                    101:     my @future_members;
                    102:     my @expired_members;
                    103: 
                    104:     #   Indices into the coures data elements.
                    105: 
                    106:     my $section    = &Apache::loncoursedata::CL_SECTION();
                    107:     my $fullname   = &Apache::loncoursedata::CL_FULLNAME();
                    108:     my $status     = &Apache::loncoursedata::CL_STATUS();
                    109:     my $start_date = &Apache::loncoursedata::CL_START();
                    110: 
                    111: 
                    112:     my $classlist = &Apache::loncoursedata::get_classlist();
1.4       albertel  113:     my @keys = keys(%{$classlist});
1.1       foxr      114:     # Sort by: Section, name
                    115:     @keys = sort {
1.8       albertel  116: 	lc($classlist->{$a}[$section])  cmp lc($classlist->{$b}[$section])  ||
                    117: 	lc($classlist->{$a}[$fullname]) cmp lc($classlist->{$b}[$fullname]) ||
                    118: 	lc($a) cmp lc($b)
1.4       albertel  119:     } (@keys);
1.1       foxr      120:  
                    121: 
                    122: 
                    123: 
1.6       albertel  124:     for my $user (@keys) {
1.7       foxr      125: 	if (!$section_restriction || 
                    126: 	    ($section_restriction eq $classlist->{$user}->[$section])) {
1.1       foxr      127: 	    
1.7       foxr      128: 	    if ( $classlist->{$user}->[$status] eq
                    129: 		 'Active') {
                    130: 		push(@current_members, [$user, $classlist->{$user}->[$fullname], 
1.6       albertel  131: 					$classlist->{$user}->[$section],
1.7       foxr      132: 					$classlist->{$user}->[$status], 'Student']);
1.1       foxr      133: 	    } else {
1.7       foxr      134: 		#  Need to figure out if this user is future or
                    135: 		#  Expired... If the start date is in the future
                    136: 		#  the user is future...else expired.
                    137: 		
                    138: 		my $now = time;
                    139: 		if ($classlist->{$user}->[$start_date] > $now) {
                    140: 		    push(@future_members, [$user, $classlist->{$user}->[$fullname],
                    141: 					   $classlist->{$user}->[$section],
                    142: 					   "Future", "Student"]);
                    143: 		} else {
                    144: 		    push(@expired_members, [$user,
                    145: 					    $classlist->{$user}->[$fullname],
                    146: 					    $classlist->{$user}->[$section],
                    147: 					    "Expired", "Student"]);
                    148: 		}
                    149: 		
1.1       foxr      150: 	    }
                    151: 	}
                    152:     }
                    153:     return (\@course_personnel, 
                    154: 	    \@current_members,
                    155: 	    \@expired_members,
                    156: 	    \@future_members);
                    157: 
                    158: }
                    159: 
                    160: #
                    161: #  Utility function used when rendering the <student> tag.
                    162: #  This function renders a segment of course personel
                    163: #  Personel are broken up by the helper into past, current and
                    164: #  future...each one gets is own subpage of selection.
                    165: #  This sub renders one of these pages.
                    166: #  Parameters:
                    167: #     $students    - Students in the section. (ref to array of references
                    168: #                    to arrays).
1.2       foxr      169: #     $formname    - Name of the form in which this stuff gets rendered.
1.1       foxr      170: #     $formprefix  - form path prefix for form element names
                    171: #                    This is used to make each form element
                    172: #                    so that the segments having to do with each
                    173: #                    set of students won't collide.
                    174: #     $defaultusers - reference to a hash containng
                    175: #                     the set of users that should be on or off.
                    176: #     $multiselect  - True if multiselect allowed.
                    177: #     $resultname   - Name of result variable.
                    178: #     $javascript   - If true, the javascript to run this is output
                    179: #                     This should be true for the first call for a page
                    180: #                     and false for all other calls... only matters if
                    181: #                     multiselect is true.
                    182: #  Returns:
                    183: #     HTML  text to add to the rendering of the helper.
                    184: #
                    185: sub render_student_list {
1.2       foxr      186:     my ($students, $formname, $formprefix, $defaultusers,
1.1       foxr      187: 	$multiselect, $resultname, $javascript) = @_;
                    188: 
                    189:     my $result = "";
                    190: 
1.8       albertel  191:     # no students so no output
                    192:     return if (!@$students);
                    193: 
1.1       foxr      194:     if ($javascript && $multiselect) {
                    195:         $result .= <<SCRIPT;
                    196: <script type="text/javascript">
                    197: // <!--
                    198: 
                    199:     function findElement(name) {
                    200: 	var i;
                    201: 	var ele;
1.2       foxr      202: 	for(i =0; i < document.forms.$formname.elements.length; i++) {
                    203: 	    ele = document.forms.$formname.elements[i];
1.1       foxr      204: 	    if(ele.name == name) {
                    205: 		return ele;
                    206: 	    }
                    207: 	}
                    208: 	return null;
                    209:     }
                    210:     function isStudent(element) {
                    211: 	if(element.value.indexOf(":Student") != -1) {
                    212: 	    return 1;
                    213: 	}
                    214: 	return 0;
                    215:     }
                    216:     function section(element) {
                    217: 	var i;
                    218: 	var info;
                    219: 	if (element.value.indexOf(':') != -1) {
                    220: 	    info = element.value.split(':');
                    221: 	    return info[2];
                    222: 	} else {
                    223: 	    return "";
                    224: 	}
                    225:     }
                    226:     function rightSubForm(element, which) {
                    227: 	if (element.value.indexOf(which) != -1) {
                    228: 	    return true;
                    229: 	} else {
                    230: 	    return false;
                    231: 	}
                    232:     }
                    233: 
                    234:     function setAllStudents(value, which) {
                    235: 	var i;
                    236: 	var ele;
1.2       foxr      237: 	for (i =0; i < document.forms.$formname.elements.length; i++) {
                    238: 	    ele = document.forms.$formname.elements[i];
1.1       foxr      239: 	    if(isStudent(ele) && rightSubForm(ele, which)) {
                    240: 		ele.checked=value;
                    241: 	    }
                    242: 	}
                    243:     }
                    244:     function setAllCoursePersonnel(value, which) {
                    245: 	var i;
                    246: 	var ele;
1.2       foxr      247: 	for (i =0; i < document.forms.$formname.elements.length; i++) {
                    248: 	    ele = document.forms.$formname.elements[i];
1.1       foxr      249: 	    if(!isStudent(ele) && rightSubForm(ele, which)) {
                    250: 		ele.checked = value;
                    251: 	    }
                    252: 	}
                    253:     }
                    254:     function setSection(which, value, subform) {
                    255: 	var i;
                    256: 	var ele;
1.2       foxr      257: 	for (i =0; i < document.forms.$formname.elements.length; i++) {
                    258: 	    ele = document.forms.$formname.elements[i];
1.1       foxr      259: 	    if (ele.value.indexOf(':') != -1) {
                    260: 		if ((section(ele) == which) && rightSubForm(ele, subform)) {
                    261: 		    ele.checked = value;
                    262: 		}
                    263: 	    }
                    264: 	}
                    265:     }
                    266: 
                    267:     function setCheckboxes(listbox, which, value) {
                    268: 	var k;
                    269: 	var elem;
                    270: 	var what;
                    271:         elem = findElement(listbox);
                    272: 	if (elem != null) {
                    273: 	    for (k = 0; k < elem.length; k++) {
                    274: 		if (elem.options[k].selected) {
1.10    ! albertel  275: 		    what = elem.options[k].value;
        !           276: 		    if (what == 'allstudents') {
1.1       foxr      277: 			setAllStudents(value, which);
1.10    ! albertel  278: 		    } else if (what == 'allpersonnel') {
1.1       foxr      279: 			setAllCoursePersonnel(value, which);
1.10    ! albertel  280: 		    } else if (what == 'nosection') {
1.1       foxr      281: 			setSection('',value, which);
                    282: 		    } else {
                    283: 			setSection(what, value, which);
                    284: 		    }
                    285: 		}
                    286: 	    }
                    287: 	}
                    288:     }
                    289:     function selectSections(listbox, which) {
                    290: 	setCheckboxes(listbox, which, true);
                    291: 
                    292:     }
                    293:     function unselectSections(listbox, which) {
                    294: 	setCheckboxes(listbox, which, false);
                    295:     }
                    296: 
                    297: // -->
                    298: </script>
                    299: SCRIPT
                    300: 
                    301:     }
                    302: 
                    303:     # If multiple selections are allowed, we have a listbox
                    304:     # at the top which allows quick selections from each section
                    305:     # as well as from categories of personnel.
                    306: 
                    307:     if ($multiselect) {
                    308: 	#  Make a section hash so we can add sections to the choice:
                    309: 
                    310: 	my %sections;
                    311: 	for my $student (@$students) {
                    312: 	    my $sect = $student->[2];
                    313: 	    if ($sect ne "") {
                    314: 		$sections{$sect} = 1;
                    315: 	    }
                    316: 	}
                    317: 
                    318: 	$result .= '<table><tr><td>';
                    319: 
                    320: 	my $size = scalar(keys(%sections));
                    321: 	$size += 3;		# We have allstudents allpersonel nosection too.
                    322: 	if ($size > 5) { 
                    323: 	    $size = 5; 
                    324: 	}
                    325: 	$result .= '<select multiple name="'.$formprefix
                    326: 	    .'.chosensections" size="'.$size.'">'."\n";
1.10    ! albertel  327: 	$result .= '<option value="allstudents">'.&mt('All Students').'</option>';
        !           328: 	$result .= '<option value="allpersonnel">'.&mt('All Course Personnel').'</option>';
        !           329: 	$result .= '<option value="nosection">'.&mt('No Section').'</option>';
1.1       foxr      330: 	$result .= "\n";
                    331: 	foreach my $sec (sort {lc($a) cmp lc($b)} (keys(%sections))) {
                    332: 	    $result .= '<option name="'.$sec.'">'.$sec.'</option>'."\n";
                    333: 	}
                    334: 	$result .= '</td><td valign="top">';
1.10    ! albertel  335: 	$result .= '<input type="button" name="'.$formprefix.'.select" value="'.'Select" onclick='
1.1       foxr      336: 	    ."'selectSections(\"$formprefix.chosensections\", \"$formprefix\")'".' /></td>';
                    337: 	$result .= '<td valign="top"><input type="button" name="'.$formprefix
1.10    ! albertel  338: 	    .'.unselect" value="'.&mt('Unselect').'"  onclick='.
1.1       foxr      339: 	    "'unselectSections(\"$formprefix.chosensections\", \"$formprefix\")' ".' /></td></tr></table>';
                    340:     }
                    341: 
                    342:     #  Now we list the students, but the form element type
                    343:     #  will depend on whether or not multiselect is true.
                    344:     #  True -> checkboxes.
                    345:     #  False -> radiobuttons.
                    346: 
1.3       albertel  347:     $result .= &Apache::loncommon::start_data_table();
                    348:     $result .= &Apache::loncommon::start_data_table_header_row();
1.10    ! albertel  349:     $result .= '<th></th><th>'.&mt('Name').'</th>'."\n";
        !           350:     $result .= '    <th>'.&mt('Section').'</th>'."\n";
        !           351:     $result .= '    <th>'.&mt('Status').'</th>'."\n";
        !           352:     $result .= '    <th>'.&mt('Role').'</th>'."\n";
        !           353:     $result .= '    <th>'.&mt('Username : Domain').'</th>'."\n";
1.3       albertel  354:     $result .= &Apache::loncommon::end_data_table_header_row();
1.1       foxr      355: 
                    356:     my $input_type;
                    357:     if ($multiselect) {
                    358: 	$input_type = "checkbox";
                    359:     } else {
                    360: 	$input_type = "radio";
                    361:     }
                    362: 
                    363:     my $checked = 0;
                    364:     for my $student (@$students) {
1.3       albertel  365: 	$result .= &Apache::loncommon::start_data_table_row().
                    366: 	    '<td><input type="'.$input_type.'"  name="'.
1.9       raeburn   367: 	    $resultname."_forminput".'"';
1.1       foxr      368: 	my $user    = $student->[0];
                    369: 
                    370: 	# Figure out which students are checked by default...
                    371: 	
1.4       albertel  372: 	if (%$defaultusers) {
1.1       foxr      373: 	    if (exists ($defaultusers->{$user})) {
                    374: 		$result .= ' checked ="checked" ';
                    375: 		$checked = 1;
                    376: 	    }
                    377: 	} elsif (!$multiselect  && !$checked) {
                    378: 	    $result .= ' checked="checked" ';
                    379: 	    $checked = 1;	# First one for radio if no default specified.
                    380: 	}
1.4       albertel  381: 	$result .= ' value="'.&HTML::Entities::encode($user .          ':'
                    382: 				 		      .$student->[2] . ':'
1.1       foxr      383: 						      .$student->[1] . ':'
                    384: 						      .$student->[3] . ':'
                    385: 						      .$student->[4] . ":"
                    386: 						      .$formprefix,   "<>&\"'")
                    387: 	    ."\" /></td><td>\n";
1.4       albertel  388: 	$result .= &HTML::Entities::encode($student->[1], '<>&"')
1.1       foxr      389: 	        . '</td><td align="center" >'."\n";
1.4       albertel  390: 	$result .= &HTML::Entities::encode($student->[2], '<>&"')
1.1       foxr      391:    	        . '</td><td align="center">'."\n";
1.4       albertel  392: 	$result .= &HTML::Entities::encode($student->[3], '<>&"')
1.1       foxr      393: 	        . '</td><td align="center">'."\n";
1.4       albertel  394: 	$result .= &HTML::Entities::encode($student->[4], '<>&"')
1.1       foxr      395:   	        . '</td><td align="center">'."\n";
1.4       albertel  396: 	$result .= &HTML::Entities::encode($student->[0], '<>&"')
1.3       albertel  397: 	        . '</td>'.&Apache::loncommon::end_data_table_row().
                    398: 		"\n";
1.1       foxr      399:     }
1.3       albertel  400:     $result .= &Apache::loncommon::end_data_table().
                    401: 	" <br /> <hr />\n";
1.1       foxr      402: 
                    403:     return $result;
                    404: }
                    405: 
                    406: 1;

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