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

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

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