File:  [LON-CAPA] / loncom / interface / lonselstudent.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu May 11 21:10:21 2006 UTC (18 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
Separated the stuff to render student selection lists into a separate
pacakge: lonselstudent.

    1: # The LearningOnline Network with CAPA
    2: # lonselstudent.pm : Reusable subs for student selection.
    3: #
    4: # $Id: lonselstudent.pm,v 1.1 2006/05/11 21:10:21 foxr Exp $
    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;
   74:     for (sort keys %coursepersonnel) {
   75: 	for my $role (split /,/, $coursepersonnel{$_}) {
   76: 	    # extract the names so we can sort them
   77: 	    my @people;
   78: 	    
   79: 	    for (split /,/, $role) {
   80: 		push @people, [split /:/, $role];
   81: 	    }
   82: 	    
   83: 	    @people = sort { $a->[0] cmp $b->[0] } @people;
   84: 	    
   85: 	    for my $person (@people) {
   86: 		push @course_personnel, [join(':', @$person), $person->[0], '', $_];
   87: 	    }
   88: 	}
   89:     }
   90:     #  Students must be split into the three categories:
   91: 
   92:     my @current_members;
   93:     my @future_members;
   94:     my @expired_members;
   95: 
   96:     #   Indices into the coures data elements.
   97: 
   98:     my $section    = &Apache::loncoursedata::CL_SECTION();
   99:     my $fullname   = &Apache::loncoursedata::CL_FULLNAME();
  100:     my $status     = &Apache::loncoursedata::CL_STATUS();
  101:     my $start_date = &Apache::loncoursedata::CL_START();
  102: 
  103: 
  104:     my $classlist = &Apache::loncoursedata::get_classlist();
  105:     my @keys = keys %{$classlist};
  106:     # Sort by: Section, name
  107:     @keys = sort {
  108:         if ($classlist->{$a}->[$section] ne $classlist->{$b}->[$section]) {
  109:             return $classlist->{$a}->[$section] cmp $classlist->{$b}->[$section];
  110:         }
  111:         return $classlist->{$a}->[$fullname] cmp $classlist->{$b}->[$fullname];
  112:     } @keys;
  113:  
  114: 
  115: 
  116: 
  117:     for (@keys) {
  118: 
  119: 	if ( $classlist->{$_}->[$status] eq
  120: 	    'Active') {
  121: 	    push @current_members, [$_, $classlist->{$_}->[$fullname], 
  122: 			     $classlist->{$_}->[$section],
  123: 			     $classlist->{$_}->[$status], 'Student'];
  124: 	} else {
  125: 	    #  Need to figure out if this user is future or
  126: 	    #  Expired... If the start date is in the future
  127: 	    #  the user is future...else expired.
  128: 	    
  129: 	    my $now = time;
  130: 	    if ($classlist->{$_}->[$start_date] > $now) {
  131: 		push @future_members, [$_, $classlist->{$_}->[$fullname],
  132: 					$classlist->{$_}->[$section],
  133: 					"Future", "Student"];
  134: 	    } else {
  135: 		push @expired_members, [$_, $classlist->{$_}->[$fullname],
  136: 					$classlist->{$_}->[$section],
  137: 					"Expired", "Student"];
  138: 	    }
  139: 
  140: 	}
  141:     }
  142:     return (\@course_personnel, 
  143: 	    \@current_members,
  144: 	    \@expired_members,
  145: 	    \@future_members);
  146: 
  147: }
  148: 
  149: #
  150: #  Utility function used when rendering the <student> tag.
  151: #  This function renders a segment of course personel
  152: #  Personel are broken up by the helper into past, current and
  153: #  future...each one gets is own subpage of selection.
  154: #  This sub renders one of these pages.
  155: #  Parameters:
  156: #     $students    - Students in the section. (ref to array of references
  157: #                    to arrays).
  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 {
  174:     my ($students, $formprefix, $defaultusers,
  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;
  187: 	for(i =0; i < document.forms.helpform.elements.length; i++) {
  188: 	    ele = document.forms.helpform.elements[i];
  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;
  222: 	for (i =0; i < document.forms.helpform.elements.length; i++) {
  223: 	    ele = document.forms.helpform.elements[i];
  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;
  232: 	for (i =0; i < document.forms.helpform.elements.length; i++) {
  233: 	    ele = document.forms.helpform.elements[i];
  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;
  242: 	for (i =0; i < document.forms.helpform.elements.length; i++) {
  243: 	    ele = document.forms.helpform.elements[i];
  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: 
  332:     $result .= "<table border=\"2\">\n";
  333:     $result .= '<tr><th></th><th align="center">Name</th>'."\n";
  334:     $result .= '    <th align="center">Section</th>'."\n";
  335:     $result .= '    <th align="center">Status</th>'."\n";
  336:     $result .= '    <th align="center">Role</th>'."\n";
  337:     $result .= '    <th align="center">Username : Domain</th></tr>'."\n";
  338: 
  339:     my $input_type;
  340:     if ($multiselect) {
  341: 	$input_type = "checkbox";
  342:     } else {
  343: 	$input_type = "radio";
  344:     }
  345: 
  346:     my $checked = 0;
  347:     for my $student (@$students) {
  348: 	$result .= '<tr><td><input type="'.$input_type.'"  name="'.
  349: 	    $resultname.".forminput".'"';
  350: 	my $user    = $student->[0];
  351: 
  352: 	# Figure out which students are checked by default...
  353: 	
  354: 	if(%$defaultusers) {
  355: 	    if (exists ($defaultusers->{$user})) {
  356: 		$result .= ' checked ="checked" ';
  357: 		$checked = 1;
  358: 	    }
  359: 	} elsif (!$multiselect  && !$checked) {
  360: 	    $result .= ' checked="checked" ';
  361: 	    $checked = 1;	# First one for radio if no default specified.
  362: 	}
  363: 	$result .= ' value="'. HTML::Entities::encode($user .          ':'
  364: 						      .$student->[2] . ':'
  365: 						      .$student->[1] . ':'
  366: 						      .$student->[3] . ':'
  367: 						      .$student->[4] . ":"
  368: 						      .$formprefix,   "<>&\"'")
  369: 	    ."\" /></td><td>\n";
  370: 	$result .= HTML::Entities::encode($student->[1], '<>&"')
  371: 	        . '</td><td align="center" >'."\n";
  372: 	$result .= HTML::Entities::encode($student->[2], '<>&"')
  373:    	        . '</td><td align="center">'."\n";
  374: 	$result .= HTML::Entities::encode($student->[3], '<>&"')
  375: 	        . '</td><td align="center">'."\n";
  376: 	$result .= HTML::Entities::encode($student->[4], '<>&"')
  377:   	        . '</td><td align="center">'."\n";
  378: 	$result .= HTML::Entities::encode($student->[0], '<>&"')
  379: 	        . '</td></tr>'."\n";
  380:     }
  381:     $result .=" </table> <br /> <hr />\n";
  382: 
  383:     return $result;
  384: }
  385: 
  386: 1;

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