File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.157: download - view: text, annotated - select for diffs
Tue May 30 12:46:09 2006 UTC (18 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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