[LON-CAPA-cvs] cvs: loncom /interface lonmeta.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Tue, 16 Jan 2007 07:36:12 -0000


This is a MIME encoded message

--raeburn1168932972
Content-Type: text/plain

raeburn		Tue Jan 16 02:36:12 2007 EDT

  Modified files:              
    /loncom/interface	lonmeta.pm 
  Log:
  Offer the use the option to move metadata stored in added fields to notes field when changing course with which a resource (from portfolio files)  is associated.
  
  Code used to store updated metadata file moved from &present_editable_metadata() to a new subroutine: &store_metadata().
  
  &store_transferred_addedfields() added - used (for portfolio files) to append metadata in any added fields with data (for existing course association) to notes field, when course association changes (if requested by user). 
  
  
--raeburn1168932972
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070116023612.txt"

Index: loncom/interface/lonmeta.pm
diff -u loncom/interface/lonmeta.pm:1.199 loncom/interface/lonmeta.pm:1.200
--- loncom/interface/lonmeta.pm:1.199	Tue Jan 16 02:26:45 2007
+++ loncom/interface/lonmeta.pm	Tue Jan 16 02:36:10 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Metadata display handler
 #
-# $Id: lonmeta.pm,v 1.199 2007/01/16 07:26:45 raeburn Exp $
+# $Id: lonmeta.pm,v 1.200 2007/01/16 07:36:10 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -383,7 +383,40 @@
 	$Apache::lonpublisher::metadatafields{'courserestricted'};
     my $selected = ($current_restriction eq 'none' ? 'selected="selected"' 
 		                                   : '');
-
+    if ($current_restriction =~ /^course\.($match_domain\_$match_courseid)$/) {
+        my $assoc_crs = $1;
+        my $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
+        if (ref($added_metadata_fields) eq 'HASH') {
+            if (keys(%{$added_metadata_fields}) > 0) {
+                my $transfernotes;
+                foreach my $field_name (keys(%{$added_metadata_fields})) {
+                    my $value = $Apache::lonpublisher::metadatafields{$field_name};
+                    if ($value) {
+                        $transfernotes .= 
+                            &Apache::loncommon::start_data_table_row(). 
+                            '<td><input type="checkbox" name="transfer_'.
+                            $field_name.'" value="1" /></td><td>'.
+                            $field_name.'</td><td>'.$value.'</td>'.
+                            &Apache::loncommon::end_data_table_row();
+                    }
+                }
+                if ($transfernotes ne '') {
+                    my %courseinfo = &Apache::lonnet::coursedescription($assoc_crs,{'one_time' => 1});
+                    my $assoc_crs_description = $courseinfo{'description'};
+                    $output .= &mt('This resource is currently associated with a course ([_1]) which includes added metadata fields specific to the course.',$assoc_crs_description).'<br />'."\n".
+                    &mt('You can choose to transfer data from the added fields to the "Notes" field if you are planning to change the course association.').'<br /><br />'.
+                    &Apache::loncommon::start_data_table().
+                    &Apache::loncommon::start_data_table_header_row().
+                    '<th>Copy to notes?</th>'."\n".
+                    '<th>Field name</th>'."\n".
+                    '<th>Values</th>'."\n".
+                    &Apache::loncommon::end_data_table_header_row().
+                    $transfernotes.
+                    &Apache::loncommon::end_data_table().'<br />';
+                }
+            }
+        }
+    }
     $output .= '<select name="new_courserestricted" >';
     $output .= '<option value="none" '.$selected.'>'.
 	&mt('None').'</option>'.$/;
@@ -1196,6 +1229,22 @@
         } else {
             &Apache::lonpublisher::metaeval($result);
         }
+        if ($env{'form.new_courserestricted'}) {
+            my $new_assoc_course = $env{'form.new_courserestricted'};
+            my $prev_courserestricted = $Apache::lonpublisher::metadatafields{'courserestricted'};
+            if (($prev_courserestricted) && 
+                ($prev_courserestricted ne $new_assoc_course)) {
+                my $transfers = [];
+                foreach my $key (keys(%env)) {
+                    if ($key =~ /^form\.transfer_(.+)$/) {
+                        push(@{$transfers},$1);
+                    }
+                }
+                if (@{$transfers} > 0) {
+                    &store_transferred_addedfields($fn,$uri,$transfers);
+                }
+            }
+        }
         $r->print(<<ENDEDIT);
 <h1>$displayfile</h1>
 <form method="post" action="" name="defaultmeta">
@@ -1321,57 +1370,8 @@
         }
         $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('<p><font color="red">'.
-			      &mt('Could not write metadata').', '.
-			      &mt('FAIL').'</font></p>');
-		} else {
-		    print $mfh ($file_content);
-                    close($mfh);
-                    &update_metadata_table($uri);
-		    $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
-			      ' '.&Apache::lonlocal::locallocaltime(time).
-			      '</font></p>');
-		}
-	    }
+            my ($outcome,$result) = &store_metadata($fn,$uri,'store');
+            $r->print($result);
 	}
 	$r->print($output.'<br /><input type="submit" name="store" value="'.
                   &mt('Store Catalog Information').'" />');
@@ -1396,20 +1396,114 @@
     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 .= '<p><font color="red">';
+            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').'</font></p>';
+            $outcome = 'fail';
+        } else {
+            print $mfh ($file_content);
+            close($mfh);
+            &update_metadata_table($uri);
+            $output .= '<p><font color="blue">';
+            if ($caller eq 'transfer') {
+                $output .= &mt('Transferred data in added fields to notes');
+            } else {
+                $output .= &mt('Wrote Metadata');
+            }
+            $output .= ' '.&Apache::lonlocal::locallocaltime(time).
+                       '</font></p>';
+            $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 '<p><font color="red">'.
-                  &mt('Could not write metadata').', '.
-                  &mt('FAIL').'</font></p>';
+        $output = '<p><font color="red">';
+        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').'</font></p>';
+        $outcome = 'fail';
     } else {
         &update_metadata_table($uri);
-        return '<p><font color="blue">'.&mt('Wrote Metadata').
-                  ' '.&Apache::lonlocal::locallocaltime(time).'</font></p>';
+        $output = '<p><font color="blue">';
+        if ($caller eq 'transfer') {
+            $output .= &mt('Transferred data in added fields to notes');
+        } else {
+            $output .= &mt('Wrote Metadata');
+        }
+        $output .= ' '.&Apache::lonlocal::locallocaltime(time).
+                   '</font></p>';
+        $outcome = 'ok';
     }
+    return ($outcome,$output);
 }
 
 sub update_metadata_table {

--raeburn1168932972--