File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.150: download - view: text, annotated - select for diffs
Wed Jan 4 05:43:19 2006 UTC (18 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- allowing a resource to be associated with any course

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

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