File:  [LON-CAPA] / loncom / interface / londependencies.pm
Revision 1.3: download - view: text, annotated - select for diffs
Thu Nov 29 20:37:07 2012 UTC (11 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Consistent interface.
- HTML files uploaded to course's Supplemental content area can be edited,
  and any dependencies can be replaced (or uploaded if missing).
- "Course View" button provided when "editing" HTML files uploaded
  directly to a course removed; use "Exit Editing" item in Functions instead.

    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.3 2012/11/29 20:37:07 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','url','folderpath']);
   98:     my $action = $env{'form.action'};
   99:     my $symb = $env{'form.symb'};
  100:     my $docs_title = $env{'form.title'};
  101:     my $docs_url = $env{'form.url'};
  102:     my $folderpath = &unescape($env{'form.folderpath'});
  103:     my ($mimetype,$numpathchgs,$numrefchanges,%allfiles,%codebase,$url);
  104:     if ($symb) {
  105:         (undef,undef,$url) = &Apache::lonnet::decode_symb($symb);
  106:     } elsif (($docs_url) && ($env{'httpref.'.$docs_url} ne '')) {
  107:         $url = $docs_url;
  108:         $url =~ s{^/}{};
  109:     }
  110:     my $title = &mt('Manage Dependencies');
  111:     my $state = '<input type="hidden" name="symb" value="'.$symb.'" />'."\n".
  112:                 '<input type="hidden" name="folderpath" value="'.$env{'form.folderpath'}.'" />'."\n".
  113:                 '<input type="hidden" name="title" value="'.$docs_title.'" />'."\n".
  114:                 '<input type="hidden" name="url" value="'.$docs_url.'" />'."\n";
  115:     my $dir_root = '/userfiles';
  116:     my ($destination) =
  117:         ($url =~ m{^\Quploaded/$cdom/$cnum/\E((?:docs|supplemental)/(?:default|\d+)/\d+)/});
  118:     my $js = &Apache::loncommon::ask_embedded_js();
  119:     my $output = &Apache::loncommon::start_page($title,$js,
  120:                                                 {'only_body' => 1});
  121:     if ($action eq 'modifyhrefs') {
  122:         my ($result,$count,$codebasecount) =
  123:             &Apache::loncommon::modify_html_refs('manage_dependencies',$destination,
  124:                                                  $cnum,$cdom,$dir_root);
  125:         $output .= $result;
  126:         $numrefchanges = $count + $codebasecount;
  127:     } elsif ($action eq 'process_changes') {
  128:         (my $result,$numpathchgs) = 
  129:             &process_changes($cdom,$cnum,$chome,$url,$destination,$dir_root,$state);
  130:         $output .= $result;
  131:     }
  132:     unless ((($action eq 'process_changes') && ($numpathchgs > 0)) ||
  133:             (($action eq 'modifyhrefs') && ($numrefchanges > 0))) {
  134:         if ($url =~ m{^\Quploaded/$cdom/$cnum/\E}) {
  135:             $output .= &display_dependencies($url,$state,$docs_title);
  136:         } else {
  137:             $output .= &mt('Cannot display dependency information - invalid file: [_1].',$url);
  138:         }
  139:         $output .= '<p><a href="javascript:window.close()">'.&mt('Close window').'</a>';
  140:     }
  141:     if (($action eq 'modifyhrefs') && ($numrefchanges > 0)) {
  142:         $output .= '<p><a href="javascript:window.opener.location.reload(true);javascript:window.close();">'.
  143:                    &mt('Close pop-up window and reload page').'</a>'; 
  144:     }
  145:     $output .= &Apache::loncommon::end_page();
  146:     $r->print($output);
  147:     return OK;
  148: }
  149: 
  150: sub process_changes {
  151:     my ($cdom,$cnum,$chome,$url,$destination,$dir_root,$state) = @_;
  152:     my ($output,$numpathchgs);
  153:     my ($numnew,$numtodelete,$numtomod) = (0,0,0);
  154:     my $url_root = "/uploaded/$cdom/$cnum";
  155:     if ($destination ne '') {
  156:         my $actionurl = '/adm/dependencies';
  157:         my @deletions = &Apache::loncommon::get_env_multiple('form.del_upload_dep');
  158:         my @modifications = &Apache::loncommon::get_env_multiple('form.mod_upload_dep');
  159:         $numtodelete = scalar(@deletions);
  160:         $numtomod = scalar(@modifications);  
  161:         $numnew = $env{'form.number_newemb_items'};
  162:         if ($numtodelete > 0) {
  163:             my ($outcome,$error);
  164:             my @ids=&Apache::lonnet::current_machine_ids();
  165:             my $dir;
  166:             if (grep(/^\Q$chome\E$/,@ids)) {
  167:                 $dir = &LONCAPA::propath($cdom,$cnum)."$dir_root/$destination";
  168:             }
  169:             foreach my $todelete (@deletions) {
  170:                 my $item = &unescape($env{'form.embedded_orig_'.$todelete});
  171:                 if ($dir eq '') {
  172:                     my $result = 
  173:                         &Apache::lonnet::removeuploadedurl("$url_root/$destination/$item");
  174:                     if ($result eq 'ok') {
  175:                         $outcome .= '<li><span class="LC_filename">'.$item.'</li>';
  176:                     } else {
  177:                         $error .= &mt('Error: [_1] occurred when removing [_2]',$result,$item).'<br />';
  178:                     }
  179:                 } else {
  180:                     if (-e "$dir/$item") {
  181:                         if (unlink("$dir/$item")) {
  182:                             $outcome .= '<li><span class="LC_filename">'.$item.'</li>';
  183:                         } else {
  184:                             $error .= &mt('Removal failed for [_1].',$item).'<br />';
  185:                         }
  186:                     } else {
  187:                         $error .= &mt('File: [_1] not found when attempting removal.',$item).'<br />';
  188:                     }
  189:                 }
  190:             }
  191:             if ($outcome ne '') {
  192:                 $output .= '<h4>'.&mt('Deleted unused files').'</h4>'.
  193:                            '<ul>'.$outcome.'</ul>';
  194:             }
  195:             if ($error ne '') {
  196:                 $output .= '<h4>'.&mt('Error(s) deleting unused files').'</h4>'.
  197:                            '<p class="LC_warning">'.$error.'</p>';
  198:             }
  199:         }
  200:         if ((@modifications > 0) || ($numnew > 0)) {
  201:             (my $result,my $flag,$numpathchgs) =
  202:                 &Apache::loncommon::upload_embedded('coursedoc',$destination,$cnum,$cdom,
  203:                                                     $dir_root,$url_root,undef,undef,undef,
  204:                                                     $state,'/adm/dependencies');
  205:             $output .= '<h4>'.&mt('Uploaded files').'</h4>'. 
  206:                        $result;
  207:         }
  208:     } else {
  209:         $output .=  '<span class="LC_warning">'.
  210:                     &mt('Unable to process any requested dependency changes - invalid file: [_1].',$url).
  211:                     '</span>';
  212:     }
  213:     if (!$numtodelete && !$numtomod && !$numnew) { 
  214:         $output .= '<h4>'.&mt('No changes made.').'</h4>';    
  215:     }
  216:     unless ($numpathchgs) {
  217:         $output .= '<hr />';
  218:     }
  219:     return ($output,$numpathchgs);
  220: }
  221: 
  222: sub display_dependencies {
  223:     my ($url,$state,$docs_title) = @_;
  224:     my ($mimetype,%allfiles,%codebase);
  225:     my $output;
  226:     if (&Apache::lonnet::repcopy_userfile("/$url") eq 'ok') {
  227:         my $file=&Apache::lonnet::filelocation("","/$url");
  228:         my $mm = new File::MMagic;
  229:         $mimetype = $mm->checktype_filename($file);
  230:         if ($mimetype eq 'text/html') {
  231:             my $parse_result = &Apache::lonnet::extract_embedded_items($file,\%allfiles,\%codebase);
  232:             if ($parse_result eq 'ok') {
  233:                 my ($embedded,$num,$delnum) =
  234:                     &Apache::loncommon::ask_for_embedded_content(
  235:                         '/adm/dependencies',$state,\%allfiles,\%codebase,
  236:                         {'error_on_invalid_names'   => 1,
  237:                          'ignore_remote_references' => 1,
  238:                          'docs_url'                 => $url,
  239:                          'docs_title'               => $docs_title});
  240:                 if ($embedded) {
  241:                     $output .= $embedded;
  242:                 } else {
  243:                     $output .= &mt('This file has no dependencies.').'<br />';
  244:                 }
  245:             } else {
  246:                 $output .= '<span class="LC_warning">'.
  247:                            &mt('Could not parse HTML file: [_1] to identify existing dependencies.',"/$url").
  248:                            '</span>';
  249:             }
  250:         } else {
  251:             $output .= '<span class="LC_warning">'.
  252:                        &mt('File: [_1] does not appear to be an HTML file.',"/$url").
  253:                        '</span>';
  254:         }
  255:     } else {
  256:         $output .= '<span class="LC_warning">'.
  257:                    &mt('Failed to access HTML file: [_1] to identify existing dependencies.',
  258:                    "/$url");
  259:     }
  260:     return $output;
  261: }
  262: 
  263: 1;

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