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

1.1       www         1: # The LearningOnline Network with CAPA
1.8       albertel    2: # Metadata display handler
                      3: #
1.176   ! banghart    4: # $Id: lonmeta.pm,v 1.175 2006/09/13 20:12:20 banghart Exp $
1.8       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
1.1       www        14: #
1.8       albertel   15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
1.100     banghart   20: # You should have received a copy of the GNU General Public License 
1.8       albertel   21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.44      www        27: 
1.1       www        28: 
                     29: package Apache::lonmeta;
                     30: 
                     31: use strict;
1.63      matthew    32: use LONCAPA::lonmetadata();
1.1       www        33: use Apache::Constants qw(:common);
1.96      albertel   34: use Apache::lonnet;
1.10      www        35: use Apache::loncommon();
1.100     banghart   36: use Apache::lonhtmlcommon(); 
1.23      www        37: use Apache::lonmsg;
                     38: use Apache::lonpublisher;
1.35      www        39: use Apache::lonlocal;
1.43      www        40: use Apache::lonmysql;
1.49      www        41: use Apache::lonmsg;
1.157     www        42: use lib '/home/httpd/lib/perl/';
                     43: use LONCAPA;
1.1       www        44: 
1.44      www        45: 
1.80      matthew    46: ############################################################
                     47: ############################################################
                     48: ##
                     49: ## &get_dynamic_metadata_from_sql($url)
                     50: ## 
                     51: ## Queries sql database for dynamic metdata
                     52: ## Returns a hash of hashes, with keys of urls which match $url
                     53: ## Returned fields are given below.
                     54: ##
                     55: ## Examples:
                     56: ## 
                     57: ## %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
                     58: ##     ('/res/msu/korte/');
                     59: ##
                     60: ## $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
                     61: ##
                     62: ############################################################
                     63: ############################################################
                     64: sub get_dynamic_metadata_from_sql {
                     65:     my ($url) = shift();
                     66:     my ($authordom,$author)=($url=~m:^/res/(\w+)/(\w+)/:);
                     67:     if (! defined($authordom)) {
                     68:         $authordom = shift();
                     69:     }
                     70:     if  (! defined($author)) { 
                     71:         $author = shift();
                     72:     }
                     73:     if (! defined($authordom) || ! defined($author)) {
                     74:         return ();
                     75:     }
1.158     www        76:     my $query = 'SELECT * FROM metadata WHERE url LIKE "'.$url.'%"';
1.80      matthew    77:     my $server = &Apache::lonnet::homeserver($author,$authordom);
                     78:     my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
                     79:                                                 ,[$server]);
                     80:     return () if (! defined($reply) || ref($reply) ne 'HASH');
                     81:     my $filename = $reply->{$server};
                     82:     if (! defined($filename) || $filename =~ /^error/) {
                     83:         return ();
                     84:     }
                     85:     my $max_time = time + 10; # wait 10 seconds for results at most
                     86:     my %ReturnHash;
                     87:     #
                     88:     # Look for results
                     89:     my $finished = 0;
                     90:     while (! $finished && time < $max_time) {
                     91:         my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
                     92:         if (! -e "$datafile.end") { next; }
                     93:         my $fh;
                     94:         if (!($fh=Apache::File->new($datafile))) { next; }
                     95:         while (my $result = <$fh>) {
                     96:             chomp($result);
                     97:             next if (! $result);
1.158     www        98:             my %hash=&LONCAPA::lonmetadata::metadata_col_to_hash(map { &unescape($_) } split(/\,/,$result));
                     99:             foreach my $key (keys(%hash)) {
                    100:                 $ReturnHash{$hash{'url'}}->{$key}=$hash{$key};
1.80      matthew   101:             }
                    102:         }
                    103:         $finished = 1;
                    104:     }
                    105:     #
                    106:     return %ReturnHash;
                    107: }
                    108: 
                    109: 
1.64      matthew   110: # Fetch and evaluate dynamic metadata
1.9       www       111: sub dynamicmeta {
                    112:     my $url=&Apache::lonnet::declutter(shift);
                    113:     $url=~s/\.meta$//;
                    114:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
1.19      www       115:     my $regexp=$url;
1.9       www       116:     $regexp=~s/(\W)/\\$1/g;
1.10      www       117:     $regexp='___'.$regexp.'___';
1.16      albertel  118:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
                    119: 				       $aauthor,$regexp);
1.63      matthew   120:     my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
                    121:     my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
                    122:                                                                \%DynamicData);
1.40      matthew   123:     #
1.46      www       124:     # Deal with 'count' separately
1.63      matthew   125:     $Data{'count'} = &access_count($url,$aauthor,$adomain);
1.67      matthew   126:     #
                    127:     # Debugging code I will probably need later
                    128:     if (0) {
                    129:         &Apache::lonnet::logthis('Dynamic Metadata');
                    130:         while(my($k,$v)=each(%Data)){
                    131:             &Apache::lonnet::logthis('    "'.$k.'"=>"'.$v.'"');
                    132:         }
                    133:         &Apache::lonnet::logthis('-------------------');
                    134:     }
1.63      matthew   135:     return %Data;
1.40      matthew   136: }
                    137: 
                    138: sub access_count {
                    139:     my ($src,$author,$adomain) = @_;
                    140:     my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
                    141:                                         $author,$src);
                    142:     if (! exists($countdata{$src})) {
1.47      www       143:         return &mt('Not Available');
1.40      matthew   144:     } else {
                    145:         return $countdata{$src};
                    146:     }
1.25      www       147: }
                    148: 
1.64      matthew   149: # Try to make an alt tag if there is none
1.25      www       150: sub alttag {
1.26      www       151:     my ($base,$src)=@_;
                    152:     my $fullpath=&Apache::lonnet::hreflocation($base,$src);
                    153:     my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
1.64      matthew   154:         &Apache::lonnet::metadata($fullpath,'subject').' '.
                    155:         &Apache::lonnet::metadata($fullpath,'abstract');
1.26      www       156:     $alttag=~s/\s+/ /gs;
                    157:     $alttag=~s/\"//gs;
                    158:     $alttag=~s/\'//gs;
                    159:     $alttag=~s/\s+$//gs;
                    160:     $alttag=~s/^\s+//gs;
1.64      matthew   161:     if ($alttag) { 
                    162:         return $alttag; 
                    163:     } else { 
                    164:         return &mt('No information available'); 
                    165:     }
1.9       www       166: }
1.1       www       167: 
1.64      matthew   168: # Author display
1.29      www       169: sub authordisplay {
                    170:     my ($aname,$adom)=@_;
1.64      matthew   171:     return &Apache::loncommon::aboutmewrapper
                    172:         (&Apache::loncommon::plainname($aname,$adom),
1.164     albertel  173:          $aname,$adom,'preview').' <tt>['.$aname.':'.$adom.']</tt>';
1.29      www       174: }
                    175: 
1.64      matthew   176: # Pretty display
1.12      www       177: sub evalgraph {
                    178:     my $value=shift;
1.65      matthew   179:     if (! $value) { 
                    180:         return '';
                    181:     }
1.12      www       182:     my $val=int($value*10.+0.5)-10;
1.71      matthew   183:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
1.12      www       184:     if ($val>=20) {
1.71      matthew   185: 	$output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
1.12      www       186:     } else {
1.71      matthew   187:         $output.='<td width="'.($val).'" bgcolor="#555555">&nbsp;</td>'.
                    188:                  '<td width="'.(20-$val).'" bgcolor="#FF3333">&nbsp;</td>';
1.12      www       189:     }
                    190:     $output.='<td bgcolor="#FFFF33">&nbsp;</td>';
                    191:     if ($val>20) {
1.71      matthew   192: 	$output.='<td width="'.($val-20).'" bgcolor="#33FF33">&nbsp;</td>'.
                    193:                  '<td width="'.(40-$val).'" bgcolor="#555555">&nbsp;</td>';
1.12      www       194:     } else {
1.71      matthew   195:         $output.='<td width="20" bgcolor="#555555">&nbsp&nbsp;</td>';
1.12      www       196:     }
1.71      matthew   197:     $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
1.12      www       198:     return $output;
                    199: }
                    200: 
                    201: sub diffgraph {
                    202:     my $value=shift;
1.65      matthew   203:     if (! $value) { 
                    204:         return '';
                    205:     }
1.12      www       206:     my $val=int(40.0*$value+0.5);
1.13      www       207:     my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
                    208:                 '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
1.71      matthew   209:     my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
1.12      www       210:     for (my $i=0;$i<8;$i++) {
                    211: 	if ($val>$i*5) {
1.71      matthew   212:             $output.='<td width="5" bgcolor="'.$colors[$i].'">&nbsp;</td>';
1.12      www       213:         } else {
1.71      matthew   214: 	    $output.='<td width="5" bgcolor="#555555">&nbsp;</td>';
1.12      www       215: 	}
                    216:     }
1.71      matthew   217:     $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
1.12      www       218:     return $output;
                    219: }
                    220: 
1.44      www       221: 
1.64      matthew   222: # The field names
1.45      www       223: sub fieldnames {
1.90      banghart  224:     my $file_type=shift;
1.115     banghart  225:     my %fields = 
                    226:         ('title' => 'Title',
1.113     banghart  227:          'author' =>'Author(s)',
1.131     albertel  228:          'authorspace' => 'Author Space',
                    229:          'modifyinguser' => 'Last Modifying User',
1.113     banghart  230:          'subject' => 'Subject',
1.133     banghart  231:          'standards' => 'Standards',
1.113     banghart  232:          'keywords' => 'Keyword(s)',
                    233:          'notes' => 'Notes',
                    234:          'abstract' => 'Abstract',
                    235:          'lowestgradelevel' => 'Lowest Grade Level',
1.149     albertel  236:          'highestgradelevel' => 'Highest Grade Level');
                    237:     
                    238:     if (! defined($file_type) || $file_type ne 'portfolio') {
                    239:         %fields = 
1.93      matthew   240:         (%fields,
                    241:          'domain' => 'Domain',
1.64      matthew   242:          'mime' => 'MIME Type',
                    243:          'language' => 'Language',
                    244:          'creationdate' => 'Creation Date',
                    245:          'lastrevisiondate' => 'Last Revision Date',
                    246:          'owner' => 'Publisher/Owner',
                    247:          'copyright' => 'Copyright/Distribution',
                    248:          'customdistributionfile' => 'Custom Distribution File',
1.84      banghart  249:          'sourceavail' => 'Source Available',
1.78      taceyjo1  250:          'sourcerights' => 'Source Custom Distribution File',
1.64      matthew   251:          'obsolete' => 'Obsolete',
                    252:          'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
                    253:          'count'      => 'Network-wide number of accesses (hits)',
                    254:          'course'     => 'Network-wide number of courses using resource',
                    255:          'course_list' => 'Network-wide courses using resource',
                    256:          'sequsage'      => 'Number of resources using or importing resource',
                    257:          'sequsage_list' => 'Resources using or importing resource',
                    258:          'goto'       => 'Number of resources that follow this resource in maps',
                    259:          'goto_list'  => 'Resources that follow this resource in maps',
                    260:          'comefrom'   => 'Number of resources that lead up to this resource in maps',
                    261:          'comefrom_list' => 'Resources that lead up to this resource in maps',
                    262:          'clear'      => 'Material presented in clear way',
                    263:          'depth'      => 'Material covered with sufficient depth',
                    264:          'helpful'    => 'Material is helpful',
                    265:          'correct'    => 'Material appears to be correct',
                    266:          'technical'  => 'Resource is technically correct', 
                    267:          'avetries'   => 'Average number of tries till solved',
                    268:          'stdno'      => 'Total number of students who have worked on this problem',
1.73      matthew   269:          'difficulty' => 'Degree of difficulty',
                    270:          'disc'       => 'Degree of discrimination',
1.133     banghart  271: 	     'dependencies' => 'Resources used by this resource',
1.64      matthew   272:          );
1.93      matthew   273:     }
                    274:     return &Apache::lonlocal::texthash(%fields);
1.45      www       275: }
1.141     albertel  276: 
1.146     albertel  277: sub portfolio_linked_path {
1.155     albertel  278:     my ($path,$group,$port_path) = @_;
                    279: 
                    280:     my $start = 'portfolio';
                    281:     if ($group) {
                    282: 	$start = "groups/$group/".$start;
                    283:     }
1.166     banghart  284:     my %anchor_fields = (
1.165     banghart  285:         'selectfile'  => $start,
                    286:         'currentpath' => '/'
                    287:     );
                    288:     my $result = &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$start);
1.146     albertel  289:     my $fullpath = '/';
                    290:     my (undef,@tree) = split('/',$path);
1.147     albertel  291:     my $filename = pop(@tree);
1.146     albertel  292:     foreach my $dir (@tree) {
                    293: 	$fullpath .= $dir.'/';
                    294: 	$result .= '/';
1.166     banghart  295: 	my %anchor_fields = (
1.165     banghart  296:             'selectfile'  => $dir,
                    297:             'currentpath' => $fullpath
                    298:         );
                    299: 	$result .= &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$dir);
1.146     albertel  300:     }
1.147     albertel  301:     $result .= "/$filename";
1.146     albertel  302:     return $result;
                    303: }
                    304: 
1.156     albertel  305: sub get_port_path_and_group {
                    306:     my ($uri)=@_;
                    307: 
1.155     albertel  308:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    309:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.156     albertel  310: 
1.155     albertel  311:     my ($port_path,$group);
                    312:     if ($uri =~ m{^/editupload/\Q$cdom\E/\Q$cnum\E/groups/}) {
                    313: 	$group = (split('/',$uri))[5];
                    314: 	$port_path = '/adm/coursegrp_portfolio';
                    315:     } else {
                    316: 	$port_path = '/adm/portfolio';
                    317:     }
1.160     albertel  318:     if ($env{'form.group'} ne $group) {
1.161     albertel  319: 	$env{'form.group'} = $group;
1.160     albertel  320:     }
1.156     albertel  321:     return ($port_path,$group);
                    322: }
                    323: 
                    324: sub portfolio_display_uri {
                    325:     my ($uri,$as_links)=@_;
                    326: 
                    327:     my ($port_path,$group) = &get_port_path_and_group($uri);
                    328: 
1.144     albertel  329:     $uri =~ s|.*/(portfolio/.*)$|$1|;
1.141     albertel  330:     my ($res_uri,$meta_uri) = ($uri,$uri);
                    331:     if ($uri =~ /\.meta$/) {
                    332: 	$res_uri =~ s/\.meta//;
                    333:     } else {
                    334: 	$meta_uri .= '.meta';
                    335:     }
1.146     albertel  336: 
1.148     albertel  337:     my ($path) = ($res_uri =~ m|^portfolio(.*/)[^/]*$|);
1.146     albertel  338:     if ($as_links) {
1.155     albertel  339: 	$res_uri = &portfolio_linked_path($res_uri,$group,$port_path);
                    340: 	$meta_uri = &portfolio_linked_path($meta_uri,$group,$port_path);
1.146     albertel  341:     }
1.145     albertel  342:     return ($res_uri,$meta_uri,$path);
1.141     albertel  343: }
                    344: 
1.140     banghart  345: sub pre_select_course {
                    346:     my ($r,$uri) = @_;
                    347:     my $output;
                    348:     my $fn=&Apache::lonnet::filelocation('',$uri);
1.145     albertel  349:     my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri);
1.140     banghart  350:     %Apache::lonpublisher::metadatafields=();
                    351:     %Apache::lonpublisher::metadatakeys=();
                    352:     my $result=&Apache::lonnet::getfile($fn);
                    353:     if ($result == -1){
1.141     albertel  354:         $r->print(&mt('Creating new file [_1]'),$meta_uri);
1.140     banghart  355:     } else {
                    356:         &Apache::lonpublisher::metaeval($result);
                    357:     }
1.141     albertel  358:     $r->print('<hr /><form method="post" action="" >');
                    359:     $r->print('<p>'.&mt('If you would like to associate this resource ([_1]) with a current or previous course, please select one from the list below, otherwise select, \'None\'','<tt>'.$res_uri.'</tt>').'</p>');
1.140     banghart  360:     $output = &select_course();
                    361:     $r->print($output.'<br /><input type="submit" name="store" value="'.
1.167     banghart  362:                   &mt('Associate Resource With Selected Course').'" />');
1.169     banghart  363:     $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
                    364:     $r->print('<input type="hidden" name="associate" value="true" />');
1.140     banghart  365:     $r->print('</form>');
1.145     albertel  366:     
1.156     albertel  367:     my ($port_path,$group) = &get_port_path_and_group($uri);
1.167     banghart  368:     my $group_input;
                    369:     if ($group) {
1.168     banghart  370:         $group_input = '<input type="hidden" name="group" value="'.$group.'" />';
                    371:     } 
1.167     banghart  372:     $r->print('<br /><br /><form method="post" action="'.$port_path.'">'.
1.145     albertel  373:               '<input type="hidden" name="currentpath" value="'.$path.'" />'.
1.167     banghart  374: 	      $group_input.
                    375: 	      '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
1.145     albertel  376: 	      '</form>');
                    377: 
1.140     banghart  378:     return;
                    379: }
1.100     banghart  380: sub select_course {
1.150     albertel  381:     my $output=$/;
                    382:     my $current_restriction=
                    383: 	$Apache::lonpublisher::metadatafields{'courserestricted'};
                    384:     my $selected = ($current_restriction eq 'none' ? 'selected="selected"' 
                    385: 		                                   : '');
                    386: 
                    387:     $output .= '<select name="new_courserestricted" >';
                    388:     $output .= '<option value="none" '.$selected.'>'.
                    389: 	&mt('None').'</option>'.$/;
1.113     banghart  390:     my %courses;
1.150     albertel  391:     foreach my $key (keys(%env)) {
                    392:         if ($key !~ m/^course\.(.+)\.description$/) { next; }
                    393: 	my $cid = $1;
                    394:         if ($env{$key} !~ /\S/) { next; }
                    395: 	$courses{$key} = $cid;
                    396:     }
                    397:     foreach my $key (sort { lc($env{$a}) cmp lc($env{$b}) } (keys(%courses))) {
                    398: 	my $cid = 'course.'.$courses{$key};
                    399: 	my $selected = ($current_restriction eq $cid ? 'selected="selected"' 
                    400: 		                                     : '');
                    401:         if ($env{$key} !~ /\S/) { next; }
                    402: 	$output .= '<option value="'.$cid.'" '.$selected.'>';
                    403: 	$output .= $env{$key};
                    404: 	$output .= '</option>'.$/;
                    405: 	$selected = '';
1.100     banghart  406:     }
1.138     banghart  407:     $output .= '</select><br />';
1.137     banghart  408:     return ($output);
1.100     banghart  409: }
1.64      matthew   410: # Pretty printing of metadata field
1.46      www       411: 
                    412: sub prettyprint {
1.82      www       413:     my ($type,$value,$target,$prefix,$form,$noformat)=@_;
                    414: # $target,$prefix,$form are optional and for filecrumbs only
1.65      matthew   415:     if (! defined($value)) { 
                    416:         return '&nbsp;'; 
                    417:     }
1.64      matthew   418:     # Title
1.46      www       419:     if ($type eq 'title') {
                    420: 	return '<font size="+1" face="arial">'.$value.'</font>';
                    421:     }
1.64      matthew   422:     # Dates
1.46      www       423:     if (($type eq 'creationdate') ||
                    424: 	($type eq 'lastrevisiondate')) {
1.55      www       425: 	return ($value?&Apache::lonlocal::locallocaltime(
                    426: 			  &Apache::lonmysql::unsqltime($value)):
                    427: 		&mt('not available'));
1.46      www       428:     }
1.64      matthew   429:     # Language
1.46      www       430:     if ($type eq 'language') {
                    431: 	return &Apache::loncommon::languagedescription($value);
                    432:     }
1.64      matthew   433:     # Copyright
1.46      www       434:     if ($type eq 'copyright') {
                    435: 	return &Apache::loncommon::copyrightdescription($value);
                    436:     }
1.78      taceyjo1  437:     # Copyright
                    438:     if ($type eq 'sourceavail') {
                    439: 	return &Apache::loncommon::source_copyrightdescription($value);
                    440:     }
1.64      matthew   441:     # MIME
1.46      www       442:     if ($type eq 'mime') {
1.64      matthew   443:         return '<img src="'.&Apache::loncommon::icon($value).'" />&nbsp;'.
                    444:             &Apache::loncommon::filedescription($value);
                    445:     }
                    446:     # Person
1.46      www       447:     if (($type eq 'author') || 
                    448: 	($type eq 'owner') ||
                    449: 	($type eq 'modifyinguser') ||
                    450: 	($type eq 'authorspace')) {
                    451: 	$value=~s/(\w+)(\:|\@)(\w+)/&authordisplay($1,$3)/gse;
                    452: 	return $value;
                    453:     }
1.64      matthew   454:     # Gradelevel
1.48      www       455:     if (($type eq 'lowestgradelevel') ||
                    456: 	($type eq 'highestgradelevel')) {
                    457: 	return &Apache::loncommon::gradeleveldescription($value);
                    458:     }
1.64      matthew   459:     # Only for advance users below
1.96      albertel  460:     if (! $env{'user.adv'}) { 
1.65      matthew   461:         return '<i>- '.&mt('not displayed').' -</i>';
                    462:     }
1.64      matthew   463:     # File
1.46      www       464:     if (($type eq 'customdistributionfile') ||
                    465: 	($type eq 'obsoletereplacement') ||
                    466: 	($type eq 'goto_list') ||
                    467: 	($type eq 'comefrom_list') ||
1.82      www       468: 	($type eq 'sequsage_list') ||
1.83      www       469: 	($type eq 'dependencies')) {
1.151     www       470: 	return '<font size="-1"><ul>'.join("\n",map {
1.70      matthew   471:             my $url = &Apache::lonnet::clutter($_);
1.72      matthew   472:             my $title = &Apache::lonnet::gettitle($url);
                    473:             if ($title eq '') {
                    474:                 $title = 'Untitled';
                    475:                 if ($url =~ /\.sequence$/) {
                    476:                     $title .= ' Sequence';
                    477:                 } elsif ($url =~ /\.page$/) {
                    478:                     $title .= ' Page';
                    479:                 } elsif ($url =~ /\.problem$/) {
                    480:                     $title .= ' Problem';
                    481:                 } elsif ($url =~ /\.html$/) {
                    482:                     $title .= ' HTML document';
                    483:                 } elsif ($url =~ m:/syllabus$:) {
                    484:                     $title .= ' Syllabus';
                    485:                 } 
                    486:             }
1.82      www       487:             $_ = '<li>'.$title.' '.
                    488: 		&Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
                    489:                 '</li>'
                    490: 	    } split(/\s*\,\s*/,$value)).'</ul></font>';
1.46      www       491:     }
1.64      matthew   492:     # Evaluations
1.46      www       493:     if (($type eq 'clear') ||
                    494: 	($type eq 'depth') ||
                    495: 	($type eq 'helpful') ||
                    496: 	($type eq 'correct') ||
                    497: 	($type eq 'technical')) {
                    498: 	return &evalgraph($value);
                    499:     }
1.64      matthew   500:     # Difficulty
1.73      matthew   501:     if ($type eq 'difficulty' || $type eq 'disc') {
1.46      www       502: 	return &diffgraph($value);
                    503:     }
1.64      matthew   504:     # List of courses
1.46      www       505:     if ($type=~/\_list/) {
1.72      matthew   506:         my @Courses = split(/\s*\,\s*/,$value);
1.151     www       507:         my $Str='<font size="-1"><ul>';
1.72      matthew   508:         foreach my $course (@Courses) {
1.154     albertel  509:             my %courseinfo =
                    510: 		&Apache::lonnet::coursedescription($course,
                    511: 						   {'one_time' => 1});
1.72      matthew   512:             if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
                    513:                 next;
                    514:             }
1.151     www       515:             $Str .= '<li><a href="/public/'.$courseinfo{'domain'}.'/'.
1.72      matthew   516:                 $courseinfo{'num'}.'/syllabus" target="preview">'.
1.151     www       517:                 $courseinfo{'description'}.'</a></li>';
1.72      matthew   518:         }
1.151     www       519: 	return $Str.'</ul></font>';
1.46      www       520:     }
1.64      matthew   521:     # No pretty print found
1.46      www       522:     return $value;
                    523: }
                    524: 
1.64      matthew   525: # Pretty input of metadata field
1.54      www       526: sub direct {
                    527:     return shift;
                    528: }
                    529: 
1.48      www       530: sub selectbox {
                    531:     my ($name,$value,$functionref,@idlist)=@_;
1.65      matthew   532:     if (! defined($functionref)) {
                    533:         $functionref=\&direct;
                    534:     }
1.48      www       535:     my $selout='<select name="'.$name.'">';
                    536:     foreach (@idlist) {
                    537:         $selout.='<option value=\''.$_.'\'';
                    538:         if ($_ eq $value) {
                    539: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
                    540: 	}
                    541:         else {$selout.='>'.&{$functionref}($_).'</option>';}
                    542:     }
                    543:     return $selout.'</select>';
                    544: }
                    545: 
1.54      www       546: sub relatedfield {
                    547:     my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
1.65      matthew   548:     if (! $relatedsearchflag) { 
                    549:         return '';
                    550:     }
                    551:     if (! defined($relatedsep)) {
                    552:         $relatedsep=' ';
                    553:     }
                    554:     if (! $show) {
                    555:         return $relatedsep.'&nbsp;';
                    556:     }
1.54      www       557:     return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
                    558: 	($relatedvalue?' checked="1"':'').' />';
                    559: }
1.48      www       560: 
1.46      www       561: sub prettyinput {
1.54      www       562:     my ($type,$value,$fieldname,$formname,
1.116     banghart  563: 	$relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
1.75      matthew   564:     if (! defined($size)) {
                    565:         $size = 80;
                    566:     }
1.128     banghart  567:     my $output;
1.150     albertel  568:     if (defined($course_key) 
                    569: 	&& exists($env{$course_key.'.metadata.'.$type.'.options'})) {
1.116     banghart  570:         my $stu_add;
                    571:         my $only_one;
1.128     banghart  572:         my %meta_options;
                    573:         my @cur_values_inst;
                    574:         my $cur_values_stu;
1.132     banghart  575:         my $values = $env{$course_key.'.metadata.'.$type.'.values'};
                    576:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
1.116     banghart  577:             $stu_add = 'true';
                    578:         }
1.132     banghart  579:         if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
1.116     banghart  580:             $only_one = 'true';
                    581:         }
1.128     banghart  582:         # need to take instructor values out of list where instructor and student
                    583:         # values may be mixed.
1.133     banghart  584:         if ($values) {
1.132     banghart  585:             foreach my $item (split(/,/,$values)) {
                    586:                 $item =~ s/^\s+//;
1.133     banghart  587:                 $meta_options{$item} = $item;
1.128     banghart  588:             }
1.132     banghart  589:             foreach my $item (split(/,/,$value)) {
                    590:                 $item =~ s/^\s+//;
                    591:                 if ($meta_options{$item}) {
                    592:                     push(@cur_values_inst,$item);
1.128     banghart  593:                 } else {
1.132     banghart  594:                     $cur_values_stu .= $item.',';
1.128     banghart  595:                 }
                    596:             }
1.129     banghart  597:         } else {
                    598:             $cur_values_stu = $value;
1.128     banghart  599:         }
1.121     banghart  600:         if ($type eq 'courserestricted') {
1.138     banghart  601:             return (&select_course());
                    602:             # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
1.121     banghart  603:         }
1.174     banghart  604:         my $course = $env{'request.course.id'};
1.130     banghart  605:         if (($type eq 'keywords') || ($type eq 'subject')
                    606:              || ($type eq 'author')||($type eq  'notes')
1.174     banghart  607:              || ($type eq  'abstract')|| ($type eq  'title')|| ($type eq  'standards')
1.176   ! banghart  608:              || (exists($env{'course.'.$env{'request.course.id'}.'.metadata.'.$type.'.added'}))) {
1.129     banghart  609:             if ($values) {
                    610:                 if ($only_one) {
1.134     banghart  611:                     $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
1.129     banghart  612:                 } else {
1.130     banghart  613:                     $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
1.129     banghart  614:                 }
1.128     banghart  615:             }
                    616:             if ($stu_add) {
                    617:                 $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
                    618:                 'value="'.$cur_values_stu.'" />'.
                    619:                 &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
                    620:                       $relatedvalue); 
1.119     banghart  621:             }
1.128     banghart  622:             return ($output);
1.176   ! banghart  623:         } 
1.116     banghart  624:         if (($type eq 'lowestgradelevel') ||
                    625: 	    ($type eq 'highestgradelevel')) {
                    626: 	    return &Apache::loncommon::select_level_form($value,$fieldname).
                    627:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
                    628:         }
                    629:         return(); 
                    630:     }
1.64      matthew   631:     # Language
1.48      www       632:     if ($type eq 'language') {
                    633: 	return &selectbox($fieldname,
                    634: 			  $value,
                    635: 			  \&Apache::loncommon::languagedescription,
1.54      www       636: 			  (&Apache::loncommon::languageids)).
1.64      matthew   637:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
1.48      www       638:     }
1.64      matthew   639:     # Copyright
1.48      www       640:     if ($type eq 'copyright') {
                    641: 	return &selectbox($fieldname,
                    642: 			  $value,
                    643: 			  \&Apache::loncommon::copyrightdescription,
1.54      www       644: 			  (&Apache::loncommon::copyrightids)).
1.64      matthew   645:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
1.48      www       646:     }
1.78      taceyjo1  647:     # Source Copyright
                    648:     if ($type eq 'sourceavail') {
                    649: 	return &selectbox($fieldname,
                    650: 			  $value,
                    651: 			  \&Apache::loncommon::source_copyrightdescription,
                    652: 			  (&Apache::loncommon::source_copyrightids)).
                    653:                               &relatedfield(0,$relatedsearchflag,$relatedsep);
                    654:     }
1.64      matthew   655:     # Gradelevels
1.48      www       656:     if (($type eq 'lowestgradelevel') ||
                    657: 	($type eq 'highestgradelevel')) {
1.54      www       658: 	return &Apache::loncommon::select_level_form($value,$fieldname).
1.64      matthew   659:             &relatedfield(0,$relatedsearchflag,$relatedsep);
1.48      www       660:     }
1.64      matthew   661:     # Obsolete
1.48      www       662:     if ($type eq 'obsolete') {
                    663: 	return '<input type="checkbox" name="'.$fieldname.'"'.
1.54      www       664: 	    ($value?' checked="1"':'').' />'.
1.64      matthew   665:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
1.48      www       666:     }
1.64      matthew   667:     # Obsolete replacement file
1.48      www       668:     if ($type eq 'obsoletereplacement') {
                    669: 	return '<input type="text" name="'.$fieldname.
                    670: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
                    671: 	    "('".$formname."','".$fieldname."'".
1.54      www       672: 	    ",'')\">".&mt('Select').'</a>'.
1.64      matthew   673:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
                    674:     }
                    675:     # Customdistribution file
1.48      www       676:     if ($type eq 'customdistributionfile') {
                    677: 	return '<input type="text" name="'.$fieldname.
                    678: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
                    679: 	    "('".$formname."','".$fieldname."'".
1.54      www       680: 	    ",'rights')\">".&mt('Select').'</a>'.
1.64      matthew   681:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
1.48      www       682:     }
1.78      taceyjo1  683:     # Source Customdistribution file
                    684:     if ($type eq 'sourcerights') {
                    685: 	return '<input type="text" name="'.$fieldname.
                    686: 	    '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
                    687: 	    "('".$formname."','".$fieldname."'".
                    688: 	    ",'rights')\">".&mt('Select').'</a>'.
                    689:             &relatedfield(0,$relatedsearchflag,$relatedsep); 
                    690:     }
1.135     banghart  691:     if ($type eq 'courserestricted') {
1.138     banghart  692:         return (&select_course());
                    693:         #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
1.135     banghart  694:     }
                    695: 
1.64      matthew   696:     # Dates
1.48      www       697:     if (($type eq 'creationdate') ||
                    698: 	($type eq 'lastrevisiondate')) {
1.64      matthew   699: 	return 
                    700:             &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
                    701:             &relatedfield(0,$relatedsearchflag,$relatedsep);
1.48      www       702:     }
1.64      matthew   703:     # No pretty input found
1.48      www       704:     $value=~s/^\s+//gs;
                    705:     $value=~s/\s+$//gs;
                    706:     $value=~s/\s+/ /gs;
1.77      matthew   707:     $value=~s/\"/\&quot\;/gs;
1.54      www       708:     return 
1.74      matthew   709:         '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
1.64      matthew   710:         'value="'.$value.'" />'.
                    711:         &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
                    712:                       $relatedvalue); 
1.46      www       713: }
                    714: 
1.64      matthew   715: # Main Handler
1.1       www       716: sub handler {
1.64      matthew   717:     my $r=shift;
1.169     banghart  718:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    719:          ['currentpath']);
1.67      matthew   720:     my $uri=$r->uri;
                    721:     #
                    722:     # Set document type
                    723:     &Apache::loncommon::content_type($r,'text/html');
                    724:     $r->send_http_header;
                    725:     return OK if $r->header_only;
1.76      matthew   726:     my ($resdomain,$resuser)=
                    727:         (&Apache::lonnet::declutter($uri)=~/^(\w+)\/(\w+)\//);
1.66      matthew   728:     if ($uri=~m:/adm/bombs/(.*)$:) {
1.153     albertel  729:         $r->print(&Apache::loncommon::start_page('Error Messages'));
1.66      matthew   730:         # Looking for all bombs?
                    731:         &report_bombs($r,$uri);
1.162     albertel  732:     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
1.140     banghart  733: 	    ($resdomain,$resuser)=
1.162     albertel  734: 		(&Apache::lonnet::declutter($uri)=~m|^(\w+)/(\w+)/portfolio|);
1.153     albertel  735:         $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
                    736: 						 undef,
                    737: 						 {'domain' => $resdomain,}));
1.140     banghart  738:         if ($env{'form.store'}) {
                    739:             &present_editable_metadata($r,$uri,'portfolio');
                    740:         } else {
                    741:             &pre_select_course($r,$uri);
                    742:         }
1.171     banghart  743:     } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/groups/|) {
1.172     banghart  744:         $r->print(&Apache::loncommon::start_page('Edit Group Portfolio File Catalog Information',
1.171     banghart  745: 						 undef,
                    746: 						 {'domain' => $resdomain,}));
1.172     banghart  747:         &present_editable_metadata($r,$uri,'groups');    
1.162     albertel  748:     } elsif ($uri=~m|^/~|) { 
1.66      matthew   749:         # Construction space
1.153     albertel  750:         $r->print(&Apache::loncommon::start_page('Edit Catalog nformation',
                    751: 						 undef,
                    752: 						 {'domain' => $resdomain,}));
1.66      matthew   753:         &present_editable_metadata($r,$uri);
                    754:     } else {
1.153     albertel  755:         $r->print(&Apache::loncommon::start_page('Catalog Information',
                    756: 						 undef,
                    757: 						 {'domain' => $resdomain,}));
1.66      matthew   758:         &present_uneditable_metadata($r,$uri);
                    759:     }
1.153     albertel  760:     $r->print(&Apache::loncommon::end_page());
1.66      matthew   761:     return OK;
                    762: }
                    763: 
1.67      matthew   764: #####################################################
                    765: #####################################################
                    766: ###                                               ###
                    767: ###                Report Bombs                   ###
                    768: ###                                               ###
                    769: #####################################################
                    770: #####################################################
1.66      matthew   771: sub report_bombs {
                    772:     my ($r,$uri) = @_;
                    773:     # Set document type
1.67      matthew   774:     $uri =~ s:/adm/bombs/::;
                    775:     $uri = &Apache::lonnet::declutter($uri);
1.66      matthew   776:     $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
                    777:     my ($domain,$author)=($uri=~/^(\w+)\/(\w+)\//);
                    778:     if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
1.98      www       779: 	if ($env{'form.clearbombs'}) {
                    780: 	    &Apache::lonmsg::clear_author_res_msg($uri);
                    781: 	}
                    782:         my $clear=&mt('Clear all Messages in Subdirectory');
                    783: 	$r->print(<<ENDCLEAR);
                    784: <form method="post">
                    785: <input type="submit" name="clearbombs" value="$clear" />
                    786: </form>
                    787: ENDCLEAR
1.67      matthew   788:         my %brokenurls = 
                    789:             &Apache::lonmsg::all_url_author_res_msg($author,$domain);
                    790:         foreach (sort(keys(%brokenurls))) {
1.66      matthew   791:             if ($_=~/^\Q$uri\E/) {
1.70      matthew   792:                 $r->print
                    793:                     ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
                    794:                      &Apache::lonmsg::retrieve_author_res_msg($_).
                    795:                      '<hr />');
1.64      matthew   796:             }
                    797:         }
1.66      matthew   798:     } else {
                    799:         $r->print(&mt('Not authorized'));
                    800:     }
                    801:     return;
                    802: }
                    803: 
1.67      matthew   804: #####################################################
                    805: #####################################################
                    806: ###                                               ###
                    807: ###        Uneditable Metadata Display            ###
                    808: ###                                               ###
                    809: #####################################################
                    810: #####################################################
1.66      matthew   811: sub present_uneditable_metadata {
                    812:     my ($r,$uri) = @_;
                    813:     #
1.162     albertel  814:     my $uploaded = ($uri =~ m|/uploaded/|);
1.66      matthew   815:     my %content=();
                    816:     # Read file
                    817:     foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
                    818:         $content{$_}=&Apache::lonnet::metadata($uri,$_);
                    819:     }
                    820:     # Render Output
                    821:     # displayed url
                    822:     my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
                    823:     $uri=~s/\.meta$//;
                    824:     my $disuri=&Apache::lonnet::clutter($uri);
1.152     www       825:     $disuri=~s/^\/adm\/wrapper//;
1.66      matthew   826:     # version
                    827:     my $versiondisplay='';
1.162     albertel  828:     if (!$uploaded) {
                    829: 	my $currentversion=&Apache::lonnet::getversion($disuri);
                    830: 	if ($thisversion) {
                    831: 	    $versiondisplay=&mt('Version').': '.$thisversion.
                    832: 		' ('.&mt('most recent version').': '.
                    833: 		($currentversion>0 ? 
                    834: 		 $currentversion   :
                    835: 		 &mt('information not available')).')';
                    836: 	} else {
                    837: 	    $versiondisplay='Version: '.$currentversion;
                    838: 	}
1.66      matthew   839:     }
1.72      matthew   840:     # crumbify displayed URL               uri     target prefix form  size
                    841:     $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
                    842:     $disuri =~ s:<br />::g;
1.66      matthew   843:     # obsolete
                    844:     my $obsolete=$content{'obsolete'};
                    845:     my $obsoletewarning='';
1.96      albertel  846:     if (($obsolete) && ($env{'user.adv'})) {
1.66      matthew   847:         $obsoletewarning='<p><font color="red">'.
                    848:             &mt('This resource has been marked obsolete by the author(s)').
                    849:             '</font></p>';
                    850:     }
                    851:     #
                    852:     my %lt=&fieldnames();
                    853:     my $table='';
1.72      matthew   854:     my $title = $content{'title'};
                    855:     if (! defined($title)) {
                    856:         $title = 'Untitled Resource';
                    857:     }
1.163     albertel  858:     my @fields;
                    859:     if ($uploaded) {
                    860: 	@fields = ('title','author','subject','keywords','notes','abstract',
                    861: 		   'lowestgradelevel','highestgradelevel','standards','mime',
                    862: 		   'owner');
                    863:     } else {
                    864: 	@fields = ('title', 
                    865: 		   'author', 
                    866: 		   'subject', 
                    867: 		   'keywords', 
                    868: 		   'notes', 
                    869: 		   'abstract',
                    870: 		   'lowestgradelevel',
                    871: 		   'highestgradelevel',
                    872: 		   'standards', 
                    873: 		   'mime', 
                    874: 		   'language', 
                    875: 		   'creationdate', 
                    876: 		   'lastrevisiondate', 
                    877: 		   'owner', 
                    878: 		   'copyright', 
                    879: 		   'customdistributionfile',
                    880: 		   'sourceavail',
                    881: 		   'sourcerights', 
                    882: 		   'obsolete', 
                    883: 		   'obsoletereplacement');
                    884:     }
                    885:     foreach my $field (@fields) {
                    886:         $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$field}.
1.66      matthew   887:             '</td><td bgcolor="#CCCCCC">'.
1.163     albertel  888:             &prettyprint($field,$content{$field}).'</td></tr>';
                    889:         delete($content{$field});
1.66      matthew   890:     }
                    891:     #
                    892:     $r->print(<<ENDHEAD);
1.72      matthew   893: <h2>$title</h2>
                    894: <p>
                    895: $disuri<br />
1.36      www       896: $obsoletewarning
1.72      matthew   897: $versiondisplay
                    898: </p>
1.88      banghart  899: <table cellspacing="2" border="0">
1.45      www       900: $table
1.11      www       901: </table>
1.1       www       902: ENDHEAD
1.162     albertel  903:     if (!$uploaded && $env{'user.adv'}) {
1.68      matthew   904:         &print_dynamic_metadata($r,$uri,\%content);
1.67      matthew   905:     }
                    906:     return;
                    907: }
                    908: 
                    909: sub print_dynamic_metadata {
1.68      matthew   910:     my ($r,$uri,$content) = @_;
                    911:     #
1.69      matthew   912:     my %content = %$content;
1.68      matthew   913:     my %lt=&fieldnames();
1.67      matthew   914:     #
                    915:     my $description = 'Dynamic Metadata (updated periodically)';
                    916:     $r->print('<h3>'.&mt($description).'</h3>'.
1.70      matthew   917:               &mt('Processing'));
1.67      matthew   918:     $r->rflush();
                    919:     my %items=&fieldnames();
                    920:     my %dynmeta=&dynamicmeta($uri);
                    921:     #
                    922:     # General Access and Usage Statistics
1.70      matthew   923:     if (exists($dynmeta{'count'}) ||
                    924:         exists($dynmeta{'sequsage'}) ||
                    925:         exists($dynmeta{'comefrom'}) ||
                    926:         exists($dynmeta{'goto'}) ||
                    927:         exists($dynmeta{'course'})) {
                    928:         $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
1.88      banghart  929:                   '<table cellspacing="2" border="0">');
1.70      matthew   930:         foreach ('count',
                    931:                  'sequsage','sequsage_list',
                    932:                  'comefrom','comefrom_list',
                    933:                  'goto','goto_list',
                    934:                  'course','course_list') {
                    935:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
                    936:                       '<td bgcolor="#CCCCCC">'.
                    937:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
                    938:         }
                    939:         $r->print('</table>');
                    940:     } else {
                    941:         $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
1.67      matthew   942:     }
1.69      matthew   943:     #
                    944:     # Assessment statistics
1.73      matthew   945:     if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
                    946:         if (exists($dynmeta{'stdno'}) ||
                    947:             exists($dynmeta{'avetries'}) ||
                    948:             exists($dynmeta{'difficulty'}) ||
                    949:             exists($dynmeta{'disc'})) {
                    950:             # This is an assessment, print assessment data
                    951:             $r->print('<h4>'.
                    952:                       &mt('Overall Assessment Statistical Data').
                    953:                       '</h4>'.
1.88      banghart  954:                       '<table cellspacing="2" border="0">');
1.73      matthew   955:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
1.66      matthew   956:                       '<td bgcolor="#CCCCCC">'.
1.73      matthew   957:                       &prettyprint('stdno',$dynmeta{'stdno'}).
                    958:                       '</td>'."</tr>\n");
                    959:             foreach ('avetries','difficulty','disc') {
                    960:                 $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
                    961:                           '<td bgcolor="#CCCCCC">'.
                    962:                           &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
                    963:                           '</td>'."</tr>\n");
                    964:             }
                    965:             $r->print('</table>');    
                    966:         }
                    967:         if (exists($dynmeta{'stats'})) {
                    968:             #
                    969:             # New assessment statistics
                    970:             $r->print('<h4>'.
                    971:                       &mt('Detailed Assessment Statistical Data').
                    972:                       '</h4>');
1.88      banghart  973:             my $table = '<table cellspacing="2" border="0">'.
1.73      matthew   974:                 '<tr>'.
                    975:                 '<th>Course</th>'.
                    976:                 '<th>Section(s)</th>'.
                    977:                 '<th>Num Students</th>'.
                    978:                 '<th>Mean Tries</th>'.
                    979:                 '<th>Degree of Difficulty</th>'.
                    980:                 '<th>Degree of Discrimination</th>'.
                    981:                 '<th>Time of computation</th>'.
                    982:                 '</tr>'.$/;
                    983:             foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
                    984:                 my $data = $dynmeta{'stats'}->{$identifier};
                    985:                 my $course = $data->{'course'};
1.154     albertel  986:                 my %courseinfo = 
                    987: 		    &Apache::lonnet::coursedescription($course,
                    988: 						       {'one_time' => 1});
1.73      matthew   989:                 if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
                    990:                     &Apache::lonnet::logthis('lookup for '.$course.' failed');
                    991:                     next;
                    992:                 }
                    993:                 $table .= '<tr>';
                    994:                 $table .= 
                    995:                     '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
                    996:                 $table .= 
                    997:                     '<td align="right">'.$data->{'sections'}.'</td>';
                    998:                 $table .=
                    999:                     '<td align="right">'.$data->{'stdno'}.'</td>';
                   1000:                 foreach ('avetries','difficulty','disc') {
                   1001:                     $table .= '<td align="right">';
                   1002:                     if (exists($data->{$_})) {
                   1003:                         $table .= sprintf('%.2f',$data->{$_}).'&nbsp;';
                   1004:                     } else {
                   1005:                         $table .= '';
                   1006:                     }
                   1007:                     $table .= '</td>';
                   1008:                 }
                   1009:                 $table .=
                   1010:                     '<td><nobr>'.
                   1011:                     &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
                   1012:                     '</nobr></td>';
                   1013:                 $table .=
                   1014:                     '</tr>'.$/;
                   1015:             }
                   1016:             $table .= '</table>'.$/;
                   1017:             $r->print($table);
                   1018:         } else {
                   1019:             $r->print('No new dynamic data found.');
1.66      matthew  1020:         }
1.70      matthew  1021:     } else {
1.73      matthew  1022:         $r->print('<h4>'.
                   1023:           &mt('No Assessment Statistical Data is available for this resource').
                   1024:                   '</h4>');
1.67      matthew  1025:     }
1.73      matthew  1026: 
                   1027:     #
                   1028:     #
1.70      matthew  1029:     if (exists($dynmeta{'clear'})   || 
                   1030:         exists($dynmeta{'depth'})   || 
                   1031:         exists($dynmeta{'helpful'}) || 
                   1032:         exists($dynmeta{'correct'}) || 
                   1033:         exists($dynmeta{'technical'})){ 
                   1034:         $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
1.88      banghart 1035:                   '<table cellspacing="2" border="0">');
1.70      matthew  1036:         foreach ('clear','depth','helpful','correct','technical') {
                   1037:             $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
                   1038:                       '<td bgcolor="#CCCCCC">'.
                   1039:                       &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
                   1040:         }
                   1041:         $r->print('</table>');
                   1042:     } else {
                   1043:         $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
1.67      matthew  1044:     }
                   1045:     $uri=~/^\/res\/(\w+)\/(\w+)\//; 
1.96      albertel 1046:     if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
                   1047:         || ($env{'user.role.ca./'.$1.'/'.$2})) {
1.70      matthew  1048:         if (exists($dynmeta{'comments'})) {
                   1049:             $r->print('<h4>'.&mt('Evaluation Comments').' ('.
                   1050:                       &mt('visible to author and co-authors only').
                   1051:                       ')</h4>'.
                   1052:                       '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
                   1053:         } else {
                   1054:             $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
                   1055:         }
                   1056:         my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
                   1057:         if (defined($bombs) && $bombs ne '') {
                   1058:             $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
                   1059:                       &mt('visible to author and co-authors only').')'.
                   1060:                       '</h4>'.$bombs);
                   1061:         } else {
                   1062:             $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
                   1063:         }
1.67      matthew  1064:     }
1.69      matthew  1065:     #
1.67      matthew  1066:     # All other stuff
                   1067:     $r->print('<h3>'.
                   1068:               &mt('Additional Metadata (non-standard, parameters, exports)').
1.81      www      1069:               '</h3><table border="0" cellspacing="1">');
1.67      matthew  1070:     foreach (sort(keys(%content))) {
                   1071:         my $name=$_;
                   1072:         if ($name!~/\.display$/) {
                   1073:             my $display=&Apache::lonnet::metadata($uri,
                   1074:                                                   $name.'.display');
                   1075:             if (! $display) { 
                   1076:                 $display=$name;
                   1077:             };
                   1078:             my $otherinfo='';
                   1079:             foreach ('name','part','type','default') {
                   1080:                 if (defined(&Apache::lonnet::metadata($uri,
                   1081:                                                       $name.'.'.$_))) {
                   1082:                     $otherinfo.=' '.$_.'='.
                   1083:                         &Apache::lonnet::metadata($uri,
                   1084:                                                   $name.'.'.$_).'; ';
                   1085:                 }
1.64      matthew  1086:             }
1.81      www      1087:             $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
1.67      matthew  1088:             if ($otherinfo) {
                   1089:                 $r->print(' ('.$otherinfo.')');
1.64      matthew  1090:             }
1.81      www      1091:             $r->print("</font></td></tr>\n");
1.64      matthew  1092:         }
1.66      matthew  1093:     }
1.81      www      1094:     $r->print("</table>");
1.67      matthew  1095:     return;
1.66      matthew  1096: }
1.105     banghart 1097: 
1.102     banghart 1098: 
                   1099: 
1.67      matthew  1100: #####################################################
                   1101: #####################################################
                   1102: ###                                               ###
                   1103: ###          Editable metadata display            ###
                   1104: ###                                               ###
                   1105: #####################################################
                   1106: #####################################################
1.66      matthew  1107: sub present_editable_metadata {
1.172     banghart 1108:     my ($r,$uri,$file_type) = @_;
1.66      matthew  1109:     # Construction Space Call
                   1110:     # Header
                   1111:     my $disuri=$uri;
                   1112:     my $fn=&Apache::lonnet::filelocation('',$uri);
1.155     albertel 1113:     $disuri=~s{^/\~}{/priv/};
1.66      matthew  1114:     $disuri=~s/\.meta$//;
1.141     albertel 1115:     my $meta_uri = $disuri;
1.147     albertel 1116:     my $path;
1.141     albertel 1117:     if ($disuri =~ m|/portfolio/|) {
1.147     albertel 1118: 	($disuri, $meta_uri, $path) =  &portfolio_display_uri($disuri,1);
1.141     albertel 1119:     }
1.66      matthew  1120:     my $target=$uri;
1.155     albertel 1121:     $target=~s{^/\~}{/res/$env{'request.role.domain'}/};
1.66      matthew  1122:     $target=~s/\.meta$//;
                   1123:     my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
                   1124:     if ($bombs) {
1.99      www      1125:         my $showdel=1;
1.96      albertel 1126:         if ($env{'form.delmsg'}) {
1.66      matthew  1127:             if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
                   1128:                 $bombs=&mt('Messages deleted.');
1.99      www      1129: 		$showdel=0;
1.66      matthew  1130:             } else {
                   1131:                 $bombs=&mt('Error deleting messages');
1.64      matthew  1132:             }
1.66      matthew  1133:         }
1.98      www      1134:         if ($env{'form.clearmsg'}) {
                   1135: 	    my $cleardir=$target;
                   1136: 	    $cleardir=~s/\/[^\/]+$/\//;
                   1137:             if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
                   1138:                 $bombs=&mt('Messages cleared.');
1.99      www      1139: 		$showdel=0;
1.98      www      1140:             } else {
                   1141:                 $bombs=&mt('Error clearing messages');
                   1142:             }
                   1143:         }
                   1144:         my $del=&mt('Delete Messages for this Resource');
                   1145: 	my $clear=&mt('Clear all Messages in Subdirectory');
1.99      www      1146: 	my $goback=&mt('Back to Source File');
1.66      matthew  1147:         $r->print(<<ENDBOMBS);
1.52      www      1148: <h1>$disuri</h1>
1.169     banghart 1149: <form method="post" action="" name="defaultmeta">
1.99      www      1150: ENDBOMBS
                   1151:         if ($showdel) {
                   1152: 	    $r->print(<<ENDDEL);
1.59      www      1153: <input type="submit" name="delmsg" value="$del" />
1.98      www      1154: <input type="submit" name="clearmsg" value="$clear" />
1.99      www      1155: ENDDEL
                   1156:         } else {
                   1157:             $r->print('<a href="'.$disuri.'" />'.$goback.'</a>');
                   1158: 	}
                   1159: 	$r->print('<br />'.$bombs);
1.66      matthew  1160:     } else {
                   1161:         my $displayfile='Catalog Information for '.$disuri;
                   1162:         if ($disuri=~/\/default$/) {
                   1163:             my $dir=$disuri;
                   1164:             $dir=~s/default$//;
                   1165:             $displayfile=
                   1166:                 &mt('Default Cataloging Information for Directory').' '.
                   1167:                 $dir;
                   1168:         }
                   1169:         %Apache::lonpublisher::metadatafields=();
                   1170:         %Apache::lonpublisher::metadatakeys=();
1.94      banghart 1171:         my $result=&Apache::lonnet::getfile($fn);
                   1172:         if ($result == -1){
1.141     albertel 1173: 	    $r->print(&mt('Creating new file [_1]'),$meta_uri);
1.94      banghart 1174:         } else {
                   1175:             &Apache::lonpublisher::metaeval($result);
                   1176:         }
1.66      matthew  1177:         $r->print(<<ENDEDIT);
1.23      www      1178: <h1>$displayfile</h1>
1.169     banghart 1179: <form method="post" action="" name="defaultmeta">
1.23      www      1180: ENDEDIT
1.169     banghart 1181:         $r->print('<script type="JavaScript">'.
1.86      albertel 1182:                   &Apache::loncommon::browser_and_searcher_javascript().
1.66      matthew  1183:                   '</script>');
1.90      banghart 1184:         my %lt=&fieldnames($file_type);
1.87      albertel 1185: 	my $output;
1.90      banghart 1186: 	my @fields;
1.174     banghart 1187: 	my $added_metadata_fields;
1.90      banghart 1188: 	if ($file_type eq 'portfolio') {
1.174     banghart 1189: 	    if(exists ($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'})) {
                   1190: 	        # retrieve fieldnames (in order) from the course restricted list
                   1191: 	        @fields = (split /,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'});
                   1192: 	    } else {
                   1193: 	        # no saved field list, use default list
                   1194: 	        @fields =  ('author','title','subject','keywords','abstract',
                   1195: 			    'notes','lowestgradelevel',
                   1196: 	                    'highestgradelevel','standards');
                   1197: 	        $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames();
1.176   ! banghart 1198: 	        
1.174     banghart 1199: 	        $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join (",",@fields);
                   1200: 	    }
1.90      banghart 1201: 	} else {
                   1202: 	    @fields = ('author','title','subject','keywords','abstract','notes',
1.66      matthew  1203:                  'copyright','customdistributionfile','language',
                   1204:                  'standards',
1.78      taceyjo1 1205:                  'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
1.90      banghart 1206:                  'obsolete','obsoletereplacement');
                   1207:         }
1.172     banghart 1208:         if ($file_type eq 'groups') {
                   1209:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
                   1210:                 'course.'.$env{'request.course.id'}; 
                   1211:         }
1.139     banghart 1212:         if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
1.172     banghart 1213:                 (! $env{'form.new_courserestricted'}) && (! $file_type eq 'groups')) {
1.138     banghart 1214:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
                   1215:                 'none';
1.139     banghart 1216:         } elsif ($env{'form.new_courserestricted'}) {
                   1217:             $Apache::lonpublisher::metadatafields{'courserestricted'}=
                   1218:                 $env{'form.new_courserestricted'}; 
                   1219:         }           
1.120     banghart 1220:         if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
                   1221:                 $Apache::lonpublisher::metadatafields{'copyright'}=
1.163     albertel 1222: 		    'default';
1.120     banghart 1223:         }
1.172     banghart 1224: 	if (($file_type eq 'portfolio') || ($file_type eq 'groups'))  {
1.163     albertel 1225: 	    if (! $Apache::lonpublisher::metadatafields{'mime'}) {
                   1226:                 ($Apache::lonpublisher::metadatafields{'mime'}) =
                   1227: 		    ( $target=~/\.(\w+)$/ );
                   1228: 	    }
                   1229: 	    if (! $Apache::lonpublisher::metadatafields{'owner'}) {
                   1230: 		$Apache::lonpublisher::metadatafields{'owner'} =
                   1231: 		    $env{'user.name'}.':'.$env{'user.domain'};
                   1232: 	    }
                   1233: 
1.149     albertel 1234: 	    if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
                   1235: 		$r->print(&mt('Associated with course [_1]','<strong>'.$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.".description"}.
                   1236: 			      '</strong>').'<br />');
                   1237: 	    } else {
                   1238: 		$r->print("This resource is not associated with a course.<br />");
                   1239: 	    }
                   1240: 	}
1.176   ! banghart 1241:         foreach my $field_name(keys (%$added_metadata_fields)) {
        !          1242:             push (@fields,$field_name);
        !          1243:             $Apache::lonpublisher::metadatafields{$field_name} =
        !          1244:                      $$added_metadata_fields{$field_name};
        !          1245:             $lt{$field_name} = $$added_metadata_fields{$field_name};
        !          1246:         }
1.143     albertel 1247:         foreach my $field_name (@fields) {
1.132     banghart 1248:             if (defined($env{'form.new_'.$field_name})) {
                   1249:                 $Apache::lonpublisher::metadatafields{$field_name}=
                   1250:                     join(',',&Apache::loncommon::get_env_multiple('form.new_'.$field_name));
1.66      matthew  1251:             }
1.150     albertel 1252:             if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
                   1253: 		&& exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
1.115     banghart 1254:                 # handle restrictions here
1.138     banghart 1255:                 if (($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
                   1256:                     ($field_name eq 'courserestricted')){
1.143     albertel 1257:                     $output.=("\n".'<p>'.$lt{$field_name}.': '.
1.132     banghart 1258:                               &prettyinput($field_name,
                   1259: 				   $Apache::lonpublisher::metadatafields{$field_name},
1.138     banghart 1260: 				                    'new_'.$field_name,'defaultmeta',
                   1261: 				                    undef,undef,undef,undef,
1.143     albertel 1262: 				                    $Apache::lonpublisher::metadatafields{'courserestricted'}).'</p>'."\n");
1.127     banghart 1263:                  }
1.115     banghart 1264:             } else {
1.138     banghart 1265: 
1.132     banghart 1266:                     $output.=('<p>'.$lt{$field_name}.': '.
                   1267:                             &prettyinput($field_name,
                   1268: 				   $Apache::lonpublisher::metadatafields{$field_name},
                   1269: 				   'new_'.$field_name,'defaultmeta').'</p>');
1.138     banghart 1270:                
1.115     banghart 1271:             }
1.66      matthew  1272:         }
1.143     albertel 1273: 	if ($env{'form.store'}) {
                   1274: 	    my $mfh;
                   1275: 	    my $formname='store'; 
                   1276: 	    my $file_content;
1.142     albertel 1277: 	    if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
                   1278: 		$Apache::lonpublisher::metadatafields{'keywords'} = 
                   1279: 		    join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
                   1280: 	    }
1.174     banghart 1281: 	    foreach my $field (sort keys %Apache::lonpublisher::metadatafields) {
                   1282: 		next if ($field =~ /\./);
                   1283: 		my $unikey=$field;
1.143     albertel 1284: 		$unikey=~/^([A-Za-z]+)/;
                   1285: 		my $tag=$1;
                   1286: 		$tag=~tr/A-Z/a-z/;
                   1287: 		$file_content.= "\n\<$tag";
1.174     banghart 1288: 		foreach my $key (split(/\,/,
1.143     albertel 1289: 			       $Apache::lonpublisher::metadatakeys{$unikey})
                   1290: 			 ) {
                   1291: 		    my $value=
1.174     banghart 1292: 			$Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
1.143     albertel 1293: 		    $value=~s/\"/\'\'/g;
1.174     banghart 1294: 		    $file_content.=' '.$key.'="'.$value.'"' ;
1.143     albertel 1295: 		}
                   1296: 		$file_content.= '>'.
                   1297: 		    &HTML::Entities::encode
                   1298: 		    ($Apache::lonpublisher::metadatafields{$unikey},
                   1299: 		     '<>&"').
                   1300: 		     '</'.$tag.'>';
1.142     albertel 1301: 	    }
1.173     banghart 1302: 	    if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
                   1303: 	        my ($path, $new_fn);
                   1304: 	        if ($fn =~ m|\w+/groups/\w+/portfolio/|) {
                   1305:                     ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
                   1306: 	        } else {
                   1307: 		    ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
                   1308: 	        }
1.159     raeburn  1309:                 $r->print(&store_portfolio_metadata($formname,$file_content,$path,
1.169     banghart 1310:                                                     $new_fn));
1.173     banghart 1311:             } else {
1.143     albertel 1312: 		if (!  ($mfh=Apache::File->new('>'.$fn))) {
                   1313: 		    $r->print('<p><font color="red">'.
                   1314: 			      &mt('Could not write metadata').', '.
                   1315: 			      &mt('FAIL').'</font></p>');
                   1316: 		} else {
                   1317: 		    print $mfh $file_content;
                   1318: 		    $r->print('<p><font color="blue">'.&mt('Wrote Metadata').
                   1319: 			      ' '.&Apache::lonlocal::locallocaltime(time).
                   1320: 			      '</font></p>');
                   1321: 		}
1.142     albertel 1322: 	    }
                   1323: 	}
1.143     albertel 1324: 	$r->print($output.'<br /><input type="submit" name="store" value="'.
1.169     banghart 1325:                   &mt('Store Catalog Information').'" />');
1.147     albertel 1326: 
1.149     albertel 1327: 	if ($file_type eq 'portfolio') {
1.156     albertel 1328: 	    my ($port_path,$group) = &get_port_path_and_group($uri);
1.159     raeburn  1329:             if ($group) {
                   1330:                 $r->print('<input type="hidden" name="group" value="'.$group.'" />');
                   1331:             }
1.169     banghart 1332:             $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
1.175     banghart 1333: 	    $r->print('</form><br /><br /><form method="post" action="'.$port_path.'">');
                   1334: 	    if ($group) {
                   1335: 	        $r->print('<input type="hidden" name="group" value="'.$group.'" />');
                   1336: 	    }
                   1337: 	    $r->print('<input type="hidden" name="currentpath" value="'.$path.'" />'.
1.169     banghart 1338: 		      '<input type="submit" name="cancel" value="'.&mt('Discard Edits and Return to Portfolio').'" />');
1.149     albertel 1339: 	}
1.142     albertel 1340:     }
1.149     albertel 1341:     
1.143     albertel 1342:     $r->print('</form>');
                   1343: 
1.66      matthew  1344:     return;
1.1       www      1345: }
1.64      matthew  1346: 
1.159     raeburn  1347: sub store_portfolio_metadata {
                   1348:     my ($formname,$content,$path,$new_fn) = @_;
                   1349:     $env{'form.'.$formname}=$content."\n";
                   1350:     $env{'form.'.$formname.'.filename'}=$new_fn;
                   1351:     my $result =&Apache::lonnet::userfileupload($formname,'',$path);
                   1352:     if ($result =~ /(error|notfound)/) {
                   1353:         return '<p><font color="red">'.
                   1354:                   &mt('Could not write metadata').', '.
                   1355:                   &mt('FAIL').'</font></p>';
                   1356:     } else {
                   1357:         return '<p><font color="blue">'.&mt('Wrote Metadata').
                   1358:                   ' '.&Apache::lonlocal::locallocaltime(time).'</font></p>';
                   1359:     }
                   1360: }
                   1361: 
1.1       www      1362: 1;
                   1363: __END__
1.97      banghart 1364: 

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