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