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

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

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