Annotation of loncom/interface/lonsearchcat.pm, revision 1.202

1.98      harris41    1: # The LearningOnline Network with CAPA
1.108     harris41    2: # Search Catalog
                      3: #
1.202   ! www         4: # $Id: lonsearchcat.pm,v 1.201 2004/01/02 16:34:03 www Exp $
1.108     harris41    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
1.98      harris41   14: #
1.108     harris41   15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.1       www        27: #
1.121     matthew    28: ###############################################################################
                     29: ###############################################################################
                     30: 
                     31: =pod 
                     32: 
                     33: =head1 NAME
                     34: 
1.140     matthew    35: lonsearchcat - LONCAPA Search Interface
1.121     matthew    36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Search interface to LON-CAPAs digital library
                     40: 
                     41: =head1 DESCRIPTION
                     42: 
                     43: This module enables searching for a distributed browseable catalog.
1.104     harris41   44: 
1.121     matthew    45: This is part of the LearningOnline Network with CAPA project
                     46: described at http://www.lon-capa.org.
                     47: 
                     48: lonsearchcat presents the user with an interface to search the LON-CAPA
                     49: digital library.  lonsearchcat also initiates the execution of a search
                     50: by sending the search parameters to LON-CAPA servers.  The progress of 
                     51: search (on a server basis) is displayed to the user in a seperate window.
                     52: 
                     53: =head1 Internals
                     54: 
                     55: =over 4
                     56: 
                     57: =cut
                     58: 
                     59: ###############################################################################
1.98      harris41   60: ###############################################################################
1.121     matthew    61: 
1.1       www        62: package Apache::lonsearchcat;
                     63: 
                     64: use strict;
1.167     www        65: use Apache::Constants qw(:common :http);
1.6       harris41   66: use Apache::lonnet();
                     67: use Apache::File();
1.7       harris41   68: use CGI qw(:standard);
1.41      harris41   69: use Text::Query;
1.101     harris41   70: use GDBM_File;
1.112     harris41   71: use Apache::loncommon();
1.144     matthew    72: use Apache::lonmysql();
1.200     www        73: use Apache::lonmeta;
                     74: use Apache::lonhtmlcommon;
1.186     www        75: use Apache::lonlocal;
1.1       www        76: 
1.121     matthew    77: ######################################################################
                     78: ######################################################################
1.196     matthew    79: ##
                     80: ## Global variables
                     81: ##
1.121     matthew    82: ######################################################################
                     83: ######################################################################
1.196     matthew    84: my %groupsearch_db;  # Database hash used to save values for the 
                     85:                      # groupsearch RAT interface.
                     86: my %persistent_db;   # gdbm hash which holds data which is supposed to
                     87:                      # persist across calls to lonsearchcat.pm
1.121     matthew    88: 
1.200     www        89: # The different view modes and associated functions
                     90: 
                     91: my %Views = ("detailed" => \&detailed_citation_view,
                     92: 	     "summary"  => \&summary_view,
                     93: 	     "fielded"  => \&fielded_format_view,
                     94: 	     "xml"      => \&xml_sgml_view,
                     95: 	     "compact"  => \&compact_view);
1.101     harris41   96: 
1.121     matthew    97: ######################################################################
                     98: ######################################################################
1.98      harris41   99: sub handler {
                    100:     my $r = shift;
1.197     www       101: #    &set_defaults();
1.196     matthew   102:     #
                    103:     # set form defaults
1.145     matthew   104:     #
1.196     matthew   105:     my $hidden_fields;# Hold all the hidden fields used to keep track
                    106:                       # of the search system state
                    107:     my $importbutton; # button to take the selected results and go to group 
                    108:                       # sorting
                    109:     my $diropendb;    # The full path to the (temporary) search database file.
                    110:                       # This is set and used in &handler() and is also used in 
                    111:                       # &output_results().
                    112:     my $bodytag;  # LON-CAPA standard body tag, gotten from 
                    113:                   # &Apache::lonnet::bodytag. 
                    114:                   # No title, no table, just a <body> tag.
1.157     www       115: 
                    116:     my $loaderror=&Apache::lonnet::overloaderror($r);
                    117:     if ($loaderror) { return $loaderror; }
                    118: 
1.145     matthew   119:     my $closebutton;  # button that closes the search window 
                    120:                       # This button is different for the RAT compared to
                    121:                       # normal invocation.
                    122:     #
1.186     www       123:     &Apache::loncommon::content_type($r,'text/html');
1.98      harris41  124:     $r->send_http_header;
                    125:     return OK if $r->header_only;
1.156     matthew   126:     ##
                    127:     ## Prevent caching of the search interface window.  Hopefully this means
                    128:     ## we will get the launch=1 passed in a little more.
                    129:     &Apache::loncommon::no_cache($r);
1.145     matthew   130:     ## 
                    131:     ## Pick up form fields passed in the links.
                    132:     ##
                    133:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.146     matthew   134:              ['catalogmode','launch','acts','mode','form','element','pause',
1.158     matthew   135:               'phase','persistent_db_id','table','start','show',
1.191     albertel  136:               'cleargroupsort','titleelement']);
1.146     matthew   137:     ##
                    138:     ## The following is a trick - we wait a few seconds if asked to so
                    139:     ##     the daemon running the search can get ahead of the daemon
                    140:     ##     printing the results.  We only need (theoretically) to do
                    141:     ##     this once, so the pause indicator is deleted
                    142:     ##
                    143:     if (exists($ENV{'form.pause'})) {
1.181     matthew   144:         sleep(1);
1.146     matthew   145:         delete($ENV{'form.pause'});
                    146:     }
1.143     matthew   147:     ##
                    148:     ## Initialize global variables
                    149:     ##
1.121     matthew   150:     my $domain  = $r->dir_config('lonDefDomain');
1.177     albertel  151:     $diropendb= "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
1.145     matthew   152:     #
                    153:     # set the name of the persistent database
1.146     matthew   154:     #          $ENV{'form.persistent_db_id'} can only have digits in it.
1.145     matthew   155:     if (! exists($ENV{'form.persistent_db_id'}) ||
1.147     matthew   156:         ($ENV{'form.persistent_db_id'} =~ /\D/) ||
                    157:         ($ENV{'form.launch'} eq '1')) {
1.145     matthew   158:         $ENV{'form.persistent_db_id'} = time;
                    159:     }
1.155     matthew   160:     $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.146     matthew   161:     my $persistent_db_file = "/home/httpd/perl/tmp/".
1.145     matthew   162:         &Apache::lonnet::escape($domain).
                    163:             '_'.&Apache::lonnet::escape($ENV{'user.name'}).
                    164:                 '_'.$ENV{'form.persistent_db_id'}.'_persistent_search.db';
1.146     matthew   165:     ##
1.152     matthew   166:     if (! &get_persistent_form_data($persistent_db_file)) {
1.150     matthew   167:         if ($ENV{'form.phase'} =~ /(run_search|results)/) {
                    168:             &Apache::lonnet::logthis("lonsearchcat:Unable to recover data ".
                    169:                                      "from $persistent_db_file");
                    170:             $r->print(<<END);
                    171: <html>
                    172: <head><title>LON-CAPA Search Error</title></head>
1.155     matthew   173: $bodytag
1.150     matthew   174: We were unable to retrieve data describing your search.  This is a serious
                    175: error and has been logged.  Please alert your LON-CAPA administrator.
                    176: </body>
                    177: </html>
                    178: END
                    179:             return OK;
                    180:         }
1.147     matthew   181:     }
1.124     matthew   182:     ##
1.143     matthew   183:     ## Clear out old values from groupsearch database
1.124     matthew   184:     ##
1.146     matthew   185:     untie %groupsearch_db if (tied(%groupsearch_db));
1.158     matthew   186:     if (($ENV{'form.cleargroupsort'} eq '1') || 
                    187:         (($ENV{'form.launch'} eq '1') && 
                    188:          ($ENV{'form.catalogmode'} eq 'groupsearch'))) {
1.148     matthew   189: 	if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.101     harris41  190: 	    &start_fresh_session();
1.142     matthew   191: 	    untie %groupsearch_db;
1.158     matthew   192:             delete($ENV{'form.cleargroupsort'});
1.122     matthew   193: 	} else {
1.155     matthew   194:             # This is a stupid error to give to the user.  
                    195:             # It really tells them nothing.
                    196: 	    $r->print('<html><head></head>'.$bodytag.
                    197:                       'Unable to tie hash to db file</body></html>');
1.101     harris41  198: 	    return OK;
                    199: 	}
                    200:     }
1.124     matthew   201:     ##
1.150     matthew   202:     ## Configure hidden fields
1.124     matthew   203:     ##
1.145     matthew   204:     $hidden_fields = '<input type="hidden" name="persistent_db_id" value="'.
1.150     matthew   205:         $ENV{'form.persistent_db_id'}.'" />'."\n";
                    206:     if (exists($ENV{'form.catalogmode'})) {
                    207:         $hidden_fields .= '<input type="hidden" name="catalogmode" value="'.
                    208:                 $ENV{'form.catalogmode'}.'" />'."\n";
                    209:     }
                    210:     if (exists($ENV{'form.form'})) {
                    211:         $hidden_fields .= '<input type="hidden" name="form" value="'.
                    212:                 $ENV{'form.form'}.'" />'."\n";
                    213:     }
                    214:     if (exists($ENV{'form.element'})) {
                    215:         $hidden_fields .= '<input type="hidden" name="element" value="'.
                    216:                 $ENV{'form.element'}.'" />'."\n";
                    217:     }
1.191     albertel  218:     if (exists($ENV{'form.titleelement'})) {
                    219:         $hidden_fields .= '<input type="hidden" name="titleelement" value="'.
                    220:                 $ENV{'form.titleelement'}.'" />'."\n";
                    221:     }
1.150     matthew   222:     if (exists($ENV{'form.mode'})) {
                    223:         $hidden_fields .= '<input type="hidden" name="mode" value="'.
                    224:                 $ENV{'form.mode'}.'" />'."\n";
                    225:     }
                    226:     ##
                    227:     ## Configure dynamic components of interface
1.146     matthew   228:     ##
1.98      harris41  229:     if ($ENV{'form.catalogmode'} eq 'interactive') {
1.150     matthew   230:         $closebutton="<input type='button' name='close' value='CLOSE' ";
                    231:         if ($ENV{'form.phase'} =~ /(results|run_search)/) {
                    232: 	    $closebutton .="onClick='parent.close()'";
                    233:         } else {
                    234:             $closebutton .="onClick='self.close()'";
                    235:         }
                    236:         $closebutton .=">\n";
1.124     matthew   237:     } elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
1.150     matthew   238:         $closebutton="<input type='button' name='close' value='CLOSE' ";
                    239:         if ($ENV{'form.phase'} =~ /(results|run_search)/) {
                    240: 	    $closebutton .="onClick='parent.close()'";
                    241:         } else {
                    242:             $closebutton .="onClick='self.close()'";
                    243:         }
                    244:         $closebutton .= ">";
1.98      harris41  245:         $importbutton=<<END;
                    246: <input type='button' name='import' value='IMPORT'
                    247: onClick='javascript:select_group()'>
                    248: END
1.146     matthew   249:     } else {
                    250:         $closebutton = '';
                    251:         $importbutton = '';
1.98      harris41  252:     }
1.124     matthew   253:     ##
1.146     matthew   254:     ## Sanity checks on form elements
1.124     matthew   255:     ##
1.146     matthew   256:     if (!defined($ENV{'form.viewselect'})) {
1.150     matthew   257:         if (($ENV{'form.catalogmode'} eq 'groupsearch') ||
                    258:             ($ENV{'form.catalogmode'} eq 'interactive')) {
                    259:             $ENV{'form.viewselect'} ="Compact View";
                    260:         } else {
                    261:             $ENV{'form.viewselect'} ="Detailed Citation View";
                    262:         }
1.146     matthew   263:     }
1.149     matthew   264:     $ENV{'form.phase'} = 'disp_basic' if (! exists($ENV{'form.phase'}));
1.151     matthew   265:     $ENV{'form.show'} = 20 if (! exists($ENV{'form.show'}));
1.146     matthew   266:     ##
                    267:     ## Switch on the phase
                    268:     ##
                    269:     if ($ENV{'form.phase'} eq 'disp_basic') {
1.196     matthew   270:         &print_basic_search_form($r,$closebutton,$hidden_fields);
1.146     matthew   271:     } elsif ($ENV{'form.phase'} eq 'disp_adv') {
1.196     matthew   272:         &print_advanced_search_form($r,$closebutton,$hidden_fields);
1.146     matthew   273:     } elsif ($ENV{'form.phase'} eq 'results') {
1.196     matthew   274:         &display_results($r,$importbutton,$closebutton,$diropendb);
1.151     matthew   275:     } elsif ($ENV{'form.phase'} =~ /^(sort|run_search)$/) {
1.146     matthew   276:         my ($query,$customquery,$customshow,$libraries,$pretty_string) =
                    277:             &get_persistent_data($persistent_db_file,
                    278:                  ['query','customquery','customshow',
                    279:                   'libraries','pretty_string']);
1.151     matthew   280:         if ($ENV{'form.phase'} eq 'sort') {
                    281:             &print_sort_form($r,$pretty_string);
                    282:         } elsif ($ENV{'form.phase'} eq 'run_search') {
                    283:             &run_search($r,$query,$customquery,$customshow,
                    284:                         $libraries,$pretty_string);
                    285:         }
1.167     www       286:     } elsif ($ENV{'form.phase'} eq 'course_search') {
                    287:         &course_search($r);
1.146     matthew   288:     } elsif(($ENV{'form.phase'} eq 'basic_search') ||
                    289:             ($ENV{'form.phase'} eq 'adv_search')) {
1.151     matthew   290:         $ENV{'form.searchmode'} = 'basic';
                    291:         if ($ENV{'form.phase'} eq 'adv_search') {
                    292:             $ENV{'form.searchmode'} = 'advanced';
                    293:         }
1.146     matthew   294:         # Set up table
                    295:         if (! defined(&create_results_table())) {
1.198     www       296: 	    my $errorstring=&Apache::lonmysql::get_error();
1.147     matthew   297:             $r->print(<<END);
                    298: <html><head><title>Search Error</title></head>
1.155     matthew   299: $bodytag
1.147     matthew   300: Unable to create table in which to store search results.  
                    301: The search has been aborted.
1.198     www       302: <br />$errorstring
1.147     matthew   303: </body>
                    304: </html>
                    305: END
                    306:             return OK;
1.146     matthew   307:         }
1.147     matthew   308:         delete($ENV{'form.launch'});
1.146     matthew   309:         if (! &make_form_data_persistent($r,$persistent_db_file)) {
1.147     matthew   310:             $r->print(<<END);
                    311: <html><head><title>Search Error</title></head>
1.155     matthew   312: $bodytag
1.147     matthew   313: Unable to properly store search information.  The search has been aborted.
                    314: </body>
                    315: </html>
                    316: END
                    317:             return OK;
1.146     matthew   318:         }
1.145     matthew   319:         #
1.139     matthew   320:         # We are running a search
1.134     matthew   321:         my ($query,$customquery,$customshow,$libraries) = 
                    322:             (undef,undef,undef,undef);
1.143     matthew   323:         my $pretty_string;
1.146     matthew   324:         if ($ENV{'form.phase'} eq 'basic_search') {
1.180     matthew   325:             ($query,$pretty_string,$libraries) = 
1.196     matthew   326:                 &parse_basic_search($r,$closebutton,$hidden_fields);
1.146     matthew   327:         } else {                      # Advanced search
1.143     matthew   328:             ($query,$customquery,$customshow,$libraries,$pretty_string) 
1.196     matthew   329:                 = &parse_advanced_search($r,$closebutton,$hidden_fields);
1.134     matthew   330:             return OK if (! defined($query));
                    331:         }
1.152     matthew   332:         &make_persistent({ query => $query,
1.146     matthew   333:                            customquery => $customquery,
                    334:                            customshow => $customshow,
                    335:                            libraries => $libraries,
                    336:                            pretty_string => $pretty_string },
                    337:                          $persistent_db_file);
1.145     matthew   338:         ##
1.146     matthew   339:         ## Print out the frames interface
1.145     matthew   340:         ##
1.146     matthew   341:         &print_frames_interface($r);
1.124     matthew   342:     }
                    343:     return OK;
                    344: } 
1.98      harris41  345: 
1.124     matthew   346: ######################################################################
                    347: ######################################################################
1.196     matthew   348: ##
                    349: ##   Course Search
                    350: ##
                    351: ######################################################################
                    352: ######################################################################
                    353: {   # Scope the course search to avoid global variables
                    354: #
                    355: # Variables For course search
                    356: my %alreadyseen;
                    357: my %hash;
                    358: my $totalfound;
1.124     matthew   359: 
1.167     www       360: sub course_search {
                    361:     my $r=shift;
                    362:     my $bodytag=&Apache::loncommon::bodytag('Course Search');
                    363:     my $pretty_search_string = '<b>'.$ENV{'form.courseexp'}.'</b>';
                    364:     my $search_string = $ENV{'form.courseexp'};
                    365:     my @New_Words;
                    366:     if ($ENV{'form.crsrelated'}) {
                    367:         ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'});
                    368:         if (@New_Words) {
1.200     www       369:             $pretty_search_string .= ' '.&mt("with related words").": <b>@New_Words</b>.";
1.167     www       370:         } else {
1.200     www       371:             $pretty_search_string .= ' '.&mt('with no related words').".";
1.167     www       372:         }
                    373:     }
                    374:     my $fulltext=$ENV{'form.crsfulltext'};
                    375:     my @allwords=($search_string,@New_Words);
1.169     www       376:     $totalfound=0;
1.167     www       377:     $r->print('<html><head><title>LON-CAPA Course Search</title></head>'.
                    378: 	      $bodytag.$pretty_search_string);
                    379:     $r->rflush();
                    380: # ======================================================= Go through the course
1.196     matthew   381:     undef %alreadyseen;
                    382:     %alreadyseen=();
1.169     www       383:     my $c=$r->connection;
1.196     matthew   384:     if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
                    385:             &GDBM_READER(),0640)) {
                    386:         foreach (keys %hash) {
                    387:             if ($c->aborted()) { last; }
                    388:             if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) {
                    389:                 &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,
                    390:                              @allwords);
                    391:             }
                    392:         }
                    393:         untie(%hash);
                    394:     }
1.169     www       395:     unless ($totalfound) {
1.187     www       396: 	$r->print('<p>'.&mt('No resources found').'.</p>');
1.169     www       397:     }
1.167     www       398: # =================================================== Done going through course
                    399:     $r->print('</body></html>');
                    400: }
                    401: 
                    402: # =============================== This pulls up a resource and its dependencies
                    403: 
                    404: sub checkonthis {
1.168     www       405:     my ($r,$url,$level,$title,$fulltext,@allwords)=@_;
1.167     www       406:     $alreadyseen{$url}=1;
                    407:     $r->rflush();
                    408:     my $result=&Apache::lonnet::metadata($url,'title').' '.
                    409:                &Apache::lonnet::metadata($url,'subject').' '.
                    410:                &Apache::lonnet::metadata($url,'abstract').' '.
                    411:                &Apache::lonnet::metadata($url,'keywords');
                    412:     if (($url) && ($fulltext)) {
1.168     www       413: 	$result.=&Apache::lonnet::ssi_body($url);
1.167     www       414:     }
                    415:     $result=~s/\s+/ /gs;
                    416:     my $applies=0;
                    417:     foreach (@allwords) {
                    418:         if ($_=~/\w/) {
                    419: 	   if ($result=~/$_/si) {
                    420: 	      $applies++;
                    421:            }
                    422:        }
                    423:     }
                    424: # Does this resource apply?
                    425:     if ($applies) {
                    426:        $r->print('<br />');
                    427:        for (my $i=0;$i<=$level*5;$i++) {
                    428:            $r->print('&nbsp;');
                    429:        }
                    430:        $r->print('<a href="'.$url.'" target="cat">'.
1.169     www       431: 		 ($title?$title:$url).'</a><br />');
                    432:        $totalfound++;
                    433:     } elsif ($fulltext) {
                    434:        $r->print(' .');
1.167     www       435:     }
1.169     www       436:     $r->rflush();
1.167     www       437: # Check also the dependencies of this one
                    438:     my $dependencies=
                    439:                 &Apache::lonnet::metadata($url,'dependencies');
                    440:     foreach (split(/\,/,$dependencies)) {
                    441:        if (($_=~/^\/res\//) && (!$alreadyseen{$_})) {
1.168     www       442:           &checkonthis($r,$_,$level+1,'',$fulltext,@allwords);
1.167     www       443:        }
                    444:     }
                    445: }
                    446: 
1.196     matthew   447: sub untiehash {
                    448:     if (tied(%hash)) {
                    449:         untie(%hash);
                    450:     }
                    451: }
                    452: 
                    453: } # End of course search scoping
                    454: 
1.167     www       455: ######################################################################
                    456: ######################################################################
                    457: 
1.124     matthew   458: =pod 
                    459: 
1.146     matthew   460: =item &print_basic_search_form() 
1.124     matthew   461: 
                    462: Returns a scalar which holds html for the basic search form.
                    463: 
                    464: =cut
                    465: 
                    466: ######################################################################
                    467: ######################################################################
1.3       harris41  468: 
1.146     matthew   469: sub print_basic_search_form{
1.196     matthew   470:     my ($r,$closebutton,$hidden_fields) = @_;
1.167     www       471:     my $bodytag=&Apache::loncommon::bodytag('Search');
1.124     matthew   472:     my $scrout=<<"ENDDOCUMENT";
                    473: <html>
                    474: <head>
                    475: <title>The LearningOnline Network with CAPA</title>
                    476: <script type="text/javascript">
                    477:     function openhelp(val) {
                    478: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    479: 	     'scrollbars=1,width=600,height=300');
                    480: 	openhelpwin.focus();
1.6       harris41  481:     }
1.124     matthew   482: </script>
                    483: </head>
1.154     www       484: $bodytag
1.167     www       485: ENDDOCUMENT
                    486: if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) {
1.193     sakharuk  487:     my $CatalogSearch=&mt('Catalog Search');
1.201     www       488:     my $Statement=&searchhelp();
1.167     www       489:     $scrout.=(<<ENDDOCUMENT);
1.193     sakharuk  490: <h1>$CatalogSearch</h1>
1.195     matthew   491: <form name="loncapa_search" method="post" action="/adm/searchcat">
1.146     matthew   492: <input type="hidden" name="phase" value="basic_search" />
1.145     matthew   493: $hidden_fields
1.124     matthew   494: <p>
1.193     sakharuk  495: $Statement.
1.124     matthew   496: </p>
                    497: <p>
                    498: <table>
                    499: <tr><td>
                    500: ENDDOCUMENT
1.200     www       501:     $scrout.='&nbsp;'.&Apache::lonhtmlcommon::textbox('basicexp',
                    502:                                             $ENV{'form.basicexp'},40).
1.124     matthew   503:         '&nbsp;';
1.200     www       504:     my $relatedcheckbox = &Apache::lonhtmlcommon::checkbox('related',
                    505: 						 $ENV{'form.related'});
1.180     matthew   506:     my $domain = $r->dir_config('lonDefDomain');
1.200     www       507:     my $domaincheckbox = &Apache::lonhtmlcommon::checkbox('domains',
                    508: 						$ENV{'form.domains'});
1.187     www       509:     my $srch=&mt('Search');
                    510:     my $header=&mt('Advanced Search');
1.193     sakharuk  511:     my $userelatedwords=&mt('use related words');
                    512:     my $onlysearchdomain=&mt('only search domain');
1.200     www       513:     my $view=&viewoptions();
1.139     matthew   514:     $scrout.=<<END;
1.160     www       515: </td><td><a
1.190     matthew   516: href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}&mode=$ENV{'form.mode'}"
1.187     www       517: >$header</a></td></tr>
1.193     sakharuk  518: <tr><td>$relatedcheckbox $userelatedwords</td>
                    519:     <td>$domaincheckbox $onlysearchdomain <b>$domain</b></td></tr>
1.141     matthew   520: </table>
1.124     matthew   521: </p>
1.200     www       522: $view
1.124     matthew   523: <p>
1.187     www       524: &nbsp;<input type="submit" name="basicsubmit" value='$srch' />&nbsp;
1.124     matthew   525: $closebutton
1.139     matthew   526: END
                    527:     $scrout.=<<ENDDOCUMENT;
1.124     matthew   528: </p>
                    529: </form>
1.167     www       530: ENDDOCUMENT
1.180     matthew   531:     }
                    532:     if ($ENV{'request.course.id'}) {
1.187     www       533: 	my %lt=&Apache::lonlocal::texthash(
                    534: 					   'srch' => 'Search',
                    535:                                            'header' => 'Course Search',
                    536: 	 'note' => 'Enter terms or phrases, then press "Search" below',
1.200     www       537: 	 'use' => 'use related words',
                    538: 	 'full' =>'fulltext search (time consuming)'
1.187     www       539: 					   );
1.180     matthew   540:         $scrout.=(<<ENDCOURSESEARCH);
                    541: <hr />
1.187     www       542: <h1>$lt{'header'}</h1>    
1.195     matthew   543: <form name="course_search" method="post" action="/adm/searchcat">
1.167     www       544: <input type="hidden" name="phase" value="course_search" />
                    545: $hidden_fields
                    546: <p>
1.187     www       547: $lt{'note'}.
1.167     www       548: </p>
                    549: <p>
                    550: <table>
                    551: <tr><td>
                    552: ENDCOURSESEARCH
1.180     matthew   553:         $scrout.='&nbsp;'.
1.200     www       554:             &Apache::lonhtmlcommon::textbox('courseexp',
                    555:                                   $ENV{'form.courseexp'},40);
1.180     matthew   556:         my $crscheckbox = 
1.200     www       557:             &Apache::lonhtmlcommon::checkbox('crsfulltext',
                    558:                                    $ENV{'form.crsfulltext'});
1.180     matthew   559:         my $relcheckbox = 
1.200     www       560:             &Apache::lonhtmlcommon::checkbox('crsrelated',
                    561: 				   $ENV{'form.crsrelated'});
1.180     matthew   562:         $scrout.=(<<ENDENDCOURSE);
1.167     www       563: </td></tr>
1.200     www       564: <tr><td>$relcheckbox $lt{'use'}</td><td></td></tr>
                    565: <tr><td>$crscheckbox $lt{'full'}</td><td></td></tr>
1.167     www       566: </table><p>
1.187     www       567: &nbsp;<input type="submit" name="coursesubmit" value='$lt{'srch'}' />
1.167     www       568: </p>
                    569: ENDENDCOURSE
1.180     matthew   570:     }
1.167     www       571:     $scrout.=(<<ENDDOCUMENT);
1.124     matthew   572: </body>
                    573: </html>
                    574: ENDDOCUMENT
1.146     matthew   575:     $r->print($scrout);
                    576:     return;
1.124     matthew   577: }
                    578: ######################################################################
                    579: ######################################################################
                    580: 
                    581: =pod 
                    582: 
                    583: =item &advanced_search_form() 
                    584: 
                    585: Returns a scalar which holds html for the advanced search form.
                    586: 
                    587: =cut
                    588: 
                    589: ######################################################################
                    590: ######################################################################
                    591: 
1.146     matthew   592: sub print_advanced_search_form{
1.196     matthew   593:     my ($r,$closebutton,$hidden_fields) = @_;
1.200     www       594:     my %lt=&Apache::lonlocal::texthash('srch' => 'Search',
                    595: 				       'reset' => 'Reset',
                    596: 				       'help' => 'Help');
1.129     matthew   597:     my $advanced_buttons = <<"END";
                    598: <p>
1.200     www       599: <input type="submit" name="advancedsubmit" value='$lt{"srch"}' />
                    600: <input type="reset" name="reset" value='$lt{"reset"}' />
1.129     matthew   601: $closebutton
1.200     www       602: <input type="button" value="$lt{'help'}" onClick="openhelp()" />
1.129     matthew   603: </p>
                    604: END
1.154     www       605:     my $bodytag=&Apache::loncommon::bodytag('Advanced Catalog Search');
1.201     www       606:     my $searchhelp=&searchhelp();
1.124     matthew   607:     my $scrout=<<"ENDHEADER";
                    608: <html>
                    609: <head>
                    610: <title>The LearningOnline Network with CAPA</title>
                    611: <script type="text/javascript">
                    612:     function openhelp(val) {
                    613: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    614: 	     'scrollbars=1,width=600,height=300');
                    615: 	openhelpwin.focus();
1.18      harris41  616:     }
1.124     matthew   617: </script>
                    618: </head>
1.154     www       619: $bodytag
1.201     www       620: $searchhelp
1.200     www       621: <form method="post" action="/adm/searchcat" name="advsearch">
1.129     matthew   622: $advanced_buttons
1.145     matthew   623: $hidden_fields
1.146     matthew   624: <input type="hidden" name="phase" value="adv_search" />
1.124     matthew   625: ENDHEADER
1.200     www       626:     $scrout.=&viewoptions();
                    627:     my %fields=&Apache::lonmeta::fieldnames();
                    628: 
                    629:     $scrout.='<table>';
                    630:     $scrout.="<tr><th>".&mt('Field').'</th><th>'.&mt('Value').'</th><th>'
                    631: 	.&mt('Related').'<br />'.&mt('Words')."</td></tr>\n";
1.202   ! www       632:     foreach ('title','author','owner','authorspace','modifyinguser',
        !           633: 	     'keywords','notes','abstract','standards',
1.200     www       634: 	     'lowestgradelevel','highestgradelevel','mime') {
1.201     www       635: 	$scrout.='<tr bgcolor="#FFFFBB"><td>'.&titlefield($fields{$_}).'</td><td>'.
1.200     www       636: 	    &Apache::lonmeta::prettyinput($_,$ENV{'form.'.$_},$_,'advsearch',
                    637: 					  1,'</td><td>',$ENV{'form.'.$_.'_related'}).
                    638: 	    '</td></tr>';
                    639:     }
1.202   ! www       640:     $scrout.='<tr bgcolor="#FFFFBB"><td>'.
        !           641: 	&titlefield(&mt('MIME Type Category')).'</td><td>'. 
        !           642: 	    &Apache::loncommon::filecategoryselect('category',
        !           643: 						   $ENV{'form.category'}).
        !           644: 	    '</td><td>&nbsp;</td></td></tr>';
        !           645:     $scrout.='<tr bgcolor="#FFFFBB"><td>'.
        !           646: 	&titlefield(&mt('Limit Search to Domains')).'</td><td>'. 
        !           647: 	    &Apache::loncommon::domain_select('domains',
        !           648: 						   $ENV{'form.domains'},1).
        !           649: 	    '</td><td>&nbsp;</td></td></tr>';
1.201     www       650:     my %dates=&Apache::lonlocal::texthash('creationdatestart'     => 'Creation Date After',
                    651: 				  	  'creationdateend'       => 'Creation Date Before',
                    652: 					  'lastrevisiondatestart' => 'Last Revision Date After',
                    653: 					  'lastrevisiondateend'   => 'Last Revision Date Before');
                    654:     foreach (sort keys %dates) {
1.202   ! www       655: 	$scrout.='<tr bgcolor="#FFFFBB"><td>'.&titlefield($dates{$_}).'</td><td>'. 
1.201     www       656: 	    &Apache::lonhtmlcommon::date_setter('advsearch',$_,0,'',1).
                    657: 	    '</td><td>&nbsp;</td></td></tr>';
                    658:     }
                    659: 
1.202   ! www       660:     $scrout.="</table>\n";
1.201     www       661: 
1.124     matthew   662:     $scrout.=<<ENDDOCUMENT;
1.129     matthew   663: $advanced_buttons
1.8       harris41  664: </form>
                    665: </body>
                    666: </html>
                    667: ENDDOCUMENT
1.146     matthew   668:     $r->print($scrout);
                    669:     return;
1.124     matthew   670: }
1.200     www       671: ######################################################################
                    672: ######################################################################
                    673: 
                    674: =pod 
                    675: 
                    676: =item &titlefield
                    677: 
                    678: Inputs: title text
                    679: 
                    680: Outputs: titletext with font wrapper
                    681: 
                    682: =cut
                    683: 
                    684: ######################################################################
                    685: ######################################################################
                    686: 
                    687: sub titlefield {
                    688:     my $title=shift;
                    689:     return '<font face="arial" color="#800000">'.$title.'</font>';
                    690: }
                    691: ######################################################################
                    692: ######################################################################
                    693: 
                    694: =pod 
                    695: 
                    696: =item viewoptiontext
                    697: 
                    698: Inputs: codename for view option
                    699: 
                    700: Outputs: displayed text
                    701: 
                    702: =cut
                    703: 
                    704: ######################################################################
                    705: ######################################################################
                    706: 
                    707: sub viewoptiontext {
                    708:     my $code=shift;
                    709:     my %desc=&Apache::lonlocal::texthash('detailed' => "Detailed Citation View",
                    710: 					 'xml' => 'XML/SGML',
                    711: 					 'compact' => 'Compact View',
                    712: 					 'fielded' => 'Fielded Format',
                    713: 					 'summary' => 'Summary View');
                    714:     return $desc{$code};
                    715: }
                    716: ######################################################################
                    717: 
                    718: =pod 
                    719: 
                    720: =item viewoptions
                    721: 
                    722: Inputs: none
                    723: 
                    724: Outputs: text for box with view options
                    725: 
                    726: =cut
                    727: 
                    728: ######################################################################
                    729: ######################################################################
                    730: 
                    731: sub viewoptions {
                    732:     my $scrout="\n\n".'<table bgcolor="#FFFFBB"><tr><th>'.&mt('View Options').'</th><th>'.
                    733: 	&mt('Records per Page').'</th></tr><tr><td>';
                    734:     unless ($ENV{'form.viewselect'}) { $ENV{'form.viewselect'}='detailed'; }
                    735:     $scrout.=&Apache::lonmeta::selectbox('viewselect',
                    736: 			$ENV{'form.viewselect'},
                    737: 			\&viewoptiontext,
                    738: 			sort(keys(%Views)));
                    739:     $scrout.='</td><td>';
                    740:     $scrout.=&Apache::lonmeta::selectbox('show',
                    741: 			$ENV{'form.show'},
                    742: 			undef,
                    743: 			(10,20,50,100,1000,10000));
                    744:     $scrout.="</td></tr></table>\n\n";
                    745:     return $scrout;
1.201     www       746: }
                    747: 
                    748: ######################################################################
                    749: 
                    750: =pod 
                    751: 
                    752: =item searchhelp
                    753: 
                    754: Inputs: none
                    755: 
                    756: Outputs: return little blurb on how to enter searches
                    757: 
                    758: =cut
                    759: 
                    760: ######################################################################
                    761: ######################################################################
                    762: 
                    763: sub searchhelp {
                    764:     return &mt('Enter terms or phrases separated by AND, OR, or NOT');
1.200     www       765: }
1.8       harris41  766: 
1.121     matthew   767: ######################################################################
                    768: ######################################################################
                    769: 
                    770: =pod 
                    771: 
1.146     matthew   772: =item &get_persistent_form_data
1.145     matthew   773: 
1.146     matthew   774: Inputs: filename of database
                    775: 
                    776: Outputs: returns undef on database errors.
                    777: 
                    778: This function is the reverse of &make_persistent() for form data.
1.145     matthew   779: Retrieve persistent data from %persistent_db.  Retrieved items will have their
1.146     matthew   780: values unescaped.  If a form value already exists in $ENV, it will not be
                    781: overwritten.  Form values that are array references may have values appended
                    782: to them.
1.145     matthew   783: 
                    784: =cut
                    785: 
                    786: ######################################################################
                    787: ######################################################################
1.146     matthew   788: sub get_persistent_form_data {
                    789:     my $filename = shift;
1.147     matthew   790:     return 0 if (! -e $filename);
1.146     matthew   791:     return undef if (! tie(%persistent_db,'GDBM_File',$filename,
1.148     matthew   792:                            &GDBM_READER(),0640));
1.146     matthew   793:     #
                    794:     # These make sure we do not get array references printed out as 'values'.
1.161     matthew   795:     my %arrays_allowed = ('form.domains'=>1);
1.146     matthew   796:     #
                    797:     # Loop through the keys, looking for 'form.'
                    798:     foreach my $name (keys(%persistent_db)) {
                    799:         next if ($name !~ /^form./);
1.182     matthew   800:         # Kludgification begins!
                    801:         if ($name eq 'form.domains' && 
                    802:             $ENV{'form.searchmode'} eq 'basic' &&
                    803:             $ENV{'form.phase'} ne 'disp_basic') {
                    804:             next;
                    805:         }
                    806:         # End kludge (hopefully)
1.152     matthew   807:         next if (exists($ENV{$name}));
1.146     matthew   808:         my @values = map { 
                    809:             &Apache::lonnet::unescape($_);
                    810:         } split(',',$persistent_db{$name});
                    811:         next if (@values <1);
1.152     matthew   812:         if ($arrays_allowed{$name}) {
                    813:             $ENV{$name} = [@values];
1.146     matthew   814:         } else {
1.152     matthew   815:             $ENV{$name} = $values[0] if ($values[0]);
1.146     matthew   816:         }
                    817:     }
                    818:     untie (%persistent_db);
                    819:     return 1;
                    820: }
1.181     matthew   821: 
1.146     matthew   822: ######################################################################
                    823: ######################################################################
                    824: 
                    825: =pod 
                    826: 
                    827: =item &get_persistent_data
                    828: 
                    829: Inputs: filename of database, ref to array of values to recover.
                    830: 
                    831: Outputs: array of values.  Returns undef on error.
                    832: 
                    833: This function is the reverse of &make_persistent();
                    834: Retrieve persistent data from %persistent_db.  Retrieved items will have their
                    835: values unescaped.  If the item contains commas (before unescaping), the
                    836: returned value will be an array pointer. 
                    837: 
                    838: =cut
                    839: 
                    840: ######################################################################
                    841: ######################################################################
                    842: sub get_persistent_data {
                    843:     my $filename = shift;
                    844:     my @Vars = @{shift()};
                    845:     my @Values;   # Return array
                    846:     return undef if (! -e $filename);
                    847:     return undef if (! tie(%persistent_db,'GDBM_File',$filename,
1.148     matthew   848:                            &GDBM_READER(),0640));
1.146     matthew   849:     foreach my $name (@Vars) {
                    850:         if (! exists($persistent_db{$name})) {
                    851:             push @Values, undef;
                    852:             next;
                    853:         }
                    854:         my @values = map { 
                    855:             &Apache::lonnet::unescape($_);
                    856:         } split(',',$persistent_db{$name});
1.152     matthew   857:         if (@values <= 1) {
1.146     matthew   858:             push @Values,$values[0];
1.145     matthew   859:         } else {
1.146     matthew   860:             push @Values,\@values;
1.145     matthew   861:         }
                    862:     }
1.146     matthew   863:     untie (%persistent_db);
                    864:     return @Values;
1.145     matthew   865: }
                    866: 
                    867: ######################################################################
                    868: ######################################################################
                    869: 
                    870: =pod 
                    871: 
1.121     matthew   872: =item &make_persistent() 
                    873: 
1.146     matthew   874: Inputs: Hash of values to save, filename of persistent database.
                    875: 
                    876: Store variables away to the %persistent_db.
1.145     matthew   877: Values will be escaped.  Values that are array pointers will have their
                    878: elements escaped and concatenated in a comma seperated string.  
1.122     matthew   879: 
1.121     matthew   880: =cut
                    881: 
                    882: ######################################################################
                    883: ######################################################################
1.98      harris41  884: sub make_persistent {
1.133     matthew   885:     my %save = %{shift()};
1.146     matthew   886:     my $filename = shift;
                    887:     return undef if (! tie(%persistent_db,'GDBM_File',
1.148     matthew   888:                            $filename,&GDBM_WRCREAT(),0640));
1.146     matthew   889:     foreach my $name (keys(%save)) {
1.145     matthew   890:         my @values = (ref($save{$name}) ? @{$save{$name}} : ($save{$name}));
                    891:         # We handle array references, but not recursively.
                    892:         my $store = join(',', map { &Apache::lonnet::escape($_); } @values );
                    893:         $persistent_db{$name} = $store;
1.109     harris41  894:     }
1.146     matthew   895:     untie(%persistent_db);
                    896:     return 1;
                    897: }
                    898: 
                    899: ######################################################################
                    900: ######################################################################
                    901: 
                    902: =pod 
                    903: 
                    904: =item &make_form_data_persistent() 
                    905: 
                    906: Inputs: filename of persistent database.
                    907: 
                    908: Store most form variables away to the %persistent_db.
                    909: Values will be escaped.  Values that are array pointers will have their
                    910: elements escaped and concatenated in a comma seperated string.  
                    911: 
                    912: =cut
                    913: 
                    914: ######################################################################
                    915: ######################################################################
                    916: sub make_form_data_persistent {
                    917:     my $r = shift;
                    918:     my $filename = shift;
                    919:     my %save;
                    920:     foreach (keys(%ENV)) {
1.150     matthew   921:         next if (!/^form/ || /submit/);
1.146     matthew   922:         $save{$_} = $ENV{$_};
                    923:     }
1.152     matthew   924:     return &make_persistent(\%save,$filename);
1.98      harris41  925: }
                    926: 
1.122     matthew   927: ######################################################################
                    928: ######################################################################
                    929: 
                    930: =pod 
                    931: 
1.134     matthew   932: =item &parse_advanced_search()
                    933: 
                    934: Parse advanced search form and return the following:
                    935: 
                    936: =over 4
                    937: 
                    938: =item $query Scalar containing an SQL query.
1.126     matthew   939: 
1.134     matthew   940: =item $customquery Scalar containing a custom query.
                    941: 
                    942: =item $customshow Scalar containing commands to show custom metadata.
                    943: 
                    944: =item $libraries_to_query Reference to array of domains to search.
                    945: 
                    946: =back
1.122     matthew   947: 
                    948: =cut
                    949: 
                    950: ######################################################################
                    951: ######################################################################
1.134     matthew   952: sub parse_advanced_search {
1.196     matthew   953:     my ($r,$closebutton,$hidden_fields)=@_;
1.32      harris41  954:     my $fillflag=0;
1.143     matthew   955:     my $pretty_search_string = "<br />\n";
1.64      harris41  956:     # Clean up fields for safety
                    957:     for my $field ('title','author','subject','keywords','url','version',
                    958: 		   'creationdatestart_month','creationdatestart_day',
                    959: 		   'creationdatestart_year','creationdateend_month',
                    960: 		   'creationdateend_day','creationdateend_year',
                    961: 		   'lastrevisiondatestart_month','lastrevisiondatestart_day',
                    962: 		   'lastrevisiondatestart_year','lastrevisiondateend_month',
                    963: 		   'lastrevisiondateend_day','lastrevisiondateend_year',
1.161     matthew   964: 		   'notes','abstract','extension','language','owner',
1.131     matthew   965: 		   'custommetadata','customshow','category') {
1.101     harris41  966: 	$ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
1.64      harris41  967:     }
1.117     matthew   968:     foreach ('mode','form','element') {
                    969: 	# is this required?  Hmmm.
                    970: 	next unless (exists($ENV{"form.$_"}));
                    971: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                    972: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                    973:     }
1.131     matthew   974:     # Preprocess the category form element.
1.161     matthew   975:     $ENV{'form.category'} = 'any' if (! defined($ENV{'form.category'}) ||
                    976:                                       ref($ENV{'form.category'}));
                    977:     #
1.90      harris41  978:     # Check to see if enough information was filled in
1.32      harris41  979:     for my $field ('title','author','subject','keywords','url','version',
1.161     matthew   980: 		   'notes','abstract','category','extension','language',
                    981:                    'owner','custommetadata') {
1.40      harris41  982: 	if (&filled($ENV{"form.$field"})) {
1.32      harris41  983: 	    $fillflag++;
                    984: 	}
                    985:     }
                    986:     unless ($fillflag) {
1.196     matthew   987: 	&output_blank_field_error($r,$closebutton,'phase=disp_adv',$hidden_fields);
1.134     matthew   988: 	return ;
1.32      harris41  989:     }
1.90      harris41  990:     # Turn the form input into a SQL-based query
1.39      harris41  991:     my $query='';
1.45      harris41  992:     my @queries;
1.143     matthew   993:     my $font = '<font color="#800000" face="helvetica">';
1.90      harris41  994:     # Evaluate logical expression AND/OR/NOT phrase fields.
1.58      harris41  995:     foreach my $field ('title','author','subject','notes','abstract','url',
1.200     www       996: 		       'keywords','version','owner','standards') {
1.44      harris41  997: 	if ($ENV{'form.'.$field}) {
1.142     matthew   998:             my $searchphrase = $ENV{'form.'.$field};
1.143     matthew   999:             $pretty_search_string .= $font."$field</font> contains <b>".
                   1000:                 $searchphrase."</b>";
1.142     matthew  1001:             if ($ENV{'form.'.$field.'_related'}) {
1.143     matthew  1002:                 my @New_Words;
                   1003:                 ($searchphrase,@New_Words) = &related_version($searchphrase);
                   1004:                 if (@New_Words) {
                   1005:                     $pretty_search_string .= " with related words: ".
                   1006:                         "<b>@New_Words</b>.";
                   1007:                 } else {
                   1008:                     $pretty_search_string .= " with no related words.";
                   1009:                 }
1.142     matthew  1010:             }
1.143     matthew  1011:             $pretty_search_string .= "<br />\n";
1.142     matthew  1012: 	    push @queries,&build_SQL_query($field,$searchphrase);
1.131     matthew  1013:         }
1.44      harris41 1014:     }
1.161     matthew  1015:     #
                   1016:     # Make the 'mime' from 'form.category' and 'form.extension'
                   1017:     #
                   1018:     my $searchphrase;
                   1019:     if (exists($ENV{'form.category'})    && 
                   1020:         $ENV{'form.category'} !~ /^\s*$/ &&
                   1021:         $ENV{'form.category'} ne 'any')     {
                   1022:         my @extensions = &Apache::loncommon::filecategorytypes
                   1023:                                                    ($ENV{'form.category'});
                   1024:         if (scalar(@extensions) > 0) {
                   1025:             $searchphrase = join(' OR ',@extensions);
                   1026:         }
                   1027:     }
                   1028:     if (exists($ENV{'form.extension'}) && $ENV{'form.extension'} !~ /^\s*$/) {
                   1029:         $searchphrase .= ' OR ' if (defined($searchphrase));
                   1030:         my @extensions = split(/,/,$ENV{'form.extension'});
                   1031:         $searchphrase .= join(' OR ',@extensions);
                   1032:     }
                   1033:     if (defined($searchphrase)) {
                   1034:         push @queries,&build_SQL_query('mime',$searchphrase);
                   1035:         $pretty_search_string .=$font.'mime</font> contains <b>'.
                   1036:             $searchphrase.'</b><br />';
1.135     matthew  1037:     }
1.161     matthew  1038:     #####
1.90      harris41 1039:     # Evaluate option lists
1.58      harris41 1040:     if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
1.90      harris41 1041: 	push @queries,"(language like \"$ENV{'form.language'}\")";
1.143     matthew  1042:         $pretty_search_string.=$font."language</font>= ".
                   1043:             &Apache::loncommon::languagedescription($ENV{'form.language'}).
                   1044:                 "<br />\n";
1.58      harris41 1045:     }
                   1046:     if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
1.90      harris41 1047: 	push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
1.143     matthew  1048:         $pretty_search_string.=$font."copyright</font> = ".
                   1049:             &Apache::loncommon::copyrightdescription($ENV{'form.copyright'}).
                   1050:                 "<br \>\n";
1.58      harris41 1051:     }
1.143     matthew  1052:     #
1.90      harris41 1053:     # Evaluate date windows
1.60      harris41 1054:     my $datequery=&build_date_queries(
                   1055: 			$ENV{'form.creationdatestart_month'},
                   1056: 			$ENV{'form.creationdatestart_day'},
                   1057: 			$ENV{'form.creationdatestart_year'},
                   1058: 			$ENV{'form.creationdateend_month'},
                   1059: 			$ENV{'form.creationdateend_day'},
                   1060: 			$ENV{'form.creationdateend_year'},
                   1061: 			$ENV{'form.lastrevisiondatestart_month'},
                   1062: 			$ENV{'form.lastrevisiondatestart_day'},
                   1063: 			$ENV{'form.lastrevisiondatestart_year'},
                   1064: 			$ENV{'form.lastrevisiondateend_month'},
                   1065: 			$ENV{'form.lastrevisiondateend_day'},
                   1066: 			$ENV{'form.lastrevisiondateend_year'},
                   1067: 			);
1.90      harris41 1068:     # Test to see if date windows are legitimate
1.61      harris41 1069:     if ($datequery=~/^Incorrect/) {
1.196     matthew  1070: 	&output_date_error($r,$datequery,$closebutton,$hidden_fields);
1.134     matthew  1071: 	return ;
1.143     matthew  1072:     } elsif ($datequery) {
                   1073:         # Here is where you would set up pretty_search_string to output
                   1074:         # date query information.
1.60      harris41 1075: 	push @queries,$datequery;
                   1076:     }
1.90      harris41 1077:     # Process form information for custom metadata querying
1.134     matthew  1078:     my $customquery=undef;
1.152     matthew  1079: #    if ($ENV{'form.custommetadata'}) {
                   1080: #        $pretty_search_string .=$font."Custom Metadata Search</font>: <b>".
                   1081: #            $ENV{'form.custommetadata'}."</b><br />\n";
                   1082: #	$customquery=&build_custommetadata_query('custommetadata',
                   1083: #				      $ENV{'form.custommetadata'});
                   1084: #    }
1.134     matthew  1085:     my $customshow=undef;
1.152     matthew  1086: #    if ($ENV{'form.customshow'}) {
                   1087: #        $pretty_search_string .=$font."Custom Metadata Display</font>: <b>".
                   1088: #            $ENV{'form.customshow'}."</b><br />\n";
                   1089: #	$customshow=$ENV{'form.customshow'};
                   1090: #	$customshow=~s/[^\w\s]//g;
                   1091: #	my @fields=split(/\s+/,$customshow);
                   1092: #	$customshow=join(" ",@fields);
                   1093: #    }
1.133     matthew  1094:     ## ---------------------------------------------------------------
1.132     matthew  1095:     ## Deal with restrictions to given domains
                   1096:     ## 
1.180     matthew  1097:     my ($libraries_to_query,$pretty_domains_string) = 
                   1098:         &parse_domain_restrictions();
                   1099:     $pretty_search_string .= $pretty_domains_string."<br />\n";
                   1100:     #
                   1101:     if (@queries) {
                   1102: 	$query=join(" AND ",@queries);
                   1103: 	$query="select * from metadata where $query";
                   1104:     } elsif ($customquery) {
                   1105:         $query = '';
                   1106:     }
                   1107:     return ($query,$customquery,$customshow,$libraries_to_query,
                   1108:             $pretty_search_string);
                   1109: }
                   1110: 
                   1111: sub parse_domain_restrictions {
1.132     matthew  1112:     my $libraries_to_query = undef;
                   1113:     # $ENV{'form.domains'} can be either a scalar or an array reference.
                   1114:     # We need an array.
1.180     matthew  1115:     if (! exists($ENV{'form.domains'})) {
                   1116:         return (undef,'');
                   1117:     }
                   1118:     my @allowed_domains;
                   1119:     if (ref($ENV{'form.domains'})) {
                   1120:         @allowed_domains =  @{$ENV{'form.domains'}};
                   1121:     } else {
                   1122:         @allowed_domains = ($ENV{'form.domains'});
                   1123:     }
1.132     matthew  1124:     my %domain_hash = ();
1.143     matthew  1125:     my $pretty_domains_string;
1.132     matthew  1126:     foreach (@allowed_domains) {
                   1127:         $domain_hash{$_}++;
                   1128:     }
1.143     matthew  1129:     if ($domain_hash{'any'}) {
1.152     matthew  1130:         $pretty_domains_string = "In all LON-CAPA domains.";
1.143     matthew  1131:     } else {
                   1132:         if (@allowed_domains > 1) {
1.152     matthew  1133:             $pretty_domains_string = "In LON-CAPA domains:";
1.143     matthew  1134:         } else {
1.152     matthew  1135:             $pretty_domains_string = "In LON-CAPA domain ";
1.143     matthew  1136:         }
                   1137:         foreach (sort @allowed_domains) {
1.152     matthew  1138:             $pretty_domains_string .= "<b>".$_."</b> ";
1.132     matthew  1139:         }
1.143     matthew  1140:         foreach (keys(%Apache::lonnet::libserv)) {
                   1141:             if (exists($domain_hash{$Apache::lonnet::hostdom{$_}})) {
                   1142:                 push @$libraries_to_query,$_;
                   1143:             }
1.132     matthew  1144:         }
                   1145:     }
1.180     matthew  1146:     return ($libraries_to_query,$pretty_domains_string);
1.18      harris41 1147: }
                   1148: 
1.122     matthew  1149: ######################################################################
                   1150: ######################################################################
                   1151: 
                   1152: =pod 
                   1153: 
1.134     matthew  1154: =item &parse_basic_search() 
1.122     matthew  1155: 
1.134     matthew  1156: Parse the basic search form and return a scalar containing an sql query.
1.126     matthew  1157: 
1.122     matthew  1158: =cut
                   1159: 
                   1160: ######################################################################
                   1161: ######################################################################
1.134     matthew  1162: sub parse_basic_search {
1.145     matthew  1163:     my ($r,$closebutton)=@_;
1.64      harris41 1164:     # Clean up fields for safety
                   1165:     for my $field ('basicexp') {
                   1166: 	$ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
                   1167:     }
1.117     matthew  1168:     foreach ('mode','form','element') {
                   1169: 	# is this required?  Hmmm.
                   1170: 	next unless (exists($ENV{"form.$_"}));
                   1171: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                   1172: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                   1173:     }
1.180     matthew  1174:     my ($libraries_to_query,$pretty_domains_string) = 
                   1175:         &parse_domain_restrictions();
1.90      harris41 1176:     # Check to see if enough is filled in
1.26      harris41 1177:     unless (&filled($ENV{'form.basicexp'})) {
1.151     matthew  1178: 	&output_blank_field_error($r,$closebutton,'phase=disp_basic');
1.24      harris41 1179: 	return OK;
                   1180:     }
1.143     matthew  1181:     my $pretty_search_string = '<b>'.$ENV{'form.basicexp'}.'</b>';
1.142     matthew  1182:     my $search_string = $ENV{'form.basicexp'};
1.141     matthew  1183:     if ($ENV{'form.related'}) {
1.143     matthew  1184:         my @New_Words;
                   1185:         ($search_string,@New_Words) = &related_version($ENV{'form.basicexp'});
                   1186:         if (@New_Words) {
                   1187:             $pretty_search_string .= " with related words: <b>@New_Words</b>.";
                   1188:         } else {
                   1189:             $pretty_search_string .= " with no related words.";
                   1190:         }
1.141     matthew  1191:     }
1.90      harris41 1192:     # Build SQL query string based on form page
1.39      harris41 1193:     my $query='';
1.178     matthew  1194:     my $concatarg=join(',',
1.124     matthew  1195: 		       ('title', 'author', 'subject', 'notes', 'abstract',
                   1196:                         'keywords'));
1.95      harris41 1197:     $concatarg='title' if $ENV{'form.titleonly'};
1.178     matthew  1198:     $query=&build_SQL_query('concat_ws(" ",'.$concatarg.')',$search_string);
1.180     matthew  1199:     if (defined($pretty_domains_string) && $pretty_domains_string ne '') {
                   1200:         $pretty_search_string .= ' '.$pretty_domains_string;
                   1201:     }
1.143     matthew  1202:     $pretty_search_string .= "<br />\n";
1.183     matthew  1203:     my $final_query = 'SELECT * FROM metadata WHERE '.$query;
                   1204: #    &Apache::lonnet::logthis($final_query);
                   1205:     return ($final_query,$pretty_search_string,
1.180     matthew  1206:             $libraries_to_query);
1.22      harris41 1207: }
                   1208: 
1.122     matthew  1209: 
                   1210: ######################################################################
                   1211: ######################################################################
                   1212: 
                   1213: =pod 
                   1214: 
1.142     matthew  1215: =item &related_version
                   1216: 
                   1217: Modifies an input string to include related words.  Words in the string
                   1218: are replaced with parenthesized lists of 'OR'd words.  For example
                   1219: "torque" is replaced with "(torque OR word1 OR word2 OR ...)".  
                   1220: 
                   1221: Note: Using this twice on a string is probably silly.
                   1222: 
                   1223: =cut
                   1224: 
                   1225: ######################################################################
                   1226: ######################################################################
                   1227: sub related_version {
                   1228:     my $search_string = shift;
                   1229:     my $result = $search_string;
1.143     matthew  1230:     my %New_Words = ();
1.142     matthew  1231:     while ($search_string =~ /(\w+)/cg) {
                   1232:         my $word = $1;
                   1233:         next if (lc($word) =~ /\b(or|and|not)\b/);
                   1234:         my @Words = &Apache::loncommon::get_related_words($word);
1.143     matthew  1235:         @Words = ($#Words>4? @Words[0..4] : @Words);
                   1236:         foreach (@Words) { $New_Words{$_}++;}
                   1237:         my $replacement = join " OR ", ($word,@Words);
1.142     matthew  1238:         $result =~ s/(\b)$word(\b)/$1($replacement)$2/g;
                   1239:     }
1.143     matthew  1240:     return $result,sort(keys(%New_Words));
1.142     matthew  1241: }
                   1242: 
                   1243: ######################################################################
                   1244: ######################################################################
                   1245: 
                   1246: =pod 
                   1247: 
1.122     matthew  1248: =item &build_SQL_query() 
                   1249: 
1.126     matthew  1250: Builds a SQL query string from a logical expression with AND/OR keywords
                   1251: using Text::Query and &recursive_SQL_query_builder()
                   1252: 
1.122     matthew  1253: =cut
                   1254: 
                   1255: ######################################################################
                   1256: ######################################################################
1.98      harris41 1257: sub build_SQL_query {
                   1258:     my ($field_name,$logic_statement)=@_;
                   1259:     my $q=new Text::Query('abc',
                   1260: 			  -parse => 'Text::Query::ParseAdvanced',
                   1261: 			  -build => 'Text::Query::Build');
                   1262:     $q->prepare($logic_statement);
                   1263:     my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
                   1264:     my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
                   1265:     return $sql_query;
                   1266: }
                   1267: 
1.122     matthew  1268: ######################################################################
                   1269: ######################################################################
                   1270: 
                   1271: =pod 
                   1272: 
                   1273: =item &build_custommetadata_query() 
                   1274: 
1.126     matthew  1275: Constructs a custom metadata query using a rather heinous regular
                   1276: expression.
                   1277: 
1.122     matthew  1278: =cut
                   1279: 
                   1280: ######################################################################
                   1281: ######################################################################
1.98      harris41 1282: sub build_custommetadata_query {
                   1283:     my ($field_name,$logic_statement)=@_;
                   1284:     my $q=new Text::Query('abc',
                   1285: 			  -parse => 'Text::Query::ParseAdvanced',
                   1286: 			  -build => 'Text::Query::BuildAdvancedString');
                   1287:     $q->prepare($logic_statement);
                   1288:     my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
                   1289:     # quick fix to change literal into xml tag-matching
                   1290:     # will eventually have to write a separate builder module
1.122     matthew  1291:     # wordone=wordtwo becomes\<wordone\>[^\<] *wordtwo[^\<]*\<\/wordone\>
                   1292:     $matchexp =~ s/(\w+)\\=([\w\\\+]+)?# wordone=wordtwo is changed to 
                   1293:                  /\\<$1\\>?#           \<wordone\>
                   1294:                    \[\^\\<\]?#        [^\<]         
                   1295:                    \*$2\[\^\\<\]?#           *wordtwo[^\<]
                   1296:                    \*\\<\\\/$1\\>?#                        *\<\/wordone\>
                   1297:                    /g;
1.98      harris41 1298:     return $matchexp;
                   1299: }
                   1300: 
1.122     matthew  1301: ######################################################################
                   1302: ######################################################################
                   1303: 
                   1304: =pod 
                   1305: 
                   1306: =item &recursive_SQL_query_build() 
                   1307: 
1.126     matthew  1308: Recursively constructs an SQL query.  Takes as input $dkey and $pattern.
                   1309: 
1.122     matthew  1310: =cut
                   1311: 
                   1312: ######################################################################
                   1313: ######################################################################
1.98      harris41 1314: sub recursive_SQL_query_build {
                   1315:     my ($dkey,$pattern)=@_;
                   1316:     my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
                   1317:     return $pattern unless @matches;
                   1318:     foreach my $match (@matches) {
1.173     matthew  1319:         $match=~/\[ (\w+)\s(.*) \]/;
                   1320:         my ($key,$value)=($1,$2);
                   1321:         my $replacement='';
                   1322:         if ($key eq 'literal') {
1.178     matthew  1323:             $replacement="($dkey LIKE \"\%$value\%\")";
                   1324:         } elsif (lc($key) eq 'not') {
                   1325:             $value=~s/LIKE/NOT LIKE/;
1.173     matthew  1326: #          $replacement="($dkey not like $value)";
                   1327:             $replacement="$value";
                   1328:         } elsif ($key eq 'and') {
                   1329:             $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1330:             $replacement="($1 AND $2)";
                   1331: 	} elsif ($key eq 'or') {
                   1332:             $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1333:             $replacement="($1 OR $2)";
1.98      harris41 1334: 	}
                   1335: 	substr($pattern,
1.173     matthew  1336:                index($pattern,$match),
                   1337:                length($match),
                   1338:                $replacement);
1.98      harris41 1339:     }
                   1340:     &recursive_SQL_query_build($dkey,$pattern);
                   1341: }
1.22      harris41 1342: 
1.122     matthew  1343: ######################################################################
                   1344: ######################################################################
                   1345: 
                   1346: =pod 
                   1347: 
                   1348: =item &build_date_queries() 
                   1349: 
1.126     matthew  1350: Builds a SQL logic query to check time/date entries.
                   1351: Also reports errors (check for /^Incorrect/).
                   1352: 
1.122     matthew  1353: =cut
                   1354: 
                   1355: ######################################################################
                   1356: ######################################################################
1.98      harris41 1357: sub build_date_queries {
                   1358:     my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
                   1359: 	$lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
                   1360:     my @queries;
                   1361:     if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
                   1362: 	unless ($cmonth1 and $cday1 and $cyear1 and
                   1363: 		$cmonth2 and $cday2 and $cyear2) {
                   1364: 	    return "Incorrect entry for the creation date.  You must specify ".
                   1365: 		   "a starting month, day, and year and an ending month, ".
                   1366: 		   "day, and year.";
                   1367: 	}
                   1368: 	my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
                   1369: 	$cnumeric1+=0;
                   1370: 	my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
                   1371: 	$cnumeric2+=0;
                   1372: 	if ($cnumeric1>$cnumeric2) {
                   1373: 	    return "Incorrect entry for the creation date.  The starting ".
                   1374: 		   "date must occur before the ending date.";
                   1375: 	}
                   1376: 	my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
                   1377: 	           "$cyear2-$cmonth2-$cday2 23:59:59')";
                   1378: 	push @queries,$cquery;
                   1379:     }
                   1380:     if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
                   1381: 	unless ($lmonth1 and $lday1 and $lyear1 and
                   1382: 		$lmonth2 and $lday2 and $lyear2) {
                   1383: 	    return "Incorrect entry for the last revision date.  You must ".
                   1384: 		   "specify a starting month, day, and year and an ending ".
                   1385: 		   "month, day, and year.";
                   1386: 	}
                   1387: 	my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
                   1388: 	$lnumeric1+=0;
                   1389: 	my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
                   1390: 	$lnumeric2+=0;
                   1391: 	if ($lnumeric1>$lnumeric2) {
                   1392: 	    return "Incorrect entry for the last revision date.  The ".
                   1393: 		   "starting date must occur before the ending date.";
                   1394: 	}
                   1395: 	my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
                   1396: 	           "$lyear2-$lmonth2-$lday2 23:59:59')";
                   1397: 	push @queries,$lquery;
                   1398:     }
                   1399:     if (@queries) {
                   1400: 	return join(" AND ",@queries);
                   1401:     }
                   1402:     return '';
1.18      harris41 1403: }
1.6       harris41 1404: 
1.122     matthew  1405: ######################################################################
                   1406: ######################################################################
                   1407: 
1.144     matthew  1408: =pod
                   1409: 
                   1410: =item &copyright_check()
                   1411: 
                   1412: =cut
                   1413: 
                   1414: ######################################################################
                   1415: ######################################################################
                   1416: sub copyright_check {
                   1417:     my $Metadata = shift;
                   1418:     # Check copyright tags and skip results the user cannot use
                   1419:     my (undef,undef,$resdom,$resname) = split('/',
                   1420:                                               $Metadata->{'url'});
                   1421:     # Check for priv
                   1422:     if (($Metadata->{'copyright'} eq 'priv') && 
                   1423:         (($ENV{'user.name'} ne $resname) &&
                   1424:          ($ENV{'user.domain'} ne $resdom))) {
                   1425:         return 0;
                   1426:     }
                   1427:     # Check for domain
                   1428:     if (($Metadata->{'copyright'} eq 'domain') &&
                   1429:         ($ENV{'user.domain'} ne $resdom)) {
                   1430:         return 0;
                   1431:     }
                   1432:     return 1;
                   1433: }
                   1434: 
1.151     matthew  1435: 
                   1436: ######################################################################
                   1437: ######################################################################
                   1438: 
                   1439: =pod
                   1440: 
                   1441: =item &ensure_db_and_table
                   1442: 
                   1443: Ensure we can get lonmysql to connect to the database and the table we
                   1444: need exists.
                   1445: 
                   1446: Inputs: $r, table id
                   1447: 
                   1448: Returns: undef on error, 1 if the table exists.
                   1449: 
                   1450: =cut
                   1451: 
                   1452: ######################################################################
                   1453: ######################################################################
                   1454: sub ensure_db_and_table {
                   1455:     my ($r,$table) = @_;
                   1456:     ##
                   1457:     ## Sanity check the table id.
                   1458:     ##
                   1459:     if (! defined($table) || $table eq '' || $table =~ /\D/ ) {
                   1460:         $r->print("Unable to retrieve search results.  ".
                   1461:                   "Unable to determine the table results were stored in.  ".
                   1462:                   "</body></html>");
                   1463:         return undef;
                   1464:     }
                   1465:     ##
                   1466:     ## Make sure we can connect and the table exists.
                   1467:     ##
                   1468:     my $connection_result = &Apache::lonmysql::connect_to_db();
                   1469:     if (!defined($connection_result)) {
                   1470:         $r->print("Unable to connect to the MySQL database where your results".
                   1471:                   " are stored. </body></html>");
                   1472:         &Apache::lonnet::logthis("lonsearchcat: unable to get lonmysql to".
                   1473:                                  " connect to database.");
                   1474:         &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
                   1475:         return undef;
                   1476:     }
                   1477:     my $table_check = &Apache::lonmysql::check_table($table);
                   1478:     if (! defined($table_check)) {
                   1479:         $r->print("A MySQL error has occurred.</form></body></html>");
                   1480:         &Apache::lonnet::logthis("lonmysql was unable to determine the status".
                   1481:                                  " of table ".$table);
                   1482:         return undef;
                   1483:     } elsif (! $table_check) {
                   1484:         $r->print("The table of results could not be found.");
                   1485:         &Apache::lonnet::logthis("The user requested a table, ".$table.
                   1486:                                  ", that could not be found.");
                   1487:         return undef;
                   1488:     }
                   1489:     return 1;
                   1490: }
                   1491: 
                   1492: ######################################################################
                   1493: ######################################################################
                   1494: 
                   1495: =pod
                   1496: 
                   1497: =item &print_sort_form
                   1498: 
                   1499: =cut
                   1500: 
                   1501: ######################################################################
                   1502: ######################################################################
                   1503: sub print_sort_form {
                   1504:     my ($r,$pretty_query_string) = @_;
1.196     matthew  1505:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.151     matthew  1506:     ##
1.187     www      1507:     my %SortableFields=&Apache::lonlocal::texthash( 
                   1508:          id        => 'Default',
1.151     matthew  1509:          title     => 'Title',
                   1510:          author    => 'Author',
                   1511:          subject   => 'Subject',
                   1512:          url       => 'URL',
                   1513:          version   => 'Version Number',
                   1514:          mime      => 'Mime type',
                   1515:          lang      => 'Language',
                   1516:          owner     => 'Owner/Publisher',
                   1517:          copyright => 'Copyright',
                   1518:          hostname  => 'Host',
                   1519:          creationdate     => 'Creation Date',
1.187     www      1520:          lastrevisiondate => 'Revision Date'
1.151     matthew  1521:      );
                   1522:     ##
                   1523:     my $table = $ENV{'form.table'};
                   1524:     return if (! &ensure_db_and_table($r,$table));
                   1525:     ##
                   1526:     ## Get the number of results 
                   1527:     ##
                   1528:     my $total_results = &Apache::lonmysql::number_of_rows($table);
                   1529:     if (! defined($total_results)) {
                   1530:         $r->print("A MySQL error has occurred.</form></body></html>");
                   1531:         &Apache::lonnet::logthis("lonmysql was unable to determine the number".
                   1532:                                  " of rows in table ".$table);
                   1533:         &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
                   1534:         return;
                   1535:     }
                   1536:     my $result;
                   1537:     $result.=<<END;
                   1538: <html>
                   1539: <head>
                   1540: <script>
                   1541:     function change_sort() {
                   1542:         var newloc = "/adm/searchcat?phase=results";
                   1543:         newloc += "&persistent_db_id=$ENV{'form.persistent_db_id'}";
                   1544:         newloc += "&sortby=";
                   1545:         newloc += document.forms.statusform.elements.sortby.value;
                   1546:         parent.resultsframe.location= newloc;
                   1547:     }
                   1548: </script>
                   1549: <title>Results</title>
                   1550: </head>
1.155     matthew  1551: $bodytag
1.151     matthew  1552: <form name="statusform" action="" method="post">
1.153     matthew  1553: <input type="hidden" name="Queue" value="" />
1.151     matthew  1554: END
                   1555: 
                   1556: #<h2>Sort Results</h2>
                   1557: #Sort by: <select size="1" name="sortby" onchange="javascript:change_sort();">
                   1558: #    $ENV{'form.sortby'} = 'id' if (! defined($ENV{'form.sortby'}));
                   1559: #    foreach (keys(%SortableFields)) {
                   1560: #        $result.="<option name=\"$_\"";
                   1561: #        if ($_ eq $ENV{'form.sortby'}) {
                   1562: #            $result.=" selected ";
                   1563: #        }
                   1564: #        $result.=" >$SortableFields{$_}</option>\n";
                   1565: #    }
                   1566: #    $result.="</select>\n";
                   1567:     my $revise = &revise_button();
                   1568:     $result.=<<END;
                   1569: <p>
                   1570: There are $total_results matches to your query. $revise
                   1571: </p><p>
                   1572: Search:$pretty_query_string
                   1573: </p>
                   1574: </form>
                   1575: </body>
                   1576: </html>
                   1577: END
                   1578:     $r->print($result);
                   1579:     return;
                   1580: }
                   1581: 
1.144     matthew  1582: #####################################################################
                   1583: #####################################################################
                   1584: 
                   1585: =pod
                   1586: 
                   1587: =item MySQL Table Description
                   1588: 
                   1589: MySQL table creation requires a precise description of the data to be
                   1590: stored.  The use of the correct types to hold data is vital to efficient
                   1591: storage and quick retrieval of records.  The columns must be described in
                   1592: the following format:
                   1593: 
                   1594: =cut
                   1595: 
1.170     matthew  1596: #####################################################################
                   1597: #####################################################################
                   1598: 
                   1599: my @Datatypes = 
                   1600:     ( { name => 'id', 
                   1601:         type => 'MEDIUMINT',
                   1602:         restrictions => 'UNSIGNED NOT NULL',
                   1603:         primary_key  => 'yes',
                   1604:         auto_inc     => 'yes' },
                   1605:       { name => 'title',     type=>'TEXT'},
                   1606:       { name => 'author',    type=>'TEXT'},
                   1607:       { name => 'subject',   type=>'TEXT'},
                   1608:       { name => 'url',       type=>'TEXT', restrictions => 'NOT NULL' },
                   1609:       { name => 'keywords',  type=>'TEXT'},
                   1610:       { name => 'version',   type=>'TEXT'},
                   1611:       { name => 'notes',     type=>'TEXT'},
                   1612:       { name => 'abstract',  type=>'TEXT'},
                   1613:       { name => 'mime',      type=>'TEXT'},
1.198     www      1614:       { name => 'language',  type=>'TEXT'},
1.170     matthew  1615:       { name => 'owner',     type=>'TEXT'},
                   1616:       { name => 'copyright', type=>'TEXT'},
1.198     www      1617:       { name => 'dependencies',     type=>'TEXT'},
                   1618:       { name => 'modifyinguser', type=>'TEXT'},
                   1619:       { name => 'authorspace', type=>'TEXT'},
                   1620:       { name => 'lowestgradelevel',  type=>'INT'},
                   1621:       { name => 'highestgradelevel', type=>'INT'},
                   1622:       { name => 'standards', type=>'TEXT'},
                   1623:       { name => 'count',     type=>'INT'},
                   1624:       { name => 'course', type=>'INT'},
                   1625:       { name => 'course_list',  type=>'TEXT'},
                   1626:       { name => 'goto', type=>'INT'},
                   1627:       { name => 'goto_list',  type=>'TEXT'},
                   1628:       { name => 'comefrom', type=>'INT'},
                   1629:       { name => 'comefrom_list',  type=>'TEXT'},
                   1630:       { name => 'sequsage', type=>'INT'},
                   1631:       { name => 'sequsage_list',  type=>'TEXT'},
                   1632:       { name => 'stdno', type=>'INT'},
                   1633:       { name => 'stdno_list',  type=>'TEXT'},
                   1634:       { name => 'avetries', type=>'FLOAT'},
                   1635:       { name => 'avetries_list',  type=>'TEXT'},
                   1636:       { name => 'difficulty', type=>'FLOAT'},
                   1637:       { name => 'difficulty_list',  type=>'TEXT'},
                   1638:       { name => 'clear',  type=>'FLOAT'},
                   1639:       { name => 'technical',  type=>'FLOAT'},
                   1640:       { name => 'correct',  type=>'FLOAT'},
                   1641:       { name => 'helpful',  type=>'FLOAT'},
                   1642:       { name => 'depth',  type=>'FLOAT'},
                   1643:       { name => 'hostname', type=> 'TEXT'},
1.144     matthew  1644:       #--------------------------------------------------
1.170     matthew  1645:       { name => 'creationdate',     type=>'DATETIME'},
                   1646:       { name => 'lastrevisiondate', type=>'DATETIME'},
1.144     matthew  1647:       #--------------------------------------------------
                   1648:       );
                   1649: 
1.147     matthew  1650: my @Fullindicies = 
1.151     matthew  1651:     qw/title/;
                   1652: #    qw/title author subject abstract mime language owner copyright/;
1.147     matthew  1653:     
1.144     matthew  1654: ######################################################################
                   1655: ######################################################################
                   1656: 
                   1657: =pod
                   1658: 
1.146     matthew  1659: =item &create_results_table()
                   1660: 
                   1661: Creates the table of search results by calling lonmysql.  Stores the
                   1662: table id in $ENV{'form.table'}
                   1663: 
                   1664: Inputs: none.
                   1665: 
                   1666: Returns: the identifier of the table on success, undef on error.
                   1667: 
                   1668: =cut
                   1669: 
                   1670: ######################################################################
                   1671: ######################################################################
                   1672: sub create_results_table {
                   1673:     my $table = &Apache::lonmysql::create_table
1.170     matthew  1674:         ( { columns => \@Datatypes,
1.172     matthew  1675:             FULLTEXT => [{'columns' => \@Fullindicies},],
1.146     matthew  1676:         } );
                   1677:     if (defined($table)) {
                   1678:         $ENV{'form.table'} = $table;
                   1679:         return $table;
                   1680:     } 
                   1681:     return undef; # Error...
                   1682: }
1.148     matthew  1683: 
1.146     matthew  1684: ######################################################################
                   1685: ######################################################################
                   1686: 
                   1687: =pod
                   1688: 
1.150     matthew  1689: =item Search Status update functions
1.144     matthew  1690: 
1.150     matthew  1691: Each of the following functions changes the values of one of the
                   1692: input fields used to display the search status to the user.  The names
                   1693: should be explanatory.
1.144     matthew  1694: 
1.150     matthew  1695: Inputs: Apache request handler ($r), text to display.
1.148     matthew  1696: 
1.150     matthew  1697: Returns: Nothing.
1.148     matthew  1698: 
                   1699: =over 4
                   1700: 
                   1701: =item &update_count_status()
                   1702: 
1.150     matthew  1703: =item &update_status()
1.148     matthew  1704: 
1.150     matthew  1705: =item &update_seconds()
1.148     matthew  1706: 
                   1707: =back
                   1708: 
                   1709: =cut
                   1710: 
                   1711: ######################################################################
                   1712: ######################################################################
                   1713: sub update_count_status {
                   1714:     my ($r,$text) = @_;
                   1715:     $text =~ s/\'/\\\'/g;
                   1716:     $r->print
                   1717:         ("<script>document.statusform.count.value = ' $text'</script>\n");
                   1718:     $r->rflush();
                   1719: }
                   1720: 
1.150     matthew  1721: sub update_status {
1.148     matthew  1722:     my ($r,$text) = @_;
                   1723:     $text =~ s/\'/\\\'/g;
                   1724:     $r->print
1.150     matthew  1725:         ("<script>document.statusform.status.value = ' $text'</script>\n");
1.148     matthew  1726:     $r->rflush();
                   1727: }
                   1728: 
1.150     matthew  1729: sub update_seconds {
1.148     matthew  1730:     my ($r,$text) = @_;
                   1731:     $text =~ s/\'/\\\'/g;
                   1732:     $r->print
1.150     matthew  1733:         ("<script>document.statusform.seconds.value = ' $text'</script>\n");
1.148     matthew  1734:     $r->rflush();
                   1735: }
                   1736: 
                   1737: ######################################################################
                   1738: ######################################################################
                   1739: 
                   1740: =pod
                   1741: 
1.151     matthew  1742: =item &revise_button
                   1743: 
                   1744: Inputs: None
                   1745: 
                   1746: Returns: html string for a 'revise search' button.
                   1747: 
                   1748: =cut
                   1749: 
                   1750: ######################################################################
                   1751: ######################################################################
                   1752: sub revise_button {
                   1753:     my $revise_phase = 'disp_basic';
                   1754:     $revise_phase = 'disp_adv' if ($ENV{'form.searchmode'} eq 'advanced');
                   1755:     my $newloc = '/adm/searchcat'.
                   1756:         '?persistent_db_id='.$ENV{'form.persistent_db_id'}.
1.158     matthew  1757:             '&cleargroupsort=1'.
1.151     matthew  1758:             '&phase='.$revise_phase;
                   1759:     my $result = qq{<input type="button" value="Revise search" name="revise"} .
                   1760:         qq{ onClick="parent.location='$newloc';" /> };
                   1761:     return $result;
                   1762: }
                   1763: 
                   1764: ######################################################################
                   1765: ######################################################################
                   1766: 
                   1767: =pod
                   1768: 
1.144     matthew  1769: =item &run_search 
                   1770: 
                   1771: =cut
                   1772: 
                   1773: ######################################################################
                   1774: ######################################################################
                   1775: sub run_search {
1.146     matthew  1776:     my ($r,$query,$customquery,$customshow,$serverlist,$pretty_string) = @_;
1.196     matthew  1777:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.150     matthew  1778:     my $connection = $r->connection;
1.144     matthew  1779:     #
1.145     matthew  1780:     # Timing variables
                   1781:     #
                   1782:     my $starttime = time;
1.151     matthew  1783:     my $max_time  = 30;  # seconds for the search to complete
1.145     matthew  1784:     #
1.146     matthew  1785:     # Print run_search header
                   1786:     #
1.151     matthew  1787:     $r->print(<<END);
                   1788: <html>
                   1789: <head><title>Search Status</title></head>
1.155     matthew  1790: $bodytag
1.151     matthew  1791: <form name="statusform" action="" method="post">
                   1792: <input type="hidden" name="Queue" value="" />
                   1793: END
                   1794:     # Check to see if $pretty_string has more than one carriage return.
                   1795:     # Assume \n s are following <br /> s and truncate the value.
                   1796:     # (there is probably a better way)...
1.152     matthew  1797:     my @Lines = split /<br \/>/,$pretty_string;
                   1798:     if (@Lines > 2) {
                   1799:         $pretty_string = join '<br \>',(@Lines[0..2],'....<br />');
1.151     matthew  1800:     }
1.187     www      1801:     $r->print(&mt("Search").": ".$pretty_string);
1.146     matthew  1802:     $r->rflush();
                   1803:     #
1.145     matthew  1804:     # Determine the servers we need to contact.
                   1805:     #
1.144     matthew  1806:     my @Servers_to_contact;
                   1807:     if (defined($serverlist)) {
1.152     matthew  1808:         if (ref($serverlist) eq 'ARRAY') {
                   1809:             @Servers_to_contact = @$serverlist;
                   1810:         } else {
                   1811:             @Servers_to_contact = ($serverlist);
                   1812:         }
1.144     matthew  1813:     } else {
                   1814:         @Servers_to_contact = sort(keys(%Apache::lonnet::libserv));
                   1815:     }
                   1816:     my %Server_status;
1.146     matthew  1817:     my $table =$ENV{'form.table'};
1.150     matthew  1818:     if (! defined($table) || $table eq '' || $table =~ /\D/ ) {
1.147     matthew  1819:         $r->print("Unable to determine table id to store search results in.".
1.148     matthew  1820:                   "The search has been aborted.</body></html>");
1.147     matthew  1821:         return;
                   1822:     }
                   1823:     my $table_status = &Apache::lonmysql::check_table($table);
                   1824:     if (! defined($table_status)) {
                   1825:         $r->print("Unable to determine status of table.</body></html>");
                   1826:         &Apache::lonnet::logthis("Bogus table id of $table for ".
                   1827:                                  "$ENV{'user.name'} @ $ENV{'user.domain'}");
                   1828:         &Apache::lonnet::logthis("lonmysql error = ".
1.144     matthew  1829:                                  &Apache::lonmysql::get_error());
1.147     matthew  1830:         return;
                   1831:     }
                   1832:     if (! $table_status) {
                   1833:         $r->print("The table id,$table, we tried to use is invalid.".
1.148     matthew  1834:                   "The search has been aborted.</body></html>");
1.144     matthew  1835:         return;
                   1836:     }
1.145     matthew  1837:     ##
1.146     matthew  1838:     ## Prepare for the big loop.
                   1839:     ##
1.144     matthew  1840:     my $hitcountsum;
                   1841:     my $server; 
                   1842:     my $status;
1.151     matthew  1843:     my $revise = &revise_button();
1.148     matthew  1844:     $r->print(<<END);
                   1845: <table>
1.151     matthew  1846: <tr><th>Status</th><th>Total Matches</th><th>Time Remaining</th><th></th></tr>
1.148     matthew  1847: <tr>
1.150     matthew  1848: <td><input type="text" name="status"  value="" size="30" /></td>
                   1849: <td><input type="text" name="count"   value="" size="10" /></td>
                   1850: <td><input type="text" name="seconds" value="" size="8" /></td>
1.151     matthew  1851: <td>$revise</td>
1.148     matthew  1852: </tr>
                   1853: </table>
                   1854: </form>
                   1855: END
                   1856:     $r->rflush();
1.150     matthew  1857:     my $time_remaining = $max_time - (time - $starttime) ;
                   1858:     my $last_time = $time_remaining;
                   1859:     &update_seconds($r,$time_remaining);
1.178     matthew  1860:     &update_status($r,'contacting '.$Servers_to_contact[0]);
1.159     matthew  1861:     while (($time_remaining > 0) &&
1.144     matthew  1862:            ((@Servers_to_contact) || keys(%Server_status))) {
                   1863:         # Send out a search request if it needs to be done.
                   1864:         if (@Servers_to_contact) {
                   1865:             # Contact one server
                   1866:             my $server = shift(@Servers_to_contact);
1.189     www      1867:             &update_status($r,&mt('contacting').' '.$server);
1.144     matthew  1868:             my $reply=&Apache::lonnet::metadata_query($query,$customquery,
                   1869:                                                       $customshow,[$server]);
                   1870:             ($server) = keys(%$reply);
                   1871:             $Server_status{$server} = $reply->{$server};
                   1872:         } else {
1.150     matthew  1873:             # wait a sec. to give time for files to be written
                   1874:             # This sleep statement is here instead of outside the else 
                   1875:             # block because we do not want to pause if we have servers
                   1876:             # left to contact.  
1.183     matthew  1877:             if (scalar (keys(%Server_status))) {
                   1878:                 &update_status($r,
1.189     www      1879:                        &mt('waiting on').' '.(join(' ',keys(%Server_status))));
1.183     matthew  1880:             }
1.150     matthew  1881:             sleep(1)1.144     matthew  1882:         }
1.159     matthew  1883:         #
                   1884:         #
                   1885:         # Loop through the servers we have contacted but do not
                   1886:         # have results from yet, looking for results.
1.144     matthew  1887:         while (my ($server,$status) = each(%Server_status)) {
1.150     matthew  1888:             last if ($connection->aborted());
1.144     matthew  1889:             if ($status eq 'con_lost') {
                   1890:                 delete ($Server_status{$server});
                   1891:                 next;
                   1892:             }
                   1893:             $status=~/^([\.\w]+)$/; 
                   1894:        	    my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1;
                   1895:             if (-e $datafile && ! -e "$datafile.end") {
1.189     www      1896:                 &update_status($r,&mt('Receiving results from').' '.$server);
1.144     matthew  1897:                 next;
                   1898:             }
1.150     matthew  1899:             last if ($connection->aborted());
1.144     matthew  1900:             if (-e "$datafile.end") {
1.189     www      1901:                 &update_status($r,&mt('Reading results from').' '.$server);
1.144     matthew  1902:                 if (-z "$datafile") {
                   1903:                     delete($Server_status{$server});
                   1904:                     next;
                   1905:                 }
                   1906:                 my $fh;
                   1907:                 if (!($fh=Apache::File->new($datafile))) { 
1.146     matthew  1908:                     $r->print("Unable to open search results file for ".
1.145     matthew  1909:                                   "server $server.  Omitting from search");
1.150     matthew  1910:                     delete($Server_status{$server}); 
                   1911:                    next;
1.144     matthew  1912:                 }
                   1913:                 # Read in the whole file.
                   1914:                 while (my $result = <$fh>) {
1.150     matthew  1915:                     last if ($connection->aborted());
1.144     matthew  1916:                     # handle custom fields?  Someday we will!
                   1917:                     chomp($result);
                   1918:                     next unless $result;
                   1919:                     # Parse the result.
                   1920:                     my %Fields = &parse_raw_result($result,$server);
                   1921:                     $Fields{'hostname'} = $server;
                   1922:                     next if (! &copyright_check(\%Fields));
                   1923:                     # Store the result in the mysql database
                   1924:                     my $result = &Apache::lonmysql::store_row($table,\%Fields);
                   1925:                     if (! defined($result)) {
1.146     matthew  1926:                         $r->print(&Apache::lonmysql::get_error());
1.144     matthew  1927:                     }
1.146     matthew  1928:                     # $r->print(&Apache::lonmysql::get_debug());
1.144     matthew  1929:                     $hitcountsum ++;
1.150     matthew  1930:                     $time_remaining = $max_time - (time - $starttime) ;
                   1931:                     if ($last_time - $time_remaining > 0) {
                   1932:                         &update_seconds($r,$time_remaining);
                   1933:                         $last_time = $time_remaining;
                   1934:                     }
                   1935:                     if ($hitcountsum % 50 == 0) {
                   1936:                         &update_count_status($r,$hitcountsum);
                   1937:                     }
1.144     matthew  1938:                 } # End of foreach (@results)
                   1939:                 $fh->close();
                   1940:                 # $server is only deleted if the results file has been 
                   1941:                 # found and (successfully) opened.  This may be a bad idea.
                   1942:                 delete($Server_status{$server});
                   1943:             }
1.150     matthew  1944:             last if ($connection->aborted());
1.148     matthew  1945:             &update_count_status($r,$hitcountsum);
1.144     matthew  1946:         }
1.150     matthew  1947:         last if ($connection->aborted());
1.144     matthew  1948:         # Finished looping through the servers
1.159     matthew  1949:         $starttime = time if (@Servers_to_contact);
1.150     matthew  1950:         $time_remaining = $max_time - (time - $starttime) ;
                   1951:         if ($last_time - $time_remaining > 0) {
                   1952:             $last_time = $time_remaining;
                   1953:             &update_seconds($r,$time_remaining);
                   1954:         }
1.144     matthew  1955:     }
1.189     www      1956:     &update_status($r,&mt('Search Complete').$server);
1.151     matthew  1957:     &update_seconds($r,0);
1.144     matthew  1958:     &Apache::lonmysql::disconnect_from_db();
                   1959:     # We have run out of time or run out of servers to talk to and
                   1960:     # results to get.  
1.146     matthew  1961:     $r->print("</body></html>");
1.153     matthew  1962:     if ($ENV{'form.catalogmode'} ne 'groupsearch') {
                   1963:         $r->print("<script>".
                   1964:                       "window.location='/adm/searchcat?".
                   1965:                       "phase=sort&".
                   1966:                       "persistent_db_id=$ENV{'form.persistent_db_id'}';".
                   1967:                   "</script>");
                   1968:     }
1.144     matthew  1969:     return;
                   1970: }
                   1971: 
                   1972: ######################################################################
                   1973: ######################################################################
                   1974: =pod
                   1975: 
1.146     matthew  1976: =item &prev_next_buttons
1.144     matthew  1977: 
                   1978: =cut
                   1979: 
                   1980: ######################################################################
                   1981: ######################################################################
1.146     matthew  1982: sub prev_next_buttons {
1.145     matthew  1983:     my ($current_min,$show,$total,$parms) = @_;
                   1984:     return '' if ($show eq 'all'); # No links if you get them all at once.
                   1985:     my $links;
                   1986:     ##
                   1987:     ## Prev
                   1988:     my $prev_min = $current_min - $show;
1.151     matthew  1989:     $prev_min = 1 if $prev_min < 1;
1.145     matthew  1990:     if ($prev_min < $current_min) {
                   1991:         $links .= qq{
1.146     matthew  1992: <a href="/adm/searchcat?$parms&start=$prev_min&show=$show">prev</a>
1.145     matthew  1993: };    
1.146     matthew  1994:     } else {
                   1995:         $links .= 'prev';
1.145     matthew  1996:     }
                   1997:     ##
                   1998:     ## Pages.... Someday.
                   1999:     ##
1.146     matthew  2000:     $links .= qq{ &nbsp;
                   2001: <a href="/adm/searchcat?$parms&start=$current_min&$show=$show">reload</a>
                   2002: };
1.145     matthew  2003:     ##
                   2004:     ## Next
                   2005:     my $next_min = $current_min + $show;
1.146     matthew  2006:     $next_min = $current_min if ($next_min > $total);
1.145     matthew  2007:     if ($next_min != $current_min) {
1.146     matthew  2008:         $links .= qq{ &nbsp;
                   2009: <a href="/adm/searchcat?$parms&start=$next_min&show=$show">next</a>
1.145     matthew  2010: };    
1.146     matthew  2011:     } else {
                   2012:         $links .= '&nbsp;next';
1.144     matthew  2013:     }
1.145     matthew  2014:     return $links;
1.144     matthew  2015: }
                   2016: ######################################################################
                   2017: ######################################################################
                   2018: 
                   2019: =pod
                   2020: 
                   2021: =item &display_results
                   2022: 
                   2023: =cut
                   2024: 
                   2025: ######################################################################
                   2026: ######################################################################
                   2027: sub display_results {
1.196     matthew  2028:     my ($r,$importbutton,$closebutton,$diropendb) = @_;
1.150     matthew  2029:     my $connection = $r->connection;
                   2030:     $r->print(&search_results_header($importbutton,$closebutton));
1.144     matthew  2031:     ##
                   2032:     ## Set viewing function
                   2033:     ##
                   2034:     my $viewfunction = $Views{$ENV{'form.viewselect'}};
                   2035:     if (!defined($viewfunction)) {
                   2036:         $r->print("Internal Error - Bad view selected.\n");
                   2037:         $r->rflush();
                   2038:         return;
                   2039:     }
                   2040:     ##
1.158     matthew  2041:     ## $checkbox_num is a count of the number of checkboxes output on the 
                   2042:     ## page this is used only during catalogmode=groupsearch.
                   2043:     my $checkbox_num = 0;
                   2044:     ##
1.144     matthew  2045:     ## Get the catalog controls setup
                   2046:     ##
1.146     matthew  2047:     my $action = "/adm/searchcat?phase=results";
                   2048:     ##
1.147     matthew  2049:     ## Deal with groupsearch
1.146     matthew  2050:     ##
                   2051:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                   2052:         if (! tie(%groupsearch_db,'GDBM_File',$diropendb,
1.148     matthew  2053:                   &GDBM_WRCREAT(),0640)) {
1.150     matthew  2054:             $r->print('Unable to store import results.</form></body></html>');
1.146     matthew  2055:             $r->rflush();
                   2056:             return;
                   2057:         } 
1.144     matthew  2058:     }
1.145     matthew  2059:     ##
                   2060:     ## Prepare the table for querying
                   2061:     ##
1.144     matthew  2062:     my $table = $ENV{'form.table'};
1.151     matthew  2063:     return if (! &ensure_db_and_table($r,$table));
1.145     matthew  2064:     ##
                   2065:     ## Get the number of results 
                   2066:     ##
                   2067:     my $total_results = &Apache::lonmysql::number_of_rows($table);
                   2068:     if (! defined($total_results)) {
1.150     matthew  2069:         $r->print("A MySQL error has occurred.</form></body></html>");
1.145     matthew  2070:         &Apache::lonnet::logthis("lonmysql was unable to determine the number".
                   2071:                                  " of rows in table ".$table);
                   2072:         &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
                   2073:         return;
                   2074:     }
                   2075:     ##
                   2076:     ## Determine how many results we need to get
                   2077:     ##
1.151     matthew  2078:     $ENV{'form.start'} = 1      if (! exists($ENV{'form.start'}));
                   2079:     $ENV{'form.show'}  = 'all'  if (! exists($ENV{'form.show'}));
1.146     matthew  2080:     my $min = $ENV{'form.start'};
1.145     matthew  2081:     my $max;
                   2082:     if ($ENV{'form.show'} eq 'all') {
                   2083:         $max = $total_results ;
                   2084:     } else {
1.151     matthew  2085:         $max = $min + $ENV{'form.show'} - 1;
1.146     matthew  2086:         $max = $total_results if ($max > $total_results);
1.145     matthew  2087:     }
                   2088:     ##
                   2089:     ## Output links (if necessary) for 'prev' and 'next' pages.
                   2090:     ##
1.146     matthew  2091:     $r->print
1.148     matthew  2092:         ('<center>'.
1.146     matthew  2093:          &prev_next_buttons($min,$ENV{'form.show'},$total_results,
                   2094:                             "table=".$ENV{'form.table'}.
                   2095:                             "&phase=results".
                   2096:                             "&persistent_db_id=".$ENV{'form.persistent_db_id'})
1.148     matthew  2097:          ."</center>\n"
1.146     matthew  2098:          );
1.150     matthew  2099:     if ($total_results == 0) {
1.181     matthew  2100:         $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="1">'.
1.187     www      2101:                   '<h3>'.&mt('There are currently no results').'.</h3>'.
1.150     matthew  2102:                   "</form></body></html>");
                   2103:         return;
                   2104:     } else {
                   2105:         $r->print
                   2106:             ("<center>Results $min to $max out of $total_results</center>\n");
                   2107:     }
1.145     matthew  2108:     ##
                   2109:     ## Get results from MySQL table
                   2110:     ##
                   2111:     my @Results = &Apache::lonmysql::get_rows($table,
1.151     matthew  2112:                                               'id>='.$min.' AND id<='.$max);
1.145     matthew  2113:     ##
                   2114:     ## Loop through the results and output them.
                   2115:     ##
1.144     matthew  2116:     foreach my $row (@Results) {
1.150     matthew  2117:         if ($connection->aborted()) {
1.162     www      2118:             &cleanup();
1.150     matthew  2119:             return;
                   2120:         }
1.144     matthew  2121:         my %Fields = %{&parse_row(@$row)};
1.145     matthew  2122:         my $output="<p>\n";
1.158     matthew  2123:         my $prefix=&catalogmode_output($Fields{'title'},$Fields{'url'},
                   2124:                                        $Fields{'id'},$checkbox_num++);
1.144     matthew  2125:         # Render the result into html
1.150     matthew  2126:         $output.= &$viewfunction($prefix,%Fields);
1.145     matthew  2127:         # Print them out as they come in.
1.144     matthew  2128:         $r->print($output);
                   2129:         $r->rflush();
                   2130:     }
                   2131:     if (@Results < 1) {
1.187     www      2132:         $r->print(&mt("There were no results matching your query"));
1.147     matthew  2133:     } else {
                   2134:         $r->print
1.148     matthew  2135:             ('<center>'.
1.147     matthew  2136:              &prev_next_buttons($min,$ENV{'form.show'},$total_results,
                   2137:                                 "table=".$ENV{'form.table'}.
                   2138:                                 "&phase=results".
                   2139:                                 "&persistent_db_id=".
                   2140:                                 $ENV{'form.persistent_db_id'})
1.148     matthew  2141:              ."</center>\n"
1.147     matthew  2142:              );
1.144     matthew  2143:     }
1.150     matthew  2144:     $r->print("</form></body></html>");
1.144     matthew  2145:     $r->rflush();
1.150     matthew  2146:     untie %groupsearch_db if (tied(%groupsearch_db));
1.144     matthew  2147:     return;
                   2148: }
                   2149: 
                   2150: ######################################################################
                   2151: ######################################################################
                   2152: 
                   2153: =pod
                   2154: 
1.158     matthew  2155: =item &catalogmode_output($title,$url,$fnum,$checkbox_num)
1.145     matthew  2156: 
                   2157: Returns html needed for the various catalog modes.  Gets inputs from
1.158     matthew  2158: $ENV{'form.catalogmode'}.  Stores data in %groupsearch_db.
1.145     matthew  2159: 
                   2160: =cut
                   2161: 
                   2162: ######################################################################
                   2163: ######################################################################
                   2164: sub catalogmode_output {
                   2165:     my $output = '';
1.158     matthew  2166:     my ($title,$url,$fnum,$checkbox_num) = @_;
1.145     matthew  2167:     if ($ENV{'form.catalogmode'} eq 'interactive') {
1.150     matthew  2168:         $title=~ s/\'/\\\'/g;
1.145     matthew  2169:         if ($ENV{'form.catalogmode'} eq 'interactive') {
                   2170:             $output.=<<END 
                   2171: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
                   2172: onClick="javascript:select_data('$title','$url')">
                   2173: </font>
                   2174: END
                   2175:         }
1.150     matthew  2176:     } elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
1.145     matthew  2177:         $groupsearch_db{"pre_${fnum}_link"}=$url;
                   2178:         $groupsearch_db{"pre_${fnum}_title"}=$title;
                   2179:         $output.=<<END;
                   2180: <font size='-1'>
                   2181: <input type="checkbox" name="returnvalues" value="SELECT"
1.158     matthew  2182: onClick="javascript:queue($checkbox_num,$fnum)" />
1.145     matthew  2183: </font>
                   2184: END
                   2185:     }
                   2186:     return $output;
                   2187: }
                   2188: ######################################################################
                   2189: ######################################################################
                   2190: 
                   2191: =pod
                   2192: 
1.144     matthew  2193: =item &parse_row
                   2194: 
                   2195: Parse a row returned from the database.
                   2196: 
                   2197: =cut
                   2198: 
                   2199: ######################################################################
                   2200: ######################################################################
                   2201: sub parse_row {
                   2202:     my @Row = @_;
                   2203:     my %Fields;
                   2204:     for (my $i=0;$i<=$#Row;$i++) {
1.170     matthew  2205:         $Fields{$Datatypes[$i]->{'name'}}=&Apache::lonnet::unescape($Row[$i]);
1.144     matthew  2206:     }
                   2207:     $Fields{'language'} = 
1.198     www      2208:         &Apache::loncommon::languagedescription($Fields{'language'});
1.144     matthew  2209:     $Fields{'copyrighttag'} =
                   2210:         &Apache::loncommon::copyrightdescription($Fields{'copyright'});
                   2211:     $Fields{'mimetag'} =
                   2212:         &Apache::loncommon::filedescription($Fields{'mime'});
                   2213:     return \%Fields;
                   2214: }
1.126     matthew  2215: 
                   2216: ###########################################################
                   2217: ###########################################################
                   2218: 
                   2219: =pod
                   2220: 
                   2221: =item &parse_raw_result()
                   2222: 
                   2223: Takes a line from the file of results and parse it.  Returns a hash 
1.198     www      2224: with keys according to column labels
1.126     matthew  2225: 
                   2226: In addition, the following tags are set by calling the appropriate 
1.198     www      2227: lonnet function: 'language', 'copyrighttag', 'mimetag'.
1.126     matthew  2228: 
                   2229: The 'title' field is set to "Untitled" if the title field is blank.
                   2230: 
                   2231: 'abstract' and 'keywords' are truncated to 200 characters.
                   2232: 
                   2233: =cut
                   2234: 
                   2235: ###########################################################
                   2236: ###########################################################
                   2237: sub parse_raw_result {
                   2238:     my ($result,$hostname) = @_;
1.198     www      2239: # conclude from self to others regarding fields
1.199     www      2240:     my %Fields=&Apache::lonmeta::metadata_col_to_hash(
                   2241: 						map {
                   2242: 						 &Apache::lonnet::unescape($_);
                   2243: 						} (split(/\,/,$result))
                   2244: 						      );
1.126     matthew  2245:     return %Fields;
                   2246: }
                   2247: 
                   2248: ###########################################################
                   2249: ###########################################################
                   2250: 
                   2251: =pod
                   2252: 
                   2253: =item &handle_custom_fields()
                   2254: 
                   2255: =cut
                   2256: 
                   2257: ###########################################################
                   2258: ###########################################################
                   2259: sub handle_custom_fields {
                   2260:     my @results = @{shift()};
                   2261:     my $customshow='';
                   2262:     my $extrashow='';
                   2263:     my @customfields;
                   2264:     if ($ENV{'form.customshow'}) {
                   2265:         $customshow=$ENV{'form.customshow'};
                   2266:         $customshow=~s/[^\w\s]//g;
                   2267:         my @fields=map {
                   2268:             "<font color=\"#008000\">$_:</font><!-- $_ -->";
                   2269:         } split(/\s+/,$customshow);
                   2270:         @customfields=split(/\s+/,$customshow);
                   2271:         if ($customshow) {
                   2272:             $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
                   2273:         }
                   2274:     }
                   2275:     my $customdata='';
                   2276:     my %customhash;
                   2277:     foreach my $result (@results) {
                   2278:         if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
                   2279:             my $tmp=$result;
                   2280:             $tmp=~s/^custom\=//;
                   2281:             my ($k,$v)=map {&Apache::lonnet::unescape($_);
                   2282:                         } split(/\,/,$tmp);
                   2283:             $customhash{$k}=$v;
                   2284:         }
                   2285:     }
                   2286:     return ($extrashow,\@customfields,\%customhash);
1.41      harris41 2287: }
                   2288: 
1.122     matthew  2289: ######################################################################
                   2290: ######################################################################
                   2291: 
1.125     matthew  2292: =pod
                   2293: 
                   2294: =item &search_results_header
                   2295: 
1.130     matthew  2296: Output the proper html headers and javascript code to deal with different 
                   2297: calling modes.
                   2298: 
                   2299: Takes most inputs directly from %ENV, except $mode.  
                   2300: 
                   2301: =over 4
                   2302: 
                   2303: =item $mode is either (at this writing) 'Basic' or 'Advanced'
                   2304: 
                   2305: =back
1.126     matthew  2306: 
1.130     matthew  2307: The following environment variables are checked:
1.126     matthew  2308: 
                   2309: =over 4
                   2310: 
                   2311: =item 'form.catalogmode' 
                   2312: 
                   2313: Checked for 'interactive' and 'groupsearch'.
                   2314: 
                   2315: =item 'form.mode'
                   2316: 
                   2317: Checked for existance & 'edit' mode.
                   2318: 
                   2319: =item 'form.form'
                   2320: 
1.191     albertel 2321: Contains the name of the form that has the input fields to set
                   2322: 
1.126     matthew  2323: =item 'form.element'
                   2324: 
1.191     albertel 2325: the name of the input field to put the URL into
                   2326: 
                   2327: =item 'form.titleelement'
                   2328: 
                   2329: the name of the input field to put the title into
                   2330: 
1.126     matthew  2331: =back
                   2332: 
1.125     matthew  2333: =cut
                   2334: 
                   2335: ######################################################################
                   2336: ######################################################################
                   2337: sub search_results_header {
1.150     matthew  2338:     my ($importbutton,$closebutton) = @_;
1.196     matthew  2339:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.125     matthew  2340:     my $result = '';
                   2341:     # output beginning of search page
                   2342:     # conditional output of script functions dependent on the mode in
                   2343:     # which the search was invoked
                   2344:     if ($ENV{'form.catalogmode'} eq 'interactive'){
                   2345: 	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
                   2346:             $result.=<<SCRIPT;
                   2347: <script type="text/javascript">
                   2348:     function select_data(title,url) {
                   2349: 	changeTitle(title);
                   2350: 	changeURL(url);
1.150     matthew  2351: 	parent.close();
1.125     matthew  2352:     }
                   2353:     function changeTitle(val) {
1.153     matthew  2354: 	if (parent.opener.inf.document.forms.resinfo.elements.t) {
                   2355: 	    parent.opener.inf.document.forms.resinfo.elements.t.value=val;
1.125     matthew  2356: 	}
                   2357:     }
                   2358:     function changeURL(val) {
1.153     matthew  2359: 	if (parent.opener.inf.document.forms.resinfo.elements.u) {
                   2360: 	    parent.opener.inf.document.forms.resinfo.elements.u.value=val;
1.125     matthew  2361: 	}
                   2362:     }
                   2363: </script>
                   2364: SCRIPT
                   2365:         } elsif ($ENV{'form.mode'} eq 'edit') {
                   2366:             my $form = $ENV{'form.form'};
                   2367:             my $element = $ENV{'form.element'};
1.191     albertel 2368:             my $titleelement = $ENV{'form.titleelement'};
                   2369: 	    my $changetitle;
                   2370: 	    if (!$titleelement) {
                   2371: 		$changetitle='function changeTitle(val) {}';
                   2372: 	    } else {
                   2373: 		    $changetitle=<<END;
                   2374: function changeTitle(val) {
                   2375:     if (parent.targetwin.document) {
                   2376:         parent.targetwin.document.forms["$form"].elements["$titleelement"].value=val;
                   2377:     } else {
                   2378: 	var url = 'forms[\"$form\"].elements[\"$titleelement\"].value';
                   2379:         alert("Unable to transfer data to "+url);
                   2380:     }
                   2381: }
                   2382: END
                   2383:             }
                   2384: 
1.125     matthew  2385:             $result.=<<SCRIPT;
                   2386: <script type="text/javascript">
                   2387: function select_data(title,url) {
                   2388:     changeURL(url);
1.191     albertel 2389:     changeTitle(title);
1.150     matthew  2390:     parent.close();
1.125     matthew  2391: }
1.191     albertel 2392: $changetitle
1.125     matthew  2393: function changeURL(val) {
1.150     matthew  2394:     if (parent.targetwin.document) {
                   2395:         parent.targetwin.document.forms["$form"].elements["$element"].value=val;
1.125     matthew  2396:     } else {
                   2397: 	var url = 'forms[\"$form\"].elements[\"$element\"].value';
                   2398:         alert("Unable to transfer data to "+url);
                   2399:     }
                   2400: }
                   2401: </script>
                   2402: SCRIPT
                   2403:         }
                   2404:     }
                   2405:     $result.=<<SCRIPT if $ENV{'form.catalogmode'} eq 'groupsearch';
                   2406: <script type="text/javascript">
1.158     matthew  2407:     function queue(checkbox_num,val) {
1.185     matthew  2408:         if (document.forms.results.returnvalues.length != "undefined" &&
                   2409:             typeof(document.forms.results.returnvalues.length) == "number") {
                   2410:             if (document.forms.results.returnvalues[checkbox_num].checked) {
                   2411:                 parent.statusframe.document.forms.statusform.elements.Queue.value +='1a'+val+'b';
                   2412:             } else {
                   2413:                 parent.statusframe.document.forms.statusform.elements.Queue.value +='0a'+val+'b';
                   2414:             }
1.150     matthew  2415:         } else {
1.185     matthew  2416:             if (document.forms.results.returnvalues.checked) {
                   2417:                 parent.statusframe.document.forms.statusform.elements.Queue.value +='1a'+val+'b';
                   2418:             } else {
                   2419:                 parent.statusframe.document.forms.statusform.elements.Queue.value +='0a'+val+'b';
                   2420:             }
1.150     matthew  2421:         }
1.125     matthew  2422:     }
                   2423:     function select_group() {
1.150     matthew  2424: 	parent.window.location=
1.125     matthew  2425:     "/adm/groupsort?mode=$ENV{'form.mode'}&catalogmode=groupsearch&acts="+
1.150     matthew  2426: 	    parent.statusframe.document.forms.statusform.elements.Queue.value;
1.125     matthew  2427:     }
                   2428: </script>
                   2429: SCRIPT
1.130     matthew  2430:     $result.=<<END;
                   2431: </head>
1.155     matthew  2432: $bodytag
1.150     matthew  2433: <form name="results" method="post" action="" >
                   2434: <input type="hidden" name="Queue" value="" />
                   2435: $importbutton
1.130     matthew  2436: END
1.125     matthew  2437:     return $result;
                   2438: }
                   2439: 
                   2440: ######################################################################
                   2441: ######################################################################
1.146     matthew  2442: sub search_status_header {
1.196     matthew  2443:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.146     matthew  2444:     return <<ENDSTATUS;
                   2445: <html><head><title>Search Status</title></head>
1.155     matthew  2446: $bodytag
1.146     matthew  2447: <h3>Search Status</h3>
                   2448: Sending search request to LON-CAPA servers.<br />
                   2449: ENDSTATUS
                   2450: }
                   2451: 
1.150     matthew  2452: sub results_link {
                   2453:     my $basic_link   = "/adm/searchcat?"."&table=".$ENV{'form.table'}.
                   2454:         "&persistent_db_id=".$ENV{'form.persistent_db_id'};
                   2455:     my $results_link = $basic_link."&phase=results".
1.151     matthew  2456:         "&pause=1"."&start=1";
1.150     matthew  2457:     return $results_link;
                   2458: }
                   2459: 
1.146     matthew  2460: ######################################################################
                   2461: ######################################################################
                   2462: sub print_frames_interface {
                   2463:     my $r = shift;
                   2464:     my $basic_link = "/adm/searchcat?"."&table=".$ENV{'form.table'}.
                   2465:         "&persistent_db_id=".$ENV{'form.persistent_db_id'};
                   2466:     my $run_search_link = $basic_link."&phase=run_search";
1.150     matthew  2467:     my $results_link = &results_link();
1.146     matthew  2468:     my $result = <<"ENDFRAMES";
                   2469: <html>
                   2470: <head>
1.150     matthew  2471: <script>
                   2472: var targetwin = opener;
1.158     matthew  2473: var queue = '';
1.150     matthew  2474: </script>
1.146     matthew  2475: <title>LON-CAPA Digital Library Search Results</title>
                   2476: </head>
1.176     www      2477: <frameset rows="150,*">
1.146     matthew  2478:     <frame name="statusframe"  src="$run_search_link">
                   2479:     <frame name="resultsframe" src="$results_link">
                   2480: </frameset>
                   2481: </html>
                   2482: ENDFRAMES
                   2483: 
                   2484:     $r->print($result);
                   2485:     return;
                   2486: }
                   2487: 
                   2488: ######################################################################
                   2489: ######################################################################
1.125     matthew  2490: 
1.122     matthew  2491: =pod 
                   2492: 
                   2493: =item Metadata Viewing Functions
                   2494: 
                   2495: Output is a HTML-ified string.
                   2496: Input arguments are title, author, subject, url, keywords, version,
                   2497: notes, short abstract, mime, language, creation date,
1.126     matthew  2498: last revision date, owner, copyright, hostname, and
1.122     matthew  2499: extra custom metadata to show.
                   2500: 
                   2501: =over 4
                   2502: 
                   2503: =item &detailed_citation_view() 
                   2504: 
                   2505: =cut
                   2506: 
                   2507: ######################################################################
                   2508: ######################################################################
1.50      harris41 2509: sub detailed_citation_view {
1.150     matthew  2510:     my ($prefix,%values) = @_;
1.192     albertel 2511:     my $icon=&Apache::loncommon::icon($values{'url'});
1.50      harris41 2512:     my $result=<<END;
1.192     albertel 2513: <b>$prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
1.150     matthew  2514:     target='search_preview'>$values{'title'}</a></b>
1.56      harris41 2515: <p>
1.130     matthew  2516: <b>$values{'author'}</b>, <i>$values{'owner'}</i><br />
                   2517: 
                   2518: <b>Subject:       </b> $values{'subject'}<br />
                   2519: <b>Keyword(s):    </b> $values{'keywords'}<br />
                   2520: <b>Notes:         </b> $values{'notes'}<br />
                   2521: <b>MIME Type:     </b> $values{'mimetag'}<br />
                   2522: <b>Language:      </b> $values{'language'}<br />
1.198     www      2523: <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />
1.78      harris41 2524: </p>
1.126     matthew  2525: $values{'extrashow'}
1.78      harris41 2526: <p>
1.126     matthew  2527: $values{'shortabstract'}
1.50      harris41 2528: </p>
1.150     matthew  2529: <hr align='left' width='200' noshade />
1.50      harris41 2530: END
                   2531:     return $result;
                   2532: }
                   2533: 
1.122     matthew  2534: ######################################################################
                   2535: ######################################################################
                   2536: 
                   2537: =pod 
                   2538: 
                   2539: =item &summary_view() 
                   2540: 
                   2541: =cut
                   2542: ######################################################################
                   2543: ######################################################################
1.50      harris41 2544: sub summary_view {
1.150     matthew  2545:     my ($prefix,%values) = @_;
1.192     albertel 2546:     my $icon=&Apache::loncommon::icon($values{'url'});
1.50      harris41 2547:     my $result=<<END;
1.192     albertel 2548: $prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
1.126     matthew  2549:    target='search_preview'>$values{'author'}</a><br />
                   2550: $values{'title'}<br />
                   2551: $values{'owner'} -- $values{'lastrevisiondate'}<br />
                   2552: $values{'copyrighttag'}<br />
                   2553: $values{'extrashow'}
1.50      harris41 2554: </p>
1.150     matthew  2555: <hr align='left' width='200' noshade />
1.50      harris41 2556: END
                   2557:     return $result;
                   2558: }
                   2559: 
1.122     matthew  2560: ######################################################################
                   2561: ######################################################################
                   2562: 
                   2563: =pod 
                   2564: 
1.150     matthew  2565: =item &compact_view() 
                   2566: 
                   2567: =cut
                   2568: 
                   2569: ######################################################################
                   2570: ######################################################################
                   2571: sub compact_view {
                   2572:     my ($prefix,%values) = @_;
1.192     albertel 2573:     my $icon=&Apache::loncommon::icon($values{'url'});
1.150     matthew  2574:     my $result=<<END;
1.192     albertel 2575: $prefix <img src="$icon" /> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}"  target='search_preview'>
1.150     matthew  2576: $values{'title'}</a>
                   2577: <b>$values{'author'}</b><br />
                   2578: END
                   2579:     return $result;
                   2580: }
                   2581: 
                   2582: 
                   2583: ######################################################################
                   2584: ######################################################################
                   2585: 
                   2586: =pod 
                   2587: 
1.122     matthew  2588: =item &fielded_format_view() 
                   2589: 
                   2590: =cut
                   2591: 
                   2592: ######################################################################
                   2593: ######################################################################
1.50      harris41 2594: sub fielded_format_view {
1.150     matthew  2595:     my ($prefix,%values) = @_;
1.192     albertel 2596:     my $icon=&Apache::loncommon::icon($values{'url'});
1.50      harris41 2597:     my $result=<<END;
1.192     albertel 2598: $prefix <img src="$icon" />
1.126     matthew  2599: <b>URL: </b> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   2600:               target='search_preview'>$values{'url'}</a>
1.56      harris41 2601: <br />
1.126     matthew  2602: <b>Title:</b> $values{'title'}<br />
                   2603: <b>Author(s):</b> $values{'author'}<br />
                   2604: <b>Subject:</b> $values{'subject'}<br />
                   2605: <b>Keyword(s):</b> $values{'keywords'}<br />
                   2606: <b>Notes:</b> $values{'notes'}<br />
                   2607: <b>MIME Type:</b> $values{'mimetag'}<br />
                   2608: <b>Language:</b> $values{'language'}<br />
                   2609: <b>Creation Date:</b> $values{'creationdate'}<br />
                   2610: <b>Last Revision Date:</b> $values{'lastrevisiondate'}<br />
                   2611: <b>Publisher/Owner:</b> $values{'owner'}<br />
                   2612: <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />
                   2613: <b>Repository Location:</b> $values{'hostname'}<br />
                   2614: <b>Abstract:</b> $values{'shortabstract'}<br />
                   2615: $values{'extrashow'}
1.50      harris41 2616: </p>
1.150     matthew  2617: <hr align='left' width='200' noshade />
1.50      harris41 2618: END
                   2619:     return $result;
                   2620: }
                   2621: 
1.122     matthew  2622: ######################################################################
                   2623: ######################################################################
                   2624: 
                   2625: =pod 
                   2626: 
                   2627: =item &xml_sgml_view() 
                   2628: 
                   2629: =back 
                   2630: 
                   2631: =cut
                   2632: 
                   2633: ######################################################################
                   2634: ######################################################################
1.50      harris41 2635: sub xml_sgml_view {
1.150     matthew  2636:     my ($prefix,%values) = @_;
1.50      harris41 2637:     my $result=<<END;
1.150     matthew  2638: $prefix
1.56      harris41 2639: <pre>
                   2640: &lt;LonCapaResource&gt;
1.126     matthew  2641: &lt;url&gt;$values{'url'}&lt;/url&gt;
                   2642: &lt;title&gt;$values{'title'}&lt;/title&gt;
                   2643: &lt;author&gt;$values{'author'}&lt;/author&gt;
                   2644: &lt;subject&gt;$values{'subject'}&lt;/subject&gt;
                   2645: &lt;keywords&gt;$values{'keywords'}&lt;/keywords&gt;
                   2646: &lt;notes&gt;$values{'notes'}&lt;/notes&gt;
1.56      harris41 2647: &lt;mimeInfo&gt;
1.126     matthew  2648: &lt;mime&gt;$values{'mime'}&lt;/mime&gt;
                   2649: &lt;mimetag&gt;$values{'mimetag'}&lt;/mimetag&gt;
1.56      harris41 2650: &lt;/mimeInfo&gt;
                   2651: &lt;languageInfo&gt;
1.198     www      2652: &lt;language&gt;$values{'language'}&lt;/language&gt;
                   2653: &lt;languagetag&gt;$values{'languagetag'}&lt;/languagetag&gt;
1.56      harris41 2654: &lt;/languageInfo&gt;
1.126     matthew  2655: &lt;creationdate&gt;$values{'creationdate'}&lt;/creationdate&gt;
                   2656: &lt;lastrevisiondate&gt;$values{'lastrevisiondate'}&lt;/lastrevisiondate&gt;
                   2657: &lt;owner&gt;$values{'owner'}&lt;/owner&gt;
1.56      harris41 2658: &lt;copyrightInfo&gt;
1.126     matthew  2659: &lt;copyright&gt;$values{'copyright'}&lt;/copyright&gt;
                   2660: &lt;copyrighttag&gt;$values{'copyrighttag'}&lt;/copyrighttag&gt;
1.56      harris41 2661: &lt;/copyrightInfo&gt;
1.126     matthew  2662: &lt;repositoryLocation&gt;$values{'hostname'}&lt;/repositoryLocation&gt;
                   2663: &lt;shortabstract&gt;$values{'shortabstract'}&lt;/shortabstract&gt;
1.57      harris41 2664: &lt;/LonCapaResource&gt;
1.56      harris41 2665: </pre>
1.126     matthew  2666: $values{'extrashow'}
1.150     matthew  2667: <hr align='left' width='200' noshade />
1.50      harris41 2668: END
                   2669:     return $result;
1.60      harris41 2670: }
                   2671: 
1.122     matthew  2672: ######################################################################
                   2673: ######################################################################
                   2674: 
                   2675: =pod 
                   2676: 
                   2677: =item &filled() see if field is filled.
                   2678: 
                   2679: =cut
                   2680: 
                   2681: ######################################################################
                   2682: ######################################################################
1.98      harris41 2683: sub filled {
                   2684:     my ($field)=@_;
                   2685:     if ($field=~/\S/ && $field ne 'any') {
                   2686: 	return 1;
1.61      harris41 2687:     }
1.98      harris41 2688:     else {
                   2689: 	return 0;
1.61      harris41 2690:     }
1.60      harris41 2691: }
                   2692: 
1.122     matthew  2693: ######################################################################
                   2694: ######################################################################
                   2695: 
                   2696: =pod 
                   2697: 
                   2698: =item &output_blank_field_error()
                   2699: 
1.151     matthew  2700: Output a complete page that indicates the user has not filled in enough
                   2701: information to do a search.
                   2702: 
                   2703: Inputs: $r (Apache request handle), $closebutton, $parms.
                   2704: 
                   2705: Returns: nothing
                   2706: 
                   2707: $parms is extra information to include in the 'Revise search request' link.
                   2708: 
1.122     matthew  2709: =cut
                   2710: 
                   2711: ######################################################################
                   2712: ######################################################################
1.98      harris41 2713: sub output_blank_field_error {
1.196     matthew  2714:     my ($r,$closebutton,$parms,$hidden_fields)=@_;
                   2715:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.98      harris41 2716:     # make query information persistent to allow for subsequent revision
                   2717:     $r->print(<<BEGINNING);
                   2718: <html>
                   2719: <head>
                   2720: <title>The LearningOnline Network with CAPA</title>
                   2721: BEGINNING
                   2722:     $r->print(<<RESULTS);
                   2723: </head>
1.155     matthew  2724: $bodytag
1.98      harris41 2725: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
                   2726: <h1>Search Catalog</h1>
                   2727: <form method="post" action="/adm/searchcat">
1.145     matthew  2728: $hidden_fields
1.151     matthew  2729: <a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}"
1.146     matthew  2730: >Revise search request</a>&nbsp;
1.98      harris41 2731: $closebutton
                   2732: <hr />
1.151     matthew  2733: <h3>Unactionable search query.</h3>
1.98      harris41 2734: <p>
1.151     matthew  2735: You did not fill in enough information for the search to be started.
                   2736: You need to fill in relevant fields on the search page in order 
                   2737: for a query to be processed.
1.98      harris41 2738: </p>
                   2739: </body>
                   2740: </html>
                   2741: RESULTS
                   2742: }
                   2743: 
1.122     matthew  2744: ######################################################################
                   2745: ######################################################################
                   2746: 
                   2747: =pod 
                   2748: 
                   2749: =item &output_date_error()
                   2750: 
                   2751: Output a full html page with an error message.
                   2752: 
1.145     matthew  2753: Inputs: 
                   2754: 
                   2755:     $r, the request pointer.
                   2756:     $message, the error message for the user.
                   2757:     $closebutton, the specialized close button needed for groupsearch.
                   2758: 
1.122     matthew  2759: =cut
                   2760: 
                   2761: ######################################################################
                   2762: ######################################################################
1.60      harris41 2763: sub output_date_error {
1.196     matthew  2764:     my ($r,$message,$closebutton,$hidden_fields)=@_;
1.60      harris41 2765:     # make query information persistent to allow for subsequent revision
1.196     matthew  2766:     my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
1.122     matthew  2767:     $r->print(<<RESULTS);
1.60      harris41 2768: <html>
                   2769: <head>
                   2770: <title>The LearningOnline Network with CAPA</title>
                   2771: </head>
1.155     matthew  2772: $bodytag
1.98      harris41 2773: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1.60      harris41 2774: <h1>Search Catalog</h1>
                   2775: <form method="post" action="/adm/searchcat">
1.145     matthew  2776: $hidden_fields
1.60      harris41 2777: <input type='button' value='Revise search request'
1.98      harris41 2778: onClick='this.form.submit();' />
1.60      harris41 2779: $closebutton
1.98      harris41 2780: <hr />
1.151     matthew  2781: <h3>Error</h3>
1.60      harris41 2782: <p>
                   2783: $message
                   2784: </p>
                   2785: </body>
                   2786: </html>
                   2787: RESULTS
1.101     harris41 2788: }
                   2789: 
1.122     matthew  2790: ######################################################################
                   2791: ######################################################################
                   2792: 
                   2793: =pod 
                   2794: 
                   2795: =item &start_fresh_session()
                   2796: 
1.142     matthew  2797: Cleans the global %groupsearch_db by removing all fields which begin with
1.122     matthew  2798: 'pre_' or 'store'.
                   2799: 
                   2800: =cut
                   2801: 
                   2802: ######################################################################
                   2803: ######################################################################
1.101     harris41 2804: sub start_fresh_session {
1.142     matthew  2805:     delete $groupsearch_db{'mode_catalog'};
                   2806:     foreach (keys %groupsearch_db) {
1.101     harris41 2807:         if ($_ =~ /^pre_/) {
1.142     matthew  2808:             delete $groupsearch_db{$_};
1.101     harris41 2809:         }
                   2810:         if ($_ =~ /^store/) {
1.142     matthew  2811: 	    delete $groupsearch_db{$_};
1.101     harris41 2812: 	}
1.109     harris41 2813:     }
1.3       harris41 2814: }
1.1       www      2815: 
                   2816: 1;
1.162     www      2817: 
                   2818: sub cleanup {
1.163     www      2819:     if (tied(%groupsearch_db)) {
                   2820:         unless (untie(%groupsearch_db)) {
                   2821: 	  &Apache::lonnet::logthis('Failed cleanup searchcat: groupsearch_db');
                   2822:         }
                   2823:     }
1.167     www      2824:     &untiehash();
1.162     www      2825:     &Apache::lonmysql::disconnect_from_db();
                   2826: }
1.98      harris41 2827: 
1.1       www      2828: __END__
1.105     harris41 2829: 
1.121     matthew  2830: =pod
1.105     harris41 2831: 
1.121     matthew  2832: =back 
1.105     harris41 2833: 
                   2834: =cut

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