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