File:  [LON-CAPA] / loncom / interface / courseclassifier.pm
Revision 1.30: download - view: text, annotated - select for diffs
Mon Jul 10 01:55:15 2023 UTC (17 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_msu, HEAD
- Remove debugging line

# The LearningOnline Network with CAPA
# Utilities for classifying courses based on institutional code
#
# $Id: courseclassifier.pm,v 1.30 2023/07/10 01:55:15 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

package Apache::courseclassifier;

use strict;
use lib '/home/httpd/lib/perl/';
use Apache::lonnet;
use Apache::loncommon;
use Apache::lonlocal;
use LONCAPA;

sub sort_cats {
    my ($num,$cat_order,$codetitles,$idsarrayref,$sorted) = @_;
    my @unsorted = @{$idsarrayref};
    if (defined($$cat_order{$$codetitles[$num]})) {
        foreach my $item (@{$$cat_order{$$codetitles[$num]}}) {
            if (grep/^\Q$item\E$/,@unsorted) {
                push(@{$sorted},$item);
            }
        }
    } else {
        @{$sorted} = sort (@unsorted);
    }
}

sub recurse_list {
    my ($cid,$codetitles,$codes,$num,$idarrays) = @_;
    if ($num == 0) {
        if (!grep/^$$codes{$cid}{$$codetitles[0]}$/,@{$$idarrays{$$codetitles[0]}}) {
            push @{$$idarrays{$$codetitles[0]}}, $$codes{$cid}{$$codetitles[0]};
        }
    } elsif ($num == 1) {
        if (defined($$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}})) {
            if (!grep/^$$codes{$cid}{$$codetitles[1]}$/,@{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}}) {
                push @{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}}, $$codes{$cid}{$$codetitles[1]};
            }
        } else {
            @{$$idarrays{$$codetitles[1]}{$$codes{$cid}{$$codetitles[0]}}} = ("$$codes{$cid}{$$codetitles[1]}");
        }
    } elsif ($num == 2) {
        if (defined($$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}})) {
            if (defined($$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
                if (!grep/^$$codes{$cid}{$$codetitles[2]}$/,@{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}}) {
                    push @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}}, $$codes{$cid}{$$codetitles[2]};
                }
            } else {
                @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ("$$codes{$cid}{$$codetitles[2]}");
            }
        } else {
            %{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}} = ();
            @{$$idarrays{$$codetitles[2]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ("$$codes{$cid}{$$codetitles[2]}");
        }
    } elsif ($num == 3) {
        if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}})) {
            if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
                if (defined($$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}})) {
                    if (!grep/^$$codes{$cid}{$$codetitles[3]}$/,@{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}}) {
                        push @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}}, $$codes{$cid}{$$codetitles[3]};
                    }
                } else {
                    @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
                }
            } else {
                %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
                @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
            }
        } else {
            %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}} = ();
            %{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
            @{$$idarrays{$$codetitles[3]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ("$$codes{$cid}{$$codetitles[3]}");
        }
    } elsif ($num == 4) {
        if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}})) {
            if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}})) {
                if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}})) {
                    if (defined($$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}})) {
                        if (!grep/^$$codes{$cid}{$$codetitles[4]}$/,@{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}}) {
                            push @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}}, $$codes{$cid}{$$codetitles[4]};
                        }
                    } else {
                        @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
                    }
                } else {
                    %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
                    @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
                }
            } else {
                %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
                %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
                @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[4]}");
            }
        } else {
            %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}} = ();
            %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}} = ();
            %{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[2]}}} = ();
            @{$$idarrays{$$codetitles[4]}{$$codes{$cid}{$$codetitles[0]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[1]}}{$$codes{$cid}{$$codetitles[3]}}} = ("$$codes{$cid}{$$codetitles[3]}");
        }
    }
    $num ++;
    if ($num <@{$codetitles}) {
        &recurse_list($cid,$codetitles,$codes,$num,$idarrays);
    }
}

sub build_code_selections {
    my ($codes,$codetitles,$cat_titles,$cat_order,$idlist,$idnums,$idlist_titles) = @_;
    my %idarrays = ();
    for (my $i=1; $i<@{$codetitles}; $i++) {
        %{$idarrays{$$codetitles[$i]}} = ();
    }
    foreach my $cid (sort(keys(%{$codes}))) {
        &recurse_list($cid,$codetitles,$codes,0,\%idarrays);
    }
    for (my $num=0; $num<@{$codetitles}; $num++) {
        if ($num == 0) {
            my @contents = ();
            my @contents_titles = ();
            &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[0]}},\@contents);
            if (defined($$cat_titles{$$codetitles[0]})) {
                foreach (@contents) {
                    push @contents_titles, $$cat_titles{$$codetitles[0]}{$_};
                }
            }
            $$idlist{$$codetitles[0]} = join('","',@contents);
            $$idnums{$$codetitles[0]} = scalar(@contents);
            if (defined($$cat_titles{$$codetitles[0]})) {
                $$idlist_titles{$$codetitles[0]} = join('","',@contents_titles);
            }
        } elsif ($num == 1) {
            %{$$idlist{$$codetitles[1]}} = ();
            %{$$idlist_titles{$$codetitles[1]}} = ();
            foreach my $key_a (keys(%{$idarrays{$$codetitles[1]}})) {
                my @sorted_a = ();
                my @sorted_a_titles = ();
                &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[1]}{$key_a}},\@sorted_a);
                if (defined($$cat_titles{$$codetitles[1]})) {
                    foreach (@sorted_a) {
                        push @sorted_a_titles, $$cat_titles{$$codetitles[1]}{$_};
                    }
                }
                $$idlist{$$codetitles[1]}{$key_a} = join('","',@sorted_a);
                $$idnums{$$codetitles[1]}{$key_a} = scalar(@sorted_a);
                if (defined($$cat_titles{$$codetitles[1]})) {
                    $$idlist_titles{$$codetitles[1]}{$key_a} = join('","',@sorted_a_titles);
                }
            }
        } elsif ($num == 2) {
            %{$$idlist{$$codetitles[2]}} = ();
            %{$$idlist_titles{$$codetitles[2]}} = ();
            foreach my $key_a (keys(%{$idarrays{$$codetitles[2]}})) {
                %{$$idlist{$$codetitles[2]}{$key_a}} = ();
                %{$$idlist_titles{$$codetitles[2]}{$key_a}} = ();
                foreach my $key_b (keys(%{$idarrays{$$codetitles[2]}{$key_a}})) {
                    my @sorted_b = ();
                    my @sorted_b_titles = ();
                    &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[2]}{$key_a}{$key_b}},\@sorted_b);
                    if (defined($$cat_titles{$$codetitles[2]})) {
                        foreach (@sorted_b) {
                            push @sorted_b_titles, $$cat_titles{$$codetitles[2]}{$_};
                        }
                    }
                    $$idlist{$$codetitles[2]}{$key_a}{$key_b} = join('","',@sorted_b);
                    $$idnums{$$codetitles[2]}{$key_a}{$key_b} = scalar(@sorted_b);
                    if (defined($$cat_titles{$$codetitles[2]})) {
                        $$idlist_titles{$$codetitles[2]}{$key_a}{$key_b} = join('","',@sorted_b_titles);
                    }
                }
            }
        } elsif ($num == 3) {
            %{$$idlist{$$codetitles[3]}} = ();
            foreach my $key_a (keys(%{$idarrays{$$codetitles[3]}})) {
                %{$$idlist{$$codetitles[3]}{$key_a}} = ();
                foreach my $key_b (keys(%{$idarrays{$$codetitles[3]}{$key_a}})) {
                    %{$$idlist{$$codetitles[3]}{$key_a}{$key_b}} = ();
                    foreach my $key_c (keys(%{$idarrays{$$codetitles[3]}{$key_a}{$key_b}})) {
                        my @sorted_c = ();
                        my @sorted_c_titles = ();
                        &sort_cats($num,$cat_order,$codetitles,\@{$idarrays{$$codetitles[3]}{$key_a}{$key_b}{$key_c}},\@sorted_c);
                        if (defined($$cat_titles{$$codetitles[3]})) {
                            foreach (@sorted_c) {
                                push @sorted_c_titles, $$cat_titles{$$codetitles[3]}{$_};
                            }
                        }
                        $$idlist{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = join('","',@sorted_c);
                        $$idnums{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = scalar(@sorted_c);
                        if (defined($$cat_titles{$$codetitles[3]})) {
                            $$idlist_titles{$$codetitles[3]}{$key_a}{$key_b}{$key_c} = join('","',@sorted_c_titles);
                        }
                    }
                }
            }
        } elsif ($num == 4) {
            %{$$idlist{$$codetitles[4]}} = ();
            foreach my $key_a (keys(%{$idarrays{$$codetitles[4]}})) {
                %{$$idlist{$$codetitles[4]}{$key_a}} = ();
                foreach my $key_b (keys(%{$idarrays{$$codetitles[4]}{$key_a}})) {
                    %{$$idlist{$$codetitles[4]}{$key_a}{$key_b}} = ();
                    foreach my $key_c (keys(%{$idarrays{$$codetitles[4]}{$key_a}{$key_b}})) {
                        %{$$idlist{$$codetitles[4]}{$key_a}{$key_b}{$key_c}} = ();
                        foreach my $key_d (keys(%{$idarrays{$$codetitles[4]}{$key_a}{$key_b}{$key_c}})) {
                            my @sorted_d = ();
                            my @sorted_d_titles = ();
                            &sort_cats($num,$cat_order,$codetitles,$idarrays{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d},\@sorted_d);
                            if (defined($$cat_titles{$$codetitles[4]})) {
                                foreach (@sorted_d) {
                                    push @sorted_d_titles, $$cat_titles{$$codetitles[4]}{$_};
                                }
                            }
                            $$idlist{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d} = join('","',@sorted_d);
                            $$idnums{$$codetitles[4]}{$key_a}{$key_b}{$key_c}{$key_d} = scalar(@sorted_d);
                            if (defined($$cat_titles{$$codetitles[4]})) {
                                $$idlist_titles{$$codetitles[3]}{$key_a}{$key_b}{$key_c}{$key_d} = join('","',@sorted_d_titles);
                            }
                        }
                    }
                }
            }
        }
    }
}

sub javascript_code_selections {
    my ($formname,$codetitlesref,$knowncodes,$sectionlist) = @_;
    return unless (ref($codetitlesref) eq 'ARRAY');
    my @codetitles = @{$codetitlesref};
    my $numtitles = @codetitles;
    my %lt = &Apache::lonlocal::texthash(
                      Select => 'Select',
                      Pick   => 'Pick',
             );
    my $output = (<<END_OF_BLOCK);
 var display = new Array($numtitles)
 if (caller == "" || caller == "$codetitles[0]") {
     if (caller == "") {
         document.$formname.Year.length = 0
         document.$formname.Year.options[0] = new Option("$lt{'Select'}","-1",true,true)
         display[0] = new Array(idyears.length)
         for (var i=0; i<idyears.length; i++) {
             display[0][i] = idyears[i]
             if (longtitles[0] == 1) {
                 if (idyearslongs[i] != "") {
                     display[0][i] = idyearslongs[i]
                 }
             }
             else {
                 if (idyearslongs[i] != "") {
                     display[0][i] = idyears[i]
                 }
             }
             document.$formname.Year.options[i+1] = new Option(display[0][i],idyears[i],false,false)
         }
         document.$formname.Year.selectedIndex = 0;
     }
     document.$formname.Semester.length = 0
     document.$formname.Department.length = 0;
     document.$formname.Number.length = 0
     document.$formname.Department.options[0] = new Option("<-$lt{'Pick'} $codetitles[1]","-1",true,true)
     document.$formname.Number.options[0] = new Option("<-$lt{'Pick'} $codetitles[2]","-1",true,true)
     if (idyr == 0 || caller == "") {
         document.$formname.Semester.options[0] = new Option("<-$lt{'Pick'} $codetitles[0]","-1",true,true)
     }
     else {
         document.$formname.Semester.options[0] = new Option("$lt{'Select'}","-1",true,true)
         display[1] = new Array(idsems[idyr-1].length)
         for (var i=0; i<idsems[idyr-1].length; i++) {
             display[1][i] = idsems[idyr-1][i]
             if (longtitles[1] == 1) {
                 if (idsemslongs[idyr-1][i] != "") {
                     display[1][i] = idsemslongs[idyr-1][i]
                 }
             }
             document.$formname.Semester.options[i+1] = new Option(display[1][i],idsems[idyr-1][i],false,false)
         }
     }
     document.$formname.Semester.selectedIndex = 0;
 }
 if (caller == "$codetitles[1]") {
   document.$formname.Department.length = 0
   document.$formname.Number.length = 0
   document.$formname.Number.options[0] = new Option("<-Pick $codetitles[2]","-1",true,true)
   if (idsem == 0) {
     document.$formname.Department.options[0] = new Option("<-$lt{'Pick'} $codetitles[1]","-1",true,true)
   }
   else {
    document.$formname.Department.options[0] = new Option("$lt{'Select'}","-1",true,true)
    display[2] = new Array(idcodes[idyr-1][idsem-1].length)
    for (var i=0; i<idcodes[idyr-1][idsem-1].length; i++) {
      display[2][i] = idcodes[idyr-1][idsem-1][i]
      if (longtitles[2] == 1) {
          if (idcodeslongs[idyr-1][idsem-1][i] != "") {
              display[2][i] = idcodeslongs[idyr-1][idsem-1][i]
          }
      }
      document.$formname.Department.options[i+1] = new Option(display[2][i],idcodes[idyr-1][idsem-1][i],false,false)
    }
   }
   document.$formname.Department.selectedIndex = 0
 }
 if (caller == "$codetitles[2]") {
   document.$formname.Number.length = 0
   if (iddept == 0) {
     document.$formname.Number.options[0] = new Option("<-$lt{'Pick'} $codetitles[2]","-1",true,true)
   }
   else {
    document.$formname.Number.options[0] = new Option("Select","-1",true,true)
    display[3] = new Array (idcourses[idyr-1][idsem-1][iddept-1].length)
    for (var i=0; i<idcourses[idyr-1][idsem-1][iddept-1].length; i++) {
      display[3][i] = idcourses[idyr-1][idsem-1][iddept-1][i]
      if (longtitles[3] == 1) {
        if (idcourseslongs[idyr-1][idsem-1][iddept-1][i] != "") {
            display[3][i] = idcourseslongs[idyr-1][idsem-1][iddept-1][i]
        }
      }
      document.$formname.Number.options[i+1] = new Option(display[3][i],idcourses[idyr-1][idsem-1][iddept-1][i],false,false)
    }
   }
   document.$formname.Number.selectedIndex = 0
 }
}
END_OF_BLOCK

    if ((ref($knowncodes) eq 'HASH') && (keys(%{$knowncodes}) > 0)) {
        my @standardnames = &Apache::loncommon::get_standard_codeitems();
        my (%local_to_standard,$currstr);
        for (my $i=0; $i<@codetitles; $i++) {
            $local_to_standard{$codetitles[$i]} = $standardnames[$i];
        }
        $output .= <<"ENDJS";

function initialize_codes() {
ENDJS
        for (my $i=0; $i<@codetitles-1; $i++) {
            my $item = $local_to_standard{$codetitles[$i]};
            $currstr .= $knowncodes->{$codetitles[$i]};
            if ($knowncodes->{$codetitles[$i]} != -1) {
                $output .= '
    for (var j=0; j<document.'.$formname.'.'.$item.'.length; j++) {
        if (document.'.$formname.'.'.$item.'[j].value == "'.$knowncodes->{$codetitles[$i]}.'") {
            document.'.$formname.'.'.$item.'.selectedIndex = j;
        }
    }
';
            }
            $output .= '   courseSet('."'$codetitles[$i]'".')'."\n";
        }
        my $lastcodetitle =  $local_to_standard{$codetitles[-1]};
        $currstr .= $knowncodes->{$codetitles[-1]};
        if ($knowncodes->{$codetitles[-1]} != -1) {
            $output .= '
    for (var j=0; j<document.'.$formname.'.'.$lastcodetitle.'.length; j++) {
        if (document.'.$formname.'.'.$lastcodetitle.'[j].value == "'.$knowncodes->{$codetitles[-1]}.'") {
            document.'.$formname.'.'.$lastcodetitle.'.selectedIndex = j;
        }
    }
';
        }
        $output .= "    return;\n}\n";
        if ($sectionlist) {
            $output .= '
function toggleSecVis() {
    var currcode = "";
    var crscode = '."'$currstr'".';
';
            for (my $i=0; $i<@codetitles-1; $i++) {
                my $item = $local_to_standard{$codetitles[$i]};
                $output .= 'currcode += document.'.$formname.'.'.$item.'.options[document.'.$formname.'.'.$item.'.selectedIndex].value;'."\n";
            }
            my $lastcodetitle =  $local_to_standard{$codetitles[-1]};
            if ($lastcodetitle ne '') {
                if ($knowncodes->{$codetitles[-1]} != -1) {
                    $output .= 'currcode += document.'.$formname.'.'.$lastcodetitle.'.options[document.'.$formname.'.'.$lastcodetitle.'.selectedIndex].value;'."\n";
                }
            }
            $output .= <<"ENDSEC";
    var showlist = 'none';
    var showbox = 'inline';
    if ((crscode != '') && (currcode == crscode)) {
        showlist = 'inline';
        showbox = 'none';
    }
    if (document.getElementById('LC_helpdesk_sectionlist')) {
        document.getElementById('LC_helpdesk_sectionlist').style.display = showlist;
    }
    if (document.getElementById('LC_helpdesk_section')) {
        document.getElementById('LC_helpdesk_section').style.display = showbox;
    }
    return;
}

ENDSEC

        }
    } else {
        $output .= <<"END_OF_JS";

function initialize_codes() {
    courseSet();
    return;
}
END_OF_JS
    }
    return $output;
}

sub javascript_definitions {
    my ($codetitles,$idlist,$idlist_titles,$idnums,$cat_titles) = @_;
    my $numtitles = @{$codetitles};
    my $longtitles = [];
    for (my $i=0; $i<$numtitles; $i++) {
       if (defined($cat_titles->{$codetitles->[$i]})) {
           $longtitles->[$i] = 1;
       } else {
           $longtitles->[$i] = 0;
       }
    }
    my $scripttext;
    if ($longtitles->[0]) {
        $scripttext =
          qq|      var idyearslongs = new Array("$idlist_titles->{$codetitles->[0]}")\n|;
    }
    $scripttext .=
          "      var idsems = new Array ($idnums->{$codetitles->[0]})\n";
    if ($longtitles->[1]) {
        $scripttext .=
          "      var idsemslongs = new Array ($idnums->{$codetitles->[0]})\n";    }
    $scripttext .=
          "      var idcodes = new Array ($idnums->{$codetitles->[0]})\n";
    if ($longtitles->[2]) {
        $scripttext .=
          "      var idcodeslongs = new Array ($idnums->{$codetitles->[0]})\n";
    }
    $scripttext .=
          "      var idcourses = new Array ($idnums->{$codetitles->[0]})\n";
    if ($longtitles->[3]) {
        $scripttext .=
          "      var idcourseslongs =  new Array ($idnums->{$codetitles->[0]})\n";
    }
    my @sort_a = split/","/,$idlist->{$codetitles->[0]};
    for (my $j=0; $j<@sort_a; $j++) {
        $scripttext .= qq| idsems[$j] = new Array("$idlist->{$codetitles->[1]}{$sort_a[$j]}")\n|;
        if ($longtitles->[1]) {
            $scripttext .= qq| idsemslongs[$j] = new Array("$idlist_titles->{$codetitles->[1]}{$sort_a[$j]}")\n|;
        }
        $scripttext .= qq| idcodes[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
        if ($longtitles->[2]) {
            $scripttext .= qq| idcodeslongs[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
        }
        $scripttext .= qq| idcourses[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
        if ($longtitles->[3]) {
            $scripttext .= qq| idcourseslongs[$j] = new Array($idnums->{$codetitles->[1]}{$sort_a[$j]})\n|;
        }
        my @sort_b = split/","/,$idlist->{$codetitles->[1]}{$sort_a[$j]};
        for (my $k=0; $k<@sort_b; $k++) {
            my $idcode_entry = $idlist->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
            $scripttext .= qq| idcodes[$j][$k] = new Array("$idcode_entry")\n|;
            if ($longtitles->[2]) {
                my $idcodelong_entry = $idlist_titles->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
                $scripttext .= qq| idcodeslongs[$j][$k] = new Array("$idcodelong_entry")\n|;
            }
            $scripttext .= qq| idcourses[$j][$k] = new Array($idnums->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]})\n|;
            if ($longtitles->[3]) {
                $scripttext .= qq| idcourseslongs[$j][$k] = new Array($idnums->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]})\n|;
            }
            my @sort_c = split/","/,$idlist->{$codetitles->[2]}{$sort_a[$j]}{$sort_b[$k]};
            for (my $l=0; $l<@sort_c; $l++) {
                my $idcourse_entry = $idlist->{$codetitles->[3]}{$sort_a[$j]}{$sort_b[$k]}{$sort_c[$l]};
                $scripttext .= qq| idcourses[$j][$k][$l] = new Array("$idcourse_entry")\n|;
                if ($longtitles->[3]) {
                    my $idcourselong_entry = $idlist_titles->{$codetitles->[3]}{$sort_a[$j]}{$sort_b[$k]}{$sort_c[$l]};
                    $scripttext .= qq| idcourseslongs[$j][$k][$l] = new Array("$idcourselong_entry")\n|;
                }
            }
        }
    }
    return ($scripttext,$longtitles);
}

sub courseset_js_start {
    my ($formname,$longtitles_str,$allidlist) = @_;
    my $output = <<END;

function courseSet(caller) {
    var longtitles = new Array ("$longtitles_str");
    var idyr = document.$formname.Year.selectedIndex
    var idsem  = document.$formname.Semester.selectedIndex
    var iddept = document.$formname.Department.selectedIndex
    var idclass = document.$formname.Number.selectedIndex
    var idyears = new Array("$allidlist");
END
    return $output;
}

sub instcode_selectors_data {
    my ($codedom,$formname,$cat_items,$codetitles,$cat_titles,$cat_order,$officialjs) = @_;
    my ($jscript,$codes,%idlist,%idnums,%idlist_titles,%by_year,%by_sem,%by_dept);
    my ($numtitles,$lasttitle,$totcodes);
    my $instcats = &Apache::lonnet::get_dom_instcats($codedom);
    if (ref($instcats) eq 'HASH') {
        if ((ref($instcats->{'codetitles'}) eq 'ARRAY') && (ref($instcats->{'codes'}) eq 'HASH') &&
            (ref($instcats->{'cat_titles'}) eq 'HASH') && (ref($instcats->{'cat_order'}) eq 'HASH')) {
            %{$codes} = %{$instcats->{'codes'}};
            @{$codetitles} = @{$instcats->{'codetitles'}};
            %{$cat_titles} = %{$instcats->{'cat_titles'}};
            %{$cat_order} = %{$instcats->{'cat_order'}};
            $totcodes = scalar(keys(%{$codes}));
            if ((!$totcodes) && (exists($instcats->{'totcodes'}))) {
                $totcodes = $instcats->{'totcodes'};
            }
            &build_code_selections($codes,$codetitles,$cat_titles,$cat_order,
                                   \%idlist,\%idnums,\%idlist_titles);
            my ($scripttext,$longtitles) =
                &javascript_definitions($codetitles,\%idlist,\%idlist_titles,
                                        \%idnums,$cat_titles);
            my $longtitles_str = join('","',@{$longtitles});
            my $allidlist = $idlist{$codetitles->[0]};
            $numtitles = @{$codetitles};
            $lasttitle = $numtitles;
            if ($numtitles > 4) {
                $lasttitle = 4;
            }
            if ($numtitles == 0) {
                if (!defined($env{'form.state'})) {
                    $env{'form.state'} = 'listing';
                }
            } else {
                my @data = ('top');
                for (my $k=0; $k<$lasttitle; $k++) {
                    my $cat = $codetitles->[$k];
                    my $level = 1;
                    $level = &recurse_options($codetitles->[$k],$idlist{$codetitles->[$k]},$level,$cat,$cat_items,\@data,\%by_year,\%by_sem,\%by_dept);
                }
                $scripttext .= &build_javascript(\%by_year,\%by_sem,\%by_dept,$cat_order,$codetitles);
                if ($officialjs) {
                    $jscript .= '

function toggleOfficial() {
   var choice; 
   for (var i=0; i<document.'.$formname.'.official.length; i++) {
       if (document.'.$formname.'.official[i].checked) {
           choice = document.'.$formname.'.official[i].value;
       }
   }
   if (choice == "off") {
       if (document.getElementById("instcodes")) {
           document.getElementById("instcodes").style.display = "none";
       }';
                    for (my $i=0; $i<@{$codetitles}-1; $i++) {
                        $jscript .= '
       document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = 0;';
                    }
    $jscript .= '
   } else {
       if (document.getElementById("instcodes")) { 
           document.getElementById("instcodes").style.display = "block";
       }
   }
}

function setOfficial(caller) {
   if (typeof(caller) != "undefined") {
       if (caller.options[caller.selectedIndex].value != 0) {
           if (document.'.$formname.'.official.length > 0) {
               for (var j=0; j<document.'.$formname.'.official.length; j++) {
                   if (document.'.$formname.'.official[j].value == "on") {
                       document.'.$formname.'.official[j].checked = true;
                   }
               }
            }
        }
    }
    return;
}

';

                    $officialjs = 'setOfficial(caller);';
                }
                $jscript .= &javascript_select_filler($formname,$scripttext,$codetitles,$longtitles_str,$allidlist,$officialjs);
                if ($env{'form.state'} eq 'listing') {
                    my @standardnames = &Apache::loncommon::get_standard_codeitems();
                    my %local_to_standard;
                    if (ref($codetitles) eq 'ARRAY') {
                        for (my $i=0; $i<@{$codetitles}; $i++) {
                            $local_to_standard{$codetitles->[$i]} = $standardnames[$i];
                        }
                    }
                    $jscript .= '
function setElements() {
';
                    for (my $i=0; $i<@{$codetitles}-1; $i++) {
                        my $item = $local_to_standard{$codetitles->[$i]};
                        if ($env{'form.'.$item} != -1) {
                            $jscript .= '
    for (var j=0; j<document.'.$formname.'.'.$item.'.length; j++) {
        if (document.'.$formname.'.'.$item.'[j].value == "'.$env{'form.'.$item}.'") {
            document.'.$formname.'.'.$item.'.selectedIndex = j;
        }
    }
';
                        }
                    }
                    $jscript .= '   courseSet()'."\n";
                    my $lastcodetitle =  $local_to_standard{$codetitles->[-1]};
                    if ($env{'form.'.$lastcodetitle} != -1) {
                        $jscript .= '
    for (var j=0; j<document.'.$formname.'.'.$lastcodetitle.'.length; j++) {
        if (document.'.$formname.'.'.$lastcodetitle.'[j].value == "'.$env{'form.'.$lastcodetitle}.'") {
            document.'.$formname.'.'.$lastcodetitle.'.selectedIndex = j;
        }
    }
';
                    }
                    $jscript .= '}';
                }
            }
        }
    }
    return ($jscript,$totcodes,$numtitles,$lasttitle);
}

sub build_instcode_selectors {
    my ($numtitles,$lasttitle,$cat_items,$codetitles,$cat_titles,$cat_order,$official,$class,$id,$noedit) = @_;
    my $output;
    my @standardnames = &Apache::loncommon::get_standard_codeitems();
    if ($numtitles > 0) {
        my $style;
        if ($official eq 'off') {
            $style = ' style="display: none"';
        }
        if ($id eq '') {
            $id = 'instcodes';
        }
        my $disabled;
        if ($noedit) {
            $disabled = ' disabled="disabled"';
        }
        $output .= '<div id="'.$id.'"'.$style.'><table><tr>';
        for (my $k=0; $k<$lasttitle-1; $k++) {
            my (@items,@unsorted);
            if (ref($cat_items->{$codetitles->[$k]}) eq 'ARRAY') {
                @unsorted = @{$cat_items->{$codetitles->[$k]}};
            }
            &sort_cats($k,$cat_order,$codetitles,\@unsorted,\@items);
            my @longitems;
            if (defined($cat_titles->{$codetitles->[$k]})) {
                foreach my $item (@items) {
                    push(@longitems,$cat_titles->{$codetitles->[$k]}{$item});
                }
            } else {
                @longitems = @items;
            }
            $output .= '<td align="center">'.$codetitles->[$k].'<br />'."\n".
                       '<select name="'.$standardnames[$k].'" onchange="courseSet(this)"';
            if ($class) {
                $output .= ' class="'.$class.'"';
            }
            $output .= $disabled.'>'."\n".'<option value="0">'.&mt('All').'</option>'."\n";
            for (my $i=0; $i<@items; $i++) {
                if ($longitems[$i] eq '') {
                    $longitems[$i] = $items[$i];
                }
                $output .= ' <option value="'.$items[$i].'">'.$longitems[$i].'</option>';
            }
            $output .= '</select></td>';
        }
        $output .= '<td align="center">'.$codetitles->[$lasttitle-1].'<br />'."\n".
                   '<select name="'.$standardnames[$lasttitle-1].'"';
        if ($class) {
            $output .= ' class="'.$class.'"';
        }
        $output .= $disabled.'>'."\n".
                   '<option value="0">'.&mt('All')."\n".
                   '</option>'."\n".'</select>'."\n".
                   '</td></tr></table>'."\n";
        if ($numtitles > 4) {
            $output .= '<br /><br />'.$codetitles->[$numtitles-1].'<br />'."\n".
                       '<input type="text" name="'.$standardnames[$numtitles-1].
                       '"'.$disabled.' /><br />'."\n";
        }
        $output .= '</div>';
    }
    return $output;
}

sub instcode_selectors {
    my ($codedom,$formname,$officialjs,$codetitles) = @_;
    my ($output,%cat_titles,%cat_order,%cat_items);
    my ($jscript,$totcodes,$numtitles,$lasttitle) =
        &instcode_selectors_data($codedom,$formname,\%cat_items,$codetitles,
                                 \%cat_titles,\%cat_order,$officialjs);
    if ($numtitles > 0) {
        my $official = ' checked="checked" ';
        my $unofficial = '';
        if (($formname eq 'filterpicker') && ($env{'form.official'} eq 'off')) {
            $unofficial = $official;
            $official = '';
        }
        $output .= '<span class="LC_nobreak">'.&mt('Official course:').'&nbsp;<label>'.
                   '<input type="radio" name="official" value="on"'.$official.
                   ' onclick="toggleOfficial();" />'.&mt('Yes').'</label>'.
                   ('&nbsp;'x3).'<label>'.
                   '<input type="radio" name="official" value="off"'.$unofficial.
                   ' onclick="toggleOfficial();" />'.&mt('No').'</label></span><br />'.
                   &build_instcode_selectors($numtitles,$lasttitle,\%cat_items,$codetitles,
                                             \%cat_titles,\%cat_order,$env{'form.official'})."\n".
                   '<input type="hidden" name="numtitles" value="'.$numtitles.'" />'."\n".
                   '<input type="hidden" name="state" value="listing" />'."\n";
    }
    return ($output,$jscript,$numtitles);
}

sub recurse_options {
    my ($currkey,$currlist,$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept) = @_;
    if (ref($currlist) eq 'HASH') {
        $level ++;
        foreach my $key (sort(keys(%{$currlist}))) {
            $$data[$level-1]= $key;
            &recurse_options($key,$currlist->{$key},$level,$cat,$cat_options,$data,$by_year,$by_sem,$by_dept);
        }
    } else {
        $level --;
        my @contents = split(/","/,$currlist);
        foreach my $item (@contents) {
            if (!grep(/^\Q$item\E$/,@{$cat_options->{$cat}})) {
                push(@{$cat_options->{$cat}},$item);
            }
            if ($level == 3) {
                if (!grep/^\Q$item\E$/,@{$by_year->{$data->[1]}->{$currkey}}) {
                    push(@{$by_year->{$data->[1]}->{$currkey}},$item);
                }
                if (!grep/^\Q$item\E$/,@{$by_sem->{$data->[2]}->{$currkey}}) {
                    push(@{$by_sem->{$data->[2]}->{$currkey}},$item);
                }
                if (!grep/^\Q$item\E$/,@{$by_dept->{$currkey}}) {
                    push(@{$by_dept->{$currkey}},$item);
                }

            }
        }
    }
    return $level;
}

sub build_javascript {
    my ($by_year,$by_sem,$by_dept,$cat_order,$codetitles) = @_;
    my @unsorted = keys(%{$by_year});
    my @sorted_yrs;
    &sort_cats('0',$cat_order,$codetitles,\@unsorted,\@sorted_yrs);
    my $output = 'var idcse_by_yr_year = new Array("'.join('","',@sorted_yrs).'");'."\n".
                 'var idcse_by_yr_dept = new Array('.scalar(@sorted_yrs).');'."\n".
                 'var idcse_by_yr_num = new Array('.scalar(@sorted_yrs).');'."\n";
    for (my $i=0; $i<@sorted_yrs; $i++) {
        my $numkeys = keys(%{$by_year->{$sorted_yrs[$i]}});
        $output .= " idcse_by_yr_num[$i] = new Array($numkeys);\n";
        if (ref($by_year->{$sorted_yrs[$i]}) eq 'HASH') {
            @unsorted = keys(%{$by_year->{$sorted_yrs[$i]}});
            my @sorted_depts;
            &Apache::courseclassifier::sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
            $output .= qq| idcse_by_yr_dept[$i] = new Array ("|.join('","',@sorted_depts).'");'."\n";
            for (my $j=0; $j<@sorted_depts; $j++) {
                $output .= qq| idcse_by_yr_num[$i][$j] = new Array ("|;
                $output .= join('","',sort(@{$by_year->{$sorted_yrs[$i]}->{$sorted_depts[$j]}})).'");'."\n";
            }
        }
    }
    @unsorted = keys(%{$by_sem});
    my @sorted_sems;
    &sort_cats('1',$cat_order,$codetitles,\@unsorted,\@sorted_sems);
    $output .=  'idcse_by_sem_sems = new Array("'.join('","',@sorted_sems).'");'."\n".
                'idcse_by_sem_dept = new Array('.scalar(@sorted_sems).');'."\n".
                'idcse_by_sem_num = new Array('.scalar(@sorted_sems).');'."\n";
    for (my $i=0; $i<@sorted_sems; $i++) {
        my $numkeys = keys(%{$by_sem->{$sorted_sems[$i]}});
        $output .= " idcse_by_sem_num[$i] = new Array($numkeys);\n";
        if (ref($by_sem->{$sorted_sems[$i]}) eq 'HASH') {
            @unsorted = keys(%{$by_sem->{$sorted_sems[$i]}});
            my @sorted_depts;
            &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_depts);
            $output .= qq| idcse_by_sem_dept[$i] = new Array("|.join('","',@sorted_depts).'");'."\n";
            for (my $j=0; $j<@sorted_depts; $j++) {
                $output .= qq| idcse_by_sem_num[$i][$j] = new Array ("|.join('","',sort(@{$by_sem->{$sorted_sems[$i]}->{$sorted_depts[$j]}})).'");'."\n";
            }
        }
    }
    @unsorted = keys(%{$by_dept});
    my @sorted_deps;
    &sort_cats('2',$cat_order,$codetitles,\@unsorted,\@sorted_deps);
    $output .= 'idcse_by_dep = new Array('.scalar(@sorted_deps).');'."\n";
    for (my $k=0; $k<@sorted_deps; $k++) {
        $output .= qq| idcse_by_dep[$k] = new Array ("|.join('","',sort(@{$by_dept->{$sorted_deps[$k]}})).'");'."\n";
    }
    return $output;
}

sub javascript_select_filler {
    my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist,$officialjs) = @_;
    my $all = &mt('All');
    my $nocrs = &mt('No courses');
    my $output = <<END;

function courseSet(caller, onload) {
    var longtitles = new Array ("$longtitles_str");
    var valyr = '';
    var valsem = '';
    var valdept = '';
    var valclass = '';
    if (onload) {
        valyr = "$env{'form.Year'}";
        valsem = "$env{'form.Semester'}";
        valdept = "$env{'form.Department'}";
        valclass = "$env{'form.Number'}";
    } else {
        valyr = document.$formname.Year.options[document.$formname.Year.selectedIndex].value;
        valsem  = document.$formname.Semester.options[document.$formname.Semester.selectedIndex].value;
        valdept = document.$formname.Department.options[document.$formname.Department.selectedIndex].value;
        valclass = document.$formname.Number.options[document.$formname.Number.selectedIndex].value;
    }
    var idyears = new Array("$allidlist");
    var idyr = -1;
    var idsem = -1;
    var iddept = -1;
    document.$formname.Number.length = 0;

    $scripttext

    if (onload) {
        if (document.$formname.Year.options.length > 0) {
            for (var i = 0; i< document.$formname.Year.options.length; i++) {
                if (document.$formname.Year.options[i].value == "$env{'form.Year'}") {
                    document.$formname.Year.selectedIndex = i;
                    break;
                }
            }
        }
        if (document.$formname.Department.options.length > 0) {
            for (var i = 0; i< document.$formname.Department.options.length; i++) {
                if (document.$formname.Department.options[i].value == "$env{'form.Department'}") {
                    document.$formname.Department.selectedIndex = i;
                    break;
                }
            }
        }
        if (document.$formname.Semester.options.length > 0) {
            for (var i = 0; i< document.$formname.Semester.options.length; i++) {
                if (document.$formname.Semester.options[i].value == "$env{'form.Semester'}") {
                    document.$formname.Semester.selectedIndex = i;
                    break;
                }
            }
        }
    }

    var selYear = document.$formname.Year.selectedIndex-1;
    var selSemester = document.$formname.Semester.selectedIndex-1;
    var selDepartment = document.$formname.Department.selectedIndex-1;
    if (selYear == -1) {
        if (selSemester == -1) {
            if (selDepartment > -1) {
                document.$formname.Number.options[0] =  new Option('$all','0',false,false);
                for (var k=0; k<idcse_by_dep[selDepartment].length; k++) {
                    document.$formname.Number.options[k+1] = new Option(idcse_by_dep[selDepartment][k],idcse_by_dep[selDepartment][k],false,false);

                }
            }
            else {
                document.$formname.Number.options[0] = new Option('$all','0',true,true);
            }
        }
        else {
            if (selDepartment > -1) {
                for (var i=0; i<idcse_by_sem_sems.length; i++) {
                    if (idcse_by_sem_sems[i] == valsem) {
                        idsem = i;
                    }
                }
                if (idsem != -1) {
                    for (var i=0; i<idcse_by_sem_dept[idsem].length; i++) {
                        if (idcse_by_sem_dept[idsem][i] == valdept) {
                            iddept = i;
                        }
                    }
                }
                if (iddept != -1) {
                    document.$formname.Number.options[0] =  new Option('$all','0',false,false);
                    for (var k=0; k<idcse_by_sem_num[idsem][iddept].length; k++) {
                        document.$formname.Number.options[k+1] = new Option(idcse_by_sem_num[idsem][iddept][k],idcse_by_sem_num[idsem][iddept][k],false,false);
                    }
                }
                else {
                    document.$formname.Number.options[0] =  new Option('$nocrs','0',true,true);
                }
            }
            else {
                document.$formname.Number.options[0] = new Option('$all','0',true,true);
            }
        }
    }
    else {
        if (selSemester == -1) {
            if (selDepartment > -1) {
                for (var i=0; i<idcse_by_yr_year.length; i++) {
                    if (idcse_by_yr_year[i] == valyr) {
                        idyr = i;
                    }
                }
                if (idyr != -1) {
                    for (var i=0; i<idcse_by_yr_dept[idyr].length; i++) {
                        if (idcse_by_yr_dept[idyr][i] == valdept) {
                            iddept = i;
                        }
                    }
                }
                if (iddept != -1) {
                    document.$formname.Number.options[0] =  new Option('$all','0',false,false);
                    for (var k=0; k<idcse_by_yr_num[idyr][iddept].length; k++) {
                        document.$formname.Number.options[k+1] = new Option(idcse_by_yr_num[idyr][iddept][k],idcse_by_yr_num[idyr][iddept][k],false,false);
                    }
                }
                else {
                    document.$formname.Number.options[0] =  new Option('$nocrs','0',true,true);
                }
            }
            else {
                document.$formname.Number.options[0] = new Option('$all','0',true,true);
            }
        }
        else {
            if (selDepartment > -1) {
                for (var k=0; k<idyears.length; k++) {
                    if (idyears[k] == valyr) {
                        idyr = k;
                    }
                }
                if (idyr != -1) {
                    for (var k=0; k<idsems[idyr].length; k++) {
                        if (idsems[idyr][k] == valsem) {
                            idsem = k;
                        }
                    }
                }
                if (idsem != -1) {
                    for (var k=0; k<idcodes[idyr][idsem].length; k++) {
                        if (idcodes[idyr][idsem][k] == valdept) {
                            iddept = k;
                        }
                    }
                }
                if (iddept != -1) {
                    document.$formname.Number.options[0] = new Option('$all','0',false,false);
                    for (var i=0; i<idcourses[idyr][idsem][iddept].length; i++) {
                        var display = idcourses[idyr][idsem][iddept][i];
                        if (longtitles[3] == 1) {
                            if (idcourseslongs[idyr][idsem][iddept][i] != "") {
                                display = idcourseslongs[idyr][idsem][iddept][i]
                            }
                        }
                        document.$formname.Number.options[i+1] = new Option(display,idcourses[idyr][idsem][iddept][i],false,false)
                    }
                }
                else {
                    document.$formname.Number.options[0] =  new Option('$nocrs','0',true,true);
                }
            }
            else {
                document.$formname.Number.options[0] =  new Option('$all','0',true,true);
            }
        }
        document.$formname.Number.selectedIndex = 0
    }
    if (onload) {
        if (document.$formname.Number.options.length > 0) {
            for (var i = 0; i< document.$formname.Number.options.length; i++) {
                if (document.$formname.Number.options[i].value == "$env{'form.Number'}") {
                    document.$formname.Number.selectedIndex = i;
                    break;
                }
            }
        }
    } else {
        $officialjs
    }
}
END
    return $output;
}

sub instcode_search_str {
    my ($domain,$numtitles,$codetitles) = @_;
    my $instcode;
    if (defined($numtitles) && $numtitles == 0) {
        $instcode = '.+';
    } else {
        my (%codedefaults,@code_order);
        my $defaults_result =
            &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults,
                                                    \@code_order);
        my @standardnames = &Apache::loncommon::get_standard_codeitems();
        my %local_to_standard;
        if (ref($codetitles) eq 'ARRAY') {
            for (my $i=0; $i<@{$codetitles}; $i++) {
                $local_to_standard{$codetitles->[$i]} = $standardnames[$i];
            }
        }
        if ($defaults_result eq 'ok') {
            $instcode ='^';
            foreach my $loctitle (@code_order) {
                my $item = $local_to_standard{$loctitle};
                if ($item ne '') {
                    if ($env{'form.'.$item} eq '0' ) {
                        $instcode .= $codedefaults{$loctitle};
                    } else {
                        $instcode .= $env{'form.'.$item};
                    }
                } else {
                    $instcode .= '.+';
                }
            }
            $instcode .= '$';
        } else {
            $instcode = '.+';
        }
    }
    return $instcode;
}

sub instcode_from_selectors {
    my ($cdom,$noregexps) = @_;
    my $instcode;
    my (@codetitles,%codedefaults,@code_order);
    my $instcats = &Apache::lonnet::get_dom_instcats($cdom);
    if (ref($instcats) eq 'HASH') {
        if (ref($instcats->{'codetitles'}) eq 'ARRAY') {
            @codetitles = @{$instcats->{'codetitles'}};
        }
    }
    my $defaults_result =
        &Apache::lonnet::auto_instcode_defaults($cdom,\%codedefaults,
                                                \@code_order);
    if (($defaults_result eq 'ok') && (@codetitles > 0)) {
        my @standardnames = &Apache::loncommon::get_standard_codeitems();
        my %local_to_standard;
        for (my $i=0; $i<@codetitles; $i++) {
            $local_to_standard{$codetitles[$i]} = $standardnames[$i];
        }
        foreach my $loctitle (@code_order) {
            my $category = $local_to_standard{$loctitle};
            if ($category ne '') {
                if ($env{'form.'.$category} eq '-1' ) {
                    if ($noregexps) {
                        $instcode .= ' ';
                    } else {
                        $instcode .= $codedefaults{$category};
                    }
                } else {
                    $instcode .= $env{'form.'.$category};
                }
            } else {
                $instcode .= '.+';
            }
        }
    }
    return $instcode;
}

1;


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