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

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

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