File:  [LON-CAPA] / loncom / interface / lonsearchcat.pm
Revision 1.98: download - view: text, annotated - select for diffs
Sun Aug 26 21:19:35 2001 UTC (22 years, 8 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
improvements to interface, adding in server grid

    1: # The LearningOnline Network with CAPA
    2: #
    3: # Search Catalog
    4: #
    5: # YEAR=2001
    6: # 03/08/2001 Scott Harrison
    7: # Scott Harrison: 03/12/2001, 03/13/2001, 03/14/2001, 03/15/2001, 03/19/2001
    8: # Scott Harrison: 03/20/2001, 03/21/2001, 03/22/2001, 03/26/2001, 03/27/2001
    9: # Scott Harrison: 04/02/2001, 08/15/2001, 08/24/2001, 08/25/2001
   10: 
   11: ###############################################################################
   12: ##                                                                           ##
   13: ## ORGANIZATION OF THIS PERL MODULE                                          ##
   14: ##                                                                           ##
   15: ## 1. Description of functions                                               ##
   16: ## 2. Modules used by this module                                            ##
   17: ## 3. Choices for different output views (detailed, summary, xml, etc)       ##
   18: ## 4. BEGIN block (to be run once after compilation)                         ##
   19: ## 5. Handling routine called via Apache and mod_perl                        ##
   20: ## 6. Other subroutines                                                      ##
   21: ##                                                                           ##
   22: ###############################################################################
   23: 
   24: # ---------------------------------------------------- Description of functions
   25: #
   26: #
   27: # === WEB HANDLER FUNCTIONS
   28: # BEGIN() : run once after compilation to initialize values
   29: # handler(server reference) : interacts with the Apache server layer
   30: #                             (for /adm/searchcat URLs)
   31: # get_unprocessed_cgi() : reads in critical name/value pairs that may have not
   32: #                         been processed and passed into %ENV by the web server
   33: # make_persistent() : makes a set of hidden HTML fields to make
   34: #                     SQL search interface information to be persistent
   35: #
   36: #
   37: # === WEB INTERFACE COMPONENT FUNCTIONS
   38: # simpletextfield(name,value) : returns HTML formatted string for simple text
   39: #                               field
   40: # simplecheckbox(name,value) : returns HTML formatted string for simple
   41: #                              checkbox
   42: # searchphrasefield(title,name,value) : returns HTML formatted string for
   43: #                                       a search expression phrase field
   44: # dateboxes(name, defaultmonth, defaultday, defaultyear) : returns HTML
   45: #                                                          formatted string
   46: #                                                          for a calendar date
   47: # selectbox(title,name,value,%HASH=options) : returns HTML formatted string for
   48: #                                             a selection box field
   49: #
   50: #
   51: # === SEARCH FUNCTIONS
   52: # advancedsearch(server reference, environment reference) : perform a complex
   53: #                                  multi-field logical query
   54: # basicsearch(server reference, environment reference) : perform a simple
   55: #                               single-field logical query
   56: # build_SQL_query(field name, logic) : builds a SQL query string from a
   57: #                                      logical expression with AND/OR keywords
   58: # build_custommetadata_query(field_name, logic_statement) : builds a perl
   59: #                 regular expression from a logical expression with AND/OR
   60: #                 keywords
   61: # recursive_SQL_query_build(field name, reverse notation expression) : 
   62: #                 builds a SQL query string from a reverse notation expression
   63: #                 logical expression with AND/OR keywords
   64: # build_date_queries(cmonth1, cday1, cyear1, cmonth2, cday2, cyear2,
   65: #                    lmonth1, lday1, lyear1, lmonth2, lday2, lyear2) :
   66: #                 Builds a SQL logic query to check time/date entries.
   67: #
   68: #
   69: # === OUTPUTTING RESULTS FUNCTION
   70: # output_results(output mode,
   71: #                server reference, 
   72: #                environment reference,
   73: #                reply list reference) : outputs results from search
   74: #
   75: #
   76: # === DIFFERENT WAYS TO VIEW METADATA RECORDS
   77: # detailed_citation_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
   78: #          see metadata viewing notes below 
   79: # summary_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
   80: #          see metadata viewing notes below 
   81: # fielded_format_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
   82: #          see metadata viewing notes below 
   83: # xml_sgml_view(ORDERED METADATA LIST FOR A RESULT OBJECT INSTANCE) :
   84: #          see metadata viewing notes below 
   85: #  ___________________________________________________________________________
   86: # | * Metadata viewing notes                                                  |
   87: # | Output is a HTML-ified string.                                            |
   88: # | Input arguments are title, author, subject, url, keywords, version, notes,|
   89: # | short abstract, mime, language, creation date, last revision date, owner, |
   90: # | copyright, hostname, httphost, and extra custom metadata to show.         |
   91: #  ---------------------------------------------------------------------------
   92: #
   93: #
   94: # === TEST CONDITIONAL FUNCTIONS
   95: # filled(field) : determines whether a given field has been filled
   96: #
   97: #
   98: # === ERROR FUNCTIONS
   99: # output_blank_field_error(server reference) : outputs a message saying that
  100: #                                              more fields need to be filled in
  101: # output_date_error(server reference, error message) : outputs
  102: #         an error message specific to bad date format.
  103: 
  104: package Apache::lonsearchcat;
  105: 
  106: # ------------------------------------------------- modules used by this module
  107: use strict;
  108: use Apache::Constants qw(:common);
  109: use Apache::lonnet();
  110: use Apache::File();
  111: use CGI qw(:standard);
  112: use Text::Query;
  113: 
  114: # ---------------------------------------- variables used throughout the module
  115: 
  116: # -- information holders
  117: my %language; # holds contents of language.tab
  118: my %cprtag; # holds contents of copyright.tab
  119: my %mimetag; # holds contents of filetypes.tab
  120: my %hostdomains; # matches host name to host domain
  121: my %hostips; # matches host name to host ip
  122: my %hitcount; # stores number of hits per host
  123: 
  124: # -- dynamically rendered interface components
  125: my $closebutton; # button that closes the search window
  126: my $importbutton; # button to take the selected results and go to group sorting
  127: 
  128: # -- miscellaneous variables
  129: my $scrout; # string that holds portions of the screen output
  130: my $fnum; # search result counter
  131: my $yourself; # allows for quickly limiting to oneself
  132: 
  133: # ------------------------------------------ choices for different output views
  134: # Detailed Citation View ---> sub detailed_citation_view
  135: # Summary View ---> sub summary_view
  136: # Fielded Format ---> sub fielded_format_view
  137: # XML/SGML ---> sub xml_sgml_view
  138: my $basicviewselect=<<END;
  139: <select name='basicviewselect'>
  140: <option value='Detailed Citation View'>Detailed Citation View</option>
  141: <option value='Summary View'>Summary View</option>
  142: <option value='Fielded Format'>Fielded Format</option>
  143: <option value='XML/SGML'>XML/SGML</option>
  144: </select>
  145: END
  146: my $advancedviewselect=<<END;
  147: <select name='advancedviewselect'>
  148: <option value='Detailed Citation View'>Detailed Citation View</option>
  149: <option value='Summary View'>Summary View</option>
  150: <option value='Fielded Format'>Fielded Format</option>
  151: <option value='XML/SGML'>XML/SGML</option>
  152: </select>
  153: END
  154: 
  155: # ----------------------------------------------------------------------- BEGIN
  156: sub BEGIN {
  157: # --------------------------------- Compute various listings of metadata values
  158:     $language{'any'}='Any language';
  159:     {
  160: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
  161: 				 '/language.tab');
  162: 	map {
  163: 	    $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
  164: 	    $language{$1}=$2;
  165: 	} <$fh>;
  166:     }
  167:     $cprtag{'any'}='Any copyright/distribution';
  168:     {
  169: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonIncludes'}.
  170: 				 '/copyright.tab');
  171: 	map {
  172: 	    $_=~/(\w+)\s+([\w\s\-]+)/; chomp;
  173: 	    $cprtag{$1}=$2;
  174: 	} <$fh>;
  175:     }
  176:     $mimetag{'any'}='Any type';
  177:     {
  178: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
  179: 				 '/filetypes.tab');
  180: 	map {
  181: 	    $_=~/(\w+)\s+(\w+)\s+([\w\s\-]+)/; chomp;
  182: 	    $mimetag{$1}=".$1 $3";
  183: 	} <$fh>;
  184:     }
  185:     {
  186: 	my $fh=Apache::File->new($Apache::lonnet::perlvar{'lonTabDir'}.
  187: 				 '/hosts.tab');
  188: 	map {
  189: 	    $_=~/(\w+?)\:(\w+?)\:(\w+?)\:(.*)/; chomp;
  190: 	    if ($3 eq 'library') {
  191: 		$hostdomains{$1}=$2;
  192: 		$hostips{$1}=$4;
  193: 	    }
  194: 	} <$fh>;
  195:     }
  196: }
  197: 
  198: # ----------------------------- Handling routine called via Apache and mod_perl
  199: sub handler {
  200:     my $r = shift;
  201: 
  202:     &get_unprocessed_cgi();
  203: 
  204:     $r->content_type('text/html');
  205:     $r->send_http_header;
  206:     return OK if $r->header_only;
  207: 
  208: # ----------------------------------- configure dynamic components of interface
  209:     my $hidden='';
  210:     if ($ENV{'form.catalogmode'} eq 'interactive') {
  211: 	$hidden="<input type='hidden' name='catalogmode' value='interactive'>".
  212: 	    "\n";
  213:         $closebutton="<input type='button' name='close' value='CLOSE' ".
  214: 	    "onClick='self.close()'>"."\n";
  215:     }
  216:     elsif ($ENV{'form.catalogmode'} eq 'groupsearch') {
  217: 	$hidden=<<END;
  218: <input type='hidden' name='catalogmode' value='groupsearch'>
  219: END
  220:         $closebutton=<<END;
  221: <input type='button' name='close' value='CLOSE' onClick='self.close()'>
  222: END
  223:         $importbutton=<<END;
  224: <input type='button' name='import' value='IMPORT'
  225: onClick='javascript:select_group()'>
  226: END
  227:     }
  228: 
  229: # ------------------------------------------------------ Determine current user
  230:     $yourself=$ENV{'user.name'}.'@'.$ENV{'user.domain'};
  231: 
  232: # --- Now, depending on the interface actions, do one of three things here:
  233: # --- 1. a basic search
  234: # --- 2. an advanced search
  235: # --- 3. output a search interface
  236: 
  237: # ----------------------------------- See if a search invocation should be done
  238:     if ($ENV{'form.basicsubmit'} eq 'SEARCH') {
  239: 	return &basicsearch($r,\%ENV);
  240:     }
  241:     elsif ($ENV{'form.advancedsubmit'} eq 'SEARCH') {
  242: 	return &advancedsearch($r,\%ENV);
  243:     }
  244: 
  245: # ----------------------------- Else, begin building search interface to output
  246:     $scrout=''; # building a part of screen output
  247:     $scrout.=&searchphrasefield('Limit by title','title',
  248: 			$ENV{'form.title'});
  249: 
  250:     $scrout.=&searchphrasefield('Limit by author','author',
  251: 			$ENV{'form.author'});
  252: 
  253:     $scrout.=&searchphrasefield('Limit by subject','subject',
  254: 			$ENV{'form.subject'});
  255: 
  256:     $scrout.=&searchphrasefield('Limit by keywords','keywords',
  257: 			$ENV{'form.keywords'});
  258: 
  259:     $scrout.=&searchphrasefield('Limit by URL','url',
  260: 			$ENV{'form.url'});
  261: 
  262: #    $scrout.=&searchphrasefield('Limit by version','version',
  263: #			$ENV{'form.version'});
  264: 
  265:     $scrout.=&searchphrasefield('Limit by notes','notes',
  266: 			$ENV{'form.notes'});
  267: 
  268:     $scrout.=&searchphrasefield('Limit by abstract','abstract',
  269: 			$ENV{'form.abstract'});
  270: 
  271:     $ENV{'form.mime'}='notxxx' unless length($ENV{'form.mime'});
  272:     $scrout.=&selectbox('Limit by MIME type','mime',
  273: 			$ENV{'form.mime'},%mimetag);
  274: 
  275:     $ENV{'form.language'}='any' unless length($ENV{'form.language'});
  276: 
  277:     $scrout.=&selectbox('Limit by language','language',
  278: 			$ENV{'form.language'},%language);
  279:     
  280: 
  281: # ------------------------------------------------ Compute date selection boxes
  282:     $scrout.=<<CREATIONDATESTART;
  283: <p>
  284: <font color="#800000" face="helvetica"><b>LIMIT BY CREATION DATE RANGE:</b>
  285: </font>
  286: <br />
  287: between:
  288: CREATIONDATESTART
  289:     $scrout.=&dateboxes('creationdatestart',1,1,1976,
  290: 			$ENV{'form.creationdatestart_month'},
  291: 			$ENV{'form.creationdatestart_day'},
  292: 			$ENV{'form.creationdatestart_year'},
  293: 			);
  294:     $scrout.=<<CREATIONDATEEND;
  295: and:
  296: CREATIONDATEEND
  297:     $scrout.=&dateboxes('creationdateend',12,31,2051,
  298: 			$ENV{'form.creationdateend_month'},
  299: 			$ENV{'form.creationdateend_day'},
  300: 			$ENV{'form.creationdateend_year'},
  301: 			);
  302:     $scrout.="</p>";
  303: 
  304:     $scrout.=<<LASTREVISIONDATESTART;
  305: <p>
  306: <font color="#800000" face="helvetica"><b>LIMIT BY LAST REVISION DATE RANGE:
  307: </b></font>
  308: <br />between:
  309: LASTREVISIONDATESTART
  310:     $scrout.=&dateboxes('lastrevisiondatestart',1,1,1976,
  311: 			$ENV{'form.lastrevisiondatestart_month'},
  312: 			$ENV{'form.lastrevisiondatestart_day'},
  313: 			$ENV{'form.lastrevisiondatestart_year'},
  314: 			);
  315:     $scrout.=<<LASTREVISIONDATEEND;
  316: and:
  317: LASTREVISIONDATEEND
  318:     $scrout.=&dateboxes('lastrevisiondateend',12,31,2051,
  319: 			$ENV{'form.lastrevisiondateend_month'},
  320: 			$ENV{'form.lastrevisiondateend_day'},
  321: 			$ENV{'form.lastrevisiondateend_year'},
  322: 			);
  323:     $scrout.='</p>';
  324: 
  325:     $scrout.=&searchphrasefield('Limit by publisher/owner','owner',
  326: 				$ENV{'form.owner'});
  327: 
  328:     $ENV{'form.copyright'}='any' unless length($ENV{'form.copyright'});
  329:     $scrout.=&selectbox('Limit by copyright/distribution','copyright',
  330: 			$ENV{'form.copyright'},%cprtag);
  331: 
  332: # ------------------------------------------- Compute customized metadata field
  333:     $scrout.=<<CUSTOMMETADATA;
  334: <p>
  335: <font color="#800000" face="helvetica"><b>LIMIT BY SPECIAL METADATA FIELDS:</b>
  336: </font>
  337: For resource-specific metadata, enter in an expression in the form of 
  338: <i>key</i>=<i>value</i> separated by operators such as AND or OR.<br />
  339: <b>Example:</b> grandmother=75 OR grandfather=85
  340: <br />
  341: CUSTOMMETADATA
  342: $scrout.=&simpletextfield('custommetadata',$ENV{'form.custommetadata'});
  343: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
  344: 
  345:     $scrout.=<<CUSTOMSHOW;
  346: <p>
  347: <font color="#800000" face="helvetica"><b>SHOW SPECIAL METADATA FIELDS:</b>
  348: </font>
  349: Enter in a space-separated list of special metadata fields to show
  350: in a fielded listing for each record result.
  351: <br />
  352: CUSTOMSHOW
  353: $scrout.=&simpletextfield('customshow',$ENV{'form.customshow'});
  354: $scrout.=' <i>initial users of this system do not need to worry about this option</i>';
  355: 
  356: # ---------------------------------------------------------------- Print screen
  357:     $r->print(<<ENDDOCUMENT);
  358: <html>
  359: <head>
  360: <title>The LearningOnline Network with CAPA</title>
  361: </head>
  362: <body bgcolor="#FFFFFF">
  363: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
  364: <h1>Search Catalog</h1>
  365: <form method="post" action="/adm/searchcat">
  366: $hidden
  367: <hr />
  368: <h3>Basic Search</h3>
  369: <p>
  370: Enter terms or phrases separated by search operators
  371: such as AND or OR then press SEARCH below.  Terms should be specific
  372: to the title, author, subject, notes, or abstract information associated
  373: with a resource.
  374: <br />
  375: ENDDOCUMENT
  376:     $r->print(&simpletextfield('basicexp',$ENV{'form.basicexp'}));
  377:     $r->print(' ');
  378:     $r->print(&simplecheckbox('titleonly',$ENV{'form.titleonly'}));
  379:     $r->print('<font color="#800000">Title only</font> ');
  380: #    $r->print(&simplecheckbox('allversions',$ENV{'form.allversions'}));
  381: # <font color="#800000">Search historic archives</font>
  382:     $r->print(<<ENDDOCUMENT);
  383: <br />
  384: <input type="submit" name="basicsubmit" value='SEARCH' />
  385: <input type="reset" name="reset" value='RESET' />
  386: $closebutton
  387: $basicviewselect
  388: </p>
  389: <hr />
  390: <h3>Advanced Search</h3>
  391: $scrout
  392: <p>
  393: <input type="submit" name="advancedsubmit" value='SEARCH' />
  394: <input type="reset" name="reset" value='RESET' />
  395: $closebutton
  396: $advancedviewselect
  397: </p>
  398: </form>
  399: </body>
  400: </html>
  401: ENDDOCUMENT
  402:     return OK;
  403: } 
  404: 
  405: # ----------- grab unprocessed CGI variables that may have been appended to URL
  406: sub get_unprocessed_cgi {
  407:     map {
  408:        my ($name, $value) = split(/=/,$_);
  409:        $value =~ tr/+/ /;
  410:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  411:        if ($name eq 'catalogmode') {
  412: 	   $ENV{'form.'.$name}=$value;
  413:        }
  414:     } (split(/&/,$ENV{'QUERY_STRING'}));
  415: }
  416: 
  417: # ------------------------------------------------------------- make persistent
  418: sub make_persistent {
  419:     my $persistent='';
  420:     
  421:     map {
  422: 	if (/^form\./ && !/submit/) {
  423: 	    my $name=$_;
  424: 	    my $key=$name;
  425: 	    $ENV{$key}=~s/\'//g; # do not mess with html field syntax
  426: 	    $name=~s/^form\.//;
  427: 	    $persistent.=<<END;
  428: <input type='hidden' name='$name' value='$ENV{$key}' />
  429: END
  430:         }
  431:     } (keys %ENV);
  432:     return $persistent;
  433: }
  434: 
  435: # --------------------------------------------------------- Various form fields
  436: 
  437: sub simpletextfield {
  438:     my ($name,$value)=@_;
  439:     return '<input type=text name=\''.$name.
  440: 	   '\' size=20 value=\''.$value.'\' />';
  441: }
  442: 
  443: sub simplecheckbox {
  444:     my ($name,$value)=@_;
  445:     my $checked='';
  446:     $checked="CHECKED" if $value eq 'on';
  447:     return '<input type=checkbox name=\''.$name.'\' '. $checked . '>';
  448: }
  449: 
  450: sub searchphrasefield {
  451:     my ($title,$name,$value)=@_;
  452:     my $instruction=<<END;
  453: Enter terms or phrases separated by search operators such
  454: as AND or OR.
  455: END
  456:     my $uctitle=uc($title);
  457:     return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:</b>".
  458: 	   "</FONT> $instruction<br />".
  459:            '<input type=text name="'.$name.'" size=80 value=\''.$value.'\'>';
  460: }
  461: 
  462: sub dateboxes {
  463:     my ($name,$defaultmonth,$defaultday,$defaultyear,
  464: 	$currentmonth,$currentday,$currentyear)=@_;
  465:     ($defaultmonth,$defaultday,$defaultyear)=('','','');
  466:     my $month=<<END;
  467: <select name="${name}_month">
  468: <option value='$defaultmonth'> </option>
  469: <option value="1">January</option>
  470: <option value="2">February</option>
  471: <option value="3">March</option>
  472: <option value="4">April</option>
  473: <option value="5">May</option>
  474: <option value="6">June</option>
  475: <option value="7">July</option>
  476: <option value="8">August</option>
  477: <option value="9">September</option>
  478: <option value="10">October</option>
  479: <option value="11">November</option>
  480: <option value="12">December</option>
  481: </select>
  482: END
  483:     $month=~s/(\"$currentmonth\")/$1 SELECTED/ if length($currentmonth);
  484:     my $day=<<END;
  485: <select name="${name}_day">
  486: <option value='$defaultday'> </option>
  487: <option value="1">1</option>
  488: <option value="2">2</option>
  489: <option value="3">3</option>
  490: <option value="4">4</option>
  491: <option value="5">5</option>
  492: <option value="6">6</option>
  493: <option value="7">7</option>
  494: <option value="8">8</option>
  495: <option value="9">9</option>
  496: <option value="10">10</option>
  497: <option value="11">11</option>
  498: <option value="12">12</option>
  499: <option value="13">13</option>
  500: <option value="14">14</option>
  501: <option value="15">15</option>
  502: <option value="16">16</option>
  503: <option value="17">17</option>
  504: <option value="18">18</option>
  505: <option value="19">19</option>
  506: <option value="20">20</option>
  507: <option value="21">21</option>
  508: <option value="22">22</option>
  509: <option value="23">23</option>
  510: <option value="24">24</option>
  511: <option value="25">25</option>
  512: <option value="26">26</option>
  513: <option value="27">27</option>
  514: <option value="28">28</option>
  515: <option value="29">29</option>
  516: <option value="30">30</option>
  517: <option value="31">31</option>
  518: </select>
  519: END
  520:     $day=~s/(\"$currentday\")/$1 SELECTED/ if length($currentday);
  521:     my $year=<<END;
  522: <select name="${name}_year">
  523: <option value='$defaultyear'> </option>
  524: <option value="1976">1976</option>
  525: <option value="1977">1977</option>
  526: <option value="1978">1978</option>
  527: <option value="1979">1979</option>
  528: <option value="1980">1980</option>
  529: <option value="1981">1981</option>
  530: <option value="1982">1982</option>
  531: <option value="1983">1983</option>
  532: <option value="1984">1984</option>
  533: <option value="1985">1985</option>
  534: <option value="1986">1986</option>
  535: <option value="1987">1987</option>
  536: <option value="1988">1988</option>
  537: <option value="1989">1989</option>
  538: <option value="1990">1990</option>
  539: <option value="1991">1991</option>
  540: <option value="1992">1992</option>
  541: <option value="1993">1993</option>
  542: <option value="1994">1994</option>
  543: <option value="1995">1995</option>
  544: <option value="1996">1996</option>
  545: <option value="1997">1997</option>
  546: <option value="1998">1998</option>
  547: <option value="1999">1999</option>
  548: <option value="2000">2000</option>
  549: <option value="2001">2001</option>
  550: <option value="2002">2002</option>
  551: <option value="2003">2003</option>
  552: <option value="2004">2004</option>
  553: <option value="2005">2005</option>
  554: <option value="2006">2006</option>
  555: <option value="2007">2007</option>
  556: <option value="2008">2008</option>
  557: <option value="2009">2009</option>
  558: <option value="2010">2010</option>
  559: <option value="2011">2011</option>
  560: <option value="2012">2012</option>
  561: <option value="2013">2013</option>
  562: <option value="2014">2014</option>
  563: <option value="2015">2015</option>
  564: <option value="2016">2016</option>
  565: <option value="2017">2017</option>
  566: <option value="2018">2018</option>
  567: <option value="2019">2019</option>
  568: <option value="2020">2020</option>
  569: <option value="2021">2021</option>
  570: <option value="2022">2022</option>
  571: <option value="2023">2023</option>
  572: <option value="2024">2024</option>
  573: <option value="2025">2025</option>
  574: <option value="2026">2026</option>
  575: <option value="2027">2027</option>
  576: <option value="2028">2028</option>
  577: <option value="2029">2029</option>
  578: <option value="2030">2030</option>
  579: <option value="2031">2031</option>
  580: <option value="2032">2032</option>
  581: <option value="2033">2033</option>
  582: <option value="2034">2034</option>
  583: <option value="2035">2035</option>
  584: <option value="2036">2036</option>
  585: <option value="2037">2037</option>
  586: <option value="2038">2038</option>
  587: <option value="2039">2039</option>
  588: <option value="2040">2040</option>
  589: <option value="2041">2041</option>
  590: <option value="2042">2042</option>
  591: <option value="2043">2043</option>
  592: <option value="2044">2044</option>
  593: <option value="2045">2045</option>
  594: <option value="2046">2046</option>
  595: <option value="2047">2047</option>
  596: <option value="2048">2048</option>
  597: <option value="2049">2049</option>
  598: <option value="2050">2050</option>
  599: <option value="2051">2051</option>
  600: </select>
  601: END
  602:     $year=~s/(\"$currentyear\")/$1 SELECTED/ if length($currentyear);
  603:     return "$month$day$year";
  604: }
  605: 
  606: sub selectbox {
  607:     my ($title,$name,$value,%options)=@_;
  608:     my $uctitle=uc($title);
  609:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
  610: 	"</b></font><br />".'<select name="'.$name.'">';
  611:     map {
  612:         $selout.='<option value=\''.$_.'\'';
  613:         if ($_ eq $value) { $selout.=' selected'; }
  614:         $selout.='>'.$options{$_}.'</option>';
  615:     } sort keys %options;
  616:     return $selout.'</select>';
  617: }
  618: 
  619: # ----------------------------------------------- Performing an advanced search
  620: sub advancedsearch {
  621:     my ($r,$envhash)=@_;
  622:     my %ENV=%{$envhash};
  623: 
  624:     my $fillflag=0;
  625:     # Clean up fields for safety
  626:     for my $field ('title','author','subject','keywords','url','version',
  627: 		   'creationdatestart_month','creationdatestart_day',
  628: 		   'creationdatestart_year','creationdateend_month',
  629: 		   'creationdateend_day','creationdateend_year',
  630: 		   'lastrevisiondatestart_month','lastrevisiondatestart_day',
  631: 		   'lastrevisiondatestart_year','lastrevisiondateend_month',
  632: 		   'lastrevisiondateend_day','lastrevisiondateend_year',
  633: 		   'notes','abstract','mime','language','owner',
  634: 		   'custommetadata','customshow') {
  635: 	$ENV{"form.$field"}=~s/[^\w\s\(\)\=\-\"\']//g;
  636:     }
  637: 
  638:     # Check to see if enough information was filled in
  639:     for my $field ('title','author','subject','keywords','url','version',
  640: 		   'notes','abstract','mime','language','owner',
  641: 		   'custommetadata') {
  642: 	if (&filled($ENV{"form.$field"})) {
  643: 	    $fillflag++;
  644: 	}
  645:     }
  646:     unless ($fillflag) {
  647: 	&output_blank_field_error($r);
  648: 	return OK;
  649:     }
  650: 
  651: 
  652:     # Turn the form input into a SQL-based query
  653:     my $query='';
  654: 
  655:     my @queries;
  656:     # Evaluate logical expression AND/OR/NOT phrase fields.
  657:     foreach my $field ('title','author','subject','notes','abstract','url',
  658: 		       'keywords','version','owner') {
  659: 	if ($ENV{'form.'.$field}) {
  660: 	    push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
  661: 	}
  662:     }
  663:     # Evaluate option lists
  664:     if ($ENV{'form.language'} and $ENV{'form.language'} ne 'any') {
  665: 	push @queries,"(language like \"$ENV{'form.language'}\")";
  666:     }
  667:     if ($ENV{'form.mime'} and $ENV{'form.mime'} ne 'any') {
  668: 	push @queries,"(mime like \"$ENV{'form.mime'}\")";
  669:     }
  670:     if ($ENV{'form.copyright'} and $ENV{'form.copyright'} ne 'any') {
  671: 	push @queries,"(copyright like \"$ENV{'form.copyright'}\")";
  672:     }
  673:     # Evaluate date windows
  674:     my $datequery=&build_date_queries(
  675: 			$ENV{'form.creationdatestart_month'},
  676: 			$ENV{'form.creationdatestart_day'},
  677: 			$ENV{'form.creationdatestart_year'},
  678: 			$ENV{'form.creationdateend_month'},
  679: 			$ENV{'form.creationdateend_day'},
  680: 			$ENV{'form.creationdateend_year'},
  681: 			$ENV{'form.lastrevisiondatestart_month'},
  682: 			$ENV{'form.lastrevisiondatestart_day'},
  683: 			$ENV{'form.lastrevisiondatestart_year'},
  684: 			$ENV{'form.lastrevisiondateend_month'},
  685: 			$ENV{'form.lastrevisiondateend_day'},
  686: 			$ENV{'form.lastrevisiondateend_year'},
  687: 			);
  688:     # Test to see if date windows are legitimate
  689:     if ($datequery=~/^Incorrect/) {
  690: 	&output_date_error($r,$datequery);
  691: 	return OK;
  692:     }
  693:     elsif ($datequery) {
  694: 	push @queries,$datequery;
  695:     }
  696: 
  697:     # Process form information for custom metadata querying
  698:     my $customquery='';
  699:     if ($ENV{'form.custommetadata'}) {
  700: 	$customquery=&build_custommetadata_query('custommetadata',
  701: 				      $ENV{'form.custommetadata'});
  702:     }
  703:     my $customshow='';
  704:     if ($ENV{'form.customshow'}) {
  705: 	$customshow=$ENV{'form.customshow'};
  706: 	$customshow=~s/[^\w\s]//g;
  707: 	my @fields=split(/\s+/,$customshow);
  708: 	$customshow=join(" ",@fields);
  709:     }
  710:     # Send query statements over the network to be processed by either the SQL
  711:     # database or a recursive scheme of 'grep'-like actions (for custom
  712:     # metadata).
  713:     if (@queries) {
  714: 	$query=join(" AND ",@queries);
  715: 	$query="select * from metadata where $query";
  716: 	my $reply; # reply hash reference
  717: 	unless ($customquery or $customshow) {
  718: 	    $reply=&Apache::lonnet::metadata_query($query);
  719: 	}
  720: 	else {
  721: 	    $reply=&Apache::lonnet::metadata_query($query,
  722: 						   $customquery,$customshow);
  723: 	}
  724: 	&output_results('Advanced',$r,$envhash,$customquery,$reply);
  725:     }
  726:     elsif ($customquery) {
  727: 	my $reply; # reply hash reference
  728: 	$reply=&Apache::lonnet::metadata_query('',
  729: 					       $customquery,$customshow);
  730: 	&output_results('Advanced',$r,$envhash,$customquery,$reply);
  731:     }
  732:     # should not get to this point
  733:     return 'Error.  Should not have gone to this point.';
  734: }
  735: 
  736: # --------------------------------------------------- Performing a basic search
  737: sub basicsearch {
  738:     my ($r,$envhash)=@_;
  739:     my %ENV=%{$envhash};
  740:     # Clean up fields for safety
  741:     for my $field ('basicexp') {
  742: 	$ENV{"form.$field"}=~s/[^\w\s\(\)\-]//g;
  743:     }
  744: 
  745:     # Check to see if enough is filled in
  746:     unless (&filled($ENV{'form.basicexp'})) {
  747: 	&output_blank_field_error($r);
  748: 	return OK;
  749:     }
  750: 
  751:     # Build SQL query string based on form page
  752:     my $query='';
  753:     my $concatarg=join(',"    ",',
  754: 		       ('title', 'author', 'subject', 'notes', 'abstract'));
  755:     $concatarg='title' if $ENV{'form.titleonly'};
  756: 
  757:     $query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
  758: 
  759:     # Get reply (either a hash reference to filehandles or bad connection)
  760:     my $reply=&Apache::lonnet::metadata_query('select * from metadata where '.$query);
  761: 
  762:     # Output search results
  763: 
  764:     &output_results('Basic',$r,$envhash,$query,$reply);
  765: 
  766:     return OK;
  767: }
  768: 
  769: # ------------------------------------------------------------- build_SQL_query
  770: sub build_SQL_query {
  771:     my ($field_name,$logic_statement)=@_;
  772:     my $q=new Text::Query('abc',
  773: 			  -parse => 'Text::Query::ParseAdvanced',
  774: 			  -build => 'Text::Query::Build');
  775:     $q->prepare($logic_statement);
  776:     my $matchexp=${$q}{'matchexp'}; chomp $matchexp;
  777:     my $sql_query=&recursive_SQL_query_build($field_name,$matchexp);
  778:     return $sql_query;
  779: }
  780: 
  781: # ------------------------------------------------- build custom metadata query
  782: sub build_custommetadata_query {
  783:     my ($field_name,$logic_statement)=@_;
  784:     my $q=new Text::Query('abc',
  785: 			  -parse => 'Text::Query::ParseAdvanced',
  786: 			  -build => 'Text::Query::BuildAdvancedString');
  787:     $q->prepare($logic_statement);
  788:     my $matchexp=${$q}{'-parse'}{'-build'}{'matchstring'};
  789:     # quick fix to change literal into xml tag-matching
  790:     # will eventually have to write a separate builder module
  791:     my $oldmatchexp=$matchexp;
  792:     $matchexp=~s/(\w+)\\\=([\w\\\+]+)/\\\<$1\\\>\[\^\\\<\]\*$2\[\^\\\<\]\*\\\<\\\/$1\\\>/g;
  793:     return $matchexp;
  794: }
  795: 
  796: # - Recursively parse a reverse notation expression into a SQL query expression
  797: sub recursive_SQL_query_build {
  798:     my ($dkey,$pattern)=@_;
  799:     my @matches=($pattern=~/(\[[^\]|\[]*\])/g);
  800:     return $pattern unless @matches;
  801:     foreach my $match (@matches) {
  802: 	$match=~/\[ (\w+)\s(.*) \]/;
  803: 	my ($key,$value)=($1,$2);
  804: 	my $replacement='';
  805: 	if ($key eq 'literal') {
  806: 	    $replacement="($dkey like \"\%$value\%\")";
  807: 	}
  808: 	elsif ($key eq 'not') {
  809: 	    $value=~s/like/not like/;
  810: #	    $replacement="($dkey not like $value)";
  811: 	    $replacement="$value";
  812: 	}
  813: 	elsif ($key eq 'and') {
  814: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
  815: 	    $replacement="($1 AND $2)";
  816: 	}
  817: 	elsif ($key eq 'or') {
  818: 	    $value=~/(.*[\"|\)]) ([|\(|\^].*)/;
  819: 	    $replacement="($1 OR $2)";
  820: 	}
  821: 	substr($pattern,
  822: 	       index($pattern,$match),
  823: 	       length($match),
  824: 	       $replacement
  825: 	       );
  826:     }
  827:     &recursive_SQL_query_build($dkey,$pattern);
  828: }
  829: 
  830: # ------------------------------------------------------------ Build date query
  831: sub build_date_queries {
  832:     my ($cmonth1,$cday1,$cyear1,$cmonth2,$cday2,$cyear2,
  833: 	$lmonth1,$lday1,$lyear1,$lmonth2,$lday2,$lyear2)=@_;
  834:     my @queries;
  835:     if ($cmonth1 or $cday1 or $cyear1 or $cmonth2 or $cday2 or $cyear2) {
  836: 	unless ($cmonth1 and $cday1 and $cyear1 and
  837: 		$cmonth2 and $cday2 and $cyear2) {
  838: 	    return "Incorrect entry for the creation date.  You must specify ".
  839: 		   "a starting month, day, and year and an ending month, ".
  840: 		   "day, and year.";
  841: 	}
  842: 	my $cnumeric1=sprintf("%d%2d%2d",$cyear1,$cmonth1,$cday1);
  843: 	$cnumeric1+=0;
  844: 	my $cnumeric2=sprintf("%d%2d%2d",$cyear2,$cmonth2,$cday2);
  845: 	$cnumeric2+=0;
  846: 	if ($cnumeric1>$cnumeric2) {
  847: 	    return "Incorrect entry for the creation date.  The starting ".
  848: 		   "date must occur before the ending date.";
  849: 	}
  850: 	my $cquery="(creationdate BETWEEN '$cyear1-$cmonth1-$cday1' AND '".
  851: 	           "$cyear2-$cmonth2-$cday2 23:59:59')";
  852: 	push @queries,$cquery;
  853:     }
  854:     if ($lmonth1 or $lday1 or $lyear1 or $lmonth2 or $lday2 or $lyear2) {
  855: 	unless ($lmonth1 and $lday1 and $lyear1 and
  856: 		$lmonth2 and $lday2 and $lyear2) {
  857: 	    return "Incorrect entry for the last revision date.  You must ".
  858: 		   "specify a starting month, day, and year and an ending ".
  859: 		   "month, day, and year.";
  860: 	}
  861: 	my $lnumeric1=sprintf("%d%2d%2d",$lyear1,$lmonth1,$lday1);
  862: 	$lnumeric1+=0;
  863: 	my $lnumeric2=sprintf("%d%2d%2d",$lyear2,$lmonth2,$lday2);
  864: 	$lnumeric2+=0;
  865: 	if ($lnumeric1>$lnumeric2) {
  866: 	    return "Incorrect entry for the last revision date.  The ".
  867: 		   "starting date must occur before the ending date.";
  868: 	}
  869: 	my $lquery="(lastrevisiondate BETWEEN '$lyear1-$lmonth1-$lday1' AND '".
  870: 	           "$lyear2-$lmonth2-$lday2 23:59:59')";
  871: 	push @queries,$lquery;
  872:     }
  873:     if (@queries) {
  874: 	return join(" AND ",@queries);
  875:     }
  876:     return '';
  877: }
  878: 
  879: # ----------------------------- format and output results based on a reply list
  880: # There are two windows that this function writes to.  The main search
  881: # window ("srch") has a listing of the results.  A secondary window ("popwin")
  882: # gives the status of the network search (time elapsed, number of machines
  883: # contacted, etc.)
  884: sub output_results {
  885:     my ($mode,$r,$envhash,$query,$replyref)=@_;
  886:     my %ENV=%{$envhash};
  887:     my %rhash=%{$replyref};
  888:     my $compiledresult='';
  889:     my $timeremain=30;
  890:     my $elapsetime=0;
  891:     my $resultflag=0;
  892:     my $tflag=1;
  893: 
  894:     # make query information persistent to allow for subsequent revision
  895:     my $persistent=&make_persistent();
  896: 
  897:     # output beginning of search page
  898: 	$r->print(<<BEGINNING);
  899: <html>
  900: <head>
  901: <title>The LearningOnline Network with CAPA</title>
  902: BEGINNING
  903: 
  904:     # conditional output of script functions dependent on the mode in
  905:     # which the search was invoked
  906:         $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'interactive';
  907: <script>
  908:     function select_data(title,url) {
  909: 	changeTitle(title);
  910: 	changeURL(url);
  911: 	self.close();
  912:     }
  913:     function changeTitle(val) {
  914: 	if (opener.inf.document.forms.resinfo.elements.t) {
  915: 	    opener.inf.document.forms.resinfo.elements.t.value=val;
  916: 	}
  917:     }
  918:     function changeURL(val) {
  919: 	if (opener.inf.document.forms.resinfo.elements.u) {
  920: 	    opener.inf.document.forms.resinfo.elements.u.value=val;
  921: 	}
  922:     }
  923: </script>
  924: SCRIPT
  925:         $r->print(<<SCRIPT) if $ENV{'form.catalogmode'} eq 'groupsearch';
  926: <script>
  927:     function select_data(title,url) {
  928: 	alert('DEBUG: Should be storing '+title+' and '+url);
  929:     }
  930:     function queue(val) {
  931: 	if (eval("document.forms."+val+".filelink.checked")) {
  932: 	    document.forms.results.acts.value+='1a'+val+'b';
  933: 	}
  934: 	else {
  935: 	    document.forms.results.acts.value+='0a'+val+'b';
  936: 	}
  937:     }
  938:     function select_group() {
  939: 	alert('DEBUG: Should be moving on to group sorting stage'+' acts');
  940:     }
  941: </script>
  942: SCRIPT
  943:         $r->print(<<SCRIPT);                                            ;
  944: <script>
  945:     function displayinfo(val) {
  946: 	popwin.document.forms.popremain.sdetails.value=val;
  947:     }
  948: </script>
  949: SCRIPT
  950:     $r->rflush();
  951: 
  952:     # begin showing the cataloged results
  953:     $r->print(<<CATALOGBEGIN);
  954: </head>
  955: <body bgcolor="#ffffff">
  956: <img align=right src=/adm/lonIcons/lonlogos.gif>
  957: <h1>Search Catalog</h1>
  958: CATALOGBEGIN
  959:         $r->print(<<CATALOGCONTROLS);
  960: <form name='results' method="post" action="/adm/searchcat">
  961: <input type='hidden' name='acts' value='' />
  962: <input type='button' value='Revise search request'
  963: onClick='this.form.submit();' />
  964: $importbutton
  965: $closebutton
  966: $persistent
  967: <hr />
  968: <h3>Search Query</h3>
  969: CATALOGCONTROLS
  970:     if ($mode eq 'Basic') {
  971: 	$r->print(<<RESULTS);
  972: <p>
  973: <b>Basic search:</b> $ENV{'form.basicexp'}
  974: </p>
  975: RESULTS
  976:     }
  977:     elsif ($mode eq 'Advanced') {
  978: 	$r->print(<<RESULTS);
  979: <p>
  980: <b>Advanced search</b>
  981: $query
  982: </p>
  983: RESULTS
  984:     }
  985:     $r->print('<h3>Search Results</h3>');
  986:     $r->rflush();
  987:     my $servernum=(keys %rhash)+0;
  988: 
  989:     # define server grid (shows status of multiple machines)
  990:     my $hcinit;
  991:     my $grid="'<br />'+";
  992:     $grid.="\n";
  993:     my $sn=1;
  994:     for my $sk (sort keys %rhash) {
  995: 	# '<a href="
  996: 	$grid.="'<a href=\"";
  997: 	# javascript:displayinfo('+
  998: 	$grid.="javascript:opener.displayinfo('+";
  999: 	# "'"+'key
 1000: 	$grid.="\"'\"+'";
 1001: 	$grid.=$sk." domain=".$hostdomains{$sk};
 1002: 	my $hc;
 1003: 	if ($rhash{$sk} eq 'con_lost') {
 1004: 	    $hc="!!!BAD CONNECTION, CONTACT SYSTEM ADMINISTRATOR!!!";
 1005: 	}
 1006: 	else {
 1007: 	    $hc="'+\"'\"+\"+hc['$sk']+\"+\"'\"+'";
 1008: 	    $hcinit.="hc[\"$sk\"]=0;";
 1009: 	}
 1010: 	$grid.=" hitcount=".$hc;
 1011: 	$grid.=" IP=".$hostips{$sk};
 1012: 	# '+"'"+'">'+
 1013: 	$grid.="'+\"'\"+')\">'+";
 1014: 	$grid.="\n";
 1015: 	$grid.="'<img border=\"0\" name=\"img".$sn."\"".
 1016: 	    " src=\"/adm/lonIcons/srvbad.gif\" alt=\"".$sk."\" /></a>'+\n";
 1017: 	$grid.="'<br />'+\n" unless $sn%10;
 1018:         $sn++;
 1019:     }
 1020: 	    $r->print(<<ENDPOP);
 1021: <script>
 1022:     popwin=open('','popwin','scrollbars=1,width=400,height=200');
 1023:     popwin.focus();
 1024:     popwin.document.writeln('<'+'html>');
 1025:     popwin.document.writeln('<'+'head>');
 1026:     popwin.document.writeln('<'+'script>');
 1027:     popwin.document.writeln('hc=new Array();$hcinit');
 1028:     popwin.document.writeln('<'+'/script>');
 1029:     popwin.document.writeln('<'+'/head>'+
 1030:         '<'+'body bgcolor="#FFFFFF">'+
 1031: 	'<'+'h3>Search Results Progress<'+'/h3>'+
 1032:         '<'+'form name="popremain">'+
 1033:         '<'+'br />Server space <'+'input type=text size=25 name=space value="">'+
 1034:         '<'+'br />Status <'+'input type=text size=25 name=status value="">'+
 1035:         '<'+'br />Maximum remaining time <'+'input type=text size=25 name=timeout '+
 1036: 	'value="30">'+
 1037:         '<'+'tt>'+
 1038: 	'<'+'image align="right" src="/adm/lonIcons/'+
 1039: 	'lonanim.gif" '+
 1040: 	'alt="animated logo" />'+
 1041: 	'<'+'br /><i>PLEASE BE PATIENT</i>'+
 1042: 	'<'+'br />SCANNING $servernum SERVERS'+
 1043: 	'<'+'br clear="all" />Number of record hits found '+
 1044: 	'<'+'input type="text" size="10" name="numhits"'+
 1045: 	' value="0" />'+
 1046: 	'<'+'br clear="all" />Time elapsed '+
 1047: 	'<'+'input type="text" size="10" name="elapsetime"'+
 1048: 	' value="0" />'+
 1049: 	'<'+'br />'+
 1050: 	'SERVER GRID (click on any cell for details)'+
 1051:         $grid
 1052:         '<'+'br />'+
 1053: 	'Server details '+
 1054: 	'<'+'input type="text" size="25" name="sdetails"'+
 1055: 	' value="" />'+
 1056: 	'<'+'br />'+
 1057: 	'<'+'input type="button" name="button"'+
 1058: 	' value="revise search" />'+
 1059: 	' <'+'input type="button" name="button"'+
 1060: 	' value="view results" />'+
 1061: 	' <'+'input type="button" name="button"'+
 1062: 	' value="help" />'+
 1063: 	'<'+'/tt>'+
 1064:         '<'+'/form>'+
 1065:         '<'+'/body><'+'/html>');
 1066:     popwin.document.close();
 1067: </script>
 1068: ENDPOP
 1069:     $r->rflush();
 1070: 
 1071:     $r->print('<script>popwin.document.popremain.space.value="'.
 1072: 	      $servernum.', 0%, count=0/'.$servernum.'";</script>');
 1073:     $r->rflush(); 
 1074:     my $servercount=0;
 1075:     $sn=0;
 1076:     my $hitcountsum=0;
 1077:     foreach my $rkey (sort keys %rhash) {
 1078: 	$sn++;
 1079: 	$servercount++;
 1080: 	$tflag=1;
 1081: 	$compiledresult='';
 1082: 	my $hostname=$rkey;
 1083: 	$r->print('<script>popwin.document.popremain.status.value="'.
 1084: 		  $rkey.', trying contact";</script>'."\n");
 1085: 	$r->rflush();
 1086: 	my $reply=$rhash{$rkey};
 1087: 	my @results;
 1088: 	
 1089: 	my $replyfile='';
 1090: 
 1091: 	if ($reply eq 'con_lost') {
 1092: 	    my $percent=sprintf('%3.0f',($servercount/$servernum*100));
 1093: 	    $r->print('<script>popwin.document.popremain.space.value="'.
 1094: 		      $servernum.', '.$percent.'%, count='.$servercount.
 1095: 		      '/'.$servernum.'";</script>');
 1096: 	}
 1097: 	else {
 1098: 	    $reply=~/^([\.\w]+)$/; # must do since 'use strict' checks for tainting
 1099: 	    $replyfile=$r->dir_config('lonDaemons').'/tmp/'.$1;
 1100: 	    $reply=~/(.*?)\_/;
 1101: 	    {
 1102: 		my $temp=0;
 1103: 	      WLOOP: while (1) {
 1104: 		  if (-e $replyfile && $tflag) {
 1105: 		      $r->print('<script>popwin.document.popremain.status.'.
 1106: 				'value="'.$rkey.', transmitting";</script>'.
 1107: 				"\n");
 1108: 		      $r->rflush();
 1109: 		      $r->print('<script>popwin.document.img'.$sn.'.'.
 1110: 				'src="/adm/lonIcons/srvempty.gif";</script>'.
 1111: 				"\n");
 1112: 		      $r->rflush();
 1113: 		      $tflag=0;
 1114: 		  }
 1115: 		  last WLOOP if $temp>5;
 1116: 		  if (-e "$replyfile.end") {
 1117: 		      if (-s $replyfile) {
 1118: 			  $r->print('<script>popwin.document.img'.$sn.'.'.
 1119: 				'src="/adm/lonIcons/srvgood.gif";</script>'.
 1120: 				"\n");
 1121: 			  $r->rflush();
 1122: 			  my $fh=Apache::File->new($replyfile) or 
 1123: 			      ($r->print('ERROR: file '.
 1124: 					 $replyfile.' cannot be opened') and
 1125: 			       return OK);
 1126: 			  @results=<$fh> if $fh;
 1127: 			  $hitcount{$rkey}=@results+0;
 1128: 			  $r->print('<script>popwin.hc["'.$rkey.'"]='.
 1129: 				    $hitcount{$rkey}.';</script>'.
 1130: 				    "\n");
 1131: 			  $r->rflush();
 1132: 			  $hitcountsum+=$hitcount{$rkey};
 1133: 			  $r->print('<script>popwin.document.forms.popremain.'.
 1134: 				    'numhits.value='.$hitcountsum.
 1135: 				    ';</script>'.
 1136: 				    "\n");
 1137: 			  $r->rflush();
 1138: 		      }
 1139: 		      last WLOOP;
 1140: 		  }
 1141: 		  last WLOOP unless $timeremain;
 1142: 		  sleep 1;
 1143: 		  $timeremain--;
 1144: 		  $elapsetime++;
 1145: 		  $r->print('<script>popwin.document.popremain.timeout.'.
 1146: 			    'value="'.$timeremain.'";</script>'."\n");
 1147: 		  $r->rflush();
 1148: 		  $r->print('<script>popwin.document.popremain.elapsetime.'.
 1149: 			    'value="'.$elapsetime.'";</script>'."\n");
 1150: 		  $r->rflush();
 1151: 		  $temp++;
 1152: 	      }
 1153: 	    }
 1154: 	}
 1155: 	my $customshow='';
 1156: 	my $extrashow='';
 1157: 	my @customfields;
 1158: 	if ($ENV{'form.customshow'}) {
 1159: 	    $customshow=$ENV{'form.customshow'};
 1160: 	    $customshow=~s/[^\w\s]//g;
 1161: 	    my @fields=map {"<font color=\"#008000\">$_:</font><!-- $_ -->"} 
 1162: 	    split(/\s+/,$customshow);
 1163: 	    @customfields=split(/\s+/,$customshow);
 1164: 	    if ($customshow) {
 1165: 		$extrashow="<ul><li>".join("</li><li>",@fields)."</li></ul>\n";
 1166: 	    }
 1167: 	}
 1168: 	my $customdata='';
 1169: 	my %customhash;
 1170: 	foreach my $result (@results) {
 1171: 	    if ($result=~/^(custom\=.*)$/) { # grab all custom metadata
 1172: 		my $tmp=$result;
 1173: 		$tmp=~s/^custom\=//;
 1174: 		my ($k,$v)=map {&Apache::lonnet::unescape($_);
 1175: 			    } split(/\,/,$tmp);
 1176: 		$customhash{$k}=$v;
 1177: 	    }
 1178: 	}
 1179: 	foreach my $result (@results) {
 1180: 	    next if $result=~/^custom\=/;
 1181: 	    chomp $result;
 1182: 	    next unless $result;
 1183: 	    my @fields=map
 1184: 	    {&Apache::lonnet::unescape($_)}
 1185: 	    (split(/\,/,$result));
 1186: 	    my ($title,$author,$subject,$url,$keywords,$version,
 1187: 		$notes,$abstract,$mime,$lang,
 1188: 		$creationdate,$lastrevisiondate,$owner,$copyright)=@fields;
 1189: 	    my $shortabstract=$abstract;
 1190: 	    $shortabstract=substr($abstract,0,200) if length($abstract)>200;
 1191: 	    $fields[7]=$shortabstract;
 1192: 	    my $extrashow2=$extrashow;
 1193: 	    if ($extrashow) {
 1194: 		foreach my $field (@customfields) {
 1195: 		    my $value='';
 1196: 		    if ($customhash{$url}=~/\<${field}[^\>]*\>(.*?)\<\/${field}[^\>]*\>/s) {
 1197: 		        $value=$1;
 1198: 		    }
 1199: 	            $extrashow2=~s/\<\!\-\- $field \-\-\>/ $value/g;
 1200: 	        }
 1201:             }
 1202: 	
 1203:             $compiledresult.=<<END if $compiledresult or $servercount!=$servernum;
 1204: <hr align='left' width='200' noshade />
 1205: END
 1206:             $compiledresult.=<<END;
 1207: <p>
 1208: END
 1209:             $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'interactive';
 1210: <font size='-1'><INPUT TYPE="button" NAME="returnvalues" VALUE="SELECT"
 1211: onClick="javascript:select_data('$title','$url')">
 1212: </font>
 1213: <br />
 1214: END
 1215:             $compiledresult.=<<END if $ENV{'form.catalogmode'} eq 'groupsearch';
 1216: <font size='-1'><input type="checkbox" name="returnvalues" value="SELECT"
 1217: onClick="javascript:queue($fnum)" />
 1218: </font>
 1219: <input type="hidden" name="title$fnum" value="$title" />
 1220: <input type="hidden" name="url$fnum" value="$url" />
 1221: <br />
 1222: END
 1223:             my $httphost=$ENV{'HTTP_HOST'};
 1224: 
 1225:             my $viewselect;
 1226:             if ($mode eq 'Basic') {
 1227: 		$viewselect=$ENV{'form.basicviewselect'};
 1228: 	    }
 1229:             elsif ($mode eq 'Advanced') {
 1230: 	        $viewselect=$ENV{'form.advancedviewselect'};
 1231:             }
 1232: 
 1233:             if ($viewselect eq 'Detailed Citation View') {
 1234: 	        $compiledresult.=&detailed_citation_view(@fields,
 1235: 						 $hostname,$httphost,
 1236: 						 $extrashow2);
 1237: 	    }
 1238:             elsif ($viewselect eq 'Summary View') {
 1239: 		$compiledresult.=&summary_view(@fields,$hostname,$httphost,
 1240: 				       $extrashow2);
 1241: 	    }
 1242:             elsif ($viewselect eq 'Fielded Format') {
 1243: 		$compiledresult.=&fielded_format_view(@fields,$hostname,
 1244: 					      $httphost,$extrashow2);
 1245: 	    }
 1246:             elsif ($viewselect eq 'XML/SGML') {
 1247: 		$compiledresult.=&xml_sgml_view(@fields,$hostname,$httphost,
 1248: 					$extrashow2);
 1249: 	    }
 1250:     
 1251:         }
 1252: 
 1253: 	if ($compiledresult) {
 1254: 	    $resultflag=1;
 1255: 	}
 1256: 
 1257: 	$r->print(<<RESULTS);
 1258: $compiledresult
 1259: RESULTS
 1260:         my $percent=sprintf('%3.0f',($servercount/$servernum*100));
 1261: 	$r->print('<script>popwin.document.popremain.space.value="'.
 1262: 	          $servernum.', '.$percent.'%, count='.$servercount.
 1263: 		  '/'.$servernum.'";</script>'."\n");
 1264:     }
 1265:     unless ($resultflag) {
 1266:         $r->print("\nThere were no results that matched your query\n");
 1267:     }
 1268: #    $r->print('<script>popwin.close()</script>'."\n"); $r->rflush(); 
 1269:     $r->print(<<RESULTS);
 1270: </body>
 1271: </html>
 1272: RESULTS
 1273: }
 1274: 
 1275: # ------------------------------------------------------ Detailed Citation View
 1276: sub detailed_citation_view {
 1277:     my ($title,$author,$subject,$url,$keywords,$version,
 1278: 	$notes,$shortabstract,$mime,$lang,
 1279: 	$creationdate,$lastrevisiondate,$owner,$copyright,
 1280: 	$hostname,$httphost,$extrashow)=@_;
 1281:     my $result=<<END;
 1282: <i>$owner</i>, last revised $lastrevisiondate
 1283: <h3><A HREF="http://$httphost$url" TARGET='search_preview'>$title</A></h3>
 1284: <h3>$author</h3>
 1285: </p>
 1286: <p>
 1287: <b>Subject:</b> $subject<br />
 1288: <b>Keyword(s):</b> $keywords<br />
 1289: <b>Notes:</b> $notes<br />
 1290: <b>MIME Type:</b> $mimetag{$mime}<br />
 1291: <b>Language:</b> $language{$lang}<br />
 1292: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
 1293: </p>
 1294: $extrashow
 1295: <p>
 1296: $shortabstract
 1297: </p>
 1298: END
 1299:     return $result;
 1300: }
 1301: 
 1302: # ---------------------------------------------------------------- Summary View
 1303: sub summary_view {
 1304:     my ($title,$author,$subject,$url,$keywords,$version,
 1305: 	$notes,$shortabstract,$mime,$lang,
 1306: 	$creationdate,$lastrevisiondate,$owner,$copyright,
 1307: 	$hostname,$httphost,$extrashow)=@_;
 1308:     my $result=<<END;
 1309: <a href="http://$httphost$url" TARGET='search_preview'>$author</a><br />
 1310: $title<br />
 1311: $owner -- $lastrevisiondate<br />
 1312: $cprtag{$copyright}<br />
 1313: $extrashow
 1314: </p>
 1315: END
 1316:     return $result;
 1317: }
 1318: 
 1319: # -------------------------------------------------------------- Fielded Format
 1320: sub fielded_format_view {
 1321:     my ($title,$author,$subject,$url,$keywords,$version,
 1322: 	$notes,$shortabstract,$mime,$lang,
 1323: 	$creationdate,$lastrevisiondate,$owner,$copyright,
 1324: 	$hostname,$httphost,$extrashow)=@_;
 1325:     my $result=<<END;
 1326: <b>URL: </b> <A HREF="http://$httphost$url" TARGET='search_preview'>$url</A>
 1327: <br />
 1328: <b>Title:</b> $title<br />
 1329: <b>Author(s):</b> $author<br />
 1330: <b>Subject:</b> $subject<br />
 1331: <b>Keyword(s):</b> $keywords<br />
 1332: <b>Notes:</b> $notes<br />
 1333: <b>MIME Type:</b> $mimetag{$mime}<br />
 1334: <b>Language:</b> $language{$lang}<br />
 1335: <b>Creation Date:</b> $creationdate<br />
 1336: <b>Last Revision Date:</b> $lastrevisiondate<br />
 1337: <b>Publisher/Owner:</b> $owner<br />
 1338: <b>Copyright/Distribution:</b> $cprtag{$copyright}<br />
 1339: <b>Repository Location:</b> $hostname<br />
 1340: <b>Abstract:</b> $shortabstract<br />
 1341: $extrashow
 1342: </p>
 1343: END
 1344:     return $result;
 1345: }
 1346: 
 1347: # -------------------------------------------------------------------- XML/SGML
 1348: sub xml_sgml_view {
 1349:     my ($title,$author,$subject,$url,$keywords,$version,
 1350: 	$notes,$shortabstract,$mime,$lang,
 1351: 	$creationdate,$lastrevisiondate,$owner,$copyright,
 1352: 	$hostname,$httphost,$extrashow)=@_;
 1353:     my $result=<<END;
 1354: <pre>
 1355: &lt;LonCapaResource&gt;
 1356: &lt;url&gt;$url&lt;/url&gt;
 1357: &lt;title&gt;$title&lt;/title&gt;
 1358: &lt;author&gt;$author&lt;/author&gt;
 1359: &lt;subject&gt;$subject&lt;/subject&gt;
 1360: &lt;keywords&gt;$keywords&lt;/keywords&gt;
 1361: &lt;notes&gt;$notes&lt;/notes&gt;
 1362: &lt;mimeInfo&gt;
 1363: &lt;mime&gt;$mime&lt;/mime&gt;
 1364: &lt;mimetag&gt;$mimetag{$mime}&lt;/mimetag&gt;
 1365: &lt;/mimeInfo&gt;
 1366: &lt;languageInfo&gt;
 1367: &lt;language&gt;$lang&lt;/language&gt;
 1368: &lt;languagetag&gt;$language{$lang}&lt;/languagetag&gt;
 1369: &lt;/languageInfo&gt;
 1370: &lt;creationdate&gt;$creationdate&lt;/creationdate&gt;
 1371: &lt;lastrevisiondate&gt;$lastrevisiondate&lt;/lastrevisiondate&gt;
 1372: &lt;owner&gt;$owner&lt;/owner&gt;
 1373: &lt;copyrightInfo&gt;
 1374: &lt;copyright&gt;$copyright&lt;/copyright&gt;
 1375: &lt;copyrighttag&gt;$cprtag{$copyright}&lt;/copyrighttag&gt;
 1376: &lt;/copyrightInfo&gt;
 1377: &lt;repositoryLocation&gt;$hostname&lt;/repositoryLocation&gt;
 1378: &lt;shortabstract&gt;$shortabstract&lt;/shortabstract&gt;
 1379: &lt;/LonCapaResource&gt;
 1380: </pre>
 1381: $extrashow
 1382: END
 1383:     return $result;
 1384: }
 1385: 
 1386: # ---------------------------------------------------- see if a field is filled
 1387: sub filled {
 1388:     my ($field)=@_;
 1389:     if ($field=~/\S/ && $field ne 'any') {
 1390: 	return 1;
 1391:     }
 1392:     else {
 1393: 	return 0;
 1394:     }
 1395: }
 1396: 
 1397: # ---------------- Message to output when there are not enough fields filled in
 1398: sub output_blank_field_error {
 1399:     my ($r)=@_;
 1400:     # make query information persistent to allow for subsequent revision
 1401:     my $persistent=&make_persistent();
 1402: 
 1403:     $r->print(<<BEGINNING);
 1404: <html>
 1405: <head>
 1406: <title>The LearningOnline Network with CAPA</title>
 1407: BEGINNING
 1408:     $r->print(<<RESULTS);
 1409: </head>
 1410: <body bgcolor="#ffffff">
 1411: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
 1412: <h1>Search Catalog</h1>
 1413: <form method="post" action="/adm/searchcat">
 1414: $persistent
 1415: <input type='button' value='Revise search request'
 1416: onClick='this.form.submit();' />
 1417: $closebutton
 1418: <hr />
 1419: <h3>Helpful Message</h3>
 1420: <p>
 1421: Incorrect search query due to blank entry fields.
 1422: You need to fill in the relevant
 1423: fields on the search page in order for a query to be
 1424: processed.
 1425: </p>
 1426: </body>
 1427: </html>
 1428: RESULTS
 1429: }
 1430: 
 1431: # ----------------------------------------------------------- Output date error
 1432: sub output_date_error {
 1433:     my ($r,$message)=@_;
 1434:     # make query information persistent to allow for subsequent revision
 1435:     my $persistent=&make_persistent();
 1436: 
 1437:     $r->print(<<BEGINNING);
 1438: <html>
 1439: <head>
 1440: <title>The LearningOnline Network with CAPA</title>
 1441: BEGINNING
 1442:     $r->print(<<RESULTS);
 1443: </head>
 1444: <body bgcolor="#ffffff">
 1445: <img align='right' src='/adm/lonIcons/lonlogos.gif' />
 1446: <h1>Search Catalog</h1>
 1447: <form method="post" action="/adm/searchcat">
 1448: $persistent
 1449: <input type='button' value='Revise search request'
 1450: onClick='this.form.submit();' />
 1451: $closebutton
 1452: <hr />
 1453: <h3>Helpful Message</h3>
 1454: <p>
 1455: $message
 1456: </p>
 1457: </body>
 1458: </html>
 1459: RESULTS
 1460: }
 1461: 
 1462: 1;
 1463: 
 1464: __END__

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