File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.168.2.2: download - view: text, annotated - select for diffs
Mon Aug 28 00:14:19 2006 UTC (17 years, 10 months ago) by albertel
Branches: version_2_2_X
Diff to branchpoint 1.168: preferred, unified
- backport 1.172

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

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