File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.81: download - view: text, annotated - select for diffs
Thu Jun 17 15:05:59 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
Have "additional metadata" look better.

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.81 2004/06/17 15:05:59 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: 
   43: 
   44: ############################################################
   45: ############################################################
   46: ##
   47: ## &get_dynamic_metadata_from_sql($url)
   48: ## 
   49: ## Queries sql database for dynamic metdata
   50: ## Returns a hash of hashes, with keys of urls which match $url
   51: ## Returned fields are given below.
   52: ##
   53: ## Examples:
   54: ## 
   55: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
   56: ##     ('/res/msu/korte/');
   57: ##
   58: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
   59: ##
   60: ############################################################
   61: ############################################################
   62: sub get_dynamic_metadata_from_sql {
   63:     my ($url) = shift();
   64:     my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
   65:     if (! defined($authordom)) {
   66:         $authordom = shift();
   67:     }
   68:     if  (! defined($author)) { 
   69:         $author = shift();
   70:     }
   71:     if (! defined($authordom) || ! defined($author)) {
   72:         return ();
   73:     }
   74:     my @Fields = ('url',
   75:                   'goto','goto_list',
   76:                   'comefrom','comefrom_list',
   77:                   'sequsage','sequsage_list',
   78:                   'stdno','stdno_list',
   79:                   'avetries','avetries_list',
   80:                   'difficulty','difficulty_list',
   81:                   'disc','disc_list',
   82:                   'clear','technical','correct',
   83:                   'helpful','depth');
   84:     #
   85:     my $query = 'SELECT '.join(',',@Fields).
   86:         ' FROM metadata WHERE url LIKE "'.$url.'%"';
   87:     my $server = &Apache::lonnet::homeserver($author,$authordom);
   88:     my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
   89:                                                 ,[$server]);
   90:     return () if (! defined($reply) || ref($reply) ne 'HASH');
   91:     my $filename = $reply->{$server};
   92:     if (! defined($filename) || $filename =~ /^error/) {
   93:         return ();
   94:     }
   95:     my $max_time = time + 10; # wait 10 seconds for results at most
   96:     my %ReturnHash;
   97:     #
   98:     # Look for results
   99:     my $finished = 0;
  100:     while (! $finished && time < $max_time) {
  101:         my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
  102:         if (! -e "$datafile.end") { next; }
  103:         my $fh;
  104:         if (!($fh=Apache::File->new($datafile))) { next; }
  105:         while (my $result = <$fh>) {
  106:             chomp($result);
  107:             next if (! $result);
  108:             my @Data = 
  109:                 map { 
  110:                     &Apache::lonnet::unescape($_); 
  111:                 } split(',',$result);
  112:             my $url = $Data[0];
  113:             for (my $i=0;$i<=$#Fields;$i++) {
  114:                 $ReturnHash{$url}->{$Fields[$i]}=$Data[$i];
  115:             }
  116:         }
  117:         $finished = 1;
  118:     }
  119:     #
  120:     return %ReturnHash;
  121: }
  122: 
  123: 
  124: # Fetch and evaluate dynamic metadata
  125: sub dynamicmeta {
  126:     my $url=&Apache::lonnet::declutter(shift);
  127:     $url=~s/\.meta$//;
  128:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
  129:     my $regexp=$url;
  130:     $regexp=~s/(\W)/\\$1/g;
  131:     $regexp='___'.$regexp.'___';
  132:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
  133: 				       $aauthor,$regexp);
  134:     my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  135:     my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  136:                                                                \%DynamicData);
  137:     #
  138:     # Deal with 'count' separately
  139:     $Data{'count'} = &access_count($url,$aauthor,$adomain);
  140:     #
  141:     # Debugging code I will probably need later
  142:     if (0) {
  143:         &Apache::lonnet::logthis('Dynamic Metadata');
  144:         while(my($k,$v)=each(%Data)){
  145:             &Apache::lonnet::logthis('    "'.$k.'"=>"'.$v.'"');
  146:         }
  147:         &Apache::lonnet::logthis('-------------------');
  148:     }
  149:     return %Data;
  150: }
  151: 
  152: sub access_count {
  153:     my ($src,$author,$adomain) = @_;
  154:     my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
  155:                                         $author,$src);
  156:     if (! exists($countdata{$src})) {
  157:         return &mt('Not Available');
  158:     } else {
  159:         return $countdata{$src};
  160:     }
  161: }
  162: 
  163: # Try to make an alt tag if there is none
  164: sub alttag {
  165:     my ($base,$src)=@_;
  166:     my $fullpath=&Apache::lonnet::hreflocation($base,$src);
  167:     my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
  168:         &Apache::lonnet::metadata($fullpath,'subject').' '.
  169:         &Apache::lonnet::metadata($fullpath,'abstract');
  170:     $alttag=~s/\s+/ /gs;
  171:     $alttag=~s/\"//gs;
  172:     $alttag=~s/\'//gs;
  173:     $alttag=~s/\s+$//gs;
  174:     $alttag=~s/^\s+//gs;
  175:     if ($alttag) { 
  176:         return $alttag; 
  177:     } else { 
  178:         return &mt('No information available'); 
  179:     }
  180: }
  181: 
  182: # Author display
  183: sub authordisplay {
  184:     my ($aname,$adom)=@_;
  185:     return &Apache::loncommon::aboutmewrapper
  186:         (&Apache::loncommon::plainname($aname,$adom),
  187:          $aname,$adom,'preview').' <tt>['.$aname.'@'.$adom.']</tt>';
  188: }
  189: 
  190: # Pretty display
  191: sub evalgraph {
  192:     my $value=shift;
  193:     if (! $value) { 
  194:         return '';
  195:     }
  196:     my $val=int($value*10.+0.5)-10;
  197:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  198:     if ($val>=20) {
  199: 	$output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  200:     } else {
  201:         $output.='<td width="'.($val).'" bgcolor="#555555">&nbsp;</td>'.
  202:                  '<td width="'.(20-$val).'" bgcolor="#FF3333">&nbsp;</td>';
  203:     }
  204:     $output.='<td bgcolor="#FFFF33">&nbsp;</td>';
  205:     if ($val>20) {
  206: 	$output.='<td width="'.($val-20).'" bgcolor="#33FF33">&nbsp;</td>'.
  207:                  '<td width="'.(40-$val).'" bgcolor="#555555">&nbsp;</td>';
  208:     } else {
  209:         $output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  210:     }
  211:     $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
  212:     return $output;
  213: }
  214: 
  215: sub diffgraph {
  216:     my $value=shift;
  217:     if (! $value) { 
  218:         return '';
  219:     }
  220:     my $val=int(40.0*$value+0.5);
  221:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
  222:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
  223:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  224:     for (my $i=0;$i<8;$i++) {
  225: 	if ($val>$i*5) {
  226:             $output.='<td width="5" bgcolor="'.$colors[$i].'">&nbsp;</td>';
  227:         } else {
  228: 	    $output.='<td width="5" bgcolor="#555555">&nbsp;</td>';
  229: 	}
  230:     }
  231:     $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
  232:     return $output;
  233: }
  234: 
  235: 
  236: # The field names
  237: sub fieldnames {
  238:     return &Apache::lonlocal::texthash
  239:         (
  240:          'title' => 'Title',
  241:          'author' =>'Author(s)',
  242:          'authorspace' => 'Author Space',
  243:          'modifyinguser' => 'Last Modifying User',
  244:          'subject' => 'Subject',
  245:          'keywords' => 'Keyword(s)',
  246:          'notes' => 'Notes',
  247:          'abstract' => 'Abstract',
  248:          'lowestgradelevel' => 'Lowest Grade Level',
  249:          'highestgradelevel' => 'Highest Grade Level',
  250:          'standards' => 'Standards',
  251:          'mime' => 'MIME Type',
  252:          'language' => 'Language',
  253:          'creationdate' => 'Creation Date',
  254:          'lastrevisiondate' => 'Last Revision Date',
  255:          'owner' => 'Publisher/Owner',
  256:          'copyright' => 'Copyright/Distribution',
  257:          'customdistributionfile' => 'Custom Distribution File',
  258:          'sourceavail' => 'Source Availible',
  259:          'sourcerights' => 'Source Custom Distribution File',
  260:          'obsolete' => 'Obsolete',
  261:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
  262:          'count'      => 'Network-wide number of accesses (hits)',
  263:          'course'     => 'Network-wide number of courses using resource',
  264:          'course_list' => 'Network-wide courses using resource',
  265:          'sequsage'      => 'Number of resources using or importing resource',
  266:          'sequsage_list' => 'Resources using or importing resource',
  267:          'goto'       => 'Number of resources that follow this resource in maps',
  268:          'goto_list'  => 'Resources that follow this resource in maps',
  269:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
  270:          'comefrom_list' => 'Resources that lead up to this resource in maps',
  271:          'clear'      => 'Material presented in clear way',
  272:          'depth'      => 'Material covered with sufficient depth',
  273:          'helpful'    => 'Material is helpful',
  274:          'correct'    => 'Material appears to be correct',
  275:          'technical'  => 'Resource is technically correct', 
  276:          'avetries'   => 'Average number of tries till solved',
  277:          'stdno'      => 'Total number of students who have worked on this problem',
  278:          'difficulty' => 'Degree of difficulty',
  279:          'disc'       => 'Degree of discrimination',
  280:          );
  281: }
  282: 
  283: # Pretty printing of metadata field
  284: 
  285: sub prettyprint {
  286:     my ($type,$value)=@_;
  287:     if (! defined($value)) { 
  288:         return '&nbsp;'; 
  289:     }
  290:     # Title
  291:     if ($type eq 'title') {
  292: 	return '<font size="+1" face="arial">'.$value.'</font>';
  293:     }
  294:     # Dates
  295:     if (($type eq 'creationdate') ||
  296: 	($type eq 'lastrevisiondate')) {
  297: 	return ($value?&Apache::lonlocal::locallocaltime(
  298: 			  &Apache::lonmysql::unsqltime($value)):
  299: 		&mt('not available'));
  300:     }
  301:     # Language
  302:     if ($type eq 'language') {
  303: 	return &Apache::loncommon::languagedescription($value);
  304:     }
  305:     # Copyright
  306:     if ($type eq 'copyright') {
  307: 	return &Apache::loncommon::copyrightdescription($value);
  308:     }
  309:     # Copyright
  310:     if ($type eq 'sourceavail') {
  311: 	return &Apache::loncommon::source_copyrightdescription($value);
  312:     }
  313:     # MIME
  314:     if ($type eq 'mime') {
  315:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
  316:             &Apache::loncommon::filedescription($value);
  317:     }
  318:     # Person
  319:     if (($type eq 'author') || 
  320: 	($type eq 'owner') ||
  321: 	($type eq 'modifyinguser') ||
  322: 	($type eq 'authorspace')) {
  323: 	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  324: 	return $value;
  325:     }
  326:     # Gradelevel
  327:     if (($type eq 'lowestgradelevel') ||
  328: 	($type eq 'highestgradelevel')) {
  329: 	return &Apache::loncommon::gradeleveldescription($value);
  330:     }
  331:     # Only for advance users below
  332:     if (! $ENV{'user.adv'}) { 
  333:         return '<i>- '.&mt('not displayed').' -</i>';
  334:     }
  335:     # File
  336:     if (($type eq 'customdistributionfile') ||
  337: 	($type eq 'obsoletereplacement') ||
  338: 	($type eq 'goto_list') ||
  339: 	($type eq 'comefrom_list') ||
  340: 	($type eq 'sequsage_list')) {
  341: 	return join('<br />',map {
  342:             my $url = &Apache::lonnet::clutter($_);
  343:             my $title = &Apache::lonnet::gettitle($url);
  344:             if ($title eq '') {
  345:                 $title = 'Untitled';
  346:                 if ($url =~ /\.sequence$/) {
  347:                     $title .= ' Sequence';
  348:                 } elsif ($url =~ /\.page$/) {
  349:                     $title .= ' Page';
  350:                 } elsif ($url =~ /\.problem$/) {
  351:                     $title .= ' Problem';
  352:                 } elsif ($url =~ /\.html$/) {
  353:                     $title .= ' HTML document';
  354:                 } elsif ($url =~ m:/syllabus$:) {
  355:                     $title .= ' Syllabus';
  356:                 } 
  357:             }
  358:             $_ = '<b>'.$title.'</b> '.
  359:                 '<a href="'.$url.'" target="preview">'.
  360:                 '<font size="-1">'.$url.'</font>'.
  361:                 '</a>'
  362:         } split(/\s*\,\s*/,$value));
  363:     }
  364:     # Evaluations
  365:     if (($type eq 'clear') ||
  366: 	($type eq 'depth') ||
  367: 	($type eq 'helpful') ||
  368: 	($type eq 'correct') ||
  369: 	($type eq 'technical')) {
  370: 	return &evalgraph($value);
  371:     }
  372:     # Difficulty
  373:     if ($type eq 'difficulty' || $type eq 'disc') {
  374: 	return &diffgraph($value);
  375:     }
  376:     # List of courses
  377:     if ($type=~/\_list/) {
  378:         my @Courses = split(/\s*\,\s*/,$value);
  379:         my $Str;
  380:         foreach my $course (@Courses) {
  381:             my %courseinfo = &Apache::lonnet::coursedescription($course);
  382:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  383:                 next;
  384:             }
  385:             if ($Str ne '') { $Str .= '<br />'; }
  386:             $Str .= '<a href="/public/'.$courseinfo{'domain'}.'/'.
  387:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
  388:                 $courseinfo{'description'}.'</a>';
  389:         }
  390: 	return $Str;
  391:     }
  392:     # No pretty print found
  393:     return $value;
  394: }
  395: 
  396: # Pretty input of metadata field
  397: sub direct {
  398:     return shift;
  399: }
  400: 
  401: sub selectbox {
  402:     my ($name,$value,$functionref,@idlist)=@_;
  403:     if (! defined($functionref)) {
  404:         $functionref=\&direct;
  405:     }
  406:     my $selout='<select name="'.$name.'">';
  407:     foreach (@idlist) {
  408:         $selout.='<option value=\''.$_.'\'';
  409:         if ($_ eq $value) {
  410: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  411: 	}
  412:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  413:     }
  414:     return $selout.'</select>';
  415: }
  416: 
  417: sub relatedfield {
  418:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
  419:     if (! $relatedsearchflag) { 
  420:         return '';
  421:     }
  422:     if (! defined($relatedsep)) {
  423:         $relatedsep=' ';
  424:     }
  425:     if (! $show) {
  426:         return $relatedsep.'&nbsp;';
  427:     }
  428:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
  429: 	($relatedvalue?' checked="1"':'').' />';
  430: }
  431: 
  432: sub prettyinput {
  433:     my ($type,$value,$fieldname,$formname,
  434: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size)=@_;
  435:     if (! defined($size)) {
  436:         $size = 80;
  437:     }
  438:     # Language
  439:     if ($type eq 'language') {
  440: 	return &selectbox($fieldname,
  441: 			  $value,
  442: 			  \&Apache::loncommon::languagedescription,
  443: 			  (&Apache::loncommon::languageids)).
  444:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  445:     }
  446:     # Copyright
  447:     if ($type eq 'copyright') {
  448: 	return &selectbox($fieldname,
  449: 			  $value,
  450: 			  \&Apache::loncommon::copyrightdescription,
  451: 			  (&Apache::loncommon::copyrightids)).
  452:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  453:     }
  454:     # Source Copyright
  455:     if ($type eq 'sourceavail') {
  456: 	return &selectbox($fieldname,
  457: 			  $value,
  458: 			  \&Apache::loncommon::source_copyrightdescription,
  459: 			  (&Apache::loncommon::source_copyrightids)).
  460:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  461:     }
  462:     # Gradelevels
  463:     if (($type eq 'lowestgradelevel') ||
  464: 	($type eq 'highestgradelevel')) {
  465: 	return &Apache::loncommon::select_level_form($value,$fieldname).
  466:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  467:     }
  468:     # Obsolete
  469:     if ($type eq 'obsolete') {
  470: 	return '<input type="checkbox" name="'.$fieldname.'"'.
  471: 	    ($value?' checked="1"':'').' />'.
  472:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  473:     }
  474:     # Obsolete replacement file
  475:     if ($type eq 'obsoletereplacement') {
  476: 	return '<input type="text" name="'.$fieldname.
  477: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  478: 	    "('".$formname."','".$fieldname."'".
  479: 	    ",'')\">".&mt('Select').'</a>'.
  480:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  481:     }
  482:     # Customdistribution file
  483:     if ($type eq 'customdistributionfile') {
  484: 	return '<input type="text" name="'.$fieldname.
  485: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  486: 	    "('".$formname."','".$fieldname."'".
  487: 	    ",'rights')\">".&mt('Select').'</a>'.
  488:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  489:     }
  490:     # Source Customdistribution file
  491:     if ($type eq 'sourcerights') {
  492: 	return '<input type="text" name="'.$fieldname.
  493: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  494: 	    "('".$formname."','".$fieldname."'".
  495: 	    ",'rights')\">".&mt('Select').'</a>'.
  496:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  497:     }
  498:     # Dates
  499:     if (($type eq 'creationdate') ||
  500: 	($type eq 'lastrevisiondate')) {
  501: 	return 
  502:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
  503:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  504:     }
  505:     # No pretty input found
  506:     $value=~s/^\s+//gs;
  507:     $value=~s/\s+$//gs;
  508:     $value=~s/\s+/ /gs;
  509:     $value=~s/\"/\&quot\;/gs;
  510:     return 
  511:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  512:         'value="'.$value.'" />'.
  513:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  514:                       $relatedvalue); 
  515: }
  516: 
  517: # Main Handler
  518: sub handler {
  519:     my $r=shift;
  520:     #
  521:     my $uri=$r->uri;
  522:     #
  523:     # Set document type
  524:     &Apache::loncommon::content_type($r,'text/html');
  525:     $r->send_http_header;
  526:     return OK if $r->header_only;
  527:     #
  528:     my ($resdomain,$resuser)=
  529:         (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
  530:     $r->print('<html><head><title>'.
  531:               'Catalog Information'.
  532:               '</title></head>');
  533:     if ($uri=~m:/adm/bombs/(.*)$:) {
  534:         $r->print(&Apache::loncommon::bodytag('Error Messages'));
  535:         # Looking for all bombs?
  536:         &report_bombs($r,$uri);
  537:     } elsif ($uri=~/^\/\~/) { 
  538:         # Construction space
  539:         $r->print(&Apache::loncommon::bodytag
  540:                   ('Edit Catalog Information','','','',$resdomain));
  541:         &present_editable_metadata($r,$uri);
  542:     } else {
  543:         $r->print(&Apache::loncommon::bodytag
  544:                   ('Catalog Information','','','',$resdomain));
  545:         &present_uneditable_metadata($r,$uri);
  546:     }
  547:     $r->print('</body></html>');
  548:     return OK;
  549: }
  550: 
  551: #####################################################
  552: #####################################################
  553: ###                                               ###
  554: ###                Report Bombs                   ###
  555: ###                                               ###
  556: #####################################################
  557: #####################################################
  558: sub report_bombs {
  559:     my ($r,$uri) = @_;
  560:     # Set document type
  561:     $uri =~ s:/adm/bombs/::;
  562:     $uri = &Apache::lonnet::declutter($uri);
  563:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  564:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
  565:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  566:         my %brokenurls = 
  567:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  568:         foreach (sort(keys(%brokenurls))) {
  569:             if ($_=~/^\Q$uri\E/) {
  570:                 $r->print
  571:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  572:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  573:                      '<hr />');
  574:             }
  575:         }
  576:     } else {
  577:         $r->print(&mt('Not authorized'));
  578:     }
  579:     return;
  580: }
  581: 
  582: #####################################################
  583: #####################################################
  584: ###                                               ###
  585: ###        Uneditable Metadata Display            ###
  586: ###                                               ###
  587: #####################################################
  588: #####################################################
  589: sub present_uneditable_metadata {
  590:     my ($r,$uri) = @_;
  591:     #
  592:     my %content=();
  593:     # Read file
  594:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  595:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  596:     }
  597:     # Render Output
  598:     # displayed url
  599:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  600:     $uri=~s/\.meta$//;
  601:     my $disuri=&Apache::lonnet::clutter($uri);
  602:     # version
  603:     my $currentversion=&Apache::lonnet::getversion($disuri);
  604:     my $versiondisplay='';
  605:     if ($thisversion) {
  606:         $versiondisplay=&mt('Version').': '.$thisversion.
  607:             ' ('.&mt('most recent version').': '.
  608:             ($currentversion>0 ? 
  609:              $currentversion   :
  610:              &mt('information not available')).')';
  611:     } else {
  612:         $versiondisplay='Version: '.$currentversion;
  613:     }
  614:     # crumbify displayed URL               uri     target prefix form  size
  615:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  616:     $disuri =~ s:<br />::g;
  617:     # obsolete
  618:     my $obsolete=$content{'obsolete'};
  619:     my $obsoletewarning='';
  620:     if (($obsolete) && ($ENV{'user.adv'})) {
  621:         $obsoletewarning='<p><font color="red">'.
  622:             &mt('This resource has been marked obsolete by the author(s)').
  623:             '</font></p>';
  624:     }
  625:     #
  626:     my %lt=&fieldnames();
  627:     my $table='';
  628:     my $title = $content{'title'};
  629:     if (! defined($title)) {
  630:         $title = 'Untitled Resource';
  631:     }
  632:     foreach ('title', 
  633:              'author', 
  634:              'subject', 
  635:              'keywords', 
  636:              'notes', 
  637:              'abstract',
  638:              'lowestgradelevel',
  639:              'highestgradelevel',
  640:              'standards', 
  641:              'mime', 
  642:              'language', 
  643:              'creationdate', 
  644:              'lastrevisiondate', 
  645:              'owner', 
  646:              'copyright', 
  647:              'customdistributionfile',
  648:              'sourceavail',
  649:              'sourcerights', 
  650:              'obsolete', 
  651:              'obsoletereplacement') {
  652:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
  653:             '</td><td bgcolor="#CCCCCC">'.
  654:             &prettyprint($_,$content{$_}).'</td></tr>';
  655:         delete $content{$_};
  656:     }
  657:     #
  658:     $r->print(<<ENDHEAD);
  659: <h2>$title</h2>
  660: <p>
  661: $disuri<br />
  662: $obsoletewarning
  663: $versiondisplay
  664: </p>
  665: <table cellspacing=2 border=0>
  666: $table
  667: </table>
  668: ENDHEAD
  669:     if ($ENV{'user.adv'}) {
  670:         &print_dynamic_metadata($r,$uri,\%content);
  671:     }
  672:     return;
  673: }
  674: 
  675: sub print_dynamic_metadata {
  676:     my ($r,$uri,$content) = @_;
  677:     #
  678:     my %content = %$content;
  679:     my %lt=&fieldnames();
  680:     #
  681:     my $description = 'Dynamic Metadata (updated periodically)';
  682:     $r->print('<h3>'.&mt($description).'</h3>'.
  683:               &mt('Processing'));
  684:     $r->rflush();
  685:     my %items=&fieldnames();
  686:     my %dynmeta=&dynamicmeta($uri);
  687:     #
  688:     # General Access and Usage Statistics
  689:     if (exists($dynmeta{'count'}) ||
  690:         exists($dynmeta{'sequsage'}) ||
  691:         exists($dynmeta{'comefrom'}) ||
  692:         exists($dynmeta{'goto'}) ||
  693:         exists($dynmeta{'course'})) {
  694:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  695:                   '<table cellspacing=2 border=0>');
  696:         foreach ('count',
  697:                  'sequsage','sequsage_list',
  698:                  'comefrom','comefrom_list',
  699:                  'goto','goto_list',
  700:                  'course','course_list') {
  701:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  702:                       '<td bgcolor="#CCCCCC">'.
  703:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  704:         }
  705:         $r->print('</table>');
  706:     } else {
  707:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
  708:     }
  709:     #
  710:     # Assessment statistics
  711:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  712:         if (exists($dynmeta{'stdno'}) ||
  713:             exists($dynmeta{'avetries'}) ||
  714:             exists($dynmeta{'difficulty'}) ||
  715:             exists($dynmeta{'disc'})) {
  716:             # This is an assessment, print assessment data
  717:             $r->print('<h4>'.
  718:                       &mt('Overall Assessment Statistical Data').
  719:                       '</h4>'.
  720:                       '<table cellspacing=2 border=0>');
  721:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
  722:                       '<td bgcolor="#CCCCCC">'.
  723:                       &prettyprint('stdno',$dynmeta{'stdno'}).
  724:                       '</td>'."</tr>\n");
  725:             foreach ('avetries','difficulty','disc') {
  726:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  727:                           '<td bgcolor="#CCCCCC">'.
  728:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
  729:                           '</td>'."</tr>\n");
  730:             }
  731:             $r->print('</table>');    
  732:         }
  733:         if (exists($dynmeta{'stats'})) {
  734:             #
  735:             # New assessment statistics
  736:             $r->print('<h4>'.
  737:                       &mt('Detailed Assessment Statistical Data').
  738:                       '</h4>');
  739:             my $table = '<table cellspacing=2 border=0>'.
  740:                 '<tr>'.
  741:                 '<th>Course</th>'.
  742:                 '<th>Section(s)</th>'.
  743:                 '<th>Num Students</th>'.
  744:                 '<th>Mean Tries</th>'.
  745:                 '<th>Degree of Difficulty</th>'.
  746:                 '<th>Degree of Discrimination</th>'.
  747:                 '<th>Time of computation</th>'.
  748:                 '</tr>'.$/;
  749:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
  750:                 my $data = $dynmeta{'stats'}->{$identifier};
  751:                 my $course = $data->{'course'};
  752:                 my %courseinfo = &Apache::lonnet::coursedescription($course);
  753:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  754:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
  755:                     next;
  756:                 }
  757:                 $table .= '<tr>';
  758:                 $table .= 
  759:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
  760:                 $table .= 
  761:                     '<td align="right">'.$data->{'sections'}.'</td>';
  762:                 $table .=
  763:                     '<td align="right">'.$data->{'stdno'}.'</td>';
  764:                 foreach ('avetries','difficulty','disc') {
  765:                     $table .= '<td align="right">';
  766:                     if (exists($data->{$_})) {
  767:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
  768:                     } else {
  769:                         $table .= '';
  770:                     }
  771:                     $table .= '</td>';
  772:                 }
  773:                 $table .=
  774:                     '<td><nobr>'.
  775:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
  776:                     '</nobr></td>';
  777:                 $table .=
  778:                     '</tr>'.$/;
  779:             }
  780:             $table .= '</table>'.$/;
  781:             $r->print($table);
  782:         } else {
  783:             $r->print('No new dynamic data found.');
  784:         }
  785:     } else {
  786:         $r->print('<h4>'.
  787:           &mt('No Assessment Statistical Data is available for this resource').
  788:                   '</h4>');
  789:     }
  790: 
  791:     #
  792:     #
  793:     if (exists($dynmeta{'clear'})   || 
  794:         exists($dynmeta{'depth'})   || 
  795:         exists($dynmeta{'helpful'}) || 
  796:         exists($dynmeta{'correct'}) || 
  797:         exists($dynmeta{'technical'})){ 
  798:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
  799:                   '<table cellspacing=2 border=0>');
  800:         foreach ('clear','depth','helpful','correct','technical') {
  801:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  802:                       '<td bgcolor="#CCCCCC">'.
  803:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  804:         }
  805:         $r->print('</table>');
  806:     } else {
  807:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
  808:     }
  809:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
  810:     if ((($ENV{'user.domain'} eq $1) && ($ENV{'user.name'} eq $2))
  811:         || ($ENV{'user.role.ca./'.$1.'/'.$2})) {
  812:         if (exists($dynmeta{'comments'})) {
  813:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
  814:                       &mt('visible to author and co-authors only').
  815:                       ')</h4>'.
  816:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  817:         } else {
  818:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
  819:         }
  820:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
  821:         if (defined($bombs) && $bombs ne '') {
  822:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
  823:                       &mt('visible to author and co-authors only').')'.
  824:                       '</h4>'.$bombs);
  825:         } else {
  826:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
  827:         }
  828:     }
  829:     #
  830:     # All other stuff
  831:     $r->print('<h3>'.
  832:               &mt('Additional Metadata (non-standard, parameters, exports)').
  833:               '</h3><table border="0" cellspacing="1">');
  834:     foreach (sort(keys(%content))) {
  835:         my $name=$_;
  836:         if ($name!~/\.display$/) {
  837:             my $display=&Apache::lonnet::metadata($uri,
  838:                                                   $name.'.display');
  839:             if (! $display) { 
  840:                 $display=$name;
  841:             };
  842:             my $otherinfo='';
  843:             foreach ('name','part','type','default') {
  844:                 if (defined(&Apache::lonnet::metadata($uri,
  845:                                                       $name.'.'.$_))) {
  846:                     $otherinfo.=' '.$_.'='.
  847:                         &Apache::lonnet::metadata($uri,
  848:                                                   $name.'.'.$_).'; ';
  849:                 }
  850:             }
  851:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
  852:             if ($otherinfo) {
  853:                 $r->print(' ('.$otherinfo.')');
  854:             }
  855:             $r->print("</font></td></tr>\n");
  856:         }
  857:     }
  858:     $r->print("</table>");
  859:     return;
  860: }
  861: 
  862: #####################################################
  863: #####################################################
  864: ###                                               ###
  865: ###          Editable metadata display            ###
  866: ###                                               ###
  867: #####################################################
  868: #####################################################
  869: sub present_editable_metadata {
  870:     my ($r,$uri) = @_;
  871:     # Construction Space Call
  872:     # Header
  873:     my $disuri=$uri;
  874:     my $fn=&Apache::lonnet::filelocation('',$uri);
  875:     $disuri=~s/^\/\~/\/priv\//;
  876:     $disuri=~s/\.meta$//;
  877:     my $target=$uri;
  878:     $target=~s/^\/\~/\/res\/$ENV{'request.role.domain'}\//;
  879:     $target=~s/\.meta$//;
  880:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
  881:     if ($bombs) {
  882:         if ($ENV{'form.delmsg'}) {
  883:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
  884:                 $bombs=&mt('Messages deleted.');
  885:             } else {
  886:                 $bombs=&mt('Error deleting messages');
  887:             }
  888:         }
  889:         my $del=&mt('Delete Messages');
  890:         $r->print(<<ENDBOMBS);
  891: <h1>$disuri</h1>
  892: <form method="post" name="defaultmeta">
  893: <input type="submit" name="delmsg" value="$del" />
  894: <br />$bombs
  895: ENDBOMBS
  896:     } else {
  897:         my $displayfile='Catalog Information for '.$disuri;
  898:         if ($disuri=~/\/default$/) {
  899:             my $dir=$disuri;
  900:             $dir=~s/default$//;
  901:             $displayfile=
  902:                 &mt('Default Cataloging Information for Directory').' '.
  903:                 $dir;
  904:         }
  905:         my $bodytag=
  906:             &Apache::loncommon::bodytag('Edit Catalog Information');
  907:         %Apache::lonpublisher::metadatafields=();
  908:         %Apache::lonpublisher::metadatakeys=();
  909:         &Apache::lonpublisher::metaeval(&Apache::lonnet::getfile($fn));
  910:         $r->print(<<ENDEDIT);
  911: <html><head><title>Edit Catalog Information</title></head>
  912: $bodytag
  913: <h1>$displayfile</h1>
  914: <form method="post" name="defaultmeta">
  915: ENDEDIT
  916:         $r->print('<script language="JavaScript">'.
  917:                   &Apache::loncommon::browser_and_searcher_javascript.
  918:                   '</script>');
  919:         my %lt=&fieldnames();
  920:         foreach ('author','title','subject','keywords','abstract','notes',
  921:                  'copyright','customdistributionfile','language',
  922:                  'standards',
  923:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
  924:                  'obsolete','obsoletereplacement') {
  925:             if (defined($ENV{'form.new_'.$_})) {
  926:                 $Apache::lonpublisher::metadatafields{$_}=
  927:                     $ENV{'form.new_'.$_};
  928:             }
  929:             if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
  930:                 $Apache::lonpublisher::metadatafields{'copyright'}=
  931:                     'default';
  932:             }
  933:             $r->print('<p>'.$lt{$_}.': '.
  934:                       &prettyinput
  935:                       ($_,$Apache::lonpublisher::metadatafields{$_},
  936:                        'new_'.$_,'defaultmeta').'</p>');
  937:         }
  938:         if ($ENV{'form.store'}) {
  939:             my $mfh;
  940:             if (!  ($mfh=Apache::File->new('>'.$fn))) {
  941:                 $r->print('<p><font color=red>'.
  942:                           &mt('Could not write metadata').', '.
  943:                           &mt('FAIL').'</font>');
  944:             } else {
  945:                 foreach (sort keys %Apache::lonpublisher::metadatafields) {
  946:                     next if ($_ =~ /\./);
  947:                     my $unikey=$_;
  948:                     $unikey=~/^([A-Za-z]+)/;
  949:                     my $tag=$1;
  950:                     $tag=~tr/A-Z/a-z/;
  951:                     print $mfh "\n\<$tag";
  952:                     foreach (split(/\,/,
  953:                                  $Apache::lonpublisher::metadatakeys{$unikey})
  954:                              ) {
  955:                         my $value=
  956:                          $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
  957:                         $value=~s/\"/\'\'/g;
  958:                         print $mfh ' '.$_.'="'.$value.'"';
  959:                     }
  960:                     print $mfh '>'.
  961:                         &HTML::Entities::encode
  962:                         ($Apache::lonpublisher::metadatafields{$unikey},
  963:                          '<>&"').
  964:                          '</'.$tag.'>';
  965:                 }
  966:                 $r->print('<p>'.&mt('Wrote Metadata'));
  967:             }
  968:         }
  969:         $r->print('<br /><input type="submit" name="store" value="'.
  970:                   &mt('Store Catalog Information').'">');
  971:     }
  972:     $r->print('</form>');
  973:     return;
  974: }
  975: 
  976: 1;
  977: __END__

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