File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.31: download - view: text, annotated - select for diffs
Tue Apr 15 00:53:04 2003 UTC (21 years, 2 months ago) by www
Branches: MAIN
CVS tags: version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
Output of link and course data

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.31 2003/04/15 00:53:04 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{$cat}.'<font size="-1"><ul>'.join("\n",
  324:       map { my $murl=$_; 
  325:  '<li><a href="'.&Apache::lonnet::clutter($murl).'" target="preview">'.
  326:                         &Apache::lonnet::gettitle($murl).' [<tt>'.$murl
  327:                         .'</tt>]</a></li>' }
  328:       split(/\,/,$dynmeta{$cat.'_list'}))."</ul></font></td></tr>\n");
  329:    }
  330:    foreach my $cat ('course') {
  331:        $r->print(
  332: '<tr><td bgcolor="#AAAAAA">'.$items{$cat}.'</td><td bgcolor="#CCCCCC">'.
  333: $dynmeta{$cat}.'<font size="-1"><ul>'.join("\n",
  334:       map { my %courseinfo=&Apache::lonnet::coursedescription($_);  
  335:  '<li><a href="/public/'.
  336:   $courseinfo{'domain'}.'/'.$courseinfo{'num'}.'/syllabus" target="preview">'.
  337:   $courseinfo{'description'}.'</a></li>' }
  338:       split(/\,/,$dynmeta{$cat.'_list'}))."</ul></font></td></tr>\n");
  339:    }
  340:        $r->print('</table>');
  341:    if ($uri=~/\.(problem|exam|quiz|assess|survey|form)\.meta$/) {
  342:       $r->print(
  343: '<h4>Assessment Statistical Data</h4><table cellspacing=2 border=0>');
  344:       foreach ('stdno','avetries') {
  345:           $r->print(
  346: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  347: $dynmeta{$_}."&nbsp;</td></tr>\n");
  348:       }
  349:       foreach ('difficulty') {
  350:          $r->print(
  351: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  352: &diffgraph($dynmeta{$_})."</td></tr>\n");
  353:       }
  354:       $r->print('</table>');    
  355:    }
  356:    $r->print('<h4>Evaluation Data</h4><table cellspacing=2 border=0>');
  357:    foreach ('clear','depth','helpful','correct','technical') {
  358:        $r->print(
  359: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
  360: &evalgraph($dynmeta{$_})."</td></tr>\n");
  361:    }    
  362:    $r->print('</table>');
  363:    $disuri=~/^(\w+)\/(\w+)\//;   
  364:    if ((($ENV{'user.domain'} eq $1) && ($ENV{'user.name'} eq $2))
  365:        || ($ENV{'user.role.ca./'.$1.'/'.$2})) {
  366:       $r->print(
  367:   '<h4>Evaluation Comments (visible to author and co-authors only)</h4>'.
  368:       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  369:       $r->print(
  370:    '<h4>Error Messages (visible to author and co-authors only)</h4>');
  371:       my %errormsgs=&Apache::lonnet::dump('nohist_res_msgs',$1,$2);
  372:       foreach (keys %errormsgs) {
  373: 	if ($_=~/^\Q$disuri\E\_\d+$/) {
  374:           my %content=&Apache::lonmsg::unpackagemsg($errormsgs{$_});
  375: 	  $r->print('<b>'.$content{'time'}.'</b>: '.$content{'message'}.
  376:                     '<br />');
  377:         }
  378:       }      
  379:    }
  380: # ------------------------------------------------------------- All other stuff
  381:    $r->print(
  382:  '<h3>Additional Metadata (non-standard, parameters, exports)</h3>');
  383:    foreach (sort keys %content) {
  384:       my $name=$_;
  385:       my $display=&Apache::lonnet::metadata($uri,$name.'.display');
  386:       unless ($display) { $display=$name; };
  387:       my $otherinfo='';
  388:       foreach ('name','part','type','default') {
  389:           if (defined(&Apache::lonnet::metadata($uri,$name.'.'.$_))) {
  390:              $otherinfo.=' '.$_.'='.
  391: 		 &Apache::lonnet::metadata($uri,$name.'.'.$_).'; ';
  392:           }
  393:       }
  394:       $r->print('<b>'.$display.':</b> '.$content{$name});
  395:       if ($otherinfo) {
  396:          $r->print(' ('.$otherinfo.')');
  397:       }
  398:       $r->print("<br>\n");
  399:    }
  400:   }
  401: # ===================================================== End Resource Space Call
  402:  } else {
  403: # ===================================================== Construction Space Call
  404: 
  405: # ----------------------------------------------------------- Set document type
  406: 
  407:   $r->content_type('text/html');
  408:   $r->send_http_header;
  409: 
  410:   return OK if $r->header_only;
  411: # ---------------------------------------------------------------------- Header
  412:   my $bodytag=&Apache::loncommon::bodytag('Edit Catalog Information');
  413:   my $disuri=$uri;
  414:   my $fn=&Apache::lonnet::filelocation('',$uri);
  415:   $disuri=~s/^\/\~\w+//;
  416:   $disuri=~s/\.meta$//;
  417:   my $displayfile='Catalog Information for '.$disuri;
  418:   if ($disuri=~/\/default$/) {
  419:       my $dir=$disuri;
  420:       $dir=~s/default$//;
  421:       $displayfile='Default Cataloging Information for Directory '.$dir;
  422:   }
  423:   %Apache::lonpublisher::metadatafields=();
  424:   %Apache::lonpublisher::metadatakeys=();
  425:   &Apache::lonpublisher::metaeval(&Apache::lonnet::getfile($fn));
  426:   $r->print(<<ENDEDIT);
  427: <html><head><title>Edit Catalog Information</title></head>
  428: $bodytag
  429: <h1>$displayfile</h1>
  430: <form method="post">
  431: ENDEDIT
  432:    foreach ('author','title','subject','keywords','abstract','notes',
  433:             'copyright','customdistributionfile','language') {
  434:        if ($ENV{'form.new_'.$_}) {
  435: 	   $Apache::lonpublisher::metadatafields{$_}=$ENV{'form.new_'.$_};
  436:        }
  437:        $r->print(
  438:          &Apache::lonpublisher::textfield($_,'new_'.$_,
  439:                                  $Apache::lonpublisher::metadatafields{$_}));
  440:    }
  441:    if ($ENV{'form.store'}) {
  442:       my $mfh;
  443:       unless ($mfh=Apache::File->new('>'.$fn)) {
  444:             $r->print(
  445:             '<p><font color=red>Could not write metadata, FAIL</font>');
  446:       } else {
  447:           foreach (sort keys %Apache::lonpublisher::metadatafields) {
  448:             unless ($_=~/\./) {
  449:                 my $unikey=$_;
  450:                 $unikey=~/^([A-Za-z]+)/;
  451:                 my $tag=$1;
  452:                 $tag=~tr/A-Z/a-z/;
  453:                 print $mfh "\n\<$tag";
  454:                 foreach 
  455:                   (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
  456:                     my $value=
  457:                        $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
  458:                     $value=~s/\"/\'\'/g;
  459:                     print $mfh ' '.$_.'="'.$value.'"';
  460:                 }
  461:                 print $mfh '>'.
  462:         &HTML::Entities::encode($Apache::lonpublisher::metadatafields{$unikey})
  463:                         .'</'.$tag.'>';
  464:             }
  465: 	  }
  466:           $r->print('<p>Wrote Metadata');
  467:       }
  468:     }
  469:     $r->print(
  470:  '<br /><input type="submit" name="store" value="Store Catalog Information"></form></body></html>');
  471:     return OK;
  472:   }
  473: }
  474: 
  475: 1;
  476: __END__
  477: 
  478: 
  479: 
  480: 
  481: 
  482: 
  483: 

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