File:  [LON-CAPA] / loncom / homework / daxeopen.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Dec 3 20:40:27 2015 UTC (8 years, 4 months ago) by damieng
Branches: MAIN
CVS tags: HEAD
integrated Daxe, opening in a separate window for now

# The LearningOnline Network
# Opening converted problems and directory listings for Daxe
#
# $Id: daxeopen.pm,v 1.1 2015/12/03 20:40:27 damieng 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::daxeopen;

use Apache::Constants;
use DateTime;
use Try::Tiny;
use File::stat;
use Fcntl ':mode';

use Apache::loncommon;
use Apache::lonnet;
use Apache::pre_xml;
use Apache::html_to_xml;
use Apache::post_xml;


sub handler {
    my $request = shift;
    my $uri = $request->uri;
    $uri =~ s/^\/daxeopen//;
    &Apache::loncommon::no_cache($request);
    if ($uri =~ /\/$/) {
        return directory_listing($uri, $request);
    } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) {
        return convert_problem($uri, $request);
    } else {
        # Apache should send other files directly
        return HTTP_NOT_ACCEPTABLE;
    }
}

sub convert_problem {
    my ($uri, $request) = @_;
    
    my $file = &Apache::lonnet::filelocation('', $uri);
    &Apache::lonnet::repcopy($file);
    if (! -e $file) {
        return HTTP_NOT_FOUND;
    }
    try {
        my $warnings = 0; # no warning printed
        my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
        $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings);
        my $text = &Apache::post_xml::post_xml($textref, $file, $warnings);
        &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
        $request->print($text);
        return OK;
    } catch {
        die "convert failed for $file: $_";
        #$request->print('<?xml version="1.0" encoding="UTF-8"?>'."\n");
        #$request->print("<problem>\n");
        #$request->print("convert failed for $file: $_");
        #$request->print("</problem>\n");
        #return OK;
    };
}

sub directory_listing {
    my ($uri, $request) = @_;
    my $dirpath = &Apache::lonnet::filelocation('', $uri);
    if (! -e $dirpath) {
        return HTTP_NOT_FOUND;
    }
    $dirpath =~ s/\/$//;
    opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
    my @files = readdir $dir;
    closedir $dir;
    my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    my $dirname = $dirpath;
    $dirname =~ s/^.*\/([^\/]*)$/$1/;
    $res .= "<directory name=\"$dirname\">\n";
    foreach my $name (@files) {
        if ($name eq '.' || $name eq '..') {
            next;
        }
        if ($name =~ /\.(bak|log|meta|save)$/) {
            next;
        }
        $sb = stat($dirpath.'/'.$name);
        my $mode = $sb->mode;
        if (S_ISDIR($mode)) {
            $res .= "<directory name=\"$name\"/>\n";
        } else {
            $res .= "<file name=\"$name\"";
            my $size = $sb->size; # total size of file, in bytes
            $res .= " size=\"$size\"";
            my $mtime = $sb->mtime; # last modify time in seconds since the epoch
            my $dt = DateTime->from_epoch(epoch => $mtime);
            my $modified = $dt->iso8601().'Z';
            $res .= " modified=\"$modified\"";
            $res .= "/>\n";
        }
    }
    $res .= "</directory>\n";
    &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
    $request->print($res);
    return OK;
}

# NOTE: binaries should be sent directly be Apache
# sub send_binary {
#     my ($request, $filepath) = @_;
# 
#     $buffer = '';
#     if (!open(FILE, "<", $filepath)) {
#         return HTTP_NOT_FOUND;
#     }
#     binmode(FILE);
# 
#     # Read file in 32K blocks
#     while ((read(FILE, $buffer, 32768)) != 0) {
#         $request->print($buffer);
#     } 
# 
#     if (!close(FILE)) {
#         &Apache::lonnet::logthis("Error closing the file $filepath");
#     }
#     return OK;
# }

1;
__END__

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