File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.30: download - view: text, annotated - select for diffs
Mon Apr 14 13:46:53 2003 UTC (21 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Starts displaying new linking information

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.30 2003/04/14 13:46:53 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: # (TeX Content Handler
   29: #
   30: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   31: #
   32: # 10/19,10/21,10/23,11/27,08/09/01,12/22,12/24,12/25 Gerd Kortemeyer
   33: 
   34: package Apache::lonmeta;
   35: 
   36: use strict;
   37: use Apache::Constants qw(:common);
   38: use Apache::lonnet();
   39: use Apache::loncommon();
   40: use Apache::lonmsg;
   41: use Apache::lonpublisher;
   42: 
   43: # ----------------------------------------- Fetch and evaluate dynamic metadata
   44: 
   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 %sum=();
   55:     my %cnt=();
   56:     my %concat=();
   57:     my %listitems=('count'        => 'add',
   58:                    'course'       => 'add',
   59:                    'goto'         => 'add',
   60:                    'comefrom'     => 'add',
   61:                    'avetries'     => 'avg',
   62:                    'stdno'        => 'add',
   63:                    'difficulty'   => 'avg',
   64:                    'clear'        => 'avg',
   65:                    'technical'    => 'avg',
   66:                    'helpful'      => 'avg',
   67:                    'correct'      => 'avg',
   68:                    'depth'        => 'avg',
   69:                    'comments'     => 'app',
   70:                    'usage'        => 'cnt'
   71:                    );
   72:     foreach (keys %evaldata) {
   73: 	my ($item,$purl,$cat)=split(/\_\_\_/,$_);
   74: ###	print "\n".$_.' - '.$item.'<br />';
   75:         if (defined($cnt{$cat})) { $cnt{$cat}++; } else { $cnt{$cat}=1; }
   76:         unless ($listitems{$cat} eq 'app') {
   77:             if (defined($sum{$cat})) {
   78:                $sum{$cat}+=$evaldata{$_};
   79:                $concat{$cat}.=','.$item;
   80: 	    } else {
   81:                $sum{$cat}=$evaldata{$_};
   82:                $concat{$cat}=$item;
   83: 	    }
   84:         } else {
   85:             if (defined($sum{$cat})) {
   86:                if ($evaldata{$_}) {
   87:                   $sum{$cat}.='<hr>'.$evaldata{$_};
   88: 	       }
   89:  	    } else {
   90: 	       $sum{$cat}=''.$evaldata{$_};
   91: 	    }
   92: 	}
   93:     }
   94:     my %returnhash=();
   95:     foreach (keys %cnt) {
   96:        if ($listitems{$_} eq 'avg') {
   97: 	   $returnhash{$_}=int(($sum{$_}/$cnt{$_})*100.0+0.5)/100.0;
   98:        } elsif ($listitems{$_} eq 'cnt') {
   99:            $returnhash{$_}=$cnt{$_};
  100:        } else {
  101:            $returnhash{$_}=$sum{$_};
  102:        }
  103:        $returnhash{$_.'_list'}=$concat{$_};
  104: ### print "\n<hr />".$_.': '.$returnhash{$_}.'<br />'.$returnhash{$_.'_list'};
  105:     }
  106:     return %returnhash;
  107: }
  108: 
  109: # ------------------------------------- Try to make an alt tag if there is none
  110: 
  111: sub alttag {
  112:     my ($base,$src)=@_;
  113:     my $fullpath=&Apache::lonnet::hreflocation($base,$src);
  114:     my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
  115:                &Apache::lonnet::metadata($fullpath,'subject').' '.
  116:                &Apache::lonnet::metadata($fullpath,'abstract');
  117:     $alttag=~s/\s+/ /gs;
  118:     $alttag=~s/\"//gs;
  119:     $alttag=~s/\'//gs;
  120:     $alttag=~s/\s+$//gs;
  121:     $alttag=~s/^\s+//gs;
  122:     if ($alttag) { return $alttag; } else 
  123:                  { return 'No information available'; }
  124: }
  125: 
  126: # -------------------------------------------------------------- Author display
  127: 
  128: sub authordisplay {
  129:     my ($aname,$adom)=@_;
  130:     return &Apache::loncommon::aboutmewrapper(
  131:                 &Apache::loncommon::plainname($aname,$adom),
  132:                     $aname,$adom).' <tt>['.$aname.'@'.$adom.']</tt>';
  133: }
  134: 
  135: # -------------------------------------------------------------- Pretty display
  136: 
  137: sub evalgraph {
  138:     my $value=shift;
  139:     unless ($value) { return ''; }
  140:     my $val=int($value*10.+0.5)-10;
  141:     my $output='<table border=0 cellpadding=0 cellspacing=0><tr>';
  142:     if ($val>=20) {
  143: 	$output.='<td width=20 bgcolor="#555555">&nbsp&nbsp;</td>';
  144:     } else {
  145:         $output.='<td width='.($val).' bgcolor="#555555">&nbsp;</td>'.
  146:                  '<td width='.(20-$val).' bgcolor="#FF3333">&nbsp;</td>';
  147:     }
  148:     $output.='<td bgcolor="#FFFF33">&nbsp;</td>';
  149:     if ($val>20) {
  150: 	$output.='<td width='.($val-20).' bgcolor="#33FF33">&nbsp;</td>'.
  151:                  '<td width='.(40-$val).' bgcolor="#555555">&nbsp;</td>';
  152:     } else {
  153:        $output.='<td width=20 bgcolor="#555555">&nbsp&nbsp;</td>';
  154:     }
  155:     $output.='<td> ('.$value.') </td></tr></table>';
  156:     return $output;
  157: }
  158: 
  159: sub diffgraph {
  160:     my $value=shift;
  161:     unless ($value) { return ''; }
  162:     my $val=int(40.0*$value+0.5);
  163:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
  164:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
  165:     my $output='<table border=0 cellpadding=0 cellspacing=0><tr>';
  166:     for (my $i=0;$i<8;$i++) {
  167: 	if ($val>$i*5) {
  168:             $output.='<td width=5 bgcolor="'.$colors[$i].'">&nbsp;</td>';
  169:         } else {
  170: 	    $output.='<td width=5 bgcolor="#555555">&nbsp;</td>';
  171: 	}
  172:     }
  173:     $output.='<td> ('.$value.') </td></tr></table>';
  174:     return $output;
  175: }
  176: 
  177: # ================================================================ Main Handler
  178: 
  179: sub handler {
  180:   my $r=shift;
  181: 
  182:     my $loaderror=&Apache::lonnet::overloaderror($r);
  183:     if ($loaderror) { return $loaderror; }
  184: 
  185: 
  186:     my $uri=$r->uri;
  187: 
  188:   unless ($uri=~/^\/\~/) { 
  189: # =========================================== This is not in construction space
  190:     my ($resdomain,$resuser)=
  191:            (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
  192: 
  193:     $loaderror=
  194:        &Apache::lonnet::overloaderror($r,
  195:          &Apache::lonnet::homeserver($resuser,$resdomain));
  196:     if ($loaderror) { return $loaderror; }
  197: 
  198:   my %content=();
  199: 
  200: # ----------------------------------------------------------- Set document type
  201: 
  202:   $r->content_type('text/html');
  203:   $r->send_http_header;
  204: 
  205:   return OK if $r->header_only;
  206: 
  207: # ------------------------------------------------------------------- Read file
  208:   foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  209:       $content{$_}=&Apache::lonnet::metadata($uri,$_);
  210:   }
  211: # ------------------------------------------------------------------ Hide stuff
  212: 
  213:   unless ($ENV{'user.adv'}) {
  214:       foreach ('keywords','notes','abstract','subject') {
  215:           $content{$_}='<i>- not displayed -</i>';
  216:       }
  217:   }
  218: 
  219: # --------------------------------------------------------------- Render Output
  220:   my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  221: my $creationdate=localtime(
  222:  &Apache::loncommon::unsqltime($content{'creationdate'}));
  223: my $lastrevisiondate=localtime(
  224:  &Apache::loncommon::unsqltime($content{'lastrevisiondate'}));
  225: my $language=&Apache::loncommon::languagedescription($content{'language'});
  226: my $mime=&Apache::loncommon::filedescription($content{'mime'}); 
  227: my $disuri=&Apache::lonnet::declutter($uri);
  228:   $disuri=~s/\.meta$//;
  229: my $currentversion=&Apache::lonnet::getversion($disuri);
  230: my $author=$content{'author'};
  231: $author=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  232: my $owner=$content{'owner'};
  233: $owner=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  234: my $versiondisplay='';
  235: if ($thisversion) {
  236:     $versiondisplay='Version: '.$thisversion.
  237:     ' (most recent version: '.$currentversion.')';
  238: } else {
  239:     $versiondisplay='Version: '.$currentversion;
  240: }
  241: my $customdistributionfile='';
  242: if ($content{'customdistributionfile'}) {
  243:    $customdistributionfile='<a href="'.$content{'customdistributionfile'}.
  244:      '"><tt>'.$content{'customdistributionfile'}.'</tt></a>';
  245: }
  246: my $bodytag=&Apache::loncommon::bodytag
  247:             ('Catalog Information','','','',$resdomain);
  248:   $r->print(<<ENDHEAD);
  249: <html><head><title>Catalog Information</title></head>
  250: $bodytag
  251: <h2>$content{'title'}</h2>
  252: <h3><tt>$disuri</tt></h3>
  253: $versiondisplay<br />
  254: <table cellspacing=2 border=0>
  255: <tr><td bgcolor='#AAAAAA'>Author(s)</td>
  256: <td bgcolor="#CCCCCC">$author&nbsp;</td></tr>
  257: <tr><td bgcolor='#AAAAAA'>Subject</td>
  258: <td bgcolor="#CCCCCC">$content{'subject'}&nbsp;</td></tr>
  259: <tr><td bgcolor='#AAAAAA'>Keyword(s)</td>
  260: <td bgcolor="#CCCCCC">$content{'keywords'}&nbsp;</td></tr>
  261: <tr><td bgcolor='#AAAAAA'>Notes</td>
  262: <td bgcolor="#CCCCCC">$content{'notes'}&nbsp;</td></tr>
  263: <tr><td bgcolor='#AAAAAA'>Abstract</td>
  264: <td bgcolor="#CCCCCC">$content{'abstract'}&nbsp;</td></tr>
  265: <tr><td bgcolor='#AAAAAA'>MIME Type</td>
  266: <td bgcolor="#CCCCCC">$mime ($content{'mime'})&nbsp;</td></tr>
  267: <tr><td bgcolor='#AAAAAA'>Language</td>
  268: <td bgcolor="#CCCCCC">$language&nbsp;</td></tr>
  269: <tr><td bgcolor='#AAAAAA'>Creation Date</td>
  270: <td bgcolor="#CCCCCC">$creationdate&nbsp;</td></tr>
  271: <tr><td bgcolor='#AAAAAA'>
  272: Last Revision Date</td><td bgcolor="#CCCCCC">$lastrevisiondate&nbsp;</td></tr>
  273: <tr><td bgcolor='#AAAAAA'>Publisher/Owner</td>
  274: <td bgcolor="#CCCCCC">$owner&nbsp;</td></tr>
  275: <tr><td bgcolor='#AAAAAA'>Copyright/Distribution</td>
  276: <td bgcolor="#CCCCCC">$content{'copyright'}&nbsp;</td></tr>
  277: <tr><td bgcolor='#AAAAAA'>Custom Distribution File</td>
  278: <td bgcolor="#CCCCCC">$customdistributionfile&nbsp;</td></tr>
  279: </table>
  280: ENDHEAD
  281:   delete($content{'title'});
  282:   delete($content{'author'});
  283:   delete($content{'subject'});
  284:   delete($content{'keywords'});
  285:   delete($content{'notes'});
  286:   delete($content{'abstract'});
  287:   delete($content{'mime'});
  288:   delete($content{'language'});
  289:   delete($content{'creationdate'});
  290:   delete($content{'lastrevisiondate'});
  291:   delete($content{'owner'});
  292:   delete($content{'copyright'});
  293:   if ($ENV{'user.adv'}) {
  294: # ------------------------------------------------------------ Dynamic Metadata
  295:    $r->print(
  296:    '<h3>Dynamic Metadata (updated periodically)</h3>Processing ...<br>');
  297:    $r->rflush();
  298:     my %items=(
  299:  'count'      => 'Network-wide number of accesses (hits)',
  300:  'course'     => 'Network-wide number of courses using resource',
  301:  'usage'      => 'Number of resources using or importing resource',
  302:  'goto'       => 'Number of resources that follow this resource in maps',
  303:  'comefrom'   => 'Number of resources that lead up to this resource in maps',
  304:  'clear'      => 'Material presented in clear way',
  305:  'depth'      => 'Material covered with sufficient depth',
  306:  'helpful'    => 'Material is helpful',
  307:  'correct'    => 'Material appears to be correct',
  308:  'technical'  => 'Resource is technically correct', 
  309:  'avetries'   => 'Average number of tries till solved',
  310:  'stdno'      => 'Total number of students who have worked on this problem',
  311:  'difficulty' => 'Degree of difficulty');
  312:    my %dynmeta=&dynamicmeta($uri);
  313:    $r->print(
  314: '</table><h4>Access and Usage Statistics</h4><table cellspacing=2 border=0>');
  315:    foreach ('count') {
  316:        $r->print(
  317: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  318: $dynmeta{$_}."&nbsp;</td></tr>\n");
  319:    }
  320:    foreach my $cat ('usage','comefrom','goto') {
  321:        $r->print(
  322: '<tr><td bgcolor="#AAAAAA">'.$items{$cat}.'</td><td bgcolor="#CCCCCC">'.
  323: $dynmeta{$_}.'<br />'.join('<br /><font size="-1">',
  324:       map { '<a href="'.&Apache::lonnet::clutter($_).'">'.
  325:                         &Apache::lonnet::gettitle($_).' <tt>['.
  326: 			$_.']</tt></a>' }
  327:       split(/\,/,$dynmeta{$cat.'_list'}))."</font></td></tr>\n");
  328:    }
  329:    foreach ('course') {
  330:        $r->print(
  331: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  332: $dynmeta{$_}."&nbsp;</td></tr>\n");
  333:    }
  334:        $r->print('</table>');
  335:    if ($uri=~/\.(problem|exam|quiz|assess|survey|form)\.meta$/) {
  336:       $r->print(
  337: '<h4>Assessment Statistical Data</h4><table cellspacing=2 border=0>');
  338:       foreach ('stdno','avetries') {
  339:           $r->print(
  340: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  341: $dynmeta{$_}."&nbsp;</td></tr>\n");
  342:       }
  343:       foreach ('difficulty') {
  344:          $r->print(
  345: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  346: &diffgraph($dynmeta{$_})."</td></tr>\n");
  347:       }
  348:       $r->print('</table>');    
  349:    }
  350:    $r->print('<h4>Evaluation Data</h4><table cellspacing=2 border=0>');
  351:    foreach ('clear','depth','helpful','correct','technical') {
  352:        $r->print(
  353: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  354: &evalgraph($dynmeta{$_})."</td></tr>\n");
  355:    }    
  356:    $r->print('</table>');
  357:    $disuri=~/^(\w+)\/(\w+)\//;   
  358:    if ((($ENV{'user.domain'} eq $1) && ($ENV{'user.name'} eq $2))
  359:        || ($ENV{'user.role.ca./'.$1.'/'.$2})) {
  360:       $r->print(
  361:   '<h4>Evaluation Comments (visible to author and co-authors only)</h4>'.
  362:       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  363:       $r->print(
  364:    '<h4>Error Messages (visible to author and co-authors only)</h4>');
  365:       my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$1,$2);
  366:       foreach (keys %errormsgs) {
  367: 	if ($_=~/^\Q$disuri\E\_\d+$/) {
  368:           my %content=&Apache::lonmsg::unpackagemsg($errormsgs{$_});
  369: 	  $r->print('<b>'.$content{'time'}.'</b>: '.$content{'message'}.
  370:                     '<br />');
  371:         }
  372:       }      
  373:    }
  374: # ------------------------------------------------------------- All other stuff
  375:    $r->print(
  376:  '<h3>Additional Metadata (non-standard, parameters, exports)</h3>');
  377:    foreach (sort keys %content) {
  378:       my $name=$_;
  379:       my $display=&Apache::lonnet::metadata($uri,$name.'.display');
  380:       unless ($display) { $display=$name; };
  381:       my $otherinfo='';
  382:       foreach ('name','part','type','default') {
  383:           if (defined(&Apache::lonnet::metadata($uri,$name.'.'.$_))) {
  384:              $otherinfo.=' '.$_.'='.
  385: 		 &Apache::lonnet::metadata($uri,$name.'.'.$_).'; ';
  386:           }
  387:       }
  388:       $r->print('<b>'.$display.':</b> '.$content{$name});
  389:       if ($otherinfo) {
  390:          $r->print(' ('.$otherinfo.')');
  391:       }
  392:       $r->print("<br>\n");
  393:    }
  394:   }
  395: # ===================================================== End Resource Space Call
  396:  } else {
  397: # ===================================================== Construction Space Call
  398: 
  399: # ----------------------------------------------------------- Set document type
  400: 
  401:   $r->content_type('text/html');
  402:   $r->send_http_header;
  403: 
  404:   return OK if $r->header_only;
  405: # ---------------------------------------------------------------------- Header
  406:   my $bodytag=&Apache::loncommon::bodytag('Edit Catalog Information');
  407:   my $disuri=$uri;
  408:   my $fn=&Apache::lonnet::filelocation('',$uri);
  409:   $disuri=~s/^\/\~\w+//;
  410:   $disuri=~s/\.meta$//;
  411:   my $displayfile='Catalog Information for '.$disuri;
  412:   if ($disuri=~/\/default$/) {
  413:       my $dir=$disuri;
  414:       $dir=~s/default$//;
  415:       $displayfile='Default Cataloging Information for Directory '.$dir;
  416:   }
  417:   %Apache::lonpublisher::metadatafields=();
  418:   %Apache::lonpublisher::metadatakeys=();
  419:   &Apache::lonpublisher::metaeval(&Apache::lonnet::getfile($fn));
  420:   $r->print(<<ENDEDIT);
  421: <html><head><title>Edit Catalog Information</title></head>
  422: $bodytag
  423: <h1>$displayfile</h1>
  424: <form method="post">
  425: ENDEDIT
  426:    foreach ('author','title','subject','keywords','abstract','notes',
  427:             'copyright','customdistributionfile','language') {
  428:        if ($ENV{'form.new_'.$_}) {
  429: 	   $Apache::lonpublisher::metadatafields{$_}=$ENV{'form.new_'.$_};
  430:        }
  431:        $r->print(
  432:          &Apache::lonpublisher::textfield($_,'new_'.$_,
  433:                                  $Apache::lonpublisher::metadatafields{$_}));
  434:    }
  435:    if ($ENV{'form.store'}) {
  436:       my $mfh;
  437:       unless ($mfh=Apache::File->new('>'.$fn)) {
  438:             $r->print(
  439:             '<p><font color=red>Could not write metadata, FAIL</font>');
  440:       } else {
  441:           foreach (sort keys %Apache::lonpublisher::metadatafields) {
  442:             unless ($_=~/\./) {
  443:                 my $unikey=$_;
  444:                 $unikey=~/^([A-Za-z]+)/;
  445:                 my $tag=$1;
  446:                 $tag=~tr/A-Z/a-z/;
  447:                 print $mfh "\n\<$tag";
  448:                 foreach 
  449:                   (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
  450:                     my $value=
  451:                        $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
  452:                     $value=~s/\"/\'\'/g;
  453:                     print $mfh ' '.$_.'="'.$value.'"';
  454:                 }
  455:                 print $mfh '>'.
  456:         &HTML::Entities::encode($Apache::lonpublisher::metadatafields{$unikey})
  457:                         .'</'.$tag.'>';
  458:             }
  459: 	  }
  460:           $r->print('<p>Wrote Metadata');
  461:       }
  462:     }
  463:     $r->print(
  464:  '<br /><input type="submit" name="store" value="Store Catalog Information"></form></body></html>');
  465:     return OK;
  466:   }
  467: }
  468: 
  469: 1;
  470: __END__
  471: 
  472: 
  473: 
  474: 
  475: 
  476: 
  477: 

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