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