Annotation of loncom/interface/lonsearchcourse.pm, revision 1.8
1.1 www 1: # The LearningOnline Network with CAPA
2: # Search Course
3: #
1.8 ! raeburn 4: # $Id: lonsearchcourse.pm,v 1.7 2021/11/30 15:55:37 raeburn Exp $
1.1 www 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.
14: #
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/
27: #
28: ###############################################################################
29: ###############################################################################
30:
31: package Apache::lonsearchcourse;
32:
33: use strict;
34: use Apache::Constants qw(:common :http);
35: use Apache::lonnet;
36: use GDBM_File;
37: use Apache::loncommon();
38: use Apache::lonmeta;
39: use Apache::lonhtmlcommon;
40: use Apache::lonlocal;
41: use LONCAPA::lonmetadata();
42: use HTML::Entities();
43: use Apache::lonnavmaps;
1.4 raeburn 44: use Apache::lonnavdisplay();
1.1 www 45: use Apache::lonindexer();
46: use LONCAPA;
47:
48: # Variables For course search
49: my %alreadyseen;
50: my %hash;
51: my $totalfound;
52:
53:
54: sub menu {
55: my $scrout='';
56: if ($env{'request.course.id'}) {
1.3 www 57: my %lt=&Apache::lonlocal::texthash(
58: 'srch' => 'Search',
59: 'note' => 'Search terms',
60: 'options' => 'Options',
1.1 www 61: 'use' => 'use related words',
62: 'full' =>'fulltext search (time consuming)',
63: 'disc' => 'search discussion postings (resources and discussion boards)',
64: );
65: $scrout.=(<<ENDCOURSESEARCH);
66: <form name="loncapa_search" method="post" action="/adm/searchcourse">
67: <input type="hidden" name="phase" value="results" />
68: ENDCOURSESEARCH
1.3 www 69: $scrout.=&Apache::lonhtmlcommon::start_pick_box().
70: &Apache::lonhtmlcommon::row_title($lt{'note'}).
71: &Apache::lonhtmlcommon::textbox('courseexp',
72: $env{'form.courseexp'},40).
73: &Apache::lonhtmlcommon::row_closure().
74: &Apache::lonhtmlcommon::row_title($lt{'options'}).
75: '<label>'.&Apache::lonhtmlcommon::checkbox('crsfulltext',$env{'form.crsfulltext'}).$lt{'full'}."</label><br />\n".
76: '<label>'.&Apache::lonhtmlcommon::checkbox('crsrelated',$env{'form.crsrelated'}).$lt{'use'}."</label><br />\n".
77: '<label>'.&Apache::lonhtmlcommon::checkbox('crsdiscuss',$env{'form.crsdiscuss'}).$lt{'disc'}."</label><br />\n".
78: &Apache::lonhtmlcommon::end_pick_box();
1.1 www 79: $scrout.=(<<ENDENDCOURSE);
80: <p>
81: <input type="submit" name="coursesubmit" value='$lt{'srch'}' />
82: </p>
83: </form>
84: ENDENDCOURSE
85: }
86: return $scrout;
87: }
88:
89: sub make_symb {
90: my ($id)=@_;
91: my ($mapid,$resid)=split(/\./,$id);
92: my $map=$hash{'map_id_'.$mapid};
93: my $res=$hash{'src_'.$id};
94: my $symb=&Apache::lonnet::encode_symb($map,$resid,$res);
95: return $symb;
96: }
97:
98: sub related_version {
99: my ($word) = @_;
100: return (undef) if (lc($word) =~ /\b(or|and|not)\b/);
101: my @Words = &Apache::loncommon::get_related_words($word);
102: # Only use 4 related words
103: @Words = ($#Words>4? @Words[0..4] : @Words);
104: my $result = join " OR ", ($word,@Words);
105: return $result,sort(@Words);
106: }
107:
108: sub course_search {
109: my $r=shift;
110: my $pretty_search_string = '<b>'.$env{'form.courseexp'}.'</b>';
111: my $search_string = $env{'form.courseexp'};
112: my @New_Words;
113: undef(%alreadyseen);
114: if ($env{'form.crsrelated'}) {
115: ($search_string,@New_Words) = &related_version($env{'form.courseexp'});
116: if (@New_Words) {
117: $pretty_search_string .= ' '.&mt("with related words").": <b>@New_Words</b>.";
118: } else {
119: $pretty_search_string .= ' '.&mt('with no related words').".";
120: }
121: }
122: my $fulltext=$env{'form.crsfulltext'};
123: my $discuss=$env{'form.crsdiscuss'};
124: my @allwords=($search_string,@New_Words);
125: $totalfound=0;
126:
127: $r->print(
128: '<hr /><center><font size="+2" face="arial">'.
129: $pretty_search_string.'</font></center>'.
130: '<hr /><b>'.&mt('Course content').':</b><br />');
131: $r->rflush();
132: # ======================================================= Go through the course
133: my $c=$r->connection;
134: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.".db",
135: &GDBM_READER(),0640)) {
136: foreach (sort(keys(%hash))) {
137: if ($c->aborted()) { last; }
138: if (($_=~/^src\_(.+)$/)) {
139: if ($hash{'randomout_'.$1} & !$env{'request.role.adv'}) {
140: next;
141: }
142: my $symb=&make_symb($1);
143: &checkonthis($r,$1,$hash{$_},0,&Apache::lonnet::gettitle($symb),
144: $fulltext,$symb,@allwords);
145: }
146: }
147: untie(%hash);
148: }
149: unless ($totalfound) {
150: $r->print('<p class="LC_info">'.&mt('No matches found in resources.').'</p>');
151: }
152:
153: # Check discussions if requested
154: if ($discuss) {
155: my $totaldiscussions = 0;
156: $r->print('<br /><br /><b>'.&mt('Discussion postings').':</b><br />');
157: my $navmap = Apache::lonnavmaps::navmap->new();
158: if (defined($navmap)) {
159: my @allres=$navmap->retrieveResources();
160: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
161: $env{'course.'.$env{'request.course.id'}.'.domain'},
162: $env{'course.'.$env{'request.course.id'}.'.num'});
163: foreach my $resource (@allres) {
164: my $result = '';
165: my $applies = 0;
166: my $symb = $resource->symb();
167: my $ressymb = $symb;
168: if ($symb =~ m#(___adm/$LONCAPA::domain_re/$LONCAPA::username_re)/(\d+)/bulletinboard$#) {
169: $ressymb = 'bulletin___'.$2.$1.'/'.$2.'/bulletinboard';
170: unless ($ressymb =~ m#bulletin___\d+___adm/wrapper#) {
171: $ressymb=~s#(bulletin___\d+___)#$1adm/wrapper/#;
172: }
173: }
174: if (defined($discussiontime{$ressymb})) {
175: my %contrib = &Apache::lonnet::restore($ressymb,$env{'request.course.id'},
176: $env{'course.'.$env{'request.course.id'}.'.domain'},
177: $env{'course.'.$env{'request.course.id'}.'.num'});
178: if ($contrib{'version'}) {
179: for (my $id=1;$id<=$contrib{'version'};$id++) {
180: unless (($contrib{'hidden'}=~/\.$id\./) || ($contrib{'deleted'}=~/\.$id\./)) {
181: if ($contrib{$id.':subject'}) {
182: $result .= $contrib{$id.':subject'};
183: }
184: if ($contrib{$id.':message'}) {
185: $result .= $contrib{$id.':message'};
186: }
187: if ($contrib{$id,':attachmenturl'}) {
188: if ($contrib{$id,':attachmenturl'} =~ m-/([^/]+)$-) {
189: $result .= $1;
190: }
191: }
192: $applies = &checkwords($result,$applies,@allwords);
193: }
194: }
195: }
196: }
197: # Does this discussion apply?
198: if ($applies) {
199: my ($map,$ind,$url)=&Apache::lonnet::decode_symb($ressymb);
200: my $disctype = &mt('resource');
201: if ($url =~ m#/bulletinboard$#) {
202: if ($url =~m#^adm/wrapper/adm/.*/bulletinboard$#) {
203: $url =~s#^adm/wrapper##;
204: }
205: $disctype = &mt('discussion board');
206: } else {
207: $url = '/res/'.$url;
208: }
209: if ($url =~ /\?/) {
210: $url .= '&symb=';
211: } else {
212: $url .= '?symb=';
213: }
214: $url .= &escape($resource->symb());
215: my $title = $resource->compTitle();
216: $r->print('<br /><a href="'.$url.'" target="cat">'.
217: ($title?$title:$url).'</a> - '.
218: $disctype.'<br />');
219: $totaldiscussions++;
220: } else {
221: $r->print(' .');
222: }
223: }
224: unless ($totaldiscussions) {
225: $r->print('<p class="LC_info">'.&mt('No matches found in postings.').'</p>');
226: }
227: } else {
228: $r->print('<div class="LC_error">'.&mt('An error occurred retrieving information about resources in the course.').'<br />'.&mt('It is recommended that you [_1]re-initialize the course[_2] and then try your search again.','<a href="/adm/roles">','</a>').'</div>');
229: }
230: }
231: }
232:
233: # =============================== This pulls up a resource and its dependencies
234:
235: sub checkonthis {
236: my ($r,$id,$url,$level,$title,$fulltext,$symb,@allwords)=@_;
237: $alreadyseen{$id}=1;
238: if (&Apache::loncommon::connection_aborted($r)) { return; }
239: $r->rflush();
240:
241: my $result=$title.' ';
242: if ($env{'request.role.adv'} || !$hash{'encrypted_'.$id}) {
243: $result.=&Apache::lonnet::metadata($url,'title').' '.
244: &Apache::lonnet::metadata($url,'subject').' '.
245: &Apache::lonnet::metadata($url,'abstract').' '.
246: &Apache::lonnet::metadata($url,'keywords');
247: }
248: my ($extension)=($url=~/\.(\w+)$/);
249: if (&Apache::loncommon::fileembstyle($extension) eq 'ssi' &&
250: ($url) && ($fulltext)) {
251: $result.=&Apache::lonnet::ssi_body($url.'?symb='.&escape($symb));
252: }
253: $result=~s/\s+/ /gs;
254: my $applies = 0;
255: $applies = &checkwords($result,$applies,@allwords);
256: # Does this resource apply?
257: if ($applies) {
258: $r->print('<br />');
259: for (my $i=0;$i<=$level*5;$i++) {
260: $r->print(' ');
261: }
262: my $href=$url;
263: if ($hash{'encrypted_'.$id} && !$env{'request.role.adv'}) {
1.8 ! raeburn 264: $href=&Apache::lonenc::encrypted($href);
! 265: if ($href =~ /\.sequence$/) {
! 266: $href .= '?navmap=1';
! 267: } else {
! 268: $href .= '?symb='.&Apache::lonenc::encrypted($symb);
! 269: }
1.1 www 270: } else {
1.8 ! raeburn 271: if ($href =~ /\.sequence$/) {
! 272: $href .= '?navmap=1';
! 273: } else {
! 274: $href .= '?symb='.&escape($symb);
! 275: }
1.1 www 276: }
277: $r->print('<a href="'.$href.'" target="cat">'.($title?$title:$url).
278: '</a><br />');
279: $totalfound++;
280: } elsif ($fulltext) {
281: $r->print(' .');
282: }
283: $r->rflush();
284: # Check also the dependencies of this one
285: my $dependencies=
286: &Apache::lonnet::metadata($url,'dependencies');
287: foreach (split(/\,/,$dependencies)) {
288: if (($_=~/^\/res\//) && (!$alreadyseen{$id})) {
289: &checkonthis($r,$id,$_,$level+1,'',$fulltext,undef,@allwords);
290: }
291: }
292: }
293:
294: sub checkwords {
295: my ($result,$applies,@allwords) = @_;
296: foreach (@allwords) {
297: if ($_=~/\w/) {
298: if ($result=~/$_/si) {
299: $applies++;
300: }
301: }
302: }
303: return $applies;
304: }
305:
306: sub untiehash {
307: if (tied(%hash)) {
308: untie(%hash);
309: }
310: }
311:
312: sub handler {
313: my $r = shift;
314: &Apache::loncommon::content_type($r,'text/html');
315: $r->send_http_header;
316: if ($r->header_only) { return OK; }
317:
318: my $crstype = &Apache::loncommon::course_type();
319: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['phase']);
1.2 www 320: $r->print(&Apache::loncommon::start_page("$crstype Search"));
1.1 www 321: &Apache::lonhtmlcommon::clear_breadcrumbs();
1.5 raeburn 322: if ($env{'request.course.id'} eq '') {
323: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
324: $r->print(&Apache::loncommon::end_page());
325: my $requrl = $r->uri;
326: $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
327: $env{'user.reinit'} = 1;
328: return HTTP_NOT_ACCEPTABLE;
329: }
1.2 www 330: &Apache::lonhtmlcommon::add_breadcrumb(
331: { href => '/adm/searchcourse',
332: text => "$crstype Search"});
333: if ($env{'form.phase'} eq 'results') {
334: &Apache::lonhtmlcommon::add_breadcrumb(
335: { href => '/adm/searchcourse?phase=results',
336: text => 'Search Results'});
337: }
1.1 www 338: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
1.4 raeburn 339: &Apache::lonnavdisplay::startContentScreen($r,'coursesearch');
1.6 raeburn 340: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
341: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.7 raeburn 342: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.6 raeburn 343: my ($blocked,$blocktext) =
1.7 raeburn 344: &Apache::loncommon::blocking_status('search',$clientip,$cnum,$cdom);
1.6 raeburn 345: if ($blocked) {
346: my $checkrole = "cm./$cdom/$cnum";
347: if ($env{'request.course.sec'} ne '') {
348: $checkrole .= "/$env{'request.course.sec'}";
349: }
350: if ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
351: ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
352: undef($blocked);
353: }
354: }
355: if ($blocked) {
356: $r->print($blocktext);
357: } elsif ($env{'form.phase'} eq 'results') {
358: &course_search($r);
1.1 www 359: } else {
1.6 raeburn 360: $r->print(&menu());
1.1 www 361: }
1.4 raeburn 362: &Apache::lonnavdisplay::endContentScreen($r);
1.1 www 363: $r->print(&Apache::loncommon::end_page());
364: return OK;
365: }
366:
367: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>