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

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

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