--- loncom/interface/lonmeta.pm	2006/09/29 18:11:49	1.180
+++ loncom/interface/lonmeta.pm	2006/12/05 02:55:53	1.190
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Metadata display handler
 #
-# $Id: lonmeta.pm,v 1.180 2006/09/29 18:11:49 albertel Exp $
+# $Id: lonmeta.pm,v 1.190 2006/12/05 02:55:53 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -39,8 +39,7 @@ use Apache::lonpublisher;
 use Apache::lonlocal;
 use Apache::lonmysql;
 use Apache::lonmsg;
-use lib '/home/httpd/lib/perl/';
-use LONCAPA;
+use LONCAPA qw(:DEFAULT :match);
 
 
 ############################################################
@@ -63,7 +62,7 @@ use LONCAPA;
 ############################################################
 sub get_dynamic_metadata_from_sql {
     my ($url) = shift();
-    my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
+    my ($authordom,$author)=($url=~m{^/res/($match_domain)/($match_username)/});
     if (! defined($authordom)) {
         $authordom = shift();
     }
@@ -112,7 +111,7 @@ sub get_dynamic_metadata_from_sql {
 sub dynamicmeta {
     my $url=&Apache::lonnet::declutter(shift);
     $url=~s/\.meta$//;
-    my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
+    my ($adomain,$aauthor)=($url=~/^($match_domain)\/($match_username)\//);
     my $regexp=$url;
     $regexp=~s/(\W)/\\$1/g;
     $regexp='___'.$regexp.'___';
@@ -449,7 +448,7 @@ sub prettyprint {
 	($type eq 'owner') ||
 	($type eq 'modifyinguser') ||
 	($type eq 'authorspace')) {
-	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
+	$value=~s/($match_username)(\:|\@)($match_domain)/&authordisplay($1,$3)/gse;
 	return $value;
     }
     # Gradelevel
@@ -469,7 +468,7 @@ sub prettyprint {
 	($type eq 'sequsage_list') ||
 	($type eq 'dependencies')) {
 	return '<font size="-1"><ul>'.join("\n",map {
-            my $url = &Apache::lonnet::clutter($_);
+            my $url = &Apache::lonnet::clutter_with_no_wrapper($_);
             my $title = &Apache::lonnet::gettitle($url);
             if ($title eq '') {
                 $title = 'Untitled';
@@ -724,7 +723,7 @@ sub prettyinput {
 sub handler {
     my $r=shift;
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-         ['currentpath']);
+         ['currentpath','changecourse']);
     my $uri=$r->uri;
     #
     # Set document type
@@ -732,21 +731,32 @@ sub handler {
     $r->send_http_header;
     return OK if $r->header_only;
     my ($resdomain,$resuser)=
-        (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
+        (&Apache::lonnet::declutter($uri)=~/^($match_domain)\/($match_username)\//);
     if ($uri=~m:/adm/bombs/(.*)$:) {
         $r->print(&Apache::loncommon::start_page('Error Messages'));
         # Looking for all bombs?
         &report_bombs($r,$uri);
     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
 	    ($resdomain,$resuser)=
-		(&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|);
+		(&Apache::lonnet::declutter($uri)=~m|^($match_domain)/($match_name)/portfolio|);
         $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
 						 undef,
 						 {'domain' => $resdomain,}));
         if ($env{'form.store'}) {
             &present_editable_metadata($r,$uri,'portfolio');
         } else {
-            &pre_select_course($r,$uri);
+            my $fn=&Apache::lonnet::filelocation('',$uri);
+            %Apache::lonpublisher::metadatafields=();
+            %Apache::lonpublisher::metadatakeys=();
+            my $result=&Apache::lonnet::getfile($fn);
+            &Apache::lonpublisher::metaeval($result);
+            &Apache::lonnet::logthis("restricted is: ".$Apache::lonpublisher::metadatafields{'courserestricted'});
+            if ((!$Apache::lonpublisher::metadatafields{'courserestricted'}) ||
+                ($env{'form.changecourse'} eq 'true')) {
+                &pre_select_course($r,$uri);
+            } else {
+                &present_editable_metadata($r,$uri,'portfolio');
+            }
         }
     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/groups/|) {
         $r->print(&Apache::loncommon::start_page('Edit Group Portfolio File Catalog Information',
@@ -782,7 +792,7 @@ sub report_bombs {
     $uri =~ s:/adm/bombs/::;
     $uri = &Apache::lonnet::declutter($uri);
     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
-    my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
+    my ($domain,$author)=($uri=~/^($match_domain)\/($match_username)\//);
     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
 	if ($env{'form.clearbombs'}) {
 	    &Apache::lonmsg::clear_author_res_msg($uri);
@@ -829,8 +839,7 @@ sub present_uneditable_metadata {
     # displayed url
     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
     $uri=~s/\.meta$//;
-    my $disuri=&Apache::lonnet::clutter($uri);
-    $disuri=~s/^\/adm\/wrapper//;
+    my $disuri=&Apache::lonnet::clutter_with_no_wrapper($uri);
     # version
     my $versiondisplay='';
     if (!$uploaded) {
@@ -1050,7 +1059,7 @@ sub print_dynamic_metadata {
     } else {
         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
     }
-    $uri=~/^\/res\/(\w+)\/(\w+)\//; 
+    $uri=~/^\/res\/($match_domain)\/($match_username)\//; 
     if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
         || ($env{'user.role.ca./'.$1.'/'.$2})) {
         if (exists($dynmeta{'comments'})) {
@@ -1193,24 +1202,28 @@ ENDEDIT
 	my $output;
 	my @fields;
 	my $added_metadata_fields;
+	my @added_order;
 	if ($file_type eq 'portfolio' || $file_type eq 'groups') {
 	    if(exists ($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'})) {
 	        # retrieve fieldnames (in order) from the course restricted list
-	        @fields = (split /,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'});
+	        @fields = (split(/,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'}));
 	    } else {
 	        # no saved field list, use default list
 	        @fields =  ('author','title','subject','keywords','abstract',
 			    'notes','lowestgradelevel',
 	                    'highestgradelevel','standards');
 	        $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames();
-	        $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join (",",@fields);
+	        if ($env{'course.'.$env{'request.course.id'}.'.metadata.addedorder'}) {
+	            @added_order = split(/,/,$env{'course.'.$env{'request.course.id'}.'.metadata.addedorder'});
+	        }
+	        $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join(",",@fields);
 	    }
 	} else {
 	    @fields = ('author','title','subject','keywords','abstract','notes',
-                 'copyright','customdistributionfile','language',
-                 'standards',
-                 'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
-                 'obsolete','obsoletereplacement');
+		       'copyright','customdistributionfile','language',
+		       'standards',
+		       'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
+		       'obsolete','obsoletereplacement');
         }
         if ($file_type eq 'groups') {
             $Apache::lonpublisher::metadatafields{'courserestricted'}=
@@ -1239,16 +1252,28 @@ ENDEDIT
 	    }
 
 	    if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
-		$r->print(&mt('Associated with course [_1]','<strong>'.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}.
-			      '</strong>').'<br />');
+		$r->print(&mt('Associated with course [_1]',
+		        '<strong><a href="'.$uri.'?changecourse=true">'.
+		        $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
+		        ".description"}.
+			      '</a></strong>').'<br />');
 	    } else {
-		$r->print("This resource is not associated with a course.<br />");
+		$r->print('<a href="'.$uri.'?changecourse=true">This resource is not associated with a course.</a><br />');
 	    }
 	}
-        foreach my $field_name(keys (%$added_metadata_fields)) {
-            push (@fields,$field_name);
-            $lt{$field_name} = $$added_metadata_fields{$field_name};
+	if (@added_order) {
+	    foreach my $field_name (@added_order) {
+                push(@fields,$field_name);
+                $lt{$field_name} = $$added_metadata_fields{$field_name};
+	    }
+	} else {
+            foreach my $field_name (keys(%$added_metadata_fields)) {
+                push(@fields,$field_name);
+                $lt{$field_name} = $$added_metadata_fields{$field_name};
+            }
         }
+        $output .= &Apache::loncommon::start_data_table();
+        my $row_alt = 1;
         foreach my $field_name (@fields) {
             if (defined($env{'form.new_'.$field_name})) {
                 $Apache::lonpublisher::metadatafields{$field_name}=
@@ -1257,24 +1282,29 @@ ENDEDIT
             if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
 		&& exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
                 # handle restrictions here
-                if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
-                    ($field_name eq 'courserestricted')){
-                    $output.=("\n".'<p>'.$lt{$field_name}.': '.
+                if ((($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
+                    ($field_name eq 'courserestricted'))&&
+                    (!($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/deleted/))){
+                    
+                    $output .= "\n".&Apache::loncommon::start_data_table_row();
+                    $output .= ('<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
                               &prettyinput($field_name,
 				   $Apache::lonpublisher::metadatafields{$field_name},
 				                    'new_'.$field_name,'defaultmeta',
 				                    undef,undef,undef,undef,
-				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</p>'."\n");
+				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</td>');
+                    $output .= &Apache::loncommon::end_data_table_row();
                  }
             } else {
 
-                    $output.=('<p>'.$lt{$field_name}.': '.
-                            &prettyinput($field_name,
-				   $Apache::lonpublisher::metadatafields{$field_name},
-				   'new_'.$field_name,'defaultmeta').'</p>');
+                    $output.=(&Apache::loncommon::start_data_table_row().'<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
+			      &prettyinput($field_name,
+					   $Apache::lonpublisher::metadatafields{$field_name},
+					   'new_'.$field_name,'defaultmeta').'</td>'.&Apache::loncommon::end_data_table_row());
                
             }
         }
+        $output .= &Apache::loncommon::end_data_table();
 	if ($env{'form.store'}) {
 	    my $mfh;
 	    my $formname='store'; 
@@ -1283,7 +1313,7 @@ ENDEDIT
 		$Apache::lonpublisher::metadatafields{'keywords'} = 
 		    join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
 	    }
-	    foreach my $field (sort keys %Apache::lonpublisher::metadatafields) {
+	    foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
 		next if ($field =~ /\./);
 		my $unikey=$field;
 		$unikey=~/^([A-Za-z_]+)/;
@@ -1291,8 +1321,8 @@ ENDEDIT
 		$tag=~tr/A-Z/a-z/;
 		$file_content.= "\n\<$tag";
 		foreach my $key (split(/\,/,
-			       $Apache::lonpublisher::metadatakeys{$unikey})
-			 ) {
+				       $Apache::lonpublisher::metadatakeys{$unikey})
+				 ) {
 		    my $value=
 			$Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
 		    $value=~s/\"/\'\'/g;
@@ -1306,7 +1336,7 @@ ENDEDIT
 	    }
 	    if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
 	        my ($path, $new_fn);
-	        if ($fn =~ m|\w+/groups/\w+/portfolio/|) {
+	        if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
                     ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
 	        } else {
 		    ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
@@ -1314,12 +1344,12 @@ ENDEDIT
                 $r->print(&store_portfolio_metadata($formname,$file_content,$path,
                                                     $new_fn));
             } else {
-		if (!  ($mfh=Apache::File->new('>'.$fn))) {
+		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;
+		    print $mfh ($file_content);
 		    $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
 			      ' '.&Apache::lonlocal::locallocaltime(time).
 			      '</font></p>');