Annotation of loncom/interface/lonmeta.pm, revision 1.29

1.1       www         1: # The LearningOnline Network with CAPA
1.8       albertel    2: # Metadata display handler
                      3: #
1.29    ! www         4: # $Id: lonmeta.pm,v 1.28 2003/03/14 19:29:36 albertel Exp $
1.8       albertel    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.
1.1       www        14: #
1.8       albertel   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/
1.1       www        27: #
                     28: # (TeX Content Handler
                     29: #
                     30: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
                     31: #
1.13      www        32: # 10/19,10/21,10/23,11/27,08/09/01,12/22,12/24,12/25 Gerd Kortemeyer
1.1       www        33: 
                     34: package Apache::lonmeta;
                     35: 
                     36: use strict;
                     37: use Apache::Constants qw(:common);
1.3       www        38: use Apache::lonnet();
1.10      www        39: use Apache::loncommon();
1.23      www        40: use Apache::lonmsg;
                     41: use Apache::lonpublisher;
1.1       www        42: 
1.9       www        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+)\//);
1.19      www        49:     my $regexp=$url;
1.9       www        50:     $regexp=~s/(\W)/\\$1/g;
1.10      www        51:     $regexp='___'.$regexp.'___';
1.16      albertel   52:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
                     53: 				       $aauthor,$regexp);
1.10      www        54:     my %sum;
                     55:     my %cnt;
                     56:     my %listitems=('count'        => 'add',
                     57:                    'course'       => 'add',
1.29    ! www        58:                    'goto'         => 'add',
        !            59:                    'comefrom'     => 'add',
1.10      www        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:     }
1.9       www        90:     my %returnhash=();
1.10      www        91:     foreach (keys %cnt) {
1.11      www        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:        }
1.9       www        99:     }
                    100:     return %returnhash;
1.25      www       101: }
                    102: 
                    103: # ------------------------------------- Try to make an alt tag if there is none
                    104: 
                    105: sub alttag {
1.26      www       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'; }
1.9       www       118: }
1.1       www       119: 
1.29    ! www       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: 
1.12      www       129: # -------------------------------------------------------------- Pretty display
                    130: 
                    131: sub evalgraph {
                    132:     my $value=shift;
1.13      www       133:     unless ($value) { return ''; }
1.12      www       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;
1.13      www       155:     unless ($value) { return ''; }
1.12      www       156:     my $val=int(40.0*$value+0.5);
1.13      www       157:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
                    158:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
1.12      www       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: 
1.1       www       171: # ================================================================ Main Handler
                    172: 
                    173: sub handler {
                    174:   my $r=shift;
1.20      www       175: 
                    176:     my $loaderror=&Apache::lonnet::overloaderror($r);
                    177:     if ($loaderror) { return $loaderror; }
                    178: 
                    179: 
                    180:     my $uri=$r->uri;
                    181: 
1.23      www       182:   unless ($uri=~/^\/\~/) { 
                    183: # =========================================== This is not in construction space
1.20      www       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: 
1.3       www       192:   my %content=();
1.1       www       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
1.14      harris41  202:   foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
1.3       www       203:       $content{$_}=&Apache::lonnet::metadata($uri,$_);
1.14      harris41  204:   }
1.6       albertel  205: # ------------------------------------------------------------------ Hide stuff
1.7       www       206: 
                    207:   unless ($ENV{'user.adv'}) {
1.14      harris41  208:       foreach ('keywords','notes','abstract','subject') {
1.7       www       209:           $content{$_}='<i>- not displayed -</i>';
1.14      harris41  210:       }
1.6       albertel  211:   }
                    212: 
1.1       www       213: # --------------------------------------------------------------- Render Output
1.23      www       214:   my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
1.22      www       215: my $creationdate=localtime(
                    216:  &Apache::loncommon::unsqltime($content{'creationdate'}));
                    217: my $lastrevisiondate=localtime(
                    218:  &Apache::loncommon::unsqltime($content{'lastrevisiondate'}));
1.10      www       219: my $language=&Apache::loncommon::languagedescription($content{'language'});
                    220: my $mime=&Apache::loncommon::filedescription($content{'mime'}); 
1.11      www       221: my $disuri=&Apache::lonnet::declutter($uri);
                    222:   $disuri=~s/\.meta$//;
1.21      www       223: my $currentversion=&Apache::lonnet::getversion($disuri);
1.29    ! www       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;
1.21      www       228: my $versiondisplay='';
                    229: if ($thisversion) {
                    230:     $versiondisplay='Version: '.$thisversion.
                    231:     ' (most recent version: '.$currentversion.')';
                    232: } else {
                    233:     $versiondisplay='Version: '.$currentversion;
                    234: }
1.29    ! www       235: my $customdistributionfile='';
        !           236: if ($content{'customdistributionfile'}) {
        !           237:    $customdistributionfile='<a href="'.$content{'customdistributionfile'}.
        !           238:      '"><tt>'.$content{'customdistributionfile'}.'</tt></a>';
        !           239: }
1.17      www       240: my $bodytag=&Apache::loncommon::bodytag
                    241:             ('Catalog Information','','','',$resdomain);
1.1       www       242:   $r->print(<<ENDHEAD);
                    243: <html><head><title>Catalog Information</title></head>
1.17      www       244: $bodytag
1.1       www       245: <h2>$content{'title'}</h2>
1.11      www       246: <h3><tt>$disuri</tt></h3>
1.21      www       247: $versiondisplay<br />
1.11      www       248: <table cellspacing=2 border=0>
                    249: <tr><td bgcolor='#AAAAAA'>Author(s)</td>
1.29    ! www       250: <td bgcolor="#CCCCCC">$author&nbsp;</td></tr>
1.11      www       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>
1.29    ! www       268: <td bgcolor="#CCCCCC">$owner&nbsp;</td></tr>
1.11      www       269: <tr><td bgcolor='#AAAAAA'>Copyright/Distribution</td>
1.29    ! www       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>
1.11      www       273: </table>
1.1       www       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'});
1.7       www       287:   if ($ENV{'user.adv'}) {
1.11      www       288: # ------------------------------------------------------------ Dynamic Metadata
1.15      www       289:    $r->print(
                    290:    '<h3>Dynamic Metadata (updated periodically)</h3>Processing ...<br>');
                    291:    $r->rflush();
1.10      www       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',
1.11      www       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', 
1.10      www       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);
1.11      www       305:    $r->print(
                    306: '</table><h4>Access and Usage Statistics</h4><table cellspacing=2 border=0>');
1.10      www       307:    foreach ('count','usage','course') {
1.11      www       308:        $r->print(
                    309: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
                    310: $dynmeta{$_}."&nbsp;</td></tr>\n");
                    311:    }
                    312:        $r->print('</table>');
1.10      www       313:    if ($uri=~/\.(problem|exam|quiz|assess|survey|form)\.meta$/) {
1.11      www       314:       $r->print(
                    315: '<h4>Assessment Statistical Data</h4><table cellspacing=2 border=0>');
1.12      www       316:       foreach ('stdno','avetries') {
1.11      www       317:           $r->print(
                    318: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
                    319: $dynmeta{$_}."&nbsp;</td></tr>\n");
                    320:       }
1.12      www       321:       foreach ('difficulty') {
                    322:          $r->print(
                    323: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
                    324: &diffgraph($dynmeta{$_})."</td></tr>\n");
                    325:       }
1.11      www       326:       $r->print('</table>');    
1.10      www       327:    }
1.11      www       328:    $r->print('<h4>Evaluation Data</h4><table cellspacing=2 border=0>');
1.10      www       329:    foreach ('clear','depth','helpful','correct','technical') {
1.11      www       330:        $r->print(
                    331: '<tr><td bgcolor="#AAAAAA">'.$items{$_}.'</td><td bgcolor="#CCCCCC">'.
1.12      www       332: &evalgraph($dynmeta{$_})."</td></tr>\n");
1.10      www       333:    }    
1.11      www       334:    $r->print('</table>');
                    335:    $disuri=~/^(\w+)\/(\w+)\//;   
1.10      www       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>'.
1.23      www       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) {
1.28      albertel  345: 	if ($_=~/^\Q$disuri\E\_\d+$/) {
1.23      www       346:           my %content=&Apache::lonmsg::unpackagemsg($errormsgs{$_});
                    347: 	  $r->print('<b>'.$content{'time'}.'</b>: '.$content{'message'}.
                    348:                     '<br />');
                    349:         }
                    350:       }      
1.10      www       351:    }
1.11      www       352: # ------------------------------------------------------------- All other stuff
1.10      www       353:    $r->print(
                    354:  '<h3>Additional Metadata (non-standard, parameters, exports)</h3>');
                    355:    foreach (sort keys %content) {
1.3       www       356:       my $name=$_;
                    357:       my $display=&Apache::lonnet::metadata($uri,$name.'.display');
1.5       www       358:       unless ($display) { $display=$name; };
                    359:       my $otherinfo='';
1.14      harris41  360:       foreach ('name','part','type','default') {
1.5       www       361:           if (defined(&Apache::lonnet::metadata($uri,$name.'.'.$_))) {
                    362:              $otherinfo.=' '.$_.'='.
                    363: 		 &Apache::lonnet::metadata($uri,$name.'.'.$_).'; ';
                    364:           }
1.14      harris41  365:       }
1.5       www       366:       $r->print('<b>'.$display.':</b> '.$content{$name});
                    367:       if ($otherinfo) {
                    368:          $r->print(' ('.$otherinfo.')');
                    369:       }
                    370:       $r->print("<br>\n");
1.10      www       371:    }
1.7       www       372:   }
1.23      www       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>
1.24      www       402: <form method="post">
1.23      www       403: ENDEDIT
1.24      www       404:    foreach ('author','title','subject','keywords','abstract','notes',
1.27      www       405:             'copyright','customdistributionfile','language') {
1.24      www       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{$_}));
1.23      www       412:    }
1.24      www       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:   }
1.1       www       445: }
                    446: 
                    447: 1;
                    448: __END__
                    449: 
                    450: 
                    451: 
                    452: 
                    453: 
                    454: 
                    455: 

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