Diff for /loncom/interface/lonsearchcat.pm between versions 1.209 and 1.235

version 1.209, 2004/04/19 16:53:27 version 1.235, 2004/12/17 21:44:19
Line 74  use Apache::lonmeta; Line 74  use Apache::lonmeta;
 use Apache::lonhtmlcommon;  use Apache::lonhtmlcommon;
 use Apache::lonlocal;  use Apache::lonlocal;
 use LONCAPA::lonmetadata();  use LONCAPA::lonmetadata();
   use HTML::Entities();
   use Parse::RecDescent;
   use Apache::lonnavmaps;
   
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
Line 116  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 149  sub handler { Line 152  sub handler {
     ## Initialize global variables      ## Initialize global variables
     ##      ##
     my $domain  = $r->dir_config('lonDefDomain');      my $domain  = $r->dir_config('lonDefDomain');
     $diropendb= "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";      $diropendb= "/home/httpd/perl/tmp/".
           "$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
     #      #
     # set the name of the persistent database      # set the name of the persistent database
     #          $ENV{'form.persistent_db_id'} can only have digits in it.      #          $ENV{'form.persistent_db_id'} can only have digits in it.
Line 172  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 180  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 '.
                                            $persistent_db_file);
                   $r->print(<<END);
 <html>  <html>
 <head><title>LON-CAPA Search Error</title></head>  <head><title>LON-CAPA Search Error</title></head>
 $bodytag  $bodytag
Line 196  error and has been logged.  Please alert Line 204  error and has been logged.  Please alert
 </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 224  END Line 235  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 284  END Line 290  END
     $ENV{'form.phase'} = 'disp_basic' if (! exists($ENV{'form.phase'}));      $ENV{'form.phase'} = 'disp_basic' if (! exists($ENV{'form.phase'}));
     $ENV{'form.show'} = 20 if (! exists($ENV{'form.show'}));      $ENV{'form.show'} = 20 if (! exists($ENV{'form.show'}));
     #      #
     $ENV{'form.searchmode'} = 'basic';      $ENV{'form.searchmode'} = 'basic' if (! exists($ENV{'form.searchmode'}));
     if ($ENV{'form.phase'} eq 'adv_search' ||      if ($ENV{'form.phase'} eq 'adv_search' ||
         $ENV{'form.phase'} eq 'disp_adv') {          $ENV{'form.phase'} eq 'disp_adv') {
         $ENV{'form.searchmode'} = 'advanced';          $ENV{'form.searchmode'} = 'advanced';
Line 333  END Line 339  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 '.
                                        'needed table.  lonmysql error:'.
                                        $errorstring);
             $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.
 <br />$errorstring  
 </body>  </body>
 </html>  </html>
 END  END
Line 358  Unable to properly store search informat Line 389  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 395  END Line 445  END
 {   # Scope the course search to avoid global variables  {   # Scope the course search to avoid global variables
 #  #
 # Variables For course search  # Variables For course search
 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');
  &Apache::loncommon::help_open_bug('Searching');  
     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;
Line 415  sub course_search { Line 472  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>'.      $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 />');        $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 445  sub course_search { Line 580  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;  
     $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 481  sub checkonthis { Line 622  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\//)) {
           &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 500  sub search_html_header { Line 653  sub search_html_header {
 <html>  <html>
 <head>  <head>
 <title>The LearningOnline Network with CAPA</title>  <title>The LearningOnline Network with CAPA</title>
 <script type="text/javascript">  
     function openhelp(val) {  
  openhelpwin=open('/adm/help/searchcat.html','helpscreen',  
      'scrollbars=1,width=600,height=300');  
  openhelpwin.focus();  
     }  
 </script>  
 </head>  </head>
 ENDHEADER  ENDHEADER
     return $Str;      return $Str;
Line 527  Prints the form for the basic search.  S Line 673  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','Finding_Resources',          &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'})) {
         my $Statement=&searchhelp();          # Define interface components
         $scrout.=(<<ENDDOCUMENT);          my $userelatedwords= '<label>'.
 <form name="loncapa_search" method="post" action="/adm/searchcat">              &mt('[_1] use related words',
 <input type="hidden" name="phase" value="basic_search" />                  &Apache::lonhtmlcommon::checkbox
 $hidden_fields                  ('related',$ENV{'form.related'},'related')).'</label>';
 <p>          my $onlysearchdomain='<label>'.
 $Statement.              &mt('[_1] only search domain [_2]',
 </p>                  &Apache::lonhtmlcommon::checkbox('domains',
 <p>                                                   $ENV{'form.domains'},
 <table>                                                   $r->dir_config('lonDefDomain')
 <tr><td>                                                   ),
 ENDDOCUMENT                  $r->dir_config('lonDefDomain')
         $scrout.='&nbsp;'.                  ).'</label>';
             &Apache::lonhtmlcommon::textbox('basicexp',          my $adv_search_link = 
                                         $ENV{'form.basicexp'},40).              '<a href="/adm/searchcat?'.
                                         '&nbsp;';              'phase=disp_adv&'.
         my $relatedcheckbox =               'catalogmode='.$ENV{'form.catalogmode'}.
             &Apache::lonhtmlcommon::checkbox('related',              '&launch='.$ENV{'form.launch'}.
                                              $ENV{'form.related'});              '&mode='.$ENV{'form.mode'}.
         my $domain = $r->dir_config('lonDefDomain');              '">'.&mt('Advanced Search').'</a>';
         my $domaincheckbox =           #
             &Apache::lonhtmlcommon::checkbox('domains',          $scrout.='<form name="loncapa_search" method="post" '.
                                              $ENV{'form.domains'});              'action="/adm/searchcat">'.
         my $srch=&mt('Search');              '<input type="hidden" name="phase" value="basic_search" />'.
         my $header=&mt('Advanced Search');              $hidden_fields;
         my $userelatedwords=&mt('use related words');          #
         my $onlysearchdomain=&mt('only search domain');          $scrout .= '<center>'.$/;
         my $view=&viewoptions();          if ($ENV{'request.course.id'}) {
         $scrout.=<<END;              $scrout .= '<h1>'.&mt('LON-CAPA Catalog Search').'</h1>';
 </td><td><a          } else {
 href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}&mode=$ENV{'form.mode'}"              # No need to tell them they are searching
 >$header</a></td></tr>              $scrout.= ('<br />'x2);
 <tr><td>$relatedcheckbox $userelatedwords</td>          }
     <td>$domaincheckbox $onlysearchdomain <b>$domain</b></td></tr>          $scrout.='<table>'.
 </table>              '<tr><td align="center" valign="top">'.
 </p>              &Apache::lonhtmlcommon::textbox
 $view              ('basicexp',
 <p>               &HTML::Entities::encode($ENV{'form.basicexp'},'<>&"'),50
 &nbsp;<input type="submit" name="basicsubmit" value='$srch' />&nbsp;               ).
 $closebutton               '<br />'.
 END              '<font size="-1">'.&searchhelp().'</font>'.'</td>'.
     $scrout.=<<ENDDOCUMENT;              '<td><font size="-1">'.
 </p>              '<nobr>'.('&nbsp;'x3).$adv_search_link.'</nobr>'.'<br />'.
 </form>              '<nobr>'.('&nbsp;'x1).$userelatedwords.'</nobr>'.'<br />'.
 ENDDOCUMENT              '<nobr>'.('&nbsp;'x1).$onlysearchdomain.'</nobr>'.'<br />'.
               '</font></td>'.
               '</tr>'.$/;
           #
           $scrout .= '<tr><td align="center" colspan="2">'.
               '<font size="-1">'.
               '<input type="submit" name="basicsubmit" '.
               'value="'.&mt('Search').'" />'.
               ('&nbsp;'x2).$closebutton.('&nbsp;'x2).
               &viewoptions().
               '</font>'.
               '</td></tr>'.$/;
           $scrout .= '</table>'.$/.'</center>'.'</form>';
     }      }
     if ($ENV{'request.course.id'}) {      if ($ENV{'request.course.id'}) {
  my %lt=&Apache::lonlocal::texthash(   my %lt=&Apache::lonlocal::texthash('srch' => 'Search',
    'srch' => 'Search',  
                                            '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">
   <center>
 <hr />  <hr />
 <h1>$lt{'header'}</h1>      <h1>$lt{'header'}</h1>    
 <form name="course_search" method="post" action="/adm/searchcat">  
 <input type="hidden" name="phase" value="course_search" />  <input type="hidden" name="phase" value="course_search" />
 $hidden_fields  $hidden_fields
 <p>  <p>
Line 608  ENDCOURSESEARCH Line 769  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>
   </center>
   </form>
 ENDENDCOURSE  ENDENDCOURSE
     }      }
     $scrout.=(<<ENDDOCUMENT);      $scrout.=(<<ENDDOCUMENT);
Line 639  Prints the advanced search form. Line 806  Prints the advanced search form.
 ######################################################################  ######################################################################
 sub print_advanced_search_form{  sub print_advanced_search_form{
     my ($r,$closebutton,$hidden_fields) = @_;      my ($r,$closebutton,$hidden_fields) = @_;
       my $bodytag=&Apache::loncommon::bodytag('Advanced Catalog Search').
           &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching',
                                               'Search_Advanced',
                                               undef,undef,
                                     $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');
     my $advanced_buttons = <<"END";      my $advanced_buttons=<<"END";
 <p>  
 <input type="submit" name="advancedsubmit" value='$lt{"srch"}' />  <input type="submit" name="advancedsubmit" value='$lt{"srch"}' />
 <input type="reset" name="reset" value='$lt{"reset"}' />  <input type="reset" name="reset" value='$lt{"reset"}' />
 $closebutton  $closebutton
 <input type="button" value="$lt{'help'}" onClick="openhelp()" />  
 END  END
     my $bodytag=&Apache::loncommon::bodytag('Advanced Catalog Search').  
         &Apache::lonhtmlcommon::breadcrumbs(undef,'Searching',  
                                             'Finding_Resources',  
                                             undef,undef,  
                                             ! $ENV{'form.launch'});  
     my $searchhelp=&searchhelp();  
     my $scrout=&search_html_header();      my $scrout=&search_html_header();
     $scrout .= <<"ENDHEADER";      $scrout .= <<"ENDHEADER";
 $bodytag  $bodytag
 <form method="post" action="/adm/searchcat" name="advsearch">  <form method="post" action="/adm/searchcat" name="advsearch">
   <p>
 $advanced_buttons  $advanced_buttons
 ENDHEADER  ENDHEADER
     $scrout.=('&nbsp;'x2).&viewoptions().'</p>'.$hidden_fields.       $scrout.=('&nbsp;'x2).&viewoptions().'</p>'.$hidden_fields. 
         '<input type="hidden" name="phase" value="adv_search" />';          '<input type="hidden" name="phase" value="adv_search" />';
     my %fields=&Apache::lonmeta::fieldnames();      my %fields=&Apache::lonmeta::fieldnames();
     #      #
     $scrout.= '<p>'.$searchhelp.'</p>'.      $scrout .= '<h3>'.&mt('Standard Metadata').'</h3>';
         "<table>\n";      $scrout .= "<table>\n";
       $scrout .= '<tr><td>&nbsp;</td><td colspan="2"><font size="-1">'.
           ('&nbsp;'x2).&searchhelp()."</font></td></tr>\n";
     my %related_word_search =       my %related_word_search = 
         ('title'=>1,          ('title'    => 1,
          'author'=>0,           'author'   => 0,
          'owner'=>0,           'owner'    => 0,
          'authorspace'=>0,           'authorspace'  => 0,
          'modifyinguser'=>0,           'modifyinguser'=> 0,
          'keywords'=>1,           'keywords' => 1,
          'notes'=>1,           'notes'    => 1,
          'abstract'=>1,           'abstract' => 1,
          'standards'=>1,           'standards'=> 1,
          'mime'=>1,           'mime'     => 1,
          );           );
     #      #
     foreach my $field ('title','author','owner','authorspace','modifyinguser',      foreach my $field ('title','author','owner','authorspace','modifyinguser',
Line 719  ENDHEADER Line 886  ENDHEADER
     &Apache::loncommon::domain_select('domains',      &Apache::loncommon::domain_select('domains',
    $ENV{'form.domains'},1).     $ENV{'form.domains'},1).
     '</td></tr>'.$/;      '</td></tr>'.$/;
     $scrout .= "</table>\n<br />\n<table>\n";      #
     my %dates=&Apache::lonlocal::texthash      # Misc metadata
         ('creationdatestart'     => 'Creation Date After',      $scrout.='<tr><td align="right" valign="top">'.
          'creationdateend'       => 'Creation Date Before',   &titlefield(&mt('Copyright/Distribution')).'</td><td colspan="2">'.
          'lastrevisiondatestart' => 'Last Revision Date After',          &Apache::lonmeta::selectbox('copyright',
          'lastrevisiondateend'   => 'Last Revision Date Before');                                      $ENV{'form.copyright'},
     foreach my $field (sort keys %dates) {                                      \&Apache::loncommon::copyrightdescription,
  $scrout.='<tr>'.                                      ( undef,
             '<td align="right">'.&titlefield($dates{$field}).'</td><td>'.                                         &Apache::loncommon::copyrightids)
     &Apache::lonhtmlcommon::date_setter('advsearch',$field,0,'',1).                                      ).'</td></tr>'.$/;
     '</td></tr>'.$/;      $scrout.='<tr><td align="right" valign="top">'.
     }   &titlefield(&mt('Language')).'</td><td colspan="2">'.
           &Apache::lonmeta::selectbox('language',
                                       $ENV{'form.language'},
                                       \&Apache::loncommon::languagedescription,
                                       ('any',&Apache::loncommon::languageids)
                                       ).'</td></tr>';
       $scrout .= "</table>\n";    
       #
       # Dynamic metadata
       $scrout .= '<h3>'.&mt('Problem Statistics').'</h3>';
       $scrout .= "<table>\n";
       $scrout .= '<tr><td>&nbsp;</td><td align="center">'.&mt('Minimum').'</td>'.
           '<td align="center">'.&mt('Maximum').'</td></tr>'."\n";
       foreach my $statistic 
           ({ name=>'count',
              description=>'Network-wide number of accesses (hits)',},
            { name=>'stdno',
              description=>
                  'Total number of students who have worked on this problem',},
            { name => 'avetries',
              description=>'Average number of tries till solved',},
            { name => 'difficulty',
              description=>'Degree of difficulty',},
            { name => 'disc',
              description=>'Degree of discrimination'}) {
           $scrout .= '<tr><td align="right">'.
               &titlefield(&mt($statistic->{'description'})).
               '</td><td align="center">'.
               '<input type="text" name="'.$statistic->{'name'}.'_min" '.
               'value="" size="6" />'.
               '</td><td align="center">'.
               '<input type="text" name="'.$statistic->{'name'}.'_max" '.
               'value="" size="6" />'.
               '</td></tr>'.$/;
       }
       $scrout .= "</table>\n";
       $scrout .= '<h3>'.&mt('Evaluation Data').'</h3>';
       $scrout .= "<table>\n";
       $scrout .= '<tr><td>&nbsp;</td><td align="center">'.&mt('Minimum').'</td>'.
           '<td align="center">'.&mt('Maximum').'</td></tr>'."\n";
       foreach my $evaluation
           ( { name => 'clear',
               description => 'Material presented in clear way'},
             { name =>'depth',
               description => 'Material covered with sufficient depth'},
             { name => 'helpful',
               description => 'Material is helpful'},
             { name => 'correct',
               description => 'Material appears to be correct'},
             { name => 'technical',
               description => 'Resource is technically correct'}){
           $scrout .= '<tr><td align="right">'.
               &titlefield(&mt($evaluation->{'description'})).
               '</td><td align="center">'.
               '<input type="text" name="'.$evaluation->{'name'}.'_min" '.
               'value="" size="6" />'.
               '</td><td align="center">'.
               '<input type="text" name="'.$evaluation->{'name'}.'_max" '.
               'value="" size="6" />'.
               '</td></tr>'.$/;
       }
       $scrout .= "</table>\n";
       #
       # Creation/Modification date limits
       $scrout .= '<h3>'.&mt('Creation and Modification dates').'</h3>';
       $scrout .= "\n<table>\n";
       my $cafter = 
           &Apache::lonhtmlcommon::date_setter('advsearch',         # formname
                                               'creationdate1', # fieldname
                                               0,           # current value
                                               '',          # special 
                                               1,           # includeempty
                                               '',          # state
                                               1,           # no_hh_mm_ss
                                               );
       my $cbefore = 
           &Apache::lonhtmlcommon::date_setter('advsearch',         # formname
                                               'creationdate2', # fieldname
                                               0,           # current value
                                               '',          # special 
                                               1,           # includeempty
                                               '',          # state
                                               1,           # no_hh_mm_ss
                                               );
       $scrout .= &mt('<tr><td align="right">Created between</td>'.
                      '<td>[_1]</td></tr>'.
                      '<tr><td align="right">and </td>'.
                      '<td>[_2]</td></tr>',$cafter,$cbefore);
       my $lafter = 
           &Apache::lonhtmlcommon::date_setter('advsearch',
                                               'revisiondate1', 
                                               0,           # current value
                                               '',          # special 
                                               1,           # includeempty
                                               '',          # state
                                               1,           # no_hh_mm_ss
                                               );
       my $lbefore = 
           &Apache::lonhtmlcommon::date_setter('advsearch',
                                               'revisiondate2',
                                               0,           # current value
                                               '',          # special 
                                               1,           # includeempty
                                               '',          # state
                                               1,           # no_hh_mm_ss
                                               );
       $scrout .= &mt('<tr><td align="right">Last modified between </td>'.
                      '<td>[_1]</td></tr>'.
                      '<tr><td align="right">and</td>'.
                      '<td>[_2]</td></tr>',$lafter,$lbefore);
     $scrout.="</table>\n";      $scrout.="</table>\n";
     $scrout.=<<ENDDOCUMENT;      $scrout.=<<ENDDOCUMENT;
 $advanced_buttons  $advanced_buttons
Line 804  Outputs: text for box with view options Line 1080  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 813  sub viewoptions { Line 1089  sub viewoptions {
  \&viewoptiontext,   \&viewoptiontext,
  sort(keys(%Views)));   sort(keys(%Views)));
     $scrout.= '&nbsp;&nbsp;';      $scrout.= '&nbsp;&nbsp;';
     $scrout.=&Apache::lonmeta::selectbox('show',      my $countselect = &Apache::lonmeta::selectbox('show',
  $ENV{'form.show'},                                                    $ENV{'form.show'},
  undef,                                                    undef,
  (10,20,50,100,1000,10000));                                                    (10,20,50,100,1000,10000));
     $scrout .= ('&nbsp;'x2).&mt('Records per Page').'</nobr>'.$/;      $scrout .= ('&nbsp;'x2).&mt('[_1] Records per Page',$countselect).
           '</nobr>'.$/;
     return $scrout;      return $scrout;
 }  }
   
Line 837  Outputs: return little blurb on how to e Line 1114  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 1027  Parse advanced search form and return th Line 1304  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',
                          'notes','abstract','extension','owner','authorspace',
   #                       'custommetadata','customshow',
                          'modifyinguser','standards','mime');
       my @StatsFields = &statfields();
       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 ('title','author','subject','keywords','url','version',      for my $field (@BasicFields,
    'creationdatestart_month','creationdatestart_day',                     'creationdatestart_month','creationdatestart_day',
    'creationdatestart_year','creationdateend_month',     'creationdatestart_year','creationdateend_month',
    'creationdateend_day','creationdateend_year',     'creationdateend_day','creationdateend_year',
    'lastrevisiondatestart_month','lastrevisiondatestart_day',     'lastrevisiondatestart_month','lastrevisiondatestart_day',
    'lastrevisiondatestart_year','lastrevisiondateend_month',     'lastrevisiondatestart_year','lastrevisiondateend_month',
    'lastrevisiondateend_day','lastrevisiondateend_year',     'lastrevisiondateend_day','lastrevisiondateend_year') {
    'notes','abstract','extension','language','owner',   $ENV{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\']//g;
    'custommetadata','customshow','category') {  
  $ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;  
     }      }
     foreach ('mode','form','element') {      foreach ('mode','form','element') {
  # is this required?  Hmmm.   # is this required?  Hmmm.
  next unless (exists($ENV{"form.$_"}));   next if (! exists($ENV{'form.'.$_}));
  $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;
     }      }
     # Preprocess the category form element.      # Preprocess the category form element.
     $ENV{'form.category'} = 'any' if (! defined($ENV{'form.category'}) ||      $ENV{'form.category'} = 'any' if (! defined($ENV{'form.category'}) ||
                                       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 ('title','author','subject','keywords','url','version',      foreach my $field (@BasicFields) {
    'notes','abstract','category','extension','language',   if (&filled($ENV{'form.'.$field})) {
                    'owner','custommetadata') {  
  if (&filled($ENV{"form.$field"})) {  
     $fillflag++;      $fillflag++;
  }   }
     }      }
     unless ($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') {
           if ( $ENV{'form.'.$field} =~ /^\d+$/ &&
                $ENV{'form.'.$field} > 0) {
               $fillflag++;
           }
       }
       if (! $fillflag) {
  &output_blank_field_error($r,$closebutton,   &output_blank_field_error($r,$closebutton,
                                   'phase=disp_adv',$hidden_fields);                                    'phase=disp_adv',$hidden_fields);
  return ;   return ;
Line 1069  sub parse_advanced_search { Line 1363  sub parse_advanced_search {
     my @queries;      my @queries;
     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 ('title','author','subject','notes','abstract','url',      foreach my $field (@BasicFields) {
        'keywords','version','owner','standards') {   next if (!defined($ENV{'form.'.$field}) || $ENV{'form.'.$field} eq '');
  if ($ENV{'form.'.$field}) {          my ($error,$SQLQuery) = 
             my $searchphrase = $ENV{'form.'.$field};              &process_phrase_input($ENV{'form.'.$field},
             $pretty_search_string .= $font."$field</font> contains <b>".                                    $ENV{'form.'.$field.'_related'},$field);
                 $searchphrase."</b>";          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 1102  sub parse_advanced_search { Line 1403  sub parse_advanced_search {
             $searchphrase = join(' OR ',@extensions);              $searchphrase = join(' OR ',@extensions);
         }          }
     }      }
     if (exists($ENV{'form.extension'}) && $ENV{'form.extension'} !~ /^\s*$/) {  
         $searchphrase .= ' OR ' if (defined($searchphrase));  
         my @extensions = split(/,/,$ENV{'form.extension'});  
         $searchphrase .= join(' OR ',@extensions);  
     }  
     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 />';
     }      }
     #      #
     # Evaluate option lists      # Evaluate option lists
       if ($ENV{'form.lowestgradelevel'}        &&
           $ENV{'form.lowestgradelevel'} ne '0' &&
           $ENV{'form.lowestgradelevel'} =~ /^\d+$/) {
    push(@queries,
                '(lowestgradelevel>='.$ENV{'form.lowestgradelevel'}.')');
           $pretty_search_string.="lowestgradelevel>=".
               $ENV{'form.lowestgradelevel'}."<br />\n";
       }
       if ($ENV{'form.highestgradelevel'}        &&
           $ENV{'form.highestgradelevel'} ne '0' &&
           $ENV{'form.highestgradelevel'} =~ /^\d+$/) {
    push(@queries,
                '(highestgradelevel<='.$ENV{'form.highestgradelevel'}.')');
           $pretty_search_string.="highestgradelevel<=".
               $ENV{'form.highestgradelevel'}."<br />\n";
       }
     if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {      if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
  push @queries,"(language like \"$ENV{'form.language'}\")";   push @queries,"(language like \"$ENV{'form.language'}\")";
         $pretty_search_string.=$font."language</font>= ".          $pretty_search_string.=$font."language</font>= ".
Line 1124  sub parse_advanced_search { Line 1437  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
       foreach my $field (@StatsFields,@EvalFields) {
           my ($min,$max);
           if (exists($ENV{'form.'.$field.'_min'}) && 
               $ENV{'form.'.$field.'_min'} ne '') {
               $min = $ENV{'form.'.$field.'_min'};
           }
           if (exists($ENV{'form.'.$field.'_max'}) &&
               $ENV{'form.'.$field.'_max'} ne '') {
               $max = $ENV{'form.'.$field.'_max'};
           }
           next if (! defined($max) && ! defined($min));
           if (defined($min) && defined($max)) {
               ($min,$max) = sort {$a <=>$b} ($min,$max);
           }
           if (defined($min) && $min =~ /^(\d+\.\d+|\d+|\.\d+)$/) {
               push(@queries,'('.$field.'>'.$min.')');
               $pretty_search_string.=$font.$field.'</font>&gt;'.$min.'<br />';
           }
           if (defined($max) && $max =~ /^(\d+\.\d+|\d+|\.\d+)$/) {
               push(@queries,'('.$field.'<'.$max.')');
               $pretty_search_string.=$font.$field.'</font>&lt;'.$max.'<br />';
           }
     }      }
     #      #
     # Evaluate date windows      # Evaluate date windows
     my $datequery=&build_date_queries(      my $cafter =
  $ENV{'form.creationdatestart_month'},          &Apache::lonhtmlcommon::get_date_from_form('creationdate1');
  $ENV{'form.creationdatestart_day'},      my $cbefore = 
  $ENV{'form.creationdatestart_year'},          &Apache::lonhtmlcommon::get_date_from_form('creationdate2');
  $ENV{'form.creationdateend_month'},      if ($cafter > $cbefore) {
  $ENV{'form.creationdateend_day'},          my $tmp = $cafter;
  $ENV{'form.creationdateend_year'},          $cafter = $cbefore;
  $ENV{'form.lastrevisiondatestart_month'},          $cbefore = $tmp;
  $ENV{'form.lastrevisiondatestart_day'},      }
  $ENV{'form.lastrevisiondatestart_year'},      my $mafter = 
  $ENV{'form.lastrevisiondateend_month'},          &Apache::lonhtmlcommon::get_date_from_form('revisiondate1');
  $ENV{'form.lastrevisiondateend_day'},      my $mbefore =
  $ENV{'form.lastrevisiondateend_year'},          &Apache::lonhtmlcommon::get_date_from_form('revisiondate2');
  );      if ($mafter > $mbefore) {
     # Test to see if date windows are legitimate          my $tmp = $mafter;
     if ($datequery=~/^Incorrect/) {          $mafter = $mbefore;
  &output_date_error($r,$datequery,$closebutton,$hidden_fields);          $mbefore = $tmp;
  return ;      }
     } elsif ($datequery) {      my ($datequery,$error,$prettydate)=&build_date_queries($cafter,$cbefore,
                                                              $mafter,$mbefore);
       if (defined($error)) {
           &output_date_error($r,$error,$closebutton,$hidden_fields);
       } elsif (defined($datequery)) {
         # Here is where you would set up pretty_search_string to output          # Here is where you would set up pretty_search_string to output
         # date query information.          # date query information.
           $pretty_search_string .= '<br />'.$prettydate.'<br />';
  push @queries,$datequery;   push @queries,$datequery;
     }      }
     #      #
Line 1182  sub parse_advanced_search { Line 1525  sub parse_advanced_search {
     $pretty_search_string .= $pretty_domains_string."<br />\n";      $pretty_search_string .= $pretty_domains_string."<br />\n";
     #      #
     if (@queries) {      if (@queries) {
  $query=join(" AND ",@queries);   $query="SELECT * FROM metadata WHERE (".join(") AND (",@queries).')';
  $query="select * from metadata where $query";  
     } elsif ($customquery) {      } elsif ($customquery) {
         $query = '';          $query = '';
     }      }
       # &Apache::lonnet::logthis('query = '.$/.$query);
     return ($query,$customquery,$customshow,$libraries_to_query,      return ($query,$customquery,$customshow,$libraries_to_query,
             $pretty_search_string);              $pretty_search_string);
 }  }
Line 1195  sub parse_domain_restrictions { Line 1538  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,'');
     }      }
     my @allowed_domains;      my @allowed_domains;
Line 1248  sub parse_basic_search { Line 1591  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 1260  sub parse_basic_search { Line 1603  sub parse_basic_search {
         &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
     unless (&filled($ENV{'form.basicexp'})) {      my $search_string = $ENV{'form.basicexp'};
       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;
     my $search_string = $ENV{'form.basicexp'};      my @Queries;
     if ($ENV{'form.related'}) {      my $searchfield = 'concat_ws(" ",'.join(',',
         my @New_Words;                                              ('title','author','subject',
         ($search_string,@New_Words) = &related_version($ENV{'form.basicexp'});                                               'notes','abstract','keywords')
         if (@New_Words) {                                              ).')';
             $pretty_search_string .= " with related words: <b>@New_Words</b>.";      my ($error,$SQLQuery) = &process_phrase_input($search_string,
         } else {                                                      $ENV{'form.related'},
             $pretty_search_string .= " with no related words.";                                                      $searchfield);
         }      if ($error) {
           &output_unparsed_phrase_error($r,$closebutton,'phase=disp_basic',
                                         '','basicexp');
           return;
     }      }
       push(@Queries,$SQLQuery);
       #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($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 1311  Note: Using this twice on a string is pr Line 1836  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 1384  sub build_custommetadata_query { Line 1879  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 1441  Also reports errors (check for /^Incorre Line 1895  Also reports errors (check for /^Incorre
 ######################################################################  ######################################################################
 ######################################################################  ######################################################################
 sub build_date_queries {  sub build_date_queries {
     my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,      my ($cafter,$cbefore,$mafter,$mbefore) = @_;
  $lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;      my ($result,$error,$pretty_string);
     my @queries;      #
     if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {      # Verify the input
  unless ($cmonth1 and $cday1 and $cyear1 and      if (! defined($cafter) && ! defined($cbefore) &&
  $cmonth2 and $cday2 and $cyear2) {          ! defined($mafter) && ! defined($mbefore)) {
     return "Incorrect entry for the creation date.  You must specify ".          # This is an okay situation, so return undef for the error
    "a starting month, day, and year and an ending month, ".          return (undef,undef,undef);
    "day, and year.";      }
  }      if ((defined($cafter)  && ! defined($cbefore)) ||
  my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);          (defined($cbefore) && ! defined($cafter))) {
  $cnumeric1+=0;          # This is bad, so let them know
  my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);          $error = &mt('Incorrect entry for the creation date.  '.
  $cnumeric2+=0;                      'You must specify both the beginning and ending dates.');
  if ($cnumeric1>$cnumeric2) {      }
     return "Incorrect entry for the creation date.  The starting ".      if (! defined($error) && 
    "date must occur before the ending date.";          ((defined($mafter)  && ! defined($mbefore)) ||
  }          (defined($mbefore) && ! defined($mafter)))) {
  my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".          # This is also bad, so let them know
            "$cyear2-$cmonth2-$cday2 23:59:59')";          $error = &mt('Incorrect entry for the last revision date.  '.
  push @queries,$cquery;                       'You must specify both the beginning and ending dates.');
     }  
     if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {  
  unless ($lmonth1 and $lday1 and $lyear1 and  
  $lmonth2 and $lday2 and $lyear2) {  
     return "Incorrect entry for the last revision date.  You must ".  
    "specify a starting month, day, and year and an ending ".  
    "month, day, and year.";  
  }  
  my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);  
  $lnumeric1+=0;  
  my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);  
  $lnumeric2+=0;  
  if ($lnumeric1>$lnumeric2) {  
     return "Incorrect entry for the last revision date.  The ".  
    "starting date must occur before the ending date.";  
  }  
  my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".  
            "$lyear2-$lmonth2-$lday2 23:59:59')";  
  push @queries,$lquery;  
     }      }
     if (@queries) {      if (! defined($error)) {
  return join(" AND ",@queries);          #
           # Build the queries
           my @queries;
           if (defined($cbefore) && defined($cafter)) {
               my (undef,undef,undef,$caday,$camon,$cayear) = localtime($cafter);
               my (undef,undef,undef,$cbday,$cbmon,$cbyear) = localtime($cbefore);
               # Correct for year being relative to 1900
               $cayear+=1900; $cbyear+=1900;
               my $cquery=
                   '(creationdate BETWEEN '.
                   "'".$cayear.'-'.$camon.'-'.$caday."'".
                   ' AND '.
                   "'".$cbyear.'-'.$cbmon.'-'.$cbday." 23:59:59')";
               $pretty_string .= '<br />' if (defined($pretty_string));
               $pretty_string .= 
                   &mt('created between [_1] and [_2]',
                       &Apache::lonlocal::locallocaltime($cafter),
                       &Apache::lonlocal::locallocaltime($cbefore+24*60*60-1));
               push(@queries,$cquery);
               $pretty_string =~ s/ 00:00:00//g;
           }
           if (defined($mbefore) && defined($mafter)) {
               my (undef,undef,undef,$maday,$mamon,$mayear) = localtime($mafter);
               my (undef,undef,undef,$mbday,$mbmon,$mbyear) = localtime($mbefore);
               # Correct for year being relative to 1900
               $mayear+=1900; $mbyear+=1900;
               my $mquery=
                   '(lastrevisiondate BETWEEN '.
                   "'".$mayear.'-'.$mamon.'-'.$maday."'".
                   ' AND '.
                   "'".$mbyear.'-'.$mbmon.'-'.$mbday." 23:59:59')";
               push(@queries,$mquery);
               $pretty_string .= '<br />' if (defined($pretty_string));
               $pretty_string .= 
                   &mt('last revised between [_1] and [_2]',
                       &Apache::lonlocal::locallocaltime($mafter),
                       &Apache::lonlocal::locallocaltime($mbefore+24*60*60-1));
               $pretty_string =~ s/ 00:00:00//g;
           }
           if (@queries) {
               $result .= join(" AND ",@queries);
           }
     }      }
     return '';      return ($result,$error,$pretty_string);
 }  }
   
 ######################################################################  ######################################################################
Line 1595  a link to change the search query. Line 2071  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 1715  Returns: the identifier of the table on Line 2195  Returns: the identifier of the table on
 sub set_up_table_structure {  sub set_up_table_structure {
     my ($datatypes,$fullindicies) =       my ($datatypes,$fullindicies) = 
         &LONCAPA::lonmetadata::describe_metadata_storage();          &LONCAPA::lonmetadata::describe_metadata_storage();
     unshift(@$datatypes,{name => 'id',       # Copy the table description before modifying it...
       @Datatypes = @{$datatypes};
       unshift(@Datatypes,{name => 'id',  
         type => 'MEDIUMINT',          type => 'MEDIUMINT',
         restrictions => 'UNSIGNED NOT NULL',          restrictions => 'UNSIGNED NOT NULL',
         primary_key  => 'yes',          primary_key  => 'yes',
         auto_inc     => 'yes' });          auto_inc     => 'yes' });
     @Datatypes = @{$datatypes};  
     @Fullindicies = @{$fullindicies};      @Fullindicies = @{$fullindicies};
     return;      return;
 }  }
Line 1783  sub update_status { Line 2264  sub update_status {
     $r->rflush();      $r->rflush();
 }  }
   
   {
       my $max_time  = 40;  # seconds for the search to complete
       my $start_time = 0;
       my $last_time = 0;
   
   sub reset_timing {
       $start_time = 0;
       $last_time = 0;
   }
   
   sub time_left {
       if ($start_time == 0) {
           $start_time = time;
       }
       my $time_left = $max_time - (time - $start_time);
       $time_left = 0 if ($time_left < 0);
       return $time_left;
   }
   
 sub update_seconds {  sub update_seconds {
     my ($r,$text) = @_;      my ($r) = @_;
     $text =~ s/\'/\\\'/g;      my $time = &time_left();
     $r->print      if (($last_time-$time) > 0) {
         ("<script>document.statusform.seconds.value = ' $text'</script>\n");          $r->print("<script>".
     $r->rflush();                    "document.statusform.seconds.value = '$time'".
                     "</script>\n");
           $r->rflush();
       }
       $last_time = $time;
   }
   
 }  }
   
 ######################################################################  ######################################################################
Line 1835  results into MySQL. Line 2341  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;
     #      #
     # Timing variables  
     #  
     my $starttime = time;  
     my $max_time  = 30;  # seconds for the search to complete  
     #  
     # Print run_search header      # Print run_search header
     #      #
     $r->print(<<END);      $r->print(<<END);
Line 1851  $bodytag Line 2355  $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").": ".$pretty_string);      $r->print(&mt("Search: [_1]",$pretty_string));
     $r->rflush();      $r->rflush();
     #      #
     # Determine the servers we need to contact.      # Determine the servers we need to contact.
Line 1873  END Line 2381  END
         @Servers_to_contact = sort(keys(%Apache::lonnet::libserv));          @Servers_to_contact = sort(keys(%Apache::lonnet::libserv));
     }      }
     my %Server_status;      my %Server_status;
       #
       # Check on the mysql table we will use to store results.
     my $table =$ENV{'form.table'};      my $table =$ENV{'form.table'};
     if (! defined($table) || $table eq '' || $table =~ /\D/ ) {      if (! defined($table) || $table eq '' || $table =~ /\D/ ) {
         $r->print("Unable to determine table id to store search results in.".          $r->print("Unable to determine table id to store search results in.".
Line 1917  END Line 2427  END
 </form>  </form>
 END  END
     $r->rflush();      $r->rflush();
     my $time_remaining = $max_time - (time - $starttime) ;      &reset_timing();
     my $last_time = $time_remaining;      &update_seconds($r);
     &update_seconds($r,$time_remaining);      &update_status($r,&mt('contacting [_1]',$Servers_to_contact[0]));
     &update_status($r,'contacting '.$Servers_to_contact[0]);      while (&time_left() &&
     while (($time_remaining > 0) &&  
            ((@Servers_to_contact) || keys(%Server_status))) {             ((@Servers_to_contact) || keys(%Server_status))) {
         # Send out a search request if it needs to be done.          &update_seconds($r);
           #
           # Send out a search request
         if (@Servers_to_contact) {          if (@Servers_to_contact) {
             # Contact one server              # Contact one server
             my $server = shift(@Servers_to_contact);              my $server = shift(@Servers_to_contact);
             &update_status($r,&mt('contacting').' '.$server);              &update_status($r,&mt('contacting [_1]',$server));
             my $reply=&Apache::lonnet::metadata_query($query,$customquery,              my $reply=&Apache::lonnet::metadata_query($query,$customquery,
                                                       $customshow,[$server]);                                                        $customshow,[$server]);
             ($server) = keys(%$reply);              ($server) = keys(%$reply);
Line 1939  END Line 2450  END
             # left to contact.                # left to contact.  
             if (scalar (keys(%Server_status))) {              if (scalar (keys(%Server_status))) {
                 &update_status($r,                  &update_status($r,
                        &mt('waiting on').' '.(join(' ',keys(%Server_status))));                         &mt('waiting on [_1]',join(' ',keys(%Server_status))));
             }              }
             sleep(1)              sleep(1)
         }          }
         #          #
         # 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);
               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 1955  END Line 2468  END
             $status=~/^([\.\w]+)$/;               $status=~/^([\.\w]+)$/; 
            my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1;             my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1;
             if (-e $datafile && ! -e "$datafile.end") {              if (-e $datafile && ! -e "$datafile.end") {
                 &update_status($r,&mt('Receiving results from').' '.$server);                  &update_status($r,&mt('Receiving results from [_1]',$server));
                 next;                  next;
             }              }
             last if ($connection->aborted());              last if ($connection->aborted());
             if (-e "$datafile.end") {              if (-e "$datafile.end") {
                 &update_status($r,&mt('Reading results from').' '.$server);                  &update_status($r,&mt('Reading results from [_1]',$server));
                 if (-z "$datafile") {                  if (-z "$datafile") {
                     delete($Server_status{$server});                      delete($Server_status{$server});
                     next;                      next;
Line 1975  END Line 2488  END
                 # Read in the whole file.                  # Read in the whole file.
                 while (my $result = <$fh>) {                  while (my $result = <$fh>) {
                     last if ($connection->aborted());                      last if ($connection->aborted());
                     # handle custom fields?  Someday we will!                      #
                       # Records are stored one per line
                     chomp($result);                      chomp($result);
                     next unless $result;                      next if (! $result);
                       #
                     # Parse the result.                      # Parse the result.
                     my %Fields = &parse_raw_result($result,$server);                      my %Fields = &parse_raw_result($result,$server);
                     $Fields{'hostname'} = $server;                      $Fields{'hostname'} = $server;
                       #
                       # Skip based on copyright
                     next if (! &copyright_check(\%Fields));                      next if (! &copyright_check(\%Fields));
                       #
                     # Store the result in the mysql database                      # Store the result in the mysql database
                     my $result = &Apache::lonmysql::store_row($table,\%Fields);                      my $result = &Apache::lonmysql::store_row($table,\%Fields);
                     if (! defined($result)) {                      if (! defined($result)) {
                         $r->print(&Apache::lonmysql::get_error());                          $r->print(&Apache::lonmysql::get_error());
                     }                      }
                     # $r->print(&Apache::lonmysql::get_debug());                      #
                     $hitcountsum ++;                      $hitcountsum ++;
                     $time_remaining = $max_time - (time - $starttime) ;                      &update_seconds($r);
                     if ($last_time - $time_remaining > 0) {  
                         &update_seconds($r,$time_remaining);  
                         $last_time = $time_remaining;  
                     }  
                     if ($hitcountsum % 50 == 0) {                      if ($hitcountsum % 50 == 0) {
                         &update_count_status($r,$hitcountsum);                          &update_count_status($r,$hitcountsum);
                     }                      }
                 } # End of foreach (@results)                  }
                 $fh->close();                  $fh->close();
                 # $server is only deleted if the results file has been                   # $server is only deleted if the results file has been 
                 # found and (successfully) opened.  This may be a bad idea.                  # found and (successfully) opened.  This may be a bad idea.
Line 2007  END Line 2521  END
             &update_count_status($r,$hitcountsum);              &update_count_status($r,$hitcountsum);
         }          }
         last if ($connection->aborted());          last if ($connection->aborted());
         # Finished looping through the servers          &update_seconds($r);
         $starttime = time if (@Servers_to_contact);  
         $time_remaining = $max_time - (time - $starttime) ;  
         if ($last_time - $time_remaining > 0) {  
             $last_time = $time_remaining;  
             &update_seconds($r,$time_remaining);  
         }  
     }      }
     &update_status($r,&mt('Search Complete').$server);      &update_status($r,&mt('Search Complete [_1]',$server));
     &update_seconds($r,0);      &update_seconds($r);
     #      #
     &Apache::lonmysql::disconnect_from_db();      &Apache::lonmysql::disconnect_from_db(); # This is unneccessary
     #      #
     # We have run out of time or run out of servers to talk to and      # We have run out of time or run out of servers to talk to and
     # results to get.        # results to get, so let the client know the top frame needs to be
       # 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 2049  Returns html for the previous and next b Line 2558  Returns html for the previous and next b
 sub prev_next_buttons {  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.
     my $links;      #
     ##      # Create buttons
     ## Prev      my $buttons = '<input type="submit" name="prev" value="'.&mt('Prev').'" ';
     my $prev_min = $current_min - $show;      $buttons .= '/>';
     $prev_min = 1 if $prev_min < 1;      $buttons .= '&nbsp;'x3;
     if ($prev_min < $current_min) {      $buttons .= '<input type="submit" name="reload" '.
         $links .=           'value="'.&mt('Reload').'" />';
             qq{<a href="/adm/searchcat?$parms&start=$prev_min&show=$show">}.      $buttons .= '&nbsp;'x3;
             &mt('prev').'</a>';      $buttons .= '<input type="submit" name="next" value="'.&mt('Next').'" ';
     } else {      $buttons .= '/>';
         $links .= &mt('prev');      return $buttons;
     }  
     ##  
     ## Pages.... Someday.  
     ##  
     $links .=   
         qq{&nbsp;  
            <a href="/adm/searchcat?$parms&start=$current_min&$show=$show">}.  
                 &mt('reload').'</a>';  
     ##  
     ## Next  
     my $next_min = $current_min + $show;  
     $next_min = $current_min if ($next_min > $total);  
     if ($next_min != $current_min) {  
         $links .=   
             qq{&nbsp;  
                <a href="/adm/searchcat?$parms&start=$next_min&show=$show">}.  
                     &mt('next').'</a>';  
     } else {  
         $links .= '&nbsp;'.&mt('next');  
     }  
     return $links;  
 }  }
   
 ######################################################################  ######################################################################
Line 2143  sub display_results { Line 2631  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 2154  sub display_results { Line 2649  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 @field_order =  ('default',
                           'title',
                           'author',
                           'subject',
                           'url',
                           'keywords',
                           'version',
                           'language',
                           'creationdate'=>,
                           'lastrevisiondate',
                           'owner',
                           'copyright',
                           'authorspace',
                           'lowestgradeleve',
                           'highestgradelevel',
                           'standards',
                           'count',
                           'stdno',
                           'avetries',
                           'difficulty',
                           'disc',
                           'clear',
                           'technical',
                           'correct',
                           'helpful',
                           'depth',
                           );                                              
       my %sort_fields = ('default'     => 'Default',
                          'title'       => 'Title',
                          'author'      => 'Author',
                          'subject'     => 'Subject',
                          'url'         => 'URL',
                          'keywords'    => 'Keywords',
                          'version'     => 'Version',
                          'language'    => 'Language',
                          'creationdate'=> 'Creation Date',
                          'lastrevisiondate' => 'Last Revision Date',
                          'owner'       => 'Owner',
                          'copyright'   => 'Copyright',
                          'authorspace' => 'Authorspace',
                          'lowestgradeleve' => 'Lowest Grade Level',
                          'highestgradelevel' => 'Highest Grade Level',
                          'standards'   => 'Standards',
                          'count'       => 'Number of Accesses',
                          'stdno'       => 'Students Attempting',
                          'avetries'    => 'Average Number of Tries',
                          'difficulty'  => 'Mean Degree of Difficulty',
                          'disc'        => 'Mean Degree of Discrimination',
                          'clear'       => 'Evaluation: Clear',
                          'technical'   => 'Evaluation: Technically Correct',
                          'correct'     => 'Evaluation: Material is Correct',
                          'helpful'     => 'Evaluation: Material is Helpful',
                          'depth'       => 'Evaluation: Material has Depth',
                          'select_form_order' => \@field_order,
                          );
   
       my $sortform = &mt('Sort by [_1]',
                          &Apache::loncommon::select_form($ENV{'form.sortfield'},
                                                         'sortfield',
                                                         %sort_fields));
       ##
     ## 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,           $sortform.
                             "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="1">'.          $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="2">'.
                   '<h3>'.&mt('There are currently no results').'.</h3>'.                    '<h3>'.&mt('There are currently no results').'.</h3>'.
                   "</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);      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'}.
               '  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 2185  sub display_results { Line 2757  sub display_results {
         }          }
         my %Fields = %{&parse_row(@$row)};          my %Fields = %{&parse_row(@$row)};
         my $output="<p>\n";          my $output="<p>\n";
           if (! defined($Fields{'title'}) || $Fields{'title'} eq '') {
               $Fields{'title'} = 'Untitled';
           }
         my $prefix=&catalogmode_output($Fields{'title'},$Fields{'url'},          my $prefix=&catalogmode_output($Fields{'title'},$Fields{'url'},
                                        $Fields{'id'},$checkbox_num++);                                         $Fields{'id'},$checkbox_num++);
         # Render the result into html          # Render the result into html
Line 2497  SCRIPT Line 3072  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 2555  ENDFRAMES Line 3130  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 2576  extra custom metadata to show. Line 3186  extra custom metadata to show.
 ######################################################################  ######################################################################
 sub detailed_citation_view {  sub detailed_citation_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
     my $icon=&Apache::loncommon::icon($values{'url'});      my $result;
     my $result=<<END;      $result .= '<b>'.$prefix.
 <b>$prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}"           '<img src="'.&Apache::loncommon::icon($values{'url'}).' " />'.'&nbsp;'.
     target='search_preview'>$values{'title'}</a></b>          '<a href="http://'.$ENV{'HTTP_HOST'}.$values{'url'}.'" '.
 <p>          'target="search_preview">'.$values{'title'}."</a></b>\n";
 <b>$values{'author'}</b>, <i>$values{'owner'}</i><br />      $result .= "<p>\n";
       $result .= '<b>'.$values{'author'}.'</b>,'.
 <b>Subject:       </b> $values{'subject'}<br />          ' <i>'.$values{'owner'}.'</i><br />';
 <b>Keyword(s):    </b> $values{'keywords'}<br />      foreach my $field 
 <b>Notes:         </b> $values{'notes'}<br />          (
 <b>MIME Type:     </b> $values{'mimetag'}<br />           { name=>'url',
 <b>Language:      </b> $values{'language'}<br />             translate => '<b>URL:</b>&nbsp;[_1]',
 <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />             special => 'url link',},
 </p>           { name=>'subject',
 $values{'extrashow'}             translate => '<b>Subject:</b>&nbsp;[_1]',},
 <p>           { name=>'keywords',
 $values{'shortabstract'}             translate => '<b>Keywords:</b>&nbsp;[_1]',},
 </p>           { name=>'notes',
 <hr align='left' width='200' noshade />             translate => '<b>Notes:</b>&nbsp;[_1]',},
 END           { name=>'mimetag',
              translate => '<b>MIME Type:</b>&nbsp;[_1]',},
            { name=>'standards',
              translate => '<b>Standards:</b>[_1]',},
            { name=>'copyrighttag',
              translate => '<b>Copyright/Distribution:</b>&nbsp;[_1]',},
            { name=>'count',
              format => "%d",
              translate => '<b>Access Count:</b>&nbsp;[_1]',},
            { name=>'stdno',
              format => "%d",
              translate => '<b>Number of Students:</b>&nbsp;[_1]',},
            { name=>'avetries',
              format => "%.2f",
              translate => '<b>Average Tries:</b>&nbsp;[_1]',},
            { name=>'disc',
              format => "%.2f",
              translate => '<b>Degree of Discrimination:</b>&nbsp;[_1]',},
            { name=>'difficulty',
              format => "%.2f",
              translate => '<b>Degree of Difficulty:</b>&nbsp;[_1]',},
            { name=>'clear',
              format => "%.2f",
              translate => '<b>Clear:</b>&nbsp;[_1]',},
            { name=>'depth',
              format => "%.2f",
              translate => '<b>Depth:</b>&nbsp;[_1]',},
            { name=>'helpful',
              format => "%.2f",
              translate => '<b>Helpful:</b>&nbsp;[_1]',},
            { name=>'correct',
              format => "%.2f",
              translate => '<b>Correct:</b>&nbsp;[_1]',},
            { name=>'technical',
              format => "%.2f",
              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',},
            ) {
           next if (! exists($values{$field->{'name'}}) ||
                    $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 .= '<hr align="left" width="200" noshade />'."\n";
     return $result;      return $result;
 }  }
   
Line 2613  sub summary_view { Line 3305  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=<<END;
 $prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}"   $prefix<img src="$icon" />&nbsp;
   <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
    target='search_preview'>$values{'author'}</a><br />     target='search_preview'>$values{'author'}</a><br />
 $values{'title'}<br />  $values{'title'}<br />
 $values{'owner'} -- $values{'lastrevisiondate'}<br />  $values{'owner'} -- $values{'lastrevisiondate'}<br />
Line 2638  END Line 3331  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'>          '">&nbsp;<a href="'.$values{'url'}.'" target="search_preview">'.
 $values{'title'}</a>          $values{'title'}.'</a>'.('&nbsp;'x2).
 <b>$values{'author'}</b><br />          '<b>'.$values{'author'}.'</b><br />';
 END  
     return $result;      return $result;
 }  }
   
Line 2662  END Line 3354  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','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 2702  END Line 3401  END
 ######################################################################  ######################################################################
 sub xml_sgml_view {  sub xml_sgml_view {
     my ($prefix,%values) = @_;      my ($prefix,%values) = @_;
       my $xml = '<LonCapaResource>'."\n";
       # The usual suspects
       foreach my $field ('url','title','author','subject','keywords','notes') {
           $xml .= qq{<$field>$values{$field}</$field>}."\n";
       }
       #
       $xml .= "<mimeInfo>\n";
       foreach my $field ('mime','mimetag') {
           $xml .= qq{<$field>$values{$field}</$field>}."\n";
       }
       $xml .= "</mimeInfo>\n";
       #
       $xml .= "<languageInfo>\n";
       foreach my $field ('language','languagetag') {
           $xml .= qq{<$field>$values{$field}</$field>}."\n";
       }
       $xml .= "</languageInfo>\n";
       #
       foreach my $field ('creationdate','lastrevisiondate','owner') {
           $xml .= qq{<$field>$values{$field}</$field>}."\n";
       }
       #
       $xml .= "<copyrightInfo>\n";
       foreach my $field ('copyright','copyrighttag') {
           $xml .= qq{<$field>$values{$field}</$field>}."\n";
       }
       $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,'<>&');
     my $result=<<END;      my $result=<<END;
 $prefix  $prefix
 <pre>  <pre>
 &lt;LonCapaResource&gt;  $xml
 &lt;url&gt;$values{'url'}&lt;/url&gt;  
 &lt;title&gt;$values{'title'}&lt;/title&gt;  
 &lt;author&gt;$values{'author'}&lt;/author&gt;  
 &lt;subject&gt;$values{'subject'}&lt;/subject&gt;  
 &lt;keywords&gt;$values{'keywords'}&lt;/keywords&gt;  
 &lt;notes&gt;$values{'notes'}&lt;/notes&gt;  
 &lt;mimeInfo&gt;  
 &lt;mime&gt;$values{'mime'}&lt;/mime&gt;  
 &lt;mimetag&gt;$values{'mimetag'}&lt;/mimetag&gt;  
 &lt;/mimeInfo&gt;  
 &lt;languageInfo&gt;  
 &lt;language&gt;$values{'language'}&lt;/language&gt;  
 &lt;languagetag&gt;$values{'languagetag'}&lt;/languagetag&gt;  
 &lt;/languageInfo&gt;  
 &lt;creationdate&gt;$values{'creationdate'}&lt;/creationdate&gt;  
 &lt;lastrevisiondate&gt;$values{'lastrevisiondate'}&lt;/lastrevisiondate&gt;  
 &lt;owner&gt;$values{'owner'}&lt;/owner&gt;  
 &lt;copyrightInfo&gt;  
 &lt;copyright&gt;$values{'copyright'}&lt;/copyright&gt;  
 &lt;copyrighttag&gt;$values{'copyrighttag'}&lt;/copyrighttag&gt;  
 &lt;/copyrightInfo&gt;  
 &lt;repositoryLocation&gt;$values{'hostname'}&lt;/repositoryLocation&gt;  
 &lt;shortabstract&gt;$values{'shortabstract'}&lt;/shortabstract&gt;  
 &lt;/LonCapaResource&gt;  
 </pre>  </pre>
 $values{'extrashow'}  $values{'extrashow'}
 <hr align='left' width='200' noshade />  <hr align='left' width='200' noshade />
Line 2762  sub filled { Line 3486  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
       $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 2779  $parms is extra information to include i Line 3548  $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');
       my $heading = &mt('Unactionable Search Queary');
       $r->print(<<ENDPAGE);
 <html>  <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>  <p>
 You did not fill in enough information for the search to be started.  $errormsg
 You need to fill in relevant fields on the search page in order   </p>
 for a query to be processed.  <p>
   <a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}">$revise</a>&nbsp;
 </p>  </p>
 </body>  </body>
 </html>  </html>
 RESULTS  ENDPAGE
       return;
 }  }
   
 ######################################################################  ######################################################################

Removed from v.1.209  
changed lines
  Added in v.1.235


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