File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.146: download - view: text, annotated - select for diffs
Mon Dec 19 21:49:12 2005 UTC (18 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- add a navigational method out of the metadata editor

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

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