--- loncom/interface/courseclassifier.pm 2010/07/08 23:19:19 1.9 +++ loncom/interface/courseclassifier.pm 2014/07/31 15:45:27 1.19 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utilities for classifying courses based on institutional code # -# $Id: courseclassifier.pm,v 1.9 2010/07/08 23:19:19 raeburn Exp $ +# $Id: courseclassifier.pm,v 1.19 2014/07/31 15:45:27 musolffc Exp $ # # Copyright Michigan State University Board of Trustees # @@ -30,17 +30,21 @@ package Apache::courseclassifier; use strict; use lib '/home/httpd/lib/perl/'; use Apache::lonnet; +use Apache::loncommon; use Apache::lonlocal; use LONCAPA; sub retrieve_instcodes { my ($coursecodes,$codedom) = @_; my $totcodes; - my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.','.', undef,undef,'Course'); + my %courses = &Apache::lonnet::courseiddump($codedom,'.',1,'.','.','.', + undef,undef,'Course'); foreach my $course (keys(%courses)) { if (ref($courses{$course}) eq 'HASH') { - $$coursecodes{$course} = $courses{$course}{'inst_code'}; - $totcodes ++; + if ($courses{$course}{'inst_code'} ne '') { + $$coursecodes{$course} = $courses{$course}{'inst_code'}; + $totcodes ++; + } } } return $totcodes; @@ -52,7 +56,7 @@ sub sort_cats { if (defined($$cat_order{$$codetitles[$num]})) { foreach my $item (@{$$cat_order{$$codetitles[$num]}}) { if (grep/^\Q$item\E$/,@unsorted) { - push @{$sorted}, $item; + push(@{$sorted},$item); } } } else { @@ -258,12 +262,16 @@ sub build_code_selections { sub javascript_code_selections { my ($formname,@codetitles) = @_; my $numtitles = @codetitles; + my %lt = &Apache::lonlocal::texthash( + Select => 'Select', + Pick => 'Pick', + ); my $output = (<[$i].'.selectedIndex = 0;'; + document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = 0;'; } - $jscript .= ' + $jscript .= ' + } else { + if (document.getElementById("instcodes")) { + document.getElementById("instcodes").style.display = "block"; + } + } } function setOfficial(caller) { @@ -510,26 +533,35 @@ function 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++) { - if ($env{'form.'.$codetitles->[$i]} != -1) { + my $item = $local_to_standard{$codetitles->[$i]}; + if ($env{'form.'.$item} != -1) { $jscript .= ' - for (var j=0; j[$i].'.length; j++) { - if (document.'.$formname.'.'.$codetitles->[$i].'[j].value == "'.$env{'form.'.$codetitles->[$i]}.'") { - document.'.$formname.'.'.$codetitles->[$i].'.selectedIndex = j; + for (var j=0; j[-1]} != -1) { + my $lastcodetitle = $local_to_standard{$codetitles->[-1]}; + if ($env{'form.'.$lastcodetitle} != -1) { $jscript .= ' - for (var j=0; j[-1].'.length; j++) { - if (document.'.$formname.'.'.$codetitles->[-1].'[j].value == "'.$env{'form.'.$codetitles->[-1]}.'") { - document.'.$formname.'.'.$codetitles->[-1].'.selectedIndex = j; + for (var j=0; j 0) { - $output .= ''; + my $style; + if ($env{'form.official'} eq 'off') { + $style = ' style="display: none"'; + } + $output .= '
'; for (my $k=0; $k<$lasttitle-1; $k++) { my (@items,@unsorted); if (ref($cat_items->{$codetitles->[$k]}) eq 'ARRAY') { @@ -563,7 +599,7 @@ sub build_instcode_selectors { @longitems = @items; } $output .= '
'.$codetitles->[$k].'
'."\n". - '
'."\n"; } - $output .= '
'; + $output .= ''; } 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 .= ''.&mt('Official course:').' '. + (' 'x3).'
'. + &build_instcode_selectors($numtitles,$lasttitle,\%cat_items,$codetitles, + \%cat_titles,\%cat_order)."\n". + ''."\n". + ''."\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') { @@ -672,14 +735,27 @@ sub build_javascript { sub javascript_select_filler { my ($formname,$scripttext,$codetitles,$longtitles_str,$allidlist,$officialjs) = @_; + my $all = &mt('All'); + my $nocrs = &mt('No courses'); my $output = < 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); + document.$formname.Number.options[0] = new Option('$all','0',false,false); for (var k=0; k 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; @@ -821,7 +935,7 @@ sub instcode_search_str { my $defaults_result = &Apache::lonnet::auto_instcode_defaults($domain,\%codedefaults, \@code_order); - my @standardnames = &get_standard_codeitems(); + my @standardnames = &Apache::loncommon::get_standard_codeitems(); my %local_to_standard; if (ref($codetitles) eq 'ARRAY') { for (my $i=0; $i<@{$codetitles}; $i++) { @@ -834,7 +948,7 @@ sub instcode_search_str { my $item = $local_to_standard{$loctitle}; if ($item ne '') { if ($env{'form.'.$item} eq '0' ) { - $instcode .= $codedefaults{$item}; + $instcode .= $codedefaults{$loctitle}; } else { $instcode .= $env{'form.'.$item}; } @@ -850,8 +964,42 @@ sub instcode_search_str { return $instcode; } -sub get_standard_codeitems { - return ('Year','Semester','Department','Number'); +sub instcode_from_selectors { + my ($cdom,$noregexps) = @_; + my $instcode; + my $caller = 'global'; + my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order, + %codedefaults,@code_order); + my $format_reply = + &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes, + \%codes,\@codetitles,\%cat_titles,\%cat_order); + my $defaults_result = + &Apache::lonnet::auto_instcode_defaults($cdom,\%codedefaults, + \@code_order); + if (($defaults_result eq 'ok') && ($format_reply eq 'ok')) { + 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;