File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.80: download - view: text, annotated - select for diffs
Wed Jun 16 18:33:46 2004 UTC (20 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added &get_dynamic_metadata_from_sql for gerd.

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

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