--- loncom/interface/lonmeta.pm 2005/12/15 22:18:57 1.139 +++ loncom/interface/lonmeta.pm 2005/12/19 21:56:07 1.147 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Metadata display handler # -# $Id: lonmeta.pm,v 1.139 2005/12/15 22:18:57 banghart Exp $ +# $Id: lonmeta.pm,v 1.147 2005/12/19 21:56:07 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -290,11 +290,73 @@ sub fieldnames { return &Apache::lonlocal::texthash(%fields); } +sub portfolio_linked_path { + my ($path) = @_; + my $result = &Apache::portfolio::make_anchor('portfolio','/'); + my $fullpath = '/'; + my (undef,@tree) = split('/',$path); + my $filename = pop(@tree); + foreach my $dir (@tree) { + $fullpath .= $dir.'/'; + $result .= '/'; + $result .= &Apache::portfolio::make_anchor($dir,$fullpath); + } + $result .= "/$filename"; + return $result; +} + +sub portfolio_display_uri { + my ($uri,$as_links)=@_; + $uri =~ s|.*/(portfolio/.*)$|$1|; + my ($res_uri,$meta_uri) = ($uri,$uri); + + if ($uri =~ /\.meta$/) { + $res_uri =~ s/\.meta//; + } else { + $meta_uri .= '.meta'; + } + + my ($path) = ($res_uri =~ m|(.*)/[^/]*$|); + + if ($as_links) { + $res_uri = &portfolio_linked_path($res_uri); + $meta_uri = &portfolio_linked_path($meta_uri); + } + return ($res_uri,$meta_uri,$path); +} + +sub pre_select_course { + my ($r,$uri) = @_; + my $output; + my $fn=&Apache::lonnet::filelocation('',$uri); + my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri); + %Apache::lonpublisher::metadatafields=(); + %Apache::lonpublisher::metadatakeys=(); + my $result=&Apache::lonnet::getfile($fn); + if ($result == -1){ + $r->print(&mt('Creating new file [_1]'),$meta_uri); + } else { + &Apache::lonpublisher::metaeval($result); + } + $r->print('
'); + $r->print('

'.&mt('If you would like to associate this resource ([_1]) with a current or previous course, please select one from the list below, otherwise select, \'None\'',''.$res_uri.'').'

'); + $output = &select_course(); + $r->print($output.'
'); + $r->print('
'); + + $r->print('

'. + ''. + ''. + '
'); + + return; +} sub select_course { my %courses; my $output; my $selected; - foreach my $key (keys (%env)) { + foreach my $key (keys(%env)) { if ($key =~ m/\.metadata\./) { $key =~ m/^course\.(.+)(\.metadata.+$)/; my $course = $1; @@ -648,11 +710,15 @@ sub handler { # Looking for all bombs? &report_bombs($r,$uri); } elsif ($uri=~/\/portfolio\//) { - ($resdomain,$resuser)= + ($resdomain,$resuser)= (&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|); $r->print(&Apache::loncommon::bodytag ('Edit Portfolio File Information','','','',$resdomain)); - &present_editable_metadata($r,$uri,'portfolio'); + if ($env{'form.store'}) { + &present_editable_metadata($r,$uri,'portfolio'); + } else { + &pre_select_course($r,$uri); + } } elsif ($uri=~/^\/\~/) { # Construction space $r->print(&Apache::loncommon::bodytag @@ -1004,7 +1070,11 @@ sub present_editable_metadata { my $fn=&Apache::lonnet::filelocation('',$uri); $disuri=~s/^\/\~/\/priv\//; $disuri=~s/\.meta$//; - $disuri=~s|^/editupload||; + my $meta_uri = $disuri; + my $path; + if ($disuri =~ m|/portfolio/|) { + ($disuri, $meta_uri, $path) = &portfolio_display_uri($disuri,1); + } my $target=$uri; $target=~s/^\/\~/\/res\/$env{'request.role.domain'}\//; $target=~s/\.meta$//; @@ -1058,7 +1128,7 @@ ENDDEL %Apache::lonpublisher::metadatakeys=(); my $result=&Apache::lonnet::getfile($fn); if ($result == -1){ - $r->print('Creating new '.$disuri); + $r->print(&mt('Creating new file [_1]'),$meta_uri); } else { &Apache::lonpublisher::metaeval($result); } @@ -1074,7 +1144,7 @@ ENDEDIT my @fields; if ($file_type eq 'portfolio') { @fields = ('author','title','subject','keywords','abstract','notes','lowestgradelevel', - 'highestgradelevel','standards','courserestricted'); + 'highestgradelevel','standards'); } else { @fields = ('author','title','subject','keywords','abstract','notes', 'copyright','customdistributionfile','language', @@ -1095,12 +1165,12 @@ ENDEDIT 'default'; } if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') { - $r->print('Using: '.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}. - " metadata framework
"); + $r->print(&mt('Associated with course [_1]',''.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}. + '').'
'); } else { - $r->print("This resource is not associated with a metadata framework
"); + $r->print("This resource is not associated with a course.
"); } - foreach my $field_name(@fields) { + foreach my $field_name (@fields) { if (defined($env{'form.new_'.$field_name})) { $Apache::lonpublisher::metadatafields{$field_name}= @@ -1110,12 +1180,12 @@ ENDEDIT # handle restrictions here if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) || ($field_name eq 'courserestricted')){ - $output.=('

'.$lt{$field_name}.': '. + $output.=("\n".'

'.$lt{$field_name}.': '. &prettyinput($field_name, $Apache::lonpublisher::metadatafields{$field_name}, 'new_'.$field_name,'defaultmeta', undef,undef,undef,undef, - $Apache::lonpublisher::metadatafields{'courserestricted'}).'

'); + $Apache::lonpublisher::metadatafields{'courserestricted'}).'

'."\n"); } } else { @@ -1126,73 +1196,78 @@ ENDEDIT } } - - $r->print($output.'
'); - - } - $r->print(''); - if ($env{'form.store'}) { - my $mfh; - my $formname='store'; - my $file_content; - foreach my $meta_field (keys %env) { - if (&Apache::loncommon::get_env_multiple('form.new_keywords')) { - $Apache::lonpublisher::metadatafields{'keywords'} = - join (',', &Apache::loncommon::get_env_multiple('form.new_keywords')); - } - } - foreach (sort keys %Apache::lonpublisher::metadatafields) { - next if ($_ =~ /\./); - my $unikey=$_; - $unikey=~/^([A-Za-z]+)/; - my $tag=$1; - $tag=~tr/A-Z/a-z/; - $file_content.= "\n\<$tag"; - foreach (split(/\,/, - $Apache::lonpublisher::metadatakeys{$unikey}) - ) { - my $value= - $Apache::lonpublisher::metadatafields{$unikey.'.'.$_}; - $value=~s/\"/\'\'/g; - $file_content.=' '.$_.'="'.$value.'"' ; - # print $mfh ' '.$_.'="'.$value.'"'; - } - $file_content.= '>'. - &HTML::Entities::encode - ($Apache::lonpublisher::metadatafields{$unikey}, - '<>&"'). - ''; - } - if ($fn =~ /\/portfolio\//) { - $fn =~ /\/portfolio\/(.*)$/; - my $new_fn = '/'.$1; - $env{'form.'.$formname}=$file_content."\n"; - $env{'form.'.$formname.'.filename'}=$new_fn; - &Apache::lonnet::userfileupload('uploaddoc','', - 'portfolio'.$env{'form.currentpath'}); - if (&Apache::lonnet::userfileupload($formname,'','portfolio') eq 'error: no uploaded file') { - $r->print('

'. - &mt('Could not write metadata').', '. - &mt('FAIL').'

'); - } else { - $r->print('

'.&mt('Wrote Metadata'). - ' '.&Apache::lonlocal::locallocaltime(time). - '

'); - } - } else { - if (! ($mfh=Apache::File->new('>'.$fn))) { - $r->print('

'. - &mt('Could not write metadata').', '. - &mt('FAIL').'

'); - } else { - print $mfh $file_content; + 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 (sort keys %Apache::lonpublisher::metadatafields) { + next if ($_ =~ /\./); + my $unikey=$_; + $unikey=~/^([A-Za-z]+)/; + my $tag=$1; + $tag=~tr/A-Z/a-z/; + $file_content.= "\n\<$tag"; + foreach (split(/\,/, + $Apache::lonpublisher::metadatakeys{$unikey}) + ) { + my $value= + $Apache::lonpublisher::metadatafields{$unikey.'.'.$_}; + $value=~s/\"/\'\'/g; + $file_content.=' '.$_.'="'.$value.'"' ; + # print $mfh ' '.$_.'="'.$value.'"'; + } + $file_content.= '>'. + &HTML::Entities::encode + ($Apache::lonpublisher::metadatafields{$unikey}, + '<>&"'). + ''; + } + if ($fn =~ m|/portfolio/|) { + my ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|); + $env{'form.'.$formname}=$file_content."\n"; + $env{'form.'.$formname.'.filename'}=$new_fn; + my $result =&Apache::lonnet::userfileupload($formname,'', + $path); + + if ($result =~ /(error|notfound)/) { + $r->print('

'. + &mt('Could not write metadata').', '. + &mt('FAIL').'

'); + } else { $r->print('

'.&mt('Wrote Metadata'). ' '.&Apache::lonlocal::locallocaltime(time). '

'); - } - } - } + } + } else { + if (! ($mfh=Apache::File->new('>'.$fn))) { + $r->print('

'. + &mt('Could not write metadata').', '. + &mt('FAIL').'

'); + } else { + print $mfh $file_content; + $r->print('

'.&mt('Wrote Metadata'). + ' '.&Apache::lonlocal::locallocaltime(time). + '

'); + } + } + } + + $r->print($output.'
'); + + $r->print(' +

'. + ''. + ''); + + } + $r->print('
'); + return; }