1: # The LearningOnline Network with CAPA
2: # Metadata display handler
3: #
4: # $Id: lonmeta.pm,v 1.215 2008/11/18 19:14:22 jms 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: =head1 NAME
30:
31: Apache::lonmeta - display meta data
32:
33: =head1 SYNOPSIS
34:
35: Handler to display meta data
36:
37: This is part of the LearningOnline Network with CAPA project
38: described at http://www.lon-capa.org.
39:
40: =head1 SUBROUTINES
41:
42: =over
43:
44: =item &get_dynamic_metadata_from_sql($url) :
45:
46: Queries sql database for dynamic metdata
47: Returns a hash of hashes, with keys of urls which match $url
48: Returned fields are given below.
49:
50: Examples:
51:
52: %DynamicMetadata = &Apache::lonmeta::get_dynmaic_metadata_from_sql
53: ('/res/msu/korte/');
54:
55: $DynamicMetadata{'/res/msu/korte/example.problem'}->{$field}
56:
57: =item dynamicmeta()
58:
59: Fetch and evaluate dynamic metadata
60:
61: =item access_count()
62:
63: =item alttag()
64:
65: Try to make an alt tag if there is none
66:
67: =item authordisplay()
68:
69: Author display
70:
71: =item evalgraph()
72:
73: Pretty display
74:
75: =item diffgraph()
76:
77: =item fieldnames()
78:
79: =item portfolio_linked_path()
80:
81: =item get_port_path_and_group()
82:
83: =item portfolio_display_uri()
84:
85: =item pre_select_course()
86:
87: =item select_course()
88:
89: =item prettyprint()
90:
91: Pretty printing of metadata field
92:
93: =item direct()
94:
95: Pretty input of metadata field
96:
97: =item selectbox()
98:
99: =item relatedfield()
100:
101: =item prettyinput()
102:
103: =item report_bombs()
104:
105: =item present_uneditable_metadata()
106:
107: =item present_editable_metadata()
108:
109: =item store_metadata()
110:
111: =item store_transferred_addedfields()
112:
113: =item store_portfolio_metadata()
114:
115: =item update_metadata_table()
116:
117: =back
118:
119: =cut
120:
121:
122: package Apache::lonmeta;
123:
124: use strict;
125: use LONCAPA::lonmetadata();
126: use Apache::Constants qw(:common);
127: use Apache::lonnet;
128: use Apache::loncommon();
129: use Apache::lonhtmlcommon();
130: use Apache::lonmsg;
131: use Apache::lonpublisher;
132: use Apache::lonlocal;
133: use Apache::lonmysql;
134: use Apache::lonmsg;
135: use LONCAPA qw(:DEFAULT :match);
136:
137:
138: sub get_dynamic_metadata_from_sql {
139: my ($url) = shift();
140: my ($authordom,$author)=($url=~m{^/res/($match_domain)/($match_username)/});
141: if (! defined($authordom)) {
142: $authordom = shift();
143: }
144: if (! defined($author)) {
145: $author = shift();
146: }
147: if (! defined($authordom) || ! defined($author)) {
148: return ();
149: }
150: my $query = 'SELECT * FROM metadata WHERE url LIKE "'.$url.'%"';
151: my $server = &Apache::lonnet::homeserver($author,$authordom);
152: my $reply = &Apache::lonnet::metadata_query($query,undef,undef,
153: ,[$server]);
154: return () if (! defined($reply) || ref($reply) ne 'HASH');
155: my $filename = $reply->{$server};
156: if (! defined($filename) || $filename =~ /^error/) {
157: return ();
158: }
159: my $max_time = time + 10; # wait 10 seconds for results at most
160: my %ReturnHash;
161: #
162: # Look for results
163: my $finished = 0;
164: while (! $finished && time < $max_time) {
165: my $datafile=$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename;
166: if (! -e "$datafile.end") { next; }
167: my $fh;
168: if (!($fh=Apache::File->new($datafile))) { next; }
169: while (my $result = <$fh>) {
170: chomp($result);
171: next if (! $result);
172: my %hash=&LONCAPA::lonmetadata::metadata_col_to_hash('metadata',
173: map { &unescape($_) } split(/\,/,$result));
174: foreach my $key (keys(%hash)) {
175: $ReturnHash{$hash{'url'}}->{$key}=$hash{$key};
176: }
177: }
178: $finished = 1;
179: }
180: #
181: return %ReturnHash;
182: }
183:
184:
185: # Fetch and evaluate dynamic metadata
186: sub dynamicmeta {
187: my $url=&Apache::lonnet::declutter(shift);
188: $url=~s/\.meta$//;
189: my ($adomain,$aauthor)=($url=~/^($match_domain)\/($match_username)\//);
190: my $regexp=$url;
191: $regexp=~s/(\W)/\\$1/g;
192: $regexp='___'.$regexp.'___';
193: my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
194: $aauthor,$regexp);
195: my %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
196: my %Data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
197: \%DynamicData);
198: #
199: # Deal with 'count' separately
200: $Data{'count'} = &access_count($url,$aauthor,$adomain);
201: #
202: # Debugging code I will probably need later
203: if (0) {
204: &Apache::lonnet::logthis('Dynamic Metadata');
205: while(my($k,$v)=each(%Data)){
206: &Apache::lonnet::logthis(' "'.$k.'"=>"'.$v.'"');
207: }
208: &Apache::lonnet::logthis('-------------------');
209: }
210: return %Data;
211: }
212:
213: sub access_count {
214: my ($src,$author,$adomain) = @_;
215: my %countdata=&Apache::lonnet::dump('nohist_accesscount',$adomain,
216: $author,$src);
217: if (! exists($countdata{$src})) {
218: return &mt('Not Available');
219: } else {
220: return $countdata{$src};
221: }
222: }
223:
224: # Try to make an alt tag if there is none
225: sub alttag {
226: my ($base,$src)=@_;
227: my $fullpath=&Apache::lonnet::hreflocation($base,$src);
228: my $alttag=&Apache::lonnet::metadata($fullpath,'title').' '.
229: &Apache::lonnet::metadata($fullpath,'subject').' '.
230: &Apache::lonnet::metadata($fullpath,'abstract');
231: $alttag=~s/\s+/ /gs;
232: $alttag=~s/\"//gs;
233: $alttag=~s/\'//gs;
234: $alttag=~s/\s+$//gs;
235: $alttag=~s/^\s+//gs;
236: if ($alttag) {
237: return $alttag;
238: } else {
239: return &mt('No information available');
240: }
241: }
242:
243: # Author display
244: sub authordisplay {
245: my ($aname,$adom)=@_;
246: return &Apache::loncommon::aboutmewrapper
247: (&Apache::loncommon::plainname($aname,$adom),
248: $aname,$adom,'preview').' <tt>['.$aname.':'.$adom.']</tt>';
249: }
250:
251: # Pretty display
252: sub evalgraph {
253: my $value=shift;
254: if (! $value) {
255: return '';
256: }
257: my $val=int($value*10.+0.5)-10;
258: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
259: if ($val>=20) {
260: $output.='<td width="20" bgcolor="#555555">  </td>';
261: } else {
262: $output.='<td width="'.($val).'" bgcolor="#555555"> </td>'.
263: '<td width="'.(20-$val).'" bgcolor="#FF3333"> </td>';
264: }
265: $output.='<td bgcolor="#FFFF33"> </td>';
266: if ($val>20) {
267: $output.='<td width="'.($val-20).'" bgcolor="#33FF33"> </td>'.
268: '<td width="'.(40-$val).'" bgcolor="#555555"> </td>';
269: } else {
270: $output.='<td width="20" bgcolor="#555555">  </td>';
271: }
272: $output.='<td> ('.sprintf("%5.2f",$value).') </td></tr></table>';
273: return $output;
274: }
275:
276: sub diffgraph {
277: my $value=shift;
278: if (! $value) {
279: return '';
280: }
281: my $val=int(40.0*$value+0.5);
282: my @colors=('#FF9933','#EEAA33','#DDBB33','#CCCC33',
283: '#BBDD33','#CCCC33','#DDBB33','#EEAA33');
284: my $output='<table border="0" cellpadding="0" cellspacing="0"><tr>';
285: for (my $i=0;$i<8;$i++) {
286: if ($val>$i*5) {
287: $output.='<td width="5" bgcolor="'.$colors[$i].'"> </td>';
288: } else {
289: $output.='<td width="5" bgcolor="#555555"> </td>';
290: }
291: }
292: $output.='<td> ('.sprintf("%3.2f",$value).') </td></tr></table>';
293: return $output;
294: }
295:
296:
297: # The field names
298: sub fieldnames {
299: my $file_type=shift;
300: my %fields =
301: ('title' => 'Title',
302: 'author' =>'Author(s)',
303: 'authorspace' => 'Author Space',
304: 'modifyinguser' => 'Last Modifying User',
305: 'subject' => 'Subject',
306: 'standards' => 'Standards',
307: 'keywords' => 'Keyword(s)',
308: 'notes' => 'Notes',
309: 'abstract' => 'Abstract',
310: 'lowestgradelevel' => 'Lowest Grade Level',
311: 'highestgradelevel' => 'Highest Grade Level');
312:
313: if ( !defined($file_type) || ($file_type ne 'portfolio' && $file_type ne 'groups') ) {
314: %fields =
315: (%fields,
316: 'domain' => 'Domain',
317: 'mime' => 'MIME Type',
318: 'language' => 'Language',
319: 'creationdate' => 'Creation Date',
320: 'lastrevisiondate' => 'Last Revision Date',
321: 'owner' => 'Publisher/Owner',
322: 'copyright' => 'Copyright/Distribution',
323: 'customdistributionfile' => 'Custom Distribution File',
324: 'sourceavail' => 'Source Available',
325: 'sourcerights' => 'Source Custom Distribution File',
326: 'obsolete' => 'Obsolete',
327: 'obsoletereplacement' => 'Suggested Replacement for Obsolete File',
328: 'count' => 'Network-wide number of accesses (hits)',
329: 'course' => 'Network-wide number of courses using resource',
330: 'course_list' => 'Network-wide courses using resource',
331: 'sequsage' => 'Number of resources using or importing resource',
332: 'sequsage_list' => 'Resources using or importing resource',
333: 'goto' => 'Number of resources that follow this resource in maps',
334: 'goto_list' => 'Resources that follow this resource in maps',
335: 'comefrom' => 'Number of resources that lead up to this resource in maps',
336: 'comefrom_list' => 'Resources that lead up to this resource in maps',
337: 'clear' => 'Material presented in clear way',
338: 'depth' => 'Material covered with sufficient depth',
339: 'helpful' => 'Material is helpful',
340: 'correct' => 'Material appears to be correct',
341: 'technical' => 'Resource is technically correct',
342: 'avetries' => 'Average number of tries till solved',
343: 'stdno' => 'Statistics calculated for number of students',
344: 'difficulty' => 'Degree of difficulty',
345: 'disc' => 'Degree of discrimination',
346: 'dependencies' => 'Resources used by this resource',
347: );
348: }
349: return &Apache::lonlocal::texthash(%fields);
350: }
351:
352: sub portfolio_linked_path {
353: my ($path,$group,$port_path) = @_;
354:
355: my $start = 'portfolio';
356: if ($group) {
357: $start = "groups/$group/".$start;
358: }
359: my %anchor_fields = (
360: 'selectfile' => $start,
361: 'currentpath' => '/'
362: );
363: my $result = &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$start);
364: my $fullpath = '/';
365: my (undef,@tree) = split('/',$path);
366: my $filename = pop(@tree);
367: foreach my $dir (@tree) {
368: $fullpath .= $dir.'/';
369: $result .= '/';
370: my %anchor_fields = (
371: 'selectfile' => $dir,
372: 'currentpath' => $fullpath
373: );
374: $result .= &Apache::portfolio::make_anchor($port_path,\%anchor_fields,$dir);
375: }
376: $result .= "/$filename";
377: return $result;
378: }
379:
380: sub get_port_path_and_group {
381: my ($uri)=@_;
382:
383: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
384: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
385:
386: my ($port_path,$group);
387: if ($uri =~ m{^/editupload/\Q$cdom\E/\Q$cnum\E/groups/}) {
388: $group = (split('/',$uri))[5];
389: $port_path = '/adm/coursegrp_portfolio';
390: } else {
391: $port_path = '/adm/portfolio';
392: }
393: if ($env{'form.group'} ne $group) {
394: $env{'form.group'} = $group;
395: }
396: return ($port_path,$group);
397: }
398:
399: sub portfolio_display_uri {
400: my ($uri,$as_links)=@_;
401:
402: my ($port_path,$group) = &get_port_path_and_group($uri);
403:
404: $uri =~ s|.*/(portfolio/.*)$|$1|;
405: my ($res_uri,$meta_uri) = ($uri,$uri);
406: if ($uri =~ /\.meta$/) {
407: $res_uri =~ s/\.meta//;
408: } else {
409: $meta_uri .= '.meta';
410: }
411:
412: my ($path) = ($res_uri =~ m|^portfolio(.*/)[^/]*$|);
413: if ($as_links) {
414: $res_uri = &portfolio_linked_path($res_uri,$group,$port_path);
415: $meta_uri = &portfolio_linked_path($meta_uri,$group,$port_path);
416: }
417: return ($res_uri,$meta_uri,$path);
418: }
419:
420: sub pre_select_course {
421: my ($r,$uri) = @_;
422: my $output;
423: my $fn=&Apache::lonnet::filelocation('',$uri);
424: my ($res_uri,$meta_uri,$path) = &portfolio_display_uri($uri);
425: %Apache::lonpublisher::metadatafields=();
426: %Apache::lonpublisher::metadatakeys=();
427: my $result=&Apache::lonnet::getfile($fn);
428: if ($result == -1){
429: $r->print(&mt('Creating new file [_1]'),$meta_uri);
430: } else {
431: &Apache::lonpublisher::metaeval($result);
432: }
433: $r->print('<hr /><form method="post" action="" >');
434: $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>');
435: $output = &select_course();
436: $r->print($output.'<br /><input type="submit" name="store" value="'.
437: &mt('Associate Resource With Selected Course').'" />');
438: $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
439: $r->print('<input type="hidden" name="associate" value="true" />');
440: $r->print('</form>');
441:
442: my ($port_path,$group) = &get_port_path_and_group($uri);
443: my $group_input;
444: if ($group) {
445: $group_input = '<input type="hidden" name="group" value="'.$group.'" />';
446: }
447: $r->print('<br /><br /><form method="post" action="'.$port_path.'">'.
448: '<input type="hidden" name="currentpath" value="'.$path.'" />'.
449: $group_input.
450: '<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'.
451: '</form>');
452:
453: return;
454: }
455: sub select_course {
456: my $output=$/;
457: my $current_restriction=
458: $Apache::lonpublisher::metadatafields{'courserestricted'};
459: my $selected = ($current_restriction eq 'none' ? 'selected="selected"'
460: : '');
461: if ($current_restriction =~ /^course\.($match_domain\_$match_courseid)$/) {
462: my $assoc_crs = $1;
463: my $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
464: if (ref($added_metadata_fields) eq 'HASH') {
465: if (keys(%{$added_metadata_fields}) > 0) {
466: my $transfernotes;
467: foreach my $field_name (keys(%{$added_metadata_fields})) {
468: my $value = $Apache::lonpublisher::metadatafields{$field_name};
469: if ($value) {
470: $transfernotes .=
471: &Apache::loncommon::start_data_table_row().
472: '<td><input type="checkbox" name="transfer_'.
473: $field_name.'" value="1" /></td><td>'.
474: $field_name.'</td><td>'.$value.'</td>'.
475: &Apache::loncommon::end_data_table_row();
476: }
477: }
478: if ($transfernotes ne '') {
479: my %courseinfo = &Apache::lonnet::coursedescription($assoc_crs,{'one_time' => 1});
480: my $assoc_crs_description = $courseinfo{'description'};
481: $output .= &mt('This resource is currently associated with a course ([_1]) which includes added metadata fields specific to the course.',$assoc_crs_description).'<br />'."\n".
482: &mt('You can choose to transfer data from the added fields to the "Notes" field if you are planning to change the course association.').'<br /><br />'.
483: &Apache::loncommon::start_data_table().
484: &Apache::loncommon::start_data_table_header_row().
485: '<th>Copy to notes?</th>'."\n".
486: '<th>Field name</th>'."\n".
487: '<th>Values</th>'."\n".
488: &Apache::loncommon::end_data_table_header_row().
489: $transfernotes.
490: &Apache::loncommon::end_data_table().'<br />';
491: }
492: }
493: }
494: }
495: $output .= '<select name="new_courserestricted" >';
496: $output .= '<option value="none" '.$selected.'>'.
497: &mt('None').'</option>'.$/;
498: my %courses;
499: foreach my $key (keys(%env)) {
500: if ($key !~ m/^course\.(.+)\.description$/) { next; }
501: my $cid = $1;
502: if ($env{$key} !~ /\S/) { next; }
503: $courses{$key} = $cid;
504: }
505: foreach my $key (sort { lc($env{$a}) cmp lc($env{$b}) } (keys(%courses))) {
506: my $cid = 'course.'.$courses{$key};
507: my $selected = ($current_restriction eq $cid ? 'selected="selected"'
508: : '');
509: if ($env{$key} !~ /\S/) { next; }
510: $output .= '<option value="'.$cid.'" '.$selected.'>';
511: $output .= $env{$key};
512: $output .= '</option>'.$/;
513: $selected = '';
514: }
515: $output .= '</select><br />';
516: return ($output);
517: }
518: # Pretty printing of metadata field
519:
520: sub prettyprint {
521: my ($type,$value,$target,$prefix,$form,$noformat)=@_;
522: # $target,$prefix,$form are optional and for filecrumbs only
523: if (! defined($value)) {
524: return ' ';
525: }
526: # Title
527: if ($type eq 'title') {
528: return '<font size="+1" face="arial">'.$value.'</font>';
529: }
530: # Dates
531: if (($type eq 'creationdate') ||
532: ($type eq 'lastrevisiondate')) {
533: return ($value?&Apache::lonlocal::locallocaltime(
534: &Apache::lonmysql::unsqltime($value)):
535: &mt('not available'));
536: }
537: # Language
538: if ($type eq 'language') {
539: return &Apache::loncommon::languagedescription($value);
540: }
541: # Copyright
542: if ($type eq 'copyright') {
543: return &Apache::loncommon::copyrightdescription($value);
544: }
545: # Copyright
546: if ($type eq 'sourceavail') {
547: return &Apache::loncommon::source_copyrightdescription($value);
548: }
549: # MIME
550: if ($type eq 'mime') {
551: return '<img src="'.&Apache::loncommon::icon($value).'" /> '.
552: &Apache::loncommon::filedescription($value);
553: }
554: # Person
555: if (($type eq 'author') ||
556: ($type eq 'owner') ||
557: ($type eq 'modifyinguser') ||
558: ($type eq 'authorspace')) {
559: $value=~s/($match_username)(\:|\@)($match_domain)/&authordisplay($1,$3)/gse;
560: return $value;
561: }
562: # Gradelevel
563: if (($type eq 'lowestgradelevel') ||
564: ($type eq 'highestgradelevel')) {
565: return &Apache::loncommon::gradeleveldescription($value);
566: }
567: # Only for advance users below
568: if (! $env{'user.adv'}) {
569: return '<i>- '.&mt('not displayed').' -</i>';
570: }
571: # File
572: if (($type eq 'customdistributionfile') ||
573: ($type eq 'obsoletereplacement') ||
574: ($type eq 'goto_list') ||
575: ($type eq 'comefrom_list') ||
576: ($type eq 'sequsage_list') ||
577: ($type eq 'dependencies')) {
578: return '<font size="-1"><ul>'.join("\n",map {
579: my $url = &Apache::lonnet::clutter_with_no_wrapper($_);
580: my $title = &Apache::lonnet::gettitle($url);
581: if ($title eq '') {
582: $title = 'Untitled';
583: if ($url =~ /\.sequence$/) {
584: $title .= ' Sequence';
585: } elsif ($url =~ /\.page$/) {
586: $title .= ' Page';
587: } elsif ($url =~ /\.problem$/) {
588: $title .= ' Problem';
589: } elsif ($url =~ /\.html$/) {
590: $title .= ' HTML document';
591: } elsif ($url =~ m:/syllabus$:) {
592: $title .= ' Syllabus';
593: }
594: }
595: $_ = '<li>'.$title.' '.
596: &Apache::lonhtmlcommon::crumbs($url,$target,$prefix,$form,'-1',$noformat).
597: '</li>'
598: } split(/\s*\,\s*/,$value)).'</ul></font>';
599: }
600: # Evaluations
601: if (($type eq 'clear') ||
602: ($type eq 'depth') ||
603: ($type eq 'helpful') ||
604: ($type eq 'correct') ||
605: ($type eq 'technical')) {
606: return &evalgraph($value);
607: }
608: # Difficulty
609: if ($type eq 'difficulty' || $type eq 'disc') {
610: return &diffgraph($value);
611: }
612: # List of courses
613: if ($type=~/\_list/) {
614: my @Courses = split(/\s*\,\s*/,$value);
615: my $Str='<font size="-1"><ul>';
616: my %descriptions;
617: foreach my $course (@Courses) {
618: my %courseinfo =
619: &Apache::lonnet::coursedescription($course,
620: {'one_time' => 1});
621: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
622: next;
623: }
624: $descriptions{join('\0',@courseinfo{'domain','description'})} .=
625: '<li><a href="/public/'.$courseinfo{'domain'}.'/'.
626: $courseinfo{'num'}.'/syllabus" target="preview">'.
627: $courseinfo{'description'}.' ('.$courseinfo{'domain'}.
628: ')</a></li>';
629: }
630: foreach my $course (sort {lc($a) cmp lc($b)} (keys(%descriptions))) {
631: $Str .= $descriptions{$course};
632: }
633:
634: return $Str.'</ul></font>';
635: }
636: # No pretty print found
637: return $value;
638: }
639:
640: # Pretty input of metadata field
641: sub direct {
642: return shift;
643: }
644:
645: sub selectbox {
646: my ($name,$value,$functionref,@idlist)=@_;
647: if (! defined($functionref)) {
648: $functionref=\&direct;
649: }
650: my $selout='<select name="'.$name.'">';
651: foreach (@idlist) {
652: $selout.='<option value=\''.$_.'\'';
653: if ($_ eq $value) {
654: $selout.=' selected>'.&{$functionref}($_).'</option>';
655: }
656: else {$selout.='>'.&{$functionref}($_).'</option>';}
657: }
658: return $selout.'</select>';
659: }
660:
661: sub relatedfield {
662: my ($show,$relatedsearchflag,$relatedsep,$fieldname,$relatedvalue)=@_;
663: if (! $relatedsearchflag) {
664: return '';
665: }
666: if (! defined($relatedsep)) {
667: $relatedsep=' ';
668: }
669: if (! $show) {
670: return $relatedsep.' ';
671: }
672: return $relatedsep.'<input type="checkbox" name="'.$fieldname.'_related"'.
673: ($relatedvalue?' checked="1"':'').' />';
674: }
675:
676: sub prettyinput {
677: my ($type,$value,$fieldname,$formname,
678: $relatedsearchflag,$relatedsep,$relatedvalue,$size,$course_key)=@_;
679: if (! defined($size)) {
680: $size = 80;
681: }
682: my $output;
683: if (defined($course_key)
684: && exists($env{$course_key.'.metadata.'.$type.'.options'})) {
685: my $stu_add;
686: my $only_one;
687: my %meta_options;
688: my @cur_values_inst;
689: my $cur_values_stu;
690: my $values = $env{$course_key.'.metadata.'.$type.'.values'};
691: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/stuadd/) {
692: $stu_add = 'true';
693: }
694: if ($env{$course_key.'.metadata.'.$type.'.options'} =~ m/onlyone/) {
695: $only_one = 'true';
696: }
697: # need to take instructor values out of list where instructor and student
698: # values may be mixed.
699: if ($values) {
700: foreach my $item (split(/,/,$values)) {
701: $item =~ s/^\s+//;
702: $meta_options{$item} = $item;
703: }
704: foreach my $item (split(/,/,$value)) {
705: $item =~ s/^\s+//;
706: if ($meta_options{$item}) {
707: push(@cur_values_inst,$item);
708: } else {
709: if ($item ne '') {
710: $cur_values_stu .= $item.',';
711: }
712: }
713: }
714: $cur_values_stu =~ s/,$//;
715: my @key_order = sort(keys(%meta_options));
716: unshift(@key_order,'');
717: $meta_options{''} = 'Not specified';
718: $meta_options{'select_form_order'} = \@key_order;
719: } else {
720: $cur_values_stu = $value;
721: }
722: if ($type eq 'courserestricted') {
723: return (&select_course());
724: # return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
725: }
726: if (($type eq 'keywords') || ($type eq 'subject')
727: || ($type eq 'author')||($type eq 'notes')
728: || ($type eq 'abstract')|| ($type eq 'title')|| ($type eq 'standards')
729: || (exists($env{$course_key.'.metadata.'.$type.'.added'}))) {
730:
731: if ($values) {
732: if ($only_one) {
733: $output .= (&Apache::loncommon::select_form($cur_values_inst[0],'new_'.$type,%meta_options));
734: } else {
735: $output .= (&Apache::loncommon::multiple_select_form('new_'.$type,\@cur_values_inst,undef,\%meta_options));
736: }
737: }
738: if ($stu_add) {
739: $output .= '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
740: 'value="'.$cur_values_stu.'" />'.
741: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
742: $relatedvalue);
743: }
744: return ($output);
745: }
746: if (($type eq 'lowestgradelevel') ||
747: ($type eq 'highestgradelevel')) {
748: return &Apache::loncommon::select_level_form($value,$fieldname).
749: &relatedfield(0,$relatedsearchflag,$relatedsep);
750: }
751: return();
752: }
753: # Language
754: if ($type eq 'language') {
755: return &selectbox($fieldname,
756: $value,
757: \&Apache::loncommon::languagedescription,
758: (&Apache::loncommon::languageids)).
759: &relatedfield(0,$relatedsearchflag,$relatedsep);
760: }
761: # Copyright
762: if ($type eq 'copyright') {
763: return &selectbox($fieldname,
764: $value,
765: \&Apache::loncommon::copyrightdescription,
766: (&Apache::loncommon::copyrightids)).
767: &relatedfield(0,$relatedsearchflag,$relatedsep);
768: }
769: # Source Copyright
770: if ($type eq 'sourceavail') {
771: return &selectbox($fieldname,
772: $value,
773: \&Apache::loncommon::source_copyrightdescription,
774: (&Apache::loncommon::source_copyrightids)).
775: &relatedfield(0,$relatedsearchflag,$relatedsep);
776: }
777: # Gradelevels
778: if (($type eq 'lowestgradelevel') ||
779: ($type eq 'highestgradelevel')) {
780: return &Apache::loncommon::select_level_form($value,$fieldname).
781: &relatedfield(0,$relatedsearchflag,$relatedsep);
782: }
783: # Obsolete
784: if ($type eq 'obsolete') {
785: return '<input type="checkbox" name="'.$fieldname.'"'.
786: ($value?' checked="1"':'').' />'.
787: &relatedfield(0,$relatedsearchflag,$relatedsep);
788: }
789: # Obsolete replacement file
790: if ($type eq 'obsoletereplacement') {
791: return '<input type="text" name="'.$fieldname.
792: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
793: "('".$formname."','".$fieldname."'".
794: ",'')\">".&mt('Select').'</a>'.
795: &relatedfield(0,$relatedsearchflag,$relatedsep);
796: }
797: # Customdistribution file
798: if ($type eq 'customdistributionfile') {
799: return '<input type="text" name="'.$fieldname.
800: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
801: "('".$formname."','".$fieldname."'".
802: ",'rights')\">".&mt('Select').'</a>'.
803: &relatedfield(0,$relatedsearchflag,$relatedsep);
804: }
805: # Source Customdistribution file
806: if ($type eq 'sourcerights') {
807: return '<input type="text" name="'.$fieldname.
808: '" size="60" value="'.$value.'" /><a href="javascript:openbrowser'.
809: "('".$formname."','".$fieldname."'".
810: ",'rights')\">".&mt('Select').'</a>'.
811: &relatedfield(0,$relatedsearchflag,$relatedsep);
812: }
813: if ($type eq 'courserestricted') {
814: return (&select_course());
815: #return ('<input type="hidden" name="new_courserestricted" value="'.$course_key.'" />');
816: }
817:
818: # Dates
819: if (($type eq 'creationdate') ||
820: ($type eq 'lastrevisiondate')) {
821: return
822: &Apache::lonhtmlcommon::date_setter($formname,$fieldname,$value).
823: &relatedfield(0,$relatedsearchflag,$relatedsep);
824: }
825: # No pretty input found
826: $value=~s/^\s+//gs;
827: $value=~s/\s+$//gs;
828: $value=~s/\s+/ /gs;
829: $value=~s/\"/\"\;/gs;
830: return
831: '<input type="text" name="'.$fieldname.'" size="'.$size.'" '.
832: 'value="'.$value.'" />'.
833: &relatedfield(1,$relatedsearchflag,$relatedsep,$fieldname,
834: $relatedvalue);
835: }
836:
837: # Main Handler
838: sub handler {
839: my $r=shift;
840: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
841: ['currentpath','changecourse']);
842: my $uri=$r->uri;
843: #
844: # Set document type
845: &Apache::loncommon::content_type($r,'text/html');
846: $r->send_http_header;
847: return OK if $r->header_only;
848: my ($resdomain,$resuser)=
849: (&Apache::lonnet::declutter($uri)=~/^($match_domain)\/($match_username)\//);
850: if ($uri=~m:/adm/bombs/(.*)$:) {
851: $r->print(&Apache::loncommon::start_page('Error Messages'));
852: # Looking for all bombs?
853: &report_bombs($r,$uri);
854: } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/portfolio/|) {
855: ($resdomain,$resuser)=
856: (&Apache::lonnet::declutter($uri)=~m|^($match_domain)/($match_name)/portfolio|);
857: $r->print(&Apache::loncommon::start_page('Edit Portfolio File Catalog Information',
858: undef,
859: {'domain' => $resdomain,}));
860: if ($env{'form.store'}) {
861: &present_editable_metadata($r,$uri,'portfolio');
862: } else {
863: my $fn=&Apache::lonnet::filelocation('',$uri);
864: %Apache::lonpublisher::metadatafields=();
865: %Apache::lonpublisher::metadatakeys=();
866: my $result=&Apache::lonnet::getfile($fn);
867: &Apache::lonpublisher::metaeval($result);
868: if ((!$Apache::lonpublisher::metadatafields{'courserestricted'}) ||
869: ($env{'form.changecourse'} eq 'true')) {
870: &pre_select_course($r,$uri);
871: } else {
872: &present_editable_metadata($r,$uri,'portfolio');
873: }
874: }
875: } elsif ($uri=~m|^/editupload/[^/]+/[^/]+/groups/|) {
876: $r->print(&Apache::loncommon::start_page('Edit Group Portfolio File Catalog Information',
877: undef,
878: {'domain' => $resdomain,}));
879: &present_editable_metadata($r,$uri,'groups');
880: } elsif ($uri=~m|^/~|) {
881: # Construction space
882: $r->print(&Apache::loncommon::start_page('Edit Catalog Information',
883: "\n".'<script type="text/javascript">'."\n".
884: &Apache::loncommon::browser_and_searcher_javascript().
885: "\n".'</script>',
886: {'domain' => $resdomain,}));
887: &present_editable_metadata($r,$uri);
888: } else {
889: $r->print(&Apache::loncommon::start_page('Metadata',
890: undef,
891: {'domain' => $resdomain,}));
892: &present_uneditable_metadata($r,$uri);
893: }
894: $r->print(&Apache::loncommon::end_page());
895: return OK;
896: }
897:
898: #####################################################
899: #####################################################
900: ### ###
901: ### Report Bombs ###
902: ### ###
903: #####################################################
904: #####################################################
905: sub report_bombs {
906: my ($r,$uri) = @_;
907: # Set document type
908: $uri =~ s:/adm/bombs/::;
909: $uri = &Apache::lonnet::declutter($uri);
910: $r->print('<h1>'.&Apache::lonnet::clutter($uri).'</h1>');
911: my ($domain,$author)=($uri=~/^($match_domain)\/($match_username)\//);
912: if (&Apache::loncacc::constructaccess('/~'.$author.'/',$domain)) {
913: if ($env{'form.clearbombs'}) {
914: &Apache::lonmsg::clear_author_res_msg($uri);
915: }
916: my $clear=&mt('Clear all Messages in Subdirectory');
917: my $cancel=&mt('Back to Directory');
918: my $cancelurl=$uri;
919: $cancelurl=~s/^\Q$domain\E/\/priv/;
920: $r->print(<<ENDCLEAR);
921: <form method="post">
922: <input type="submit" name="clearbombs" value="$clear" />
923: <a href="$cancelurl">$cancel</a>
924: </form><hr />
925: ENDCLEAR
926: my %brokenurls =
927: &Apache::lonmsg::all_url_author_res_msg($author,$domain);
928: foreach (sort(keys(%brokenurls))) {
929: if ($_=~/^\Q$uri\E/) {
930: $r->print
931: ('<a href="'.&Apache::lonnet::clutter($_).'">'.$_.'</a>'.
932: &Apache::lonmsg::retrieve_author_res_msg($_).
933: '<hr />');
934: }
935: }
936: } else {
937: $r->print(&mt('Not authorized'));
938: }
939: return;
940: }
941:
942: #####################################################
943: #####################################################
944: ### ###
945: ### Uneditable Metadata Display ###
946: ### ###
947: #####################################################
948: #####################################################
949: sub present_uneditable_metadata {
950: my ($r,$uri) = @_;
951: #
952: my $uploaded = ($uri =~ m|/uploaded/|);
953: my %content=();
954: # Read file
955: foreach (split(/\,/,&Apache::lonnet::metadata($uri,'keys'))) {
956: $content{$_}=&Apache::lonnet::metadata($uri,$_);
957: }
958: # Render Output
959: # displayed url
960: my ($thisversion)=($uri=~/\.(\d+)\.(\w+)\.meta$/);
961: $uri=~s/\.meta$//;
962: my $disuri=&Apache::lonnet::clutter_with_no_wrapper($uri);
963: # version
964: my $versiondisplay='';
965: if (!$uploaded) {
966: my $currentversion=&Apache::lonnet::getversion($disuri);
967: if ($thisversion) {
968: $versiondisplay=&mt('Version').': '.$thisversion.
969: ' ('.&mt('most recent version').': '.
970: ($currentversion>0 ?
971: $currentversion :
972: &mt('information not available')).')';
973: } else {
974: $versiondisplay='Version: '.$currentversion;
975: }
976: }
977: # crumbify displayed URL uri target prefix form size
978: $disuri=&Apache::lonhtmlcommon::crumbs($disuri,undef, undef, undef,'+1');
979: $disuri =~ s:<br />::g;
980: # obsolete
981: my $obsolete=$content{'obsolete'};
982: my $obsoletewarning='';
983: if (($obsolete) && ($env{'user.adv'})) {
984: $obsoletewarning='<p><font color="red">'.
985: &mt('This resource has been marked obsolete by the author(s)').
986: '</font></p>';
987: }
988: #
989: my %lt=&fieldnames();
990: my $table='';
991: my $title = $content{'title'};
992: if (! defined($title)) {
993: $title = 'Untitled Resource';
994: }
995: my @fields;
996: if ($uploaded) {
997: @fields = ('title','author','subject','keywords','notes','abstract',
998: 'lowestgradelevel','highestgradelevel','standards','mime',
999: 'owner');
1000: } else {
1001: @fields = ('title',
1002: 'author',
1003: 'subject',
1004: 'keywords',
1005: 'notes',
1006: 'abstract',
1007: 'lowestgradelevel',
1008: 'highestgradelevel',
1009: 'standards',
1010: 'mime',
1011: 'language',
1012: 'creationdate',
1013: 'lastrevisiondate',
1014: 'owner',
1015: 'copyright',
1016: 'customdistributionfile',
1017: 'sourceavail',
1018: 'sourcerights',
1019: 'obsolete',
1020: 'obsoletereplacement');
1021: }
1022: foreach my $field (@fields) {
1023: $table.='<tr><td bgcolor="#AAAAAA">'.$lt{$field}.
1024: '</td><td bgcolor="#CCCCCC">'.
1025: &prettyprint($field,$content{$field}).'</td></tr>';
1026: delete($content{$field});
1027: }
1028: #
1029: $r->print(<<ENDHEAD);
1030: <h2>$title</h2>
1031: <p>
1032: $disuri<br />
1033: $obsoletewarning
1034: $versiondisplay
1035: </p>
1036: <table cellspacing="2" border="0">
1037: $table
1038: </table>
1039: ENDHEAD
1040: if (!$uploaded && $env{'user.adv'}) {
1041: &print_dynamic_metadata($r,$uri,\%content);
1042: }
1043: return;
1044: }
1045:
1046: sub print_dynamic_metadata {
1047: my ($r,$uri,$content) = @_;
1048: #
1049: my %content = %$content;
1050: my %lt=&fieldnames();
1051: #
1052: my $description = 'Dynamic Metadata (updated periodically)';
1053: $r->print('<h3>'.&mt($description).'</h3>'.
1054: &mt('Processing'));
1055: $r->rflush();
1056: my %items=&fieldnames();
1057: my %dynmeta=&dynamicmeta($uri);
1058: #
1059: # General Access and Usage Statistics
1060: if (exists($dynmeta{'count'}) ||
1061: exists($dynmeta{'sequsage'}) ||
1062: exists($dynmeta{'comefrom'}) ||
1063: exists($dynmeta{'goto'}) ||
1064: exists($dynmeta{'course'})) {
1065: $r->print('<h4>'.&mt('Access and Usage Statistics').'</h4>'.
1066: '<table cellspacing="2" border="0">');
1067: foreach ('count',
1068: 'sequsage','sequsage_list',
1069: 'comefrom','comefrom_list',
1070: 'goto','goto_list',
1071: 'course','course_list') {
1072: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
1073: '<td bgcolor="#CCCCCC">'.
1074: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
1075: }
1076: $r->print('</table>');
1077: } else {
1078: $r->print('<h4>'.&mt('No Access or Usages Statistics are available for this resource.').'</h4>');
1079: }
1080: #
1081: # Assessment statistics
1082: if ($uri=~/\.(problem|exam|quiz|assess|survey|form)$/) {
1083: if (exists($dynmeta{'stdno'}) ||
1084: exists($dynmeta{'avetries'}) ||
1085: exists($dynmeta{'difficulty'}) ||
1086: exists($dynmeta{'disc'})) {
1087: # This is an assessment, print assessment data
1088: $r->print('<h4>'.
1089: &mt('Overall Assessment Statistical Data').
1090: '</h4>'.
1091: '<table cellspacing="2" border="0">');
1092: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{'stdno'}.'</td>'.
1093: '<td bgcolor="#CCCCCC">'.
1094: &prettyprint('stdno',$dynmeta{'stdno'}).
1095: '</td>'."</tr>\n");
1096: foreach ('avetries','difficulty','disc') {
1097: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
1098: '<td bgcolor="#CCCCCC">'.
1099: &prettyprint($_,sprintf('%5.2f',$dynmeta{$_})).
1100: '</td>'."</tr>\n");
1101: }
1102: $r->print('</table>');
1103: }
1104: if (exists($dynmeta{'stats'})) {
1105: #
1106: # New assessment statistics
1107: $r->print('<h4>'.
1108: &mt('Recent Detailed Assessment Statistical Data').
1109: '</h4>');
1110: my $table = '<table cellspacing="2" border="0">'.
1111: '<tr>'.
1112: '<th>'.&mt('Course').'</th>'.
1113: '<th>'.&mt('Section(s)').'</th>'.
1114: '<th>'.&mt('Num Student').'s</th>'.
1115: '<th>'.&mt('Mean Tries').'</th>'.
1116: '<th>'.&mt('Degree of Difficulty').'</th>'.
1117: '<th>'.&mt('Degree of Discrimination').'</th>'.
1118: '<th>'.&mt('Time of computation').'</th>'.
1119: '</tr>'.$/;
1120: foreach my $identifier (sort(keys(%{$dynmeta{'stats'}}))) {
1121: my $data = $dynmeta{'stats'}->{$identifier};
1122: my $course = $data->{'course'};
1123: my %courseinfo =
1124: &Apache::lonnet::coursedescription($course,
1125: {'one_time' => 1});
1126: if (! exists($courseinfo{'num'}) || $courseinfo{'num'} eq '') {
1127: &Apache::lonnet::logthis('lookup for '.$course.' failed');
1128: next;
1129: }
1130: $table .= '<tr>';
1131: $table .=
1132: '<td><nobr>'.$courseinfo{'description'}.'</nobr></td>';
1133: $table .=
1134: '<td align="right">'.$data->{'sections'}.'</td>';
1135: $table .=
1136: '<td align="right">'.$data->{'stdno'}.'</td>';
1137: foreach ('avetries','difficulty','disc') {
1138: $table .= '<td align="right">';
1139: if (exists($data->{$_})) {
1140: $table .= sprintf('%.2f',$data->{$_}).' ';
1141: } else {
1142: $table .= '';
1143: }
1144: $table .= '</td>';
1145: }
1146: $table .=
1147: '<td><nobr>'.
1148: &Apache::lonlocal::locallocaltime($data->{'timestamp'}).
1149: '</nobr></td>';
1150: $table .=
1151: '</tr>'.$/;
1152: }
1153: $table .= '</table>'.$/;
1154: $r->print($table);
1155: } else {
1156: $r->print(&mt('No new dynamic data found.'));
1157: }
1158: } else {
1159: $r->print('<h4>'.
1160: &mt('No Assessment Statistical Data is available for this resource').
1161: '</h4>');
1162: }
1163:
1164: #
1165: #
1166: if (exists($dynmeta{'clear'}) ||
1167: exists($dynmeta{'depth'}) ||
1168: exists($dynmeta{'helpful'}) ||
1169: exists($dynmeta{'correct'}) ||
1170: exists($dynmeta{'technical'})){
1171: $r->print('<h4>'.&mt('Evaluation Data').'</h4>'.
1172: '<table cellspacing="2" border="0">');
1173: foreach ('clear','depth','helpful','correct','technical') {
1174: $r->print('<tr><td bgcolor="#AAAAAA">'.$lt{$_}.'</td>'.
1175: '<td bgcolor="#CCCCCC">'.
1176: &prettyprint($_,$dynmeta{$_})."</td></tr>\n");
1177: }
1178: $r->print('</table>');
1179: } else {
1180: $r->print('<h4>'.&mt('No Evaluation Data is available for this resource.').'</h4>');
1181: }
1182: $uri=~/^\/res\/($match_domain)\/($match_username)\//;
1183: if ((($env{'user.domain'} eq $1) && ($env{'user.name'} eq $2))
1184: || ($env{'user.role.ca./'.$1.'/'.$2})) {
1185: if (exists($dynmeta{'comments'})) {
1186: $r->print('<h4>'.&mt('Evaluation Comments').' ('.
1187: &mt('visible to author and co-authors only').
1188: ')</h4>'.
1189: '<blockquote>'.$dynmeta{'comments'}.'</blockquote>');
1190: } else {
1191: $r->print('<h4>'.&mt('There are no Evaluation Comments on this resource.').'</h4>');
1192: }
1193: my $bombs = &Apache::lonmsg::retrieve_author_res_msg($uri);
1194: if (defined($bombs) && $bombs ne '') {
1195: $r->print('<a name="bombs" /><h4>'.&mt('Error Messages').' ('.
1196: &mt('visible to author and co-authors only').')'.
1197: '</h4>'.$bombs);
1198: } else {
1199: $r->print('<h4>'.&mt('There are currently no Error Messages for this resource.').'</h4>');
1200: }
1201: }
1202: #
1203: # All other stuff
1204: $r->print('<h3>'.
1205: &mt('Additional Metadata (non-standard, parameters, exports)').
1206: '</h3><table border="0" cellspacing="1">');
1207: foreach (sort(keys(%content))) {
1208: my $name=$_;
1209: if ($name!~/\.display$/) {
1210: my $display=&Apache::lonnet::metadata($uri,
1211: $name.'.display');
1212: if (! $display) {
1213: $display=$name;
1214: };
1215: my $otherinfo='';
1216: foreach ('name','part','type','default') {
1217: if (defined(&Apache::lonnet::metadata($uri,
1218: $name.'.'.$_))) {
1219: $otherinfo.=' '.$_.'='.
1220: &Apache::lonnet::metadata($uri,
1221: $name.'.'.$_).'; ';
1222: }
1223: }
1224: $r->print('<tr><td bgcolor="#bbccbb"><font size="-1" color="#556655">'.$display.'</font></td><td bgcolor="#ccddcc"><font size="-1" color="#556655">'.$content{$name});
1225: if ($otherinfo) {
1226: $r->print(' ('.$otherinfo.')');
1227: }
1228: $r->print("</font></td></tr>\n");
1229: }
1230: }
1231: $r->print("</table>");
1232: return;
1233: }
1234:
1235:
1236:
1237: #####################################################
1238: #####################################################
1239: ### ###
1240: ### Editable metadata display ###
1241: ### ###
1242: #####################################################
1243: #####################################################
1244: sub present_editable_metadata {
1245: my ($r,$uri,$file_type) = @_;
1246: # Construction Space Call
1247: # Header
1248: my $disuri=$uri;
1249: my $fn=&Apache::lonnet::filelocation('',$uri);
1250: $disuri=~s{^/\~}{/priv/};
1251: $disuri=~s/\.meta$//;
1252: my $meta_uri = $disuri;
1253: my $path;
1254: if ($disuri =~ m|/portfolio/|) {
1255: ($disuri, $meta_uri, $path) = &portfolio_display_uri($disuri,1);
1256: }
1257: my $target=$uri;
1258: $target=~s{^/\~}{/res/$env{'request.role.domain'}/};
1259: $target=~s/\.meta$//;
1260: my $bombs=&Apache::lonmsg::retrieve_author_res_msg($target);
1261: if ($bombs) {
1262: my $showdel=1;
1263: if ($env{'form.delmsg'}) {
1264: if (&Apache::lonmsg::del_url_author_res_msg($target) eq 'ok') {
1265: $bombs=&mt('Messages deleted.');
1266: $showdel=0;
1267: } else {
1268: $bombs=&mt('Error deleting messages');
1269: }
1270: }
1271: if ($env{'form.clearmsg'}) {
1272: my $cleardir=$target;
1273: $cleardir=~s/\/[^\/]+$/\//;
1274: if (&Apache::lonmsg::clear_author_res_msg($cleardir) eq 'ok') {
1275: $bombs=&mt('Messages cleared.');
1276: $showdel=0;
1277: } else {
1278: $bombs=&mt('Error clearing messages');
1279: }
1280: }
1281: my $del=&mt('Delete Messages for this Resource');
1282: my $clear=&mt('Clear all Messages in Subdirectory');
1283: my $goback=&mt('Back to Source File');
1284: $r->print(<<ENDBOMBS);
1285: <h1>$disuri</h1>
1286: <form method="post" action="" name="defaultmeta">
1287: ENDBOMBS
1288: if ($showdel) {
1289: $r->print(<<ENDDEL);
1290: <input type="submit" name="delmsg" value="$del" />
1291: <input type="submit" name="clearmsg" value="$clear" />
1292: ENDDEL
1293: } else {
1294: $r->print('<p><a href="'.$disuri.'">'.$goback.'</a></p>');
1295: if ($env{'form.clearmsg'}) {
1296: my ($diruri) = ($disuri =~ m{(.*/)[^/]*});
1297: $r->print('<p><a href="'.$diruri.'">'.
1298: &mt('Back To Directory').'</a></p>');
1299: }
1300: }
1301: $r->print('<br />'.$bombs);
1302: } else {
1303: my $displayfile=&mt('Metadata for [_1]',$disuri);
1304: if ($disuri=~/\/default$/) {
1305: my $dir=$disuri;
1306: $dir=~s/default$//;
1307: $displayfile=&mt('Default Metadata for Directory [_1]',$dir);
1308: }
1309: %Apache::lonpublisher::metadatafields=();
1310: %Apache::lonpublisher::metadatakeys=();
1311: my $result=&Apache::lonnet::getfile($fn);
1312: if ($result == -1){
1313: $r->print(&mt('Creating new file [_1]'),$meta_uri);
1314: } else {
1315: &Apache::lonpublisher::metaeval($result);
1316: }
1317: if ($env{'form.new_courserestricted'}) {
1318: my $new_assoc_course = $env{'form.new_courserestricted'};
1319: my $prev_courserestricted = $Apache::lonpublisher::metadatafields{'courserestricted'};
1320: if (($prev_courserestricted) &&
1321: ($prev_courserestricted ne $new_assoc_course)) {
1322: my $transfers = [];
1323: foreach my $key (keys(%env)) {
1324: if ($key =~ /^form\.transfer_(.+)$/) {
1325: push(@{$transfers},$1);
1326: }
1327: }
1328: if (@{$transfers} > 0) {
1329: &store_transferred_addedfields($fn,$uri,$transfers);
1330: }
1331: }
1332: }
1333: $r->print(<<ENDEDIT);
1334: <h1>$displayfile</h1>
1335: <form method="post" action="" name="defaultmeta">
1336: ENDEDIT
1337: my %lt=&fieldnames($file_type);
1338: my $output;
1339: my @fields;
1340: my $added_metadata_fields;
1341: my @added_order;
1342: if ($file_type eq 'groups') {
1343: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1344: 'course.'.$env{'request.course.id'};
1345: }
1346: if ((! $Apache::lonpublisher::metadatafields{'courserestricted'}) &&
1347: (! $env{'form.new_courserestricted'}) && (! $file_type eq 'groups')) {
1348: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1349: 'none';
1350: } elsif ($env{'form.new_courserestricted'}) {
1351: $Apache::lonpublisher::metadatafields{'courserestricted'}=
1352: $env{'form.new_courserestricted'};
1353: }
1354: if ($file_type eq 'portfolio' || $file_type eq 'groups') {
1355: if(exists ($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'})) {
1356: # retrieve fieldnames (in order) from the course restricted list
1357: @fields = (split(/,/,$env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'}));
1358: } else {
1359: # no saved field list, use default list
1360: @fields = ('author','title','subject','keywords','abstract',
1361: 'notes','lowestgradelevel',
1362: 'highestgradelevel','standards');
1363: if ($Apache::lonpublisher::metadatafields{'courserestricted'} =~ /^course\.($match_domain\_$match_courseid)$/) {
1364: my $assoc_crs = $1;
1365: $added_metadata_fields = &Apache::lonparmset::get_added_meta_fieldnames($assoc_crs);
1366: if ($env{'course.'.$assoc_crs.'.metadata.addedorder'}) {
1367: @added_order = split(/,/,$env{'course.'.$assoc_crs.'.metadata.addedorder'});
1368: }
1369: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.fieldlist'} = join(",",@fields);
1370: }
1371: }
1372: } else {
1373: @fields = ('author','title','subject','keywords','abstract','notes',
1374: 'copyright','customdistributionfile','language',
1375: 'standards',
1376: 'lowestgradelevel','highestgradelevel','sourceavail','sourcerights',
1377: 'obsolete','obsoletereplacement');
1378: }
1379: if (! $Apache::lonpublisher::metadatafields{'copyright'}) {
1380: $Apache::lonpublisher::metadatafields{'copyright'}=
1381: 'default';
1382: }
1383: if (($file_type eq 'portfolio') || ($file_type eq 'groups')) {
1384: if (! $Apache::lonpublisher::metadatafields{'mime'}) {
1385: ($Apache::lonpublisher::metadatafields{'mime'}) =
1386: ( $target=~/\.(\w+)$/ );
1387: }
1388: if (! $Apache::lonpublisher::metadatafields{'owner'}) {
1389: $Apache::lonpublisher::metadatafields{'owner'} =
1390: $env{'user.name'}.':'.$env{'user.domain'};
1391: }
1392: if (! $Apache::lonpublisher::metadatafields{'author'}) {
1393: $Apache::lonpublisher::metadatafields{'author'} =
1394: &Apache::loncommon::plainname($env{'user.name'},
1395: $env{'user.domain'});
1396: }
1397: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none') {
1398:
1399: if ($file_type eq 'portfolio') {
1400: $r->print(&mt('Associated with course [_1]',
1401: '<strong><a href="'.$uri.'?changecourse=true">'.
1402: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
1403: ".description"}.
1404: '</a></strong>').'<br />');
1405: } else {
1406: $r->print(&mt('Associated with course [_1]',
1407: '<strong>'.
1408: $env{$Apache::lonpublisher::metadatafields{'courserestricted'}.
1409: ".description"}.'</strong>').'<br />');
1410: }
1411: } else {
1412: $r->print('<a href="'.$uri.'?changecourse=true">'.&mt('This resource is not associated with a course.').'</a><br />');
1413: }
1414: }
1415: if (@added_order) {
1416: foreach my $field_name (@added_order) {
1417: push(@fields,$field_name);
1418: $lt{$field_name} = $$added_metadata_fields{$field_name};
1419: }
1420: } else {
1421: foreach my $field_name (keys(%$added_metadata_fields)) {
1422: push(@fields,$field_name);
1423: $lt{$field_name} = $$added_metadata_fields{$field_name};
1424: }
1425: }
1426: $output .= &Apache::loncommon::start_data_table();
1427: my $row_alt = 1;
1428: foreach my $field_name (@fields) {
1429: if (defined($env{'form.new_'.$field_name})) {
1430: my @values = &Apache::loncommon::get_env_multiple('form.new_'.$field_name);
1431: my $newvalue = '';
1432: foreach my $item (@values) {
1433: if ($item ne '') {
1434: $newvalue .= $item.',';
1435: }
1436: }
1437: $newvalue =~ s/,$//;
1438: $Apache::lonpublisher::metadatafields{$field_name}=$newvalue;
1439: }
1440: if ($Apache::lonpublisher::metadatafields{'courserestricted'} ne 'none'
1441: && exists($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'})) {
1442: # handle restrictions here
1443: if ((($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/active/) ||
1444: ($field_name eq 'courserestricted'))&&
1445: (!($env{$Apache::lonpublisher::metadatafields{'courserestricted'}.'.metadata.'.$field_name.'.options'} =~ m/deleted/))){
1446:
1447: $output .= "\n".&Apache::loncommon::start_data_table_row();
1448: $output .= ('<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
1449: &prettyinput($field_name,
1450: $Apache::lonpublisher::metadatafields{$field_name},
1451: 'new_'.$field_name,'defaultmeta',
1452: undef,undef,undef,undef,
1453: $Apache::lonpublisher::metadatafields{'courserestricted'}).'</td>');
1454: $output .= &Apache::loncommon::end_data_table_row();
1455: }
1456: } else {
1457:
1458: $output.=(&Apache::loncommon::start_data_table_row().'<td><span class="LC_metadata">'.$lt{$field_name}.':</span></td><td> '.
1459: &prettyinput($field_name,
1460: $Apache::lonpublisher::metadatafields{$field_name},
1461: 'new_'.$field_name,'defaultmeta').'</td>'.&Apache::loncommon::end_data_table_row());
1462:
1463: }
1464: }
1465: $output .= &Apache::loncommon::end_data_table();
1466: if ($env{'form.store'}) {
1467: my ($outcome,$result) = &store_metadata($fn,$uri,'store');
1468: $r->print($result);
1469: }
1470: $r->print($output.'<br /><input type="submit" title="Save Metadata" name="store" value="'.
1471: &mt('Save').'" />');
1472:
1473: if ($file_type eq 'portfolio' || $file_type eq 'groups') {
1474: my ($port_path,$group) = &get_port_path_and_group($uri);
1475: if ($group ne '') {
1476: $r->print('<input type="hidden" name="group" value="'.$group.'" />');
1477: }
1478: $r->print('<input type="hidden" name="currentpath" value="'.$env{'form.currentpath'}.'" />');
1479: $r->print('</form><br /><br /><form method="post" action="'.$port_path.'">');
1480: if ($group ne '') {
1481: $r->print('<input type="hidden" name="group" value="'.$group.'" />');
1482: }
1483: $r->print('<input type="hidden" name="currentpath" value="'.$path.'" />'.
1484: '<input type="submit" name="cancel" value="'.&mt('Discard Edits and Return to Portfolio').'" />');
1485: }
1486: }
1487:
1488: $r->print('</form>');
1489:
1490: return;
1491: }
1492:
1493: sub store_metadata {
1494: my ($fn,$uri,$caller) = @_;
1495: my $mfh;
1496: my $formname='store';
1497: my ($file_content,$output,$outcome);
1498: if (&Apache::loncommon::get_env_multiple('form.new_keywords')) {
1499: $Apache::lonpublisher::metadatafields{'keywords'} =
1500: join (',', &Apache::loncommon::get_env_multiple('form.new_keywords'));
1501: }
1502: foreach my $field (sort(keys(%Apache::lonpublisher::metadatafields))) {
1503: next if ($field =~ /\./);
1504: my $unikey=$field;
1505: $unikey=~/^([A-Za-z_]+)/;
1506: my $tag=$1;
1507: $tag=~tr/A-Z/a-z/;
1508: $file_content.= "\n\<$tag";
1509: foreach my $key (split(/\,/,$Apache::lonpublisher::metadatakeys{$unikey})) {
1510: my $value = $Apache::lonpublisher::metadatafields{$unikey.'.'.$key};
1511: $value=~s/\"/\'\'/g;
1512: $file_content.=' '.$key.'="'.$value.'"' ;
1513: }
1514: $file_content.= '>'.
1515: &HTML::Entities::encode
1516: ($Apache::lonpublisher::metadatafields{$unikey},'<>&"').
1517: '</'.$tag.'>';
1518: }
1519: if ($fn =~ m|^$Apache::lonnet::perlvar{'lonDocRoot'}/userfiles|) {
1520: my ($path, $new_fn);
1521: if ($fn =~ m|$match_name/groups/\w+/portfolio/|) {
1522: ($path, $new_fn) = ($fn =~ m|/(groups/\w+/portfolio.*)/([^/]*)$|);
1523: } else {
1524: ($path, $new_fn) = ($fn =~ m|/(portfolio.*)/([^/]*)$|);
1525: }
1526: ($outcome,my $result) =
1527: &store_portfolio_metadata($formname,$file_content,
1528: $path,$new_fn,$uri,$caller);
1529: $output .= $result;
1530: } else {
1531: if (! ($mfh=Apache::File->new('>'.$fn))) {
1532: $output .= '<p><font color="red">';
1533: if ($caller eq 'transfer') {
1534: $output .= &mt('Could not transfer data in added fields to notes');
1535: } else {
1536: $output .= &mt('Could not write metadata');
1537: }
1538: $output .= ', '.&mt('FAIL').'</font></p>';
1539: $outcome = 'fail';
1540: } else {
1541: print $mfh ($file_content);
1542: close($mfh);
1543: &update_metadata_table($uri);
1544: $output .= '<p><font color="blue">';
1545: if ($caller eq 'transfer') {
1546: $output .= &mt('Transferred data in added fields to notes');
1547: } else {
1548: $output .= &mt('Wrote Metadata');
1549: }
1550: $output .= ' '.&Apache::lonlocal::locallocaltime(time).
1551: '</font></p>';
1552: $outcome = 'ok';
1553: }
1554: }
1555: return ($outcome,$output);
1556: }
1557:
1558: sub store_transferred_addedfields {
1559: my ($fn,$uri,$transfers) = @_;
1560: foreach my $item (@{$transfers}) {
1561: $Apache::lonpublisher::metadatafields{'notes'} .=
1562: ' '.$item.' = '.$Apache::lonpublisher::metadatafields{$item};
1563: }
1564: my ($outcome,$output) = &store_metadata($fn,$uri,'transfer');
1565: if ($outcome eq 'ok') {
1566: foreach my $item (@{$transfers}) {
1567: delete($Apache::lonpublisher::metadatafields{$item});
1568: }
1569: }
1570: }
1571:
1572: sub store_portfolio_metadata {
1573: my ($formname,$content,$path,$new_fn,$uri,$caller) = @_;
1574: my ($outcome,$output);
1575: $env{'form.'.$formname}=$content."\n";
1576: $env{'form.'.$formname.'.filename'}=$new_fn;
1577: my $result =&Apache::lonnet::userfileupload($formname,'',$path);
1578: if ($result =~ /(error|notfound)/) {
1579: $output = '<p><font color="red">';
1580: if ($caller eq 'transfer') {
1581: $output .=
1582: &mt('Could not transfer data in added fields to notes');
1583: } else {
1584: $output .= &mt('Could not write metadata');
1585: }
1586: $output .= ', '.&mt('FAIL').'</font></p>';
1587: $outcome = 'fail';
1588: } else {
1589: &update_metadata_table($uri);
1590: $output = '<p><font color="blue">';
1591: if ($caller eq 'transfer') {
1592: $output .= &mt('Transferred data in added fields to notes');
1593: } else {
1594: $output .= &mt('Wrote Metadata');
1595: }
1596: $output .= ' '.&Apache::lonlocal::locallocaltime(time).
1597: '</font></p>';
1598: $outcome = 'ok';
1599: }
1600: return ($outcome,$output);
1601: }
1602:
1603: sub update_metadata_table {
1604: my ($uri) = @_;
1605: my ($type,$udom,$uname,$file_name,$group) =
1606: &Apache::lonnet::parse_portfolio_url($uri);
1607: $file_name =~ s/\.meta$//;
1608: my $current_permissions =
1609: &Apache::lonnet::get_portfile_permissions($udom,$uname);
1610: my %access_controls =
1611: &Apache::lonnet::get_access_controls($current_permissions,$group,
1612: $file_name);
1613: my $access_hash = $access_controls{$file_name};
1614: my $available = 0;
1615: if (ref($access_hash) eq 'HASH') {
1616: foreach my $key (keys(%{$access_hash})) {
1617: my ($num,$scope,$end,$start) =
1618: ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1619: if ($scope eq 'public' || $scope eq 'guest') {
1620: $available = 1;
1621: last;
1622: }
1623: }
1624: }
1625: if ($available) {
1626: my $result =
1627: &Apache::lonnet::update_portfolio_table($uname,$udom,
1628: $file_name,'portfolio_metadata',$group,'update');
1629: }
1630: }
1631:
1632:
1633: 1;
1634: __END__
1635:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>