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

1.98      harris41    1: # The LearningOnline Network with CAPA
1.108     harris41    2: # Search Catalog
                      3: #
1.134   ! matthew     4: # $Id: lonsearchcat.pm,v 1.133 2002/06/27 19:28:50 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.97      harris41   28: # YEAR=2001
1.104     harris41   29: # 3/8, 3/12, 3/13, 3/14, 3/15, 3/19 Scott Harrison
                     30: # 3/20, 3/21, 3/22, 3/26, 3/27, 4/2, 8/15, 8/24, 8/25 Scott Harrison
1.113     harris41   31: # 10/12,10/14,10/15,10/16,11/28,11/29,12/10,12/12,12/16 Scott Harrison
1.115     harris41   32: # YEAR=2002
                     33: # 1/17 Scott Harrison
1.121     matthew    34: # 6/17 Matthew Hall
1.104     harris41   35: #
1.121     matthew    36: ###############################################################################
                     37: ###############################################################################
                     38: 
                     39: =pod 
                     40: 
                     41: =head1 NAME
                     42: 
                     43: lonsearchcat
                     44: 
                     45: =head1 SYNOPSIS
                     46: 
                     47: Search interface to LON-CAPAs digital library
                     48: 
                     49: =head1 DESCRIPTION
                     50: 
                     51: This module enables searching for a distributed browseable catalog.
1.104     harris41   52: 
1.121     matthew    53: This is part of the LearningOnline Network with CAPA project
                     54: described at http://www.lon-capa.org.
                     55: 
                     56: lonsearchcat presents the user with an interface to search the LON-CAPA
                     57: digital library.  lonsearchcat also initiates the execution of a search
                     58: by sending the search parameters to LON-CAPA servers.  The progress of 
                     59: search (on a server basis) is displayed to the user in a seperate window.
                     60: 
                     61: =head1 Internals
                     62: 
                     63: =over 4
                     64: 
                     65: =cut
                     66: 
                     67: ###############################################################################
1.98      harris41   68: ###############################################################################
1.121     matthew    69: 
1.128     harris41   70: ###############################################################################
1.98      harris41   71: ##                                                                           ##
                     72: ## ORGANIZATION OF THIS PERL MODULE                                          ##
                     73: ##                                                                           ##
1.105     harris41   74: ## 1. Modules used by this module                                            ##
1.128     harris41   75: ## 2. Variables used throughout the module                                   ##
                     76: ## 3. handler subroutine called via Apache and mod_perl                      ##
                     77: ## 4. Other subroutines                                                      ##
1.98      harris41   78: ##                                                                           ##
                     79: ###############################################################################
                     80: 
1.1       www        81: package Apache::lonsearchcat;
                     82: 
1.98      harris41   83: # ------------------------------------------------- modules used by this module
1.1       www        84: use strict;
                     85: use Apache::Constants qw(:common);
1.6       harris41   86: use Apache::lonnet();
                     87: use Apache::File();
1.7       harris41   88: use CGI qw(:standard);
1.41      harris41   89: use Text::Query;
1.101     harris41   90: use GDBM_File;
1.112     harris41   91: use Apache::loncommon();
1.1       www        92: 
1.90      harris41   93: # ---------------------------------------- variables used throughout the module
                     94: 
1.121     matthew    95: ######################################################################
                     96: ######################################################################
                     97: 
                     98: =pod 
                     99: 
                    100: =item Global variables
                    101: 
                    102: =over 4
                    103: 
                    104: =item $closebutton
                    105: 
                    106: button that closes the search window
                    107: 
                    108: =item $importbutton
                    109: 
1.134   ! matthew   110: button to take the select results and go to group sorting
1.121     matthew   111: 
                    112: =item %hash   
                    113: 
                    114: The ubiquitous database hash
                    115: 
                    116: =item $diropendb 
                    117: 
                    118: The full path to the (temporary) search database file.  This is set and
                    119: used in &handler() and is also used in &output_results().
                    120: 
                    121: =back 
                    122: 
                    123: =cut
                    124: 
                    125: ######################################################################
                    126: ######################################################################
                    127: 
1.98      harris41  128: # -- dynamically rendered interface components
1.121     matthew   129: my $closebutton;  # button that closes the search window
1.98      harris41  130: my $importbutton; # button to take the selected results and go to group sorting
                    131: 
                    132: # -- miscellaneous variables
1.121     matthew   133: my %hash;     # database hash
1.127     matthew   134: my $diropendb = "";    # db file
1.98      harris41  135: 
1.121     matthew   136: ######################################################################
                    137: ######################################################################
                    138: 
                    139: =pod 
                    140: 
                    141: =item &handler() - main handler invoked by httpd child
                    142: 
1.124     matthew   143: =item Variables
                    144: 
                    145: =over 4
                    146: 
                    147: =item $hidden
                    148: 
                    149: holds 'hidden' html forms
                    150: 
                    151: =item $scrout
                    152: 
                    153: string that holds portions of the screen output
                    154: 
                    155: =back 
                    156: 
1.121     matthew   157: =cut
1.101     harris41  158: 
1.121     matthew   159: ######################################################################
                    160: ######################################################################
1.98      harris41  161: sub handler {
                    162:     my $r = shift;
1.103     harris41  163:     untie %hash;
1.98      harris41  164: 
                    165:     $r->content_type('text/html');
                    166:     $r->send_http_header;
                    167:     return OK if $r->header_only;
                    168: 
1.121     matthew   169:     my $domain  = $r->dir_config('lonDefDomain');
1.122     matthew   170:     $diropendb= "/home/httpd/perl/tmp/".&Apache::lonnet::escape($domain).
                    171:             "\_".&Apache::lonnet::escape($ENV{'user.name'})."_searchcat.db";
1.101     harris41  172: 
1.116     matthew   173:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.124     matthew   174:              ['catalogmode','launch','acts','mode','form','element',
                    175:               'reqinterface']);
                    176:     ##
                    177:     ## Clear out old values from database
                    178:     ##
1.101     harris41  179:     if ($ENV{'form.launch'} eq '1') {
                    180: 	if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
                    181: 	    &start_fresh_session();
                    182: 	    untie %hash;
1.122     matthew   183: 	} else {
1.101     harris41  184: 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
                    185: 		      'file</body></html>');
                    186: 	    return OK;
                    187: 	}
                    188:     }
1.124     matthew   189:     ##
                    190:     ## Produce some output, so people know it is working
                    191:     ##
1.119     www       192:     $r->print("\n");
                    193:     $r->rflush;
1.124     matthew   194:     ##
                    195:     ## Configure dynamic components of interface
                    196:     ##
                    197:     my $hidden;       # Holds 'hidden' html forms
1.98      harris41  198:     if ($ENV{'form.catalogmode'} eq 'interactive') {
                    199: 	$hidden="<input type='hidden' name='catalogmode' value='interactive'>".
                    200: 	    "\n";
                    201:         $closebutton="<input type='button' name='close' value='CLOSE' ".
                    202: 	    "onClick='self.close()'>"."\n";
1.124     matthew   203:     } elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
1.98      harris41  204: 	$hidden=<<END;
                    205: <input type='hidden' name='catalogmode' value='groupsearch'>
                    206: END
                    207:         $closebutton=<<END;
                    208: <input type='button' name='close' value='CLOSE' onClick='self.close()'>
                    209: END
                    210:         $importbutton=<<END;
                    211: <input type='button' name='import' value='IMPORT'
                    212: onClick='javascript:select_group()'>
                    213: END
                    214:     }
1.133     matthew   215:     $hidden .= &make_persistent({ "form.mode"    => $ENV{'form.mode'},
                    216:                                   "form.form"    => $ENV{'form.form'},
                    217:                                   "form.element" => $ENV{'form.element'},
                    218:                                   "form.date"    => 2 });
1.124     matthew   219:     ##
                    220:     ##  What are we doing?
                    221:     ##
1.134   ! matthew   222:     my $searchtype;
        !           223:     $searchtype = 'Basic'    if ($ENV{'form.basicsubmit'}    eq 'SEARCH');
        !           224:     $searchtype = 'Advanced' if ($ENV{'form.advancedsubmit'} eq 'SEARCH');
        !           225:     if ($searchtype) {
        !           226:         my ($query,$customquery,$customshow,$libraries) = 
        !           227:             (undef,undef,undef,undef);
        !           228:         if ($searchtype eq 'Basic') {
        !           229:             $query = &parse_basic_search($r);
        !           230:         } elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
        !           231:             ($query,$customquery,$customshow,$libraries) 
        !           232:                 = &parse_advanced_search($r);
        !           233:             return OK if (! defined($query));
        !           234:         }
        !           235:         # Send query statements over the network to be processed by 
        !           236:         # either the SQL database or a recursive scheme of 'grep'-like 
        !           237:         # actions (for custom metadata).
        !           238:         $r->rflush();
        !           239:         my $reply=&Apache::lonnet::metadata_query($query,$customquery,
        !           240:                                                $customshow,$libraries);
        !           241:         &output_results($searchtype,$r,$reply,$hidden);
1.124     matthew   242:     } elsif ($ENV{'form.reqinterface'} eq 'advanced') {
                    243:         # Output the advanced interface
                    244:         $r->print(&advanced_search_form($closebutton,$hidden));
                    245:     } else { 
                    246:         # Output normal search interface
                    247:         $r->print(&basic_search_form($closebutton,$hidden));
                    248:     }
                    249:     return OK;
                    250: } 
1.98      harris41  251: 
1.124     matthew   252: ######################################################################
                    253: ######################################################################
                    254: 
                    255: =pod 
                    256: 
                    257: =item &basic_search_form() 
                    258: 
                    259: Returns a scalar which holds html for the basic search form.
                    260: 
                    261: =cut
                    262: 
                    263: ######################################################################
                    264: ######################################################################
1.3       harris41  265: 
1.124     matthew   266: sub basic_search_form{
                    267:     my ($closebutton,$hidden) = @_;
                    268:     my $scrout=<<"ENDDOCUMENT";
                    269: <html>
                    270: <head>
                    271: <title>The LearningOnline Network with CAPA</title>
                    272: <script type="text/javascript">
                    273:     function openhelp(val) {
                    274: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    275: 	     'scrollbars=1,width=600,height=300');
                    276: 	openhelpwin.focus();
1.6       harris41  277:     }
1.124     matthew   278: </script>
                    279: </head>
                    280: <body bgcolor="#FFFFFF">
                    281: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
                    282: <h1>Search Catalog</h1>
                    283: <form method="post" action="/adm/searchcat">
                    284: $hidden
                    285: <h3>Basic Search</h3>
                    286: <p>
1.131     matthew   287: Enter terms or phrases separated by AND, OR, or NOT 
1.129     matthew   288: then press SEARCH below.
1.124     matthew   289: </p>
                    290: <p>
                    291: <table>
                    292: <tr><td>
                    293: ENDDOCUMENT
                    294:     $scrout.='&nbsp;'.&simpletextfield('basicexp',$ENV{'form.basicexp'},40).
                    295:         '&nbsp;';
                    296: #    $scrout.=&simplecheckbox('allversions',$ENV{'form.allversions'});
                    297: #    $scrout.='<font color="#800000">Search historic archives</font>';
                    298:     $scrout.=<<ENDDOCUMENT;
                    299: </td><td><a href="/adm/searchcat?reqinterface=advanced">Advanced Search</a></td></tr></table>
                    300: </p>
                    301: <p>
                    302: &nbsp;<input type="submit" name="basicsubmit" value='SEARCH' />&nbsp;
                    303: $closebutton
1.130     matthew   304: <!-- view selection -->
                    305: <select name="viewselect">
                    306: <option value="Detailed Citation View" selected="true" >Detailed Citation View</option>
                    307: <option value="Summary View"  >Summary View</option>
                    308: <option value="Fielded Format">Fielded Format</option>
                    309: <option value="XML/SGML"      >XML/SGML</option>
1.124     matthew   310: </select>
1.130     matthew   311: <!-- end of view selection -->
1.124     matthew   312: <input type="button" value="HELP" onClick="openhelp()" />
                    313: </p>
                    314: </form>
                    315: </body>
                    316: </html>
                    317: ENDDOCUMENT
                    318:     return $scrout;
                    319: }
                    320: ######################################################################
                    321: ######################################################################
                    322: 
                    323: =pod 
                    324: 
                    325: =item &advanced_search_form() 
                    326: 
                    327: Returns a scalar which holds html for the advanced search form.
                    328: 
                    329: =cut
                    330: 
                    331: ######################################################################
                    332: ######################################################################
                    333: 
                    334: sub advanced_search_form{
                    335:     my ($closebutton,$hidden) = @_;
1.129     matthew   336:     my $advanced_buttons = <<"END";
                    337: <p>
                    338: <input type="submit" name="advancedsubmit" value='SEARCH' />
                    339: <input type="reset" name="reset" value='RESET' />
                    340: $closebutton
                    341: <input type="button" value="HELP" onClick="openhelp()" />
                    342: </p>
                    343: END
1.124     matthew   344:     my $scrout=<<"ENDHEADER";
                    345: <html>
                    346: <head>
                    347: <title>The LearningOnline Network with CAPA</title>
                    348: <script type="text/javascript">
                    349:     function openhelp(val) {
                    350: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                    351: 	     'scrollbars=1,width=600,height=300');
                    352: 	openhelpwin.focus();
1.18      harris41  353:     }
1.124     matthew   354: </script>
                    355: </head>
                    356: <body bgcolor="#FFFFFF">
                    357: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1.129     matthew   358: <h1>Advanced Catalog Search</h1>
                    359: <hr />
1.131     matthew   360: Enter terms or phrases separated by search operators 
1.129     matthew   361: such as AND, OR, or NOT.<br />
1.130     matthew   362: <form method="post" action="/adm/searchcat">
1.129     matthew   363: $advanced_buttons
1.124     matthew   364: $hidden
1.129     matthew   365: <table>
1.130     matthew   366: <tr><td><font color="#800000" face="helvetica"><b>VIEW:</b></font></td>
                    367: <td>
                    368: <!-- view selection -->
                    369: <select name="viewselect" size ="1">
                    370:     <option value="Detailed Citation View" selected="true">Detailed Citation View</option>
                    371:     <option value="Summary View">Summary View</option>
                    372:     <option value="Fielded Format">Fielded Format</option>
                    373:     <option value="XML/SGML">XML/SGML</option>
                    374: </select>
                    375: <!-- end of view selection -->
                    376: </td></tr>
1.124     matthew   377: ENDHEADER
1.129     matthew   378:     $scrout.=&searchphrasefield('title','title',
1.11      harris41  379: 			$ENV{'form.title'});
1.129     matthew   380:     $scrout.=&searchphrasefield('author','author',
1.11      harris41  381: 			$ENV{'form.author'});
1.129     matthew   382:     $scrout.=&searchphrasefield('subject','subject',
1.11      harris41  383: 			$ENV{'form.subject'});
1.129     matthew   384:     $scrout.=&searchphrasefield('keywords','keywords',
1.11      harris41  385: 			$ENV{'form.keywords'});
1.129     matthew   386:     $scrout.=&searchphrasefield('URL','url',
1.11      harris41  387: 			$ENV{'form.url'});
1.96      harris41  388: #    $scrout.=&searchphrasefield('Limit by version','version',
                    389: #			$ENV{'form.version'});
1.129     matthew   390:     $scrout.=&searchphrasefield('notes','notes',
1.11      harris41  391: 			$ENV{'form.notes'});
1.129     matthew   392:     $scrout.=&searchphrasefield('abstract','abstract',
1.11      harris41  393: 			$ENV{'form.abstract'});
1.129     matthew   394:     # Hack - an empty table row.
                    395:     $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
                    396:     $scrout.=&searchphrasefield('file<br />extension','mime',
                    397:                         $ENV{'form.mime'});
                    398:     $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
                    399:     $scrout.=&searchphrasefield('publisher<br />owner','owner',
                    400: 				$ENV{'form.owner'});
                    401:     $scrout.="</table>\n";
1.131     matthew   402:     $ENV{'form.category'}='any' unless length($ENV{'form.category'});
1.132     matthew   403:     $scrout.=&selectbox('File Category','category',
1.131     matthew   404: 			$ENV{'form.category'},
                    405: 			'any','Any category',
                    406: 			undef,
                    407: 			(&Apache::loncommon::filecategories()));
1.11      harris41  408:     $ENV{'form.language'}='any' unless length($ENV{'form.language'});
1.133     matthew   409:     #----------------------------------------------------------------
1.132     matthew   410:     # Allow restriction to multiple domains.
                    411:     #   I make the crazy assumption that there will never be a domain 'any'.
                    412:     #
1.133     matthew   413:     $ENV{'form.domains'} = 'any' if (! exists($ENV{'form.domains'}));
                    414:     my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} 
                    415:                            :  ($ENV{'form.domains'}) );
                    416:     my %domain_hash = ();
                    417:     foreach (@allowed_domains) {
                    418:         $domain_hash{$_}++;
                    419:     }
1.132     matthew   420:     my @domains =&Apache::loncommon::get_domains();
                    421:     # adjust the size of the select box
                    422:     my $size = 4;
                    423:     my $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size);
                    424:     # standalone machines do not get to choose a domain to search.
                    425:     if ((scalar @domains) == 1) {
                    426:         $scrout .='<input type="hidden" name="domains" value="any" />'."\n";
                    427:     } else {
1.133     matthew   428:         $scrout.="\n".'<font color="#800000" face="helvetica"><b>'.
1.132     matthew   429:             'DOMAINS</b></font><br />'.
                    430:                 '<select name="domains" size="'.$size.'" multiple>'."\n".
1.133     matthew   431:                     '<option name="any" value="any" '.
                    432:                         ($domain_hash{'any'}? 'selected ' :'').
                    433:                         '>all domains</option>'."\n";
1.132     matthew   434:         foreach my $dom (sort @domains) {
1.133     matthew   435:             $scrout.="<option name=\"$dom\" ".
                    436:                 ($domain_hash{$dom} ? 'selected ' :'').">$dom</option>\n";
1.132     matthew   437:         }
                    438:         $scrout.="</select>\n";
                    439:     }
1.133     matthew   440:     #----------------------------------------------------------------
1.132     matthew   441:     # 
                    442:     #
1.3       harris41  443:     $scrout.=&selectbox('Limit by language','language',
1.111     harris41  444: 			$ENV{'form.language'},'any','Any Language',
                    445: 			\&{Apache::loncommon::languagedescription},
                    446: 			(&Apache::loncommon::languageids),
                    447: 			);
1.8       harris41  448: # ------------------------------------------------ Compute date selection boxes
                    449:     $scrout.=<<CREATIONDATESTART;
1.3       harris41  450: <p>
                    451: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
                    452: </font>
1.98      harris41  453: <br />
1.8       harris41  454: between:
                    455: CREATIONDATESTART
1.11      harris41  456:     $scrout.=&dateboxes('creationdatestart',1,1,1976,
                    457: 			$ENV{'form.creationdatestart_month'},
                    458: 			$ENV{'form.creationdatestart_day'},
                    459: 			$ENV{'form.creationdatestart_year'},
                    460: 			);
1.124     matthew   461:     $scrout.="and:\n";
1.11      harris41  462:     $scrout.=&dateboxes('creationdateend',12,31,2051,
                    463: 			$ENV{'form.creationdateend_month'},
                    464: 			$ENV{'form.creationdateend_day'},
                    465: 			$ENV{'form.creationdateend_year'},
                    466: 			);
1.8       harris41  467:     $scrout.="</p>";
                    468:     $scrout.=<<LASTREVISIONDATESTART;
                    469: <p>
                    470: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
                    471: </b></font>
1.98      harris41  472: <br />between:
1.8       harris41  473: LASTREVISIONDATESTART
1.11      harris41  474:     $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
                    475: 			$ENV{'form.lastrevisiondatestart_month'},
                    476: 			$ENV{'form.lastrevisiondatestart_day'},
                    477: 			$ENV{'form.lastrevisiondatestart_year'},
                    478: 			);
1.8       harris41  479:     $scrout.=<<LASTREVISIONDATEEND;
                    480: and:
                    481: LASTREVISIONDATEEND
1.11      harris41  482:     $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
                    483: 			$ENV{'form.lastrevisiondateend_month'},
                    484: 			$ENV{'form.lastrevisiondateend_day'},
                    485: 			$ENV{'form.lastrevisiondateend_year'},
                    486: 			);
1.8       harris41  487:     $scrout.='</p>';
1.11      harris41  488:     $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
1.8       harris41  489:     $scrout.=&selectbox('Limit by copyright/distribution','copyright',
1.111     harris41  490: 			 $ENV{'form.copyright'},
                    491: 			 'any','Any copyright/distribution',
                    492: 			 \&{Apache::loncommon::copyrightdescription},
                    493: 			 (&Apache::loncommon::copyrightids),
                    494: 			 );
1.14      harris41  495: # ------------------------------------------- Compute customized metadata field
                    496:     $scrout.=<<CUSTOMMETADATA;
                    497: <p>
1.77      harris41  498: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
1.14      harris41  499: </font>
1.77      harris41  500: For resource-specific metadata, enter in an expression in the form of 
1.100     harris41  501: <i>key</i>=<i>value</i> separated by operators such as AND, OR or NOT.<br />
1.14      harris41  502: <b>Example:</b> grandmother=75 OR grandfather=85
1.98      harris41  503: <br />
1.14      harris41  504: CUSTOMMETADATA
1.124     matthew   505:     $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
1.77      harris41  506:     $scrout.=<<CUSTOMSHOW;
                    507: <p>
                    508: <font color="#800000" face="helvetica"><b>SHOW SPECIAL METADATA FIELDS:</b>
                    509: </font>
                    510: Enter in a space-separated list of special metadata fields to show
                    511: in a fielded listing for each record result.
1.98      harris41  512: <br />
1.77      harris41  513: CUSTOMSHOW
1.124     matthew   514:     $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'});
                    515:     $scrout.=<<ENDDOCUMENT;
1.129     matthew   516: $advanced_buttons
1.8       harris41  517: </form>
                    518: </body>
                    519: </html>
                    520: ENDDOCUMENT
1.124     matthew   521:     return $scrout;
                    522: }
1.8       harris41  523: 
1.121     matthew   524: ######################################################################
                    525: ######################################################################
                    526: 
                    527: =pod 
                    528: 
                    529: =item &make_persistent() 
                    530: 
                    531: Returns a scalar which holds the current ENV{'form.*'} values in
1.126     matthew   532: a 'hidden' html input tag.  This allows search interface information
                    533: to be somewhat persistent.
1.122     matthew   534: 
1.121     matthew   535: =cut
                    536: 
                    537: ######################################################################
                    538: ######################################################################
                    539: 
1.98      harris41  540: sub make_persistent {
1.133     matthew   541:     my %save = %{shift()};
1.98      harris41  542:     my $persistent='';
1.133     matthew   543:     foreach (keys %save) {
1.98      harris41  544: 	if (/^form\./ && !/submit/) {
                    545: 	    my $name=$_;
1.133     matthew   546:             my @values = (ref($save{$name}) ? @{$save{$name}} : ($save{$name}));
1.98      harris41  547: 	    $name=~s/^form\.//;
1.133     matthew   548:             foreach (@values) {
                    549:                 s/\"/\'/g; # do not mess with html field syntax
                    550:                 $persistent.=<<END;
                    551: <input type="hidden" name="$name" value="$_" />
1.98      harris41  552: END
1.133     matthew   553:             }
1.98      harris41  554:         }
1.109     harris41  555:     }
1.98      harris41  556:     return $persistent;
                    557: }
                    558: 
1.122     matthew   559: 
                    560: ######################################################################
                    561: ######################################################################
                    562: 
                    563: =pod 
                    564: 
                    565: =item HTML form building functions
                    566: 
                    567: =over 4
                    568: 
                    569: =item &simpletextfield() 
                    570: 
                    571: Inputs: $name,$value,$size
                    572: 
                    573: Returns a text input field with the given name, value, and size.  
                    574: If size is not specified, a value of 20 is used.
                    575: 
                    576: =item &simplecheckbox()
                    577: 
                    578: Inputs: $name,$value
                    579: 
                    580: Returns a simple check box with the given $name.
                    581: If $value eq 'on' the box is checked.
                    582: 
                    583: =item &searchphrasefield()
                    584: 
                    585: Inputs: $title,$name,$value
                    586: 
                    587: Returns html for a title line and an input field for entering search terms.
                    588: the instructions "Enter terms or phrases separated by search operators such 
                    589: as AND, OR, or NOT." are given following the title.  The entry field (which
                    590: is where the $name and $value are used) is an 80 column simpletextfield.
                    591: 
                    592: =item &dateboxes()
                    593: 
1.126     matthew   594: Returns html selection form elements for the specification of 
                    595: the day, month, and year.
                    596: 
1.122     matthew   597: =item &selectbox()
                    598: 
1.129     matthew   599: Returns a scalar containing an html <select> form.  
                    600: 
                    601: Inputs: 
                    602: 
                    603: =over 4
                    604: 
                    605: =item $title 
                    606: 
                    607: Printed above the select box, in uppercase.
                    608: 
                    609: =item $name 
                    610: 
                    611: The name element of the <select> tag.
                    612: 
                    613: =item $default 
                    614: 
                    615: The default value of the form.  Can be $anyvalue or in @idlist.
                    616: 
                    617: =item $anyvalue 
                    618: 
                    619: The <option value="..."> used to indicate a default of 
                    620: none of the values.
                    621: 
                    622: =item $anytag 
                    623: 
                    624: The text associate with $anyvalue above.
                    625: 
                    626: =item $functionref 
                    627: 
                    628: Each element in @idlist will be passed as a parameter 
                    629: to the function referenced here.  The return value of the function should
                    630: be a scalar description of the items.  If this value is undefined the 
                    631: description of each item in @idlist will be the item name.
                    632: 
                    633: =item @idlist 
                    634: 
                    635: The items to be selected from.  One of these or $anyvalue will be the 
                    636: value returned by the form element, $ENV{form.$name}.
                    637: 
                    638: =back
1.126     matthew   639: 
1.122     matthew   640: =back 
                    641: 
                    642: =cut
                    643: 
                    644: ######################################################################
                    645: ######################################################################
1.8       harris41  646: 
1.11      harris41  647: sub simpletextfield {
1.122     matthew   648:     my ($name,$value,$size)=@_;
                    649:     $size = 20 if (! defined($size));
                    650:     return '<input type="text" name="'.$name.
                    651:         '" size="'.$size.'" value="'.$value.'" />';
1.11      harris41  652: }
                    653: 
                    654: sub simplecheckbox {
                    655:     my ($name,$value)=@_;
                    656:     my $checked='';
1.129     matthew   657:     $checked="checked" if $value eq 'on';
1.122     matthew   658:     return '<input type="checkbox" name="'.$name.'" '. $checked . ' />';
1.11      harris41  659: }
                    660: 
1.8       harris41  661: sub searchphrasefield {
                    662:     my ($title,$name,$value)=@_;
                    663:     my $uctitle=uc($title);
1.129     matthew   664:     return '<tr><td><font color="#800000" face="helvetica">'.
                    665:         '<b>'.$uctitle.':&nbsp;</b></font></td><td>'.
                    666:                 &simpletextfield($name,$value,50)."</td></tr>\n";
1.8       harris41  667: }
1.3       harris41  668: 
1.8       harris41  669: sub dateboxes {
1.11      harris41  670:     my ($name,$defaultmonth,$defaultday,$defaultyear,
                    671: 	$currentmonth,$currentday,$currentyear)=@_;
                    672:     ($defaultmonth,$defaultday,$defaultyear)=('','','');
1.117     matthew   673:     #
                    674:     # Day
                    675:     my $day=<<END;
                    676: <select name="${name}_day">
                    677: <option value='$defaultday'> </option>
                    678: END
                    679:     for (my $i = 1; $i<=31; $i++) {
                    680: 	$day.="<option value=\"$i\">$i</option>\n";
                    681:     }
                    682:     $day.="</select>\n";
                    683:     $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
                    684:     #
                    685:     # Month
1.11      harris41  686:     my $month=<<END;
1.8       harris41  687: <select name="${name}_month">
1.11      harris41  688: <option value='$defaultmonth'> </option>
                    689: END
1.117     matthew   690:     my $i = 1;
                    691:     foreach (qw/January February March April May June 
                    692: 	     July August September October November December /){
                    693: 	$month .="<option value=\"$i\">$_</option>\n";
                    694: 	$i++;
                    695:     }
                    696:     $month.="</select>\n";
1.11      harris41  697:     $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
1.117     matthew   698:     #
                    699:     # Year (obviously)
1.11      harris41  700:     my $year=<<END;
1.8       harris41  701: <select name="${name}_year">
1.11      harris41  702: <option value='$defaultyear'> </option>
1.3       harris41  703: END
1.117     matthew   704:     my $maxyear = 2051; 
                    705:     for (my $i = 1976; $i<=$maxyear; $i++) {
                    706: 	$year.="<option value=\"$i\">$i</option>\n";
                    707:     }
                    708:     $year.="</select>\n";
1.11      harris41  709:     $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
                    710:     return "$month$day$year";
1.3       harris41  711: }
                    712: 
                    713: sub selectbox {
1.129     matthew   714:     my ($title,$name,$default,$anyvalue,$anytag,$functionref,@idlist)=@_;
                    715:     if (! defined($functionref)) { $functionref = sub { $_[0]}; }
1.3       harris41  716:     my $uctitle=uc($title);
1.122     matthew   717:     my $selout="\n".'<p><font color="#800000" face="helvetica">'.
1.131     matthew   718:         '<b>'.$uctitle.': </b></font><select name="'.$name.'">';
1.111     harris41  719:     foreach ($anyvalue,@idlist) {
1.122     matthew   720:         $selout.='<option value="'.$_.'"';
1.129     matthew   721:         if ($_ eq $default and !/^any$/) {
1.122     matthew   722: 	    $selout.=' selected >'.&{$functionref}($_).'</option>';
1.111     harris41  723: 	}
1.129     matthew   724: 	elsif ($_ eq $default and /^$anyvalue$/) {
1.122     matthew   725: 	    $selout.=' selected >'.$anytag.'</option>';
1.111     harris41  726: 	}
                    727:         else {$selout.='>'.&{$functionref}($_).'</option>';}
1.109     harris41  728:     }
1.129     matthew   729:     return $selout.'</select></p>';
1.6       harris41  730: }
                    731: 
1.122     matthew   732: ######################################################################
                    733: ######################################################################
                    734: 
                    735: =pod 
                    736: 
1.134   ! matthew   737: =item &parse_advanced_search()
        !           738: 
        !           739: Parse advanced search form and return the following:
        !           740: 
        !           741: =over 4
        !           742: 
        !           743: =item $query Scalar containing an SQL query.
1.126     matthew   744: 
1.134   ! matthew   745: =item $customquery Scalar containing a custom query.
        !           746: 
        !           747: =item $customshow Scalar containing commands to show custom metadata.
        !           748: 
        !           749: =item $libraries_to_query Reference to array of domains to search.
        !           750: 
        !           751: =back
1.122     matthew   752: 
                    753: =cut
                    754: 
                    755: ######################################################################
                    756: ######################################################################
1.134   ! matthew   757: sub parse_advanced_search {
        !           758:     my ($r)=@_;
1.32      harris41  759:     my $fillflag=0;
1.64      harris41  760:     # Clean up fields for safety
                    761:     for my $field ('title','author','subject','keywords','url','version',
                    762: 		   'creationdatestart_month','creationdatestart_day',
                    763: 		   'creationdatestart_year','creationdateend_month',
                    764: 		   'creationdateend_day','creationdateend_year',
                    765: 		   'lastrevisiondatestart_month','lastrevisiondatestart_day',
                    766: 		   'lastrevisiondatestart_year','lastrevisiondateend_month',
                    767: 		   'lastrevisiondateend_day','lastrevisiondateend_year',
                    768: 		   'notes','abstract','mime','language','owner',
1.131     matthew   769: 		   'custommetadata','customshow','category') {
1.101     harris41  770: 	$ENV{"form.$field"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
1.64      harris41  771:     }
1.117     matthew   772:     foreach ('mode','form','element') {
                    773: 	# is this required?  Hmmm.
                    774: 	next unless (exists($ENV{"form.$_"}));
                    775: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                    776: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                    777:     }
1.131     matthew   778:     # Preprocess the category form element.
                    779:     if ($ENV{'form.category'} ne 'any') {
                    780:         my @extensions = &Apache::loncommon::filecategorytypes
                    781:             ($ENV{'form.category'});
                    782:         $ENV{'form.mime'} = join ' OR ',@extensions;
                    783:     }
1.90      harris41  784:     # Check to see if enough information was filled in
1.32      harris41  785:     for my $field ('title','author','subject','keywords','url','version',
                    786: 		   'notes','abstract','mime','language','owner',
                    787: 		   'custommetadata') {
1.40      harris41  788: 	if (&filled($ENV{"form.$field"})) {
1.32      harris41  789: 	    $fillflag++;
                    790: 	}
                    791:     }
                    792:     unless ($fillflag) {
                    793: 	&output_blank_field_error($r);
1.134   ! matthew   794: 	return ;
1.32      harris41  795:     }
1.90      harris41  796:     # Turn the form input into a SQL-based query
1.39      harris41  797:     my $query='';
1.45      harris41  798:     my @queries;
1.90      harris41  799:     # Evaluate logical expression AND/OR/NOT phrase fields.
1.58      harris41  800:     foreach my $field ('title','author','subject','notes','abstract','url',
1.129     matthew   801: 		       'keywords','version','owner','mime') {
1.44      harris41  802: 	if ($ENV{'form.'.$field}) {
1.45      harris41  803: 	    push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
1.131     matthew   804:         }
1.44      harris41  805:     }
1.90      harris41  806:     # Evaluate option lists
1.58      harris41  807:     if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
1.90      harris41  808: 	push @queries,"(language like \"$ENV{'form.language'}\")";
1.58      harris41  809:     }
                    810:     if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
1.90      harris41  811: 	push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
1.58      harris41  812:     }
1.90      harris41  813:     # Evaluate date windows
1.60      harris41  814:     my $datequery=&build_date_queries(
                    815: 			$ENV{'form.creationdatestart_month'},
                    816: 			$ENV{'form.creationdatestart_day'},
                    817: 			$ENV{'form.creationdatestart_year'},
                    818: 			$ENV{'form.creationdateend_month'},
                    819: 			$ENV{'form.creationdateend_day'},
                    820: 			$ENV{'form.creationdateend_year'},
                    821: 			$ENV{'form.lastrevisiondatestart_month'},
                    822: 			$ENV{'form.lastrevisiondatestart_day'},
                    823: 			$ENV{'form.lastrevisiondatestart_year'},
                    824: 			$ENV{'form.lastrevisiondateend_month'},
                    825: 			$ENV{'form.lastrevisiondateend_day'},
                    826: 			$ENV{'form.lastrevisiondateend_year'},
                    827: 			);
1.90      harris41  828:     # Test to see if date windows are legitimate
1.61      harris41  829:     if ($datequery=~/^Incorrect/) {
                    830: 	&output_date_error($r,$datequery);
1.134   ! matthew   831: 	return ;
1.61      harris41  832:     }
                    833:     elsif ($datequery) {
1.60      harris41  834: 	push @queries,$datequery;
                    835:     }
1.90      harris41  836:     # Process form information for custom metadata querying
1.134   ! matthew   837:     my $customquery=undef;
1.64      harris41  838:     if ($ENV{'form.custommetadata'}) {
                    839: 	$customquery=&build_custommetadata_query('custommetadata',
                    840: 				      $ENV{'form.custommetadata'});
                    841:     }
1.134   ! matthew   842:     my $customshow=undef;
1.83      harris41  843:     if ($ENV{'form.customshow'}) {
                    844: 	$customshow=$ENV{'form.customshow'};
                    845: 	$customshow=~s/[^\w\s]//g;
                    846: 	my @fields=split(/\s+/,$customshow);
                    847: 	$customshow=join(" ",@fields);
                    848:     }
1.133     matthew   849:     ## ---------------------------------------------------------------
1.132     matthew   850:     ## Deal with restrictions to given domains
                    851:     ## 
                    852:     my $libraries_to_query = undef;
                    853:     # $ENV{'form.domains'} can be either a scalar or an array reference.
                    854:     # We need an array.
                    855:     my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} 
                    856:                            :  ($ENV{'form.domains'}) );
                    857:     my %domain_hash = ();
                    858:     foreach (@allowed_domains) {
                    859:         $domain_hash{$_}++;
                    860:     }
                    861:     foreach (keys(%Apache::lonnet::libserv)) {
                    862:         if ($_ eq 'any') {
                    863:             $libraries_to_query = undef;
                    864:             last;
                    865:         }
                    866:         if (exists($domain_hash{$Apache::lonnet::hostdom{$_}})) {
                    867:             push @$libraries_to_query,$_;
                    868:         }
                    869:     }
                    870:     #
1.45      harris41  871:     if (@queries) {
1.58      harris41  872: 	$query=join(" AND ",@queries);
1.46      harris41  873: 	$query="select * from metadata where $query";
1.126     matthew   874:     } elsif ($customquery) {
1.134   ! matthew   875:         $query = '';
1.45      harris41  876:     }
1.134   ! matthew   877:     return ($query,$customquery,$customshow,$libraries_to_query);
1.18      harris41  878: }
                    879: 
1.122     matthew   880: ######################################################################
                    881: ######################################################################
                    882: 
                    883: =pod 
                    884: 
1.134   ! matthew   885: =item &parse_basic_search() 
1.122     matthew   886: 
1.134   ! matthew   887: Parse the basic search form and return a scalar containing an sql query.
1.126     matthew   888: 
1.122     matthew   889: =cut
                    890: 
                    891: ######################################################################
                    892: ######################################################################
1.134   ! matthew   893: sub parse_basic_search {
        !           894:     my ($r)=@_;
1.64      harris41  895:     # Clean up fields for safety
                    896:     for my $field ('basicexp') {
                    897: 	$ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
                    898:     }
1.117     matthew   899:     foreach ('mode','form','element') {
                    900: 	# is this required?  Hmmm.
                    901: 	next unless (exists($ENV{"form.$_"}));
                    902: 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
                    903: 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
                    904:     }
1.64      harris41  905: 
1.90      harris41  906:     # Check to see if enough is filled in
1.26      harris41  907:     unless (&filled($ENV{'form.basicexp'})) {
1.24      harris41  908: 	&output_blank_field_error($r);
                    909: 	return OK;
                    910:     }
1.22      harris41  911: 
1.90      harris41  912:     # Build SQL query string based on form page
1.39      harris41  913:     my $query='';
1.33      harris41  914:     my $concatarg=join(',"    ",',
1.124     matthew   915: 		       ('title', 'author', 'subject', 'notes', 'abstract',
                    916:                         'keywords'));
1.95      harris41  917:     $concatarg='title' if $ENV{'form.titleonly'};
1.94      harris41  918: 
                    919:     $query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
1.134   ! matthew   920:     return 'select * from metadata where '.$query;
1.22      harris41  921: }
                    922: 
1.122     matthew   923: 
                    924: ######################################################################
                    925: ######################################################################
                    926: 
                    927: =pod 
                    928: 
                    929: =item &build_SQL_query() 
                    930: 
1.126     matthew   931: Builds a SQL query string from a logical expression with AND/OR keywords
                    932: using Text::Query and &recursive_SQL_query_builder()
                    933: 
1.122     matthew   934: =cut
                    935: 
                    936: ######################################################################
                    937: ######################################################################
1.98      harris41  938: sub build_SQL_query {
                    939:     my ($field_name,$logic_statement)=@_;
                    940:     my $q=new Text::Query('abc',
                    941: 			  -parse => 'Text::Query::ParseAdvanced',
                    942: 			  -build => 'Text::Query::Build');
                    943:     $q->prepare($logic_statement);
                    944:     my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
                    945:     my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
                    946:     return $sql_query;
                    947: }
                    948: 
1.122     matthew   949: ######################################################################
                    950: ######################################################################
                    951: 
                    952: =pod 
                    953: 
                    954: =item &build_custommetadata_query() 
                    955: 
1.126     matthew   956: Constructs a custom metadata query using a rather heinous regular
                    957: expression.
                    958: 
1.122     matthew   959: =cut
                    960: 
                    961: ######################################################################
                    962: ######################################################################
1.98      harris41  963: sub build_custommetadata_query {
                    964:     my ($field_name,$logic_statement)=@_;
                    965:     my $q=new Text::Query('abc',
                    966: 			  -parse => 'Text::Query::ParseAdvanced',
                    967: 			  -build => 'Text::Query::BuildAdvancedString');
                    968:     $q->prepare($logic_statement);
                    969:     my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
                    970:     # quick fix to change literal into xml tag-matching
                    971:     # will eventually have to write a separate builder module
1.122     matthew   972:     # wordone=wordtwo becomes\<wordone\>[^\<] *wordtwo[^\<]*\<\/wordone\>
                    973:     $matchexp =~ s/(\w+)\\=([\w\\\+]+)?# wordone=wordtwo is changed to 
                    974:                  /\\<$1\\>?#           \<wordone\>
                    975:                    \[\^\\<\]?#        [^\<]         
                    976:                    \*$2\[\^\\<\]?#           *wordtwo[^\<]
                    977:                    \*\\<\\\/$1\\>?#                        *\<\/wordone\>
                    978:                    /g;
1.98      harris41  979:     return $matchexp;
                    980: }
                    981: 
1.122     matthew   982: ######################################################################
                    983: ######################################################################
                    984: 
                    985: =pod 
                    986: 
                    987: =item &recursive_SQL_query_build() 
                    988: 
1.126     matthew   989: Recursively constructs an SQL query.  Takes as input $dkey and $pattern.
                    990: 
1.122     matthew   991: =cut
                    992: 
                    993: ######################################################################
                    994: ######################################################################
1.98      harris41  995: sub recursive_SQL_query_build {
                    996:     my ($dkey,$pattern)=@_;
                    997:     my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
                    998:     return $pattern unless @matches;
                    999:     foreach my $match (@matches) {
                   1000: 	$match=~/\[ (\w+)\s(.*) \]/;
                   1001: 	my ($key,$value)=($1,$2);
                   1002: 	my $replacement='';
                   1003: 	if ($key eq 'literal') {
                   1004: 	    $replacement="($dkey like \"\%$value\%\")";
                   1005: 	}
                   1006: 	elsif ($key eq 'not') {
                   1007: 	    $value=~s/like/not like/;
                   1008: #	    $replacement="($dkey not like $value)";
                   1009: 	    $replacement="$value";
                   1010: 	}
                   1011: 	elsif ($key eq 'and') {
                   1012: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1013: 	    $replacement="($1 AND $2)";
                   1014: 	}
                   1015: 	elsif ($key eq 'or') {
                   1016: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
                   1017: 	    $replacement="($1 OR $2)";
                   1018: 	}
                   1019: 	substr($pattern,
                   1020: 	       index($pattern,$match),
                   1021: 	       length($match),
                   1022: 	       $replacement
                   1023: 	       );
                   1024:     }
                   1025:     &recursive_SQL_query_build($dkey,$pattern);
                   1026: }
1.22      harris41 1027: 
1.122     matthew  1028: ######################################################################
                   1029: ######################################################################
                   1030: 
                   1031: =pod 
                   1032: 
                   1033: =item &build_date_queries() 
                   1034: 
1.126     matthew  1035: Builds a SQL logic query to check time/date entries.
                   1036: Also reports errors (check for /^Incorrect/).
                   1037: 
1.122     matthew  1038: =cut
                   1039: 
                   1040: ######################################################################
                   1041: ######################################################################
1.98      harris41 1042: sub build_date_queries {
                   1043:     my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
                   1044: 	$lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
                   1045:     my @queries;
                   1046:     if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
                   1047: 	unless ($cmonth1 and $cday1 and $cyear1 and
                   1048: 		$cmonth2 and $cday2 and $cyear2) {
                   1049: 	    return "Incorrect entry for the creation date.  You must specify ".
                   1050: 		   "a starting month, day, and year and an ending month, ".
                   1051: 		   "day, and year.";
                   1052: 	}
                   1053: 	my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
                   1054: 	$cnumeric1+=0;
                   1055: 	my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
                   1056: 	$cnumeric2+=0;
                   1057: 	if ($cnumeric1>$cnumeric2) {
                   1058: 	    return "Incorrect entry for the creation date.  The starting ".
                   1059: 		   "date must occur before the ending date.";
                   1060: 	}
                   1061: 	my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
                   1062: 	           "$cyear2-$cmonth2-$cday2 23:59:59')";
                   1063: 	push @queries,$cquery;
                   1064:     }
                   1065:     if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
                   1066: 	unless ($lmonth1 and $lday1 and $lyear1 and
                   1067: 		$lmonth2 and $lday2 and $lyear2) {
                   1068: 	    return "Incorrect entry for the last revision date.  You must ".
                   1069: 		   "specify a starting month, day, and year and an ending ".
                   1070: 		   "month, day, and year.";
                   1071: 	}
                   1072: 	my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
                   1073: 	$lnumeric1+=0;
                   1074: 	my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
                   1075: 	$lnumeric2+=0;
                   1076: 	if ($lnumeric1>$lnumeric2) {
                   1077: 	    return "Incorrect entry for the last revision date.  The ".
                   1078: 		   "starting date must occur before the ending date.";
                   1079: 	}
                   1080: 	my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
                   1081: 	           "$lyear2-$lmonth2-$lday2 23:59:59')";
                   1082: 	push @queries,$lquery;
                   1083:     }
                   1084:     if (@queries) {
                   1085: 	return join(" AND ",@queries);
                   1086:     }
                   1087:     return '';
1.18      harris41 1088: }
1.6       harris41 1089: 
1.122     matthew  1090: ######################################################################
                   1091: ######################################################################
                   1092: 
                   1093: =pod 
                   1094: 
                   1095: =item &output_results() 
                   1096: 
                   1097: Format and output results based on a reply list.
                   1098: There are two windows that this function writes to.  The main search
                   1099: window ("srch") has a listing of the results.  A secondary window ("popwin")
                   1100: gives the status of the network search (time elapsed, number of machines
                   1101: contacted, etc.)
                   1102: 
                   1103: =cut
                   1104: 
                   1105: ######################################################################
                   1106: ######################################################################
1.18      harris41 1107: sub output_results {
1.126     matthew  1108: #    &Apache::lonnet::logthis("output_results:".time);
1.101     harris41 1109:     my $fnum; # search result counter
1.134   ! matthew  1110:     my ($mode,$r,$replyref,$hidden)=@_;
1.92      harris41 1111:     my %rhash=%{$replyref};
1.44      harris41 1112:     my $compiledresult='';
1.125     matthew  1113:     my $timeremain=300; # (seconds)
1.98      harris41 1114:     my $elapsetime=0;
1.93      harris41 1115:     my $resultflag=0;
                   1116:     my $tflag=1;
1.130     matthew  1117:     my $viewselect=$ENV{'form.viewselect'};
1.125     matthew  1118:     #
1.93      harris41 1119:     # make query information persistent to allow for subsequent revision
1.133     matthew  1120:     my $persistent=&make_persistent(\%ENV);
1.130     matthew  1121:     # spit out the results header
                   1122:     $r->print(&search_results_header($mode));
1.98      harris41 1123:     $r->rflush();
                   1124:     # begin showing the cataloged results
1.129     matthew  1125:     my $action = "/adm/searchcat";
                   1126:     if ($mode eq 'Basic') { 
                   1127:         $action .= "?reqinterface=basic";
                   1128:     } elsif ($mode eq 'Advanced') {
                   1129:         $action .= "?reqinterface=advanced";
                   1130:     }
                   1131:     $r->print(<<CATALOGCONTROLS);
                   1132: <form name='results' method="post" action="$action">
1.118     www      1133: $hidden
1.98      harris41 1134: <input type='hidden' name='acts' value='' />
1.93      harris41 1135: <input type='button' value='Revise search request'
1.98      harris41 1136: onClick='this.form.submit();' />
                   1137: $importbutton
1.93      harris41 1138: $closebutton
                   1139: $persistent
1.98      harris41 1140: <hr />
                   1141: CATALOGCONTROLS
1.125     matthew  1142:     #
                   1143:     # make the pop-up window for status
                   1144:     #
                   1145:     $r->print(&make_popwin(%rhash));
1.92      harris41 1146:     $r->rflush();
1.125     matthew  1147:     ##
                   1148:     ## Prepare for the main loop below
                   1149:     ##
1.93      harris41 1150:     my $servercount=0;
1.98      harris41 1151:     my $hitcountsum=0;
1.125     matthew  1152:     my $servernum=(keys %rhash);
                   1153:     my $serversleft=$servernum;
                   1154:     ##
                   1155:     ## Run until we run out of time or we run out of servers
                   1156:     ##
                   1157:     while($serversleft && $timeremain) {
                   1158:       ##
                   1159:       ## %rhash has servers deleted from it as results come in 
                   1160:       ## (within the foreach loop below).
                   1161:       ##
                   1162:       foreach my $rkey (sort keys %rhash) {
1.126     matthew  1163: #        &Apache::lonnet::logthis("Server $rkey:".time);
1.93      harris41 1164: 	$servercount++;
                   1165: 	$compiledresult='';
1.92      harris41 1166: 	my $reply=$rhash{$rkey};
1.18      harris41 1167: 	my @results;
1.93      harris41 1168: 	if ($reply eq 'con_lost') {
1.125     matthew  1169: 	    &popwin_imgupdate($r,$rkey,"srvbad.gif");
                   1170: 	    $serversleft--;
                   1171:             delete $rhash{$rkey};
                   1172: 	} else {
                   1173:             # must do since 'use strict' checks for tainting
                   1174: 	    $reply=~/^([\.\w]+)$/; 
                   1175: 	    my $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
1.93      harris41 1176: 	    $reply=~/(.*?)\_/;
1.126     matthew  1177:             for (my $counter=0;$counter<2;$counter++) {
                   1178:                 if (-e $replyfile && ! -e "$replyfile.end") {
                   1179:                     &popwin_imgupdate($r,$rkey,"srvhalf.gif");
                   1180:                     &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
                   1181:                                '"still transferring..."'.';');
                   1182:                 }
                   1183:                 # Are we finished transferring data?
                   1184:                 if (-e "$replyfile.end") {
                   1185:                     $serversleft--;
                   1186:                     delete $rhash{$rkey};
                   1187:                     if (-s $replyfile) {
                   1188:                         &popwin_imgupdate($r,$rkey,"srvgood.gif");
                   1189:                         my $fh;
                   1190:                         unless ($fh=Apache::File->new($replyfile)){ 
                   1191:                             # Is it really appropriate to die on this error?
                   1192:                             $r->print('ERROR: file '.
                   1193:                                       $replyfile.' cannot be opened');
                   1194:                             return OK;
                   1195:                         }
                   1196:                         @results=<$fh> if $fh;
1.127     matthew  1197:                         my $hits =@results;
1.126     matthew  1198:                         &popwin_js($r,'popwin.hc["'.$rkey.'"]='.
1.127     matthew  1199:                                    $hits.';');
                   1200:                         $hitcountsum+=$hits;
1.126     matthew  1201:                         &popwin_js($r,'popwin.document.forms.popremain.'.
                   1202:                                    'numhits.value='.$hitcountsum.';');
                   1203:                     } else {
                   1204:                         &popwin_imgupdate($r,$rkey,"srvempty.gif");
                   1205:                         &popwin_js($r,'popwin.hc["'.$rkey.'"]=0;');
                   1206:                     }
                   1207:                     last;
                   1208:                 } # end of if ( -e "$replyfile.end")
                   1209:                 last unless $timeremain;
                   1210:                 sleep 1;    # wait for daemons to write files?
                   1211:                 $timeremain--;
                   1212:                 $elapsetime++;
                   1213:                 &popwin_js($r,"popwin.document.popremain.".
                   1214:                            "elapsetime.value=$elapsetime;");
1.93      harris41 1215: 	    }
1.117     matthew  1216: 	    &popwin_js($r,'popwin.document.whirly.'.
                   1217: 		       'src="/adm/lonIcons/lonanimend.gif";');
1.125     matthew  1218: 	} # end of if ($reply eq 'con_lost') else statement
1.126     matthew  1219:         my %Fields = undef;     # Holds the data to be sent to the various 
                   1220:                                 # *_view routines.
                   1221:         my ($extrashow,$customfields,$customhash) = &handle_custom_fields(\@results);
                   1222:         my @customfields = @$customfields;
                   1223:         my %customhash   = %$customhash;
                   1224: 	untie %hash if (keys %hash);
                   1225:         #
                   1226: 	if (! tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
                   1227: 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
                   1228:                       'file</body></html>');
                   1229:         } else {
1.101     harris41 1230: 	    if ($ENV{'form.launch'} eq '1') {
                   1231: 		&start_fresh_session();
                   1232: 	    }
                   1233: 	    foreach my $result (@results) {
                   1234: 		next if $result=~/^custom\=/;
                   1235: 		chomp $result;
                   1236: 		next unless $result;
1.126     matthew  1237:                 %Fields = &parse_raw_result($result,$rkey);
                   1238: 		$Fields{'extrashow'}=$extrashow;
1.101     harris41 1239: 		if ($extrashow) {
                   1240: 		    foreach my $field (@customfields) {
                   1241: 			my $value='';
1.126     matthew  1242: 			$value = $1 if ($customhash{$Fields{'url'}}=~/\<{$field}[^\>]*\>(.*?)\<\/{$field}[^\>]*\>/s);
                   1243:                         $Fields{'extrashow'}=~s/\<\!\-\- $field \-\-\>/ $value/g;
                   1244:                     }
                   1245:                 }
                   1246:                 $compiledresult.="\n<p>\n";
                   1247:                 if ($ENV{'form.catalogmode'} eq 'interactive') {
                   1248:                     my $titleesc=$Fields{'title'};
                   1249:                     $titleesc=~s/\'/\\'/; # '
                   1250:                     $compiledresult.=<<END if ($ENV{'form.catalogmode'} eq 'interactive');
1.8       harris41 1251: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
1.126     matthew  1252: onClick="javascript:select_data('$titleesc','$Fields{'url'}')">
1.8       harris41 1253: </font>
1.98      harris41 1254: <br />
                   1255: END
1.115     harris41 1256:                 }
1.101     harris41 1257:                 if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                   1258: 		    $fnum+=0;
1.126     matthew  1259: 		    $hash{"pre_${fnum}_link"}=$Fields{'url'};
                   1260: 		    $hash{"pre_${fnum}_title"}=$Fields{'title'};
1.101     harris41 1261: 		    $compiledresult.=<<END;
1.118     www      1262: <font size='-1'>
                   1263: <input type="checkbox" name="returnvalues" value="SELECT"
1.98      harris41 1264: onClick="javascript:queue($fnum)" />
                   1265: </font>
                   1266: <br />
1.8       harris41 1267: END
1.101     harris41 1268: # <input type="hidden" name="title$fnum" value="$title" />
                   1269: # <input type="hidden" name="url$fnum" value="$url" />
                   1270:                     $fnum++;
                   1271: 		}
                   1272: 	        if ($viewselect eq 'Detailed Citation View') {
1.126     matthew  1273: 		    $compiledresult.=&detailed_citation_view
                   1274:                         (%Fields, hostname => $rkey );
1.101     harris41 1275: 		}
                   1276:                 elsif ($viewselect eq 'Summary View') {
1.126     matthew  1277: 		    $compiledresult.=&summary_view
                   1278:                         (%Fields, hostname => $rkey );
1.101     harris41 1279: 	        }
                   1280:                 elsif ($viewselect eq 'Fielded Format') {
1.126     matthew  1281: 		    $compiledresult.=&fielded_format_view
                   1282:                         (%Fields, hostname => $rkey );
1.101     harris41 1283: 	        }
                   1284:                 elsif ($viewselect eq 'XML/SGML') {
1.126     matthew  1285: 		    $compiledresult.=&xml_sgml_view
                   1286:                         (%Fields, hostname => $rkey );
1.101     harris41 1287: 		}
1.130     matthew  1288:                 if ($compiledresult or $servercount!=$servernum) {
                   1289:                     $compiledresult.="<hr align='left' width='200' noshade />";
                   1290:                 }
1.101     harris41 1291:             }
                   1292:             untie %hash;
1.18      harris41 1293:         }
1.93      harris41 1294: 	if ($compiledresult) {
                   1295: 	    $resultflag=1;
1.126     matthew  1296:             $r->print($compiledresult);
1.18      harris41 1297: 	}
1.93      harris41 1298:         my $percent=sprintf('%3.0f',($servercount/$servernum*100));
1.126     matthew  1299:       } # End of foreach loop over servers remaining
                   1300:     }   # End of big loop - while($serversleft && $timeremain)
1.93      harris41 1301:     unless ($resultflag) {
                   1302:         $r->print("\nThere were no results that matched your query\n");
1.43      harris41 1303:     }
1.100     harris41 1304: #    $r->print('<script type="text/javascript">'.'popwin.close()</script>'."\n"); $r->rflush(); 
1.126     matthew  1305:     $r->print("</body>\n</html>\n");
                   1306:     return;
                   1307: }
                   1308: 
                   1309: ###########################################################
                   1310: ###########################################################
                   1311: 
                   1312: =pod
                   1313: 
                   1314: =item &parse_raw_result()
                   1315: 
                   1316: Takes a line from the file of results and parse it.  Returns a hash 
                   1317: with keys for the following fields:
                   1318: 'title', 'author', 'subject', 'url', 'keywords', 'version', 'notes', 
                   1319: 'abstract', 'mime', 'lang', 'owner', 'copyright', 'creationdate', 
                   1320: 'lastrevisiondate'.
                   1321: 
                   1322: In addition, the following tags are set by calling the appropriate 
                   1323: lonnet function: 'language', 'cprtag', 'mimetag'.
                   1324: 
                   1325: The 'title' field is set to "Untitled" if the title field is blank.
                   1326: 
                   1327: 'abstract' and 'keywords' are truncated to 200 characters.
                   1328: 
                   1329: =cut
                   1330: 
                   1331: ###########################################################
                   1332: ###########################################################
                   1333: sub parse_raw_result {
                   1334:     my ($result,$hostname) = @_;
                   1335:     # Check for a comma - if it is there then we do not need to unescape the
                   1336:     # string.  There seems to be some kind of problem with some items in
                   1337:     # the database - the entire string gets sent out unescaped...?
                   1338:     unless ($result =~ /,/) {
                   1339:         $result = &Apache::lonnet::unescape($result);
                   1340:     }
                   1341:     my @fields=map {
                   1342:         &Apache::lonnet::unescape($_);
                   1343:     } (split(/\,/,$result));
                   1344:     my ($title,$author,$subject,$url,$keywords,$version,
                   1345:         $notes,$abstract,$mime,$lang,
                   1346:         $creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
                   1347:     my %Fields = 
                   1348:         ( title     => &Apache::lonnet::unescape($title),
                   1349:           author    => &Apache::lonnet::unescape($author),
                   1350:           subject   => &Apache::lonnet::unescape($subject),
                   1351:           url       => &Apache::lonnet::unescape($url),
                   1352:           keywords  => &Apache::lonnet::unescape($keywords),
                   1353:           version   => &Apache::lonnet::unescape($version),
                   1354:           notes     => &Apache::lonnet::unescape($notes),
                   1355:           abstract  => &Apache::lonnet::unescape($abstract),
                   1356:           mime      => &Apache::lonnet::unescape($mime),
                   1357:           lang      => &Apache::lonnet::unescape($lang),
                   1358:           owner     => &Apache::lonnet::unescape($owner),
                   1359:           copyright => &Apache::lonnet::unescape($copyright),
                   1360:           creationdate     => &Apache::lonnet::unescape($creationdate),
                   1361:           lastrevisiondate => &Apache::lonnet::unescape($lastrevisiondate)
                   1362:         );
                   1363:     $Fields{'language'} = 
                   1364:         &Apache::loncommon::languagedescription($Fields{'lang'});
                   1365:     $Fields{'copyrighttag'} =
                   1366:         &Apache::loncommon::copyrightdescription($Fields{'copyright'});
                   1367:     $Fields{'mimetag'} =
                   1368:         &Apache::loncommon::filedescription($Fields{'mime'});
1.134   ! matthew  1369:     if ($Fields{'author'}=~/^(\s*|error)$/) {
        !          1370:         $Fields{'author'}="Unknown Author";
        !          1371:     }
1.126     matthew  1372:     # Put spaces in the keyword list, if needed.
                   1373:     $Fields{'keywords'}=~ s/,([A-z])/, $1/g; 
                   1374:     if ($Fields{'title'}=~ /^\s*$/ ) { 
                   1375:         $Fields{'title'}='Untitled'; 
                   1376:     }
                   1377:     unless ($ENV{'user.adv'}) {
                   1378:         $Fields{'keywords'} = '- not displayed -';
                   1379:         $Fields{'notes'}    = '- not displayed -';
                   1380:         $Fields{'abstract'} = '- not displayed -';
                   1381:         $Fields{'subject'}  = '- not displayed -';
                   1382:     }
                   1383:     if (length($Fields{'abstract'})>200) {
                   1384:         $Fields{'abstract'} = 
                   1385:             substr($Fields{'abstract'},0,200).'...';
                   1386:     }
                   1387:     if (length($Fields{'keywords'})>200) {
                   1388:         $Fields{'keywords'} =
                   1389:             substr($Fields{'keywords'},0,200).'...';
                   1390:     }
                   1391:     return %Fields;
                   1392: }
                   1393: 
                   1394: ###########################################################
                   1395: ###########################################################
                   1396: 
                   1397: =pod
                   1398: 
                   1399: =item &handle_custom_fields()
                   1400: 
                   1401: =cut
                   1402: 
                   1403: ###########################################################
                   1404: ###########################################################
                   1405: sub handle_custom_fields {
                   1406:     my @results = @{shift()};
                   1407:     my $customshow='';
                   1408:     my $extrashow='';
                   1409:     my @customfields;
                   1410:     if ($ENV{'form.customshow'}) {
                   1411:         $customshow=$ENV{'form.customshow'};
                   1412:         $customshow=~s/[^\w\s]//g;
                   1413:         my @fields=map {
                   1414:             "<font color=\"#008000\">$_:</font><!-- $_ -->";
                   1415:         } split(/\s+/,$customshow);
                   1416:         @customfields=split(/\s+/,$customshow);
                   1417:         if ($customshow) {
                   1418:             $extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
                   1419:         }
                   1420:     }
                   1421:     my $customdata='';
                   1422:     my %customhash;
                   1423:     foreach my $result (@results) {
                   1424:         if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
                   1425:             my $tmp=$result;
                   1426:             $tmp=~s/^custom\=//;
                   1427:             my ($k,$v)=map {&Apache::lonnet::unescape($_);
                   1428:                         } split(/\,/,$tmp);
                   1429:             $customhash{$k}=$v;
                   1430:         }
                   1431:     }
                   1432:     return ($extrashow,\@customfields,\%customhash);
1.41      harris41 1433: }
                   1434: 
1.122     matthew  1435: ######################################################################
                   1436: ######################################################################
                   1437: 
1.125     matthew  1438: =pod
                   1439: 
                   1440: =item &search_results_header
                   1441: 
1.130     matthew  1442: Output the proper html headers and javascript code to deal with different 
                   1443: calling modes.
                   1444: 
                   1445: Takes most inputs directly from %ENV, except $mode.  
                   1446: 
                   1447: =over 4
                   1448: 
                   1449: =item $mode is either (at this writing) 'Basic' or 'Advanced'
                   1450: 
                   1451: =back
1.126     matthew  1452: 
1.130     matthew  1453: The following environment variables are checked:
1.126     matthew  1454: 
                   1455: =over 4
                   1456: 
                   1457: =item 'form.catalogmode' 
                   1458: 
                   1459: Checked for 'interactive' and 'groupsearch'.
                   1460: 
                   1461: =item 'form.mode'
                   1462: 
                   1463: Checked for existance & 'edit' mode.
                   1464: 
                   1465: =item 'form.form'
                   1466: 
                   1467: =item 'form.element'
                   1468: 
                   1469: =back
                   1470: 
1.125     matthew  1471: =cut
                   1472: 
                   1473: ######################################################################
                   1474: ######################################################################
                   1475: sub search_results_header {
1.130     matthew  1476:     my ($mode) = @_;
                   1477:     $mode = lc($mode);
                   1478:     my $title;
                   1479:     if ($mode eq 'advanced') {
                   1480:         $title = "Advanced Search Results";
                   1481:     } elsif ($mode eq 'basic') {
                   1482:         $title = "Basic Search Results";
                   1483:     }
1.125     matthew  1484:     my $result = '';
                   1485:     # output beginning of search page
                   1486:     $result.=<<BEGINNING;
                   1487: <html>
                   1488: <head>
1.130     matthew  1489: <title>$title</title>
1.125     matthew  1490: BEGINNING
                   1491:     # conditional output of script functions dependent on the mode in
                   1492:     # which the search was invoked
                   1493:     if ($ENV{'form.catalogmode'} eq 'interactive'){
                   1494: 	if (! exists($ENV{'form.mode'}) || $ENV{'form.mode'} ne 'edit') {
                   1495:             $result.=<<SCRIPT;
                   1496: <script type="text/javascript">
                   1497:     function select_data(title,url) {
                   1498: 	changeTitle(title);
                   1499: 	changeURL(url);
                   1500: 	self.close();
                   1501:     }
                   1502:     function changeTitle(val) {
                   1503: 	if (opener.inf.document.forms.resinfo.elements.t) {
                   1504: 	    opener.inf.document.forms.resinfo.elements.t.value=val;
                   1505: 	}
                   1506:     }
                   1507:     function changeURL(val) {
                   1508: 	if (opener.inf.document.forms.resinfo.elements.u) {
                   1509: 	    opener.inf.document.forms.resinfo.elements.u.value=val;
                   1510: 	}
                   1511:     }
                   1512: </script>
                   1513: SCRIPT
                   1514:         } elsif ($ENV{'form.mode'} eq 'edit') {
                   1515:             my $form = $ENV{'form.form'};
                   1516:             my $element = $ENV{'form.element'};
                   1517:             $result.=<<SCRIPT;
                   1518: <script type="text/javascript">
                   1519: function select_data(title,url) {
                   1520:     changeURL(url);
                   1521:     self.close();
                   1522: }
                   1523: function changeTitle(val) {
                   1524: }
                   1525: function changeURL(val) {
                   1526:     if (window.opener.document) {
                   1527:         window.opener.document.forms["$form"].elements["$element"].value=val;
                   1528:     } else {
                   1529: 	var url = 'forms[\"$form\"].elements[\"$element\"].value';
                   1530:         alert("Unable to transfer data to "+url);
                   1531:     }
                   1532: }
                   1533: </script>
                   1534: SCRIPT
                   1535:         }
                   1536:     }
                   1537:     $result.=<<SCRIPT if $ENV{'form.catalogmode'} eq 'groupsearch';
                   1538: <script type="text/javascript">
                   1539:     function select_data(title,url) {
                   1540: //	alert('DEBUG: Should be storing '+title+' and '+url);
                   1541:     }
                   1542:     function queue(val) {
                   1543: 	if (eval("document.forms.results.returnvalues["+val+"].checked")) {
                   1544: 	    document.forms.results.acts.value+='1a'+val+'b';
                   1545: 	}
                   1546: 	else {
                   1547: 	    document.forms.results.acts.value+='0a'+val+'b';
                   1548: 	}
                   1549:     }
                   1550:     function select_group() {
                   1551: 	window.location=
                   1552:     "/adm/groupsort?mode=$ENV{'form.mode'}&catalogmode=groupsearch&acts="+
                   1553: 	    document.forms.results.acts.value;
                   1554:     }
                   1555: </script>
                   1556: SCRIPT
                   1557:     $result.=<<SCRIPT;
                   1558: <script type="text/javascript">
                   1559:     function displayinfo(val) {
                   1560: 	popwin.document.forms.popremain.sdetails.value=val;
                   1561:     }
                   1562:     function openhelp(val) {
                   1563: 	openhelpwin=open('/adm/help/searchcat.html','helpscreen',
                   1564: 	     'scrollbars=1,width=400,height=300');
                   1565: 	openhelpwin.focus();
                   1566:     }
                   1567:     function abortsearch(val) {
                   1568: 	popwin.close();
                   1569:     }
                   1570: </script>
                   1571: SCRIPT
1.130     matthew  1572:     $result.=<<END;
                   1573: </head>
                   1574: <body bgcolor="#ffffff">
                   1575: <img align=right src=/adm/lonIcons/lonlogos.gif>
                   1576: <h1>$title</h1>
                   1577: END
1.125     matthew  1578:     return $result;
                   1579: }
                   1580: 
                   1581: ######################################################################
                   1582: ######################################################################
                   1583: 
                   1584: =pod
                   1585: 
                   1586: =item &make_popwin()
                   1587: 
                   1588: Returns html with javascript in it to open up the status window.
                   1589: 
                   1590: =cut
                   1591: 
                   1592: ######################################################################
                   1593: ######################################################################
                   1594: sub make_popwin {
                   1595:     my %rhash = @_;
                   1596:     my $servernum=(keys %rhash);
                   1597:     my $hcinit;
                   1598:     my $grid="'<br />'+\n";
                   1599:     # $sn is the server number, used ONLY to make sure we have
                   1600:     # rows of 10 each.  No longer used to index images.
                   1601:     my $sn=1;
                   1602:     foreach my $sk (sort keys %rhash) {
                   1603: 	# '<a href="
                   1604: 	$grid.="'<a href=\"";
                   1605: 	# javascript:displayinfo('+
                   1606: 	$grid.="javascript:opener.displayinfo('+";
                   1607: 	# "'"+'key
                   1608: 	$grid.="\"'\"+'";
                   1609: 	$grid.=$sk;
                   1610: 	my $hc;
                   1611: 	if ($rhash{$sk} eq 'con_lost') {
                   1612: 	    $hc="BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR ";
                   1613: 	}
                   1614: 	else {
                   1615: 	    $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
                   1616: 	    $hcinit.="hc[\"$sk\"]=\"not yet connected...\";";
                   1617: 	}
                   1618: 	$grid.=" hitcount=".$hc;
1.127     matthew  1619: 	$grid.=" domain=".$Apache::lonnet::hostdom{$sk};
                   1620: 	$grid.=" IP=".$Apache::lonnet::hostip{$sk};
1.125     matthew  1621: 	# '+"'"+'">'+
                   1622: 	$grid.="'+\"'\"+')\">'+";
                   1623: 	$grid.="\n";
1.127     matthew  1624: 	$grid.="'<img border=\"0\" name=\"img_".$Apache::lonnet::hostdom{$sk}.
                   1625:             '_'.$sk."\" src=\"/adm/lonIcons/srvnull.gif\" alt=\"".$sk.
                   1626:                 "\" /></a>'+\n";
1.125     matthew  1627: 	$grid.="'<br />'+\n" unless $sn%10;
                   1628:         $sn++;
                   1629:     }
                   1630:     my $result.=<<ENDPOP;
                   1631: <script type="text/javascript">
                   1632:     popwin=open('','popwin','scrollbars=1,width=400,height=220');
                   1633:     popwin.focus();
                   1634:     popwin.document.writeln('<'+'html>');
                   1635:     popwin.document.writeln('<'+'head>');
                   1636:     popwin.document.writeln('<'+'script>');
                   1637:     popwin.document.writeln('hc=new Array();$hcinit');
                   1638:     popwin.document.writeln('<'+'/script>');
                   1639:     popwin.document.writeln('<'+'/head>'+
                   1640:         '<'+'body bgcolor="#FFFFFF">'+
                   1641: 	'<'+'image name="whirly" align="right" src="/adm/lonIcons/'+
                   1642: 	'lonanim.gif" '+
                   1643: 	'alt="animated logo" />'+
                   1644: 	'<'+'h3>Search Results Progress<'+'/h3>'+
                   1645:         '<'+'form name="popremain">'+
                   1646:         '<'+'tt>'+
                   1647: 	'<'+'br clear="all"/><i>PLEASE BE PATIENT</i>'+
                   1648: 	'<'+'br />SCANNING $servernum SERVERS'+
                   1649: 	'<'+'br clear="all" />Number of record hits found '+
                   1650: 	'<'+'input type="text" size="10" name="numhits"'+
                   1651: 	' value="0" />'+
                   1652: 	'<'+'br clear="all" />Time elapsed '+
                   1653: 	'<'+'input type="text" size="10" name="elapsetime"'+
                   1654: 	' value="0" />'+
                   1655: 	'<'+'br />'+
                   1656: 	'SERVER GRID (click on any cell for details)'+
                   1657:         $grid
                   1658:         '<'+'br />'+
                   1659: 	'Server details '+
                   1660: 	'<'+'input type="text" size="35" name="sdetails"'+
                   1661: 	' value="" />'+
                   1662: 	'<'+'br />'+
                   1663: 	' <'+'input type="button" name="button"'+
                   1664: 	' value="close this window" '+
                   1665: 	' onClick="javascript:opener.abortsearch()" />'+
                   1666: 	' <'+'input type="button" name="button"'+
                   1667: 	' value="help" onClick="javascript:opener.openhelp()" />'+
                   1668: 	'<'+'/tt>'+
                   1669:         '<'+'/form>'+
                   1670:         '<'+'/body><'+'/html>');
                   1671:     popwin.document.close();
                   1672: </script>
                   1673: ENDPOP
                   1674:     return $result;
                   1675: }
                   1676: 
                   1677: ######################################################################
                   1678: ######################################################################
                   1679: 
1.122     matthew  1680: =pod 
                   1681: 
                   1682: =item Metadata Viewing Functions
                   1683: 
                   1684: Output is a HTML-ified string.
                   1685: Input arguments are title, author, subject, url, keywords, version,
                   1686: notes, short abstract, mime, language, creation date,
1.126     matthew  1687: last revision date, owner, copyright, hostname, and
1.122     matthew  1688: extra custom metadata to show.
                   1689: 
                   1690: =over 4
                   1691: 
                   1692: =item &detailed_citation_view() 
                   1693: 
                   1694: =cut
                   1695: 
                   1696: ######################################################################
                   1697: ######################################################################
1.50      harris41 1698: sub detailed_citation_view {
1.126     matthew  1699:     my %values = @_;
1.50      harris41 1700:     my $result=<<END;
1.126     matthew  1701: <h3><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1702:     target='search_preview'>$values{'title'}</a></h3>
1.56      harris41 1703: <p>
1.130     matthew  1704: <b>$values{'author'}</b>, <i>$values{'owner'}</i><br />
                   1705: 
                   1706: <b>Subject:       </b> $values{'subject'}<br />
                   1707: <b>Keyword(s):    </b> $values{'keywords'}<br />
                   1708: <b>Notes:         </b> $values{'notes'}<br />
                   1709: <b>MIME Type:     </b> $values{'mimetag'}<br />
                   1710: <b>Language:      </b> $values{'language'}<br />
                   1711: <b>Copyright/Distribution:</b> $values{'cprtag'}<br />
1.78      harris41 1712: </p>
1.126     matthew  1713: $values{'extrashow'}
1.78      harris41 1714: <p>
1.126     matthew  1715: $values{'shortabstract'}
1.50      harris41 1716: </p>
                   1717: END
                   1718:     return $result;
                   1719: }
                   1720: 
1.122     matthew  1721: ######################################################################
                   1722: ######################################################################
                   1723: 
                   1724: =pod 
                   1725: 
                   1726: =item &summary_view() 
                   1727: 
                   1728: =cut
                   1729: 
                   1730: ######################################################################
                   1731: ######################################################################
1.50      harris41 1732: sub summary_view {
1.126     matthew  1733:     my %values = @_;
1.50      harris41 1734:     my $result=<<END;
1.126     matthew  1735: <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1736:    target='search_preview'>$values{'author'}</a><br />
                   1737: $values{'title'}<br />
                   1738: $values{'owner'} -- $values{'lastrevisiondate'}<br />
                   1739: $values{'copyrighttag'}<br />
                   1740: $values{'extrashow'}
1.50      harris41 1741: </p>
                   1742: END
                   1743:     return $result;
                   1744: }
                   1745: 
1.122     matthew  1746: ######################################################################
                   1747: ######################################################################
                   1748: 
                   1749: =pod 
                   1750: 
                   1751: =item &fielded_format_view() 
                   1752: 
                   1753: =cut
                   1754: 
                   1755: ######################################################################
                   1756: ######################################################################
1.50      harris41 1757: sub fielded_format_view {
1.126     matthew  1758:     my %values = @_;
1.50      harris41 1759:     my $result=<<END;
1.126     matthew  1760: <b>URL: </b> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
                   1761:               target='search_preview'>$values{'url'}</a>
1.56      harris41 1762: <br />
1.126     matthew  1763: <b>Title:</b> $values{'title'}<br />
                   1764: <b>Author(s):</b> $values{'author'}<br />
                   1765: <b>Subject:</b> $values{'subject'}<br />
                   1766: <b>Keyword(s):</b> $values{'keywords'}<br />
                   1767: <b>Notes:</b> $values{'notes'}<br />
                   1768: <b>MIME Type:</b> $values{'mimetag'}<br />
                   1769: <b>Language:</b> $values{'language'}<br />
                   1770: <b>Creation Date:</b> $values{'creationdate'}<br />
                   1771: <b>Last Revision Date:</b> $values{'lastrevisiondate'}<br />
                   1772: <b>Publisher/Owner:</b> $values{'owner'}<br />
                   1773: <b>Copyright/Distribution:</b> $values{'copyrighttag'}<br />
                   1774: <b>Repository Location:</b> $values{'hostname'}<br />
                   1775: <b>Abstract:</b> $values{'shortabstract'}<br />
                   1776: $values{'extrashow'}
1.50      harris41 1777: </p>
                   1778: END
                   1779:     return $result;
                   1780: }
                   1781: 
1.122     matthew  1782: ######################################################################
                   1783: ######################################################################
                   1784: 
                   1785: =pod 
                   1786: 
                   1787: =item &xml_sgml_view() 
                   1788: 
                   1789: =back 
                   1790: 
                   1791: =cut
                   1792: 
                   1793: ######################################################################
                   1794: ######################################################################
1.50      harris41 1795: sub xml_sgml_view {
1.126     matthew  1796:     my %values = @_;
1.50      harris41 1797:     my $result=<<END;
1.56      harris41 1798: <pre>
                   1799: &lt;LonCapaResource&gt;
1.126     matthew  1800: &lt;url&gt;$values{'url'}&lt;/url&gt;
                   1801: &lt;title&gt;$values{'title'}&lt;/title&gt;
                   1802: &lt;author&gt;$values{'author'}&lt;/author&gt;
                   1803: &lt;subject&gt;$values{'subject'}&lt;/subject&gt;
                   1804: &lt;keywords&gt;$values{'keywords'}&lt;/keywords&gt;
                   1805: &lt;notes&gt;$values{'notes'}&lt;/notes&gt;
1.56      harris41 1806: &lt;mimeInfo&gt;
1.126     matthew  1807: &lt;mime&gt;$values{'mime'}&lt;/mime&gt;
                   1808: &lt;mimetag&gt;$values{'mimetag'}&lt;/mimetag&gt;
1.56      harris41 1809: &lt;/mimeInfo&gt;
                   1810: &lt;languageInfo&gt;
1.126     matthew  1811: &lt;language&gt;$values{'lang'}&lt;/language&gt;
                   1812: &lt;languagetag&gt;$values{'language'}&lt;/languagetag&gt;
1.56      harris41 1813: &lt;/languageInfo&gt;
1.126     matthew  1814: &lt;creationdate&gt;$values{'creationdate'}&lt;/creationdate&gt;
                   1815: &lt;lastrevisiondate&gt;$values{'lastrevisiondate'}&lt;/lastrevisiondate&gt;
                   1816: &lt;owner&gt;$values{'owner'}&lt;/owner&gt;
1.56      harris41 1817: &lt;copyrightInfo&gt;
1.126     matthew  1818: &lt;copyright&gt;$values{'copyright'}&lt;/copyright&gt;
                   1819: &lt;copyrighttag&gt;$values{'copyrighttag'}&lt;/copyrighttag&gt;
1.56      harris41 1820: &lt;/copyrightInfo&gt;
1.126     matthew  1821: &lt;repositoryLocation&gt;$values{'hostname'}&lt;/repositoryLocation&gt;
                   1822: &lt;shortabstract&gt;$values{'shortabstract'}&lt;/shortabstract&gt;
1.57      harris41 1823: &lt;/LonCapaResource&gt;
1.56      harris41 1824: </pre>
1.126     matthew  1825: $values{'extrashow'}
1.50      harris41 1826: END
                   1827:     return $result;
1.60      harris41 1828: }
                   1829: 
1.122     matthew  1830: ######################################################################
                   1831: ######################################################################
                   1832: 
                   1833: =pod 
                   1834: 
                   1835: =item &filled() see if field is filled.
                   1836: 
                   1837: =cut
                   1838: 
                   1839: ######################################################################
                   1840: ######################################################################
1.98      harris41 1841: sub filled {
                   1842:     my ($field)=@_;
                   1843:     if ($field=~/\S/ && $field ne 'any') {
                   1844: 	return 1;
1.61      harris41 1845:     }
1.98      harris41 1846:     else {
                   1847: 	return 0;
1.61      harris41 1848:     }
1.60      harris41 1849: }
                   1850: 
1.122     matthew  1851: ######################################################################
                   1852: ######################################################################
                   1853: 
                   1854: =pod 
                   1855: 
                   1856: =item &output_blank_field_error()
                   1857: 
                   1858: =cut
                   1859: 
                   1860: ######################################################################
                   1861: ######################################################################
1.98      harris41 1862: sub output_blank_field_error {
                   1863:     my ($r)=@_;
                   1864:     # make query information persistent to allow for subsequent revision
1.133     matthew  1865:     my $persistent=&make_persistent(\%ENV);
1.98      harris41 1866: 
                   1867:     $r->print(<<BEGINNING);
                   1868: <html>
                   1869: <head>
                   1870: <title>The LearningOnline Network with CAPA</title>
                   1871: BEGINNING
                   1872:     $r->print(<<RESULTS);
                   1873: </head>
                   1874: <body bgcolor="#ffffff">
                   1875: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
                   1876: <h1>Search Catalog</h1>
                   1877: <form method="post" action="/adm/searchcat">
                   1878: $persistent
                   1879: <input type='button' value='Revise search request'
                   1880: onClick='this.form.submit();' />
                   1881: $closebutton
                   1882: <hr />
                   1883: <h3>Helpful Message</h3>
                   1884: <p>
                   1885: Incorrect search query due to blank entry fields.
                   1886: You need to fill in the relevant
                   1887: fields on the search page in order for a query to be
                   1888: processed.
                   1889: </p>
                   1890: </body>
                   1891: </html>
                   1892: RESULTS
                   1893: }
                   1894: 
1.122     matthew  1895: ######################################################################
                   1896: ######################################################################
                   1897: 
                   1898: =pod 
                   1899: 
                   1900: =item &output_date_error()
                   1901: 
                   1902: Output a full html page with an error message.
                   1903: 
                   1904: =cut
                   1905: 
                   1906: ######################################################################
                   1907: ######################################################################
1.60      harris41 1908: sub output_date_error {
                   1909:     my ($r,$message)=@_;
                   1910:     # make query information persistent to allow for subsequent revision
1.133     matthew  1911:     my $persistent=&make_persistent(\%ENV);
1.60      harris41 1912: 
1.122     matthew  1913:     $r->print(<<RESULTS);
1.60      harris41 1914: <html>
                   1915: <head>
                   1916: <title>The LearningOnline Network with CAPA</title>
                   1917: </head>
                   1918: <body bgcolor="#ffffff">
1.98      harris41 1919: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
1.60      harris41 1920: <h1>Search Catalog</h1>
                   1921: <form method="post" action="/adm/searchcat">
                   1922: $persistent
                   1923: <input type='button' value='Revise search request'
1.98      harris41 1924: onClick='this.form.submit();' />
1.60      harris41 1925: $closebutton
1.98      harris41 1926: <hr />
1.60      harris41 1927: <h3>Helpful Message</h3>
                   1928: <p>
                   1929: $message
                   1930: </p>
                   1931: </body>
                   1932: </html>
                   1933: RESULTS
1.101     harris41 1934: }
                   1935: 
1.122     matthew  1936: ######################################################################
                   1937: ######################################################################
                   1938: 
                   1939: =pod 
                   1940: 
                   1941: =item &start_fresh_session()
                   1942: 
                   1943: Cleans the global %hash by removing all fields which begin with
                   1944: 'pre_' or 'store'.
                   1945: 
                   1946: =cut
                   1947: 
                   1948: ######################################################################
                   1949: ######################################################################
1.101     harris41 1950: sub start_fresh_session {
                   1951:     delete $hash{'mode_catalog'};
1.109     harris41 1952:     foreach (keys %hash) {
1.101     harris41 1953:         if ($_ =~ /^pre_/) {
                   1954:             delete $hash{$_};
                   1955:         }
                   1956:         if ($_ =~ /^store/) {
                   1957: 	    delete $hash{$_};
                   1958: 	}
1.109     harris41 1959:     }
1.3       harris41 1960: }
1.117     matthew  1961: 
1.122     matthew  1962: ######################################################################
                   1963: ######################################################################
                   1964: 
                   1965: =pod 
                   1966: 
                   1967: =item &popwin_js() send javascript to popwin
                   1968: 
                   1969: =cut
                   1970: 
                   1971: ######################################################################
                   1972: ######################################################################
1.117     matthew  1973: sub popwin_js {
                   1974:     # Print javascript out to popwin, but make sure we dont generate
                   1975:     # any javascript errors in doing so.
                   1976:     my ($r,$text) = @_;
                   1977:     $r->print(<<"END");
                   1978: <script type="text/javascript">
                   1979:     if (! popwin.closed) {
                   1980: 	$text
                   1981:     }
                   1982: </script>
                   1983: END
                   1984:     $r->rflush();
                   1985: }
                   1986: 
1.122     matthew  1987: ######################################################################
                   1988: ######################################################################
                   1989: 
                   1990: =pod 
                   1991: 
                   1992: =item &popwin_imgupdate()
                   1993: 
1.125     matthew  1994: Send a given image (and its location) out to the browser.  Takes as 
                   1995: input $r, loncapa server id, and an icon URL.
                   1996: 
1.122     matthew  1997: =cut
                   1998: 
                   1999: ######################################################################
                   2000: ######################################################################
1.117     matthew  2001: sub popwin_imgupdate {
1.125     matthew  2002:     my ($r,$server,$icon) = @_;
1.127     matthew  2003:     &popwin_js($r,'popwin.document.img_'.$Apache::lonnet::hostdom{$server}.
                   2004:                '_'.$server.'.'.'src="/adm/lonIcons/'.$icon.'";');
1.117     matthew  2005: }    
1.1       www      2006: 
                   2007: 1;
1.98      harris41 2008: 
1.1       www      2009: __END__
1.105     harris41 2010: 
1.121     matthew  2011: =pod
1.105     harris41 2012: 
1.121     matthew  2013: =back 
1.105     harris41 2014: 
                   2015: =cut

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