File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.171: download - view: text, annotated - select for diffs
Mon Aug 21 18:04:20 2006 UTC (17 years, 9 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Allow group repository files to have editable metadata.

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

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