Annotation of loncom/interface/lonsearchcourse.pm, revision 1.6
1.1 www 1: # The LearningOnline Network with CAPA
2: # Search Course
3: #
1.6 ! raeburn 4: # $Id: lonsearchcourse.pm,v 1.5 2020/09/22 11:23:08 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'}) {
264: $href=&Apache::lonenc::encrypted($href)
265: .'?symb='.&Apache::lonenc::encrypted($symb);
266: } else {
267: $href.='?symb='.&escape($symb);
268: }
269: $r->print('<a href="'.$href.'" target="cat">'.($title?$title:$url).
270: '</a><br />');
271: $totalfound++;
272: } elsif ($fulltext) {
273: $r->print(' .');
274: }
275: $r->rflush();
276: # Check also the dependencies of this one
277: my $dependencies=
278: &Apache::lonnet::metadata($url,'dependencies');
279: foreach (split(/\,/,$dependencies)) {
280: if (($_=~/^\/res\//) && (!$alreadyseen{$id})) {
281: &checkonthis($r,$id,$_,$level+1,'',$fulltext,undef,@allwords);
282: }
283: }
284: }
285:
286: sub checkwords {
287: my ($result,$applies,@allwords) = @_;
288: foreach (@allwords) {
289: if ($_=~/\w/) {
290: if ($result=~/$_/si) {
291: $applies++;
292: }
293: }
294: }
295: return $applies;
296: }
297:
298: sub untiehash {
299: if (tied(%hash)) {
300: untie(%hash);
301: }
302: }
303:
304: sub handler {
305: my $r = shift;
306: &Apache::loncommon::content_type($r,'text/html');
307: $r->send_http_header;
308: if ($r->header_only) { return OK; }
309:
310: my $crstype = &Apache::loncommon::course_type();
311: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['phase']);
1.2 www 312: $r->print(&Apache::loncommon::start_page("$crstype Search"));
1.1 www 313: &Apache::lonhtmlcommon::clear_breadcrumbs();
1.5 raeburn 314: if ($env{'request.course.id'} eq '') {
315: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
316: $r->print(&Apache::loncommon::end_page());
317: my $requrl = $r->uri;
318: $env{'user.error.msg'} = "$requrl:bre:0:0:Course not initialized";
319: $env{'user.reinit'} = 1;
320: return HTTP_NOT_ACCEPTABLE;
321: }
1.2 www 322: &Apache::lonhtmlcommon::add_breadcrumb(
323: { href => '/adm/searchcourse',
324: text => "$crstype Search"});
325: if ($env{'form.phase'} eq 'results') {
326: &Apache::lonhtmlcommon::add_breadcrumb(
327: { href => '/adm/searchcourse?phase=results',
328: text => 'Search Results'});
329: }
1.1 www 330: $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Search"));
1.4 raeburn 331: &Apache::lonnavdisplay::startContentScreen($r,'coursesearch');
1.6 ! raeburn 332: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
! 333: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
! 334: my ($blocked,$blocktext) =
! 335: &Apache::loncommon::blocking_status('search',$cnum,$cdom);
! 336: if ($blocked) {
! 337: my $checkrole = "cm./$cdom/$cnum";
! 338: if ($env{'request.course.sec'} ne '') {
! 339: $checkrole .= "/$env{'request.course.sec'}";
! 340: }
! 341: if ((&Apache::lonnet::allowed('evb',undef,undef,$checkrole)) &&
! 342: ($env{'request.role'} !~ m{^st\./$cdom/$cnum})) {
! 343: undef($blocked);
! 344: }
! 345: }
! 346: if ($blocked) {
! 347: $r->print($blocktext);
! 348: } elsif ($env{'form.phase'} eq 'results') {
! 349: &course_search($r);
1.1 www 350: } else {
1.6 ! raeburn 351: $r->print(&menu());
1.1 www 352: }
1.4 raeburn 353: &Apache::lonnavdisplay::endContentScreen($r);
1.1 www 354: $r->print(&Apache::loncommon::end_page());
355: return OK;
356: }
357:
358: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>