File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.24: download - view: text, annotated - select for diffs
Fri Oct 25 13:50:48 2002 UTC (21 years, 8 months ago) by www
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
Bug #492

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

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