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