'.
@@ -1286,8 +1346,15 @@ ENDEDIT
my $row_alt = 1;
foreach my $field_name (@fields) {
if (defined($env{'form.new_'.$field_name})) {
- $Apache::lonpublisher::metadatafields{$field_name}=
- join(',',&Apache::loncommon::get_env_multiple('form.new_'.$field_name));
+ my @values = &Apache::loncommon::get_env_multiple('form.new_'.$field_name);
+ my $newvalue = '';
+ foreach my $item (@values) {
+ if ($item ne '') {
+ $newvalue .= $item.',';
+ }
+ }
+ $newvalue =~ s/,$//;
+ $Apache::lonpublisher::metadatafields{$field_name}=$newvalue;
}
if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
&& exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
@@ -1316,57 +1383,8 @@ ENDEDIT
}
$output .= &Apache::loncommon::end_data_table();
if ($env{'form.store'}) {
- my $mfh;
- my $formname='store';
- my $file_content;
- if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
- $Apache::lonpublisher::metadatafields{'keywords'} =
- join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
- }
- foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
- next if ($field =~ /\./);
- my $unikey=$field;
- $unikey=~/^([A-Za-z_]+)/;
- my $tag=$1;
- $tag=~tr/A-Z/a-z/;
- $file_content.= "\n\<$tag";
- foreach my $key (split(/\,/,
- $Apache::lonpublisher::metadatakeys{$unikey})
- ) {
- my $value=
- $Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
- $value=~s/\"/\'\'/g;
- $file_content.=' '.$key.'="'.$value.'"' ;
- }
- $file_content.= '>'.
- &HTML::Entities::encode
- ($Apache::lonpublisher::metadatafields{$unikey},
- '<>&"').
- ''.$tag.'>';
- }
- if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
- my ($path, $new_fn);
- if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
- ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
- } else {
- ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
- }
- $r->print(&store_portfolio_metadata($formname,$file_content,$path,
- $new_fn,$uri));
- } else {
- if (! ($mfh=Apache::File->new('>'.$fn))) {
- $r->print(''.
- &mt('Could not write metadata').', '.
- &mt('FAIL').'
');
- } else {
- print $mfh ($file_content);
- close($mfh);
- &update_metadata_table($uri);
- $r->print(''.&mt('Wrote Metadata').
- ' '.&Apache::lonlocal::locallocaltime(time).
- '
');
- }
- }
+ my ($outcome,$result) = &store_metadata($fn,$uri,'store');
+ $r->print($result);
}
$r->print($output.' ');
@@ -1391,40 +1409,126 @@ ENDEDIT
return;
}
+sub store_metadata {
+ my ($fn,$uri,$caller) = @_;
+ my $mfh;
+ my $formname='store';
+ my ($file_content,$output,$outcome);
+ if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
+ $Apache::lonpublisher::metadatafields{'keywords'} =
+ join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
+ }
+ foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
+ next if ($field =~ /\./);
+ my $unikey=$field;
+ $unikey=~/^([A-Za-z_]+)/;
+ my $tag=$1;
+ $tag=~tr/A-Z/a-z/;
+ $file_content.= "\n\<$tag";
+ foreach my $key (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
+ my $value = $Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
+ $value=~s/\"/\'\'/g;
+ $file_content.=' '.$key.'="'.$value.'"' ;
+ }
+ $file_content.= '>'.
+ &HTML::Entities::encode
+ ($Apache::lonpublisher::metadatafields{$unikey},'<>&"').
+ ''.$tag.'>';
+ }
+ if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
+ my ($path, $new_fn);
+ if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
+ ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
+ } else {
+ ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
+ }
+ ($outcome,my $result) =
+ &store_portfolio_metadata($formname,$file_content,
+ $path,$new_fn,$uri,$caller);
+ $output .= $result;
+ } else {
+ if (! ($mfh=Apache::File->new('>'.$fn))) {
+ $output .= '';
+ if ($caller eq 'transfer') {
+ $output .= &mt('Could not transfer data in added fields to notes');
+ } else {
+ $output .= &mt('Could not write metadata');
+ }
+ $output .= ', '.&mt('FAIL').'
';
+ $outcome = 'fail';
+ } else {
+ print $mfh ($file_content);
+ close($mfh);
+ &update_metadata_table($uri);
+ $output .= '';
+ if ($caller eq 'transfer') {
+ $output .= &mt('Transferred data in added fields to notes');
+ } else {
+ $output .= &mt('Wrote Metadata');
+ }
+ $output .= ' '.&Apache::lonlocal::locallocaltime(time).
+ '
';
+ $outcome = 'ok';
+ }
+ }
+ return ($outcome,$output);
+}
+
+sub store_transferred_addedfields {
+ my ($fn,$uri,$transfers) = @_;
+ foreach my $item (@{$transfers}) {
+ $Apache::lonpublisher::metadatafields{'notes'} .=
+ ' '.$item.' = '.$Apache::lonpublisher::metadatafields{$item};
+ }
+ my ($outcome,$output) = &store_metadata($fn,$uri,'transfer');
+ if ($outcome eq 'ok') {
+ foreach my $item (@{$transfers}) {
+ delete($Apache::lonpublisher::metadatafields{$item});
+ }
+ }
+}
+
sub store_portfolio_metadata {
- my ($formname,$content,$path,$new_fn,$uri) = @_;
+ my ($formname,$content,$path,$new_fn,$uri,$caller) = @_;
+ my ($outcome,$output);
$env{'form.'.$formname}=$content."\n";
$env{'form.'.$formname.'.filename'}=$new_fn;
my $result =&Apache::lonnet::userfileupload($formname,'',$path);
if ($result =~ /(error|notfound)/) {
- return ''.
- &mt('Could not write metadata').', '.
- &mt('FAIL').'
';
+ $output = '';
+ if ($caller eq 'transfer') {
+ $output .=
+ &mt('Could not transfer data in added fields to notes');
+ } else {
+ $output .= &mt('Could not write metadata');
+ }
+ $output .= ', '.&mt('FAIL').'
';
+ $outcome = 'fail';
} else {
&update_metadata_table($uri);
- return ''.&mt('Wrote Metadata').
- ' '.&Apache::lonlocal::locallocaltime(time).'
';
+ $output = '';
+ if ($caller eq 'transfer') {
+ $output .= &mt('Transferred data in added fields to notes');
+ } else {
+ $output .= &mt('Wrote Metadata');
+ }
+ $output .= ' '.&Apache::lonlocal::locallocaltime(time).
+ '
';
+ $outcome = 'ok';
}
+ return ($outcome,$output);
}
sub update_metadata_table {
my ($uri) = @_;
- my ($group,$file_name);
- my ($udom,$uname,$remainder) =
- ($uri=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
-
- if ($remainder =~ /^groups\/(\w+)\/portfolio(\/.+)$/) {
- $group = $1;
- $file_name = $2;
- } elsif ($remainder =~ /^portfolio(\/.+)$/) {
- $file_name = $1;
- }
+ my ($type,$udom,$uname,$file_name,$group) =
+ &Apache::lonnet::parse_portfolio_url($uri);
$file_name =~ s/\.meta$//;
my $current_permissions =
&Apache::lonnet::get_portfile_permissions($udom,$uname);
my %access_controls =
&Apache::lonnet::get_access_controls($current_permissions,$group,
- $group.$file_name);
+ $file_name);
my $access_hash = $access_controls{$file_name};
my $available = 0;
if (ref($access_hash) eq 'HASH') {