File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.29: download - view: text, annotated - select for diffs
Sat Apr 12 15:57:30 2003 UTC (21 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
"Return Location" link in inline remote

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

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