File:  [LON-CAPA] / loncom / interface / lonindexcourse.pm
Revision 1.4: download - view: text, annotated - select for diffs
Mon Feb 12 04:04:06 2024 UTC (2 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- For unprivileged users exclude "unlisted" resources from course index
  when deep linking is used in course.

# The LearningOnline Network with CAPA
# Index Course
#
# $Id: lonindexcourse.pm,v 1.4 2024/02/12 04:04:06 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
###############################################################################
###############################################################################

package Apache::lonindexcourse;

use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
use GDBM_File;
use Apache::loncommon();
use Apache::lonmeta;
use Apache::lonhtmlcommon;
use Apache::lonlocal;
use LONCAPA::lonmetadata();
use HTML::Entities();
use Apache::lonnavmaps;
use Apache::lonnavdisplay();
use Apache::lonindexer();
use LONCAPA;

# Variables For course search
my %alreadyseen;
my %hash;
my %indexhash=();
my %indextitles=();

sub make_symb {
    my ($id)=@_;
    my ($mapid,$resid)=split(/\./,$id);
    my $map=$hash{'map_id_'.$mapid};
    my $res=$hash{'src_'.$id};
    my $symb=&Apache::lonnet::encode_symb($map,$resid,$res);
    return $symb;
}

sub course_index {
    my $r=shift;
    $r->rflush();
# ======================================================= Go through the course
    my $c=$r->connection;
    %indexhash=();
    %indextitles=();
    %alreadyseen=();
    if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.".db",
            &GDBM_READER(),0640)) {
        foreach my $key (sort(keys(%hash))) {
            last if ($c->aborted());
            if ($key =~ /^src\_(.+)$/) {
                my $rid = $1;
                unless ($env{'request.role.adv'}) {
                    next if ($hash{'randomout_'.$rid} || $hash{'deeplinkout_'.$rid});
                    if (!$env{'request.deeplink.login'} && $hash{'deeplinkonly_'.$rid}) {
                        my ($value) = map { &unescape($_); } split(/:/,$hash{'deeplinkonly_'.$rid});
                        my ($state,$others,$listed) = split(/,/,$value);
                        next if (($state eq 'only') &&
                                 (($listed eq 'absent') || ($listed eq 'grades')));
                    }
                }
                my $symb=&make_symb($rid);
                my %newwords=&checkonthis($r,$rid,$hash{$key},0,&Apache::lonnet::gettitle($symb),
                             $symb);
            }
        }
        untie(%hash);
    }
# Output
    $r->print(&Apache::loncommon::start_data_table());
    my $currentchar='';
    foreach my $lword (sort(keys(%indexhash))) {
       unless ($lword=~/\w/) { next; }
       if ($lword=~/^\d+$/) { next; }
       my $firstchar=substr($lword,0,1);
       if ($currentchar ne $firstchar) {
          $r->print(&Apache::loncommon::start_data_table_header_row().
                    '<th>'.$firstchar.'</th><th>&nbsp;</th>'.&Apache::loncommon::end_data_table_header_row());
          $currentchar=$firstchar;
       }
       $r->print("\n".&Apache::loncommon::start_data_table_row()."<td>$lword</td><td>");
       foreach my $href (split(/\,/,$indexhash{$lword})) {
           unless ($href) { next; }
           $r->print(' &nbsp; <a href="'.$href.'">'.$indextitles{$href}.'</a>');
       }
       $r->print("</td>".&Apache::loncommon::start_data_table_row());
    }
    $r->print(&Apache::loncommon::end_data_table());
}

# =============================== This pulls up a resource and its dependencies

sub checkonthis {
    my ($r,$id,$url,$level,$title,$symb)=@_;
    $alreadyseen{$id}=1;
    if (&Apache::loncommon::connection_aborted($r)) { return; }
    $r->rflush();

    my $result='';
    if ($env{'request.role.adv'} || !$hash{'encrypted_'.$id}) {
        $result=&Apache::lonnet::metadata($url,'subject').','.
                &Apache::lonnet::metadata($url,'keywords');
    }
    $result=~s/\s+/\,/gs;
    my $href=$url;
    if ($hash{'encrypted_'.$id} && !$env{'request.role.adv'}) {
        $href=&Apache::lonenc::encrypted($href)
               .'?symb='.&Apache::lonenc::encrypted($symb);
    } else {
        $href.='?symb='.&escape($symb);
    }
    foreach my $word (split(/\,/,$result)) {
        unless ($word) { next; }
        $indexhash{lc($word)}.=','.$href;
        $indextitles{$href}=($title?substr($title,0,10):'').' ...';
    }
    
    $r->rflush();
# Check also the dependencies of this one
    my $dependencies=
                &Apache::lonnet::metadata($url,'dependencies');
    foreach my $item (split(/\,/,$dependencies)) {
       if (($item =~ /^\/res\//) && (!$alreadyseen{$id})) {
          &checkonthis($r,$id,$item,$level+1,'');
       }
    }
}

sub untiehash {
    if (tied(%hash)) {
        untie(%hash);
    }
}

sub handler {
    my $r = shift;
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;
    if ($r->header_only) { return OK; }

    my $crstype = &Apache::loncommon::course_type();
    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['phase']);
    $r->print(&Apache::loncommon::start_page("$crstype Index"));
    &Apache::lonhtmlcommon::clear_breadcrumbs();
    &Apache::lonhtmlcommon::add_breadcrumb(
            {   href => '/adm/indexcourse',
                text => "$crstype Index"});
    $r->print(&Apache::lonhtmlcommon::breadcrumbs("$crstype Index"));
    &Apache::lonnavdisplay::startContentScreen($r,'courseindex');
    &course_index($r);
    &Apache::lonnavdisplay::endContentScreen($r);
    $r->print(&Apache::loncommon::end_page());
    return OK;
}

1;

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