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

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

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