File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.75: download - view: text, annotated - select for diffs
Mon Apr 19 16:47:43 2004 UTC (20 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
&pretty_input now has a default value for $size.  What a concept.

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

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