File:  [LON-CAPA] / loncom / interface / londependencies.pm
Revision 1.4: download - view: text, annotated - select for diffs
Fri May 3 21:57:13 2013 UTC (11 years ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
- Provide the ability to edit an HTML file uploaded as the syllabus, i.e.,
  a user supplied file, instead of the templated syllabus, and to manage
  dependencies (e.g., images etc.)

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

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