File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.212: download - view: text, annotated - select for diffs
Thu Mar 6 20:20:18 2008 UTC (16 years, 2 months ago) by www
Branches: MAIN
CVS tags: version_2_8_X, version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, HEAD
There was no way back.

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.212 2008/03/06 20:20:18 www 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 LONCAPA qw(:DEFAULT :match);
   43: 
   44: 
   45: ############################################################
   46: ############################################################
   47: ##
   48: ## &get_dynamic_metadata_from_sql($url)
   49: ## 
   50: ## Queries sql database for dynamic metdata
   51: ## Returns a hash of hashes, with keys of urls which match $url
   52: ## Returned fields are given below.
   53: ##
   54: ## Examples:
   55: ## 
   56: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
   57: ##     ('/res/msu/korte/');
   58: ##
   59: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
   60: ##
   61: ############################################################
   62: ############################################################
   63: sub get_dynamic_metadata_from_sql {
   64:     my ($url) = shift();
   65:     my ($authordom,$author)=($url=~m{^/res/($match_domain)/($match_username)/});
   66:     if (! defined($authordom)) {
   67:         $authordom = shift();
   68:     }
   69:     if  (! defined($author)) { 
   70:         $author = shift();
   71:     }
   72:     if (! defined($authordom) || ! defined($author)) {
   73:         return ();
   74:     }
   75:     my $query = 'SELECT * FROM metadata WHERE url LIKE "'.$url.'%"';
   76:     my $server = &Apache::lonnet::homeserver($author,$authordom);
   77:     my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
   78:                                                 ,[$server]);
   79:     return () if (! defined($reply) || ref($reply) ne 'HASH');
   80:     my $filename = $reply->{$server};
   81:     if (! defined($filename) || $filename =~ /^error/) {
   82:         return ();
   83:     }
   84:     my $max_time = time + 10; # wait 10 seconds for results at most
   85:     my %ReturnHash;
   86:     #
   87:     # Look for results
   88:     my $finished = 0;
   89:     while (! $finished && time < $max_time) {
   90:         my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
   91:         if (! -e "$datafile.end") { next; }
   92:         my $fh;
   93:         if (!($fh=Apache::File->new($datafile))) { next; }
   94:         while (my $result = <$fh>) {
   95:             chomp($result);
   96:             next if (! $result);
   97:             my %hash=&LONCAPA::lonmetadata::metadata_col_to_hash('metadata',
   98: 								 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=~/^($match_domain)\/($match_username)\//);
  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' && $file_type ne 'groups') ) {
  239:         %fields = 
  240:         (%fields,
  241:          'domain' => 'Domain',
  242:          'mime' => 'MIME Type',
  243:          'language' => 'Language',
  244:          'creationdate' => 'Creation Date',
  245:          'lastrevisiondate' => 'Last Revision Date',
  246:          'owner' => 'Publisher/Owner',
  247:          'copyright' => 'Copyright/Distribution',
  248:          'customdistributionfile' => 'Custom Distribution File',
  249:          'sourceavail' => 'Source Available',
  250:          'sourcerights' => 'Source Custom Distribution File',
  251:          'obsolete' => 'Obsolete',
  252:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
  253:          'count'      => 'Network-wide number of accesses (hits)',
  254:          'course'     => 'Network-wide number of courses using resource',
  255:          'course_list' => 'Network-wide courses using resource',
  256:          'sequsage'      => 'Number of resources using or importing resource',
  257:          'sequsage_list' => 'Resources using or importing resource',
  258:          'goto'       => 'Number of resources that follow this resource in maps',
  259:          'goto_list'  => 'Resources that follow this resource in maps',
  260:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
  261:          'comefrom_list' => 'Resources that lead up to this resource in maps',
  262:          'clear'      => 'Material presented in clear way',
  263:          'depth'      => 'Material covered with sufficient depth',
  264:          'helpful'    => 'Material is helpful',
  265:          'correct'    => 'Material appears to be correct',
  266:          'technical'  => 'Resource is technically correct', 
  267:          'avetries'   => 'Average number of tries till solved',
  268:          'stdno'      => 'Statistics calculated for number of students',
  269:          'difficulty' => 'Degree of difficulty',
  270:          'disc'       => 'Degree of discrimination',
  271: 	     'dependencies' => 'Resources used by this resource',
  272:          );
  273:     }
  274:     return &Apache::lonlocal::texthash(%fields);
  275: }
  276: 
  277: sub portfolio_linked_path {
  278:     my ($path,$group,$port_path) = @_;
  279: 
  280:     my $start = 'portfolio';
  281:     if ($group) {
  282: 	$start = "groups/$group/".$start;
  283:     }
  284:     my %anchor_fields = (
  285:         'selectfile'  => $start,
  286:         'currentpath' => '/'
  287:     );
  288:     my $result = &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$start);
  289:     my $fullpath = '/';
  290:     my (undef,@tree) = split('/',$path);
  291:     my $filename = pop(@tree);
  292:     foreach my $dir (@tree) {
  293: 	$fullpath .= $dir.'/';
  294: 	$result .= '/';
  295: 	my %anchor_fields = (
  296:             'selectfile'  => $dir,
  297:             'currentpath' => $fullpath
  298:         );
  299: 	$result .= &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$dir);
  300:     }
  301:     $result .= "/$filename";
  302:     return $result;
  303: }
  304: 
  305: sub get_port_path_and_group {
  306:     my ($uri)=@_;
  307: 
  308:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  309:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  310: 
  311:     my ($port_path,$group);
  312:     if ($uri =~ m{^/editupload/\Q$cdom\E/\Q$cnum\E/groups/}) {
  313: 	$group = (split('/',$uri))[5];
  314: 	$port_path = '/adm/coursegrp_portfolio';
  315:     } else {
  316: 	$port_path = '/adm/portfolio';
  317:     }
  318:     if ($env{'form.group'} ne $group) {
  319: 	$env{'form.group'} = $group;
  320:     }
  321:     return ($port_path,$group);
  322: }
  323: 
  324: sub portfolio_display_uri {
  325:     my ($uri,$as_links)=@_;
  326: 
  327:     my ($port_path,$group) = &get_port_path_and_group($uri);
  328: 
  329:     $uri =~ s|.*/(portfolio/.*)$|$1|;
  330:     my ($res_uri,$meta_uri) = ($uri,$uri);
  331:     if ($uri =~ /\.meta$/) {
  332: 	$res_uri =~ s/\.meta//;
  333:     } else {
  334: 	$meta_uri .= '.meta';
  335:     }
  336: 
  337:     my ($path) = ($res_uri =~ m|^portfolio(.*/)[^/]*$|);
  338:     if ($as_links) {
  339: 	$res_uri = &portfolio_linked_path($res_uri,$group,$port_path);
  340: 	$meta_uri = &portfolio_linked_path($meta_uri,$group,$port_path);
  341:     }
  342:     return ($res_uri,$meta_uri,$path);
  343: }
  344: 
  345: sub pre_select_course {
  346:     my ($r,$uri) = @_;
  347:     my $output;
  348:     my $fn=&Apache::lonnet::filelocation('',$uri);
  349:     my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri);
  350:     %Apache::lonpublisher::metadatafields=();
  351:     %Apache::lonpublisher::metadatakeys=();
  352:     my $result=&Apache::lonnet::getfile($fn);
  353:     if ($result == -1){
  354:         $r->print(&mt('Creating new file [_1]'),$meta_uri);
  355:     } else {
  356:         &Apache::lonpublisher::metaeval($result);
  357:     }
  358:     $r->print('<hr /><form method="post" action="" >');
  359:     $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>');
  360:     $output = &select_course();
  361:     $r->print($output.'<br /><input type="submit" name="store" value="'.
  362:                   &mt('Associate Resource With Selected Course').'" />');
  363:     $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
  364:     $r->print('<input type="hidden" name="associate" value="true" />');
  365:     $r->print('</form>');
  366:     
  367:     my ($port_path,$group) = &get_port_path_and_group($uri);
  368:     my $group_input;
  369:     if ($group) {
  370:         $group_input = '<input type="hidden" name="group" value="'.$group.'" />';
  371:     } 
  372:     $r->print('<br /><br /><form method="post" action="'.$port_path.'">'.
  373:               '<input type="hidden" name="currentpath" value="'.$path.'" />'.
  374: 	      $group_input.
  375: 	      '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
  376: 	      '</form>');
  377: 
  378:     return;
  379: }
  380: sub select_course {
  381:     my $output=$/;
  382:     my $current_restriction=
  383: 	$Apache::lonpublisher::metadatafields{'courserestricted'};
  384:     my $selected = ($current_restriction eq 'none' ? 'selected="selected"' 
  385: 		                                   : '');
  386:     if ($current_restriction =~ /^course\.($match_domain\_$match_courseid)$/) {
  387:         my $assoc_crs = $1;
  388:         my $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
  389:         if (ref($added_metadata_fields) eq 'HASH') {
  390:             if (keys(%{$added_metadata_fields}) > 0) {
  391:                 my $transfernotes;
  392:                 foreach my $field_name (keys(%{$added_metadata_fields})) {
  393:                     my $value = $Apache::lonpublisher::metadatafields{$field_name};
  394:                     if ($value) {
  395:                         $transfernotes .= 
  396:                             &Apache::loncommon::start_data_table_row(). 
  397:                             '<td><input type="checkbox" name="transfer_'.
  398:                             $field_name.'" value="1" /></td><td>'.
  399:                             $field_name.'</td><td>'.$value.'</td>'.
  400:                             &Apache::loncommon::end_data_table_row();
  401:                     }
  402:                 }
  403:                 if ($transfernotes ne '') {
  404:                     my %courseinfo = &Apache::lonnet::coursedescription($assoc_crs,{'one_time' => 1});
  405:                     my $assoc_crs_description = $courseinfo{'description'};
  406:                     $output .= &mt('This resource is currently associated with a course ([_1]) which includes added metadata fields specific to the course.',$assoc_crs_description).'<br />'."\n".
  407:                     &mt('You can choose to transfer data from the added fields to the "Notes" field if you are planning to change the course association.').'<br /><br />'.
  408:                     &Apache::loncommon::start_data_table().
  409:                     &Apache::loncommon::start_data_table_header_row().
  410:                     '<th>Copy to notes?</th>'."\n".
  411:                     '<th>Field name</th>'."\n".
  412:                     '<th>Values</th>'."\n".
  413:                     &Apache::loncommon::end_data_table_header_row().
  414:                     $transfernotes.
  415:                     &Apache::loncommon::end_data_table().'<br />';
  416:                 }
  417:             }
  418:         }
  419:     }
  420:     $output .= '<select name="new_courserestricted" >';
  421:     $output .= '<option value="none" '.$selected.'>'.
  422: 	&mt('None').'</option>'.$/;
  423:     my %courses;
  424:     foreach my $key (keys(%env)) {
  425:         if ($key !~ m/^course\.(.+)\.description$/) { next; }
  426: 	my $cid = $1;
  427:         if ($env{$key} !~ /\S/) { next; }
  428: 	$courses{$key} = $cid;
  429:     }
  430:     foreach my $key (sort { lc($env{$a}) cmp lc($env{$b}) } (keys(%courses))) {
  431: 	my $cid = 'course.'.$courses{$key};
  432: 	my $selected = ($current_restriction eq $cid ? 'selected="selected"' 
  433: 		                                     : '');
  434:         if ($env{$key} !~ /\S/) { next; }
  435: 	$output .= '<option value="'.$cid.'" '.$selected.'>';
  436: 	$output .= $env{$key};
  437: 	$output .= '</option>'.$/;
  438: 	$selected = '';
  439:     }
  440:     $output .= '</select><br />';
  441:     return ($output);
  442: }
  443: # Pretty printing of metadata field
  444: 
  445: sub prettyprint {
  446:     my ($type,$value,$target,$prefix,$form,$noformat)=@_;
  447: # $target,$prefix,$form are optional and for filecrumbs only
  448:     if (! defined($value)) { 
  449:         return '&nbsp;'; 
  450:     }
  451:     # Title
  452:     if ($type eq 'title') {
  453: 	return '<font size="+1" face="arial">'.$value.'</font>';
  454:     }
  455:     # Dates
  456:     if (($type eq 'creationdate') ||
  457: 	($type eq 'lastrevisiondate')) {
  458: 	return ($value?&Apache::lonlocal::locallocaltime(
  459: 			  &Apache::lonmysql::unsqltime($value)):
  460: 		&mt('not available'));
  461:     }
  462:     # Language
  463:     if ($type eq 'language') {
  464: 	return &Apache::loncommon::languagedescription($value);
  465:     }
  466:     # Copyright
  467:     if ($type eq 'copyright') {
  468: 	return &Apache::loncommon::copyrightdescription($value);
  469:     }
  470:     # Copyright
  471:     if ($type eq 'sourceavail') {
  472: 	return &Apache::loncommon::source_copyrightdescription($value);
  473:     }
  474:     # MIME
  475:     if ($type eq 'mime') {
  476:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
  477:             &Apache::loncommon::filedescription($value);
  478:     }
  479:     # Person
  480:     if (($type eq 'author') || 
  481: 	($type eq 'owner') ||
  482: 	($type eq 'modifyinguser') ||
  483: 	($type eq 'authorspace')) {
  484: 	$value=~s/($match_username)(\:|\@)($match_domain)/&authordisplay($1,$3)/gse;
  485: 	return $value;
  486:     }
  487:     # Gradelevel
  488:     if (($type eq 'lowestgradelevel') ||
  489: 	($type eq 'highestgradelevel')) {
  490: 	return &Apache::loncommon::gradeleveldescription($value);
  491:     }
  492:     # Only for advance users below
  493:     if (! $env{'user.adv'}) { 
  494:         return '<i>- '.&mt('not displayed').' -</i>';
  495:     }
  496:     # File
  497:     if (($type eq 'customdistributionfile') ||
  498: 	($type eq 'obsoletereplacement') ||
  499: 	($type eq 'goto_list') ||
  500: 	($type eq 'comefrom_list') ||
  501: 	($type eq 'sequsage_list') ||
  502: 	($type eq 'dependencies')) {
  503: 	return '<font size="-1"><ul>'.join("\n",map {
  504:             my $url = &Apache::lonnet::clutter_with_no_wrapper($_);
  505:             my $title = &Apache::lonnet::gettitle($url);
  506:             if ($title eq '') {
  507:                 $title = 'Untitled';
  508:                 if ($url =~ /\.sequence$/) {
  509:                     $title .= ' Sequence';
  510:                 } elsif ($url =~ /\.page$/) {
  511:                     $title .= ' Page';
  512:                 } elsif ($url =~ /\.problem$/) {
  513:                     $title .= ' Problem';
  514:                 } elsif ($url =~ /\.html$/) {
  515:                     $title .= ' HTML document';
  516:                 } elsif ($url =~ m:/syllabus$:) {
  517:                     $title .= ' Syllabus';
  518:                 } 
  519:             }
  520:             $_ = '<li>'.$title.' '.
  521: 		&Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
  522:                 '</li>'
  523: 	    } split(/\s*\,\s*/,$value)).'</ul></font>';
  524:     }
  525:     # Evaluations
  526:     if (($type eq 'clear') ||
  527: 	($type eq 'depth') ||
  528: 	($type eq 'helpful') ||
  529: 	($type eq 'correct') ||
  530: 	($type eq 'technical')) {
  531: 	return &evalgraph($value);
  532:     }
  533:     # Difficulty
  534:     if ($type eq 'difficulty' || $type eq 'disc') {
  535: 	return &diffgraph($value);
  536:     }
  537:     # List of courses
  538:     if ($type=~/\_list/) {
  539:         my @Courses = split(/\s*\,\s*/,$value);
  540:         my $Str='<font size="-1"><ul>';
  541: 	my %descriptions;
  542:         foreach my $course (@Courses) {
  543:             my %courseinfo =
  544: 		&Apache::lonnet::coursedescription($course,
  545: 						   {'one_time' => 1});
  546:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  547:                 next;
  548:             }
  549: 	    $descriptions{join('\0',@courseinfo{'domain','description'})} .= 
  550: 		'<li><a href="/public/'.$courseinfo{'domain'}.'/'.
  551:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
  552:                 $courseinfo{'description'}.' ('.$courseinfo{'domain'}.
  553: 		')</a></li>';
  554:         }
  555: 	foreach my $course (sort {lc($a) cmp lc($b)} (keys(%descriptions))) {
  556: 	    $Str .= $descriptions{$course};
  557: 	}
  558: 
  559: 	return $Str.'</ul></font>';
  560:     }
  561:     # No pretty print found
  562:     return $value;
  563: }
  564: 
  565: # Pretty input of metadata field
  566: sub direct {
  567:     return shift;
  568: }
  569: 
  570: sub selectbox {
  571:     my ($name,$value,$functionref,@idlist)=@_;
  572:     if (! defined($functionref)) {
  573:         $functionref=\&direct;
  574:     }
  575:     my $selout='<select name="'.$name.'">';
  576:     foreach (@idlist) {
  577:         $selout.='<option value=\''.$_.'\'';
  578:         if ($_ eq $value) {
  579: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  580: 	}
  581:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  582:     }
  583:     return $selout.'</select>';
  584: }
  585: 
  586: sub relatedfield {
  587:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
  588:     if (! $relatedsearchflag) { 
  589:         return '';
  590:     }
  591:     if (! defined($relatedsep)) {
  592:         $relatedsep=' ';
  593:     }
  594:     if (! $show) {
  595:         return $relatedsep.'&nbsp;';
  596:     }
  597:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
  598: 	($relatedvalue?' checked="1"':'').' />';
  599: }
  600: 
  601: sub prettyinput {
  602:     my ($type,$value,$fieldname,$formname,
  603: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
  604:     if (! defined($size)) {
  605:         $size = 80;
  606:     }
  607:     my $output;
  608:     if (defined($course_key) 
  609: 	&& exists($env{$course_key.'.metadata.'.$type.'.options'})) {
  610:         my $stu_add;
  611:         my $only_one;
  612:         my %meta_options;
  613:         my @cur_values_inst;
  614:         my $cur_values_stu;
  615:         my $values = $env{$course_key.'.metadata.'.$type.'.values'};
  616:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
  617:             $stu_add = 'true';
  618:         }
  619:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
  620:             $only_one = 'true';
  621:         }
  622:         # need to take instructor values out of list where instructor and student
  623:         # values may be mixed.
  624:         if ($values) {
  625:             foreach my $item (split(/,/,$values)) {
  626:                 $item =~ s/^\s+//;
  627:                 $meta_options{$item} = $item;
  628:             }
  629:             foreach my $item (split(/,/,$value)) {
  630:                 $item =~ s/^\s+//;
  631:                 if ($meta_options{$item}) {
  632:                     push(@cur_values_inst,$item);
  633:                 } else {
  634:                     if ($item ne '') {
  635:                         $cur_values_stu .= $item.',';
  636:                     }
  637:                 }
  638:             }
  639:              $cur_values_stu =~ s/,$//;
  640:             my @key_order = sort(keys(%meta_options));
  641:             unshift(@key_order,'');
  642:             $meta_options{''} = 'Not specified';
  643:             $meta_options{'select_form_order'} = \@key_order;
  644:         } else {
  645:             $cur_values_stu = $value;
  646:         }
  647:         if ($type eq 'courserestricted') {
  648:             return (&select_course());
  649:             # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  650:         }
  651:         if (($type eq 'keywords') || ($type eq 'subject')
  652:              || ($type eq 'author')||($type eq  'notes')
  653:              || ($type eq  'abstract')|| ($type eq  'title')|| ($type eq  'standards')
  654:              || (exists($env{$course_key.'.metadata.'.$type.'.added'}))) {
  655:             
  656:             if ($values) {
  657:                 if ($only_one) {
  658:                     $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
  659:                 } else {
  660:                     $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
  661:                 }
  662:             }
  663:             if ($stu_add) {
  664:                 $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  665:                 'value="'.$cur_values_stu.'" />'.
  666:                 &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  667:                       $relatedvalue); 
  668:             }
  669:             return ($output);
  670:         } 
  671:         if (($type eq 'lowestgradelevel') ||
  672: 	    ($type eq 'highestgradelevel')) {
  673: 	    return &Apache::loncommon::select_level_form($value,$fieldname).
  674:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  675:         }
  676:         return(); 
  677:     }
  678:     # Language
  679:     if ($type eq 'language') {
  680: 	return &selectbox($fieldname,
  681: 			  $value,
  682: 			  \&Apache::loncommon::languagedescription,
  683: 			  (&Apache::loncommon::languageids)).
  684:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  685:     }
  686:     # Copyright
  687:     if ($type eq 'copyright') {
  688: 	return &selectbox($fieldname,
  689: 			  $value,
  690: 			  \&Apache::loncommon::copyrightdescription,
  691: 			  (&Apache::loncommon::copyrightids)).
  692:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  693:     }
  694:     # Source Copyright
  695:     if ($type eq 'sourceavail') {
  696: 	return &selectbox($fieldname,
  697: 			  $value,
  698: 			  \&Apache::loncommon::source_copyrightdescription,
  699: 			  (&Apache::loncommon::source_copyrightids)).
  700:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  701:     }
  702:     # Gradelevels
  703:     if (($type eq 'lowestgradelevel') ||
  704: 	($type eq 'highestgradelevel')) {
  705: 	return &Apache::loncommon::select_level_form($value,$fieldname).
  706:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  707:     }
  708:     # Obsolete
  709:     if ($type eq 'obsolete') {
  710: 	return '<input type="checkbox" name="'.$fieldname.'"'.
  711: 	    ($value?' checked="1"':'').' />'.
  712:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  713:     }
  714:     # Obsolete replacement file
  715:     if ($type eq 'obsoletereplacement') {
  716: 	return '<input type="text" name="'.$fieldname.
  717: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  718: 	    "('".$formname."','".$fieldname."'".
  719: 	    ",'')\">".&mt('Select').'</a>'.
  720:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  721:     }
  722:     # Customdistribution file
  723:     if ($type eq 'customdistributionfile') {
  724: 	return '<input type="text" name="'.$fieldname.
  725: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  726: 	    "('".$formname."','".$fieldname."'".
  727: 	    ",'rights')\">".&mt('Select').'</a>'.
  728:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  729:     }
  730:     # Source Customdistribution file
  731:     if ($type eq 'sourcerights') {
  732: 	return '<input type="text" name="'.$fieldname.
  733: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  734: 	    "('".$formname."','".$fieldname."'".
  735: 	    ",'rights')\">".&mt('Select').'</a>'.
  736:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  737:     }
  738:     if ($type eq 'courserestricted') {
  739:         return (&select_course());
  740:         #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
  741:     }
  742: 
  743:     # Dates
  744:     if (($type eq 'creationdate') ||
  745: 	($type eq 'lastrevisiondate')) {
  746: 	return 
  747:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
  748:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  749:     }
  750:     # No pretty input found
  751:     $value=~s/^\s+//gs;
  752:     $value=~s/\s+$//gs;
  753:     $value=~s/\s+/ /gs;
  754:     $value=~s/\"/\&quot\;/gs;
  755:     return 
  756:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  757:         'value="'.$value.'" />'.
  758:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  759:                       $relatedvalue); 
  760: }
  761: 
  762: # Main Handler
  763: sub handler {
  764:     my $r=shift;
  765:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  766:          ['currentpath','changecourse']);
  767:     my $uri=$r->uri;
  768:     #
  769:     # Set document type
  770:     &Apache::loncommon::content_type($r,'text/html');
  771:     $r->send_http_header;
  772:     return OK if $r->header_only;
  773:     my ($resdomain,$resuser)=
  774:         (&Apache::lonnet::declutter($uri)=~/^($match_domain)\/($match_username)\//);
  775:     if ($uri=~m:/adm/bombs/(.*)$:) {
  776:         $r->print(&Apache::loncommon::start_page('Error Messages'));
  777:         # Looking for all bombs?
  778:         &report_bombs($r,$uri);
  779:     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
  780: 	    ($resdomain,$resuser)=
  781: 		(&Apache::lonnet::declutter($uri)=~m|^($match_domain)/($match_name)/portfolio|);
  782:         $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
  783: 						 undef,
  784: 						 {'domain' => $resdomain,}));
  785:         if ($env{'form.store'}) {
  786:             &present_editable_metadata($r,$uri,'portfolio');
  787:         } else {
  788:             my $fn=&Apache::lonnet::filelocation('',$uri);
  789:             %Apache::lonpublisher::metadatafields=();
  790:             %Apache::lonpublisher::metadatakeys=();
  791:             my $result=&Apache::lonnet::getfile($fn);
  792:             &Apache::lonpublisher::metaeval($result);
  793:             if ((!$Apache::lonpublisher::metadatafields{'courserestricted'}) ||
  794:                 ($env{'form.changecourse'} eq 'true')) {
  795:                 &pre_select_course($r,$uri);
  796:             } else {
  797:                 &present_editable_metadata($r,$uri,'portfolio');
  798:             }
  799:         }
  800:     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/groups/|) {
  801:         $r->print(&Apache::loncommon::start_page('Edit Group Portfolio File Catalog Information',
  802: 						 undef,
  803: 						 {'domain' => $resdomain,}));
  804:         &present_editable_metadata($r,$uri,'groups');    
  805:     } elsif ($uri=~m|^/~|) { 
  806:         # Construction space
  807:         $r->print(&Apache::loncommon::start_page('Edit Catalog Information',
  808: 						"\n".'<script type="text/javascript">'."\n".
  809:                                                 &Apache::loncommon::browser_and_searcher_javascript().
  810:                                                 "\n".'</script>',
  811: 						 {'domain' => $resdomain,}));
  812:         &present_editable_metadata($r,$uri);
  813:     } else {
  814:         $r->print(&Apache::loncommon::start_page('Catalog Information',
  815: 						 undef,
  816: 						 {'domain' => $resdomain,}));
  817:         &present_uneditable_metadata($r,$uri);
  818:     }
  819:     $r->print(&Apache::loncommon::end_page());
  820:     return OK;
  821: }
  822: 
  823: #####################################################
  824: #####################################################
  825: ###                                               ###
  826: ###                Report Bombs                   ###
  827: ###                                               ###
  828: #####################################################
  829: #####################################################
  830: sub report_bombs {
  831:     my ($r,$uri) = @_;
  832:     # Set document type
  833:     $uri =~ s:/adm/bombs/::;
  834:     $uri = &Apache::lonnet::declutter($uri);
  835:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  836:     my ($domain,$author)=($uri=~/^($match_domain)\/($match_username)\//);
  837:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  838: 	if ($env{'form.clearbombs'}) {
  839: 	    &Apache::lonmsg::clear_author_res_msg($uri);
  840: 	}
  841:         my $clear=&mt('Clear all Messages in Subdirectory');
  842:         my $cancel=&mt('Back to Directory');
  843:         my $cancelurl=$uri;
  844:         $cancelurl=~s/^\Q$domain\E/\/priv/;
  845:         $r->print(<<ENDCLEAR);
  846: <form method="post">
  847: <input type="submit" name="clearbombs" value="$clear" />
  848: <a href="$cancelurl">$cancel</a>
  849: </form><hr />
  850: ENDCLEAR
  851:         my %brokenurls = 
  852:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  853:         foreach (sort(keys(%brokenurls))) {
  854:             if ($_=~/^\Q$uri\E/) {
  855:                 $r->print
  856:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  857:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  858:                      '<hr />');
  859:             }
  860:         }
  861:     } else {
  862:         $r->print(&mt('Not authorized'));
  863:     }
  864:     return;
  865: }
  866: 
  867: #####################################################
  868: #####################################################
  869: ###                                               ###
  870: ###        Uneditable Metadata Display            ###
  871: ###                                               ###
  872: #####################################################
  873: #####################################################
  874: sub present_uneditable_metadata {
  875:     my ($r,$uri) = @_;
  876:     #
  877:     my $uploaded = ($uri =~ m|/uploaded/|);
  878:     my %content=();
  879:     # Read file
  880:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  881:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  882:     }
  883:     # Render Output
  884:     # displayed url
  885:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  886:     $uri=~s/\.meta$//;
  887:     my $disuri=&Apache::lonnet::clutter_with_no_wrapper($uri);
  888:     # version
  889:     my $versiondisplay='';
  890:     if (!$uploaded) {
  891: 	my $currentversion=&Apache::lonnet::getversion($disuri);
  892: 	if ($thisversion) {
  893: 	    $versiondisplay=&mt('Version').': '.$thisversion.
  894: 		' ('.&mt('most recent version').': '.
  895: 		($currentversion>0 ? 
  896: 		 $currentversion   :
  897: 		 &mt('information not available')).')';
  898: 	} else {
  899: 	    $versiondisplay='Version: '.$currentversion;
  900: 	}
  901:     }
  902:     # crumbify displayed URL               uri     target prefix form  size
  903:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  904:     $disuri =~ s:<br />::g;
  905:     # obsolete
  906:     my $obsolete=$content{'obsolete'};
  907:     my $obsoletewarning='';
  908:     if (($obsolete) && ($env{'user.adv'})) {
  909:         $obsoletewarning='<p><font color="red">'.
  910:             &mt('This resource has been marked obsolete by the author(s)').
  911:             '</font></p>';
  912:     }
  913:     #
  914:     my %lt=&fieldnames();
  915:     my $table='';
  916:     my $title = $content{'title'};
  917:     if (! defined($title)) {
  918:         $title = 'Untitled Resource';
  919:     }
  920:     my @fields;
  921:     if ($uploaded) {
  922: 	@fields = ('title','author','subject','keywords','notes','abstract',
  923: 		   'lowestgradelevel','highestgradelevel','standards','mime',
  924: 		   'owner');
  925:     } else {
  926: 	@fields = ('title', 
  927: 		   'author', 
  928: 		   'subject', 
  929: 		   'keywords', 
  930: 		   'notes', 
  931: 		   'abstract',
  932: 		   'lowestgradelevel',
  933: 		   'highestgradelevel',
  934: 		   'standards', 
  935: 		   'mime', 
  936: 		   'language', 
  937: 		   'creationdate', 
  938: 		   'lastrevisiondate', 
  939: 		   'owner', 
  940: 		   'copyright', 
  941: 		   'customdistributionfile',
  942: 		   'sourceavail',
  943: 		   'sourcerights', 
  944: 		   'obsolete', 
  945: 		   'obsoletereplacement');
  946:     }
  947:     foreach my $field (@fields) {
  948:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$field}.
  949:             '</td><td bgcolor="#CCCCCC">'.
  950:             &prettyprint($field,$content{$field}).'</td></tr>';
  951:         delete($content{$field});
  952:     }
  953:     #
  954:     $r->print(<<ENDHEAD);
  955: <h2>$title</h2>
  956: <p>
  957: $disuri<br />
  958: $obsoletewarning
  959: $versiondisplay
  960: </p>
  961: <table cellspacing="2" border="0">
  962: $table
  963: </table>
  964: ENDHEAD
  965:     if (!$uploaded && $env{'user.adv'}) {
  966:         &print_dynamic_metadata($r,$uri,\%content);
  967:     }
  968:     return;
  969: }
  970: 
  971: sub print_dynamic_metadata {
  972:     my ($r,$uri,$content) = @_;
  973:     #
  974:     my %content = %$content;
  975:     my %lt=&fieldnames();
  976:     #
  977:     my $description = 'Dynamic Metadata (updated periodically)';
  978:     $r->print('<h3>'.&mt($description).'</h3>'.
  979:               &mt('Processing'));
  980:     $r->rflush();
  981:     my %items=&fieldnames();
  982:     my %dynmeta=&dynamicmeta($uri);
  983:     #
  984:     # General Access and Usage Statistics
  985:     if (exists($dynmeta{'count'}) ||
  986:         exists($dynmeta{'sequsage'}) ||
  987:         exists($dynmeta{'comefrom'}) ||
  988:         exists($dynmeta{'goto'}) ||
  989:         exists($dynmeta{'course'})) {
  990:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  991:                   '<table cellspacing="2" border="0">');
  992:         foreach ('count',
  993:                  'sequsage','sequsage_list',
  994:                  'comefrom','comefrom_list',
  995:                  'goto','goto_list',
  996:                  'course','course_list') {
  997:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  998:                       '<td bgcolor="#CCCCCC">'.
  999:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
 1000:         }
 1001:         $r->print('</table>');
 1002:     } else {
 1003:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
 1004:     }
 1005:     #
 1006:     # Assessment statistics
 1007:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
 1008:         if (exists($dynmeta{'stdno'}) ||
 1009:             exists($dynmeta{'avetries'}) ||
 1010:             exists($dynmeta{'difficulty'}) ||
 1011:             exists($dynmeta{'disc'})) {
 1012:             # This is an assessment, print assessment data
 1013:             $r->print('<h4>'.
 1014:                       &mt('Overall Assessment Statistical Data').
 1015:                       '</h4>'.
 1016:                       '<table cellspacing="2" border="0">');
 1017:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
 1018:                       '<td bgcolor="#CCCCCC">'.
 1019:                       &prettyprint('stdno',$dynmeta{'stdno'}).
 1020:                       '</td>'."</tr>\n");
 1021:             foreach ('avetries','difficulty','disc') {
 1022:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
 1023:                           '<td bgcolor="#CCCCCC">'.
 1024:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
 1025:                           '</td>'."</tr>\n");
 1026:             }
 1027:             $r->print('</table>');    
 1028:         }
 1029:         if (exists($dynmeta{'stats'})) {
 1030:             #
 1031:             # New assessment statistics
 1032:             $r->print('<h4>'.
 1033:                       &mt('Recent Detailed Assessment Statistical Data').
 1034:                       '</h4>');
 1035:             my $table = '<table cellspacing="2" border="0">'.
 1036:                 '<tr>'.
 1037:                 '<th>'.&mt('Course').'</th>'.
 1038:                 '<th>'.&mt('Section(s)').'</th>'.
 1039:                 '<th>'.&mt('Num Student').'s</th>'.
 1040:                 '<th>'.&mt('Mean Tries').'</th>'.
 1041:                 '<th>'.&mt('Degree of Difficulty').'</th>'.
 1042:                 '<th>'.&mt('Degree of Discrimination').'</th>'.
 1043:                 '<th>'.&mt('Time of computation').'</th>'.
 1044:                 '</tr>'.$/;
 1045:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
 1046:                 my $data = $dynmeta{'stats'}->{$identifier};
 1047:                 my $course = $data->{'course'};
 1048:                 my %courseinfo = 
 1049: 		    &Apache::lonnet::coursedescription($course,
 1050: 						       {'one_time' => 1});
 1051:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
 1052:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
 1053:                     next;
 1054:                 }
 1055:                 $table .= '<tr>';
 1056:                 $table .= 
 1057:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
 1058:                 $table .= 
 1059:                     '<td align="right">'.$data->{'sections'}.'</td>';
 1060:                 $table .=
 1061:                     '<td align="right">'.$data->{'stdno'}.'</td>';
 1062:                 foreach ('avetries','difficulty','disc') {
 1063:                     $table .= '<td align="right">';
 1064:                     if (exists($data->{$_})) {
 1065:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
 1066:                     } else {
 1067:                         $table .= '';
 1068:                     }
 1069:                     $table .= '</td>';
 1070:                 }
 1071:                 $table .=
 1072:                     '<td><nobr>'.
 1073:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
 1074:                     '</nobr></td>';
 1075:                 $table .=
 1076:                     '</tr>'.$/;
 1077:             }
 1078:             $table .= '</table>'.$/;
 1079:             $r->print($table);
 1080:         } else {
 1081:             $r->print(&mt('No new dynamic data found.'));
 1082:         }
 1083:     } else {
 1084:         $r->print('<h4>'.
 1085:           &mt('No Assessment Statistical Data is available for this resource').
 1086:                   '</h4>');
 1087:     }
 1088: 
 1089:     #
 1090:     #
 1091:     if (exists($dynmeta{'clear'})   || 
 1092:         exists($dynmeta{'depth'})   || 
 1093:         exists($dynmeta{'helpful'}) || 
 1094:         exists($dynmeta{'correct'}) || 
 1095:         exists($dynmeta{'technical'})){ 
 1096:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
 1097:                   '<table cellspacing="2" border="0">');
 1098:         foreach ('clear','depth','helpful','correct','technical') {
 1099:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
 1100:                       '<td bgcolor="#CCCCCC">'.
 1101:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
 1102:         }
 1103:         $r->print('</table>');
 1104:     } else {
 1105:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
 1106:     }
 1107:     $uri=~/^\/res\/($match_domain)\/($match_username)\//; 
 1108:     if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
 1109:         || ($env{'user.role.ca./'.$1.'/'.$2})) {
 1110:         if (exists($dynmeta{'comments'})) {
 1111:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
 1112:                       &mt('visible to author and co-authors only').
 1113:                       ')</h4>'.
 1114:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
 1115:         } else {
 1116:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
 1117:         }
 1118:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
 1119:         if (defined($bombs) && $bombs ne '') {
 1120:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
 1121:                       &mt('visible to author and co-authors only').')'.
 1122:                       '</h4>'.$bombs);
 1123:         } else {
 1124:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
 1125:         }
 1126:     }
 1127:     #
 1128:     # All other stuff
 1129:     $r->print('<h3>'.
 1130:               &mt('Additional Metadata (non-standard, parameters, exports)').
 1131:               '</h3><table border="0" cellspacing="1">');
 1132:     foreach (sort(keys(%content))) {
 1133:         my $name=$_;
 1134:         if ($name!~/\.display$/) {
 1135:             my $display=&Apache::lonnet::metadata($uri,
 1136:                                                   $name.'.display');
 1137:             if (! $display) { 
 1138:                 $display=$name;
 1139:             };
 1140:             my $otherinfo='';
 1141:             foreach ('name','part','type','default') {
 1142:                 if (defined(&Apache::lonnet::metadata($uri,
 1143:                                                       $name.'.'.$_))) {
 1144:                     $otherinfo.=' '.$_.'='.
 1145:                         &Apache::lonnet::metadata($uri,
 1146:                                                   $name.'.'.$_).'; ';
 1147:                 }
 1148:             }
 1149:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
 1150:             if ($otherinfo) {
 1151:                 $r->print(' ('.$otherinfo.')');
 1152:             }
 1153:             $r->print("</font></td></tr>\n");
 1154:         }
 1155:     }
 1156:     $r->print("</table>");
 1157:     return;
 1158: }
 1159: 
 1160: 
 1161: 
 1162: #####################################################
 1163: #####################################################
 1164: ###                                               ###
 1165: ###          Editable metadata display            ###
 1166: ###                                               ###
 1167: #####################################################
 1168: #####################################################
 1169: sub present_editable_metadata {
 1170:     my ($r,$uri,$file_type) = @_;
 1171:     # Construction Space Call
 1172:     # Header
 1173:     my $disuri=$uri;
 1174:     my $fn=&Apache::lonnet::filelocation('',$uri);
 1175:     $disuri=~s{^/\~}{/priv/};
 1176:     $disuri=~s/\.meta$//;
 1177:     my $meta_uri = $disuri;
 1178:     my $path;
 1179:     if ($disuri =~ m|/portfolio/|) {
 1180: 	($disuri, $meta_uri, $path) =  &portfolio_display_uri($disuri,1);
 1181:     }
 1182:     my $target=$uri;
 1183:     $target=~s{^/\~}{/res/$env{'request.role.domain'}/};
 1184:     $target=~s/\.meta$//;
 1185:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
 1186:     if ($bombs) {
 1187:         my $showdel=1;
 1188:         if ($env{'form.delmsg'}) {
 1189:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
 1190:                 $bombs=&mt('Messages deleted.');
 1191: 		$showdel=0;
 1192:             } else {
 1193:                 $bombs=&mt('Error deleting messages');
 1194:             }
 1195:         }
 1196:         if ($env{'form.clearmsg'}) {
 1197: 	    my $cleardir=$target;
 1198: 	    $cleardir=~s/\/[^\/]+$/\//;
 1199:             if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
 1200:                 $bombs=&mt('Messages cleared.');
 1201: 		$showdel=0;
 1202:             } else {
 1203:                 $bombs=&mt('Error clearing messages');
 1204:             }
 1205:         }
 1206:         my $del=&mt('Delete Messages for this Resource');
 1207: 	my $clear=&mt('Clear all Messages in Subdirectory');
 1208: 	my $goback=&mt('Back to Source File');
 1209:         $r->print(<<ENDBOMBS);
 1210: <h1>$disuri</h1>
 1211: <form method="post" action="" name="defaultmeta">
 1212: ENDBOMBS
 1213:         if ($showdel) {
 1214: 	    $r->print(<<ENDDEL);
 1215: <input type="submit" name="delmsg" value="$del" />
 1216: <input type="submit" name="clearmsg" value="$clear" />
 1217: ENDDEL
 1218:         } else {
 1219:             $r->print('<p><a href="'.$disuri.'">'.$goback.'</a></p>');
 1220: 	    if ($env{'form.clearmsg'}) {
 1221: 		my ($diruri) = ($disuri =~ m{(.*/)[^/]*});
 1222: 		$r->print('<p><a href="'.$diruri.'">'.
 1223: 			  &mt('Back To Directory').'</a></p>');
 1224: 	    }
 1225: 	}
 1226: 	$r->print('<br />'.$bombs);
 1227:     } else {
 1228:         my $displayfile=&mt('Catalog Information for [_1]',$disuri);
 1229:         if ($disuri=~/\/default$/) {
 1230:             my $dir=$disuri;
 1231:             $dir=~s/default$//;
 1232:             $displayfile=&mt('Default Cataloging Information for Directory [_1]',$dir);
 1233:         }
 1234:         %Apache::lonpublisher::metadatafields=();
 1235:         %Apache::lonpublisher::metadatakeys=();
 1236:         my $result=&Apache::lonnet::getfile($fn);
 1237:         if ($result == -1){
 1238: 	    $r->print(&mt('Creating new file [_1]'),$meta_uri);
 1239:         } else {
 1240:             &Apache::lonpublisher::metaeval($result);
 1241:         }
 1242:         if ($env{'form.new_courserestricted'}) {
 1243:             my $new_assoc_course = $env{'form.new_courserestricted'};
 1244:             my $prev_courserestricted = $Apache::lonpublisher::metadatafields{'courserestricted'};
 1245:             if (($prev_courserestricted) && 
 1246:                 ($prev_courserestricted ne $new_assoc_course)) {
 1247:                 my $transfers = [];
 1248:                 foreach my $key (keys(%env)) {
 1249:                     if ($key =~ /^form\.transfer_(.+)$/) {
 1250:                         push(@{$transfers},$1);
 1251:                     }
 1252:                 }
 1253:                 if (@{$transfers} > 0) {
 1254:                     &store_transferred_addedfields($fn,$uri,$transfers);
 1255:                 }
 1256:             }
 1257:         }
 1258:         $r->print(<<ENDEDIT);
 1259: <h1>$displayfile</h1>
 1260: <form method="post" action="" name="defaultmeta">
 1261: ENDEDIT
 1262:         my %lt=&fieldnames($file_type);
 1263: 	my $output;
 1264: 	my @fields;
 1265: 	my $added_metadata_fields;
 1266: 	my @added_order;
 1267:         if ($file_type eq 'groups') {
 1268:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1269:                 'course.'.$env{'request.course.id'};
 1270:         }
 1271:         if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
 1272:                 (! $env{'form.new_courserestricted'}) && (! $file_type eq 'groups')) {
 1273:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1274:                 'none';
 1275:         } elsif ($env{'form.new_courserestricted'}) {
 1276:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
 1277:                 $env{'form.new_courserestricted'};
 1278:         }
 1279: 	if ($file_type eq 'portfolio' || $file_type eq 'groups') {
 1280: 	    if(exists ($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'})) {
 1281: 	        # retrieve fieldnames (in order) from the course restricted list
 1282: 	        @fields = (split(/,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'}));
 1283: 	    } else {
 1284: 	        # no saved field list, use default list
 1285: 	        @fields =  ('author','title','subject','keywords','abstract',
 1286: 			    'notes','lowestgradelevel',
 1287: 	                    'highestgradelevel','standards');
 1288:                 if ($Apache::lonpublisher::metadatafields{'courserestricted'} =~ /^course\.($match_domain\_$match_courseid)$/) {
 1289:                     my $assoc_crs = $1;
 1290: 	            $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
 1291: 	            if ($env{'course.'.$assoc_crs.'.metadata.addedorder'}) {
 1292: 	                @added_order = split(/,/,$env{'course.'.$assoc_crs.'.metadata.addedorder'});
 1293: 	            }
 1294: 	            $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join(",",@fields);
 1295:                 }
 1296: 	    }
 1297: 	} else {
 1298: 	    @fields = ('author','title','subject','keywords','abstract','notes',
 1299: 		       'copyright','customdistributionfile','language',
 1300: 		       'standards',
 1301: 		       'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
 1302: 		       'obsolete','obsoletereplacement');
 1303:         }
 1304:         if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
 1305:                 $Apache::lonpublisher::metadatafields{'copyright'}=
 1306: 		    'default';
 1307:         }
 1308: 	if (($file_type eq 'portfolio') || ($file_type eq 'groups'))  {
 1309: 	    if (! $Apache::lonpublisher::metadatafields{'mime'}) {
 1310:                 ($Apache::lonpublisher::metadatafields{'mime'}) =
 1311: 		    ( $target=~/\.(\w+)$/ );
 1312: 	    }
 1313: 	    if (! $Apache::lonpublisher::metadatafields{'owner'}) {
 1314: 		$Apache::lonpublisher::metadatafields{'owner'} =
 1315: 		    $env{'user.name'}.':'.$env{'user.domain'};
 1316: 	    }
 1317: 	    if (! $Apache::lonpublisher::metadatafields{'author'}) {
 1318: 		$Apache::lonpublisher::metadatafields{'author'} =
 1319: 		    &Apache::loncommon::plainname($env{'user.name'},
 1320: 						  $env{'user.domain'});
 1321: 	    }
 1322: 	    if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
 1323: 
 1324:                 if ($file_type eq 'portfolio') {
 1325: 		    $r->print(&mt('Associated with course [_1]',
 1326: 		        '<strong><a href="'.$uri.'?changecourse=true">'.
 1327: 		        $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
 1328: 		        ".description"}.
 1329: 			      '</a></strong>').'<br />');
 1330:                 } else {
 1331:                     $r->print(&mt('Associated with course [_1]',
 1332:                         '<strong>'.
 1333:   $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
 1334:                         ".description"}.'</strong>').'<br />');
 1335:                 }
 1336: 	    } else {
 1337: 		$r->print('<a href="'.$uri.'?changecourse=true">'.&mt('This resource is not associated with a course.').'</a><br />');
 1338: 	    }
 1339: 	}
 1340: 	if (@added_order) {
 1341: 	    foreach my $field_name (@added_order) {
 1342:                 push(@fields,$field_name);
 1343:                 $lt{$field_name} = $$added_metadata_fields{$field_name};
 1344: 	    }
 1345: 	} else {
 1346:             foreach my $field_name (keys(%$added_metadata_fields)) {
 1347:                 push(@fields,$field_name);
 1348:                 $lt{$field_name} = $$added_metadata_fields{$field_name};
 1349:             }
 1350:         }
 1351:         $output .= &Apache::loncommon::start_data_table();
 1352:         my $row_alt = 1;
 1353:         foreach my $field_name (@fields) {
 1354:             if (defined($env{'form.new_'.$field_name})) {
 1355:                 my @values = &Apache::loncommon::get_env_multiple('form.new_'.$field_name);
 1356:                 my $newvalue = '';
 1357:                 foreach my $item (@values) {
 1358:                     if ($item ne '') {
 1359:                         $newvalue .= $item.',';
 1360:                     }
 1361:                 }
 1362:                 $newvalue =~ s/,$//; 
 1363:                 $Apache::lonpublisher::metadatafields{$field_name}=$newvalue;
 1364:             }
 1365:             if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
 1366: 		&& exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
 1367:                 # handle restrictions here
 1368:                 if ((($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
 1369:                     ($field_name eq 'courserestricted'))&&
 1370:                     (!($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/deleted/))){
 1371:                     
 1372:                     $output .= "\n".&Apache::loncommon::start_data_table_row();
 1373:                     $output .= ('<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
 1374:                               &prettyinput($field_name,
 1375: 				   $Apache::lonpublisher::metadatafields{$field_name},
 1376: 				                    'new_'.$field_name,'defaultmeta',
 1377: 				                    undef,undef,undef,undef,
 1378: 				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</td>');
 1379:                     $output .= &Apache::loncommon::end_data_table_row();
 1380:                  }
 1381:             } else {
 1382: 
 1383:                     $output.=(&Apache::loncommon::start_data_table_row().'<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
 1384: 			      &prettyinput($field_name,
 1385: 					   $Apache::lonpublisher::metadatafields{$field_name},
 1386: 					   'new_'.$field_name,'defaultmeta').'</td>'.&Apache::loncommon::end_data_table_row());
 1387:                
 1388:             }
 1389:         }
 1390:         $output .= &Apache::loncommon::end_data_table();
 1391: 	if ($env{'form.store'}) {
 1392:             my ($outcome,$result) = &store_metadata($fn,$uri,'store');
 1393:             $r->print($result);
 1394: 	}
 1395: 	$r->print($output.'<br /><input type="submit" name="store" value="'.
 1396:                   &mt('Save Catalog Information').'" />');
 1397: 
 1398: 	if ($file_type eq 'portfolio' || $file_type eq 'groups') {
 1399: 	    my ($port_path,$group) = &get_port_path_and_group($uri);
 1400:             if ($group ne '') {
 1401:                 $r->print('<input type="hidden" name="group" value="'.$group.'" />');
 1402:             }
 1403:             $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
 1404: 	    $r->print('</form><br /><br /><form method="post" action="'.$port_path.'">');
 1405: 	    if ($group ne '') {
 1406: 	        $r->print('<input type="hidden" name="group" value="'.$group.'" />');
 1407:             }
 1408: 	    $r->print('<input type="hidden" name="currentpath" value="'.$path.'" />'.
 1409: 		      '<input type="submit" name="cancel" value="'.&mt('Discard Edits and Return to Portfolio').'" />');
 1410: 	}
 1411:     }
 1412:     
 1413:     $r->print('</form>');
 1414: 
 1415:     return;
 1416: }
 1417: 
 1418: sub store_metadata {
 1419:     my ($fn,$uri,$caller) = @_;
 1420:     my $mfh;
 1421:     my $formname='store';
 1422:     my ($file_content,$output,$outcome);
 1423:     if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
 1424:         $Apache::lonpublisher::metadatafields{'keywords'} =
 1425:             join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
 1426:             }
 1427:     foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
 1428:         next if ($field =~ /\./);
 1429:         my $unikey=$field;
 1430:         $unikey=~/^([A-Za-z_]+)/;
 1431:         my $tag=$1;
 1432:         $tag=~tr/A-Z/a-z/;
 1433:         $file_content.= "\n\<$tag";
 1434:         foreach my $key (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
 1435:             my $value = $Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
 1436:             $value=~s/\"/\'\'/g;
 1437:             $file_content.=' '.$key.'="'.$value.'"' ;
 1438:         }
 1439:         $file_content.= '>'.
 1440:             &HTML::Entities::encode
 1441:                 ($Apache::lonpublisher::metadatafields{$unikey},'<>&"').
 1442:                 '</'.$tag.'>';
 1443:     }
 1444:     if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
 1445:         my ($path, $new_fn);
 1446:         if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
 1447:             ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
 1448:         } else {
 1449:             ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
 1450:         }
 1451:         ($outcome,my $result) = 
 1452:             &store_portfolio_metadata($formname,$file_content,
 1453:                                       $path,$new_fn,$uri,$caller);
 1454:         $output .= $result;
 1455:     } else {
 1456:         if (! ($mfh=Apache::File->new('>'.$fn))) {
 1457:             $output .= '<p><font color="red">';
 1458:             if ($caller eq 'transfer') {
 1459:                 $output .= &mt('Could not transfer data in added fields to notes');
 1460:             } else { 
 1461:                 $output .= &mt('Could not write metadata');
 1462:             }
 1463:             $output .= ', '.&mt('FAIL').'</font></p>';
 1464:             $outcome = 'fail';
 1465:         } else {
 1466:             print $mfh ($file_content);
 1467:             close($mfh);
 1468:             &update_metadata_table($uri);
 1469:             $output .= '<p><font color="blue">';
 1470:             if ($caller eq 'transfer') {
 1471:                 $output .= &mt('Transferred data in added fields to notes');
 1472:             } else {
 1473:                 $output .= &mt('Wrote Metadata');
 1474:             }
 1475:             $output .= ' '.&Apache::lonlocal::locallocaltime(time).
 1476:                        '</font></p>';
 1477:             $outcome = 'ok';
 1478:         }
 1479:     }
 1480:     return ($outcome,$output);
 1481: }
 1482: 
 1483: sub store_transferred_addedfields {
 1484:     my ($fn,$uri,$transfers) = @_;
 1485:     foreach my $item (@{$transfers}) {
 1486:         $Apache::lonpublisher::metadatafields{'notes'} .= 
 1487:            ' '.$item.' = '.$Apache::lonpublisher::metadatafields{$item};
 1488:     }
 1489:     my ($outcome,$output) = &store_metadata($fn,$uri,'transfer');
 1490:     if ($outcome eq 'ok') {
 1491:         foreach my $item (@{$transfers}) {
 1492:             delete($Apache::lonpublisher::metadatafields{$item});
 1493:         }
 1494:     }
 1495: }
 1496: 
 1497: sub store_portfolio_metadata {
 1498:     my ($formname,$content,$path,$new_fn,$uri,$caller) = @_;
 1499:     my ($outcome,$output);
 1500:     $env{'form.'.$formname}=$content."\n";
 1501:     $env{'form.'.$formname.'.filename'}=$new_fn;
 1502:     my $result =&Apache::lonnet::userfileupload($formname,'',$path);
 1503:     if ($result =~ /(error|notfound)/) {
 1504:         $output = '<p><font color="red">';
 1505:         if ($caller eq 'transfer') {
 1506:             $output .= 
 1507:                 &mt('Could not transfer data in added fields to notes'); 
 1508:         } else {
 1509:             $output .= &mt('Could not write metadata');
 1510:         }
 1511:         $output .= ', '.&mt('FAIL').'</font></p>';
 1512:         $outcome = 'fail';
 1513:     } else {
 1514:         &update_metadata_table($uri);
 1515:         $output = '<p><font color="blue">';
 1516:         if ($caller eq 'transfer') {
 1517:             $output .= &mt('Transferred data in added fields to notes');
 1518:         } else {
 1519:             $output .= &mt('Wrote Metadata');
 1520:         }
 1521:         $output .= ' '.&Apache::lonlocal::locallocaltime(time).
 1522:                    '</font></p>';
 1523:         $outcome = 'ok';
 1524:     }
 1525:     return ($outcome,$output);
 1526: }
 1527: 
 1528: sub update_metadata_table {
 1529:     my ($uri) = @_;
 1530:     my ($type,$udom,$uname,$file_name,$group) =
 1531: 	&Apache::lonnet::parse_portfolio_url($uri);
 1532:     $file_name =~ s/\.meta$//;
 1533:     my $current_permissions =
 1534:         &Apache::lonnet::get_portfile_permissions($udom,$uname);
 1535:     my %access_controls =
 1536:         &Apache::lonnet::get_access_controls($current_permissions,$group,
 1537:                                              $file_name);
 1538:     my $access_hash = $access_controls{$file_name};
 1539:     my $available = 0;
 1540:     if (ref($access_hash) eq 'HASH') {
 1541:         foreach my $key (keys(%{$access_hash})) {
 1542:             my ($num,$scope,$end,$start) =
 1543:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 1544:             if ($scope eq 'public' || $scope eq 'guest') {
 1545:                 $available = 1;
 1546:                 last;
 1547:             }
 1548:         }
 1549:     }
 1550:     if ($available) {
 1551:         my $result =
 1552:             &Apache::lonnet::update_portfolio_table($uname,$udom,
 1553:             $file_name,'portfolio_metadata',$group,'update');
 1554:     }
 1555: }
 1556: 
 1557: 
 1558: 1;
 1559: __END__
 1560: 

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