File:  [LON-CAPA] / loncom / interface / londependencies.pm
Revision 1.1: download - view: text, annotated - select for diffs
Mon Apr 16 19:59:12 2012 UTC (12 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6448. Manage dependencies for HTML files uploaded to course.
Interface for uploading, replacing and deleting files which are currently
(or used to be) dependencies for an HTML page uploaded directly to a course.

    1: # The LearningOnline Network with CAPA
    2: # Handler to manage dependencies for HTML files uploaded directly
    3: # to a course. 
    4: #
    5: # $Id: londependencies.pm,v 1.1 2012/04/16 19:59:12 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ##############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: londependencies- Handler to manage dependencies for HTML files
   37: uploaded directly to a course. 
   38: 
   39: =head1 SYNOPSIS
   40: 
   41: londependencies provides an interface for uploading, replacing 
   42: and deleting files which are dependencies for an HTML page
   43: uploaded directly to a course.
   44: 
   45: =head1 DESCRIPTION
   46: 
   47: This module is used when editing a web page uploaded to a course,
   48: in course context.
   49: 
   50: =head1 INTERNAL SUBROUTINES
   51: 
   52: =over
   53: 
   54: =item process_changes()
   55: 
   56: =item display_dependencies()
   57: 
   58: =back
   59: 
   60: =cut
   61: 
   62: package Apache::londependencies;
   63: 
   64: use strict;
   65: use Apache::Constants qw(:common :http);
   66: use Apache::lonnet;
   67: use Apache::loncommon();
   68: use Apache::lonhtmlcommon();
   69: use Apache::lonlocal;
   70: use LONCAPA qw(:DEFAULT :match);
   71: use File::MMagic;
   72: 
   73: sub handler {
   74:     my $r=shift;
   75:     if ($r->header_only) {
   76:         &Apache::loncommon::content_type($r,'text/html');
   77:         $r->send_http_header;
   78:         return OK;
   79:     }
   80:     my ($allowed,$cnum,$cdom,$chome);
   81:     if ($env{'request.course.id'}) {
   82:         $allowed = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
   83:     }
   84:     if (!$env{'request.course.fn'} || !$allowed) {
   85:         # Not in a course, or no mdc priv in course
   86:         $env{'user.error.msg'}="/adm/dependencies:mdc:0:0:Cannot display dependencies for page uploaded to course";
   87:         return HTTP_NOT_ACCEPTABLE;
   88:     } else {
   89:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   90:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   91:         $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
   92:     }
   93:     &Apache::loncommon::content_type($r,'text/html');
   94:     $r->send_http_header;
   95: 
   96:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   97:                                             ['action','symb','title']);
   98:     my $action = $env{'form.action'};
   99:     my $symb = $env{'form.symb'};
  100:     my $docs_title = $env{'form.title'};
  101:     my ($mimetype,$numpathchgs,$numrefchanges,%allfiles,%codebase);
  102:     my ($map,$id,$url) = &Apache::lonnet::decode_symb($symb);
  103:     my $title = &mt('Manage Dependencies');
  104:     my $state = '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
  105:                 '<input type="hidden" name="title" value="'.$docs_title.'" />'."\n";
  106:     my $dir_root = '/userfiles';
  107:     my ($destination) =
  108:         ($url =~ m{^\Quploaded/$cdom/$cnum/\E((?:docs|supplemental)/(?:default|\d+)/\d+)/});
  109:     my $js = &Apache::loncommon::ask_embedded_js();
  110:     my $output = &Apache::loncommon::start_page($title,$js,
  111:                                                 {'only_body' => 1});
  112:     if ($action eq 'modifyhrefs') {
  113:         my ($result,$count,$codebasecount) =
  114:             &Apache::loncommon::modify_html_refs('manage_dependencies',$destination,
  115:                                                  $cnum,$cdom,$dir_root);
  116:         $output .= $result;
  117:         $numrefchanges = $count + $codebasecount;
  118:     } elsif ($action eq 'process_changes') {
  119:         (my $result,$numpathchgs) = 
  120:             &process_changes($cdom,$cnum,$chome,$url,$destination,$dir_root,$state);
  121:         $output .= $result;
  122:     }
  123:     unless ((($action eq 'process_changes') && ($numpathchgs > 0)) ||
  124:             (($action eq 'modifyhrefs') && ($numrefchanges > 0))) {
  125:         if ($url =~ m{^\Quploaded/$cdom/$cnum/\E}) {
  126:             $output .= &display_dependencies($url,$state,$docs_title);
  127:         } else {
  128:             $output .= &mt('Cannot display dependency information - invalid file: [_1].',$url);
  129:         }
  130:         $output .= '<p><a href="javascript:window.close()">'.&mt('Close window').'</a>';
  131:     }
  132:     if (($action eq 'modifyhrefs') && ($numrefchanges > 0)) {
  133:         $output .= '<p><a href="javascript:window.opener.location.reload(true);javascript:window.close();">'.
  134:                    &mt('Close pop-up window and reload page').'</a>'; 
  135:     }
  136:     $output .= &Apache::loncommon::end_page();
  137:     $r->print($output);
  138:     return OK;
  139: }
  140: 
  141: sub process_changes {
  142:     my ($cdom,$cnum,$chome,$url,$destination,$dir_root,$state) = @_;
  143:     my ($output,$numpathchgs);
  144:     my ($numnew,$numtodelete,$numtomod) = (0,0,0);
  145:     my $url_root = "/uploaded/$cdom/$cnum";
  146:     if ($destination ne '') {
  147:         my $actionurl = '/adm/dependencies';
  148:         my @deletions = &Apache::loncommon::get_env_multiple('form.del_upload_dep');
  149:         my @modifications = &Apache::loncommon::get_env_multiple('form.mod_upload_dep');
  150:         $numtodelete = scalar(@deletions);
  151:         $numtomod = scalar(@modifications);  
  152:         $numnew = $env{'form.number_newemb_items'};
  153:         if ($numtodelete > 0) {
  154:             my ($outcome,$error);
  155:             my @ids=&Apache::lonnet::current_machine_ids();
  156:             my $dir;
  157:             if (grep(/^\Q$chome\E$/,@ids)) {
  158:                 $dir = &LONCAPA::propath($cdom,$cnum)."$dir_root/$destination";
  159:             }
  160:             foreach my $todelete (@deletions) {
  161:                 my $item = &unescape($env{'form.embedded_orig_'.$todelete});
  162:                 if ($dir eq '') {
  163:                     my $result = 
  164:                         &Apache::lonnet::removeuploadedurl("$url_root/$destination/$item");
  165:                     if ($result eq 'ok') {
  166:                         $outcome .= '<li><span class="LC_filename">'.$item.'</li>';
  167:                     } else {
  168:                         $error .= &mt('Error: [_1] occurred when removing [_2]',$result,$item).'<br />';
  169:                     }
  170:                 } else {
  171:                     if (-e "$dir/$item") {
  172:                         if (unlink("$dir/$item")) {
  173:                             $outcome .= '<li><span class="LC_filename">'.$item.'</li>';
  174:                         } else {
  175:                             $error .= &mt('Removal failed for [_1].',$item).'<br />';
  176:                         }
  177:                     } else {
  178:                         $error .= &mt('File: [_1] not found when attempting removal.',$item).'<br />';
  179:                     }
  180:                 }
  181:             }
  182:             if ($outcome ne '') {
  183:                 $output .= '<h4>'.&mt('Deleted unused files').'</h4>'.
  184:                            '<ul>'.$outcome.'</ul>';
  185:             }
  186:             if ($error ne '') {
  187:                 $output .= '<h4>'.&mt('Error(s) deleting unused files').'</h4>'.
  188:                            '<p class="LC_warning">'.$error.'</p>';
  189:             }
  190:         }
  191:         if ((@modifications > 0) || ($numnew > 0)) {
  192:             (my $result,my $flag,$numpathchgs) =
  193:                 &Apache::loncommon::upload_embedded('coursedoc',$destination,$cnum,$cdom,
  194:                                                     $dir_root,$url_root,undef,undef,undef,
  195:                                                     $state,'/adm/dependencies');
  196:             $output .= '<h4>'.&mt('Uploaded files').'</h4>'. 
  197:                        $result;
  198:         }
  199:     } else {
  200:         $output .=  '<span class="LC_warning">'.
  201:                     &mt('Unable to process any requested dependency changes - invalid file: [_1].',$url).
  202:                     '</span>';
  203:     }
  204:     if (!$numtodelete && !$numtomod && !$numnew) { 
  205:         $output .= '<h4>'.&mt('No changes made.').'</h4>';    
  206:     }
  207:     unless ($numpathchgs) {
  208:         $output .= '<hr />';
  209:     }
  210:     return ($output,$numpathchgs);
  211: }
  212: 
  213: sub display_dependencies {
  214:     my ($url,$state,$docs_title) = @_;
  215:     my ($mimetype,%allfiles,%codebase);
  216:     my $output;
  217:     if (&Apache::lonnet::repcopy_userfile("/$url") eq 'ok') {
  218:         my $file=&Apache::lonnet::filelocation("","/$url");
  219:         my $mm = new File::MMagic;
  220:         $mimetype = $mm->checktype_filename($file);
  221:         if ($mimetype eq 'text/html') {
  222:             my $parse_result = &Apache::lonnet::extract_embedded_items($file,\%allfiles,\%codebase);
  223:             if ($parse_result eq 'ok') {
  224:                 my ($embedded,$num,$delnum) =
  225:                     &Apache::loncommon::ask_for_embedded_content(
  226:                         '/adm/dependencies',$state,\%allfiles,\%codebase,
  227:                         {'error_on_invalid_names'   => 1,
  228:                          'ignore_remote_references' => 1,
  229:                          'docs_url'                 => $url,
  230:                          'docs_title'               => $docs_title});
  231:                 if ($embedded) {
  232:                     $output .= $embedded;
  233:                 } else {
  234:                     $output .= &mt('This file has no dependencies.').'<br />';
  235:                 }
  236:             } else {
  237:                 $output .= '<span class="LC_warning">'.
  238:                            &mt('Could not parse HTML file: [_1] to identify existing dependencies.',"/$url").
  239:                            '</span>';
  240:             }
  241:         } else {
  242:             $output .= '<span class="LC_warning">'.
  243:                        &mt('File: [_1] does not appear to be an HTML file.',"/$url").
  244:                        '</span>';
  245:         }
  246:     } else {
  247:         $output .= '<span class="LC_warning">'.
  248:                    &mt('Failed to access HTML file: [_1] to identify existing dependencies.',
  249:                    "/$url");
  250:     }
  251:     return $output;
  252: }
  253: 
  254: 1;

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