File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.84: download - view: text, annotated - select for diffs
Fri Jul 9 21:35:05 2004 UTC (19 years, 9 months ago) by banghart
Branches: MAIN
CVS tags: version_1_1_99_3, version_1_1_99_2, HEAD
	typo correction

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.84 2004/07/09 21:35:05 banghart 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: 
   29: package Apache::lonmeta;
   30: 
   31: use strict;
   32: use LONCAPA::lonmetadata();
   33: use Apache::Constants qw(:common);
   34: use Apache::lonnet();
   35: use Apache::loncommon();
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonmsg;
   38: use Apache::lonpublisher;
   39: use Apache::lonlocal;
   40: use Apache::lonmysql;
   41: use Apache::lonmsg;
   42: 
   43: 
   44: ############################################################
   45: ############################################################
   46: ##
   47: ## &get_dynamic_metadata_from_sql($url)
   48: ## 
   49: ## Queries sql database for dynamic metdata
   50: ## Returns a hash of hashes, with keys of urls which match $url
   51: ## Returned fields are given below.
   52: ##
   53: ## Examples:
   54: ## 
   55: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
   56: ##     ('/res/msu/korte/');
   57: ##
   58: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
   59: ##
   60: ############################################################
   61: ############################################################
   62: sub get_dynamic_metadata_from_sql {
   63:     my ($url) = shift();
   64:     my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
   65:     if (! defined($authordom)) {
   66:         $authordom = shift();
   67:     }
   68:     if  (! defined($author)) { 
   69:         $author = shift();
   70:     }
   71:     if (! defined($authordom) || ! defined($author)) {
   72:         return ();
   73:     }
   74:     my @Fields = ('url','count','course',
   75:                   'goto','goto_list',
   76:                   'comefrom','comefrom_list',
   77:                   'sequsage','sequsage_list',
   78:                   'stdno','stdno_list',
   79: 		  'dependencies',
   80:                   'avetries','avetries_list',
   81:                   'difficulty','difficulty_list',
   82:                   'disc','disc_list',
   83:                   'clear','technical','correct',
   84:                   'helpful','depth');
   85:     #
   86:     my $query = 'SELECT '.join(',',@Fields).
   87:         ' FROM metadata WHERE url LIKE "'.$url.'%"';
   88:     my $server = &Apache::lonnet::homeserver($author,$authordom);
   89:     my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
   90:                                                 ,[$server]);
   91:     return () if (! defined($reply) || ref($reply) ne 'HASH');
   92:     my $filename = $reply->{$server};
   93:     if (! defined($filename) || $filename =~ /^error/) {
   94:         return ();
   95:     }
   96:     my $max_time = time + 10; # wait 10 seconds for results at most
   97:     my %ReturnHash;
   98:     #
   99:     # Look for results
  100:     my $finished = 0;
  101:     while (! $finished && time < $max_time) {
  102:         my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
  103:         if (! -e "$datafile.end") { next; }
  104:         my $fh;
  105:         if (!($fh=Apache::File->new($datafile))) { next; }
  106:         while (my $result = <$fh>) {
  107:             chomp($result);
  108:             next if (! $result);
  109:             my @Data = 
  110:                 map { 
  111:                     &Apache::lonnet::unescape($_); 
  112:                 } split(',',$result);
  113:             my $url = $Data[0];
  114:             for (my $i=0;$i<=$#Fields;$i++) {
  115:                 $ReturnHash{$url}->{$Fields[$i]}=$Data[$i];
  116:             }
  117:         }
  118:         $finished = 1;
  119:     }
  120:     #
  121:     return %ReturnHash;
  122: }
  123: 
  124: 
  125: # Fetch and evaluate dynamic metadata
  126: sub dynamicmeta {
  127:     my $url=&Apache::lonnet::declutter(shift);
  128:     $url=~s/\.meta$//;
  129:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
  130:     my $regexp=$url;
  131:     $regexp=~s/(\W)/\\$1/g;
  132:     $regexp='___'.$regexp.'___';
  133:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
  134: 				       $aauthor,$regexp);
  135:     my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  136:     my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  137:                                                                \%DynamicData);
  138:     #
  139:     # Deal with 'count' separately
  140:     $Data{'count'} = &access_count($url,$aauthor,$adomain);
  141:     #
  142:     # Debugging code I will probably need later
  143:     if (0) {
  144:         &Apache::lonnet::logthis('Dynamic Metadata');
  145:         while(my($k,$v)=each(%Data)){
  146:             &Apache::lonnet::logthis('    "'.$k.'"=>"'.$v.'"');
  147:         }
  148:         &Apache::lonnet::logthis('-------------------');
  149:     }
  150:     return %Data;
  151: }
  152: 
  153: sub access_count {
  154:     my ($src,$author,$adomain) = @_;
  155:     my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
  156:                                         $author,$src);
  157:     if (! exists($countdata{$src})) {
  158:         return &mt('Not Available');
  159:     } else {
  160:         return $countdata{$src};
  161:     }
  162: }
  163: 
  164: # Try to make an alt tag if there is none
  165: sub alttag {
  166:     my ($base,$src)=@_;
  167:     my $fullpath=&Apache::lonnet::hreflocation($base,$src);
  168:     my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
  169:         &Apache::lonnet::metadata($fullpath,'subject').' '.
  170:         &Apache::lonnet::metadata($fullpath,'abstract');
  171:     $alttag=~s/\s+/ /gs;
  172:     $alttag=~s/\"//gs;
  173:     $alttag=~s/\'//gs;
  174:     $alttag=~s/\s+$//gs;
  175:     $alttag=~s/^\s+//gs;
  176:     if ($alttag) { 
  177:         return $alttag; 
  178:     } else { 
  179:         return &mt('No information available'); 
  180:     }
  181: }
  182: 
  183: # Author display
  184: sub authordisplay {
  185:     my ($aname,$adom)=@_;
  186:     return &Apache::loncommon::aboutmewrapper
  187:         (&Apache::loncommon::plainname($aname,$adom),
  188:          $aname,$adom,'preview').' <tt>['.$aname.'@'.$adom.']</tt>';
  189: }
  190: 
  191: # Pretty display
  192: sub evalgraph {
  193:     my $value=shift;
  194:     if (! $value) { 
  195:         return '';
  196:     }
  197:     my $val=int($value*10.+0.5)-10;
  198:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  199:     if ($val>=20) {
  200: 	$output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  201:     } else {
  202:         $output.='<td width="'.($val).'" bgcolor="#555555">&nbsp;</td>'.
  203:                  '<td width="'.(20-$val).'" bgcolor="#FF3333">&nbsp;</td>';
  204:     }
  205:     $output.='<td bgcolor="#FFFF33">&nbsp;</td>';
  206:     if ($val>20) {
  207: 	$output.='<td width="'.($val-20).'" bgcolor="#33FF33">&nbsp;</td>'.
  208:                  '<td width="'.(40-$val).'" bgcolor="#555555">&nbsp;</td>';
  209:     } else {
  210:         $output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
  211:     }
  212:     $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
  213:     return $output;
  214: }
  215: 
  216: sub diffgraph {
  217:     my $value=shift;
  218:     if (! $value) { 
  219:         return '';
  220:     }
  221:     my $val=int(40.0*$value+0.5);
  222:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
  223:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
  224:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
  225:     for (my $i=0;$i<8;$i++) {
  226: 	if ($val>$i*5) {
  227:             $output.='<td width="5" bgcolor="'.$colors[$i].'">&nbsp;</td>';
  228:         } else {
  229: 	    $output.='<td width="5" bgcolor="#555555">&nbsp;</td>';
  230: 	}
  231:     }
  232:     $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
  233:     return $output;
  234: }
  235: 
  236: 
  237: # The field names
  238: sub fieldnames {
  239:     return &Apache::lonlocal::texthash
  240:         (
  241:          'title' => 'Title',
  242:          'author' =>'Author(s)',
  243:          'authorspace' => 'Author Space',
  244:          'modifyinguser' => 'Last Modifying User',
  245:          'subject' => 'Subject',
  246:          'keywords' => 'Keyword(s)',
  247:          'notes' => 'Notes',
  248:          'abstract' => 'Abstract',
  249:          'lowestgradelevel' => 'Lowest Grade Level',
  250:          'highestgradelevel' => 'Highest Grade Level',
  251:          'standards' => 'Standards',
  252:          'mime' => 'MIME Type',
  253:          'language' => 'Language',
  254:          'creationdate' => 'Creation Date',
  255:          'lastrevisiondate' => 'Last Revision Date',
  256:          'owner' => 'Publisher/Owner',
  257:          'copyright' => 'Copyright/Distribution',
  258:          'customdistributionfile' => 'Custom Distribution File',
  259:          'sourceavail' => 'Source Available',
  260:          'sourcerights' => 'Source Custom Distribution File',
  261:          'obsolete' => 'Obsolete',
  262:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
  263:          'count'      => 'Network-wide number of accesses (hits)',
  264:          'course'     => 'Network-wide number of courses using resource',
  265:          'course_list' => 'Network-wide courses using resource',
  266:          'sequsage'      => 'Number of resources using or importing resource',
  267:          'sequsage_list' => 'Resources using or importing resource',
  268:          'goto'       => 'Number of resources that follow this resource in maps',
  269:          'goto_list'  => 'Resources that follow this resource in maps',
  270:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
  271:          'comefrom_list' => 'Resources that lead up to this resource in maps',
  272:          'clear'      => 'Material presented in clear way',
  273:          'depth'      => 'Material covered with sufficient depth',
  274:          'helpful'    => 'Material is helpful',
  275:          'correct'    => 'Material appears to be correct',
  276:          'technical'  => 'Resource is technically correct', 
  277:          'avetries'   => 'Average number of tries till solved',
  278:          'stdno'      => 'Total number of students who have worked on this problem',
  279:          'difficulty' => 'Degree of difficulty',
  280:          'disc'       => 'Degree of discrimination',
  281: 	 'dependencies' => 'Resources used by this resource',
  282:          );
  283: }
  284: 
  285: # Pretty printing of metadata field
  286: 
  287: sub prettyprint {
  288:     my ($type,$value,$target,$prefix,$form,$noformat)=@_;
  289: # $target,$prefix,$form are optional and for filecrumbs only
  290:     if (! defined($value)) { 
  291:         return '&nbsp;'; 
  292:     }
  293:     # Title
  294:     if ($type eq 'title') {
  295: 	return '<font size="+1" face="arial">'.$value.'</font>';
  296:     }
  297:     # Dates
  298:     if (($type eq 'creationdate') ||
  299: 	($type eq 'lastrevisiondate')) {
  300: 	return ($value?&Apache::lonlocal::locallocaltime(
  301: 			  &Apache::lonmysql::unsqltime($value)):
  302: 		&mt('not available'));
  303:     }
  304:     # Language
  305:     if ($type eq 'language') {
  306: 	return &Apache::loncommon::languagedescription($value);
  307:     }
  308:     # Copyright
  309:     if ($type eq 'copyright') {
  310: 	return &Apache::loncommon::copyrightdescription($value);
  311:     }
  312:     # Copyright
  313:     if ($type eq 'sourceavail') {
  314: 	return &Apache::loncommon::source_copyrightdescription($value);
  315:     }
  316:     # MIME
  317:     if ($type eq 'mime') {
  318:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
  319:             &Apache::loncommon::filedescription($value);
  320:     }
  321:     # Person
  322:     if (($type eq 'author') || 
  323: 	($type eq 'owner') ||
  324: 	($type eq 'modifyinguser') ||
  325: 	($type eq 'authorspace')) {
  326: 	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
  327: 	return $value;
  328:     }
  329:     # Gradelevel
  330:     if (($type eq 'lowestgradelevel') ||
  331: 	($type eq 'highestgradelevel')) {
  332: 	return &Apache::loncommon::gradeleveldescription($value);
  333:     }
  334:     # Only for advance users below
  335:     if (! $ENV{'user.adv'}) { 
  336:         return '<i>- '.&mt('not displayed').' -</i>';
  337:     }
  338:     # File
  339:     if (($type eq 'customdistributionfile') ||
  340: 	($type eq 'obsoletereplacement') ||
  341: 	($type eq 'goto_list') ||
  342: 	($type eq 'comefrom_list') ||
  343: 	($type eq 'sequsage_list') ||
  344: 	($type eq 'dependencies')) {
  345: 	return '<ul><font size="-1">'.join("\n",map {
  346:             my $url = &Apache::lonnet::clutter($_);
  347:             my $title = &Apache::lonnet::gettitle($url);
  348:             if ($title eq '') {
  349:                 $title = 'Untitled';
  350:                 if ($url =~ /\.sequence$/) {
  351:                     $title .= ' Sequence';
  352:                 } elsif ($url =~ /\.page$/) {
  353:                     $title .= ' Page';
  354:                 } elsif ($url =~ /\.problem$/) {
  355:                     $title .= ' Problem';
  356:                 } elsif ($url =~ /\.html$/) {
  357:                     $title .= ' HTML document';
  358:                 } elsif ($url =~ m:/syllabus$:) {
  359:                     $title .= ' Syllabus';
  360:                 } 
  361:             }
  362:             $_ = '<li>'.$title.' '.
  363: 		&Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
  364:                 '</li>'
  365: 	    } split(/\s*\,\s*/,$value)).'</ul></font>';
  366:     }
  367:     # Evaluations
  368:     if (($type eq 'clear') ||
  369: 	($type eq 'depth') ||
  370: 	($type eq 'helpful') ||
  371: 	($type eq 'correct') ||
  372: 	($type eq 'technical')) {
  373: 	return &evalgraph($value);
  374:     }
  375:     # Difficulty
  376:     if ($type eq 'difficulty' || $type eq 'disc') {
  377: 	return &diffgraph($value);
  378:     }
  379:     # List of courses
  380:     if ($type=~/\_list/) {
  381:         my @Courses = split(/\s*\,\s*/,$value);
  382:         my $Str;
  383:         foreach my $course (@Courses) {
  384:             my %courseinfo = &Apache::lonnet::coursedescription($course);
  385:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  386:                 next;
  387:             }
  388:             if ($Str ne '') { $Str .= '<br />'; }
  389:             $Str .= '<a href="/public/'.$courseinfo{'domain'}.'/'.
  390:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
  391:                 $courseinfo{'description'}.'</a>';
  392:         }
  393: 	return $Str;
  394:     }
  395:     # No pretty print found
  396:     return $value;
  397: }
  398: 
  399: # Pretty input of metadata field
  400: sub direct {
  401:     return shift;
  402: }
  403: 
  404: sub selectbox {
  405:     my ($name,$value,$functionref,@idlist)=@_;
  406:     if (! defined($functionref)) {
  407:         $functionref=\&direct;
  408:     }
  409:     my $selout='<select name="'.$name.'">';
  410:     foreach (@idlist) {
  411:         $selout.='<option value=\''.$_.'\'';
  412:         if ($_ eq $value) {
  413: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  414: 	}
  415:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  416:     }
  417:     return $selout.'</select>';
  418: }
  419: 
  420: sub relatedfield {
  421:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
  422:     if (! $relatedsearchflag) { 
  423:         return '';
  424:     }
  425:     if (! defined($relatedsep)) {
  426:         $relatedsep=' ';
  427:     }
  428:     if (! $show) {
  429:         return $relatedsep.'&nbsp;';
  430:     }
  431:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
  432: 	($relatedvalue?' checked="1"':'').' />';
  433: }
  434: 
  435: sub prettyinput {
  436:     my ($type,$value,$fieldname,$formname,
  437: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size)=@_;
  438:     if (! defined($size)) {
  439:         $size = 80;
  440:     }
  441:     # Language
  442:     if ($type eq 'language') {
  443: 	return &selectbox($fieldname,
  444: 			  $value,
  445: 			  \&Apache::loncommon::languagedescription,
  446: 			  (&Apache::loncommon::languageids)).
  447:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  448:     }
  449:     # Copyright
  450:     if ($type eq 'copyright') {
  451: 	return &selectbox($fieldname,
  452: 			  $value,
  453: 			  \&Apache::loncommon::copyrightdescription,
  454: 			  (&Apache::loncommon::copyrightids)).
  455:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  456:     }
  457:     # Source Copyright
  458:     if ($type eq 'sourceavail') {
  459: 	return &selectbox($fieldname,
  460: 			  $value,
  461: 			  \&Apache::loncommon::source_copyrightdescription,
  462: 			  (&Apache::loncommon::source_copyrightids)).
  463:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
  464:     }
  465:     # Gradelevels
  466:     if (($type eq 'lowestgradelevel') ||
  467: 	($type eq 'highestgradelevel')) {
  468: 	return &Apache::loncommon::select_level_form($value,$fieldname).
  469:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  470:     }
  471:     # Obsolete
  472:     if ($type eq 'obsolete') {
  473: 	return '<input type="checkbox" name="'.$fieldname.'"'.
  474: 	    ($value?' checked="1"':'').' />'.
  475:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  476:     }
  477:     # Obsolete replacement file
  478:     if ($type eq 'obsoletereplacement') {
  479: 	return '<input type="text" name="'.$fieldname.
  480: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  481: 	    "('".$formname."','".$fieldname."'".
  482: 	    ",'')\">".&mt('Select').'</a>'.
  483:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  484:     }
  485:     # Customdistribution file
  486:     if ($type eq 'customdistributionfile') {
  487: 	return '<input type="text" name="'.$fieldname.
  488: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  489: 	    "('".$formname."','".$fieldname."'".
  490: 	    ",'rights')\">".&mt('Select').'</a>'.
  491:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  492:     }
  493:     # Source Customdistribution file
  494:     if ($type eq 'sourcerights') {
  495: 	return '<input type="text" name="'.$fieldname.
  496: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
  497: 	    "('".$formname."','".$fieldname."'".
  498: 	    ",'rights')\">".&mt('Select').'</a>'.
  499:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
  500:     }
  501:     # Dates
  502:     if (($type eq 'creationdate') ||
  503: 	($type eq 'lastrevisiondate')) {
  504: 	return 
  505:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
  506:             &relatedfield(0,$relatedsearchflag,$relatedsep);
  507:     }
  508:     # No pretty input found
  509:     $value=~s/^\s+//gs;
  510:     $value=~s/\s+$//gs;
  511:     $value=~s/\s+/ /gs;
  512:     $value=~s/\"/\&quot\;/gs;
  513:     return 
  514:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
  515:         'value="'.$value.'" />'.
  516:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
  517:                       $relatedvalue); 
  518: }
  519: 
  520: # Main Handler
  521: sub handler {
  522:     my $r=shift;
  523:     #
  524:     my $uri=$r->uri;
  525:     #
  526:     # Set document type
  527:     &Apache::loncommon::content_type($r,'text/html');
  528:     $r->send_http_header;
  529:     return OK if $r->header_only;
  530:     #
  531:     my ($resdomain,$resuser)=
  532:         (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
  533:     $r->print('<html><head><title>'.
  534:               'Catalog Information'.
  535:               '</title></head>');
  536:     if ($uri=~m:/adm/bombs/(.*)$:) {
  537:         $r->print(&Apache::loncommon::bodytag('Error Messages'));
  538:         # Looking for all bombs?
  539:         &report_bombs($r,$uri);
  540:     } elsif ($uri=~/^\/\~/) { 
  541:         # Construction space
  542:         $r->print(&Apache::loncommon::bodytag
  543:                   ('Edit Catalog Information','','','',$resdomain));
  544:         &present_editable_metadata($r,$uri);
  545:     } else {
  546:         $r->print(&Apache::loncommon::bodytag
  547:                   ('Catalog Information','','','',$resdomain));
  548:         &present_uneditable_metadata($r,$uri);
  549:     }
  550:     $r->print('</body></html>');
  551:     return OK;
  552: }
  553: 
  554: #####################################################
  555: #####################################################
  556: ###                                               ###
  557: ###                Report Bombs                   ###
  558: ###                                               ###
  559: #####################################################
  560: #####################################################
  561: sub report_bombs {
  562:     my ($r,$uri) = @_;
  563:     # Set document type
  564:     $uri =~ s:/adm/bombs/::;
  565:     $uri = &Apache::lonnet::declutter($uri);
  566:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  567:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
  568:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  569:         my %brokenurls = 
  570:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  571:         foreach (sort(keys(%brokenurls))) {
  572:             if ($_=~/^\Q$uri\E/) {
  573:                 $r->print
  574:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  575:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  576:                      '<hr />');
  577:             }
  578:         }
  579:     } else {
  580:         $r->print(&mt('Not authorized'));
  581:     }
  582:     return;
  583: }
  584: 
  585: #####################################################
  586: #####################################################
  587: ###                                               ###
  588: ###        Uneditable Metadata Display            ###
  589: ###                                               ###
  590: #####################################################
  591: #####################################################
  592: sub present_uneditable_metadata {
  593:     my ($r,$uri) = @_;
  594:     #
  595:     my %content=();
  596:     # Read file
  597:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  598:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  599:     }
  600:     # Render Output
  601:     # displayed url
  602:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  603:     $uri=~s/\.meta$//;
  604:     my $disuri=&Apache::lonnet::clutter($uri);
  605:     # version
  606:     my $currentversion=&Apache::lonnet::getversion($disuri);
  607:     my $versiondisplay='';
  608:     if ($thisversion) {
  609:         $versiondisplay=&mt('Version').': '.$thisversion.
  610:             ' ('.&mt('most recent version').': '.
  611:             ($currentversion>0 ? 
  612:              $currentversion   :
  613:              &mt('information not available')).')';
  614:     } else {
  615:         $versiondisplay='Version: '.$currentversion;
  616:     }
  617:     # crumbify displayed URL               uri     target prefix form  size
  618:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  619:     $disuri =~ s:<br />::g;
  620:     # obsolete
  621:     my $obsolete=$content{'obsolete'};
  622:     my $obsoletewarning='';
  623:     if (($obsolete) && ($ENV{'user.adv'})) {
  624:         $obsoletewarning='<p><font color="red">'.
  625:             &mt('This resource has been marked obsolete by the author(s)').
  626:             '</font></p>';
  627:     }
  628:     #
  629:     my %lt=&fieldnames();
  630:     my $table='';
  631:     my $title = $content{'title'};
  632:     if (! defined($title)) {
  633:         $title = 'Untitled Resource';
  634:     }
  635:     foreach ('title', 
  636:              'author', 
  637:              'subject', 
  638:              'keywords', 
  639:              'notes', 
  640:              'abstract',
  641:              'lowestgradelevel',
  642:              'highestgradelevel',
  643:              'standards', 
  644:              'mime', 
  645:              'language', 
  646:              'creationdate', 
  647:              'lastrevisiondate', 
  648:              'owner', 
  649:              'copyright', 
  650:              'customdistributionfile',
  651:              'sourceavail',
  652:              'sourcerights', 
  653:              'obsolete', 
  654:              'obsoletereplacement') {
  655:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
  656:             '</td><td bgcolor="#CCCCCC">'.
  657:             &prettyprint($_,$content{$_}).'</td></tr>';
  658:         delete $content{$_};
  659:     }
  660:     #
  661:     $r->print(<<ENDHEAD);
  662: <h2>$title</h2>
  663: <p>
  664: $disuri<br />
  665: $obsoletewarning
  666: $versiondisplay
  667: </p>
  668: <table cellspacing=2 border=0>
  669: $table
  670: </table>
  671: ENDHEAD
  672:     if ($ENV{'user.adv'}) {
  673:         &print_dynamic_metadata($r,$uri,\%content);
  674:     }
  675:     return;
  676: }
  677: 
  678: sub print_dynamic_metadata {
  679:     my ($r,$uri,$content) = @_;
  680:     #
  681:     my %content = %$content;
  682:     my %lt=&fieldnames();
  683:     #
  684:     my $description = 'Dynamic Metadata (updated periodically)';
  685:     $r->print('<h3>'.&mt($description).'</h3>'.
  686:               &mt('Processing'));
  687:     $r->rflush();
  688:     my %items=&fieldnames();
  689:     my %dynmeta=&dynamicmeta($uri);
  690:     #
  691:     # General Access and Usage Statistics
  692:     if (exists($dynmeta{'count'}) ||
  693:         exists($dynmeta{'sequsage'}) ||
  694:         exists($dynmeta{'comefrom'}) ||
  695:         exists($dynmeta{'goto'}) ||
  696:         exists($dynmeta{'course'})) {
  697:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  698:                   '<table cellspacing=2 border=0>');
  699:         foreach ('count',
  700:                  'sequsage','sequsage_list',
  701:                  'comefrom','comefrom_list',
  702:                  'goto','goto_list',
  703:                  'course','course_list') {
  704:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  705:                       '<td bgcolor="#CCCCCC">'.
  706:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  707:         }
  708:         $r->print('</table>');
  709:     } else {
  710:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
  711:     }
  712:     #
  713:     # Assessment statistics
  714:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  715:         if (exists($dynmeta{'stdno'}) ||
  716:             exists($dynmeta{'avetries'}) ||
  717:             exists($dynmeta{'difficulty'}) ||
  718:             exists($dynmeta{'disc'})) {
  719:             # This is an assessment, print assessment data
  720:             $r->print('<h4>'.
  721:                       &mt('Overall Assessment Statistical Data').
  722:                       '</h4>'.
  723:                       '<table cellspacing=2 border=0>');
  724:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
  725:                       '<td bgcolor="#CCCCCC">'.
  726:                       &prettyprint('stdno',$dynmeta{'stdno'}).
  727:                       '</td>'."</tr>\n");
  728:             foreach ('avetries','difficulty','disc') {
  729:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  730:                           '<td bgcolor="#CCCCCC">'.
  731:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
  732:                           '</td>'."</tr>\n");
  733:             }
  734:             $r->print('</table>');    
  735:         }
  736:         if (exists($dynmeta{'stats'})) {
  737:             #
  738:             # New assessment statistics
  739:             $r->print('<h4>'.
  740:                       &mt('Detailed Assessment Statistical Data').
  741:                       '</h4>');
  742:             my $table = '<table cellspacing=2 border=0>'.
  743:                 '<tr>'.
  744:                 '<th>Course</th>'.
  745:                 '<th>Section(s)</th>'.
  746:                 '<th>Num Students</th>'.
  747:                 '<th>Mean Tries</th>'.
  748:                 '<th>Degree of Difficulty</th>'.
  749:                 '<th>Degree of Discrimination</th>'.
  750:                 '<th>Time of computation</th>'.
  751:                 '</tr>'.$/;
  752:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
  753:                 my $data = $dynmeta{'stats'}->{$identifier};
  754:                 my $course = $data->{'course'};
  755:                 my %courseinfo = &Apache::lonnet::coursedescription($course);
  756:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  757:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
  758:                     next;
  759:                 }
  760:                 $table .= '<tr>';
  761:                 $table .= 
  762:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
  763:                 $table .= 
  764:                     '<td align="right">'.$data->{'sections'}.'</td>';
  765:                 $table .=
  766:                     '<td align="right">'.$data->{'stdno'}.'</td>';
  767:                 foreach ('avetries','difficulty','disc') {
  768:                     $table .= '<td align="right">';
  769:                     if (exists($data->{$_})) {
  770:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
  771:                     } else {
  772:                         $table .= '';
  773:                     }
  774:                     $table .= '</td>';
  775:                 }
  776:                 $table .=
  777:                     '<td><nobr>'.
  778:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
  779:                     '</nobr></td>';
  780:                 $table .=
  781:                     '</tr>'.$/;
  782:             }
  783:             $table .= '</table>'.$/;
  784:             $r->print($table);
  785:         } else {
  786:             $r->print('No new dynamic data found.');
  787:         }
  788:     } else {
  789:         $r->print('<h4>'.
  790:           &mt('No Assessment Statistical Data is available for this resource').
  791:                   '</h4>');
  792:     }
  793: 
  794:     #
  795:     #
  796:     if (exists($dynmeta{'clear'})   || 
  797:         exists($dynmeta{'depth'})   || 
  798:         exists($dynmeta{'helpful'}) || 
  799:         exists($dynmeta{'correct'}) || 
  800:         exists($dynmeta{'technical'})){ 
  801:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
  802:                   '<table cellspacing=2 border=0>');
  803:         foreach ('clear','depth','helpful','correct','technical') {
  804:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  805:                       '<td bgcolor="#CCCCCC">'.
  806:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  807:         }
  808:         $r->print('</table>');
  809:     } else {
  810:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
  811:     }
  812:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
  813:     if ((($ENV{'user.domain'} eq $1) && ($ENV{'user.name'} eq $2))
  814:         || ($ENV{'user.role.ca./'.$1.'/'.$2})) {
  815:         if (exists($dynmeta{'comments'})) {
  816:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
  817:                       &mt('visible to author and co-authors only').
  818:                       ')</h4>'.
  819:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  820:         } else {
  821:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
  822:         }
  823:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
  824:         if (defined($bombs) && $bombs ne '') {
  825:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
  826:                       &mt('visible to author and co-authors only').')'.
  827:                       '</h4>'.$bombs);
  828:         } else {
  829:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
  830:         }
  831:     }
  832:     #
  833:     # All other stuff
  834:     $r->print('<h3>'.
  835:               &mt('Additional Metadata (non-standard, parameters, exports)').
  836:               '</h3><table border="0" cellspacing="1">');
  837:     foreach (sort(keys(%content))) {
  838:         my $name=$_;
  839:         if ($name!~/\.display$/) {
  840:             my $display=&Apache::lonnet::metadata($uri,
  841:                                                   $name.'.display');
  842:             if (! $display) { 
  843:                 $display=$name;
  844:             };
  845:             my $otherinfo='';
  846:             foreach ('name','part','type','default') {
  847:                 if (defined(&Apache::lonnet::metadata($uri,
  848:                                                       $name.'.'.$_))) {
  849:                     $otherinfo.=' '.$_.'='.
  850:                         &Apache::lonnet::metadata($uri,
  851:                                                   $name.'.'.$_).'; ';
  852:                 }
  853:             }
  854:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
  855:             if ($otherinfo) {
  856:                 $r->print(' ('.$otherinfo.')');
  857:             }
  858:             $r->print("</font></td></tr>\n");
  859:         }
  860:     }
  861:     $r->print("</table>");
  862:     return;
  863: }
  864: 
  865: #####################################################
  866: #####################################################
  867: ###                                               ###
  868: ###          Editable metadata display            ###
  869: ###                                               ###
  870: #####################################################
  871: #####################################################
  872: sub present_editable_metadata {
  873:     my ($r,$uri) = @_;
  874:     # Construction Space Call
  875:     # Header
  876:     my $disuri=$uri;
  877:     my $fn=&Apache::lonnet::filelocation('',$uri);
  878:     $disuri=~s/^\/\~/\/priv\//;
  879:     $disuri=~s/\.meta$//;
  880:     my $target=$uri;
  881:     $target=~s/^\/\~/\/res\/$ENV{'request.role.domain'}\//;
  882:     $target=~s/\.meta$//;
  883:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
  884:     if ($bombs) {
  885:         if ($ENV{'form.delmsg'}) {
  886:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
  887:                 $bombs=&mt('Messages deleted.');
  888:             } else {
  889:                 $bombs=&mt('Error deleting messages');
  890:             }
  891:         }
  892:         my $del=&mt('Delete Messages');
  893:         $r->print(<<ENDBOMBS);
  894: <h1>$disuri</h1>
  895: <form method="post" name="defaultmeta">
  896: <input type="submit" name="delmsg" value="$del" />
  897: <br />$bombs
  898: ENDBOMBS
  899:     } else {
  900:         my $displayfile='Catalog Information for '.$disuri;
  901:         if ($disuri=~/\/default$/) {
  902:             my $dir=$disuri;
  903:             $dir=~s/default$//;
  904:             $displayfile=
  905:                 &mt('Default Cataloging Information for Directory').' '.
  906:                 $dir;
  907:         }
  908:         my $bodytag=
  909:             &Apache::loncommon::bodytag('Edit Catalog Information');
  910:         %Apache::lonpublisher::metadatafields=();
  911:         %Apache::lonpublisher::metadatakeys=();
  912:         &Apache::lonpublisher::metaeval(&Apache::lonnet::getfile($fn));
  913:         $r->print(<<ENDEDIT);
  914: <html><head><title>Edit Catalog Information</title></head>
  915: $bodytag
  916: <h1>$displayfile</h1>
  917: <form method="post" name="defaultmeta">
  918: ENDEDIT
  919:         $r->print('<script language="JavaScript">'.
  920:                   &Apache::loncommon::browser_and_searcher_javascript.
  921:                   '</script>');
  922:         my %lt=&fieldnames();
  923:         foreach ('author','title','subject','keywords','abstract','notes',
  924:                  'copyright','customdistributionfile','language',
  925:                  'standards',
  926:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
  927:                  'obsolete','obsoletereplacement') {
  928:             if (defined($ENV{'form.new_'.$_})) {
  929:                 $Apache::lonpublisher::metadatafields{$_}=
  930:                     $ENV{'form.new_'.$_};
  931:             }
  932:             if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
  933:                 $Apache::lonpublisher::metadatafields{'copyright'}=
  934:                     'default';
  935:             }
  936:             $r->print('<p>'.$lt{$_}.': '.
  937:                       &prettyinput
  938:                       ($_,$Apache::lonpublisher::metadatafields{$_},
  939:                        'new_'.$_,'defaultmeta').'</p>');
  940:         }
  941:         if ($ENV{'form.store'}) {
  942:             my $mfh;
  943:             if (!  ($mfh=Apache::File->new('>'.$fn))) {
  944:                 $r->print('<p><font color=red>'.
  945:                           &mt('Could not write metadata').', '.
  946:                           &mt('FAIL').'</font>');
  947:             } else {
  948:                 foreach (sort keys %Apache::lonpublisher::metadatafields) {
  949:                     next if ($_ =~ /\./);
  950:                     my $unikey=$_;
  951:                     $unikey=~/^([A-Za-z]+)/;
  952:                     my $tag=$1;
  953:                     $tag=~tr/A-Z/a-z/;
  954:                     print $mfh "\n\<$tag";
  955:                     foreach (split(/\,/,
  956:                                  $Apache::lonpublisher::metadatakeys{$unikey})
  957:                              ) {
  958:                         my $value=
  959:                          $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
  960:                         $value=~s/\"/\'\'/g;
  961:                         print $mfh ' '.$_.'="'.$value.'"';
  962:                     }
  963:                     print $mfh '>'.
  964:                         &HTML::Entities::encode
  965:                         ($Apache::lonpublisher::metadatafields{$unikey},
  966:                          '<>&"').
  967:                          '</'.$tag.'>';
  968:                 }
  969:                 $r->print('<p>'.&mt('Wrote Metadata'));
  970:             }
  971:         }
  972:         $r->print('<br /><input type="submit" name="store" value="'.
  973:                   &mt('Store Catalog Information').'">');
  974:     }
  975:     $r->print('</form>');
  976:     return;
  977: }
  978: 
  979: 1;
  980: __END__

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