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