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

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

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