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

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

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