File:  [LON-CAPA] / loncom / interface / lonsearchcourse.pm
Revision 1.8.2.1: download - view: text, annotated - select for diffs
Thu Jul 4 17:30:17 2024 UTC (6 days, 9 hours ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_5
- For 2.11
  Include one change from 1.8 and backport 1.14

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>