Annotation of loncom/interface/londependencies.pm, revision 1.2

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

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