--- loncom/interface/courseclassifier.pm 2014/12/11 00:07:12 1.20 +++ loncom/interface/courseclassifier.pm 2019/07/23 13:58:53 1.27 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Utilities for classifying courses based on institutional code # -# $Id: courseclassifier.pm,v 1.20 2014/12/11 00:07:12 raeburn Exp $ +# $Id: courseclassifier.pm,v 1.27 2019/07/23 13:58:53 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -34,22 +34,6 @@ 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'); - foreach my $course (keys(%courses)) { - if (ref($courses{$course}) eq 'HASH') { - if ($courses{$course}{'inst_code'} ne '') { - $$coursecodes{$course} = $courses{$course}{'inst_code'}; - $totcodes ++; - } - } - } - return $totcodes; -} - sub sort_cats { my ($num,$cat_order,$codetitles,$idsarrayref,$sorted) = @_; my @unsorted = @{$idsarrayref}; @@ -260,7 +244,9 @@ sub build_code_selections { } sub javascript_code_selections { - my ($formname,@codetitles) = @_; + my ($formname,$codetitlesref,$knowncodes,$sectionlist) = @_; + return unless (ref($codetitlesref) eq 'ARRAY'); + my @codetitles = @{$codetitlesref}; my $numtitles = @codetitles; my %lt = &Apache::lonlocal::texthash( Select => 'Select', @@ -355,15 +341,92 @@ sub javascript_code_selections { 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{$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{$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'; + } + alert("crscode is ||"+crscode+"|| and currcode is ||"+currcode+"|| and showlist is ||"+showlist+"|| and showbox is ||"+showbox+"||"); + 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_BLOCK +END_OF_JS + } + return $output; } - sub javascript_definitions { my ($codetitles,$idlist,$idlist_titles,$idnums,$cat_titles) = @_; my $numtitles = @{$codetitles}; @@ -454,15 +517,18 @@ END sub instcode_selectors_data { my ($codedom,$formname,$cat_items,$codetitles,$cat_titles,$cat_order,$officialjs) = @_; - my ($jscript,%coursecodes,%codes,%idlist,%idnums,%idlist_titles, - %by_year,%by_sem,%by_dept); - my ($numtitles,$lasttitle); - my $caller = 'global'; - my $totcodes = &retrieve_instcodes(\%coursecodes,$codedom); - if ($totcodes > 0) { - if (&Apache::lonnet::auto_instcode_format($caller,$codedom,\%coursecodes, - \%codes,$codetitles,$cat_titles,$cat_order) eq 'ok') { - &build_code_selections(\%codes,$codetitles,$cat_titles,$cat_order, + 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})); + &build_code_selections($codes,$codetitles,$cat_titles,$cat_order, \%idlist,\%idnums,\%idlist_titles); my ($scripttext,$longtitles) = &javascript_definitions($codetitles,\%idlist,\%idlist_titles, @@ -575,15 +641,22 @@ function setElements() { } sub build_instcode_selectors { - my ($numtitles,$lasttitle,$cat_items,$codetitles,$cat_titles,$cat_order) = @_; + 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 ($env{'form.official'} eq 'off') { + if ($official eq 'off') { $style = ' style="display: none"'; } - $output .= '
'; + if ($id eq '') { + $id = 'instcodes'; + } + my $disabled; + if ($noedit) { + $disabled = ' disabled="disabled"'; + } + $output .= '
'; for (my $k=0; $k<$lasttitle-1; $k++) { my (@items,@unsorted); if (ref($cat_items->{$codetitles->[$k]}) eq 'ARRAY') { @@ -599,8 +672,11 @@ sub build_instcode_selectors { @longitems = @items; } $output .= ''; } $output .= '
'.$codetitles->[$k].'
'."\n". - ''."\n".''."\n"; for (my $i=0; $i<@items; $i++) { if ($longitems[$i] eq '') { $longitems[$i] = $items[$i]; @@ -610,14 +686,18 @@ sub build_instcode_selectors { $output .= '
'.$codetitles->[$lasttitle-1].'
'."\n". - ''."\n". ''."\n".''."\n". '
'."\n"; if ($numtitles > 4) { $output .= '

'.$codetitles->[$numtitles-1].'
'."\n". '
'."\n"; + '"'.$disabled.' />
'."\n"; } $output .= '
'; } @@ -644,7 +724,7 @@ sub instcode_selectors { ''.&mt('No').'
'. &build_instcode_selectors($numtitles,$lasttitle,\%cat_items,$codetitles, - \%cat_titles,\%cat_order)."\n". + \%cat_titles,\%cat_order,$env{'form.official'})."\n". ''."\n". ''."\n"; } @@ -687,7 +767,8 @@ 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". + &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++) { @@ -967,16 +1048,17 @@ sub instcode_search_str { 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 (@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') && ($format_reply eq 'ok')) { + 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++) {