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

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

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