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