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

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

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