Annotation of loncom/interface/lonsearchcourse.pm, revision 1.9
1.1 www 1: # The LearningOnline Network with CAPA
2: # Search Course
3: #
1.9 ! raeburn 4: # $Id: lonsearchcourse.pm,v 1.8 2024/02/11 20:32:42 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\_(.+)$/)) {
1.9 ! raeburn 139: my $rid = $1;
! 140: unless ($env{'request.role.adv'}) {
! 141: next if ($hash{'randomout_'.$rid} || $hash{'deeplinkout_'.$rid});
! 142: if (!$env{'request.deeplink.login'} && $hash{'deeplinkonly_'.$rid}) {
! 143: my ($value) = map { &unescape($_); } split(/:/,$hash{'deeplinkonly_'.$rid});
! 144: my ($state,$others,$listed) = split(/,/,$value);
! 145: next if (($state eq 'only') &&
! 146: (($listed eq 'absent') || ($listed eq 'grades')));
! 147: }
1.1 www 148: }
149: my $symb=&make_symb($1);
150: &checkonthis($r,$1,$hash{$_},0,&Apache::lonnet::gettitle($symb),
151: $fulltext,$symb,@allwords);
152: }
153: }
154: untie(%hash);
155: }
156: unless ($totalfound) {
157: $r->print('<p class="LC_info">'.&mt('No matches found in resources.').'</p>');
158: }
159:
160: # Check discussions if requested
161: if ($discuss) {
162: my $totaldiscussions = 0;
163: $r->print('<br /><br /><b>'.&mt('Discussion postings').':</b><br />');
164: my $navmap = Apache::lonnavmaps::navmap->new();
165: if (defined($navmap)) {
166: my @allres=$navmap->retrieveResources();
167: my %discussiontime = &Apache::lonnet::dump('discussiontimes',
168: $env{'course.'.$env{'request.course.id'}.'.domain'},
169: $env{'course.'.$env{'request.course.id'}.'.num'});
170: foreach my $resource (@allres) {
171: my $result = '';
172: my $applies = 0;
173: my $symb = $resource->symb();
174: my $ressymb = $symb;
175: if ($symb =~ m#(___adm/$LONCAPA::domain_re/$LONCAPA::username_re)/(\d+)/bulletinboard$#) {
176: $ressymb = 'bulletin___'.$2.$1.'/'.$2.'/bulletinboard';
177: unless ($ressymb =~ m#bulletin___\d+___adm/wrapper#) {
178: $ressymb=~s#(bulletin___\d+___)#$1adm/wrapper/#;
179: }
180: }
181: if (defined($discussiontime{$ressymb})) {
182: my %contrib = &Apache::lonnet::restore($ressymb,$env{'request.course.id'},
183: $env{'course.'.$env{'request.course.id'}.'.domain'},
184: $env{'course.'.$env{'request.course.id'}.'.num'});
185: if ($contrib{'version'}) {
186: for (my $id=1;$id<=$contrib{'version'};$id++) {
187: unless (($contrib{'hidden'}=~/\.$id\./) || ($contrib{'deleted'}=~/\.$id\./)) {
188: if ($contrib{$id.':subject'}) {
189: $result .= $contrib{$id.':subject'};
190: }
191: if ($contrib{$id.':message'}) {
192: $result .= $contrib{$id.':message'};
193: }
194: if ($contrib{$id,':attachmenturl'}) {
195: if ($contrib{$id,':attachmenturl'} =~ m-/([^/]+)$-) {
196: $result .= $1;
197: }
198: }
199: $applies = &checkwords($result,$applies,@allwords);
200: }
201: }
202: }
203: }
204: # Does this discussion apply?
205: if ($applies) {
206: my ($map,$ind,$url)=&Apache::lonnet::decode_symb($ressymb);
207: my $disctype = &mt('resource');
208: if ($url =~ m#/bulletinboard$#) {
209: if ($url =~m#^adm/wrapper/adm/.*/bulletinboard$#) {
210: $url =~s#^adm/wrapper##;
211: }
212: $disctype = &mt('discussion board');
213: } else {
214: $url = '/res/'.$url;
215: }
216: if ($url =~ /\?/) {
217: $url .= '&symb=';
218: } else {
219: $url .= '?symb=';
220: }
221: $url .= &escape($resource->symb());
222: my $title = $resource->compTitle();
223: $r->print('<br /><a href="'.$url.'" target="cat">'.
224: ($title?$title:$url).'</a> - '.
225: $disctype.'<br />');
226: $totaldiscussions++;
227: } else {
228: $r->print(' .');
229: }
230: }
231: unless ($totaldiscussions) {
232: $r->print('<p class="LC_info">'.&mt('No matches found in postings.').'</p>');
233: }
234: } else {
235: $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>');
236: }
237: }
238: }
239:
240: # =============================== This pulls up a resource and its dependencies
241:
242: sub checkonthis {
243: my ($r,$id,$url,$level,$title,$fulltext,$symb,@allwords)=@_;
244: $alreadyseen{$id}=1;
245: if (&Apache::loncommon::connection_aborted($r)) { return; }
246: $r->rflush();
247:
248: my $result=$title.' ';
249: if ($env{'request.role.adv'} || !$hash{'encrypted_'.$id}) {
250: $result.=&Apache::lonnet::metadata($url,'title').' '.
251: &Apache::lonnet::metadata($url,'subject').' '.
252: &Apache::lonnet::metadata($url,'abstract').' '.
253: &Apache::lonnet::metadata($url,'keywords');
254: }
255: my ($extension)=($url=~/\.(\w+)$/);
256: if (&Apache::loncommon::fileembstyle($extension) eq 'ssi' &&
257: ($url) && ($fulltext)) {
258: $result.=&Apache::lonnet::ssi_body($url.'?symb='.&escape($symb));
259: }
260: $result=~s/\s+/ /gs;
261: my $applies = 0;
262: $applies = &checkwords($result,$applies,@allwords);
263: # Does this resource apply?
264: if ($applies) {
265: $r->print('<br />');
266: for (my $i=0;$i<=$level*5;$i++) {
267: $r->print(' ');
268: }
269: my $href=$url;
270: if ($hash{'encrypted_'.$id} && !$env{'request.role.adv'}) {
1.8 raeburn 271: $href=&Apache::lonenc::encrypted($href);
272: if ($href =~ /\.sequence$/) {
273: $href .= '?navmap=1';
274: } else {
275: $href .= '?symb='.&Apache::lonenc::encrypted($symb);
276: }
1.1 www 277: } else {
1.8 raeburn 278: if ($href =~ /\.sequence$/) {
279: $href .= '?navmap=1';
280: } else {
281: $href .= '?symb='.&escape($symb);
282: }
1.1 www 283: }
284: $r->print('<a href="'.$href.'" target="cat">'.($title?$title:$url).
285: '</a><br />');
286: $totalfound++;
287: } elsif ($fulltext) {
288: $r->print(' .');
289: }
290: $r->rflush();
291: # Check also the dependencies of this one
292: my $dependencies=
293: &Apache::lonnet::metadata($url,'dependencies');
294: foreach (split(/\,/,$dependencies)) {
295: if (($_=~/^\/res\//) && (!$alreadyseen{$id})) {
296: &checkonthis($r,$id,$_,$level+1,'',$fulltext,undef,@allwords);
297: }
298: }
299: }
300:
301: sub checkwords {
302: my ($result,$applies,@allwords) = @_;
303: foreach (@allwords) {
304: if ($_=~/\w/) {
305: if ($result=~/$_/si) {
306: $applies++;
307: }
308: }
309: }
310: return $applies;
311: }
312:
313: sub untiehash {
314: if (tied(%hash)) {
315: untie(%hash);
316: }
317: }
318:
319: sub handler {
320: my $r = shift;
321: &Apache::loncommon::content_type($r,'text/html');
322: $r->send_http_header;
323: if ($r->header_only) { return OK; }
324:
325: my $crstype = &Apache::loncommon::course_type();
326: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['phase']);
1.2 www 327: $r->print(&Apache::loncommon::start_page("$crstype Search"));
1.1 www 328: &Apache::lonhtmlcommon::clear_breadcrumbs();
1.5 raeburn 329: if ($env{'request.course.id'} eq '') {
330: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
331: $r->print(&Apache::loncommon::end_page());
332: my $requrl = $r->uri;
333: $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
334: $env{'user.reinit'} = 1;
335: return HTTP_NOT_ACCEPTABLE;
336: }
1.2 www 337: &Apache::lonhtmlcommon::add_breadcrumb(
338: { href => '/adm/searchcourse',
339: text => "$crstype Search"});
340: if ($env{'form.phase'} eq 'results') {
341: &Apache::lonhtmlcommon::add_breadcrumb(
342: { href => '/adm/searchcourse?phase=results',
343: text => 'Search Results'});
344: }
1.1 www 345: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
1.4 raeburn 346: &Apache::lonnavdisplay::startContentScreen($r,'coursesearch');
1.6 raeburn 347: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
348: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.7 raeburn 349: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.6 raeburn 350: my ($blocked,$blocktext) =
1.7 raeburn 351: &Apache::loncommon::blocking_status('search',$clientip,$cnum,$cdom);
1.6 raeburn 352: if ($blocked) {
353: my $checkrole = "cm./$cdom/$cnum";
354: if ($env{'request.course.sec'} ne '') {
355: $checkrole .= "/$env{'request.course.sec'}";
356: }
357: if ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
358: ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
359: undef($blocked);
360: }
361: }
362: if ($blocked) {
363: $r->print($blocktext);
364: } elsif ($env{'form.phase'} eq 'results') {
365: &course_search($r);
1.1 www 366: } else {
1.6 raeburn 367: $r->print(&menu());
1.1 www 368: }
1.4 raeburn 369: &Apache::lonnavdisplay::endContentScreen($r);
1.1 www 370: $r->print(&Apache::loncommon::end_page());
371: return OK;
372: }
373:
374: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>