--- loncom/interface/domainprefs.pm 2008/05/07 23:01:50 1.48 +++ loncom/interface/domainprefs.pm 2008/05/08 22:13:32 1.49 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler to set domain-wide configuration settings # -# $Id: domainprefs.pm,v 1.48 2008/05/07 23:01:50 raeburn Exp $ +# $Id: domainprefs.pm,v 1.49 2008/05/08 22:13:32 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -370,7 +370,11 @@ sub process_changes { sub print_config_box { my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_; my $rowtotal = 0; - my $output = + my $output; + if ($action eq 'coursecategories') { + $output = &coursecategories_javascript($settings); + } + $output .= ''; $itemcount ++; } $css_class = $itemcount%2?' class="LC_odd_row"':''; - $datatable .= '' + $datatable .= '' .&initialize_categories($itemcount); } $$rowtotal += $itemcount; return $datatable; } +sub coursecategories_javascript { + my ($settings) = @_; + my ($output,$jstext); + if (ref($settings) eq 'HASH') { + my (@cats,@jsarray,%idx); + &gather_categories($settings,\@cats,\%idx,\@jsarray); + if (@jsarray > 0) { + $jstext = ' var categories = Array('.scalar(@jsarray).');'."\n"; + for (my $i=0; $i<@jsarray; $i++) { + if (ref($jsarray[$i]) eq 'ARRAY') { + my $catstr = join('","',@{$jsarray[$i]}); + $jstext .= ' categories['.$i.'] = Array("'.$catstr.'");'."\n"; + } + } + } + } else { + $jstext = ' var categories = Array(1);'."\n". + ' categories[0] = Array("instcode_pos");'."\n"; + } + $output = <<"ENDSCRIPT"; + + +ENDSCRIPT + return $output; +} + sub initialize_categories { my ($itemcount) = @_; my $datatable; my $css_class = $itemcount%2?' class="LC_odd_row"':''; + my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','instcode_pos','0'".');"'; + $datatable = ''; $itemcount ++; $css_class = $itemcount%2?' class="LC_odd_row"':''; + $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"'; $datatable .= ''; @@ -2061,8 +2155,8 @@ sub initialize_categories { } sub build_category_rows { - my ($itemcount,$cats,$depth,$parent,$path) = @_; - my ($text,$name,$item); + my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_; + my ($text,$name,$item,$chgstr); if (ref($cats) eq 'ARRAY') { my $maxdepth = scalar(@{$cats}); if (ref($cats->[$depth]) eq 'HASH') { @@ -2070,22 +2164,27 @@ sub build_category_rows { my $numchildren = @{$cats->[$depth]{$parent}}; my $css_class = $itemcount%2?' class="LC_odd_row"':''; $text .= '
'.&mt($item->{text}). @@ -1935,15 +1939,14 @@ sub legacy_scantronformat { return ($url,$error); } -sub print_coursecategories { - my ($dom,$item,$settings,$rowtotal) = @_; +sub print_coursecategories { + my ($dom,$hdritem,$settings,$rowtotal) = @_; my ($datatable,$css_class); my $itemcount = 1; - # FIXME Need to add javascrpt to update other select boxes when one is changed. if (ref($settings) eq 'HASH') { - my (@cats,@trails,%allitems); - &extract_categories($settings,\@cats,\@trails,\%allitems); - my $maxdepth = scalar(@cats); + my (@cats,@trails,%allitems,%idx,@jsarray); + &extract_categories($settings,\@cats,\@trails,\%allitems,\%idx,\@jsarray); + my $maxdepth = scalar(@cats); my $colattrib = ''; if ($maxdepth > 2) { $colattrib = ' colspan="2" '; @@ -1956,12 +1959,15 @@ sub print_coursecategories { if ((!grep(/^instcode$/,@{$cats[0]})) || ($settings->{'instcode::0'} eq '')) { $maxnum ++; } + my $lastidx; for (my $i=0; $i<$numtop; $i++) { my $parent = $cats[0][$i]; $css_class = $itemcount%2?' class="LC_odd_row"':''; my $item = &escape($parent).'::0'; + my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"'; + $lastidx = $idx{$item}; $datatable .= '
' - .''; for (my $k=0; $k<=$maxnum; $k++) { my $vpos = $k+1; my $selstr; @@ -1986,13 +1992,14 @@ sub print_coursecategories { } my $depth = 1; push(@path,$parent); - $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path); + $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx); pop(@path); $datatable .= '
'. - ''; for (my $k=0; $k<=$maxnum; $k++) { my $vpos = $k+1; my $selstr; @@ -2030,17 +2038,102 @@ sub print_coursecategories { $datatable .= &initialize_categories($itemcount); } } else { - $datatable .= ''.$item->{'header'}->[0]->{'col2'}.''.$hdritem->{'header'}->[0]->{'col2'}.'
' .' ' @@ -2052,8 +2145,9 @@ sub initialize_categories { .'
' - .'' .' ' .&mt('Add category').''.&mt('Name:') .' 
'; + my ($idxnum,$parent_name,$parent_item); + my $higher = $depth - 1; + if ($higher == 0) { + $parent_name = &escape($parent).'::'.$higher; + } else { + if (ref($path) eq 'ARRAY') { + $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher; + } + } + $parent_item = 'addcategory_pos_'.$parent_name; for (my $j=0; $j<=$numchildren; $j++) { - if ($j == $numchildren) { - my $higher = $depth-1; - if ($higher == 0) { - $name = &escape($parent).'::'.$higher; - } else { - if (ref($path) eq 'ARRAY') { - $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher; - } - } - $item = 'addcategory_pos_'.$name; - } else { + if ($j < $numchildren) { $name = $cats->[$depth]{$parent}[$j]; $item = &escape($name).':'.&escape($parent).':'.$depth; + $idxnum = $idx->{$item}; + } else { + $name = $parent_name; + $item = $parent_item; } - $text .= '
'; if(ref($path) eq 'ARRAY') { push(@{$path},$name); - $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path); + $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx); pop(@{$path}); } } else { @@ -4226,22 +4325,41 @@ sub recurse_cat_deletes { return; } +sub gather_categories { + my ($categories,$cats,$idx,$jsarray) = @_; + my %counters; + my $num = 0; + foreach my $item (keys(%{$categories})) { + my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item); + if ($container eq '' && $depth == 0) { + $cats->[$depth][$categories->{$item}] = $cat; + } else { + $cats->[$depth]{$container}[$categories->{$item}] = $cat; + } + my ($escitem,$tail) = split(/:/,$item,2); + if ($counters{$tail} eq '') { + $counters{$tail} = $num; + $num ++; + } + if (ref($idx) eq 'HASH') { + $idx->{$item} = $counters{$tail}; + } + if (ref($jsarray) eq 'ARRAY') { + push(@{$jsarray->[$counters{$tail}]},$item); + } + } + return; +} + sub extract_categories { - my ($categories,$cats,$trails,$allitems) = @_; + my ($categories,$cats,$trails,$allitems,$idx,$jsarray) = @_; if (ref($categories) eq 'HASH') { - foreach my $item (keys(%{$categories})) { - my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item); - if ($container eq '' && $depth == 0) { - $cats->[$depth][$categories->{$item}] = $cat; - } else { - $cats->[$depth]{$container}[$categories->{$item}] = $cat; - } - } + &gather_categories($categories,$cats,$idx,$jsarray); if (ref($cats->[0]) eq 'ARRAY') { for (my $i=0; $i<@{$cats->[0]}; $i++) { my $name = $cats->[0][$i]; my $item = &escape($name).'::0'; - my $trailstr; + my $trailstr; if ($name eq 'instcode') { $trailstr = &mt('Official courses (with institutional codes)'); } else { @@ -4266,9 +4384,9 @@ sub extract_categories { sub recurse_categories { my ($cats,$depth,$category,$trails,$allitems,$parents) = @_; + my $shallower = $depth - 1; if (ref($cats->[$depth]{$category}) eq 'ARRAY') { for (my $k=0; $k<@{$cats->[$depth]{$category}}; $k++) { - my $shallower = $depth - 1; my $name = $cats->[$depth]{$category}[$k]; my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower; my $trailstr = join(' -> ',(@{$parents},$category)); @@ -4282,8 +4400,7 @@ sub recurse_categories { pop(@{$parents}); } } else { - $depth --; - my $item = &escape($category).':'.&escape($parents->[-1]).':'.$depth; + my $item = &escape($category).':'.&escape($parents->[-1]).':'.$shallower; my $trailstr = join(' -> ',(@{$parents},$category)); if ($allitems->{$item} eq '') { push(@{$trails},$trailstr);