Diff for /loncom/interface/lonsearchcat.pm between versions 1.220 and 1.241

version 1.220, 2004/05/03 19:24:14 version 1.241, 2005/03/22 21:15:42
Line 75  use Apache::lonhtmlcommon; Line 75  use Apache::lonhtmlcommon;
 use Apache::lonlocal;  use Apache::lonlocal;
 use LONCAPA::lonmetadata();  use LONCAPA::lonmetadata();
 use HTML::Entities();  use HTML::Entities();
   use Parse::RecDescent;
   use Apache::lonnavmaps;
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
Line 117  sub handler { Line 119  sub handler {
   
     my $loaderror=&Apache::lonnet::overloaderror($r);      my $loaderror=&Apache::lonnet::overloaderror($r);
     if ($loaderror) { return $loaderror; }      if ($loaderror) { return $loaderror; }
       #
     my $closebutton;  # button that closes the search window       my $closebutton;  # button that closes the search window 
                       # This button is different for the RAT compared to                        # This button is different for the RAT compared to
                       # normal invocation.                        # normal invocation.
Line 174  sub handler { Line 176  sub handler {
                    '&launch='.$ENV{'form.launch'}.                     '&launch='.$ENV{'form.launch'}.
                    '&mode='.$ENV{'form.mode'},                     '&mode='.$ENV{'form.mode'},
               text=>"Course and Catalog Search",                text=>"Course and Catalog Search",
                 target=>'_top',
               bug=>'Searching',});                bug=>'Searching',});
     } else {      } else {
         &Apache::lonhtmlcommon::add_breadcrumb          &Apache::lonhtmlcommon::add_breadcrumb
Line 182  sub handler { Line 185  sub handler {
                    '&launch='.$ENV{'form.launch'}.                     '&launch='.$ENV{'form.launch'}.
                    '&mode='.$ENV{'form.mode'},                     '&mode='.$ENV{'form.mode'},
               text=>"Catalog Search",                text=>"Catalog Search",
                 target=>'_top',
               bug=>'Searching',});                bug=>'Searching',});
     }      }
     #      #
     if (! &get_persistent_form_data($persistent_db_file)) {      if ($ENV{'form.phase'} !~ m/(basic|adv|course)_search/) {
         if ($ENV{'form.phase'} =~ /(run_search|results)/) {          if (! &get_persistent_form_data($persistent_db_file)) {
             &Apache::lonnet::logthis("lonsearchcat:Unable to recover data ".              if ($ENV{'form.phase'} =~ /(run_search|results)/) {
                                      "from $persistent_db_file");                  &Apache::lonnet::logthis('lonsearchcat:'.
             $r->print(<<END);                                           'Unable to recover data from '.
 <html>                                           $persistent_db_file);
 <head><title>LON-CAPA Search Error</title></head>   my $html=&Apache::lonxml::xmlbegin();
    $r->print(<<END);
   $html
   <head>
   <title>LON-CAPA Search Error</title></head>
 $bodytag  $bodytag
 We were unable to retrieve data describing your search.  This is a serious  We were unable to retrieve data describing your search.  This is a serious
 error and has been logged.  Please alert your LON-CAPA administrator.  error and has been logged.  Please alert your LON-CAPA administrator.
 </body>  </body>
 </html>  </html>
 END  END
             return OK;                  return OK;
               }
         }          }
       } else {
           &clean_up_environment();
     }      }
     ##      ##
     ## Clear out old values from groupsearch database      ## Clear out old values from groupsearch database
Line 215  END Line 226  END
  } else {   } else {
             # This is a stupid error to give to the user.                # This is a stupid error to give to the user.  
             # It really tells them nothing.              # It really tells them nothing.
     $r->print('<html><head></head>'.$bodytag.      my $html=&Apache::lonxml::xmlbegin();
       $r->print($html.'<head></head>'.$bodytag.
                       'Unable to tie hash to db file</body></html>');                        'Unable to tie hash to db file</body></html>');
     return OK;      return OK;
  }   }
Line 226  END Line 238  END
     $hidden_fields = '<input type="hidden" name="persistent_db_id" value="'.      $hidden_fields = '<input type="hidden" name="persistent_db_id" value="'.
         $ENV{'form.persistent_db_id'}.'" />'."\n";          $ENV{'form.persistent_db_id'}.'" />'."\n";
     if (exists($ENV{'form.catalogmode'})) {      if (exists($ENV{'form.catalogmode'})) {
         $hidden_fields .= '<input type="hidden" name="catalogmode" value="'.          $hidden_fields .= &hidden_field('catalogmode');
                 $ENV{'form.catalogmode'}.'" />'."\n";  
     }      }
     if (exists($ENV{'form.form'})) {      if (exists($ENV{'form.form'})) {
         $hidden_fields .= '<input type="hidden" name="form" value="'.          $hidden_fields .= &hidden_field('form');
                 $ENV{'form.form'}.'" />'."\n";  
     }      }
     if (exists($ENV{'form.element'})) {      if (exists($ENV{'form.element'})) {
         $hidden_fields .= '<input type="hidden" name="element" value="'.          $hidden_fields .= &hidden_field('element');
                 $ENV{'form.element'}.'" />'."\n";  
     }      }
     if (exists($ENV{'form.titleelement'})) {      if (exists($ENV{'form.titleelement'})) {
         $hidden_fields .= '<input type="hidden" name="titleelement" value="'.          $hidden_fields .= &hidden_field('titleelement');
                 $ENV{'form.titleelement'}.'" />'."\n";  
     }      }
     if (exists($ENV{'form.mode'})) {      if (exists($ENV{'form.mode'})) {
         $hidden_fields .= '<input type="hidden" name="mode" value="'.          $hidden_fields .= &hidden_field('mode');
                 $ENV{'form.mode'}.'" />'."\n";  
     }      }
     ##      ##
     ## Configure dynamic components of interface      ## Configure dynamic components of interface
Line 335  END Line 342  END
         &course_search($r);          &course_search($r);
     } elsif(($ENV{'form.phase'} eq 'basic_search') ||      } elsif(($ENV{'form.phase'} eq 'basic_search') ||
             ($ENV{'form.phase'} eq 'adv_search')) {              ($ENV{'form.phase'} eq 'adv_search')) {
           #
           # We are running a search, try to parse it
           my ($query,$customquery,$customshow,$libraries) = 
               (undef,undef,undef,undef);
           my $pretty_string;
           if ($ENV{'form.phase'} eq 'basic_search') {
               ($query,$pretty_string,$libraries) = 
                   &parse_basic_search($r,$closebutton,$hidden_fields);
               return OK if (! defined($query));
               &make_persistent({ basicexp => $ENV{'form.basicexp'}},
                                $persistent_db_file);
           } else {                      # Advanced search
               ($query,$customquery,$customshow,$libraries,$pretty_string) 
                   = &parse_advanced_search($r,$closebutton,$hidden_fields);
               return OK if (! defined($query));
           }
           &make_persistent({ query => $query,
                              customquery => $customquery,
                              customshow => $customshow,
                              libraries => $libraries,
                              pretty_string => $pretty_string },
                            $persistent_db_file);
           #
         # Set up table          # Set up table
         if (! defined(&create_results_table())) {          if (! defined(&create_results_table())) {
     my $errorstring=&Apache::lonmysql::get_error();      my $errorstring=&Apache::lonmysql::get_error();
             &Apache::lonnet::logthis('lonsearchcat.pm: Unable to create '.              &Apache::lonnet::logthis('lonsearchcat.pm: Unable to create '.
                                      'needed table.  lonmysql error:'.                                       'needed table.  lonmysql error:'.
                                      $errorstring);                                       $errorstring);
       my $html=&Apache::lonxml::xmlbegin();
             $r->print(<<END);              $r->print(<<END);
 <html><head><title>Search Error</title></head>  $html
   <head>
   <title>Search Error</title></head>
 $bodytag  $bodytag
 Unable to create table in which to store search results.    Unable to create table in which to store search results.  
 The search has been aborted.  The search has been aborted.
Line 353  END Line 386  END
         }          }
         delete($ENV{'form.launch'});          delete($ENV{'form.launch'});
         if (! &make_form_data_persistent($r,$persistent_db_file)) {          if (! &make_form_data_persistent($r,$persistent_db_file)) {
             $r->print(<<END);      my $html=&Apache::lonxml::xmlbegin();
 <html><head><title>Search Error</title></head>      $r->print(<<END);
   $html
   <head>
   <title>Search Error</title></head>
 $bodytag  $bodytag
 Unable to properly store search information.  The search has been aborted.  Unable to properly store search information.  The search has been aborted.
 </body>  </body>
Line 362  Unable to properly store search informat Line 398  Unable to properly store search informat
 END  END
             return OK;              return OK;
         }          }
         #  
         # We are running a search  
         my ($query,$customquery,$customshow,$libraries) =   
             (undef,undef,undef,undef);  
         my $pretty_string;  
         if ($ENV{'form.phase'} eq 'basic_search') {  
             ($query,$pretty_string,$libraries) =   
                 &parse_basic_search($r,$closebutton,$hidden_fields);  
         } else {                      # Advanced search  
             ($query,$customquery,$customshow,$libraries,$pretty_string)   
                 = &parse_advanced_search($r,$closebutton,$hidden_fields);  
             return OK if (! defined($query));  
         }  
         &make_persistent({ query => $query,  
                            customquery => $customquery,  
                            customshow => $customshow,  
                            libraries => $libraries,  
                            pretty_string => $pretty_string },  
                          $persistent_db_file);  
         ##          ##
         ## Print out the frames interface          ## Print out the frames interface
         ##          ##
         &print_frames_interface($r);          if (defined($query)) {
               &print_frames_interface($r);
           }
     }      }
     return OK;      return OK;
 }   } 
   
   #
   # The mechanism used to store values away and retrieve them does not
   # handle the case of missing environment variables being significant.
   #
   # This routine sets non existant checkbox form elements to ''.
   #
   sub clean_up_environment {
       if ($ENV{'form.phase'} eq 'basic_search') {
           if (! exists($ENV{'form.related'})) {
               $ENV{'form.related'} = '';
           }
           if (! exists($ENV{'form.domains'})) {
               $ENV{'form.domains'} = '';
           }
       } elsif ($ENV{'form.phase'} eq 'adv_search') {
           foreach my $field ('title','keywords','notes',
                              'abstract','standards','mime') {
               if (! exists($ENV{'form.'.$field.'_related'})) {
                   $ENV{'form.'.$field.'_related'} = '';
               }
           }
       } elsif ($ENV{'form.phase'} eq 'course_search') {
           if (! exists($ENV{'form.crsrelated'})) {
               $ENV{'form.crsrelated'} = '';
           }
       }
   }
   
   sub hidden_field {
       my ($name,$value) = @_;
       if (! defined($value)) {
           $value = $ENV{'form.'.$name};
       }
       return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'.$/;
   }
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
 ##  ##
Line 403  my %alreadyseen; Line 458  my %alreadyseen;
 my %hash;  my %hash;
 my $totalfound;  my $totalfound;
   
   sub make_symb {
       my ($id)=@_;
       my ($mapid,$resid)=split(/\./,$id);
       my $map=$hash{'map_id_'.$mapid};
       my $res=$hash{'src_'.$id};
       my $symb=&Apache::lonnet::encode_symb($map,$resid,$res);
       return $symb;
   }
   
 sub course_search {  sub course_search {
     my $r=shift;      my $r=shift;
     my $bodytag=&Apache::loncommon::bodytag('Course Search');      my $bodytag=&Apache::loncommon::bodytag('Course Search');
     my $pretty_search_string = '<b>'.$ENV{'form.courseexp'}.'</b>';      my $pretty_search_string = '<b>'.$ENV{'form.courseexp'}.'</b>';
     my $search_string = $ENV{'form.courseexp'};      my $search_string = $ENV{'form.courseexp'};
     my @New_Words;      my @New_Words;
       undef(%alreadyseen);
     if ($ENV{'form.crsrelated'}) {      if ($ENV{'form.crsrelated'}) {
         ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'});          ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'});
         if (@New_Words) {          if (@New_Words) {
Line 418  sub course_search { Line 483  sub course_search {
         }          }
     }      }
     my $fulltext=$ENV{'form.crsfulltext'};      my $fulltext=$ENV{'form.crsfulltext'};
       my $discuss=$ENV{'form.crsdiscuss'};
     my @allwords=($search_string,@New_Words);      my @allwords=($search_string,@New_Words);
     $totalfound=0;      $totalfound=0;
     $r->print('<html><head><title>LON-CAPA Course Search</title></head>'.      my $html=&Apache::lonxml::xmlbegin();
       $bodytag.'<hr /><center><font size="+2" face="arial">'.$pretty_search_string.'</font></center><hr />');      $r->print($html.'<head><title>LON-CAPA Course Search</title></head>'.
         $bodytag.'<hr /><center><font size="+2" face="arial">'.$pretty_search_string.'</font></center><hr /><b>'.&mt('Course content').':</b><br />');
     $r->rflush();      $r->rflush();
 # ======================================================= Go through the course  # ======================================================= Go through the course
     undef %alreadyseen;  
     %alreadyseen=();  
     my $c=$r->connection;      my $c=$r->connection;
     if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",      if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
             &GDBM_READER(),0640)) {              &GDBM_READER(),0640)) {
         foreach (keys %hash) {          foreach (sort(keys(%hash))) {
             if ($c->aborted()) { last; }              if ($c->aborted()) { last; }
             if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) {              if (($_=~/^src\_(.+)$/)) {
                 &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,   if ($hash{'randomout_'.$1} & !$ENV{'request.role.adv'}) {
                              @allwords);      next; 
    }
    my $symb=&make_symb($1);
                   &checkonthis($r,$1,$hash{$_},0,&Apache::lonnet::gettitle($symb),
        $fulltext,$symb,@allwords);
             }              }
         }          }
         untie(%hash);          untie(%hash);
     }      }
     unless ($totalfound) {      unless ($totalfound) {
  $r->print('<p>'.&mt('No resources found').'.</p>');   $r->print('<p>'.&mt('No matches found in resources').'.</p>');
     }      }
   
   # Check discussions if requested
       if ($discuss) {
           my $totaldiscussions = 0;
           $r->print('<br /><br /><b>'.&mt('Discussion postings').':</b><br />'); 
           my $navmap = Apache::lonnavmaps::navmap->new();
           my @allres=$navmap->retrieveResources();
           my %discussiontime = &Apache::lonnet::dump('discussiontimes',
                                  $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                                  $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
           foreach my $resource (@allres) {
               my $result = '';
               my $applies = 0;
               my $symb = $resource->symb();
               my $ressymb = $symb;
               if ($symb =~ m#(___adm/\w+/\w+)/(\d+)/bulletinboard$#) {
                   $ressymb = 'bulletin___'.$2.$1.'/'.$2.'/bulletinboard';
                   unless ($ressymb =~ m#bulletin___\d+___adm/wrapper#) {
                       $ressymb=~s#(bulletin___\d+___)#$1adm/wrapper/#;
                   }
               }
               if (defined($discussiontime{$ressymb})) { 
                   my %contrib = &Apache::lonnet::restore($ressymb,$ENV{'request.course.id'},
                        $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
                        $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
                   if ($contrib{'version'}) {
                       for (my $id=1;$id<=$contrib{'version'};$id++) {
                           unless (($contrib{'hidden'}=~/\.$id\./) || ($contrib{'deleted'}=~/\.$id\./)) { 
                               if ($contrib{$id.':subject'}) {
                                   $result .= $contrib{$id.':subject'};
                               }
                               if ($contrib{$id.':message'}) {
                                   $result .= $contrib{$id.':message'};
                               }
                               if ($contrib{$id,':attachmenturl'}) {
                                   if ($contrib{$id,':attachmenturl'} =~ m-/([^/]+)$-) {
                                       $result .= $1;
                                   }
                               }
                               $applies = &checkwords($result,$applies,@allwords);
                           }
                       }
                   }
               }
   # Does this discussion apply?
               if ($applies) {
                   my ($map,$ind,$url)=&Apache::lonnet::decode_symb($ressymb);
                   my $disctype = &mt('resource');
                   if ($url =~ m#/bulletinboard$#) {
                       if ($url =~m#^adm/wrapper/adm/.*/bulletinboard$#) {
                           $url =~s#^adm/wrapper##;
                       }
                       $disctype = &mt('bulletin board');
                   } else {
                       $url = '/res/'.$url;
                   }
                   if ($url =~ /\?/) {
                       $url .= '&symb=';
                   } else {
                       $url .= '?symb=';
                   }
                   $url .= &Apache::lonnet::escape($resource->symb());
                   my $title = $resource->compTitle();
                   $r->print('<br /><a href="'.$url.'" target="cat">'.
                        ($title?$title:$url).'</a>&nbsp;&nbsp;-&nbsp;'.$disctype.'<br />');
                   $totaldiscussions++;
               } else {
                   $r->print(' .');
               }
           }
           unless ($totaldiscussions) {
               $r->print('<p>'.&mt('No matches found in postings').'.</p>');
           }
       }
    
 # =================================================== Done going through course  # =================================================== Done going through course
     $r->print('</body></html>');      $r->print('</body></html>');
 }  }
Line 448  sub course_search { Line 592  sub course_search {
 # =============================== This pulls up a resource and its dependencies  # =============================== This pulls up a resource and its dependencies
   
 sub checkonthis {  sub checkonthis {
     my ($r,$url,$level,$title,$fulltext,@allwords)=@_;      my ($r,$id,$url,$level,$title,$fulltext,$symb,@allwords)=@_;
     $alreadyseen{$url}=1;      $alreadyseen{$id}=1;
       if (&Apache::loncommon::connection_aborted($r)) { return; }
     $r->rflush();      $r->rflush();
     my $result=&Apache::lonnet::metadata($url,'title').' '.      
                &Apache::lonnet::metadata($url,'subject').' '.      my $result=$title.' ';
                &Apache::lonnet::metadata($url,'abstract').' '.      if ($ENV{'request.role.adv'} || !$hash{'encrypted_'.$id}) {
                &Apache::lonnet::metadata($url,'keywords');   $result.=&Apache::lonnet::metadata($url,'title').' '.
     if (($url) && ($fulltext)) {      &Apache::lonnet::metadata($url,'subject').' '.
  $result.=&Apache::lonnet::ssi_body($url);      &Apache::lonnet::metadata($url,'abstract').' '.
       &Apache::lonnet::metadata($url,'keywords');
       }
       my ($extension)=($url=~/\.(\w+)$/);
       if (&Apache::loncommon::fileembstyle($extension) eq 'ssi' &&
    ($url) && ($fulltext)) {
    $result.=&Apache::lonnet::ssi_body($url.'?symb='.&Apache::lonnet::escape($symb));
     }      }
     $result=~s/\s+/ /gs;      $result=~s/\s+/ /gs;
     my $applies=0;      my $applies = 0;
     foreach (@allwords) {      $applies = &checkwords($result,$applies,@allwords);
         if ($_=~/\w/) {  
    if ($result=~/$_/si) {  
       $applies++;  
            }  
        }  
     }  
 # Does this resource apply?  # Does this resource apply?
     if ($applies) {      if ($applies) {
        $r->print('<br />');         $r->print('<br />');
        for (my $i=0;$i<=$level*5;$i++) {         for (my $i=0;$i<=$level*5;$i++) {
            $r->print('&nbsp;');             $r->print('&nbsp;');
        }         }
        $r->print('<a href="'.$url.'" target="cat">'.         my $href=$url;
  ($title?$title:$url).'</a><br />');         if ($hash{'encrypted_'.$id} && !$ENV{'request.role.adv'}) {
      $href=&Apache::lonenc::encrypted($href)
          .'?symb='.&Apache::lonenc::encrypted($symb);
          } else {
      $href.='?symb='.&Apache::lonnet::escape($symb);
          }
          $r->print('<a href="'.$href.'" target="cat">'.($title?$title:$url).
    '</a><br />');
        $totalfound++;         $totalfound++;
     } elsif ($fulltext) {      } elsif ($fulltext) {
        $r->print(' .');         $r->print(' .');
Line 484  sub checkonthis { Line 636  sub checkonthis {
     my $dependencies=      my $dependencies=
                 &Apache::lonnet::metadata($url,'dependencies');                  &Apache::lonnet::metadata($url,'dependencies');
     foreach (split(/\,/,$dependencies)) {      foreach (split(/\,/,$dependencies)) {
        if (($_=~/^\/res\//) && (!$alreadyseen{$_})) {         if (($_=~/^\/res\//) && (!$alreadyseen{$id})) { 
           &checkonthis($r,$_,$level+1,'',$fulltext,@allwords);            &checkonthis($r,$id,$_,$level+1,'',$fulltext,undef,@allwords);
        }         }
     }      }
 }  }
   
   sub checkwords {
       my ($result,$applies,@allwords) = @_;
       foreach (@allwords) {
           if ($_=~/\w/) {
               if ($result=~/$_/si) {
                   $applies++;
               }
           }
       }
       return $applies;
   }
   
 sub untiehash {  sub untiehash {
     if (tied(%hash)) {      if (tied(%hash)) {
         untie(%hash);          untie(%hash);
Line 499  sub untiehash { Line 663  sub untiehash {
 } # End of course search scoping  } # End of course search scoping
   
 sub search_html_header {  sub search_html_header {
       my $html=&Apache::lonxml::xmlbegin();
     my $Str = <<ENDHEADER;      my $Str = <<ENDHEADER;
 <html>  $html
 <head>  <head>
 <title>The LearningOnline Network with CAPA</title>  <title>The LearningOnline Network with CAPA</title>
 </head>  </head>
Line 523  Prints the form for the basic search.  S Line 688  Prints the form for the basic search.  S
 ######################################################################  ######################################################################
 sub print_basic_search_form {  sub print_basic_search_form {
     my ($r,$closebutton,$hidden_fields) = @_;      my ($r,$closebutton,$hidden_fields) = @_;
       my $result = ($ENV{'form.catalogmode'} ne 'groupsearch');
     my $bodytag=&Apache::loncommon::bodytag('Search').      my $bodytag=&Apache::loncommon::bodytag('Search').
         &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching','Searching',          &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching','Search_Basic',
                                             undef,undef,! $ENV{'form.launch'});                                     undef,undef,
                                      $ENV{'form.catalogmode'} ne 'groupsearch');
     my $scrout = &search_html_header().$bodytag;      my $scrout = &search_html_header().$bodytag;
     if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) {      if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) {
         # Define interface components          # Define interface components
         my $userelatedwords=          my $userelatedwords= '<label>'.
             &mt('[_1] use related words',              &mt('[_1] use related words',
                 &Apache::lonhtmlcommon::checkbox                  &Apache::lonhtmlcommon::checkbox
                 ('related',$ENV{'form.related'}));                  ('related',$ENV{'form.related'},'related')).'</label>';
         my $onlysearchdomain=          my $onlysearchdomain='<label>'.
             &mt('[_1] only search domain [_2]',              &mt('[_1] only search domain [_2]',
                 &Apache::lonhtmlcommon::checkbox                  &Apache::lonhtmlcommon::checkbox('domains',
                 ('domains',$ENV{'form.domains'}),                                                   $ENV{'form.domains'},
                 $r->dir_config('lonDefDomain'));                                                   $r->dir_config('lonDefDomain')
                                                    ),
                   $r->dir_config('lonDefDomain')
                   ).'</label>';
         my $adv_search_link =           my $adv_search_link = 
             '<a href="/adm/searchcat?'.              '<a href="/adm/searchcat?'.
             'phase=disp_adv&'.              'phase=disp_adv&'.
Line 560  sub print_basic_search_form { Line 730  sub print_basic_search_form {
         }          }
         $scrout.='<table>'.          $scrout.='<table>'.
             '<tr><td align="center" valign="top">'.              '<tr><td align="center" valign="top">'.
             &Apache::lonhtmlcommon::textbox('basicexp',              &Apache::lonhtmlcommon::textbox
                                             $ENV{'form.basicexp'},50).'<br />'.              ('basicexp',
                &HTML::Entities::encode($ENV{'form.basicexp'},'<>&"'),50
                ).
                '<br />'.
             '<font size="-1">'.&searchhelp().'</font>'.'</td>'.              '<font size="-1">'.&searchhelp().'</font>'.'</td>'.
             '<td><font size="-1">'.              '<td><font size="-1">'.
             '<nobr>'.('&nbsp;'x3).$adv_search_link.'</nobr>'.'<br />'.              '<nobr>'.('&nbsp;'x3).$adv_search_link.'</nobr>'.'<br />'.
Line 570  sub print_basic_search_form { Line 743  sub print_basic_search_form {
             '</font></td>'.              '</font></td>'.
             '</tr>'.$/;              '</tr>'.$/;
         #          #
 #        $scrout .= '<tr><td align="center">'.  
 #            '<font size="-1">'.  
 #            $userelatedwords.('&nbsp;'x3).  
 #            $onlysearchdomain.('&nbsp;'x2).$adv_search_link.  
 #            '</font>'.  
 #            '</td></tr>'.$/;  
         $scrout .= '<tr><td align="center" colspan="2">'.          $scrout .= '<tr><td align="center" colspan="2">'.
             '<font size="-1">'.              '<font size="-1">'.
             '<input type="submit" name="basicsubmit" '.              '<input type="submit" name="basicsubmit" '.
             'value="'.&mt('Search').'" />'.              'value="'.&mt('Search').'" />'.
             ('&nbsp;'x2).$closebutton.('&nbsp;'x2).&viewoptions().              ('&nbsp;'x2).$closebutton.('&nbsp;'x2).
               &viewoptions().
             '</font>'.              '</font>'.
             '</td></tr>'.$/;              '</td></tr>'.$/;
         $scrout .= '</table>'.$/.'</center>'.'</form>';          $scrout .= '</table>'.$/.'</center>'.'</form>';
Line 590  sub print_basic_search_form { Line 758  sub print_basic_search_form {
                                            'header' => 'Course Search',                                             'header' => 'Course Search',
  'note' => 'Enter terms or phrases, then press "Search" below',   'note' => 'Enter terms or phrases, then press "Search" below',
  'use' => 'use related words',   'use' => 'use related words',
  'full' =>'fulltext search (time consuming)'   'full' =>'fulltext search (time consuming)',
            'disc' => 'search discussion postings (resources and bulletin boards)',
    );     );
         $scrout.=(<<ENDCOURSESEARCH);          $scrout.=(<<ENDCOURSESEARCH);
 <form name="loncapa_search" method="post" action="/adm/searchcat">  <form name="loncapa_search" method="post" action="/adm/searchcat">
Line 615  ENDCOURSESEARCH Line 784  ENDCOURSESEARCH
         my $relcheckbox =           my $relcheckbox = 
             &Apache::lonhtmlcommon::checkbox('crsrelated',              &Apache::lonhtmlcommon::checkbox('crsrelated',
    $ENV{'form.crsrelated'});     $ENV{'form.crsrelated'});
           my $discheckbox = 
               &Apache::lonhtmlcommon::checkbox('crsdiscuss',
                                      $ENV{'form.crsrelated'});
         $scrout.=(<<ENDENDCOURSE);          $scrout.=(<<ENDENDCOURSE);
 </td></tr>  </td></tr>
 <tr><td>$relcheckbox $lt{'use'}</td><td></td></tr>  <tr><td><label>$relcheckbox $lt{'use'}</label></td><td></td></tr>
 <tr><td>$crscheckbox $lt{'full'}</td><td></td></tr>  <tr><td><label>$crscheckbox $lt{'full'}</label></td><td></td></tr>
   <tr><td><label>$discheckbox $lt{'disc'}</label></td><td></td></tr>
 </table><p>  </table><p>
 &nbsp;<input type="submit" name="coursesubmit" value='$lt{'srch'}' />  &nbsp;<input type="submit" name="coursesubmit" value='$lt{'srch'}' />
 </p>  </p>
Line 650  sub print_advanced_search_form{ Line 823  sub print_advanced_search_form{
     my ($r,$closebutton,$hidden_fields) = @_;      my ($r,$closebutton,$hidden_fields) = @_;
     my $bodytag=&Apache::loncommon::bodytag('Advanced Catalog Search').      my $bodytag=&Apache::loncommon::bodytag('Advanced Catalog Search').
         &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching',          &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching',
                                             'Searching',                                              'Search_Advanced',
                                             undef,undef,                                              undef,undef,
                                             ! $ENV{'form.launch'});                                    $ENV{'form.catalogmode'} ne 'groupsearch');
   
     my %lt=&Apache::lonlocal::texthash('srch' => 'Search',      my %lt=&Apache::lonlocal::texthash('srch' => 'Search',
        'reset' => 'Reset',         'reset' => 'Reset',
        'help' => 'Help');         'help' => 'Help');
Line 734  ENDHEADER Line 906  ENDHEADER
     $scrout.='<tr><td align="right" valign="top">'.      $scrout.='<tr><td align="right" valign="top">'.
  &titlefield(&mt('Copyright/Distribution')).'</td><td colspan="2">'.   &titlefield(&mt('Copyright/Distribution')).'</td><td colspan="2">'.
         &Apache::lonmeta::selectbox('copyright',          &Apache::lonmeta::selectbox('copyright',
                                     '',,                                      $ENV{'form.copyright'},
                                     \&Apache::loncommon::copyrightdescription,                                      \&Apache::loncommon::copyrightdescription,
                                     ( undef,                                      ( undef,
                                       &Apache::loncommon::copyrightids)                                        &Apache::loncommon::copyrightids)
Line 742  ENDHEADER Line 914  ENDHEADER
     $scrout.='<tr><td align="right" valign="top">'.      $scrout.='<tr><td align="right" valign="top">'.
  &titlefield(&mt('Language')).'</td><td colspan="2">'.   &titlefield(&mt('Language')).'</td><td colspan="2">'.
         &Apache::lonmeta::selectbox('language',          &Apache::lonmeta::selectbox('language',
                                     'notset',,                                      $ENV{'form.language'},
                                     \&Apache::loncommon::languagedescription,                                      \&Apache::loncommon::languagedescription,
                                     ('any',&Apache::loncommon::languageids)                                      ('any',&Apache::loncommon::languageids)
                                     ).'</td></tr>';                                      ).'</td></tr>';
Line 754  ENDHEADER Line 926  ENDHEADER
     $scrout .= '<tr><td>&nbsp;</td><td align="center">'.&mt('Minimum').'</td>'.      $scrout .= '<tr><td>&nbsp;</td><td align="center">'.&mt('Minimum').'</td>'.
         '<td align="center">'.&mt('Maximum').'</td></tr>'."\n";          '<td align="center">'.&mt('Maximum').'</td></tr>'."\n";
     foreach my $statistic       foreach my $statistic 
         ({ name=>'stdno',          ({ name=>'count',
            description=>'Number of Students',},             description=>'Network-wide number of accesses (hits)',},
            { name=>'stdno',
              description=>
                  'Total number of students who have worked on this problem',},
          { name => 'avetries',           { name => 'avetries',
            description=>'Average tries to answer correctly',},             description=>'Average number of tries till solved',},
          { name => 'difficulty',           { name => 'difficulty',
            description=>'Degree of difficulty',},             description=>'Degree of difficulty',},
          { name => 'disc',           { name => 'disc',
Line 920  Outputs: text for box with view options Line 1095  Outputs: text for box with view options
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
 sub viewoptions {  sub viewoptions {
     my $scrout="\n".'<nobr>';      my $scrout;
     if (! defined($ENV{'form.viewselect'})) {       if (! defined($ENV{'form.viewselect'})) { 
         $ENV{'form.viewselect'}='detailed';           $ENV{'form.viewselect'}='detailed'; 
     }      }
Line 954  Outputs: return little blurb on how to e Line 1129  Outputs: return little blurb on how to e
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
 sub searchhelp {  sub searchhelp {
     return &mt('Enter terms or phrases separated by AND, OR, or NOT');      return &mt('Enter words and quoted phrases');
 }  }
   
 ######################################################################  ######################################################################
Line 1145  Parse advanced search form and return th Line 1320  Parse advanced search form and return th
 sub parse_advanced_search {  sub parse_advanced_search {
     my ($r,$closebutton,$hidden_fields)=@_;      my ($r,$closebutton,$hidden_fields)=@_;
     my @BasicFields = ('title','author','subject','keywords','url','version',      my @BasicFields = ('title','author','subject','keywords','url','version',
                        'notes','abstract','extension','owner',                         'notes','abstract','extension','owner','authorspace',
 #                       'custommetadata','customshow',  #                       'custommetadata','customshow',
                        'modifyinguser','standards','mime');                         'modifyinguser','standards','mime');
     my @StatsFields = ('stdno','avetries','difficulty','disc');      my @StatsFields = &statfields();
     my @EvalFields = ('clear','depth','helpful','correct','technical');      my @EvalFields = &evalfields();
     my $fillflag=0;      my $fillflag=0;
     my $pretty_search_string = "<br />\n";      my $pretty_search_string = "";
     # Clean up fields for safety      # Clean up fields for safety
     for my $field (@BasicFields,      for my $field (@BasicFields,
                    'creationdatestart_month','creationdatestart_day',                     'creationdatestart_month','creationdatestart_day',
Line 1161  sub parse_advanced_search { Line 1336  sub parse_advanced_search {
    'lastrevisiondatestart_year','lastrevisiondateend_month',     'lastrevisiondatestart_year','lastrevisiondateend_month',
    'lastrevisiondateend_day','lastrevisiondateend_year') {     'lastrevisiondateend_day','lastrevisiondateend_year') {
  $ENV{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\']//g;   $ENV{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\']//g;
         $ENV{'form.'.$field}=~s/(not\s*$|^\s*(and|or)|)//gi;  
     }      }
     foreach ('mode','form','element') {      foreach ('mode','form','element') {
  # is this required?  Hmmm.   # is this required?  Hmmm.
Line 1174  sub parse_advanced_search { Line 1348  sub parse_advanced_search {
                                       ref($ENV{'form.category'}));                                        ref($ENV{'form.category'}));
     #      #
     # Check to see if enough information was filled in      # Check to see if enough information was filled in
     for my $field (@BasicFields) {      foreach my $field (@BasicFields) {
  if (&filled($ENV{'form.'.$field})) {   if (&filled($ENV{'form.'.$field})) {
     $fillflag++;      $fillflag++;
  }   }
     }      }
       foreach my $field (@StatsFields,@EvalFields) {
           if (&filled($ENV{'form.'.$field.'_max'})) {
               $fillflag++;
           }
           if (&filled($ENV{'form.'.$field.'_min'})) {
               $fillflag++;
           }
       }
   
     for my $field ('lowestgradelevel','highestgradelevel') {      for my $field ('lowestgradelevel','highestgradelevel') {
         if ( $ENV{'form.'.$field} =~ /^\d+$/ &&          if ( $ENV{'form.'.$field} =~ /^\d+$/ &&
              $ENV{'form.'.$field} > 0) {               $ENV{'form.'.$field} > 0) {
Line 1196  sub parse_advanced_search { Line 1379  sub parse_advanced_search {
     my $font = '<font color="#800000" face="helvetica">';      my $font = '<font color="#800000" face="helvetica">';
     # Evaluate logical expression AND/OR/NOT phrase fields.      # Evaluate logical expression AND/OR/NOT phrase fields.
     foreach my $field (@BasicFields) {      foreach my $field (@BasicFields) {
  if ($ENV{'form.'.$field}) {   next if (!defined($ENV{'form.'.$field}) || $ENV{'form.'.$field} eq '');
             my $searchphrase = $ENV{'form.'.$field};          my ($error,$SQLQuery) = 
             $pretty_search_string .= $font."$field</font> contains <b>".              &process_phrase_input($ENV{'form.'.$field},
                 $searchphrase."</b>";                                    $ENV{'form.'.$field.'_related'},$field);
           if (defined($error)) {
               &output_unparsed_phrase_error($r,$closebutton,'phase=disp_adv',
                                            $hidden_fields,$field);
               return;
           } else {
               $pretty_search_string .= 
                   $font.$field.'</font>: '.$ENV{'form.'.$field};
             if ($ENV{'form.'.$field.'_related'}) {              if ($ENV{'form.'.$field.'_related'}) {
                 my @New_Words;                  my @Words = 
                 ($searchphrase,@New_Words) = &related_version($searchphrase);                      &Apache::loncommon::get_related_words
                 if (@New_Words) {                      ($ENV{'form.'.$field});
                     $pretty_search_string .= " with related words: ".                  if (@Words) {
                         "<b>@New_Words</b>.";                      $pretty_search_string.= ' with related words: '.
                           join(', ',@Words[0..4]);
                 } else {                  } else {
                     $pretty_search_string .= " with no related words.";                      $pretty_search_string.= ' with related words.';
                 }                  }
             }              }
             $pretty_search_string .= "<br />\n";              $pretty_search_string .= '<br />';
     push @queries,&build_SQL_query($field,$searchphrase);              push (@queries,$SQLQuery);
         }          }
     }      }
     #      #
Line 1228  sub parse_advanced_search { Line 1419  sub parse_advanced_search {
         }          }
     }      }
     if (defined($searchphrase)) {      if (defined($searchphrase)) {
         push @queries,&build_SQL_query('mime',$searchphrase);          my ($error,$SQLsearch) = &process_phrase_input($searchphrase,0,'mime');
           push @queries,$SQLsearch;
         $pretty_search_string .=$font.'mime</font> contains <b>'.          $pretty_search_string .=$font.'mime</font> contains <b>'.
             $searchphrase.'</b><br />';              $searchphrase.'</b><br />';
     }      }
Line 1260  sub parse_advanced_search { Line 1452  sub parse_advanced_search {
  push @queries,"(copyright like \"$ENV{'form.copyright'}\")";   push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
         $pretty_search_string.=$font."copyright</font> = ".          $pretty_search_string.=$font."copyright</font> = ".
             &Apache::loncommon::copyrightdescription($ENV{'form.copyright'}).              &Apache::loncommon::copyrightdescription($ENV{'form.copyright'}).
                 "<br \>\n";                  "<br />\n";
     }      }
     #      #
     # Statistics      # Statistics
Line 1343  sub parse_advanced_search { Line 1535  sub parse_advanced_search {
     ##      ##
     ## Deal with restrictions to given domains      ## Deal with restrictions to given domains
     ##       ## 
     my ($libraries_to_query,$pretty_domains_string) =       my ($libraries_to_query,$pretty_domains_string,$domain_sql_restriction) = 
         &parse_domain_restrictions();          &parse_domain_restrictions();
       push(@queries,$domain_sql_restriction);
     $pretty_search_string .= $pretty_domains_string."<br />\n";      $pretty_search_string .= $pretty_domains_string."<br />\n";
     #      #
     if (@queries) {      if (@queries) {
  $query="select * from metadata where ".join(" AND ",@queries);   $query="SELECT * FROM metadata WHERE (".join(") AND (",@queries).')';
     } elsif ($customquery) {      } elsif ($customquery) {
         $query = '';          $query = '';
     }      }
 #    &Apache::lonnet::logthis('query = '.$/.$query);      #&Apache::lonnet::logthis('advanced query = '.$/.$query);
     return ($query,$customquery,$customshow,$libraries_to_query,      return ($query,$customquery,$customshow,$libraries_to_query,
             $pretty_search_string);              $pretty_search_string);
 }  }
Line 1361  sub parse_domain_restrictions { Line 1554  sub parse_domain_restrictions {
     my $libraries_to_query = undef;      my $libraries_to_query = undef;
     # $ENV{'form.domains'} can be either a scalar or an array reference.      # $ENV{'form.domains'} can be either a scalar or an array reference.
     # We need an array.      # We need an array.
     if (! exists($ENV{'form.domains'})) {      if (! exists($ENV{'form.domains'}) || $ENV{'form.domains'} eq '') {
         return (undef,'');          return (undef,'',undef);
     }      }
     my @allowed_domains;      my @allowed_domains;
     if (ref($ENV{'form.domains'})) {      if (ref($ENV{'form.domains'})) {
Line 1373  sub parse_domain_restrictions { Line 1566  sub parse_domain_restrictions {
     #      #
     my %domain_hash = ();      my %domain_hash = ();
     my $pretty_domains_string;      my $pretty_domains_string;
       my $domain_sql_restriction;
     foreach (@allowed_domains) {      foreach (@allowed_domains) {
         $domain_hash{$_}++;          $domain_hash{$_}++;
     }      }
     if ($domain_hash{'any'}) {      if ($domain_hash{'any'}) {
         $pretty_domains_string = "In all LON-CAPA domains.";          $pretty_domains_string = "In all LON-CAPA domains.";
           $domain_sql_restriction = undef;
     } else {      } else {
         if (@allowed_domains > 1) {          if (@allowed_domains > 1) {
             $pretty_domains_string = "In LON-CAPA domains:";              $pretty_domains_string = "In LON-CAPA domains:";
         } else {          } else {
             $pretty_domains_string = "In LON-CAPA domain ";              $pretty_domains_string = "In LON-CAPA domain ";
         }          }
           $domain_sql_restriction = 
               '(domain="'.join('" OR domain="',@allowed_domains).'")';
         foreach (sort @allowed_domains) {          foreach (sort @allowed_domains) {
             $pretty_domains_string .= "<b>".$_."</b> ";              $pretty_domains_string .= "<b>".$_."</b> ";
         }          }
Line 1393  sub parse_domain_restrictions { Line 1590  sub parse_domain_restrictions {
             }              }
         }          }
     }      }
     return ($libraries_to_query,$pretty_domains_string);      return ($libraries_to_query,
               $pretty_domains_string,
               $domain_sql_restriction);
 }  }
   
 ######################################################################  ######################################################################
Line 1414  sub parse_basic_search { Line 1613  sub parse_basic_search {
     #      #
     # Clean up fields for safety      # Clean up fields for safety
     for my $field ('basicexp') {      for my $field ('basicexp') {
  $ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;   $ENV{"form.$field"}=~s/[^\w\s\'\"\!\(\)\-]//g;
     }      }
     foreach ('mode','form','element') {      foreach ('mode','form','element') {
  # is this required?  Hmmm.   # is this required?  Hmmm.
Line 1422  sub parse_basic_search { Line 1621  sub parse_basic_search {
  $ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});   $ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
  $ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;   $ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
     }      }
     my ($libraries_to_query,$pretty_domains_string) =       my ($libraries_to_query,$pretty_domains_string,$domain_sql_restriction) = 
         &parse_domain_restrictions();          &parse_domain_restrictions();
     #      #
     # Check to see if enough of a query is filled in      # Check to see if enough of a query is filled in
     my $search_string = $ENV{'form.basicexp'};      my $search_string = $ENV{'form.basicexp'};
     $search_string =~ s/(not\s*$|^\s*(and|or)|)//gi;  
     if (! &filled($search_string)) {      if (! &filled($search_string)) {
  &output_blank_field_error($r,$closebutton,'phase=disp_basic');   &output_blank_field_error($r,$closebutton,'phase=disp_basic');
  return OK;   return OK;
     }      }
     my $pretty_search_string = '<b>'.$ENV{'form.basicexp'}.'</b>';      my $pretty_search_string=$search_string;
     if ($ENV{'form.related'}) {      my @Queries;
         my @New_Words;      my $searchfield = 'concat_ws(" ",'.join(',',
         ($search_string,@New_Words) = &related_version($ENV{'form.basicexp'});                                              ('title','author','subject',
         if (@New_Words) {                                               'notes','abstract','keywords')
             $pretty_search_string .= " with related words: <b>@New_Words</b>.";                                              ).')';
         } else {      my ($error,$SQLQuery) = &process_phrase_input($search_string,
             $pretty_search_string .= " with no related words.";                                                      $ENV{'form.related'},
         }                                                      $searchfield);
       if ($error) {
           &output_unparsed_phrase_error($r,$closebutton,'phase=disp_basic',
                                         '','basicexp');
           return;
     }      }
       push(@Queries,$SQLQuery);
       if (defined($domain_sql_restriction) && $domain_sql_restriction ne '') {
           push(@Queries,$domain_sql_restriction);
       }
       #foreach my $q (@Queries) {
       #    &Apache::lonnet::logthis('    '.$q);
       #}
       my $final_query = 'SELECT * FROM metadata WHERE '.join(" AND ",@Queries);
     #      #
     # Build SQL query string based on form page  
     my $query='';  
     my $concatarg=join(',',  
        ('title', 'author', 'subject', 'notes', 'abstract',  
                         'keywords'));  
     $concatarg='title' if $ENV{'form.titleonly'};  
     $query=&build_SQL_query('concat_ws(" ",'.$concatarg.')',$search_string);  
     if (defined($pretty_domains_string) && $pretty_domains_string ne '') {      if (defined($pretty_domains_string) && $pretty_domains_string ne '') {
         $pretty_search_string .= ' '.$pretty_domains_string;          $pretty_search_string .= ' '.$pretty_domains_string;
     }      }
     $pretty_search_string .= "<br />\n";      $pretty_search_string .= "<br />\n";
     my $final_query = 'SELECT * FROM metadata WHERE '.$query;      $pretty_search_string =~ s:^<br /> and ::;
     # &Apache::lonnet::logthis($final_query);      #&Apache::lonnet::logthis('simple search final query = '.$/.$final_query);
     return ($final_query,$pretty_search_string,      return ($final_query,$pretty_search_string,
             $libraries_to_query);              $libraries_to_query);
 }  }
   
   
   ###############################################################
   ###############################################################
   
   my @Phrases;
   
   sub concat {
       my ($item) = @_;
       my $results = '';
       foreach (@$item) {
           if (ref($_) eq 'ARRAY') {
               $results .= join(' ',@$_);
           }
       }
       return $results;
   }
   
   sub process_phrase_input {
       my ($phrase,$related,$field)=@_;
       #&Apache::lonnet::logthis('phrase = :'.$phrase.':');
       my $grammar = <<'ENDGRAMMAR';
       searchphrase:
           expression /^\Z/ {
               # &Apache::lonsearchcat::print_item(\@item,0);
               [@item];
           }
       expression:
           phrase(s)   {
               [@item];
           }
       phrase:
           orword {
               [@item];
           }
         | andword {
               [@item];
           }
         | minusword {
               unshift(@::Phrases,$item[1]->[0]);
               unshift(@::Phrases,$item[1]->[1]);
               [@item];
           }
         | word {
               unshift(@::Phrases,$item[1]);
               [@item];
           } 
       #
       orword:
           word 'OR' phrase {
               unshift(@::Phrases,'OR');
               unshift(@::Phrases,$item[1]);
               [@item];
           }
           | word 'or' phrase {
               unshift(@::Phrases,'OR');
               unshift(@::Phrases,$item[1]);
               [@item];
           }    
           | minusword 'OR' phrase {
               unshift(@::Phrases,'OR');
               unshift(@::Phrases,$item[1]->[0]);
               unshift(@::Phrases,$item[1]->[1]);
               [@item];
           }
           | minusword 'or' phrase {
               unshift(@::Phrases,'OR');
               unshift(@::Phrases,$item[1]->[0]);
               unshift(@::Phrases,$item[1]->[1]);
               [@item];
           }    
       andword:
           word phrase {
               unshift(@::Phrases,'AND');
               unshift(@::Phrases,$item[1]);
               [@item];
           }
           | minusword phrase {
               unshift(@::Phrases,'AND');
               unshift(@::Phrases,$item[1]->[0]);
               unshift(@::Phrases,$item[1]->[1]);
               [@item];
           }
       #
       minusword:
           '-' word {
               [$item[2],'NOT'];
           }
       word:
           "'" term(s) "'" {
             &Apache::lonsearchcat::concat(\@item);
           }
         | '"' term(s) '"' {
             &Apache::lonsearchcat::concat(\@item);
           }
         | term {
               $item[1];
           }
       term:
           /[\w\Q:!@#$%^&*()+_=|{}<>,.;\\\/?\E]+/ {
               $item[1];
           }
   ENDGRAMMAR
       #
       # The end result of parsing the phrase with the grammar is an array
       # @::Phrases.
       # $phrase = "gene splicing" or cat -> "gene splicing","OR","cat"
       # $phrase = "genetic engineering" -dna ->
       #                      "genetic engineering","AND","NOT","dna"
       # $phrase = cat or dog -poodle -> "cat","OR","dog","AND","NOT","poodle"
       undef(@::Phrases);
       my $p = new Parse::RecDescent($grammar);
       if (! defined($p->searchphrase($phrase))) {
           &Apache::lonnet::logthis('lonsearchcat:unable to process:'.$phrase);
           return 'Unable to process phrase '.$phrase;
       }
       #
       # Go through the phrases and make sense of them.  
       # Apply modifiers NOT OR and AND to the phrases.
       my @NewPhrases;
       while(@::Phrases) {
           my $phrase = shift(@::Phrases);
           # &Apache::lonnet::logthis('phrase = '.$phrase);
           my $phrasedata;
           if ($phrase =~ /^(NOT|OR|AND)$/) {
               if ($phrase eq 'OR') {
                   $phrasedata->{'or'}++;
                   if (! @::Phrases) { $phrasedata = undef; last; }
                   $phrase = shift(@::Phrases);
               } elsif ($phrase eq 'AND') {
                   $phrasedata->{'and'}++;
                   if (! @::Phrases) { $phrasedata = undef; last; }
                   $phrase = shift(@::Phrases);
               }
               if ($phrase eq 'NOT') {
                   $phrasedata->{'negate'}++;
                   if (! @::Phrases) { $phrasedata = undef; last; }
                   $phrase = shift(@::Phrases);
               }
           }
           $phrasedata->{'phrase'} = $phrase;
           if ($related) {
               my @NewWords;
               (undef,@NewWords) = &related_version($phrasedata->{'phrase'});
               $phrasedata->{'related_words'} = \@NewWords;
           }
           push(@NewPhrases,$phrasedata);
       }
       #
       # Actually build the sql query from the phrases
       my $SQLQuery;
       foreach my $phrase (@NewPhrases) {
           my $query;
           if ($phrase->{'negate'}) {
               $query .= $field.' NOT LIKE "%'.$phrase->{'phrase'}.'%"';
           } else {
               $query .= $field.' LIKE "%'.$phrase->{'phrase'}.'%"';
           }
           foreach my $related (@{$phrase->{'related_words'}}) {
               if ($phrase->{'negate'}) {
                   $query .= ' AND '.$field.' NOT LIKE "%'.$related.'%"';
               } else {
                   $query .= ' OR '.$field.' LIKE "%'.$related.'%"';
               }
           }
           if ($SQLQuery) {
               if ($phrase->{'or'}) {
                   $SQLQuery .= ' OR ('.$query.')';
               } else {
                   $SQLQuery .= ' AND ('.$query.')';
               }
           } else {
               $SQLQuery = '('.$query.')';
           }
       }
       #
       # &Apache::lonnet::logthis("SQLQuery = $SQLQuery");
       #
       return undef,$SQLQuery;
   }
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
   
Line 1478  Note: Using this twice on a string is pr Line 1861  Note: Using this twice on a string is pr
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
 sub related_version {  sub related_version {
     my $search_string = shift;      my ($word) = @_;
     my $result = $search_string;      return (undef) if (lc($word) =~ /\b(or|and|not)\b/);
     my %New_Words = ();      my @Words = &Apache::loncommon::get_related_words($word);
     while ($search_string =~ /(\w+)/cg) {      # Only use 4 related words
         my $word = $1;      @Words = ($#Words>4? @Words[0..4] : @Words);
         next if (lc($word) =~ /\b(or|and|not)\b/);      my $result = join " OR ", ($word,@Words);
         my @Words = &Apache::loncommon::get_related_words($word);      return $result,sort(@Words);
         @Words = ($#Words>4? @Words[0..4] : @Words);  
         foreach (@Words) { $New_Words{$_}++;}  
         my $replacement = join " OR ", ($word,@Words);  
         $result =~ s/(\b)$word(\b)/$1($replacement)$2/g;  
     }  
     return $result,sort(keys(%New_Words));  
 }  }
   
 ######################################################################  
 ######################################################################  
   
 =pod   
   
 =item &build_SQL_query()   
   
 Builds a SQL query string from a logical expression with AND/OR keywords  
 using Text::Query and &recursive_SQL_query_builder()  
   
 =cut  
   
 ######################################################################  
 ######################################################################  
 sub build_SQL_query {  
     my ($field_name,$logic_statement)=@_;  
     my $q=new Text::Query('abc',  
   -parse => 'Text::Query::ParseAdvanced',  
   -build => 'Text::Query::Build');  
     $q->prepare($logic_statement);  
     my $matchexp=${$q}{'matchexp'}; chomp $matchexp;  
     my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);  
     return $sql_query;  
 }  
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
Line 1551  sub build_custommetadata_query { Line 1904  sub build_custommetadata_query {
     return $matchexp;      return $matchexp;
 }  }
   
 ######################################################################  
 ######################################################################  
   
 =pod   
   
 =item &recursive_SQL_query_build()   
   
 Recursively constructs an SQL query.  Takes as input $dkey and $pattern.  
   
 =cut  
   
 ######################################################################  
 ######################################################################  
 sub recursive_SQL_query_build {  
     my ($dkey,$pattern)=@_;  
     my @matches=($pattern=~/(\[[^\]|\[]*\])/g);  
     return $pattern unless @matches;  
     foreach my $match (@matches) {  
         $match=~/\[ (\w+)\s(.*) \]/;  
         my ($key,$value)=($1,$2);  
         my $replacement='';  
         if ($key eq 'literal') {  
             $replacement="($dkey LIKE \"\%$value\%\")";  
         } elsif (lc($key) eq 'not') {  
             $value=~s/LIKE/NOT LIKE/;  
 #          $replacement="($dkey not like $value)";  
             $replacement="$value";  
         } elsif ($key eq 'and') {  
             $value=~/(.*[\"|\)]) ([|\(|\^].*)/;  
             $replacement="($1 AND $2)";  
  } elsif ($key eq 'or') {  
             $value=~/(.*[\"|\)]) ([|\(|\^].*)/;  
             $replacement="($1 OR $2)";  
  }  
  substr($pattern,  
                index($pattern,$match),  
                length($match),  
                $replacement);  
     }  
     &recursive_SQL_query_build($dkey,$pattern);  
 }  
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
Line 1784  a link to change the search query. Line 2096  a link to change the search query.
 ######################################################################  ######################################################################
 sub print_sort_form {  sub print_sort_form {
     my ($r,$pretty_query_string) = @_;      my ($r,$pretty_query_string) = @_;
     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);      my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1).
           &Apache::lonhtmlcommon::breadcrumbs
           (undef,'Searching','Searching',undef,undef,
            $ENV{'form.catalogmode'} ne 'groupsearch');
   
     ##      ##
     my %SortableFields=&Apache::lonlocal::texthash(       my %SortableFields=&Apache::lonlocal::texthash( 
          id        => 'Default',           id        => 'Default',
Line 1816  sub print_sort_form { Line 2132  sub print_sort_form {
         return;          return;
     }      }
     my $result;      my $result;
       my $html=&Apache::lonxml::xmlbegin();
     $result.=<<END;      $result.=<<END;
 <html>  $html
 <head>  <head>
 <script>  <script>
     function change_sort() {      function change_sort() {
Line 2050  results into MySQL. Line 2367  results into MySQL.
 sub run_search {  sub run_search {
     my ($r,$query,$customquery,$customshow,$serverlist,$pretty_string) = @_;      my ($r,$query,$customquery,$customshow,$serverlist,$pretty_string) = @_;
     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);      my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
       $bodytag.=&Apache::lonhtmlcommon::breadcrumbs
           (undef,'Searching','Searching',undef,undef,
            $ENV{'form.catalogmode'} ne 'groupsearch');
     my $connection = $r->connection;      my $connection = $r->connection;
     #      #
     # Print run_search header      # Print run_search header
     #      #
       my $html=&Apache::lonxml::xmlbegin();
     $r->print(<<END);      $r->print(<<END);
 <html>  $html
 <head><title>Search Status</title></head>  <head>
   <title>Search Status</title></head>
 $bodytag  $bodytag
 <form name="statusform" action="" method="post">  <form name="statusform" action="" method="post">
 <input type="hidden" name="Queue" value="" />  <input type="hidden" name="Queue" value="" />
 END  END
     # Check to see if $pretty_string has more than one carriage return.      # Remove leading and trailing <br />
     # Assume \n s are following <br /> s and truncate the value.      $pretty_string =~ s:^\s*<br />::i;
     # (there is probably a better way)...      $pretty_string =~ s:(<br />)*\s*$::im;
     my @Lines = split /<br \/>/,$pretty_string;      my @Lines = split("<br />",$pretty_string);
       # I keep getting blank items at the end of the list, hence the following:
       while ($Lines[-1] =~ /^\s*$/ && @Lines) {
           pop(@Lines);
       }
     if (@Lines > 2) {      if (@Lines > 2) {
         $pretty_string = join '<br \>',(@Lines[0..2],'....<br />');          $pretty_string = join '<br />',(@Lines[0..2],'....<br />');
     }      }
     $r->print(&mt("Search: [_1]",$pretty_string));      $r->print(&mt("Search: [_1]",$pretty_string));
     $r->rflush();      $r->rflush();
Line 2120  END Line 2446  END
 <table>  <table>
 <tr><th>Status</th><th>Total Matches</th><th>Time Remaining</th><th></th></tr>  <tr><th>Status</th><th>Total Matches</th><th>Time Remaining</th><th></th></tr>
 <tr>  <tr>
 <td><input type="text" name="status"  value="" size="30" /></td>  <td><input type="text" name="status"  value="" size="50" /></td>
 <td><input type="text" name="count"   value="" size="10" /></td>  <td><input type="text" name="count"   value="" size="10" /></td>
 <td><input type="text" name="seconds" value="" size="8" /></td>  <td><input type="text" name="seconds" value="" size="8" /></td>
 <td>$revise</td>  <td>$revise</td>
Line 2159  END Line 2485  END
         #          #
         # Loop through the servers we have contacted but do not          # Loop through the servers we have contacted but do not
         # have results from yet, looking for results.          # have results from yet, looking for results.
         while (my ($server,$status) = each(%Server_status)) {          foreach my $server (keys(%Server_status)) {
             last if ($connection->aborted());              last if ($connection->aborted());
             &update_seconds($r);              &update_seconds($r);
               my $status = $Server_status{$server};
             if ($status eq 'con_lost') {              if ($status eq 'con_lost') {
                 delete ($Server_status{$server});                  delete ($Server_status{$server});
                 next;                  next;
Line 2233  END Line 2560  END
     # results to get, so let the client know the top frame needs to be      # results to get, so let the client know the top frame needs to be
     # loaded from /adm/searchcat      # loaded from /adm/searchcat
     $r->print("</body></html>");      $r->print("</body></html>");
     if ($ENV{'form.catalogmode'} ne 'groupsearch') {  #    if ($ENV{'form.catalogmode'} ne 'groupsearch') {
         $r->print("<script>".          $r->print("<script>".
                       "window.location='/adm/searchcat?".                        "window.location='/adm/searchcat?".
                       "phase=sort&".                        "phase=sort&".
                       "persistent_db_id=$ENV{'form.persistent_db_id'}';".                        "persistent_db_id=$ENV{'form.persistent_db_id'}';".
                   "</script>");                    "</script>");
     }  #    }
     return;      return;
 }  }
   
Line 2260  sub prev_next_buttons { Line 2587  sub prev_next_buttons {
     my ($current_min,$show,$total,$parms) = @_;      my ($current_min,$show,$total,$parms) = @_;
     return '' if ($show eq 'all'); # No links if you get them all at once.      return '' if ($show eq 'all'); # No links if you get them all at once.
     #      #
     # Create links      # Create buttons
     my $prev_min = $current_min - $show;      my $buttons = '<input type="submit" name="prev" value="'.&mt('Prev').'" ';
     $prev_min = 1 if $prev_min < 1;      $buttons .= '/>';
     my $prevlink =       $buttons .= '&nbsp;'x3;
         qq{<a href="/adm/searchcat?$parms&start=$prev_min&show=$show">};      $buttons .= '<input type="submit" name="reload" '.
     #          'value="'.&mt('Reload').'" />';
     my $next_min = $current_min + $show;      $buttons .= '&nbsp;'x3;
     $next_min = $current_min if ($next_min > $total);      $buttons .= '<input type="submit" name="next" value="'.&mt('Next').'" ';
     my $nextlink =      $buttons .= '/>';
         qq{<a href="/adm/searchcat?$parms&start=$next_min&show=$show">};      return $buttons;
     my $reloadlink =   
         qq{<a href="/adm/searchcat?$parms&start=$current_min&$show=$show">};  
     #  
     # Determine which parameters to pass  
     my $String = '[_1]prev[_2] &nbsp; [_3]reload[_4] &nbsp; [_5]next[_6]';  
     if ($prev_min == $current_min) {  
         $String =~ s:\[_[12]\]::g;  
     }  
     if ($next_min == $current_min) {  
         $String =~ s:\[_[56]\]::g;  
     }  
     my $links = &mt($String,  
                     $prevlink,  '</a>',  
                     $reloadlink,'</a>',  
                     $nextlink,  '</a>');  
     return $links;  
 }  }
   
 ######################################################################  ######################################################################
Line 2348  sub display_results { Line 2659  sub display_results {
     }      }
     ##      ##
     ## Determine how many results we need to get      ## Determine how many results we need to get
     $ENV{'form.start'} = 1      if (! exists($ENV{'form.start'}));      $ENV{'form.start'} = 1  if (! exists($ENV{'form.start'}));
     $ENV{'form.show'}  = 'all'  if (! exists($ENV{'form.show'}));      $ENV{'form.show'}  = 20 if (! exists($ENV{'form.show'}));
       if (exists($ENV{'form.prev'})) {
           $ENV{'form.start'} -= $ENV{'form.show'};
       } elsif (exists($ENV{'form.next'})) {
           $ENV{'form.start'} += $ENV{'form.show'};
       }
       $ENV{'form.start'} = 1 if ($ENV{'form.start'}<1);
       $ENV{'form.start'} = $total_results if ($ENV{'form.start'}>$total_results);
     my $min = $ENV{'form.start'};      my $min = $ENV{'form.start'};
     my $max;      my $max;
     if ($ENV{'form.show'} eq 'all') {      if ($ENV{'form.show'} eq 'all') {
Line 2359  sub display_results { Line 2677  sub display_results {
         $max = $total_results if ($max > $total_results);          $max = $total_results if ($max > $total_results);
     }      }
     ##      ##
       ## Output form elements
       $r->print(&hidden_field('table').
                 &hidden_field('phase').
                 &hidden_field('persistent_db_id').
                 &hidden_field('start')
                 );
       #
       # Build sorting selector
       my @fields = 
           (
            {key=>'default' },
            {key=>'title' },
            {key =>'author' },
            {key =>'subject'},
            {key =>'url',desc=>'URL'},
            {key =>'keywords'},
            {key =>'language'},
            {key =>'creationdate'},
            {key =>'lastrevisiondate'},
            {key =>'owner'},
            {key =>'copyright'},
            {key =>'authorspace'},
            {key =>'lowestgradelevel'},
            {key =>'highestgradelevel'},
            {key =>'standards',desc=>'Standards'},
            {key =>'count',desc=>'Number of accesses'},
            {key =>'stdno',desc=>'Students Attempting'},
            {key =>'avetries',desc=>'Average Number of Tries'},
            {key =>'difficulty',desc=>'Mean Degree of Difficulty'},
            {key =>'disc',desc=>'Mean Degree of Discrimination'},
            {key =>'clear',desc=>'Evaluation: Clear'},
            {key =>'technical',desc=>'Evaluation: Technically Correct'},
            {key =>'correct',desc=>'Evaluation: Material is Correct'},
            {key =>'helpful',desc=>'Evaluation: Material is Helpful'},
            {key =>'depth',desc=>'Evaluation: Material has Depth'},
            );
       my %fieldnames = &Apache::lonmeta::fieldnames();
       my @field_order;
       foreach my $field_data (@fields) {
           push(@field_order,$field_data->{'key'});
           if (! exists($field_data->{'desc'})) {
               $field_data->{'desc'}=$fieldnames{$field_data->{'key'}};
           } else {
               if (! defined($field_data->{'desc'})) {
                   $field_data->{'desc'} = ucfirst($field_data->{'key'});
               }
               $field_data->{'desc'} = &mt($field_data->{'desc'});
           }
       }
       my %sort_fields = map {$_->{'key'},$_->{'desc'}} @fields;
       $sort_fields{'select_form_order'} = \@field_order;
       $ENV{'form.sortorder'} = 'asc' if (! exists($ENV{'form.sortorder'}));
       my $sortform = &mt('Sort by [_1] [_2]',
                          &Apache::loncommon::select_form($ENV{'form.sortfield'},
                                                         'sortfield',
                                                         %sort_fields),
                          &Apache::loncommon::select_form($ENV{'form.sortorder'},
                                                         'sortorder',
                                                         (asc =>&mt('Ascending'),
                                                          desc=>&mt('Descending')
                                                          ))
                          );
       ##
     ## Output links (if necessary) for 'prev' and 'next' pages.      ## Output links (if necessary) for 'prev' and 'next' pages.
     $r->print      $r->print
         ('<center>'.          ('<table width="100%"><tr><td width="25%" align="right">'.
          &prev_next_buttons($min,$ENV{'form.show'},$total_results,           '<nobr>'.$sortform.'</nobr>'.
                             "table=".$ENV{'form.table'}.           '</td><td width="25%" align="right">'.
                             "&phase=results".           &prev_next_buttons($min,$ENV{'form.show'},$total_results).
                             "&persistent_db_id=".$ENV{'form.persistent_db_id'})           '</td><td align="right">'.
          ."</center>\n"           &viewoptions().'</td></tr></table>'
          );           );
     if ($total_results == 0) {      if ($total_results == 0) {
         $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2">'.          $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2">'.
Line 2374  sub display_results { Line 2755  sub display_results {
                   "</form></body></html>");                    "</form></body></html>");
         return;          return;
     } else {      } else {
         $r->print          $r->print('<center>'.
             ("<center>Results $min to $max out of $total_results</center>\n");                    mt('Results [_1] to [_2] out of [_3]',
                        $min,$max,$total_results).
                     "</center>\n");
     }      }
     ##      ##
     ## Get results from MySQL table      ## Get results from MySQL table
     my @Results = &Apache::lonmysql::get_rows($table,      my $sort_command  = 'id>='.$min.' AND id<='.$max;
                                               'id>='.$min.' AND id<='.$max);      my $order;
       if (exists($ENV{'form.sortorder'})) {
           if ($ENV{'form.sortorder'} eq 'asc') {
               $order = 'ASC';
           } elsif ($ENV{'form.sortorder'} eq 'desc') {
               $order = 'DESC';
           } else {
               $order = '';
           }
       } else {
           $order = '';
       }
       if ($ENV{'form.sortfield'} ne 'default' && 
           exists($sort_fields{$ENV{'form.sortfield'}})) {
           $sort_command = $ENV{'form.sortfield'}.' IS NOT NULL '.
               'ORDER BY '.$ENV{'form.sortfield'}.' '.$order.
               '  LIMIT '.($min-1).','.($max-$min);
       }
       my @Results = &Apache::lonmysql::get_rows($table,$sort_command);
     ##      ##
     ## Loop through the results and output them.      ## Loop through the results and output them.
     foreach my $row (@Results) {      foreach my $row (@Results) {
Line 2705  SCRIPT Line 3106  SCRIPT
     $result.=<<END;      $result.=<<END;
 </head>  </head>
 $bodytag  $bodytag
 <form name="results" method="post" action="" >  <form name="results" method="post" action="/adm/searchcat" >
 <input type="hidden" name="Queue" value="" />  <input type="hidden" name="Queue" value="" />
 $importbutton  $importbutton
 END  END
Line 2716  END Line 3117  END
 ######################################################################  ######################################################################
 sub search_status_header {  sub search_status_header {
     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);      my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
       my $html=&Apache::lonxml::xmlbegin();
     return <<ENDSTATUS;      return <<ENDSTATUS;
 <html><head><title>Search Status</title></head>  $html
   <head>
   <title>Search Status</title></head>
 $bodytag  $bodytag
 <h3>Search Status</h3>  <h3>Search Status</h3>
 Sending search request to LON-CAPA servers.<br />  Sending search request to LON-CAPA servers.<br />
Line 2740  sub print_frames_interface { Line 3144  sub print_frames_interface {
         "&persistent_db_id=".$ENV{'form.persistent_db_id'};          "&persistent_db_id=".$ENV{'form.persistent_db_id'};
     my $run_search_link = $basic_link."&phase=run_search";      my $run_search_link = $basic_link."&phase=run_search";
     my $results_link = &results_link();      my $results_link = &results_link();
       my $html=&Apache::lonxml::xmlbegin();
     my $result = <<"ENDFRAMES";      my $result = <<"ENDFRAMES";
 <html>  $html
 <head>  <head>
 <script>  <script>
 var targetwin = opener;  var targetwin = opener;
Line 2763  ENDFRAMES Line 3168  ENDFRAMES
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
   
   sub has_stat_data {
       my ($values) = @_;
       if ( (defined($values->{'count'})      && $values->{'count'}      ne '') ||
            (defined($values->{'stdno'})      && $values->{'stdno'}      ne '') ||
            (defined($values->{'disc'})       && $values->{'disc'}       ne '') ||
            (defined($values->{'avetries'})   && $values->{'avetries'}   ne '') ||
            (defined($values->{'difficulty'}) && $values->{'difficulty'} ne '')) {
           return 1;
       }
       return 0;
   }
   
   sub statfields {
       return ('count','stdno','disc','avetries','difficulty');
   }
   
   sub has_eval_data {
       my ($values) = @_;
       if ( (defined($values->{'clear'})     && $values->{'clear'}     ne '') ||
            (defined($values->{'technical'}) && $values->{'technical'} ne '') ||
            (defined($values->{'correct'})   && $values->{'correct'}   ne '') ||
            (defined($values->{'helpful'})   && $values->{'helpful'}   ne '') ||
            (defined($values->{'depth'})     && $values->{'depth'}     ne '')) {
           return 1;
       }
       return 0;
   }
   
   sub evalfields { 
       return ('clear','technical','correct','helpful','depth');
   }
   
   ######################################################################
   ######################################################################
   
 =pod   =pod 
   
 =item Metadata Viewing Functions  =item Metadata Viewing Functions
Line 2784  extra custom metadata to show. Line 3224  extra custom metadata to show.
 ######################################################################  ######################################################################
 sub detailed_citation_view {  sub detailed_citation_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $icon=  
     my $result;      my $result;
     $result .= '<b>'.$prefix.      $result .= '<b>'.$prefix.
         '<img src="'.&Apache::loncommon::icon($values{'url'}).' " />'.          '<img src="'.&Apache::loncommon::icon($values{'url'}).' " />'.'&nbsp;'.
         '<a href="http://'.$ENV{'HTTP_HOST'}.$values{'url'}.'" '.          '<a href="http://'.$ENV{'HTTP_HOST'}.$values{'url'}.'" '.
         'target="search_preview">'.$values{'title'}."</a></b>\n";          'target="search_preview">'.$values{'title'}."</a></b>\n";
     $result .= "<p>\n";      $result .= "<p>\n";
Line 2795  sub detailed_citation_view { Line 3234  sub detailed_citation_view {
         ' <i>'.$values{'owner'}.'</i><br />';          ' <i>'.$values{'owner'}.'</i><br />';
     foreach my $field       foreach my $field 
         (          (
            { name=>'url',
              translate => '<b>URL:</b>&nbsp;[_1]',
              special => 'url link',},
          { name=>'subject',           { name=>'subject',
            translate => '<b>Subject:</b>&nbsp;[_1]',},             translate => '<b>Subject:</b>&nbsp;[_1]',},
          { name=>'keywords',           { name=>'keywords',
Line 2807  sub detailed_citation_view { Line 3249  sub detailed_citation_view {
            translate => '<b>Standards:</b>[_1]',},             translate => '<b>Standards:</b>[_1]',},
          { name=>'copyrighttag',           { name=>'copyrighttag',
            translate => '<b>Copyright/Distribution:</b>&nbsp;[_1]',},             translate => '<b>Copyright/Distribution:</b>&nbsp;[_1]',},
            { name=>'count',
              format => "%d",
              translate => '<b>Access Count:</b>&nbsp;[_1]',},
          { name=>'stdno',           { name=>'stdno',
              format => "%d",
            translate => '<b>Number of Students:</b>&nbsp;[_1]',},             translate => '<b>Number of Students:</b>&nbsp;[_1]',},
          { name=>'avetries',           { name=>'avetries',
              format => "%.2f",
            translate => '<b>Average Tries:</b>&nbsp;[_1]',},             translate => '<b>Average Tries:</b>&nbsp;[_1]',},
          { name=>'disc',           { name=>'disc',
              format => "%.2f",
            translate => '<b>Degree of Discrimination:</b>&nbsp;[_1]',},             translate => '<b>Degree of Discrimination:</b>&nbsp;[_1]',},
          { name=>'difficulty',           { name=>'difficulty',
              format => "%.2f",
            translate => '<b>Degree of Difficulty:</b>&nbsp;[_1]',},             translate => '<b>Degree of Difficulty:</b>&nbsp;[_1]',},
          { name=>'clear',           { name=>'clear',
              format => "%.2f",
            translate => '<b>Clear:</b>&nbsp;[_1]',},             translate => '<b>Clear:</b>&nbsp;[_1]',},
          { name=>'depth',           { name=>'depth',
              format => "%.2f",
            translate => '<b>Depth:</b>&nbsp;[_1]',},             translate => '<b>Depth:</b>&nbsp;[_1]',},
          { name=>'helpful',           { name=>'helpful',
              format => "%.2f",
            translate => '<b>Helpful:</b>&nbsp;[_1]',},             translate => '<b>Helpful:</b>&nbsp;[_1]',},
          { name=>'correct',           { name=>'correct',
            translate => '<b>Correcy:</b>&nbsp;[_1]',},             format => "%.2f",
              translate => '<b>Correct:</b>&nbsp;[_1]',},
          { name=>'technical',           { name=>'technical',
              format => "%.2f",
            translate => '<b>Technical:</b>&nbsp;[_1]',},             translate => '<b>Technical:</b>&nbsp;[_1]',},
            { name=>'comefrom_list',
              type => 'list',
              translate => 'Resources that lead up to this resource in maps',},
            { name=>'goto_list',
              type => 'list',
              translate => 'Resources that follow this resource in maps',},
            { name=>'sequsage_list',
              type => 'list',
              translate => 'Resources using or importing resource',},
          ) {           ) {
         $result.= &mt($field->{'translate'},$values{$field->{'name'}}).          next if (! exists($values{$field->{'name'}}) ||
             "<br />\n";                   $values{$field->{'name'}} eq '');
           if (exists($field->{'type'}) && $field->{'type'} eq 'list') {
               $result .= '<b>'.&mt($field->{'translate'}).'</b><ul>';
               foreach my $item (split(',',$values{$field->{'name'}})){
                   $result .= '<li>'.
                       '<a target="search_preview" '.
                       'href="/res/'.$item.'">'.$item.'</a></li>';
               }
               $result .= '</ul>';
           } elsif (exists($field->{'format'}) && $field->{'format'} ne ''){
               $result.= &mt($field->{'translate'},
                             sprintf($field->{'format'},
                                     $values{$field->{'name'}}))."<br />\n";
           } else {
               if ($field->{'special'} eq 'url link') {
                   $result.= 
                        &mt($field->{'translate'},
                            '<a href="'.$values{'url'}.'" '.
                            'target="search_preview">'.
                            $values{$field->{'name'}}.
                            '</a>');
               } else {
                   $result.= &mt($field->{'translate'},
                                 $values{$field->{'name'}});
               }
               $result .= "<br />\n";
           }
       }
       $result .= "</p>";
       if (exists($values{'extrashow'}) && $values{'extrashow'} ne '') {
           $result .= '<p>'.$values{'extrashow'}.'</p>';
       }
       if (exists($values{'shortabstract'}) && $values{'shortabstract'} ne '') {
           $result .= '<p>'.$values{'shortabstract'}.'</p>';
     }      }
     $result .= "</p>".$values{'extrashow'}.  
         '<p>'.$values{'shortabstract'}.'</p>';  
     $result .= '<hr align="left" width="200" noshade />'."\n";      $result .= '<hr align="left" width="200" noshade />'."\n";
     return $result;      return $result;
 }  }
Line 2848  sub detailed_citation_view { Line 3342  sub detailed_citation_view {
 sub summary_view {  sub summary_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $icon=&Apache::loncommon::icon($values{'url'});      my $icon=&Apache::loncommon::icon($values{'url'});
     my $result=<<END;      my $result=qq{$prefix<img src="$icon" />};
 $prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}"       if (exists($ENV{'form.sortfield'}) && 
    target='search_preview'>$values{'author'}</a><br />          $ENV{'form.sortfield'} !~ /^(default|
 $values{'title'}<br />                                       author|
 $values{'owner'} -- $values{'lastrevisiondate'}<br />                                       url|
                                        title|
                                        owner|
                                        lastrevisiondate|
                                        copyright)$/x) {
           my $tmp = $values{$ENV{'form.sortfield'}};
           if (! defined($tmp)) { $tmp = 'undefined'; }
           $result .= '&nbsp;'.$tmp.'&nbsp;';
       }
       $result.=<<END;
   <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
      target='search_preview'>$values{'title'}</a><br />
   $values{'author'}, $values{'owner'} -- $values{'lastrevisiondate'}<br />
 $values{'copyrighttag'}<br />  $values{'copyrighttag'}<br />
 $values{'extrashow'}  $values{'extrashow'}
 </p>  </p>
Line 2874  END Line 3380  END
 ######################################################################  ######################################################################
 sub compact_view {  sub compact_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $icon=&Apache::loncommon::icon($values{'url'});      my $result = 
     my $result=<<END;          $prefix.'<img src="'.&Apache::loncommon::icon($values{'url'}).'">';
 $prefix <img src="$icon" /> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}"  target='search_preview'>      if (exists($ENV{'form.sortfield'}) && 
 $values{'title'}</a>          $ENV{'form.sortfield'} !~ /^(default|author|url|title)$/) {
 <b>$values{'author'}</b><br />          my $tmp = $values{$ENV{'form.sortfield'}};
 END          if (! defined($tmp)) { $tmp = 'undefined'; }
           $result .= '&nbsp;'.$tmp.'&nbsp;';
       }
       $result.='&nbsp;<a href="'.$values{'url'}.'" target="search_preview">'.
           $values{'title'}.'</a>'.('&nbsp;'x2).
           '<b>'.$values{'author'}.'</b><br />';
     return $result;      return $result;
 }  }
   
Line 2898  END Line 3409  END
 sub fielded_format_view {  sub fielded_format_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $icon=&Apache::loncommon::icon($values{'url'});      my $icon=&Apache::loncommon::icon($values{'url'});
       my %Translated = &Apache::lonmeta::fieldnames();
     my $result=<<END;      my $result=<<END;
 $prefix <img src="$icon" />  $prefix <img src="$icon" />
 <b>URL: </b> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}"   <dl>
               target='search_preview'>$values{'url'}</a>  <dt>URL:</dt>
 <br />      <dd><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
 <b>Title:</b> $values{'title'}<br />           target='search_preview'>$values{'url'}</a></dd>
 <b>Author(s):</b> $values{'author'}<br />  
 <b>Subject:</b> $values{'subject'}<br />  
 <b>Keyword(s):</b> $values{'keywords'}<br />  
 <b>Notes:</b> $values{'notes'}<br />  
 <b>MIME Type:</b> $values{'mimetag'}<br />  
 <b>Language:</b> $values{'language'}<br />  
 <b>Creation Date:</b> $values{'creationdate'}<br />  
 <b>Last Revision Date:</b> $values{'lastrevisiondate'}<br />  
 <b>Publisher/Owner:</b> $values{'owner'}<br />  
 <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />  
 <b>Repository Location:</b> $values{'hostname'}<br />  
 <b>Abstract:</b> $values{'shortabstract'}<br />  
 $values{'extrashow'}  
 </p>  
 <hr align='left' width='200' noshade />  
 END  END
       foreach my $field ('title','author','domain','subject','keywords','notes',
                          'mimetag','language','creationdate','lastrevisiondate',
                          'owner','copyrighttag','hostname','abstract') {
           $result .= (' 'x4).'<dt>'.$Translated{$field}.'</dt>'."\n".
               (' 'x8).'<dd>'.$values{$field}.'</dd>'."\n";
       }
       if (&has_stat_data(\%values)) {
           foreach my $field (&statfields()) {
               $result .= (' 'x4).'<dt>'.$Translated{$field}.'</dt>'."\n".
                   (' 'x8).'<dd>'.$values{$field}.'</dd>'."\n";
           }
       }
       if (&has_eval_data(\%values)) {
           foreach my $field (&evalfields()) {
               $result .= (' 'x4).'<dt>'.$Translated{$field}.'</dt>'."\n".
                   (' 'x8).'<dd>'.$values{$field}.'</dd>'."\n";
           }
       }
       $result .= "</dl>\n";
       $result .= $values{'extrashow'};
       $result .= '<hr align="left" width="200" noshade />'."\n";
     return $result;      return $result;
 }  }
   
Line 2938  END Line 3456  END
 ######################################################################  ######################################################################
 sub xml_sgml_view {  sub xml_sgml_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $xml = <<END;      my $xml = '<LonCapaResource>'."\n";
 <LonCapaResource>      # The usual suspects
 <url>$values{'url'}</url>      foreach my $field ('url','title','author','subject','keywords','notes','domain') {
 <title>$values{'title'}</title>          $xml .= qq{<$field>$values{$field}</$field>}."\n";
 <author>$values{'author'}</author>      }
 <subject>$values{'subject'}</subject>      #
 <keywords>$values{'keywords'}</keywords>      $xml .= "<mimeInfo>\n";
 <notes>$values{'notes'}</notes>      foreach my $field ('mime','mimetag') {
 <mimeInfo>          $xml .= qq{<$field>$values{$field}</$field>}."\n";
 <mime>$values{'mime'}</mime>      }
 <mimetag>$values{'mimetag'}</mimetag>      $xml .= "</mimeInfo>\n";
 </mimeInfo>      #
 <languageInfo>      $xml .= "<languageInfo>\n";
 <language>$values{'language'}</language>      foreach my $field ('language','languagetag') {
 <languagetag>$values{'languagetag'}</languagetag>          $xml .= qq{<$field>$values{$field}</$field>}."\n";
 </languageInfo>      }
 <creationdate>$values{'creationdate'}</creationdate>      $xml .= "</languageInfo>\n";
 <lastrevisiondate>$values{'lastrevisiondate'}</lastrevisiondate>      #
 <owner>$values{'owner'}</owner>      foreach my $field ('creationdate','lastrevisiondate','owner') {
 <copyrightInfo>          $xml .= qq{<$field>$values{$field}</$field>}."\n";
 <copyright>$values{'copyright'}</copyright>      }
 <copyrighttag>$values{'copyrighttag'}</copyrighttag>      #
 </copyrightInfo>      $xml .= "<copyrightInfo>\n";
 <repositoryLocation>$values{'hostname'}</repositoryLocation>      foreach my $field ('copyright','copyrighttag') {
 <shortabstract>$values{'shortabstract'}</shortabstract>          $xml .= qq{<$field>$values{$field}</$field>}."\n";
 </LonCapaResource>      }
 END      $xml .= "</copyrightInfo>\n";
       $xml .= qq{<repositoryLocation>$values{'hostname'}</repositoryLocation>}.
           "\n";
       $xml .= qq{<shortabstract>$values{'shortabstract'}</shortabstract>}."\n";
       #
       if (&has_stat_data(\%values)){
           $xml .= "<problemstatistics>\n";
           foreach my $field (&statfields()) {
               $xml .= qq{<$field>$values{$field}</$field>}."\n";            
           }
           $xml .= "</problemstatistics>\n";
       }
       #
       if (&has_eval_data(\%values)) {
           $xml .= "<evaluation>\n";
           foreach my $field (&evalfields) {
               $xml .= qq{<$field>$values{$field}</$field>}."\n";            
           }
           $xml .= "</evaluation>\n";
       }    
       #
       $xml .= "</LonCapaResource>\n";
     $xml = &HTML::Entities::encode($xml,'<>&');      $xml = &HTML::Entities::encode($xml,'<>&');
     my $result=<<END;      my $result=<<END;
 $prefix  $prefix
Line 3002  sub filled { Line 3541  sub filled {
   
 =pod   =pod 
   
   =item &output_unparsed_phrase_error()
   
   =cut
   
   ######################################################################
   ######################################################################
   sub output_unparsed_phrase_error {
       my ($r,$closebutton,$parms,$hidden_fields,$field)=@_;
       my $errorstring;
       if ($field eq 'basicexp') {
           $errorstring = &mt('Unable to understand the search phrase <i>[_1]</i>.  Please modify your search.',$ENV{'form.basicexp'});
       } else {
           $errorstring = &mt('Unable to understand the search phrase <b>[_1]</b>:<i>[_2]</i>.',$field,$ENV{'form.'.$field});
       }
       my $bodytag = &Apache::loncommon::bodytag('Search');
       my $heading = &mt('Unparsed Field');
       my $revise  = &mt('Revise search request');
       # make query information persistent to allow for subsequent revision
       my $html=&Apache::lonxml::xmlbegin();
       $r->print(<<ENDPAGE);
   $html
   <head>
   <title>The LearningOnline Network with CAPA</title>
   </head>
   $bodytag
   <form method="post" action="/adm/searchcat">
   $hidden_fields
   $closebutton
   <hr />
   <h2>$heading</h2>
   <p>
   $errorstring
   </p>
   <p>
   <a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}">$revise</a>
   </p>
   </body>
   </html>
   ENDPAGE
   }
   
   ######################################################################
   ######################################################################
   
   =pod 
   
 =item &output_blank_field_error()  =item &output_blank_field_error()
   
 Output a complete page that indicates the user has not filled in enough  Output a complete page that indicates the user has not filled in enough
Line 3019  $parms is extra information to include i Line 3604  $parms is extra information to include i
 ######################################################################  ######################################################################
 sub output_blank_field_error {  sub output_blank_field_error {
     my ($r,$closebutton,$parms,$hidden_fields)=@_;      my ($r,$closebutton,$parms,$hidden_fields)=@_;
     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);      my $bodytag=&Apache::loncommon::bodytag('Search');
     # make query information persistent to allow for subsequent revision      my $errormsg = &mt('You did not fill in enough information for the search to be started.  You need to fill in relevant fields on the search page in order for a query to be processed.');
     $r->print(<<BEGINNING);      my $revise = &mt('Revise Search Request');
 <html>      my $heading = &mt('Unactionable Search Queary');
       my $html=&Apache::lonxml::xmlbegin();
       $r->print(<<ENDPAGE);
   $html
 <head>  <head>
 <title>The LearningOnline Network with CAPA</title>  <title>The LearningOnline Network with CAPA</title>
 BEGINNING  
     $r->print(<<RESULTS);  
 </head>  </head>
 $bodytag  $bodytag
 <img align='right' src='/adm/lonIcons/lonlogos.gif' />  
 <h1>Search Catalog</h1>  
 <form method="post" action="/adm/searchcat">  <form method="post" action="/adm/searchcat">
 $hidden_fields  $hidden_fields
 <a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}"  
 >Revise search request</a>&nbsp;  
 $closebutton  $closebutton
 <hr />  <hr />
 <h3>Unactionable search query.</h3>  <h2>$heading</h2>
   <p>
   $errormsg
   </p>
 <p>  <p>
 You did not fill in enough information for the search to be started.  <a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}">$revise</a>&nbsp;
 You need to fill in relevant fields on the search page in order   
 for a query to be processed.  
 </p>  </p>
 </body>  </body>
 </html>  </html>
 RESULTS  ENDPAGE
       return;
 }  }
   
 ######################################################################  ######################################################################
Line 3071  sub output_date_error { Line 3655  sub output_date_error {
     my ($r,$message,$closebutton,$hidden_fields)=@_;      my ($r,$message,$closebutton,$hidden_fields)=@_;
     # make query information persistent to allow for subsequent revision      # make query information persistent to allow for subsequent revision
     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);      my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
       my $html=&Apache::lonxml::xmlbegin();
     $r->print(<<RESULTS);      $r->print(<<RESULTS);
 <html>  $html
 <head>  <head>
 <title>The LearningOnline Network with CAPA</title>  <title>The LearningOnline Network with CAPA</title>
 </head>  </head>

Removed from v.1.220  
changed lines
  Added in v.1.241


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