Annotation of loncom/homework/daxeopen.pm, revision 1.1

1.1     ! damieng     1: # The LearningOnline Network
        !             2: # Opening converted problems and directory listings for Daxe
        !             3: #
        !             4: # $Id$
        !             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: package Apache::daxeopen;
        !            31: 
        !            32: use Apache::Constants;
        !            33: use DateTime;
        !            34: use Try::Tiny;
        !            35: use File::stat;
        !            36: use Fcntl ':mode';
        !            37: 
        !            38: use Apache::loncommon;
        !            39: use Apache::lonnet;
        !            40: use Apache::pre_xml;
        !            41: use Apache::html_to_xml;
        !            42: use Apache::post_xml;
        !            43: 
        !            44: 
        !            45: sub handler {
        !            46:     my $request = shift;
        !            47:     my $uri = $request->uri;
        !            48:     $uri =~ s/^\/daxeopen//;
        !            49:     &Apache::loncommon::no_cache($request);
        !            50:     if ($uri =~ /\/$/) {
        !            51:         return directory_listing($uri, $request);
        !            52:     } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library)$/) {
        !            53:         return convert_problem($uri, $request);
        !            54:     } else {
        !            55:         # Apache should send other files directly
        !            56:         return HTTP_NOT_ACCEPTABLE;
        !            57:     }
        !            58: }
        !            59: 
        !            60: sub convert_problem {
        !            61:     my ($uri, $request) = @_;
        !            62:     
        !            63:     my $file = &Apache::lonnet::filelocation('', $uri);
        !            64:     &Apache::lonnet::repcopy($file);
        !            65:     if (! -e $file) {
        !            66:         return HTTP_NOT_FOUND;
        !            67:     }
        !            68:     try {
        !            69:         my $warnings = 0; # no warning printed
        !            70:         my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
        !            71:         $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings);
        !            72:         my $text = &Apache::post_xml::post_xml($textref, $file, $warnings);
        !            73:         &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
        !            74:         $request->print($text);
        !            75:         return OK;
        !            76:     } catch {
        !            77:         die "convert failed for $file: $_";
        !            78:         #$request->print('<?xml version="1.0" encoding="UTF-8"?>'."\n");
        !            79:         #$request->print("<problem>\n");
        !            80:         #$request->print("convert failed for $file: $_");
        !            81:         #$request->print("</problem>\n");
        !            82:         #return OK;
        !            83:     };
        !            84: }
        !            85: 
        !            86: sub directory_listing {
        !            87:     my ($uri, $request) = @_;
        !            88:     my $dirpath = &Apache::lonnet::filelocation('', $uri);
        !            89:     if (! -e $dirpath) {
        !            90:         return HTTP_NOT_FOUND;
        !            91:     }
        !            92:     $dirpath =~ s/\/$//;
        !            93:     opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
        !            94:     my @files = readdir $dir;
        !            95:     closedir $dir;
        !            96:     my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
        !            97:     my $dirname = $dirpath;
        !            98:     $dirname =~ s/^.*\/([^\/]*)$/$1/;
        !            99:     $res .= "<directory name=\"$dirname\">\n";
        !           100:     foreach my $name (@files) {
        !           101:         if ($name eq '.' || $name eq '..') {
        !           102:             next;
        !           103:         }
        !           104:         if ($name =~ /\.(bak|log|meta|save)$/) {
        !           105:             next;
        !           106:         }
        !           107:         $sb = stat($dirpath.'/'.$name);
        !           108:         my $mode = $sb->mode;
        !           109:         if (S_ISDIR($mode)) {
        !           110:             $res .= "<directory name=\"$name\"/>\n";
        !           111:         } else {
        !           112:             $res .= "<file name=\"$name\"";
        !           113:             my $size = $sb->size; # total size of file, in bytes
        !           114:             $res .= " size=\"$size\"";
        !           115:             my $mtime = $sb->mtime; # last modify time in seconds since the epoch
        !           116:             my $dt = DateTime->from_epoch(epoch => $mtime);
        !           117:             my $modified = $dt->iso8601().'Z';
        !           118:             $res .= " modified=\"$modified\"";
        !           119:             $res .= "/>\n";
        !           120:         }
        !           121:     }
        !           122:     $res .= "</directory>\n";
        !           123:     &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
        !           124:     $request->print($res);
        !           125:     return OK;
        !           126: }
        !           127: 
        !           128: # NOTE: binaries should be sent directly be Apache
        !           129: # sub send_binary {
        !           130: #     my ($request, $filepath) = @_;
        !           131: # 
        !           132: #     $buffer = '';
        !           133: #     if (!open(FILE, "<", $filepath)) {
        !           134: #         return HTTP_NOT_FOUND;
        !           135: #     }
        !           136: #     binmode(FILE);
        !           137: # 
        !           138: #     # Read file in 32K blocks
        !           139: #     while ((read(FILE, $buffer, 32768)) != 0) {
        !           140: #         $request->print($buffer);
        !           141: #     } 
        !           142: # 
        !           143: #     if (!close(FILE)) {
        !           144: #         &Apache::lonnet::logthis("Error closing the file $filepath");
        !           145: #     }
        !           146: #     return OK;
        !           147: # }
        !           148: 
        !           149: 1;
        !           150: __END__

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