File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.162: download - view: text, annotated - select for diffs
Fri Aug 4 19:42:55 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- can display the metadata of a portfolio file

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.162 2006/08/04 19:42:55 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License 
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: 
   28: 
   29: package Apache::lonmeta;
   30: 
   31: use strict;
   32: use LONCAPA::lonmetadata();
   33: use Apache::Constants qw(:common);
   34: use Apache::lonnet;
   35: use Apache::loncommon();
   36: use Apache::lonhtmlcommon(); 
   37: use Apache::lonmsg;
   38: use Apache::lonpublisher;
   39: use Apache::lonlocal;
   40: use Apache::lonmysql;
   41: use Apache::lonmsg;
   42: use lib '/home/httpd/lib/perl/';
   43: use LONCAPA;
   44: 
   45: 
   46: ############################################################
   47: ############################################################
   48: ##
   49: ## &get_dynamic_metadata_from_sql($url)
   50: ## 
   51: ## Queries sql database for dynamic metdata
   52: ## Returns a hash of hashes, with keys of urls which match $url
   53: ## Returned fields are given below.
   54: ##
   55: ## Examples:
   56: ## 
   57: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
   58: ##     ('/res/msu/korte/');
   59: ##
   60: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
   61: ##
   62: ############################################################
   63: ############################################################
   64: sub get_dynamic_metadata_from_sql {
   65:     my ($url) = shift();
   66:     my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
   67:     if (! defined($authordom)) {
   68:         $authordom = shift();
   69:     }
   70:     if  (! defined($author)) { 
   71:         $author = shift();
   72:     }
   73:     if (! defined($authordom) || ! defined($author)) {
   74:         return ();
   75:     }
   76:     my $query = 'SELECT * FROM metadata WHERE url LIKE "'.$url.'%"';
   77:     my $server = &Apache::lonnet::homeserver($author,$authordom);
   78:     my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
   79:                                                 ,[$server]);
   80:     return () if (! defined($reply) || ref($reply) ne 'HASH');
   81:     my $filename = $reply->{$server};
   82:     if (! defined($filename) || $filename =~ /^error/) {
   83:         return ();
   84:     }
   85:     my $max_time = time + 10; # wait 10 seconds for results at most
   86:     my %ReturnHash;
   87:     #
   88:     # Look for results
   89:     my $finished = 0;
   90:     while (! $finished && time < $max_time) {
   91:         my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
   92:         if (! -e "$datafile.end") { next; }
   93:         my $fh;
   94:         if (!($fh=Apache::File->new($datafile))) { next; }
   95:         while (my $result = <$fh>) {
   96:             chomp($result);
   97:             next if (! $result);
   98:             my %hash=&LONCAPA::lonmetadata::metadata_col_to_hash(map { &unescape($_) } split(/\,/,$result));
   99:             foreach my $key (keys(%hash)) {
  100:                 $ReturnHash{$hash{'url'}}->{$key}=$hash{$key};
  101:             }
  102:         }
  103:         $finished = 1;
  104:     }
  105:     #
  106:     return %ReturnHash;
  107: }
  108: 
  109: 
  110: # Fetch and evaluate dynamic metadata
  111: sub dynamicmeta {
  112:     my $url=&Apache::lonnet::declutter(shift);
  113:     $url=~s/\.meta$//;
  114:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
  115:     my $regexp=$url;
  116:     $regexp=~s/(\W)/\\$1/g;
  117:     $regexp='___'.$regexp.'___';
  118:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
  119: 				       $aauthor,$regexp);
  120:     my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  121:     my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  122:                                                                \%DynamicData);
  123:     #
  124:     # Deal with 'count' separately
  125:     $Data{'count'} = &access_count($url,$aauthor,$adomain);
  126:     #
  127:     # Debugging code I will probably need later
  128:     if (0) {
  129:         &Apache::lonnet::logthis('Dynamic Metadata');
  130:         while(my($k,$v)=each(%Data)){
  131:             &Apache::lonnet::logthis('    "'.$k.'"=>"'.$v.'"');
  132:         }
  133:         &Apache::lonnet::logthis('-------------------');
  134:     }
  135:     return %Data;
  136: }
  137: 
  138: sub access_count {
  139:     my ($src,$author,$adomain) = @_;
  140:     my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
  141:                                         $author,$src);
  142:     if (! exists($countdata{$src})) {
  143:         return &mt('Not Available');
  144:     } else {
  145:         return $countdata{$src};
  146:     }
  147: }
  148: 
  149: # Try to make an alt tag if there is none
  150: sub alttag {
  151:     my ($base,$src)=@_;
  152:     my $fullpath=&Apache::lonnet::hreflocation($base,$src);
  153:     my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
  154:         &Apache::lonnet::metadata($fullpath,'subject').' '.
  155:         &Apache::lonnet::metadata($fullpath,'abstract');
  156:     $alttag=~s/\s+/ /gs;
  157:     $alttag=~s/\"//gs;
  158:     $alttag=~s/\'//gs;
  159:     $alttag=~s/\s+$//gs;
  160:     $alttag=~s/^\s+//gs;
  161:     if ($alttag) { 
  162:         return $alttag; 
  163:     } else { 
  164:         return &mt('No information available'); 
  165:     }
  166: }
  167: 
  168: # Author display
  169: sub authordisplay {
  170:     my ($aname,$adom)=@_;
  171:     return &Apache::loncommon::aboutmewrapper
  172:         (&Apache::loncommon::plainname($aname,$adom),
  173:          $aname,$adom,'preview').' <tt>['.$aname.'@'.$adom.']</tt>';
  174: }
  175: 
  176: # Pretty display
  177: sub evalgraph {
  178:     my $value=shift;
  179:     if (! $value) { 
  180:         return '';
  181:     }
  182:     my $val=int($value*10.+0.5)-10;
  183:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  184:     if ($val>=20) {
  185: 	$output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  186:     } else {
  187:         $output.='<td width="'.($val).'" bgcolor="#555555">&nbsp;</td>'.
  188:                  '<td width="'.(20-$val).'" bgcolor="#FF3333">&nbsp;</td>';
  189:     }
  190:     $output.='<td bgcolor="#FFFF33">&nbsp;</td>';
  191:     if ($val>20) {
  192: 	$output.='<td width="'.($val-20).'" bgcolor="#33FF33">&nbsp;</td>'.
  193:                  '<td width="'.(40-$val).'" bgcolor="#555555">&nbsp;</td>';
  194:     } else {
  195:         $output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  196:     }
  197:     $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
  198:     return $output;
  199: }
  200: 
  201: sub diffgraph {
  202:     my $value=shift;
  203:     if (! $value) { 
  204:         return '';
  205:     }
  206:     my $val=int(40.0*$value+0.5);
  207:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
  208:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
  209:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  210:     for (my $i=0;$i<8;$i++) {
  211: 	if ($val>$i*5) {
  212:             $output.='<td width="5" bgcolor="'.$colors[$i].'">&nbsp;</td>';
  213:         } else {
  214: 	    $output.='<td width="5" bgcolor="#555555">&nbsp;</td>';
  215: 	}
  216:     }
  217:     $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
  218:     return $output;
  219: }
  220: 
  221: 
  222: # The field names
  223: sub fieldnames {
  224:     my $file_type=shift;
  225:     my %fields = 
  226:         ('title' => 'Title',
  227:          'author' =>'Author(s)',
  228:          'authorspace' => 'Author Space',
  229:          'modifyinguser' => 'Last Modifying User',
  230:          'subject' => 'Subject',
  231:          'standards' => 'Standards',
  232:          'keywords' => 'Keyword(s)',
  233:          'notes' => 'Notes',
  234:          'abstract' => 'Abstract',
  235:          'lowestgradelevel' => 'Lowest Grade Level',
  236:          'highestgradelevel' => 'Highest Grade Level');
  237:     
  238:     if (! defined($file_type) || $file_type ne 'portfolio') {
  239:         %fields = 
  240: 	    (%fields,
  241: 	     'courserestricted' => 'Course Restricting Metadata');
  242:     }
  243:          
  244:     if (! defined($file_type) || $file_type ne 'portfolio') {
  245:         %fields = 
  246:         (%fields,
  247:          'domain' => 'Domain',
  248:          'mime' => 'MIME Type',
  249:          'language' => 'Language',
  250:          'creationdate' => 'Creation Date',
  251:          'lastrevisiondate' => 'Last Revision Date',
  252:          'owner' => 'Publisher/Owner',
  253:          'copyright' => 'Copyright/Distribution',
  254:          'customdistributionfile' => 'Custom Distribution File',
  255:          'sourceavail' => 'Source Available',
  256:          'sourcerights' => 'Source Custom Distribution File',
  257:          'obsolete' => 'Obsolete',
  258:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
  259:          'count'      => 'Network-wide number of accesses (hits)',
  260:          'course'     => 'Network-wide number of courses using resource',
  261:          'course_list' => 'Network-wide courses using resource',
  262:          'sequsage'      => 'Number of resources using or importing resource',
  263:          'sequsage_list' => 'Resources using or importing resource',
  264:          'goto'       => 'Number of resources that follow this resource in maps',
  265:          'goto_list'  => 'Resources that follow this resource in maps',
  266:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
  267:          'comefrom_list' => 'Resources that lead up to this resource in maps',
  268:          'clear'      => 'Material presented in clear way',
  269:          'depth'      => 'Material covered with sufficient depth',
  270:          'helpful'    => 'Material is helpful',
  271:          'correct'    => 'Material appears to be correct',
  272:          'technical'  => 'Resource is technically correct', 
  273:          'avetries'   => 'Average number of tries till solved',
  274:          'stdno'      => 'Total number of students who have worked on this problem',
  275:          'difficulty' => 'Degree of difficulty',
  276:          'disc'       => 'Degree of discrimination',
  277: 	     'dependencies' => 'Resources used by this resource',
  278:          );
  279:     }
  280:     return &Apache::lonlocal::texthash(%fields);
  281: }
  282: 
  283: sub portfolio_linked_path {
  284:     my ($path,$group,$port_path) = @_;
  285: 
  286:     my $start = 'portfolio';
  287:     if ($group) {
  288: 	$start = "groups/$group/".$start;
  289:     }
  290:     my $result = &Apache::portfolio::make_anchor($port_path,$start,'/');
  291:     
  292:     my $fullpath = '/';
  293:     my (undef,@tree) = split('/',$path);
  294:     my $filename = pop(@tree);
  295:     foreach my $dir (@tree) {
  296: 	$fullpath .= $dir.'/';
  297: 	$result .= '/';
  298: 	$result .= &Apache::portfolio::make_anchor($port_path,$dir,$fullpath);
  299:     }
  300:     $result .= "/$filename";
  301:     return $result;
  302: }
  303: 
  304: sub get_port_path_and_group {
  305:     my ($uri)=@_;
  306: 
  307:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  308:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  309: 
  310:     my ($port_path,$group);
  311:     if ($uri =~ m{^/editupload/\Q$cdom\E/\Q$cnum\E/groups/}) {
  312: 	$group = (split('/',$uri))[5];
  313: 	$port_path = '/adm/coursegrp_portfolio';
  314:     } else {
  315: 	$port_path = '/adm/portfolio';
  316:     }
  317:     if ($env{'form.group'} ne $group) {
  318: 	$env{'form.group'} = $group;
  319:     }
  320:     return ($port_path,$group);
  321: }
  322: 
  323: sub portfolio_display_uri {
  324:     my ($uri,$as_links)=@_;
  325: 
  326:     my ($port_path,$group) = &get_port_path_and_group($uri);
  327: 
  328:     $uri =~ s|.*/(portfolio/.*)$|$1|;
  329:     my ($res_uri,$meta_uri) = ($uri,$uri);
  330:     if ($uri =~ /\.meta$/) {
  331: 	$res_uri =~ s/\.meta//;
  332:     } else {
  333: 	$meta_uri .= '.meta';
  334:     }
  335: 
  336:     my ($path) = ($res_uri =~ m|^portfolio(.*/)[^/]*$|);
  337:     if ($as_links) {
  338: 	$res_uri = &portfolio_linked_path($res_uri,$group,$port_path);
  339: 	$meta_uri = &portfolio_linked_path($meta_uri,$group,$port_path);
  340:     }
  341:     return ($res_uri,$meta_uri,$path);
  342: }
  343: 
  344: sub pre_select_course {
  345:     my ($r,$uri) = @_;
  346:     my $output;
  347:     my $fn=&Apache::lonnet::filelocation('',$uri);
  348:     my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri);
  349:     %Apache::lonpublisher::metadatafields=();
  350:     %Apache::lonpublisher::metadatakeys=();
  351:     my $result=&Apache::lonnet::getfile($fn);
  352:     if ($result == -1){
  353:         $r->print(&mt('Creating new file [_1]'),$meta_uri);
  354:     } else {
  355:         &Apache::lonpublisher::metaeval($result);
  356:     }
  357:     $r->print('<hr /><form method="post" action="" >');
  358:     $r->print('<p>'.&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\'','<tt>'.$res_uri.'</tt>').'</p>');
  359:     $output = &select_course();
  360:     $r->print($output.'<br /><input type="submit" name="store" value="'.
  361:                   &mt('Associate Resource With Selected Course').'">');
  362:     $r->print('</form>');
  363:     
  364:     my ($port_path,$group) = &get_port_path_and_group($uri);
  365:     $r->print('<br /><br /><form method="POST" action="'.$port_path.'">'.
  366:               '<input type="hidden" name="currentpath" value="'.$path.'" />'.
  367: 	      '<input type="hidden" name="group" value="'.$group.'" />'.
  368: 	      '<input type="submit" name="cancel" value="'.&mt('Cancel').'">'.
  369: 	      '</form>');
  370: 
  371:     return;
  372: }
  373: sub select_course {
  374:     my $output=$/;
  375:     my $current_restriction=
  376: 	$Apache::lonpublisher::metadatafields{'courserestricted'};
  377:     my $selected = ($current_restriction eq 'none' ? 'selected="selected"' 
  378: 		                                   : '');
  379: 
  380:     $output .= '<select name="new_courserestricted" >';
  381:     $output .= '<option value="none" '.$selected.'>'.
  382: 	&mt('None').'</option>'.$/;
  383:     my %courses;
  384:     foreach my $key (keys(%env)) {
  385:         if ($key !~ m/^course\.(.+)\.description$/) { next; }
  386: 	my $cid = $1;
  387:         if ($env{$key} !~ /\S/) { next; }
  388: 	$courses{$key} = $cid;
  389:     }
  390:     foreach my $key (sort { lc($env{$a}) cmp lc($env{$b}) } (keys(%courses))) {
  391: 	my $cid = 'course.'.$courses{$key};
  392: 	my $selected = ($current_restriction eq $cid ? 'selected="selected"' 
  393: 		                                     : '');
  394:         if ($env{$key} !~ /\S/) { next; }
  395: 	$output .= '<option value="'.$cid.'" '.$selected.'>';
  396: 	$output .= $env{$key};
  397: 	$output .= '</option>'.$/;
  398: 	$selected = '';
  399:     }
  400:     $output .= '</select><br />';
  401:     return ($output);
  402: }
  403: # Pretty printing of metadata field
  404: 
  405: sub prettyprint {
  406:     my ($type,$value,$target,$prefix,$form,$noformat)=@_;
  407: # $target,$prefix,$form are optional and for filecrumbs only
  408:     if (! defined($value)) { 
  409:         return '&nbsp;'; 
  410:     }
  411:     # Title
  412:     if ($type eq 'title') {
  413: 	return '<font size="+1" face="arial">'.$value.'</font>';
  414:     }
  415:     # Dates
  416:     if (($type eq 'creationdate') ||
  417: 	($type eq 'lastrevisiondate')) {
  418: 	return ($value?&Apache::lonlocal::locallocaltime(
  419: 			  &Apache::lonmysql::unsqltime($value)):
  420: 		&mt('not available'));
  421:     }
  422:     # Language
  423:     if ($type eq 'language') {
  424: 	return &Apache::loncommon::languagedescription($value);
  425:     }
  426:     # Copyright
  427:     if ($type eq 'copyright') {
  428: 	return &Apache::loncommon::copyrightdescription($value);
  429:     }
  430:     # Copyright
  431:     if ($type eq 'sourceavail') {
  432: 	return &Apache::loncommon::source_copyrightdescription($value);
  433:     }
  434:     # MIME
  435:     if ($type eq 'mime') {
  436:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
  437:             &Apache::loncommon::filedescription($value);
  438:     }
  439:     # Person
  440:     if (($type eq 'author') || 
  441: 	($type eq 'owner') ||
  442: 	($type eq 'modifyinguser') ||
  443: 	($type eq 'authorspace')) {
  444: 	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  445: 	return $value;
  446:     }
  447:     # Gradelevel
  448:     if (($type eq 'lowestgradelevel') ||
  449: 	($type eq 'highestgradelevel')) {
  450: 	return &Apache::loncommon::gradeleveldescription($value);
  451:     }
  452:     # Only for advance users below
  453:     if (! $env{'user.adv'}) { 
  454:         return '<i>- '.&mt('not displayed').' -</i>';
  455:     }
  456:     # File
  457:     if (($type eq 'customdistributionfile') ||
  458: 	($type eq 'obsoletereplacement') ||
  459: 	($type eq 'goto_list') ||
  460: 	($type eq 'comefrom_list') ||
  461: 	($type eq 'sequsage_list') ||
  462: 	($type eq 'dependencies')) {
  463: 	return '<font size="-1"><ul>'.join("\n",map {
  464:             my $url = &Apache::lonnet::clutter($_);
  465:             my $title = &Apache::lonnet::gettitle($url);
  466:             if ($title eq '') {
  467:                 $title = 'Untitled';
  468:                 if ($url =~ /\.sequence$/) {
  469:                     $title .= ' Sequence';
  470:                 } elsif ($url =~ /\.page$/) {
  471:                     $title .= ' Page';
  472:                 } elsif ($url =~ /\.problem$/) {
  473:                     $title .= ' Problem';
  474:                 } elsif ($url =~ /\.html$/) {
  475:                     $title .= ' HTML document';
  476:                 } elsif ($url =~ m:/syllabus$:) {
  477:                     $title .= ' Syllabus';
  478:                 } 
  479:             }
  480:             $_ = '<li>'.$title.' '.
  481: 		&Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
  482:                 '</li>'
  483: 	    } split(/\s*\,\s*/,$value)).'</ul></font>';
  484:     }
  485:     # Evaluations
  486:     if (($type eq 'clear') ||
  487: 	($type eq 'depth') ||
  488: 	($type eq 'helpful') ||
  489: 	($type eq 'correct') ||
  490: 	($type eq 'technical')) {
  491: 	return &evalgraph($value);
  492:     }
  493:     # Difficulty
  494:     if ($type eq 'difficulty' || $type eq 'disc') {
  495: 	return &diffgraph($value);
  496:     }
  497:     # List of courses
  498:     if ($type=~/\_list/) {
  499:         my @Courses = split(/\s*\,\s*/,$value);
  500:         my $Str='<font size="-1"><ul>';
  501:         foreach my $course (@Courses) {
  502:             my %courseinfo =
  503: 		&Apache::lonnet::coursedescription($course,
  504: 						   {'one_time' => 1});
  505:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  506:                 next;
  507:             }
  508:             $Str .= '<li><a href="/public/'.$courseinfo{'domain'}.'/'.
  509:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
  510:                 $courseinfo{'description'}.'</a></li>';
  511:         }
  512: 	return $Str.'</ul></font>';
  513:     }
  514:     # No pretty print found
  515:     return $value;
  516: }
  517: 
  518: # Pretty input of metadata field
  519: sub direct {
  520:     return shift;
  521: }
  522: 
  523: sub selectbox {
  524:     my ($name,$value,$functionref,@idlist)=@_;
  525:     if (! defined($functionref)) {
  526:         $functionref=\&direct;
  527:     }
  528:     my $selout='<select name="'.$name.'">';
  529:     foreach (@idlist) {
  530:         $selout.='<option value=\''.$_.'\'';
  531:         if ($_ eq $value) {
  532: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  533: 	}
  534:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  535:     }
  536:     return $selout.'</select>';
  537: }
  538: 
  539: sub relatedfield {
  540:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
  541:     if (! $relatedsearchflag) { 
  542:         return '';
  543:     }
  544:     if (! defined($relatedsep)) {
  545:         $relatedsep=' ';
  546:     }
  547:     if (! $show) {
  548:         return $relatedsep.'&nbsp;';
  549:     }
  550:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
  551: 	($relatedvalue?' checked="1"':'').' />';
  552: }
  553: 
  554: sub prettyinput {
  555:     my ($type,$value,$fieldname,$formname,
  556: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
  557:     if (! defined($size)) {
  558:         $size = 80;
  559:     }
  560:     my $output;
  561:     if (defined($course_key) 
  562: 	&& exists($env{$course_key.'.metadata.'.$type.'.options'})) {
  563:         my $stu_add;
  564:         my $only_one;
  565:         my %meta_options;
  566:         my @cur_values_inst;
  567:         my $cur_values_stu;
  568:         my $values = $env{$course_key.'.metadata.'.$type.'.values'};
  569:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
  570:             $stu_add = 'true';
  571:         }
  572:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
  573:             $only_one = 'true';
  574:         }
  575:         # need to take instructor values out of list where instructor and student
  576:         # values may be mixed.
  577:         if ($values) {
  578:             foreach my $item (split(/,/,$values)) {
  579:                 $item =~ s/^\s+//;
  580:                 $meta_options{$item} = $item;
  581:             }
  582:             foreach my $item (split(/,/,$value)) {
  583:                 $item =~ s/^\s+//;
  584:                 if ($meta_options{$item}) {
  585:                     push(@cur_values_inst,$item);
  586:                 } else {
  587:                     $cur_values_stu .= $item.',';
  588:                 }
  589:             }
  590:         } else {
  591:             $cur_values_stu = $value;
  592:         }
  593:         if ($type eq 'courserestricted') {
  594:             return (&select_course());
  595:             # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  596:         }
  597:         if (($type eq 'keywords') || ($type eq 'subject')
  598:              || ($type eq 'author')||($type eq  'notes')
  599:              || ($type eq  'abstract')|| ($type eq  'title')|| ($type eq  'standards')) {
  600:             if ($values) {
  601:                 if ($only_one) {
  602:                     $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
  603:                 } else {
  604:                     $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
  605:                 }
  606:             }
  607:             if ($stu_add) {
  608:                 $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  609:                 'value="'.$cur_values_stu.'" />'.
  610:                 &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  611:                       $relatedvalue); 
  612:             }
  613:             return ($output);
  614:         }
  615:         if (($type eq 'lowestgradelevel') ||
  616: 	    ($type eq 'highestgradelevel')) {
  617: 	    return &Apache::loncommon::select_level_form($value,$fieldname).
  618:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  619:         }
  620:         return(); 
  621:     }
  622:     # Language
  623:     if ($type eq 'language') {
  624: 	return &selectbox($fieldname,
  625: 			  $value,
  626: 			  \&Apache::loncommon::languagedescription,
  627: 			  (&Apache::loncommon::languageids)).
  628:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  629:     }
  630:     # Copyright
  631:     if ($type eq 'copyright') {
  632: 	return &selectbox($fieldname,
  633: 			  $value,
  634: 			  \&Apache::loncommon::copyrightdescription,
  635: 			  (&Apache::loncommon::copyrightids)).
  636:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  637:     }
  638:     # Source Copyright
  639:     if ($type eq 'sourceavail') {
  640: 	return &selectbox($fieldname,
  641: 			  $value,
  642: 			  \&Apache::loncommon::source_copyrightdescription,
  643: 			  (&Apache::loncommon::source_copyrightids)).
  644:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  645:     }
  646:     # Gradelevels
  647:     if (($type eq 'lowestgradelevel') ||
  648: 	($type eq 'highestgradelevel')) {
  649: 	return &Apache::loncommon::select_level_form($value,$fieldname).
  650:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  651:     }
  652:     # Obsolete
  653:     if ($type eq 'obsolete') {
  654: 	return '<input type="checkbox" name="'.$fieldname.'"'.
  655: 	    ($value?' checked="1"':'').' />'.
  656:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  657:     }
  658:     # Obsolete replacement file
  659:     if ($type eq 'obsoletereplacement') {
  660: 	return '<input type="text" name="'.$fieldname.
  661: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  662: 	    "('".$formname."','".$fieldname."'".
  663: 	    ",'')\">".&mt('Select').'</a>'.
  664:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  665:     }
  666:     # Customdistribution file
  667:     if ($type eq 'customdistributionfile') {
  668: 	return '<input type="text" name="'.$fieldname.
  669: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  670: 	    "('".$formname."','".$fieldname."'".
  671: 	    ",'rights')\">".&mt('Select').'</a>'.
  672:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  673:     }
  674:     # Source Customdistribution file
  675:     if ($type eq 'sourcerights') {
  676: 	return '<input type="text" name="'.$fieldname.
  677: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  678: 	    "('".$formname."','".$fieldname."'".
  679: 	    ",'rights')\">".&mt('Select').'</a>'.
  680:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  681:     }
  682:     if ($type eq 'courserestricted') {
  683:         return (&select_course());
  684:         #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  685:     }
  686: 
  687:     # Dates
  688:     if (($type eq 'creationdate') ||
  689: 	($type eq 'lastrevisiondate')) {
  690: 	return 
  691:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
  692:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  693:     }
  694:     # No pretty input found
  695:     $value=~s/^\s+//gs;
  696:     $value=~s/\s+$//gs;
  697:     $value=~s/\s+/ /gs;
  698:     $value=~s/\"/\&quot\;/gs;
  699:     return 
  700:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  701:         'value="'.$value.'" />'.
  702:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  703:                       $relatedvalue); 
  704: }
  705: 
  706: # Main Handler
  707: sub handler {
  708:     my $r=shift;
  709:     #
  710:     my $uri=$r->uri;
  711:     #
  712:     # Set document type
  713:     &Apache::loncommon::content_type($r,'text/html');
  714:     $r->send_http_header;
  715:     return OK if $r->header_only;
  716:     #
  717:     my ($resdomain,$resuser)=
  718:         (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
  719:     if ($uri=~m:/adm/bombs/(.*)$:) {
  720:         $r->print(&Apache::loncommon::start_page('Error Messages'));
  721:         # Looking for all bombs?
  722:         &report_bombs($r,$uri);
  723:     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
  724: 	    ($resdomain,$resuser)=
  725: 		(&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|);
  726:         $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
  727: 						 undef,
  728: 						 {'domain' => $resdomain,}));
  729:         if ($env{'form.store'}) {
  730:             &present_editable_metadata($r,$uri,'portfolio');
  731:         } else {
  732:             &pre_select_course($r,$uri);
  733:         }
  734:     } elsif ($uri=~m|^/~|) { 
  735:         # Construction space
  736:         $r->print(&Apache::loncommon::start_page('Edit Catalog nformation',
  737: 						 undef,
  738: 						 {'domain' => $resdomain,}));
  739:         &present_editable_metadata($r,$uri);
  740:     } else {
  741:         $r->print(&Apache::loncommon::start_page('Catalog Information',
  742: 						 undef,
  743: 						 {'domain' => $resdomain,}));
  744:         &present_uneditable_metadata($r,$uri);
  745:     }
  746:     $r->print(&Apache::loncommon::end_page());
  747:     return OK;
  748: }
  749: 
  750: #####################################################
  751: #####################################################
  752: ###                                               ###
  753: ###                Report Bombs                   ###
  754: ###                                               ###
  755: #####################################################
  756: #####################################################
  757: sub report_bombs {
  758:     my ($r,$uri) = @_;
  759:     # Set document type
  760:     $uri =~ s:/adm/bombs/::;
  761:     $uri = &Apache::lonnet::declutter($uri);
  762:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  763:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
  764:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  765: 	if ($env{'form.clearbombs'}) {
  766: 	    &Apache::lonmsg::clear_author_res_msg($uri);
  767: 	}
  768:         my $clear=&mt('Clear all Messages in Subdirectory');
  769: 	$r->print(<<ENDCLEAR);
  770: <form method="post">
  771: <input type="submit" name="clearbombs" value="$clear" />
  772: </form>
  773: ENDCLEAR
  774:         my %brokenurls = 
  775:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  776:         foreach (sort(keys(%brokenurls))) {
  777:             if ($_=~/^\Q$uri\E/) {
  778:                 $r->print
  779:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  780:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  781:                      '<hr />');
  782:             }
  783:         }
  784:     } else {
  785:         $r->print(&mt('Not authorized'));
  786:     }
  787:     return;
  788: }
  789: 
  790: #####################################################
  791: #####################################################
  792: ###                                               ###
  793: ###        Uneditable Metadata Display            ###
  794: ###                                               ###
  795: #####################################################
  796: #####################################################
  797: sub present_uneditable_metadata {
  798:     my ($r,$uri) = @_;
  799:     #
  800:     my $uploaded = ($uri =~ m|/uploaded/|);
  801:     my %content=();
  802:     # Read file
  803:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  804:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  805:     }
  806:     # Render Output
  807:     # displayed url
  808:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  809:     $uri=~s/\.meta$//;
  810:     my $disuri=&Apache::lonnet::clutter($uri);
  811:     $disuri=~s/^\/adm\/wrapper//;
  812:     # version
  813:     my $versiondisplay='';
  814:     if (!$uploaded) {
  815: 	my $currentversion=&Apache::lonnet::getversion($disuri);
  816: 	if ($thisversion) {
  817: 	    $versiondisplay=&mt('Version').': '.$thisversion.
  818: 		' ('.&mt('most recent version').': '.
  819: 		($currentversion>0 ? 
  820: 		 $currentversion   :
  821: 		 &mt('information not available')).')';
  822: 	} else {
  823: 	    $versiondisplay='Version: '.$currentversion;
  824: 	}
  825:     }
  826:     # crumbify displayed URL               uri     target prefix form  size
  827:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  828:     $disuri =~ s:<br />::g;
  829:     # obsolete
  830:     my $obsolete=$content{'obsolete'};
  831:     my $obsoletewarning='';
  832:     if (($obsolete) && ($env{'user.adv'})) {
  833:         $obsoletewarning='<p><font color="red">'.
  834:             &mt('This resource has been marked obsolete by the author(s)').
  835:             '</font></p>';
  836:     }
  837:     #
  838:     my %lt=&fieldnames();
  839:     my $table='';
  840:     my $title = $content{'title'};
  841:     if (! defined($title)) {
  842:         $title = 'Untitled Resource';
  843:     }
  844:     foreach ('title', 
  845:              'author', 
  846:              'subject', 
  847:              'keywords', 
  848:              'notes', 
  849:              'abstract',
  850:              'lowestgradelevel',
  851:              'highestgradelevel',
  852:              'standards', 
  853:              'mime', 
  854:              'language', 
  855:              'creationdate', 
  856:              'lastrevisiondate', 
  857:              'owner', 
  858:              'copyright', 
  859:              'customdistributionfile',
  860:              'sourceavail',
  861:              'sourcerights', 
  862:              'obsolete', 
  863:              'obsoletereplacement') {
  864:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
  865:             '</td><td bgcolor="#CCCCCC">'.
  866:             &prettyprint($_,$content{$_}).'</td></tr>';
  867:         delete $content{$_};
  868:     }
  869:     #
  870:     $r->print(<<ENDHEAD);
  871: <h2>$title</h2>
  872: <p>
  873: $disuri<br />
  874: $obsoletewarning
  875: $versiondisplay
  876: </p>
  877: <table cellspacing="2" border="0">
  878: $table
  879: </table>
  880: ENDHEAD
  881:     if (!$uploaded && $env{'user.adv'}) {
  882:         &print_dynamic_metadata($r,$uri,\%content);
  883:     }
  884:     return;
  885: }
  886: 
  887: sub print_dynamic_metadata {
  888:     my ($r,$uri,$content) = @_;
  889:     #
  890:     my %content = %$content;
  891:     my %lt=&fieldnames();
  892:     #
  893:     my $description = 'Dynamic Metadata (updated periodically)';
  894:     $r->print('<h3>'.&mt($description).'</h3>'.
  895:               &mt('Processing'));
  896:     $r->rflush();
  897:     my %items=&fieldnames();
  898:     my %dynmeta=&dynamicmeta($uri);
  899:     #
  900:     # General Access and Usage Statistics
  901:     if (exists($dynmeta{'count'}) ||
  902:         exists($dynmeta{'sequsage'}) ||
  903:         exists($dynmeta{'comefrom'}) ||
  904:         exists($dynmeta{'goto'}) ||
  905:         exists($dynmeta{'course'})) {
  906:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  907:                   '<table cellspacing="2" border="0">');
  908:         foreach ('count',
  909:                  'sequsage','sequsage_list',
  910:                  'comefrom','comefrom_list',
  911:                  'goto','goto_list',
  912:                  'course','course_list') {
  913:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  914:                       '<td bgcolor="#CCCCCC">'.
  915:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  916:         }
  917:         $r->print('</table>');
  918:     } else {
  919:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
  920:     }
  921:     #
  922:     # Assessment statistics
  923:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  924:         if (exists($dynmeta{'stdno'}) ||
  925:             exists($dynmeta{'avetries'}) ||
  926:             exists($dynmeta{'difficulty'}) ||
  927:             exists($dynmeta{'disc'})) {
  928:             # This is an assessment, print assessment data
  929:             $r->print('<h4>'.
  930:                       &mt('Overall Assessment Statistical Data').
  931:                       '</h4>'.
  932:                       '<table cellspacing="2" border="0">');
  933:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
  934:                       '<td bgcolor="#CCCCCC">'.
  935:                       &prettyprint('stdno',$dynmeta{'stdno'}).
  936:                       '</td>'."</tr>\n");
  937:             foreach ('avetries','difficulty','disc') {
  938:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  939:                           '<td bgcolor="#CCCCCC">'.
  940:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
  941:                           '</td>'."</tr>\n");
  942:             }
  943:             $r->print('</table>');    
  944:         }
  945:         if (exists($dynmeta{'stats'})) {
  946:             #
  947:             # New assessment statistics
  948:             $r->print('<h4>'.
  949:                       &mt('Detailed Assessment Statistical Data').
  950:                       '</h4>');
  951:             my $table = '<table cellspacing="2" border="0">'.
  952:                 '<tr>'.
  953:                 '<th>Course</th>'.
  954:                 '<th>Section(s)</th>'.
  955:                 '<th>Num Students</th>'.
  956:                 '<th>Mean Tries</th>'.
  957:                 '<th>Degree of Difficulty</th>'.
  958:                 '<th>Degree of Discrimination</th>'.
  959:                 '<th>Time of computation</th>'.
  960:                 '</tr>'.$/;
  961:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
  962:                 my $data = $dynmeta{'stats'}->{$identifier};
  963:                 my $course = $data->{'course'};
  964:                 my %courseinfo = 
  965: 		    &Apache::lonnet::coursedescription($course,
  966: 						       {'one_time' => 1});
  967:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  968:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
  969:                     next;
  970:                 }
  971:                 $table .= '<tr>';
  972:                 $table .= 
  973:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
  974:                 $table .= 
  975:                     '<td align="right">'.$data->{'sections'}.'</td>';
  976:                 $table .=
  977:                     '<td align="right">'.$data->{'stdno'}.'</td>';
  978:                 foreach ('avetries','difficulty','disc') {
  979:                     $table .= '<td align="right">';
  980:                     if (exists($data->{$_})) {
  981:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
  982:                     } else {
  983:                         $table .= '';
  984:                     }
  985:                     $table .= '</td>';
  986:                 }
  987:                 $table .=
  988:                     '<td><nobr>'.
  989:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
  990:                     '</nobr></td>';
  991:                 $table .=
  992:                     '</tr>'.$/;
  993:             }
  994:             $table .= '</table>'.$/;
  995:             $r->print($table);
  996:         } else {
  997:             $r->print('No new dynamic data found.');
  998:         }
  999:     } else {
 1000:         $r->print('<h4>'.
 1001:           &mt('No Assessment Statistical Data is available for this resource').
 1002:                   '</h4>');
 1003:     }
 1004: 
 1005:     #
 1006:     #
 1007:     if (exists($dynmeta{'clear'})   || 
 1008:         exists($dynmeta{'depth'})   || 
 1009:         exists($dynmeta{'helpful'}) || 
 1010:         exists($dynmeta{'correct'}) || 
 1011:         exists($dynmeta{'technical'})){ 
 1012:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
 1013:                   '<table cellspacing="2" border="0">');
 1014:         foreach ('clear','depth','helpful','correct','technical') {
 1015:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
 1016:                       '<td bgcolor="#CCCCCC">'.
 1017:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
 1018:         }
 1019:         $r->print('</table>');
 1020:     } else {
 1021:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
 1022:     }
 1023:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
 1024:     if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
 1025:         || ($env{'user.role.ca./'.$1.'/'.$2})) {
 1026:         if (exists($dynmeta{'comments'})) {
 1027:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
 1028:                       &mt('visible to author and co-authors only').
 1029:                       ')</h4>'.
 1030:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
 1031:         } else {
 1032:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
 1033:         }
 1034:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
 1035:         if (defined($bombs) && $bombs ne '') {
 1036:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
 1037:                       &mt('visible to author and co-authors only').')'.
 1038:                       '</h4>'.$bombs);
 1039:         } else {
 1040:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
 1041:         }
 1042:     }
 1043:     #
 1044:     # All other stuff
 1045:     $r->print('<h3>'.
 1046:               &mt('Additional Metadata (non-standard, parameters, exports)').
 1047:               '</h3><table border="0" cellspacing="1">');
 1048:     foreach (sort(keys(%content))) {
 1049:         my $name=$_;
 1050:         if ($name!~/\.display$/) {
 1051:             my $display=&Apache::lonnet::metadata($uri,
 1052:                                                   $name.'.display');
 1053:             if (! $display) { 
 1054:                 $display=$name;
 1055:             };
 1056:             my $otherinfo='';
 1057:             foreach ('name','part','type','default') {
 1058:                 if (defined(&Apache::lonnet::metadata($uri,
 1059:                                                       $name.'.'.$_))) {
 1060:                     $otherinfo.=' '.$_.'='.
 1061:                         &Apache::lonnet::metadata($uri,
 1062:                                                   $name.'.'.$_).'; ';
 1063:                 }
 1064:             }
 1065:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
 1066:             if ($otherinfo) {
 1067:                 $r->print(' ('.$otherinfo.')');
 1068:             }
 1069:             $r->print("</font></td></tr>\n");
 1070:         }
 1071:     }
 1072:     $r->print("</table>");
 1073:     return;
 1074: }
 1075: 
 1076: 
 1077: 
 1078: #####################################################
 1079: #####################################################
 1080: ###                                               ###
 1081: ###          Editable metadata display            ###
 1082: ###                                               ###
 1083: #####################################################
 1084: #####################################################
 1085: sub present_editable_metadata {
 1086:     my ($r,$uri, $file_type) = @_;
 1087:     # Construction Space Call
 1088:     # Header
 1089:     my $disuri=$uri;
 1090:     my $fn=&Apache::lonnet::filelocation('',$uri);
 1091:     $disuri=~s{^/\~}{/priv/};
 1092:     $disuri=~s/\.meta$//;
 1093:     my $meta_uri = $disuri;
 1094:     my $path;
 1095:     if ($disuri =~ m|/portfolio/|) {
 1096: 	($disuri, $meta_uri, $path) =  &portfolio_display_uri($disuri,1);
 1097:     }
 1098:     my $target=$uri;
 1099:     $target=~s{^/\~}{/res/$env{'request.role.domain'}/};
 1100:     $target=~s/\.meta$//;
 1101:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
 1102:     if ($bombs) {
 1103:         my $showdel=1;
 1104:         if ($env{'form.delmsg'}) {
 1105:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
 1106:                 $bombs=&mt('Messages deleted.');
 1107: 		$showdel=0;
 1108:             } else {
 1109:                 $bombs=&mt('Error deleting messages');
 1110:             }
 1111:         }
 1112:         if ($env{'form.clearmsg'}) {
 1113: 	    my $cleardir=$target;
 1114: 	    $cleardir=~s/\/[^\/]+$/\//;
 1115:             if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
 1116:                 $bombs=&mt('Messages cleared.');
 1117: 		$showdel=0;
 1118:             } else {
 1119:                 $bombs=&mt('Error clearing messages');
 1120:             }
 1121:         }
 1122:         my $del=&mt('Delete Messages for this Resource');
 1123: 	my $clear=&mt('Clear all Messages in Subdirectory');
 1124: 	my $goback=&mt('Back to Source File');
 1125:         $r->print(<<ENDBOMBS);
 1126: <h1>$disuri</h1>
 1127: <form method="post" name="defaultmeta">
 1128: ENDBOMBS
 1129:         if ($showdel) {
 1130: 	    $r->print(<<ENDDEL);
 1131: <input type="submit" name="delmsg" value="$del" />
 1132: <input type="submit" name="clearmsg" value="$clear" />
 1133: ENDDEL
 1134:         } else {
 1135:             $r->print('<a href="'.$disuri.'" />'.$goback.'</a>');
 1136: 	}
 1137: 	$r->print('<br />'.$bombs);
 1138:     } else {
 1139:         my $displayfile='Catalog Information for '.$disuri;
 1140:         if ($disuri=~/\/default$/) {
 1141:             my $dir=$disuri;
 1142:             $dir=~s/default$//;
 1143:             $displayfile=
 1144:                 &mt('Default Cataloging Information for Directory').' '.
 1145:                 $dir;
 1146:         }
 1147:         %Apache::lonpublisher::metadatafields=();
 1148:         %Apache::lonpublisher::metadatakeys=();
 1149:         my $result=&Apache::lonnet::getfile($fn);
 1150:         if ($result == -1){
 1151: 	    $r->print(&mt('Creating new file [_1]'),$meta_uri);
 1152:         } else {
 1153:             &Apache::lonpublisher::metaeval($result);
 1154:         }
 1155:         $r->print(<<ENDEDIT);
 1156: <h1>$displayfile</h1>
 1157: <form method="post" name="defaultmeta">
 1158: ENDEDIT
 1159:         $r->print('<script language="JavaScript">'.
 1160:                   &Apache::loncommon::browser_and_searcher_javascript().
 1161:                   '</script>');
 1162:         my %lt=&fieldnames($file_type);
 1163: 	my $output;
 1164: 	my @fields;
 1165: 	if ($file_type eq 'portfolio') {
 1166: 	    @fields =  ('author','title','subject','keywords','abstract','notes','lowestgradelevel',
 1167: 	                'highestgradelevel','standards');
 1168: 	} else {
 1169: 	    @fields = ('author','title','subject','keywords','abstract','notes',
 1170:                  'copyright','customdistributionfile','language',
 1171:                  'standards',
 1172:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
 1173:                  'obsolete','obsoletereplacement');
 1174:         }
 1175:         if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
 1176:                 (! $env{'form.new_courserestricted'})) {
 1177:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1178:                 'none';
 1179:         } elsif ($env{'form.new_courserestricted'}) {
 1180:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1181:                 $env{'form.new_courserestricted'}; 
 1182:         }           
 1183:         if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
 1184:                 $Apache::lonpublisher::metadatafields{'copyright'}=
 1185:                 'default';
 1186:         }
 1187: 	if ($file_type eq 'portfolio') {
 1188: 	    if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
 1189: 		$r->print(&mt('Associated with course [_1]','<strong>'.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}.
 1190: 			      '</strong>').'<br />');
 1191: 	    } else {
 1192: 		$r->print("This resource is not associated with a course.<br />");
 1193: 	    }
 1194: 	}
 1195:         foreach my $field_name (@fields) {
 1196: 
 1197:             if (defined($env{'form.new_'.$field_name})) {
 1198:                 $Apache::lonpublisher::metadatafields{$field_name}=
 1199:                     join(',',&Apache::loncommon::get_env_multiple('form.new_'.$field_name));
 1200:             }
 1201:             if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
 1202: 		&& exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
 1203:                 # handle restrictions here
 1204:                 if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
 1205:                     ($field_name eq 'courserestricted')){
 1206:                     $output.=("\n".'<p>'.$lt{$field_name}.': '.
 1207:                               &prettyinput($field_name,
 1208: 				   $Apache::lonpublisher::metadatafields{$field_name},
 1209: 				                    'new_'.$field_name,'defaultmeta',
 1210: 				                    undef,undef,undef,undef,
 1211: 				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</p>'."\n");
 1212:                  }
 1213:             } else {
 1214: 
 1215:                     $output.=('<p>'.$lt{$field_name}.': '.
 1216:                             &prettyinput($field_name,
 1217: 				   $Apache::lonpublisher::metadatafields{$field_name},
 1218: 				   'new_'.$field_name,'defaultmeta').'</p>');
 1219:                
 1220:             }
 1221:         }
 1222: 	if ($env{'form.store'}) {
 1223: 	    my $mfh;
 1224: 	    my $formname='store'; 
 1225: 	    my $file_content;
 1226: 	    if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
 1227: 		$Apache::lonpublisher::metadatafields{'keywords'} = 
 1228: 		    join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
 1229: 	    }
 1230: 
 1231: 	    foreach (sort keys %Apache::lonpublisher::metadatafields) {
 1232: 		next if ($_ =~ /\./);
 1233: 		my $unikey=$_;
 1234: 		$unikey=~/^([A-Za-z]+)/;
 1235: 		my $tag=$1;
 1236: 		$tag=~tr/A-Z/a-z/;
 1237: 		$file_content.= "\n\<$tag";
 1238: 		foreach (split(/\,/,
 1239: 			       $Apache::lonpublisher::metadatakeys{$unikey})
 1240: 			 ) {
 1241: 		    my $value=
 1242: 			$Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
 1243: 		    $value=~s/\"/\'\'/g;
 1244: 		    $file_content.=' '.$_.'="'.$value.'"' ;
 1245: 		    # print $mfh ' '.$_.'="'.$value.'"';
 1246: 		}
 1247: 		$file_content.= '>'.
 1248: 		    &HTML::Entities::encode
 1249: 		    ($Apache::lonpublisher::metadatafields{$unikey},
 1250: 		     '<>&"').
 1251: 		     '</'.$tag.'>';
 1252: 	    }
 1253: 	    if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles/portfolio/|) {
 1254: 		my ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
 1255:                 $r->print(&store_portfolio_metadata($formname,$file_content,$path,
 1256:                                                     $new_fn));    
 1257:             } elsif ($fn =~  m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles/groups/\w+/portfolio/|) {
 1258:                 my ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
 1259:                 $r->print(&store_portfolio_metadata($formname,$file_content,$path,$new_fn));
 1260: 	    } else {
 1261: 		if (!  ($mfh=Apache::File->new('>'.$fn))) {
 1262: 		    $r->print('<p><font color="red">'.
 1263: 			      &mt('Could not write metadata').', '.
 1264: 			      &mt('FAIL').'</font></p>');
 1265: 		} else {
 1266: 		    print $mfh $file_content;
 1267: 		    $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
 1268: 			      ' '.&Apache::lonlocal::locallocaltime(time).
 1269: 			      '</font></p>');
 1270: 		}
 1271: 	    }
 1272: 	}
 1273: 	
 1274: 	$r->print($output.'<br /><input type="submit" name="store" value="'.
 1275:                   &mt('Store Catalog Information').'">');
 1276: 
 1277: 	if ($file_type eq 'portfolio') {
 1278: 	    my ($port_path,$group) = &get_port_path_and_group($uri);
 1279:             if ($group) {
 1280:                 $r->print('<input type="hidden" name="group" value="'.$group.'" />');
 1281:             }
 1282: 	    $r->print('</form>
 1283:                <br /><br /><form method="POST" action="'.$port_path.'">'.
 1284: 		      '<input type="hidden" name="group" value="'.$group.'" />'.
 1285: 		      '<input type="hidden" name="currentpath" value="'.$path.'" />'.
 1286: 		      '<input type="submit" name="cancel" value="'.&mt('Discard Edits and Return to Portfolio').'">');
 1287: 	}
 1288:     }
 1289:     
 1290:     $r->print('</form>');
 1291: 
 1292:     return;
 1293: }
 1294: 
 1295: sub store_portfolio_metadata {
 1296:     my ($formname,$content,$path,$new_fn) = @_;
 1297:     $env{'form.'.$formname}=$content."\n";
 1298:     $env{'form.'.$formname.'.filename'}=$new_fn;
 1299:     my $result =&Apache::lonnet::userfileupload($formname,'',$path);
 1300:     if ($result =~ /(error|notfound)/) {
 1301:         return '<p><font color="red">'.
 1302:                   &mt('Could not write metadata').', '.
 1303:                   &mt('FAIL').'</font></p>';
 1304:     } else {
 1305:         return '<p><font color="blue">'.&mt('Wrote Metadata').
 1306:                   ' '.&Apache::lonlocal::locallocaltime(time).'</font></p>';
 1307:     }
 1308: }
 1309: 
 1310: 1;
 1311: __END__
 1312: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>