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