--- loncom/interface/londocs.pm 2011/05/27 19:39:25 1.434.2.4
+++ loncom/interface/londocs.pm 2010/08/16 08:58:39 1.435
@@ -1,7 +1,7 @@
# The LearningOnline Network
# Documents
#
-# $Id: londocs.pm,v 1.434.2.4 2011/05/27 19:39:25 raeburn Exp $
+# $Id: londocs.pm,v 1.435 2010/08/16 08:58:39 wenzelju Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -1660,7 +1660,7 @@ sub editor {
.'
'.&mt('Actions').' '
.''.&mt('Document').' ');
if ($folder !~ /^supplemental/) {
- $r->print(''.&mt('Settings').' ');
+ $->print(''.&mt('Settings').' ');
}
$r->print(&Apache::loncommon::end_data_table_header_row());
}
@@ -1682,10 +1682,11 @@ sub editor {
sub process_file_upload {
my ($upload_output,$coursenum,$coursedom,$allfiles,$codebase,$uploadcmd) = @_;
# upload a file, if present
- my ($parseaction,$showupload,$nextphase,$mimetype);
- if ($env{'form.parserflag'}) {
+ my $parseaction;
+ if ($env{'form.parserflag'}) {
$parseaction = 'parse';
}
+ my $phase_status;
my $folder=$env{'form.folder'};
if ($folder eq '') {
$folder='default';
@@ -1704,8 +1705,7 @@ sub process_file_upload {
$LONCAPA::map::resources[1]='';
}
if ($fatal) {
- $$upload_output = ''.&mt('The uploaded file has not been stored as an error occurred reading the contents of the current folder.').'
';
- return;
+ return 'failed';
}
my $destination = 'docs/';
if ($folder =~ /^supplemental/) {
@@ -1716,23 +1716,13 @@ sub process_file_upload {
} elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
$destination .= $2.'/';
}
-# this is for a course, not a user, so set context to coursedoc.
+# this is for a course, not a user, so set coursedoc flag
+# probably the only place in the system where this should be "1"
my $newidx=&LONCAPA::map::getresidx();
$destination .= $newidx;
- my $url=&Apache::lonnet::userfileupload('uploaddoc','coursedoc',$destination,
+ my $url=&Apache::lonnet::userfileupload('uploaddoc',1,$destination,
$parseaction,$allfiles,
- $codebase,undef,undef,undef,undef,
- undef,undef,\$mimetype);
- if ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E.*/([^/]+)$}) {
- my $stored = $1;
- $showupload = ''.&mt('Uploaded [_1]',''.
- $stored.' ').'
';
- } else {
- my ($filename) = ($env{'form.uploaddoc.filename'} =~ m{([^/]+)$});
-
- $$upload_output = ''.&mt('Unable to save file [_1].',''.$filename.' ').'
';
- return;
- }
+ $codebase);
my $ext='false';
if ($url=~m{^http://}) { $ext='true'; }
$url = &LONCAPA::map::qtunescape($url);
@@ -1749,37 +1739,51 @@ sub process_file_upload {
($errtext,$fatal)=&storemap($coursenum,$coursedom,
$folder.'.'.$container);
if ($fatal) {
- $$upload_output = ''.$errtext.'
';
- return;
+ $$upload_output .= ''.$errtext.'
';
+ return 'failed';
} else {
- if ($parseaction eq 'parse' && $mimetype eq 'text/html') {
- $$upload_output = $showupload;
+ if ($parseaction eq 'parse') {
my $total_embedded = scalar(keys(%{$allfiles}));
if ($total_embedded > 0) {
- my $uploadphase = 'upload_embedded';
- my $primaryurl = &HTML::Entities::encode($url,'<>&"');
- my $state = &embedded_form_elems($uploadphase,$primaryurl,$newidx);
- my ($embedded,$num) =
- &Apache::loncommon::ask_for_embedded_content(
- '/adm/coursedocs',$state,$allfiles,$codebase,{'docs_url' => $url});
- if ($embedded) {
- if ($num) {
- $$upload_output .=
- ''.&mt('This file contains embedded multimedia objects, which need to be uploaded.').'
'.$embedded;
- $nextphase = $uploadphase;
- } else {
- $$upload_output .= $embedded;
- }
- } else {
- $$upload_output .= &mt('Embedded item(s) already present, so no additional upload(s) required').' ';
- }
+ my $num = 0;
+ my $state = '
+
+
+
+
+ ';
+ $phase_status = 'phasetwo';
+
+ $$upload_output .=
+ 'This file contains embedded multimedia objects, which need to be uploaded to LON-CAPA. '.
+ &Apache::loncommon::ask_for_embedded_content(
+ '/adm/coursedocs',$state,$allfiles,$codebase);
} else {
- $$upload_output .= &mt('No embedded items identified').' ';
+ $$upload_output .= 'No embedded items identified ';
}
}
}
}
- return $nextphase;
+ return $phase_status;
+}
+
+sub process_secondary_uploads {
+ my ($upload_output,$coursedom,$coursenum,$formname,$num,$newidx) = @_;
+ my $folder=$env{'form.folder'};
+ my $destination = 'docs/';
+ if ($folder =~ /^supplemental/) {
+ $destination = 'supplemental/';
+ }
+ if (($folder eq 'default') || ($folder eq 'supplemental')) {
+ $destination .= 'default/';
+ } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
+ $destination .= $2.'/';
+ }
+ $destination .= $newidx;
+ my ($url,$filename);
+ $url=&Apache::lonnet::userfileupload($formname.$num,1,$destination);
+ ($filename) = ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E/\Q$destination\E/(.+)$});
+ return $filename;
}
sub is_supplemental_title {
@@ -1844,7 +1848,7 @@ sub entryline {
}
if ($env{'form.pagepath'}) {
$type = $container = 'page';
- $esc_path=&escape($env{'form.pagepath'});
+ $esc_path=&escape($path = $env{'form.pagepath'});
$path = &HTML::Entities::encode($env{'form.pagepath'},'<>&"');
$symb=&escape($env{'form.pagesymb'});
}
@@ -2821,49 +2825,92 @@ sub create_form_ul {
my %allfiles = ();
my %codebase = ();
- my ($upload_result,$upload_output,$uploadphase);
+ my ($upload_result,$upload_output);
if ($allowed) {
if (($env{'form.uploaddoc.filename'}) &&
($env{'form.cmd'}=~/^upload_(\w+)/)) {
- my $context = $1;
- # Process file upload - phase one - upload and parse primary file.
- undef($hadchanges);
- $uploadphase = &process_file_upload(\$upload_output,$coursenum,$coursedom,
- \%allfiles,\%codebase,$context);
+# Process file upload - phase one - upload and parse primary file.
+ undef($hadchanges);
+ $upload_result = &process_file_upload(\$upload_output,$coursenum,
+ $coursedom,\%allfiles,
+ \%codebase,$1);
if ($hadchanges) {
&mark_hash_old();
}
- $r->print($upload_output);
- } elsif ($env{'form.phase'} eq 'upload_embedded') {
- # Process file upload - phase two - upload embedded objects
- $uploadphase = 'check_embedded';
- my $primaryurl = &HTML::Entities::encode($env{'form.primaryurl'},'<>&"');
- my $state = &embedded_form_elems($uploadphase,$primaryurl,
- $env{'form.newidx'});
- my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
- my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
- my ($destination,$dir_root) = &embedded_destination();
- my $url_root = '/uploaded/'.$docudom.'/'.$docuname;
- my $actionurl = '/adm/coursedocs';
- my ($result,$flag) =
- &Apache::loncommon::upload_embedded('coursedoc',$destination,
- $docuname,$docudom,$dir_root,$url_root,undef,undef,undef,$state,
- $actionurl);
- $r->print($result.&return_to_editor());
- } elsif ($env{'form.phase'} eq 'check_embedded') {
- # Process file upload - phase three - modify references in HTML file
- $uploadphase = 'modified_orightml';
- my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
- my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
- my ($destination,$dir_root) = &embedded_destination();
- $r->print(&Apache::loncommon::modify_html_refs('coursedoc',$destination,
- $docuname,$docudom,undef,
- $dir_root).
- &return_to_editor());
+ if ($upload_result eq 'phasetwo') {
+ $r->print($upload_output);
+ }
+ } elsif ($env{'form.phasetwo'}) {
+ my %newname = ();
+ my %origname = ();
+ my %attribs = ();
+ my $updateflag = 0;
+ my $residx = $env{'form.newidx'};
+ my $primary_url = &unescape($env{'form.primaryurl'});
+# Process file upload - phase two - gather secondary files.
+ for (my $i=0; $i<$env{'form.phasetwo'}; $i++) {
+ if ($env{'form.embedded_item_'.$i.'.filename'}) {
+ my $javacodebase;
+ $newname{$i} = &process_secondary_uploads(\$upload_output,$coursedom,$coursenum,'embedded_item_',$i,$residx);
+ $origname{$i} = &unescape($env{'form.embedded_orig_'.$i});
+ if (exists($env{'form.embedded_codebase_'.$i})) {
+ $javacodebase = &unescape($env{'form.embedded_codebase_'.$i});
+ $origname{$i} =~ s#^\Q$javacodebase\E/##;
+ }
+ my @attributes = ();
+ if ($env{'form.embedded_attrib_'.$i} =~ /:/) {
+ @attributes = split(/:/,$env{'form.embedded_attrib_'.$i});
+ } else {
+ @attributes = ($env{'form.embedded_attrib_'.$i});
+ }
+ foreach my $attr (@attributes) {
+ push(@{$attribs{$i}},&unescape($attr));
+ }
+ if ($javacodebase) {
+ $codebase{$i} = $javacodebase;
+ $codebase{$i} =~ s#/$##;
+ $updateflag = 1;
+ }
+ }
+ unless ($newname{$i} eq $origname{$i}) {
+ $updateflag = 1;
+ }
+ }
+# Process file upload - phase three - modify primary file
+ if ($updateflag) {
+ my ($content,$rtncode);
+ my $updateflag = 0;
+ my $getstatus = &Apache::lonnet::getuploaded('GET',$primary_url,$coursedom,$coursenum,\$content,\$rtncode);
+ if ($getstatus eq 'ok') {
+ foreach my $item (keys(%newname)) {
+ if ($newname{$item} ne $origname{$item}) {
+ my $attrib_regexp = '';
+ if (@{$attribs{$item}} > 1) {
+ $attrib_regexp = join('|',@{$attribs{$item}});
+ } else {
+ $attrib_regexp = $attribs{$item}[0];
+ }
+ if ($content =~ m#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#) {
+ }
+ $content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#$1$newname{$item}$2#gi;
+ }
+ if (exists($codebase{$item})) {
+ $content =~ s/(codebase\s*=\s*["']?)\Q$codebase{$item}\E(["']?)/$1.$2/i; #' stupid emacs
+ }
+ }
+# Save edited file.
+ my $saveresult;
+ my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
+ my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
+ my $url = &Apache::lonnet::store_edited_file($primary_url,$content,$docudom,$docuname,\$saveresult);
+ } else {
+ &Apache::lonnet::logthis('retrieval of uploaded file - '.$primary_url.' - for editing, failed: '.$getstatus);
+ }
+ }
}
}
- unless ($showdoc || $uploadphase) {
+ unless ($showdoc || $upload_result eq 'phasetwo') {
# -----------------------------------------------------------------------------
my %lt=&Apache::lonlocal::texthash(
'uplm' => 'Upload a new main '.lc($crstype).' document',
@@ -2874,6 +2921,7 @@ sub create_form_ul {
'srch' => 'Search',
'impo' => 'Import',
'book' => 'Import Bookmarks',
+ 'wish' => 'Import from Wishlist',
'selm' => 'Select Map',
'load' => 'Load Map',
'reco' => 'Recover Deleted Documents',
@@ -2924,7 +2972,7 @@ CHBO
$fileupload
$lt{'title'}:
-
+
$uploadtag
@@ -2947,6 +2995,7 @@ SEDFFORM
{ ' ' => "$uploadtag" },
{ ' ' => "$help{'Importing_LON-CAPA_Resource'}" },
{ ' ' => "" },
+ { ' ' => "" },
);
$simpleeditdefaultform .= create_form_ul(create_list_elements(@simpleeditdefaultforma));
$simpleeditdefaultform .=(<print(''.$tabtitles{'main'}{$crstype}.' ');
$active = '';
if (!$forcestandard || ($env{'form.folderpath'}=~/^supplemental/)) {
@@ -3024,6 +3074,7 @@ HIDDENFORM
}
}
$r->print(''.$tabtitles{'supplemental'}{$crstype}.' ');
$r->print('');
} else {
@@ -3421,7 +3472,7 @@ $r->print('');
');
}
} else {
- unless ($uploadphase) {
+ unless ($upload_result eq 'phasetwo') {
# -------------------------------------------------------- This is showdoc mode
$r->print("".&mt('Uploaded Document').' - '.
&Apache::lonnet::gettitle($r->uri).' '.
@@ -3434,42 +3485,6 @@ $r->print('');
return OK;
}
-sub embedded_form_elems {
- my ($phase,$primaryurl,$newidx) = @_;
- my $folderpath = &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
- return <
-
-
-
-
-STATE
-}
-
-sub embedded_destination {
- my $folder=$env{'form.folder'};
- my $destination = 'docs/';
- if ($folder =~ /^supplemental/) {
- $destination = 'supplemental/';
- }
- if (($folder eq 'default') || ($folder eq 'supplemental')) {
- $destination .= 'default/';
- } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
- $destination .= $2.'/';
- }
- $destination .= $env{'form.newidx'};
- my $dir_root = '/userfiles';
- return ($destination,$dir_root);
-}
-
-sub return_to_editor {
- my $actionurl = '/adm/coursedocs';
- return '
'."\n".
- ' '."\n".
- ''.&mt('Return to Editor').
- ' ';
-}
-
sub generate_admin_options {
my ($help_ref,$env_ref) = @_;
my %lt=&Apache::lonlocal::texthash(