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