Annotation of loncom/homework/daxeopen.pm, revision 1.4
1.1 damieng 1: # The LearningOnline Network
2: # Opening converted problems and directory listings for Daxe
3: #
1.4 ! damieng 4: # $Id: daxeopen.pm,v 1.3 2015/12/15 15:00:58 damieng Exp $
1.1 damieng 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);
1.4 ! damieng 52: } elsif ($uri =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) {
1.1 damieng 53: return convert_problem($uri, $request);
54: } else {
55: # Apache should send other files directly
1.2 damieng 56: $request->status(406);
57: return OK;
1.1 damieng 58: }
59: }
60:
61: sub convert_problem {
62: my ($uri, $request) = @_;
63:
64: my $file = &Apache::lonnet::filelocation('', $uri);
65: &Apache::lonnet::repcopy($file);
66: if (! -e $file) {
1.2 damieng 67: $request->status(404);
68: return OK;
1.1 damieng 69: }
70: try {
71: my $warnings = 0; # no warning printed
72: my $textref = &Apache::pre_xml::pre_xml($file, $warnings);
1.4 ! damieng 73: my $case_sensitive;
! 74: if ($uri =~ /\.(task)$/) {
! 75: $case_sensitive = 1;
! 76: } else {
! 77: $case_sensitive = 0;
! 78: }
! 79: $textref = &Apache::html_to_xml::html_to_xml($textref, $warnings, $case_sensitive);
1.3 damieng 80: my $text = &Apache::post_xml::post_xml($textref, $file, $perlvar{'lonDocRoot'}, $warnings);
1.1 damieng 81: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
82: $request->print($text);
83: return OK;
84: } catch {
1.2 damieng 85: $request->content_type('text/plain');
86: $request->print("convert failed for $file: $_");
87: $request->status(406);
88: return OK;
1.1 damieng 89: };
90: }
91:
92: sub directory_listing {
93: my ($uri, $request) = @_;
94: my $dirpath = &Apache::lonnet::filelocation('', $uri);
95: if (! -e $dirpath) {
1.2 damieng 96: $request->status(404);
97: return OK;
1.1 damieng 98: }
99: $dirpath =~ s/\/$//;
100: opendir my $dir, $dirpath or die "Cannot open directory: $dirpath";
101: my @files = readdir $dir;
102: closedir $dir;
103: my $res = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
104: my $dirname = $dirpath;
105: $dirname =~ s/^.*\/([^\/]*)$/$1/;
106: $res .= "<directory name=\"$dirname\">\n";
107: foreach my $name (@files) {
108: if ($name eq '.' || $name eq '..') {
109: next;
110: }
111: if ($name =~ /\.(bak|log|meta|save)$/) {
112: next;
113: }
114: $sb = stat($dirpath.'/'.$name);
115: my $mode = $sb->mode;
116: if (S_ISDIR($mode)) {
117: $res .= "<directory name=\"$name\"/>\n";
118: } else {
119: $res .= "<file name=\"$name\"";
120: my $size = $sb->size; # total size of file, in bytes
121: $res .= " size=\"$size\"";
122: my $mtime = $sb->mtime; # last modify time in seconds since the epoch
123: my $dt = DateTime->from_epoch(epoch => $mtime);
124: my $modified = $dt->iso8601().'Z';
125: $res .= " modified=\"$modified\"";
126: $res .= "/>\n";
127: }
128: }
129: $res .= "</directory>\n";
130: &Apache::loncommon::content_type($request, 'text/xml', 'utf-8');
131: $request->print($res);
132: return OK;
133: }
134:
135: 1;
136: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>