File:  [LON-CAPA] / loncom / interface / lonmeta.pm
Revision 1.89: download - view: text, annotated - select for diffs
Wed Jan 19 01:28:33 2005 UTC (19 years, 4 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Actually writes the meta data. Next to to retrieve it!

    1: # The LearningOnline Network with CAPA
    2: # Metadata display handler
    3: #
    4: # $Id: lonmeta.pm,v 1.89 2005/01/19 01:28:33 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=~/\/portfolio\//) {
  541:         $r->print(&Apache::loncommon::bodytag
  542:           ('Edit Portfolio File Information','','','',$resdomain));
  543:         &present_editable_metadata($r,$uri);
  544:         
  545:     } elsif ($uri=~/^\/\~/) { 
  546:         # Construction space
  547:         $r->print(&Apache::loncommon::bodytag
  548:                   ('Edit Catalog Information','','','',$resdomain));
  549:         &present_editable_metadata($r,$uri);
  550:     } else {
  551:         $r->print(&Apache::loncommon::bodytag
  552: 		  ('Catalog Information','','','',$resdomain));
  553:         &present_uneditable_metadata($r,$uri);
  554:     }
  555:     $r->print('</body></html>');
  556:     return OK;
  557: }
  558: 
  559: #####################################################
  560: #####################################################
  561: ###                                               ###
  562: ###                Report Bombs                   ###
  563: ###                                               ###
  564: #####################################################
  565: #####################################################
  566: sub report_bombs {
  567:     my ($r,$uri) = @_;
  568:     # Set document type
  569:     $uri =~ s:/adm/bombs/::;
  570:     $uri = &Apache::lonnet::declutter($uri);
  571:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
  572:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
  573:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
  574:         my %brokenurls = 
  575:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
  576:         foreach (sort(keys(%brokenurls))) {
  577:             if ($_=~/^\Q$uri\E/) {
  578:                 $r->print
  579:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
  580:                      &Apache::lonmsg::retrieve_author_res_msg($_).
  581:                      '<hr />');
  582:             }
  583:         }
  584:     } else {
  585:         $r->print(&mt('Not authorized'));
  586:     }
  587:     return;
  588: }
  589: 
  590: #####################################################
  591: #####################################################
  592: ###                                               ###
  593: ###        Uneditable Metadata Display            ###
  594: ###                                               ###
  595: #####################################################
  596: #####################################################
  597: sub present_uneditable_metadata {
  598:     my ($r,$uri) = @_;
  599:     #
  600:     my %content=();
  601:     # Read file
  602:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
  603:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
  604:     }
  605:     # Render Output
  606:     # displayed url
  607:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
  608:     $uri=~s/\.meta$//;
  609:     my $disuri=&Apache::lonnet::clutter($uri);
  610:     # version
  611:     my $currentversion=&Apache::lonnet::getversion($disuri);
  612:     my $versiondisplay='';
  613:     if ($thisversion) {
  614:         $versiondisplay=&mt('Version').': '.$thisversion.
  615:             ' ('.&mt('most recent version').': '.
  616:             ($currentversion>0 ? 
  617:              $currentversion   :
  618:              &mt('information not available')).')';
  619:     } else {
  620:         $versiondisplay='Version: '.$currentversion;
  621:     }
  622:     # crumbify displayed URL               uri     target prefix form  size
  623:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
  624:     $disuri =~ s:<br />::g;
  625:     # obsolete
  626:     my $obsolete=$content{'obsolete'};
  627:     my $obsoletewarning='';
  628:     if (($obsolete) && ($ENV{'user.adv'})) {
  629:         $obsoletewarning='<p><font color="red">'.
  630:             &mt('This resource has been marked obsolete by the author(s)').
  631:             '</font></p>';
  632:     }
  633:     #
  634:     my %lt=&fieldnames();
  635:     my $table='';
  636:     my $title = $content{'title'};
  637:     if (! defined($title)) {
  638:         $title = 'Untitled Resource';
  639:     }
  640:     foreach ('title', 
  641:              'author', 
  642:              'subject', 
  643:              'keywords', 
  644:              'notes', 
  645:              'abstract',
  646:              'lowestgradelevel',
  647:              'highestgradelevel',
  648:              'standards', 
  649:              'mime', 
  650:              'language', 
  651:              'creationdate', 
  652:              'lastrevisiondate', 
  653:              'owner', 
  654:              'copyright', 
  655:              'customdistributionfile',
  656:              'sourceavail',
  657:              'sourcerights', 
  658:              'obsolete', 
  659:              'obsoletereplacement') {
  660:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$_}.
  661:             '</td><td bgcolor="#CCCCCC">'.
  662:             &prettyprint($_,$content{$_}).'</td></tr>';
  663:         delete $content{$_};
  664:     }
  665:     #
  666:     $r->print(<<ENDHEAD);
  667: <h2>$title</h2>
  668: <p>
  669: $disuri<br />
  670: $obsoletewarning
  671: $versiondisplay
  672: </p>
  673: <table cellspacing="2" border="0">
  674: $table
  675: </table>
  676: ENDHEAD
  677:     if ($ENV{'user.adv'}) {
  678:         &print_dynamic_metadata($r,$uri,\%content);
  679:     }
  680:     return;
  681: }
  682: 
  683: sub print_dynamic_metadata {
  684:     my ($r,$uri,$content) = @_;
  685:     #
  686:     my %content = %$content;
  687:     my %lt=&fieldnames();
  688:     #
  689:     my $description = 'Dynamic Metadata (updated periodically)';
  690:     $r->print('<h3>'.&mt($description).'</h3>'.
  691:               &mt('Processing'));
  692:     $r->rflush();
  693:     my %items=&fieldnames();
  694:     my %dynmeta=&dynamicmeta($uri);
  695:     #
  696:     # General Access and Usage Statistics
  697:     if (exists($dynmeta{'count'}) ||
  698:         exists($dynmeta{'sequsage'}) ||
  699:         exists($dynmeta{'comefrom'}) ||
  700:         exists($dynmeta{'goto'}) ||
  701:         exists($dynmeta{'course'})) {
  702:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
  703:                   '<table cellspacing="2" border="0">');
  704:         foreach ('count',
  705:                  'sequsage','sequsage_list',
  706:                  'comefrom','comefrom_list',
  707:                  'goto','goto_list',
  708:                  'course','course_list') {
  709:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  710:                       '<td bgcolor="#CCCCCC">'.
  711:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  712:         }
  713:         $r->print('</table>');
  714:     } else {
  715:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
  716:     }
  717:     #
  718:     # Assessment statistics
  719:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
  720:         if (exists($dynmeta{'stdno'}) ||
  721:             exists($dynmeta{'avetries'}) ||
  722:             exists($dynmeta{'difficulty'}) ||
  723:             exists($dynmeta{'disc'})) {
  724:             # This is an assessment, print assessment data
  725:             $r->print('<h4>'.
  726:                       &mt('Overall Assessment Statistical Data').
  727:                       '</h4>'.
  728:                       '<table cellspacing="2" border="0">');
  729:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
  730:                       '<td bgcolor="#CCCCCC">'.
  731:                       &prettyprint('stdno',$dynmeta{'stdno'}).
  732:                       '</td>'."</tr>\n");
  733:             foreach ('avetries','difficulty','disc') {
  734:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  735:                           '<td bgcolor="#CCCCCC">'.
  736:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
  737:                           '</td>'."</tr>\n");
  738:             }
  739:             $r->print('</table>');    
  740:         }
  741:         if (exists($dynmeta{'stats'})) {
  742:             #
  743:             # New assessment statistics
  744:             $r->print('<h4>'.
  745:                       &mt('Detailed Assessment Statistical Data').
  746:                       '</h4>');
  747:             my $table = '<table cellspacing="2" border="0">'.
  748:                 '<tr>'.
  749:                 '<th>Course</th>'.
  750:                 '<th>Section(s)</th>'.
  751:                 '<th>Num Students</th>'.
  752:                 '<th>Mean Tries</th>'.
  753:                 '<th>Degree of Difficulty</th>'.
  754:                 '<th>Degree of Discrimination</th>'.
  755:                 '<th>Time of computation</th>'.
  756:                 '</tr>'.$/;
  757:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
  758:                 my $data = $dynmeta{'stats'}->{$identifier};
  759:                 my $course = $data->{'course'};
  760:                 my %courseinfo = &Apache::lonnet::coursedescription($course);
  761:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
  762:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
  763:                     next;
  764:                 }
  765:                 $table .= '<tr>';
  766:                 $table .= 
  767:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
  768:                 $table .= 
  769:                     '<td align="right">'.$data->{'sections'}.'</td>';
  770:                 $table .=
  771:                     '<td align="right">'.$data->{'stdno'}.'</td>';
  772:                 foreach ('avetries','difficulty','disc') {
  773:                     $table .= '<td align="right">';
  774:                     if (exists($data->{$_})) {
  775:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
  776:                     } else {
  777:                         $table .= '';
  778:                     }
  779:                     $table .= '</td>';
  780:                 }
  781:                 $table .=
  782:                     '<td><nobr>'.
  783:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
  784:                     '</nobr></td>';
  785:                 $table .=
  786:                     '</tr>'.$/;
  787:             }
  788:             $table .= '</table>'.$/;
  789:             $r->print($table);
  790:         } else {
  791:             $r->print('No new dynamic data found.');
  792:         }
  793:     } else {
  794:         $r->print('<h4>'.
  795:           &mt('No Assessment Statistical Data is available for this resource').
  796:                   '</h4>');
  797:     }
  798: 
  799:     #
  800:     #
  801:     if (exists($dynmeta{'clear'})   || 
  802:         exists($dynmeta{'depth'})   || 
  803:         exists($dynmeta{'helpful'}) || 
  804:         exists($dynmeta{'correct'}) || 
  805:         exists($dynmeta{'technical'})){ 
  806:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
  807:                   '<table cellspacing="2" border="0">');
  808:         foreach ('clear','depth','helpful','correct','technical') {
  809:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
  810:                       '<td bgcolor="#CCCCCC">'.
  811:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
  812:         }
  813:         $r->print('</table>');
  814:     } else {
  815:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
  816:     }
  817:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
  818:     if ((($ENV{'user.domain'} eq $1) && ($ENV{'user.name'} eq $2))
  819:         || ($ENV{'user.role.ca./'.$1.'/'.$2})) {
  820:         if (exists($dynmeta{'comments'})) {
  821:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
  822:                       &mt('visible to author and co-authors only').
  823:                       ')</h4>'.
  824:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
  825:         } else {
  826:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
  827:         }
  828:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
  829:         if (defined($bombs) && $bombs ne '') {
  830:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
  831:                       &mt('visible to author and co-authors only').')'.
  832:                       '</h4>'.$bombs);
  833:         } else {
  834:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
  835:         }
  836:     }
  837:     #
  838:     # All other stuff
  839:     $r->print('<h3>'.
  840:               &mt('Additional Metadata (non-standard, parameters, exports)').
  841:               '</h3><table border="0" cellspacing="1">');
  842:     foreach (sort(keys(%content))) {
  843:         my $name=$_;
  844:         if ($name!~/\.display$/) {
  845:             my $display=&Apache::lonnet::metadata($uri,
  846:                                                   $name.'.display');
  847:             if (! $display) { 
  848:                 $display=$name;
  849:             };
  850:             my $otherinfo='';
  851:             foreach ('name','part','type','default') {
  852:                 if (defined(&Apache::lonnet::metadata($uri,
  853:                                                       $name.'.'.$_))) {
  854:                     $otherinfo.=' '.$_.'='.
  855:                         &Apache::lonnet::metadata($uri,
  856:                                                   $name.'.'.$_).'; ';
  857:                 }
  858:             }
  859:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
  860:             if ($otherinfo) {
  861:                 $r->print(' ('.$otherinfo.')');
  862:             }
  863:             $r->print("</font></td></tr>\n");
  864:         }
  865:     }
  866:     $r->print("</table>");
  867:     return;
  868: }
  869: 
  870: #####################################################
  871: #####################################################
  872: ###                                               ###
  873: ###          Editable metadata display            ###
  874: ###                                               ###
  875: #####################################################
  876: #####################################################
  877: sub present_editable_metadata {
  878:     my ($r,$uri) = @_;
  879:     # Construction Space Call
  880:     # Header
  881:     my $disuri=$uri;
  882:     my $fn=&Apache::lonnet::filelocation('',$uri);
  883:     $disuri=~s/^\/\~/\/priv\//;
  884:     $disuri=~s/\.meta$//;
  885:     my $target=$uri;
  886:     $target=~s/^\/\~/\/res\/$ENV{'request.role.domain'}\//;
  887:     $target=~s/\.meta$//;
  888:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
  889:     if ($bombs) {
  890:         if ($ENV{'form.delmsg'}) {
  891:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
  892:                 $bombs=&mt('Messages deleted.');
  893:             } else {
  894:                 $bombs=&mt('Error deleting messages');
  895:             }
  896:         }
  897:         my $del=&mt('Delete Messages');
  898:         $r->print(<<ENDBOMBS);
  899: <h1>$disuri</h1>
  900: <form method="post" name="defaultmeta">
  901: <input type="submit" name="delmsg" value="$del" />
  902: <br />$bombs
  903: ENDBOMBS
  904:     } else {
  905:         my $displayfile='Catalog Information for '.$disuri;
  906:         if ($disuri=~/\/default$/) {
  907:             my $dir=$disuri;
  908:             $dir=~s/default$//;
  909:             $displayfile=
  910:                 &mt('Default Cataloging Information for Directory').' '.
  911:                 $dir;
  912:         }
  913:         %Apache::lonpublisher::metadatafields=();
  914:         %Apache::lonpublisher::metadatakeys=();
  915:         &Apache::lonpublisher::metaeval(&Apache::lonnet::getfile($fn));
  916:         $r->print(<<ENDEDIT);
  917: <h1>$displayfile</h1>
  918: <form method="post" name="defaultmeta">
  919: ENDEDIT
  920:         $r->print('<script language="JavaScript">'.
  921:                   &Apache::loncommon::browser_and_searcher_javascript().
  922:                   '</script>');
  923:         my %lt=&fieldnames();
  924: 	my $output;
  925:         foreach ('author','title','subject','keywords','abstract','notes',
  926:                  'copyright','customdistributionfile','language',
  927:                  'standards',
  928:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
  929:                  'obsolete','obsoletereplacement') {
  930:             if (defined($ENV{'form.new_'.$_})) {
  931:                 $Apache::lonpublisher::metadatafields{$_}=
  932:                     $ENV{'form.new_'.$_};
  933:             }
  934:             if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
  935:                 $Apache::lonpublisher::metadatafields{'copyright'}=
  936:                     'default';
  937:             }
  938:             $output.=('<p>'.$lt{$_}.': '.
  939:                       &prettyinput($_,
  940: 				   $Apache::lonpublisher::metadatafields{$_},
  941: 				   'new_'.$_,'defaultmeta').'</p>');
  942:         }
  943:         if ($ENV{'form.store'}) {
  944:             my $mfh;
  945:             if (!  ($mfh=Apache::File->new('>'.$fn))) {
  946:                 $r->print('<p><font color="red">'.
  947:                           &mt('Could not write metadata').', '.
  948:                           &mt('FAIL').'</font></p>');
  949:             } else {
  950:                 foreach (sort keys %Apache::lonpublisher::metadatafields) {
  951:                     next if ($_ =~ /\./);
  952:                     my $unikey=$_;
  953:                     $unikey=~/^([A-Za-z]+)/;
  954:                     my $tag=$1;
  955:                     $tag=~tr/A-Z/a-z/;
  956:                     print $mfh "\n\<$tag";
  957:                     foreach (split(/\,/,
  958:                                  $Apache::lonpublisher::metadatakeys{$unikey})
  959:                              ) {
  960:                         my $value=
  961:                          $Apache::lonpublisher::metadatafields{$unikey.'.'.$_};
  962:                         $value=~s/\"/\'\'/g;
  963:                         print $mfh ' '.$_.'="'.$value.'"';
  964:                     }
  965:                     print $mfh '>'.
  966:                         &HTML::Entities::encode
  967:                         ($Apache::lonpublisher::metadatafields{$unikey},
  968:                          '<>&"').
  969:                          '</'.$tag.'>';
  970:                 }
  971:                 $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
  972: 			  ' '.&Apache::lonlocal::locallocaltime(time).
  973: 			  '</font></p>');
  974:             }
  975:         }
  976: 	$r->print($output.'<br /><input type="submit" name="store" value="'.
  977:                   &mt('Store Catalog Information').'">');
  978:     }
  979:     $r->print('</form>');
  980:     return;
  981: }
  982: 
  983: 1;
  984: __END__

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