File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.82: download - view: text, annotated - select for diffs
Thu Jun 17 18:22:13 2004 UTC (20 years ago) by www
Branches: MAIN
CVS tags: HEAD
"People who got this also get that" now works. Still needs stats.

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