File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.140: download - view: text, annotated - select for diffs
Mon Dec 19 16:00:05 2005 UTC (18 years, 5 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Make metadata for portfolio files a two-step process.
	1. select framework
	2. display/edit metadata
	Still needs label changes, testing, etc.

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.140 2005/12/19 16:00:05 banghart 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:          'courserestricted' => 'Course Restricting Metadata');
  253:          
  254:     if (! defined($file_type) || $file_type ne 'portfolio') {
  255:         %fields = 
  256:         (%fields,
  257:          'domain' => 'Domain',
  258:          'mime' => 'MIME Type',
  259:          'language' => 'Language',
  260:          'creationdate' => 'Creation Date',
  261:          'lastrevisiondate' => 'Last Revision Date',
  262:          'owner' => 'Publisher/Owner',
  263:          'copyright' => 'Copyright/Distribution',
  264:          'customdistributionfile' => 'Custom Distribution File',
  265:          'sourceavail' => 'Source Available',
  266:          'sourcerights' => 'Source Custom Distribution File',
  267:          'obsolete' => 'Obsolete',
  268:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
  269:          'count'      => 'Network-wide number of accesses (hits)',
  270:          'course'     => 'Network-wide number of courses using resource',
  271:          'course_list' => 'Network-wide courses using resource',
  272:          'sequsage'      => 'Number of resources using or importing resource',
  273:          'sequsage_list' => 'Resources using or importing resource',
  274:          'goto'       => 'Number of resources that follow this resource in maps',
  275:          'goto_list'  => 'Resources that follow this resource in maps',
  276:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
  277:          'comefrom_list' => 'Resources that lead up to this resource in maps',
  278:          'clear'      => 'Material presented in clear way',
  279:          'depth'      => 'Material covered with sufficient depth',
  280:          'helpful'    => 'Material is helpful',
  281:          'correct'    => 'Material appears to be correct',
  282:          'technical'  => 'Resource is technically correct', 
  283:          'avetries'   => 'Average number of tries till solved',
  284:          'stdno'      => 'Total number of students who have worked on this problem',
  285:          'difficulty' => 'Degree of difficulty',
  286:          'disc'       => 'Degree of discrimination',
  287: 	     'dependencies' => 'Resources used by this resource',
  288:          );
  289:     }
  290:     return &Apache::lonlocal::texthash(%fields);
  291: }
  292: sub pre_select_course {
  293:     my ($r,$uri) = @_;
  294:     my $output;
  295:     my $fn=&Apache::lonnet::filelocation('',$uri);
  296:     my $disuri=$uri;
  297:     %Apache::lonpublisher::metadatafields=();
  298:     %Apache::lonpublisher::metadatakeys=();
  299:     my $result=&Apache::lonnet::getfile($fn);
  300:     if ($result == -1){
  301:         $r->print('Creating new '.$disuri);
  302:     } else {
  303:         &Apache::lonpublisher::metaeval($result);
  304:     }
  305:     $r->print('<form method="post" action="" >');
  306:     $output = &select_course();
  307:     $r->print($output.'<br /><input type="submit" name="store" value="'.
  308:                   &mt('Select Metadata Course Restrictions').'">');
  309:     $r->print('</form>');
  310:     return;
  311: }
  312: sub select_course {
  313:     my %courses;
  314:     my $output;
  315:     my $selected;
  316:     foreach my $key (keys (%env)) { 
  317:         if ($key =~ m/\.metadata\./) {
  318:             $key =~ m/^course\.(.+)(\.metadata.+$)/;
  319:             my $course = $1;
  320:             my $coursekey = 'course.'.$course.'.description';
  321:             my $value = $env{$coursekey};
  322:             $courses{$coursekey} = $value;
  323:         }
  324:     }
  325:     &Apache::lonnet::logthis('the restricted is'.$Apache::lonpublisher::metadatafields{'courserestricted'});
  326:     my $meta_not_found = 1;
  327:     if ($Apache::lonpublisher::metadatafields{'courserestricted'} eq 'none') {
  328:         $selected = ' SELECTED ';
  329:     } else {
  330:         $selected = '';
  331:     }
  332:     $output .= '<select name="new_courserestricted" >';
  333:     $output .= '<option value="none" '.$selected.'>None</option>';
  334:     foreach my $key (keys (%courses)) {    
  335:         $key =~ m/(^.+)\.description$/;
  336:         if ($Apache::lonpublisher::metadatafields{'courserestricted'} eq $1) {
  337:             $selected = ' SELECTED ';
  338:         } else {
  339:             $selected = '';
  340:         }
  341:         $output .= '<option value="'.$1.'"'.$selected.'>';
  342:         $output .= $courses{$key};
  343:         $output .= '</option>';
  344:     }
  345:     $output .= '</select><br />';
  346:     return ($output);
  347: }
  348: # Pretty printing of metadata field
  349: 
  350: sub prettyprint {
  351:     my ($type,$value,$target,$prefix,$form,$noformat)=@_;
  352: # $target,$prefix,$form are optional and for filecrumbs only
  353:     if (! defined($value)) { 
  354:         return '&nbsp;'; 
  355:     }
  356:     # Title
  357:     if ($type eq 'title') {
  358: 	return '<font size="+1" face="arial">'.$value.'</font>';
  359:     }
  360:     # Dates
  361:     if (($type eq 'creationdate') ||
  362: 	($type eq 'lastrevisiondate')) {
  363: 	return ($value?&Apache::lonlocal::locallocaltime(
  364: 			  &Apache::lonmysql::unsqltime($value)):
  365: 		&mt('not available'));
  366:     }
  367:     # Language
  368:     if ($type eq 'language') {
  369: 	return &Apache::loncommon::languagedescription($value);
  370:     }
  371:     # Copyright
  372:     if ($type eq 'copyright') {
  373: 	return &Apache::loncommon::copyrightdescription($value);
  374:     }
  375:     # Copyright
  376:     if ($type eq 'sourceavail') {
  377: 	return &Apache::loncommon::source_copyrightdescription($value);
  378:     }
  379:     # MIME
  380:     if ($type eq 'mime') {
  381:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
  382:             &Apache::loncommon::filedescription($value);
  383:     }
  384:     # Person
  385:     if (($type eq 'author') || 
  386: 	($type eq 'owner') ||
  387: 	($type eq 'modifyinguser') ||
  388: 	($type eq 'authorspace')) {
  389: 	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  390: 	return $value;
  391:     }
  392:     # Gradelevel
  393:     if (($type eq 'lowestgradelevel') ||
  394: 	($type eq 'highestgradelevel')) {
  395: 	return &Apache::loncommon::gradeleveldescription($value);
  396:     }
  397:     # Only for advance users below
  398:     if (! $env{'user.adv'}) { 
  399:         return '<i>- '.&mt('not displayed').' -</i>';
  400:     }
  401:     # File
  402:     if (($type eq 'customdistributionfile') ||
  403: 	($type eq 'obsoletereplacement') ||
  404: 	($type eq 'goto_list') ||
  405: 	($type eq 'comefrom_list') ||
  406: 	($type eq 'sequsage_list') ||
  407: 	($type eq 'dependencies')) {
  408: 	return '<ul><font size="-1">'.join("\n",map {
  409:             my $url = &Apache::lonnet::clutter($_);
  410:             my $title = &Apache::lonnet::gettitle($url);
  411:             if ($title eq '') {
  412:                 $title = 'Untitled';
  413:                 if ($url =~ /\.sequence$/) {
  414:                     $title .= ' Sequence';
  415:                 } elsif ($url =~ /\.page$/) {
  416:                     $title .= ' Page';
  417:                 } elsif ($url =~ /\.problem$/) {
  418:                     $title .= ' Problem';
  419:                 } elsif ($url =~ /\.html$/) {
  420:                     $title .= ' HTML document';
  421:                 } elsif ($url =~ m:/syllabus$:) {
  422:                     $title .= ' Syllabus';
  423:                 } 
  424:             }
  425:             $_ = '<li>'.$title.' '.
  426: 		&Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
  427:                 '</li>'
  428: 	    } split(/\s*\,\s*/,$value)).'</ul></font>';
  429:     }
  430:     # Evaluations
  431:     if (($type eq 'clear') ||
  432: 	($type eq 'depth') ||
  433: 	($type eq 'helpful') ||
  434: 	($type eq 'correct') ||
  435: 	($type eq 'technical')) {
  436: 	return &evalgraph($value);
  437:     }
  438:     # Difficulty
  439:     if ($type eq 'difficulty' || $type eq 'disc') {
  440: 	return &diffgraph($value);
  441:     }
  442:     # List of courses
  443:     if ($type=~/\_list/) {
  444:         my @Courses = split(/\s*\,\s*/,$value);
  445:         my $Str;
  446:         foreach my $course (@Courses) {
  447:             my %courseinfo = &Apache::lonnet::coursedescription($course);
  448:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  449:                 next;
  450:             }
  451:             if ($Str ne '') { $Str .= '<br />'; }
  452:             $Str .= '<a href="/public/'.$courseinfo{'domain'}.'/'.
  453:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
  454:                 $courseinfo{'description'}.'</a>';
  455:         }
  456: 	return $Str;
  457:     }
  458:     # No pretty print found
  459:     return $value;
  460: }
  461: 
  462: # Pretty input of metadata field
  463: sub direct {
  464:     return shift;
  465: }
  466: 
  467: sub selectbox {
  468:     my ($name,$value,$functionref,@idlist)=@_;
  469:     if (! defined($functionref)) {
  470:         $functionref=\&direct;
  471:     }
  472:     my $selout='<select name="'.$name.'">';
  473:     foreach (@idlist) {
  474:         $selout.='<option value=\''.$_.'\'';
  475:         if ($_ eq $value) {
  476: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  477: 	}
  478:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  479:     }
  480:     return $selout.'</select>';
  481: }
  482: 
  483: sub relatedfield {
  484:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
  485:     if (! $relatedsearchflag) { 
  486:         return '';
  487:     }
  488:     if (! defined($relatedsep)) {
  489:         $relatedsep=' ';
  490:     }
  491:     if (! $show) {
  492:         return $relatedsep.'&nbsp;';
  493:     }
  494:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
  495: 	($relatedvalue?' checked="1"':'').' />';
  496: }
  497: 
  498: sub prettyinput {
  499:     my ($type,$value,$fieldname,$formname,
  500: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
  501:     if (! defined($size)) {
  502:         $size = 80;
  503:     }
  504:     my $output;
  505:     if (defined($course_key)) {
  506:         my $stu_add;
  507:         my $only_one;
  508:         my %meta_options;
  509:         my @cur_values_inst;
  510:         my $cur_values_stu;
  511:         my $values = $env{$course_key.'.metadata.'.$type.'.values'};
  512:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
  513:             $stu_add = 'true';
  514:         }
  515:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
  516:             $only_one = 'true';
  517:         }
  518:         # need to take instructor values out of list where instructor and student
  519:         # values may be mixed.
  520:         if ($values) {
  521:             foreach my $item (split(/,/,$values)) {
  522:                 $item =~ s/^\s+//;
  523:                 $meta_options{$item} = $item;
  524:             }
  525:             foreach my $item (split(/,/,$value)) {
  526:                 $item =~ s/^\s+//;
  527:                 if ($meta_options{$item}) {
  528:                     push(@cur_values_inst,$item);
  529:                 } else {
  530:                     $cur_values_stu .= $item.',';
  531:                 }
  532:             }
  533:         } else {
  534:             $cur_values_stu = $value;
  535:         }
  536:         if ($type eq 'courserestricted') {
  537:             return (&select_course());
  538:             # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  539:         }
  540:         if (($type eq 'keywords') || ($type eq 'subject')
  541:              || ($type eq 'author')||($type eq  'notes')
  542:              || ($type eq  'abstract')|| ($type eq  'title')|| ($type eq  'standards')) {
  543:             if ($values) {
  544:                 if ($only_one) {
  545:                     $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
  546:                 } else {
  547:                     $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
  548:                 }
  549:             }
  550:             if ($stu_add) {
  551:                 $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  552:                 'value="'.$cur_values_stu.'" />'.
  553:                 &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  554:                       $relatedvalue); 
  555:             }
  556:             return ($output);
  557:         }
  558:         if (($type eq 'lowestgradelevel') ||
  559: 	    ($type eq 'highestgradelevel')) {
  560: 	    return &Apache::loncommon::select_level_form($value,$fieldname).
  561:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  562:         }
  563:         return(); 
  564:     }
  565:     # Language
  566:     if ($type eq 'language') {
  567: 	return &selectbox($fieldname,
  568: 			  $value,
  569: 			  \&Apache::loncommon::languagedescription,
  570: 			  (&Apache::loncommon::languageids)).
  571:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  572:     }
  573:     # Copyright
  574:     if ($type eq 'copyright') {
  575: 	return &selectbox($fieldname,
  576: 			  $value,
  577: 			  \&Apache::loncommon::copyrightdescription,
  578: 			  (&Apache::loncommon::copyrightids)).
  579:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  580:     }
  581:     # Source Copyright
  582:     if ($type eq 'sourceavail') {
  583: 	return &selectbox($fieldname,
  584: 			  $value,
  585: 			  \&Apache::loncommon::source_copyrightdescription,
  586: 			  (&Apache::loncommon::source_copyrightids)).
  587:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  588:     }
  589:     # Gradelevels
  590:     if (($type eq 'lowestgradelevel') ||
  591: 	($type eq 'highestgradelevel')) {
  592: 	return &Apache::loncommon::select_level_form($value,$fieldname).
  593:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  594:     }
  595:     # Obsolete
  596:     if ($type eq 'obsolete') {
  597: 	return '<input type="checkbox" name="'.$fieldname.'"'.
  598: 	    ($value?' checked="1"':'').' />'.
  599:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  600:     }
  601:     # Obsolete replacement file
  602:     if ($type eq 'obsoletereplacement') {
  603: 	return '<input type="text" name="'.$fieldname.
  604: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  605: 	    "('".$formname."','".$fieldname."'".
  606: 	    ",'')\">".&mt('Select').'</a>'.
  607:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  608:     }
  609:     # Customdistribution file
  610:     if ($type eq 'customdistributionfile') {
  611: 	return '<input type="text" name="'.$fieldname.
  612: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  613: 	    "('".$formname."','".$fieldname."'".
  614: 	    ",'rights')\">".&mt('Select').'</a>'.
  615:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  616:     }
  617:     # Source Customdistribution file
  618:     if ($type eq 'sourcerights') {
  619: 	return '<input type="text" name="'.$fieldname.
  620: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  621: 	    "('".$formname."','".$fieldname."'".
  622: 	    ",'rights')\">".&mt('Select').'</a>'.
  623:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  624:     }
  625:     if ($type eq 'courserestricted') {
  626:         return (&select_course());
  627:         #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  628:     }
  629: 
  630:     # Dates
  631:     if (($type eq 'creationdate') ||
  632: 	($type eq 'lastrevisiondate')) {
  633: 	return 
  634:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
  635:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  636:     }
  637:     # No pretty input found
  638:     $value=~s/^\s+//gs;
  639:     $value=~s/\s+$//gs;
  640:     $value=~s/\s+/ /gs;
  641:     $value=~s/\"/\&quot\;/gs;
  642:     return 
  643:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  644:         'value="'.$value.'" />'.
  645:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  646:                       $relatedvalue); 
  647: }
  648: 
  649: # Main Handler
  650: sub handler {
  651:     my $r=shift;
  652:     #
  653:     my $uri=$r->uri;
  654:     #
  655:     # Set document type
  656:     &Apache::loncommon::content_type($r,'text/html');
  657:     $r->send_http_header;
  658:     return OK if $r->header_only;
  659:     #
  660:     my ($resdomain,$resuser)=
  661:         (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
  662:     my $html=&Apache::lonxml::xmlbegin();
  663:     $r->print($html.'<head><title>'.
  664:               'Catalog Information'.
  665:               '</title></head>');
  666:     if ($uri=~m:/adm/bombs/(.*)$:) {
  667:         $r->print(&Apache::loncommon::bodytag('Error Messages'));
  668:         # Looking for all bombs?
  669:         &report_bombs($r,$uri);
  670:     } elsif ($uri=~/\/portfolio\//) {
  671: 	    ($resdomain,$resuser)=
  672: 	    (&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|);
  673:         $r->print(&Apache::loncommon::bodytag
  674:           ('Edit Portfolio File Information','','','',$resdomain));
  675:         if ($env{'form.store'}) {
  676:             &present_editable_metadata($r,$uri,'portfolio');
  677:         } else {
  678:             &pre_select_course($r,$uri);
  679:         }
  680:     } elsif ($uri=~/^\/\~/) { 
  681:         # Construction space
  682:         $r->print(&Apache::loncommon::bodytag
  683:                   ('Edit Catalog Information','','','',$resdomain));
  684:         &present_editable_metadata($r,$uri);
  685:     } else {
  686:         $r->print(&Apache::loncommon::bodytag
  687: 		  ('Catalog Information','','','',$resdomain));
  688:         &present_uneditable_metadata($r,$uri);
  689:     }
  690:     $r->print('</body></html>');
  691:     return OK;
  692: }
  693: 
  694: #####################################################
  695: #####################################################
  696: ###                                               ###
  697: ###                Report Bombs                   ###
  698: ###                                               ###
  699: #####################################################
  700: #####################################################
  701: sub report_bombs {
  702:     my ($r,$uri) = @_;
  703:     # Set document type
  704:     $uri =~ s:/adm/bombs/::;
  705:     $uri = &Apache::lonnet::declutter($uri);
  706:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  707:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
  708:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  709: 	if ($env{'form.clearbombs'}) {
  710: 	    &Apache::lonmsg::clear_author_res_msg($uri);
  711: 	}
  712:         my $clear=&mt('Clear all Messages in Subdirectory');
  713: 	$r->print(<<ENDCLEAR);
  714: <form method="post">
  715: <input type="submit" name="clearbombs" value="$clear" />
  716: </form>
  717: ENDCLEAR
  718:         my %brokenurls = 
  719:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  720:         foreach (sort(keys(%brokenurls))) {
  721:             if ($_=~/^\Q$uri\E/) {
  722:                 $r->print
  723:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  724:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  725:                      '<hr />');
  726:             }
  727:         }
  728:     } else {
  729:         $r->print(&mt('Not authorized'));
  730:     }
  731:     return;
  732: }
  733: 
  734: #####################################################
  735: #####################################################
  736: ###                                               ###
  737: ###        Uneditable Metadata Display            ###
  738: ###                                               ###
  739: #####################################################
  740: #####################################################
  741: sub present_uneditable_metadata {
  742:     my ($r,$uri) = @_;
  743:     #
  744:     my %content=();
  745:     # Read file
  746:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  747:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  748:     }
  749:     # Render Output
  750:     # displayed url
  751:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  752:     $uri=~s/\.meta$//;
  753:     my $disuri=&Apache::lonnet::clutter($uri);
  754:     # version
  755:     my $currentversion=&Apache::lonnet::getversion($disuri);
  756:     my $versiondisplay='';
  757:     if ($thisversion) {
  758:         $versiondisplay=&mt('Version').': '.$thisversion.
  759:             ' ('.&mt('most recent version').': '.
  760:             ($currentversion>0 ? 
  761:              $currentversion   :
  762:              &mt('information not available')).')';
  763:     } else {
  764:         $versiondisplay='Version: '.$currentversion;
  765:     }
  766:     # crumbify displayed URL               uri     target prefix form  size
  767:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  768:     $disuri =~ s:<br />::g;
  769:     # obsolete
  770:     my $obsolete=$content{'obsolete'};
  771:     my $obsoletewarning='';
  772:     if (($obsolete) && ($env{'user.adv'})) {
  773:         $obsoletewarning='<p><font color="red">'.
  774:             &mt('This resource has been marked obsolete by the author(s)').
  775:             '</font></p>';
  776:     }
  777:     #
  778:     my %lt=&fieldnames();
  779:     my $table='';
  780:     my $title = $content{'title'};
  781:     if (! defined($title)) {
  782:         $title = 'Untitled Resource';
  783:     }
  784:     foreach ('title', 
  785:              'author', 
  786:              'subject', 
  787:              'keywords', 
  788:              'notes', 
  789:              'abstract',
  790:              'lowestgradelevel',
  791:              'highestgradelevel',
  792:              'standards', 
  793:              'mime', 
  794:              'language', 
  795:              'creationdate', 
  796:              'lastrevisiondate', 
  797:              'owner', 
  798:              'copyright', 
  799:              'customdistributionfile',
  800:              'sourceavail',
  801:              'sourcerights', 
  802:              'obsolete', 
  803:              'obsoletereplacement') {
  804:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
  805:             '</td><td bgcolor="#CCCCCC">'.
  806:             &prettyprint($_,$content{$_}).'</td></tr>';
  807:         delete $content{$_};
  808:     }
  809:     #
  810:     $r->print(<<ENDHEAD);
  811: <h2>$title</h2>
  812: <p>
  813: $disuri<br />
  814: $obsoletewarning
  815: $versiondisplay
  816: </p>
  817: <table cellspacing="2" border="0">
  818: $table
  819: </table>
  820: ENDHEAD
  821:     if ($env{'user.adv'}) {
  822:         &print_dynamic_metadata($r,$uri,\%content);
  823:     }
  824:     return;
  825: }
  826: 
  827: sub print_dynamic_metadata {
  828:     my ($r,$uri,$content) = @_;
  829:     #
  830:     my %content = %$content;
  831:     my %lt=&fieldnames();
  832:     #
  833:     my $description = 'Dynamic Metadata (updated periodically)';
  834:     $r->print('<h3>'.&mt($description).'</h3>'.
  835:               &mt('Processing'));
  836:     $r->rflush();
  837:     my %items=&fieldnames();
  838:     my %dynmeta=&dynamicmeta($uri);
  839:     #
  840:     # General Access and Usage Statistics
  841:     if (exists($dynmeta{'count'}) ||
  842:         exists($dynmeta{'sequsage'}) ||
  843:         exists($dynmeta{'comefrom'}) ||
  844:         exists($dynmeta{'goto'}) ||
  845:         exists($dynmeta{'course'})) {
  846:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  847:                   '<table cellspacing="2" border="0">');
  848:         foreach ('count',
  849:                  'sequsage','sequsage_list',
  850:                  'comefrom','comefrom_list',
  851:                  'goto','goto_list',
  852:                  'course','course_list') {
  853:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  854:                       '<td bgcolor="#CCCCCC">'.
  855:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  856:         }
  857:         $r->print('</table>');
  858:     } else {
  859:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
  860:     }
  861:     #
  862:     # Assessment statistics
  863:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  864:         if (exists($dynmeta{'stdno'}) ||
  865:             exists($dynmeta{'avetries'}) ||
  866:             exists($dynmeta{'difficulty'}) ||
  867:             exists($dynmeta{'disc'})) {
  868:             # This is an assessment, print assessment data
  869:             $r->print('<h4>'.
  870:                       &mt('Overall Assessment Statistical Data').
  871:                       '</h4>'.
  872:                       '<table cellspacing="2" border="0">');
  873:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
  874:                       '<td bgcolor="#CCCCCC">'.
  875:                       &prettyprint('stdno',$dynmeta{'stdno'}).
  876:                       '</td>'."</tr>\n");
  877:             foreach ('avetries','difficulty','disc') {
  878:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  879:                           '<td bgcolor="#CCCCCC">'.
  880:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
  881:                           '</td>'."</tr>\n");
  882:             }
  883:             $r->print('</table>');    
  884:         }
  885:         if (exists($dynmeta{'stats'})) {
  886:             #
  887:             # New assessment statistics
  888:             $r->print('<h4>'.
  889:                       &mt('Detailed Assessment Statistical Data').
  890:                       '</h4>');
  891:             my $table = '<table cellspacing="2" border="0">'.
  892:                 '<tr>'.
  893:                 '<th>Course</th>'.
  894:                 '<th>Section(s)</th>'.
  895:                 '<th>Num Students</th>'.
  896:                 '<th>Mean Tries</th>'.
  897:                 '<th>Degree of Difficulty</th>'.
  898:                 '<th>Degree of Discrimination</th>'.
  899:                 '<th>Time of computation</th>'.
  900:                 '</tr>'.$/;
  901:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
  902:                 my $data = $dynmeta{'stats'}->{$identifier};
  903:                 my $course = $data->{'course'};
  904:                 my %courseinfo = &Apache::lonnet::coursedescription($course);
  905:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  906:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
  907:                     next;
  908:                 }
  909:                 $table .= '<tr>';
  910:                 $table .= 
  911:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
  912:                 $table .= 
  913:                     '<td align="right">'.$data->{'sections'}.'</td>';
  914:                 $table .=
  915:                     '<td align="right">'.$data->{'stdno'}.'</td>';
  916:                 foreach ('avetries','difficulty','disc') {
  917:                     $table .= '<td align="right">';
  918:                     if (exists($data->{$_})) {
  919:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
  920:                     } else {
  921:                         $table .= '';
  922:                     }
  923:                     $table .= '</td>';
  924:                 }
  925:                 $table .=
  926:                     '<td><nobr>'.
  927:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
  928:                     '</nobr></td>';
  929:                 $table .=
  930:                     '</tr>'.$/;
  931:             }
  932:             $table .= '</table>'.$/;
  933:             $r->print($table);
  934:         } else {
  935:             $r->print('No new dynamic data found.');
  936:         }
  937:     } else {
  938:         $r->print('<h4>'.
  939:           &mt('No Assessment Statistical Data is available for this resource').
  940:                   '</h4>');
  941:     }
  942: 
  943:     #
  944:     #
  945:     if (exists($dynmeta{'clear'})   || 
  946:         exists($dynmeta{'depth'})   || 
  947:         exists($dynmeta{'helpful'}) || 
  948:         exists($dynmeta{'correct'}) || 
  949:         exists($dynmeta{'technical'})){ 
  950:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
  951:                   '<table cellspacing="2" border="0">');
  952:         foreach ('clear','depth','helpful','correct','technical') {
  953:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  954:                       '<td bgcolor="#CCCCCC">'.
  955:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  956:         }
  957:         $r->print('</table>');
  958:     } else {
  959:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
  960:     }
  961:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
  962:     if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
  963:         || ($env{'user.role.ca./'.$1.'/'.$2})) {
  964:         if (exists($dynmeta{'comments'})) {
  965:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
  966:                       &mt('visible to author and co-authors only').
  967:                       ')</h4>'.
  968:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  969:         } else {
  970:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
  971:         }
  972:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
  973:         if (defined($bombs) && $bombs ne '') {
  974:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
  975:                       &mt('visible to author and co-authors only').')'.
  976:                       '</h4>'.$bombs);
  977:         } else {
  978:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
  979:         }
  980:     }
  981:     #
  982:     # All other stuff
  983:     $r->print('<h3>'.
  984:               &mt('Additional Metadata (non-standard, parameters, exports)').
  985:               '</h3><table border="0" cellspacing="1">');
  986:     foreach (sort(keys(%content))) {
  987:         my $name=$_;
  988:         if ($name!~/\.display$/) {
  989:             my $display=&Apache::lonnet::metadata($uri,
  990:                                                   $name.'.display');
  991:             if (! $display) { 
  992:                 $display=$name;
  993:             };
  994:             my $otherinfo='';
  995:             foreach ('name','part','type','default') {
  996:                 if (defined(&Apache::lonnet::metadata($uri,
  997:                                                       $name.'.'.$_))) {
  998:                     $otherinfo.=' '.$_.'='.
  999:                         &Apache::lonnet::metadata($uri,
 1000:                                                   $name.'.'.$_).'; ';
 1001:                 }
 1002:             }
 1003:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
 1004:             if ($otherinfo) {
 1005:                 $r->print(' ('.$otherinfo.')');
 1006:             }
 1007:             $r->print("</font></td></tr>\n");
 1008:         }
 1009:     }
 1010:     $r->print("</table>");
 1011:     return;
 1012: }
 1013: 
 1014: 
 1015: 
 1016: #####################################################
 1017: #####################################################
 1018: ###                                               ###
 1019: ###          Editable metadata display            ###
 1020: ###                                               ###
 1021: #####################################################
 1022: #####################################################
 1023: sub present_editable_metadata {
 1024:     my ($r,$uri, $file_type) = @_;
 1025:     # Construction Space Call
 1026:     # Header
 1027:     my $disuri=$uri;
 1028:     my $fn=&Apache::lonnet::filelocation('',$uri);
 1029:     $disuri=~s/^\/\~/\/priv\//;
 1030:     $disuri=~s/\.meta$//;
 1031:     $disuri=~s|^/editupload||;
 1032:     my $target=$uri;
 1033:     $target=~s/^\/\~/\/res\/$env{'request.role.domain'}\//;
 1034:     $target=~s/\.meta$//;
 1035:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
 1036:     if ($bombs) {
 1037:         my $showdel=1;
 1038:         if ($env{'form.delmsg'}) {
 1039:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
 1040:                 $bombs=&mt('Messages deleted.');
 1041: 		$showdel=0;
 1042:             } else {
 1043:                 $bombs=&mt('Error deleting messages');
 1044:             }
 1045:         }
 1046:         if ($env{'form.clearmsg'}) {
 1047: 	    my $cleardir=$target;
 1048: 	    $cleardir=~s/\/[^\/]+$/\//;
 1049:             if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
 1050:                 $bombs=&mt('Messages cleared.');
 1051: 		$showdel=0;
 1052:             } else {
 1053:                 $bombs=&mt('Error clearing messages');
 1054:             }
 1055:         }
 1056:         my $del=&mt('Delete Messages for this Resource');
 1057: 	my $clear=&mt('Clear all Messages in Subdirectory');
 1058: 	my $goback=&mt('Back to Source File');
 1059:         $r->print(<<ENDBOMBS);
 1060: <h1>$disuri</h1>
 1061: <form method="post" name="defaultmeta">
 1062: ENDBOMBS
 1063:         if ($showdel) {
 1064: 	    $r->print(<<ENDDEL);
 1065: <input type="submit" name="delmsg" value="$del" />
 1066: <input type="submit" name="clearmsg" value="$clear" />
 1067: ENDDEL
 1068:         } else {
 1069:             $r->print('<a href="'.$disuri.'" />'.$goback.'</a>');
 1070: 	}
 1071: 	$r->print('<br />'.$bombs);
 1072:     } else {
 1073:         my $displayfile='Catalog Information for '.$disuri;
 1074:         if ($disuri=~/\/default$/) {
 1075:             my $dir=$disuri;
 1076:             $dir=~s/default$//;
 1077:             $displayfile=
 1078:                 &mt('Default Cataloging Information for Directory').' '.
 1079:                 $dir;
 1080:         }
 1081:         %Apache::lonpublisher::metadatafields=();
 1082:         %Apache::lonpublisher::metadatakeys=();
 1083:         my $result=&Apache::lonnet::getfile($fn);
 1084:         if ($result == -1){
 1085:             $r->print('Creating new '.$disuri);
 1086:         } else {
 1087:             &Apache::lonpublisher::metaeval($result);
 1088:         }
 1089:         $r->print(<<ENDEDIT);
 1090: <h1>$displayfile</h1>
 1091: <form method="post" name="defaultmeta">
 1092: ENDEDIT
 1093:         $r->print('<script language="JavaScript">'.
 1094:                   &Apache::loncommon::browser_and_searcher_javascript().
 1095:                   '</script>');
 1096:         my %lt=&fieldnames($file_type);
 1097: 	my $output;
 1098: 	my @fields;
 1099: 	if ($file_type eq 'portfolio') {
 1100: 	    @fields =  ('author','title','subject','keywords','abstract','notes','lowestgradelevel',
 1101: 	                'highestgradelevel','standards');
 1102: 	} else {
 1103: 	    @fields = ('author','title','subject','keywords','abstract','notes',
 1104:                  'copyright','customdistributionfile','language',
 1105:                  'standards',
 1106:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
 1107:                  'obsolete','obsoletereplacement');
 1108:         }
 1109:         if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
 1110:                 (! $env{'form.new_courserestricted'})) {
 1111:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1112:                 'none';
 1113:         } elsif ($env{'form.new_courserestricted'}) {
 1114:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1115:                 $env{'form.new_courserestricted'}; 
 1116:         }           
 1117:         if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
 1118:                 $Apache::lonpublisher::metadatafields{'copyright'}=
 1119:                 'default';
 1120:         }
 1121:         if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
 1122:             $r->print('Using: <strong> '.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}.
 1123:                         "</strong> metadata framework<br />");
 1124:         } else {
 1125:             $r->print("This resource is not associated with a metadata framework<br />");
 1126:         }
 1127:         foreach my $field_name(@fields) {
 1128: 
 1129:             if (defined($env{'form.new_'.$field_name})) {
 1130:                 $Apache::lonpublisher::metadatafields{$field_name}=
 1131:                     join(',',&Apache::loncommon::get_env_multiple('form.new_'.$field_name));
 1132:             }
 1133:             if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
 1134:                 # handle restrictions here
 1135:                 if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
 1136:                     ($field_name eq 'courserestricted')){
 1137:                     $output.=('<p>'.$lt{$field_name}.': '.
 1138:                               &prettyinput($field_name,
 1139: 				   $Apache::lonpublisher::metadatafields{$field_name},
 1140: 				                    'new_'.$field_name,'defaultmeta',
 1141: 				                    undef,undef,undef,undef,
 1142: 				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</p>');
 1143:                  }
 1144:             } else {
 1145: 
 1146:                     $output.=('<p>'.$lt{$field_name}.': '.
 1147:                             &prettyinput($field_name,
 1148: 				   $Apache::lonpublisher::metadatafields{$field_name},
 1149: 				   'new_'.$field_name,'defaultmeta').'</p>');
 1150:                
 1151:             }
 1152:         }
 1153: 
 1154: 	    $r->print($output.'<br /><input type="submit" name="store" value="'.
 1155:                   &mt('Store Catalog Information').'">');
 1156:         
 1157:     }
 1158:     $r->print('</form>');
 1159:             if ($env{'form.store'}) {
 1160:             my $mfh;
 1161:             my $formname='store'; 
 1162:             my $file_content;
 1163:             foreach my $meta_field (keys %env) {
 1164:                 if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
 1165:                     $Apache::lonpublisher::metadatafields{'keywords'} = 
 1166:                         join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
 1167:                 }
 1168:             }
 1169:             foreach (sort keys %Apache::lonpublisher::metadatafields) {
 1170:                 next if ($_ =~ /\./);
 1171:                 my $unikey=$_;
 1172:                 $unikey=~/^([A-Za-z]+)/;
 1173:                 my $tag=$1;
 1174:                 $tag=~tr/A-Z/a-z/;
 1175:                 $file_content.= "\n\<$tag";
 1176:                 foreach (split(/\,/,
 1177:                              $Apache::lonpublisher::metadatakeys{$unikey})
 1178:                          ) {
 1179:                     my $value=
 1180:                     $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
 1181:                     $value=~s/\"/\'\'/g;
 1182:                     $file_content.=' '.$_.'="'.$value.'"' ;
 1183:                     # print $mfh ' '.$_.'="'.$value.'"';
 1184:                 }
 1185:                 $file_content.= '>'.
 1186:                     &HTML::Entities::encode
 1187:                     ($Apache::lonpublisher::metadatafields{$unikey},
 1188:                      '<>&"').
 1189:                      '</'.$tag.'>';
 1190:             }
 1191:             if ($fn =~ /\/portfolio\//) {
 1192:                 $fn =~ /\/portfolio\/(.*)$/;
 1193:                 my $new_fn = '/'.$1;
 1194:                 $env{'form.'.$formname}=$file_content."\n";
 1195:                 $env{'form.'.$formname.'.filename'}=$new_fn;
 1196:                 &Apache::lonnet::userfileupload('uploaddoc','',
 1197: 	        	 'portfolio'.$env{'form.currentpath'});
 1198:                 if (&Apache::lonnet::userfileupload($formname,'','portfolio') eq 'error: no uploaded file') {
 1199:                     $r->print('<p><font color="red">'.
 1200:                       &mt('Could not write metadata').', '.
 1201:                      &mt('FAIL').'</font></p>');
 1202:                 } else {
 1203:                     $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
 1204: 		  ' '.&Apache::lonlocal::locallocaltime(time).
 1205: 		  '</font></p>');
 1206:                 }
 1207:             } else {
 1208:                 if (!  ($mfh=Apache::File->new('>'.$fn))) {
 1209:                     $r->print('<p><font color="red">'.
 1210:                         &mt('Could not write metadata').', '.
 1211:                         &mt('FAIL').'</font></p>');
 1212:                 } else {
 1213:                     print $mfh $file_content;
 1214: 		    $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
 1215: 			      ' '.&Apache::lonlocal::locallocaltime(time).
 1216: 			      '</font></p>');
 1217:                 }
 1218:             }
 1219:         }
 1220:     return;
 1221: }
 1222: 
 1223: 1;
 1224: __END__
 1225: 
 1226:      

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