File:  [LON-CAPA] / loncom / publisher / loncoursepub.pm
Revision 1.2: download - view: text, annotated - select for diffs
Tue Apr 24 11:18:09 2018 UTC (6 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Course Editor "Standard Problem" item (Grading tab) for creation
  of a new problem in user's Authoring Space.

# The LearningOnline Network with CAPA
# Handler to store uploaded files etc. in Course's Authoring
# Space,  
#
# $Id: loncoursepub.pm,v 1.2 2018/04/24 11:18:09 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::loncoursepub;

use strict;
use Apache::Constants qw(:common :http :methods);
use Apache::lonnet;
use Apache::loncommon();
use Apache::lonpublisher;
use Apache::londocs;
use Apache::lonlocal;
use LONCAPA qw(:DEFAULT :match);

sub handler {
    my $r = shift;
    &Apache::loncommon::content_type($r,'text/html');
    $r->send_http_header;
    if ($env{'request.course.id'}) {
        my $newurl;
        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
        if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
            &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['LC_uploadcrsres']);
            my $suffix = $env{'form.LC_uploadcrsres'};
            my $containerfile = $env{'form.filename_'.$suffix};
            if ($containerfile =~ m{^(/priv/\Q$cdom/$cnum\E)}) {
                my $toppath = $1;
                if ($suffix) { 
                    my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
                    my $newfilename = $env{'form.uploadcrsres_'.$suffix.'.filename'};
                    $newfilename =~ s/(\s+$|^\s+)//g;
                    $newfilename =~ s/[\#\?&%\":]//g;
                    $newfilename =~ s/\.(\d+)(\.\w+)$/$2/;
                    my $relpath = $env{'form.crsauthorpath_'.$suffix};
                    my $newsubdir;
                    if ($env{'form.newsubdir_'.$suffix}) {
                        $newsubdir = $env{'form.newsubdirname_'.$suffix};
                    }
                    if ($newfilename ne '') {
                        my $path = $londocroot.$toppath;
                        my $redirect = &Apache::londocs::finishnewprob($toppath,$path,$relpath,$newsubdir,$newfilename,'upload');
                        if ($redirect) {
                            my $url = $redirect;
                            my $srcfile = $londocroot.$url;
                            $url =~ s{^/priv/}{/res/};
                            my $targetfile = $londocroot.$url;                            
                            if (open(my $fh,'>'.$srcfile)) {
                                print $fh $env{'form.uploadcrsres_'.$suffix};
                                close($fh);
                                $url =~ s{^/priv/}{/res/};
                                my $targetfile = $londocroot.$url;
                                my $nokeyref = &Apache::lonpublisher::getnokey($Apache::lonnet::perlvar{'lonIncludes'});
                                my $output = &Apache::lonpublisher::batchpublish($r,$srcfile,$targetfile,$nokeyref,1);
                                $newurl = $url;
                            }
                        }
                    }
                }
            }
            $r->print(<<"END");
<html>
<head>
<script type="text/javascript">
window.onload=top.uploadDone('$suffix','$newurl');
</script>
<body></body>
</html>
END
        }
    }
    return OK;
}

1;

__END__

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